⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tstring.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------
/*
File name:	Tstring.c
Comments:	This file contains the implementation of the 
			text_string type.
*/

//---------------------------------------------------------
// Include files

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "Tstring.h"

// End include files
//---------------------------------------------------------



//---------------------------------------------------------
/*
Function:		TextStringInitString

Parameters
	In:			None.
	Out:		theString - Contains a pointer to the 
					text_string to initialize.
	In/Out:		None.

Return Values:	This function does not return a value.

Comments:		Programs use this function to set a 
				text_string into a known state. If this
				function is not called before the 
				text_string is used, it is likely that 
				there will be a fatal program error.
*/

void TextStringInitString(text_string *theString)
{
	assert(theString!=NULL);

	theString->charArray = NULL;
	theString->charsAllocated = 0;
}

// End TextStringInitString
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringSetFromCharArray

Parameters
	In:			charArray - Contains a pointer to the 
					source array of characters.
	Out:		destinationString - Contains a pointer to
					the destination text_string.
	In/Out:		None.
Return Values:	Upon success, this function returns
				TSE_NO_ERROR. If it can't allocate memory
				for the characters in the destination 
				text_string, it returns
				TSE_CANT_ALLOCATE_STRING.
Comments:		This function copies the characters from 
				charArray into a text_string. If the 
				text_string already contains characters,
				the memory for those characters is freed.
				This function then allocates a block of
				memory that is large enough to hold the
				characters from charArray (plus the null
				character).
*/

text_string_error TextStringSetFromCharArray(
						text_string *destinationString,
					    const char * const charArray)
{
	int length=0;
	text_string_error errorStatus = TSE_NO_ERROR;

	assert(destinationString != NULL);

	// If the destination string contains characters...
	if (destinationString->charArray!=NULL)
	{
		// Free the memory for them.
		TextStringFree(destinationString);
	}

	// If the character array is not empty...
	if (charArray!=NULL)
	{
		// Get its length.
		length = strlen(charArray);
	}

	// If the length is greater than 0...
	if (length>0)
	{
		// Allocate memory for the characters.
		destinationString->charArray = 
			calloc(length+1,sizeof(char));

		// If the memory was allocated...
		if (destinationString->charArray)
		{
			// Copy characters into it.
			strcpy(destinationString->charArray,
				   charArray);

			// Set the length of the destination string.
			destinationString->charsAllocated = length+1;
		}
		// Else the memory was not allocated...
		else
		{
			errorStatus = TSE_CANT_ALLOCATE_STRING;
		}

	}

	return (errorStatus);
}

// End TextStringSetFromChars
//---------------------------------------------------------



//---------------------------------------------------------
/*
Function:		TextStringSetFromTextString

Parameters
	In:			sourceString - Contains a pointer to the 
					source text_string.
	Out:		destinationString - Contains a pointer to
					the destination text_string.
	In/Out:		None.
Return Values:	Upon success, this function returns
				TSE_NO_ERROR. If it can't allocated memory
				for the characters in the destination 
				text_string, it returns
				TSE_CANT_ALLOCATE_STRING.
Comments:		This function copies the characters from 
				the source text_string into the destination
				text_string. If the destination text_string 
				already contains characters, the memory for 
				those characters is freed. This function 
				then allocates a block of memory in the 
				destination text_string that is large enough 
				to hold the characters from the source 
				text_string (plus the null character).
*/

text_string_error TextStringSetFromTextString(
						text_string *destinationString,
					    const text_string * const sourceString)
{
	int length=0;
	text_string_error errorStatus = TSE_NO_ERROR;

	assert(destinationString != NULL);
	assert(sourceString != NULL);

	// If the destination string contains characters...
	if (destinationString->charArray!=NULL)
	{
		// Free the memory for them.
		TextStringFree(destinationString);
	}

	// If the source text_string is not empty...
	if ((sourceString->charArray!=NULL) && 
		(sourceString->charsAllocated!=0))
	{
		// Get its length.
		length = strlen(sourceString->charArray);
	}

	// If the length is greater than 0...
	if (length>0)
	{
		// Allocate memory for the characters.
		destinationString->charArray = 
			calloc(length+1,sizeof(char));

		// If the memory was allocated...
		if (destinationString->charArray)
		{
			// Copy characters into it.
			strcpy(destinationString->charArray,
				   sourceString->charArray);

			// Set the length of the destination string.
			destinationString->charsAllocated = length+1;
		}
		// Else the memory was not allocated...
		else
		{
			errorStatus = TSE_CANT_ALLOCATE_STRING;
		}

	}

	return (errorStatus);
}

// End TextStringSetString
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringSetCharacter

Parameters
	In:			theCharacter - Contains the character to
					copy into the text_string.
				characterIndex - Specifies the zero-based
					index number of the position in the
					destination string where the character
					will be copied.
	Out:		theString - Contains a pointer to the 
					destination text_string.
	In/Out:		None.
Return Values:	This function does not return a value.
Comments:		Programs call this function to set a 
				character in an existing position in a
				text_string. The memory for the character
				in the destination string must already be
				allocated before this function is invoked.
				It is not possible to use this function to
				set a character in a position that is beyond 
				the end of the string (after the null 
				character). This is true even if the memory
				for that position is already allocated. To
				add characters to the end of a string, call
				TextStringAppendCharacter().
*/

void TextStringSetCharacter(text_string *theString,
							char theCharacter,
							int characterIndex)
{
	int length;

	assert(theString!=NULL);

	length = strlen(theString->charArray);

	assert(characterIndex >= 0);
	assert((characterIndex == 0 && length == 0) ? 
				theString->charsAllocated > 0:
				characterIndex < length);

	// Set the character.
	theString->charArray[characterIndex]=theCharacter;
}

// End TextStringSetCharacter
//---------------------------------------------------------



//---------------------------------------------------------
/*
Function:		TextStringGetCharacter

Parameters
	In:			theString - Contains a pointer to the 
					source text_string.
				characterIndex - Specifies the zero-based
					index number of the position in the
					source text_string from which the 
					character will be copied.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns the character from 
				the location in theString that is specified
				by characterIndex.
Comments:		See Return Values.
*/

char TextStringGetCharacter(const text_string * const theString,
 						    int characterIndex)
{
	int length;

	assert(theString!=NULL);
	assert(theString->charArray!=NULL);

	length = strlen(theString->charArray);

	assert((characterIndex>=0) && (characterIndex<length));

	return (theString->charArray[characterIndex]);
}

// TextStringGetCharacter
//---------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -