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

📄 tstring.c

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

// End TextStringAppendCharacter
//---------------------------------------------------------



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

Parameters
	In:			theString - Contains the identifier of the
					source 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(text_string sourceString)
{
	internal_text_string *theString = NULL;
	int charsPrinted=0;
	
	assert(TextStringIsValid(sourceString));

	// Get the string from the instance list.
	theString = 
		(internal_text_string *)InstanceListGetItem(
									&stringList,
									sourceString);

	assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);

	// If the string contains printable characters...
	if (theString->charArray)
	{
		// Print them.
		charsPrinted = printf("%s",theString->charArray);
	}

	return (charsPrinted);
}

// End TextStringPrintString
//---------------------------------------------------------



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

Parameters
	In:			theString - Contains the identifier of the
					destination string.
	Out:		None.
	In/Out:		None.
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 destinationString)
{
	int inputStringLength;
	int destinationStringLength;
	char inputChar;
	internal_text_string *theString = NULL;

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

	// 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(destinationString,
								   inputChar,
								   inputStringLength);
		}
		// Else the destination string is full...
		else
		{
			// Append the character.
			TextStringAppendCharacter(destinationString,inputChar);
			destinationStringLength = 
				TextStringGetLength(destinationString);
		}
	}

	if (inputStringLength < destinationStringLength)
	{
		// Append a null character at the end.
		TextStringSetCharacter(destinationString,
							   '\0',
							   inputStringLength);
	}

	return (inputStringLength);
}

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



//---------------------------------------------------------
/*
Function:		TextStringFGetString

Parameters
	In:			theString - Contains the identifier of the
					destination string.
	Out:		None.
	In/Out:		None.
Return Values:	Upon success, this function returns 
				TSE_NO_ERROR. If it runs out of memory, it
				returns TSE_CANT_ALLOCATE_STRING.
Comments:		Programs call this function to get a
				text_string from the keyboard.
*/

text_string_error TextStringFGetString(text_string theString,
									   FILE *inputFile)
{
	text_string_error errorStatus = TSE_NO_ERROR;
	int i;
	char tempChar;
	boolean done;

	assert(TextStringIsValid(theString));
	assert(inputFile);

	// While the end of the line has not been reached...
	for (i=0,done = FALSE;!done;i++)
	{
		// Get a character from the file.
		tempChar = fgetc(inputFile);

		/* If not at the end of the file, and the character
		is not equal to the newline or end of file 
		characters... */
		if ((!feof(inputFile)) && 
			(tempChar != '\n') && 
			(tempChar != EOF) && 
			(errorStatus == TSE_NO_ERROR))
		{
			// If index number is less than the length of the string...
			if (i < TextStringGetLength(theString))
			{
				/* Store the character into the space that has already 
				been allocated. */
				errorStatus = TextStringSetCharacter(theString,tempChar,i);
			}
			// Else there are more characters than memory allocated...
			else
			{
				// Append the character to the end of the string.
				errorStatus = TextStringAppendCharacter(theString,tempChar);
			}
		}
		// Else there is no more input on the line...
		else
		{
			done = TRUE;
		}
	}
	return (errorStatus);
}

// End TextStringFGetString
//---------------------------------------------------------



//---------------------------------------------------------
/*
Function:		TextStringFPutString

Parameters
	In:			theString - Contains the identifier of the
					destination string.
	Out:		None.
	In/Out:		None.
Return Values:	Upon success, this function returns 
				TSE_NO_ERROR. If it runs out of memory, it
				returns TSE_CANT_ALLOCATE_STRING.
Comments:		Programs call this function to get a
				text_string from the keyboard.
*/

