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

📄 tstring.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
📖 第 1 页 / 共 2 页
字号:




//---------------------------------------------------------
/*
Function:		TextStringAppendCharacter

Parameters
	In:			theCharacter - Specifies the character to 
					append to the text_string.
	Out:		None.
	In/Out:		theString - Contains a pointer to the 
					destination text_string.
Return Values:	If it is successfull, this function returns
				TSE_NO_ERROR. Otherwise, it returns 
				TSE_CANT_ALLOCATE_STRING.
Comments:		This function appends a character to a
				text_string. The text_string may or may
				not already contain characters. If the
				text_string does not contain enough memory
				for the new character, more memory is 
				allocated. 
*/

text_string_error TextStringAppendCharacter(
						text_string *theString,
						char theCharacter)
{
	text_string_error errorStatus = TSE_NO_ERROR;
	int length=0;
	char *temp;

	assert(theString!=NULL);

	// If the string is not empty...
	if (theString->charArray != NULL)
	{
		// Get its length.
		length = strlen(theString->charArray);
	}

	// If the string has some room in it...
	if (length < theString->charsAllocated-1)
	{
		// Add the character.
		theString->charArray[length]=theCharacter;
		theString->charArray[length+1]='\0';
	}
	// Else the string is empty...
	else if (theString->charsAllocated == 0)
	{
		// Allocate space for the character & the '\0';
		theString->charArray = calloc(2,sizeof (char));
		if (theString->charArray != NULL)
		{
			theString->charArray[0]=theCharacter;
			theString->charArray[1]='\0';
			theString->charsAllocated = 2;
		}
		else
		{
			errorStatus = TSE_CANT_ALLOCATE_STRING;
		}
	}
	// Else the string is full...
	else
	{
		// Allocate more memory.
		temp = calloc(theString->charsAllocated+1,sizeof(char));

		// If the memory was allocated...
		if (temp!=NULL)
		{
			// Copy the string into the new memory.
			strcpy(temp,theString->charArray);

			// Append the character and the '\0'.
			temp[theString->charsAllocated-1]=theCharacter;
			temp[theString->charsAllocated]='\0';

			// Free the old memory.
			free(theString->charArray);

			// Set a pointer to the new memory.
			theString->charArray = temp;

			// Increase the allocation size.
			theString->charsAllocated++;
		}
		else
		{
			errorStatus = TSE_CANT_ALLOCATE_STRING;
		}
	}

	return (errorStatus);
}

// TextStringAppendCharacter
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringPrintString

Parameters
	In:			theString - Contains a pointer to the 
					source text_string.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns the number of 
				characters it prints to the screen.
Comments:		Programs call this function to print a
				text_string to the screen.
*/

int TextStringPrintString(const text_string * const theString)
{
	int charsPrinted=0;
	
	assert(theString != NULL);

	if (theString->charArray)
	{
		charsPrinted = printf("%s",theString->charArray);
	}

	return (charsPrinted);
}

// TextStringPrintString
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringScanString

Parameters
	In:			None.
	Out:		None.
	In/Out:		theString - Contains a pointer to the 
					source text_string.
Return Values:	This function returns the number of 
				characters it reads from the keyboard.
Comments:		Programs call this function to get a
				text_string from the keyboard.
*/

int TextStringScanString(text_string *theString)
{
	int inputStringLength;
	int destinationStringLength;
	char inputChar;

	// Get the length of the destination string.
	destinationStringLength = 
		TextStringGetLength(theString);

	// While the user types in characters...
	for (inputStringLength = 0;
		 ((inputChar = getchar()) != EOF) && 
		 (inputChar!= '\n');
		 inputStringLength++)
	{
		/* If the length of the input string is less than 
		the length of the destination string... */
		if (inputStringLength < destinationStringLength)
		{
			// Set the character.
			TextStringSetCharacter(theString,
								   inputChar,
								   inputStringLength);
		}
		// Else the destination string is full...
		else
		{
			// Append the character.
			TextStringAppendCharacter(theString,inputChar);
			destinationStringLength = 
				TextStringGetLength(theString);
		}
	}

	// Mare sure there's a null character at the end.
	theString->charArray[inputStringLength] = '\0';

	return (inputStringLength);
}

// End TextStringScanString
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringGetLength

Parameters
	In:			theString - Contains a pointer to the 
					text_string.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns the length of the 
				text_string.
Comments:		Applications use this function to retrieve
				the number of characters (not including the
				'\0') in a text_string.
*/

int TextStringGetLength(
		const text_string * const theString)
{
	int length=0;

	assert(theString != NULL);

	// If the string is not empty...
	if (theString->charArray != NULL)
	{
		// Get its length.
		length = strlen(theString->charArray);
	}

	return (length);
}

// TextStringGetLength
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringFree

Parameters
	In:			None.
	Out:		None.
	In/Out:		theString - Contains a pointer to the 
					text_string.
Return Values:	This function does not return a value.
Comments:		Applications use this function to free the
				memory used by the characters in a 
				text_string. If this function is not called
				before the text_string goes out of scope,
				a memory leak may occur.
*/

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

	if (theString->charArray != NULL)
	{
		free(theString->charArray);
		theString->charArray = NULL;
		theString->charsAllocated = 0;
	}

	assert(theString->charsAllocated==0);
}

// End TextStringFree
//---------------------------------------------------------




//---------------------------------------------------------
/*
Function:		TextStringToCharPointer

Parameters
	In:			theString - Contains a pointer to the 
					text_string.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns a pointer to the 
				character array in a text_string.
Comments:		Applications use this function to retrieve 
				a pointer to the character array in a 
				text_string. This enables the text_string
				type to be used with C Standard Library
				functions.
*/

char *TextStringToCharPointer(const text_string *theString)
{
	return (theString->charArray);
}

// end TextStringToCharPointer
//---------------------------------------------------------


// end TString.c
//---------------------------------------------------------

⌨️ 快捷键说明

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