text_string_error TextStringFPutString(
						text_string theString,
						FILE *outputFile)
{
	internal_text_string *outputString = NULL;

	assert(outputFile);
	assert(TextStringIsValid(theString));

	// Get the string from the instance list.
	outputString = 
		(internal_text_string *)InstanceListGetItem(
									&stringList,
									theString);

	assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);

	// If the string is not empty...
	if ((outputString->charArray) && 
		(strlen(outputString->charArray) > 0))
	{
		// Write it to the file.
		fprintf(outputFile,"%s\n",outputString->charArray);
	}
	// Else the string is empty...
	else
	{
		// Write a blank line.
		fputc('\n',outputFile);
	}

	return (TSE_NO_ERROR);
}

// End TextStringFPutString
//---------------------------------------------------------



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

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

int TextStringGetLength(text_string sourceString)
{
	int length=0;
	internal_text_string *theString = NULL;

	assert(TextStringIsValid(sourceString));

	// Get the source string from the instance list.
	theString = 
		(internal_text_string *)InstanceListGetItem(&stringList,
												    sourceString);

	assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);

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

	return (length);
}

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



//---------------------------------------------------------
/*
Function:		TextStringCopyAsCharArray

Parameters
	In:			sourceString - Contains the identifier of 
					the source string.
	Out:		None.
	In/Out:		destinationArray Specifies the address of a
					pointer to characters. Upon return, 
					this variable will contain the address
					of a character array.
Return Values:	Upon success, this function returns 
				TSE_NO_ERROR. If the character array cannot
				be allocated, it returns 
				TSE_CANT_ALLOCATE_STRING.
Comments:		Applications use this function to copy the 
				characters from a text_string into a 
				character array. Functions pass in a pointer
				to a pointer to characters in the 
				destinationArray parameter. This function
				allocates enough memory to hold the characters
				in the string specified by sourceString. The
				characters are then copied into to allocated
				memory. 
				
				Programs calling this function must free the
				memory for the character array allocated by
				TextStringCopyAsCharArray(). If they do not, 
				there may be a memory leak.
*/

text_string_error TextStringCopyAsCharArray(
						text_string sourceString,
						char **destinationArray)
{
	text_string_error errorStatus = TSE_NO_ERROR;
	int stringLength;
	internal_text_string *source;

	assert(TextStringIsValid(sourceString));
	assert(destinationArray != NULL);
	assert(*destinationArray == NULL);

	// Get the source string from the instance list.
	source = 
		(internal_text_string *)InstanceListGetItem(
									&stringList,sourceString);

	assert(source != NULL);

	// Get the length of the source string.
	stringLength = TextStringGetLength(sourceString);
		
	// Allocate enough memory for the characters.
	*destinationArray = (char *)calloc(stringLength+1,
									   sizeof(char));

	// If the memory was allocated...
	if (*destinationArray != NULL)
	{
		/* Copy characters from the source string to 
		the destination character array. */
		strcpy(*destinationArray,source->charArray);
	}
	// Else the memory was not allocated...
	else
	{
		// Set the errorStatus.
		errorStatus = TSE_CANT_ALLOCATE_STRING;
	}

	return (errorStatus);
}

// TextStringCopyAsCharArray
//---------------------------------------------------------



//---------------------------------------------------------
/*
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)
{
	internal_text_string *textString = NULL;


	assert(theString >=0);

	// Get the source string from the instance list.
	textString = 
		(internal_text_string *)InstanceListRemoveItem(&stringList, 
													   theString);

	assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);

	// If there are characters in the string...
	if (textString->charArray != NULL)
	{
		// Free them.
		FreeCharacters(textString);
	}

	assert(textString->charsAllocated == 0);

	free(textString);
}

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



//---------------------------------------------------------
/*
Function:		FreeCharacters

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:		The FreeCharacters() function is visible
				only to the text_string functions. They call
				it to free the memory in an instance of an
				internal_text_string. It also resets the 
				other members of an internal_text_string
				structure to their default state. In essense,
				this function empties a string.
*/

static void FreeCharacters(internal_text_string *theString)
{
	assert(theString);

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

// End FreeCharacters
//---------------------------------------------------------

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

⌨️ 快捷键说明

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