tbuffer.c

来自「Software Development in C: A Practical A」· C语言 代码 · 共 695 行 · 第 1/2 页

C
695
字号

// End TextBufferGetTopLine
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferGetBufferLength
Parameters:
	In:			tBuffer - Contains a pointer to the text 
					buffer.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns the number of rows
				currently allocated to the text buffer.
Comments:		See Return Values.
*/

int TextBufferGetBufferLength(
		const text_buffer * const tBuffer)
{
	assert(tBuffer!=NULL);

	return(tBuffer->totalRows);
}

// End TextBufferGetBufferLength
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferFree
Parameters:
	In:			None.
	Out:		None.
	In/Out:		tBuffer - Contains a pointer to the text 
					buffer.
Return Values:	This function does not return a value.
Comments:		Applications use this function to free the
				memory associated with a text_buffer. If 
				this function is not called whenever a 
				text_buffer goes out of scope, there may 
				be a memory leak.
*/

void TextBufferFree(text_buffer *tBuffer)
{
	int i;

	assert(tBuffer!=NULL);

	// Free each row.
	for (i=0;i<tBuffer->totalRows;i++)
	{
		TextStringFree(&tBuffer->allRows[i]);
	}

	// Free the array of rows.
	if (tBuffer->allRows != NULL)
	{
		free(tBuffer->allRows);
		tBuffer->allRows = NULL;
		tBuffer->topLine = 0;
		tBuffer->totalRows = 0;
	}

	assert(tBuffer->totalRows==0);
}

// TextBufferFree
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferLoadFile
Parameters:
	In:			fileName - Specifies the name of the file 
					to load.
	Out:		None.
	In/Out:		tBuffer - Contains a pointer to the text 
					buffer.
Return Values:	If the file is loaded, this function 
				returns TBE_NO_ERROR. Otherwise, it returns
				TBE_CANT_LOAD_FILE.
Comments:		Use the TextBufferLoadFile() function to 
				load a file into a text buffer. The
				individual rows of text in the file can be
				any length allowed by the available memory. 
				If there is not enough memory to hold the 
				entire file, this function will return an 
				error. 
*/

buffer_error TextBufferLoadFile(text_buffer *tBuffer,
							    const text_string *fileName)
{
	buffer_error errorStatus = TBE_NO_ERROR;
	FILE *inputFile = NULL;
	int currentRow;
	int inputChar;
	text_string tempString;


	// Try to open the file.
	inputFile = fopen(TextStringToCharPointer(fileName),"r");

	// If the file was opened...
	if (inputFile)
	{
		TextStringInitString(&tempString);

		// While not at the end of the file and 
		// there is no error...
		for (currentRow=0;
			 (!feof(inputFile)) && 
				(errorStatus == TBE_NO_ERROR);
			 currentRow++)
		{
			// Read in a line of text.
			while (((inputChar = fgetc(inputFile)) != '\n') && 
				   (inputChar != EOF) && 
				   (!feof(inputFile)) && 
				   (errorStatus == TBE_NO_ERROR))
			{
				// Add the character to the file name.
				if (TextStringAppendCharacter(
						&tempString,
						(char)inputChar) != TSE_NO_ERROR)
				{
					errorStatus = TBE_CANT_LOAD_FILE;
				}
			}

			// Save the text in the buffer
			if ((errorStatus == TBE_NO_ERROR) && 
				(!feof(inputFile)) &&
				(TextBufferSetRow(currentRow,
								  &tempString,
								  tBuffer) != TSE_NO_ERROR))
			{
				errorStatus = TBE_CANT_LOAD_FILE;
			}
	
			// Blank out the temporary string.
			if ((errorStatus == TBE_NO_ERROR) && 
				(!feof(inputFile)))
			{
				TextStringSetCharacter(&tempString,'\0',0);
			}
		}

		TextStringFree(&tempString);

		fclose(inputFile);
	}
	else
	{
		errorStatus = TBE_CANT_LOAD_FILE;
	}
	
	// If the file was loaded...
	if (errorStatus == TBE_NO_ERROR)
	{
		// Mark the buffer as being dirty.
		tBuffer->isDirty = TRUE;
	}

	return (errorStatus);
}

// end TextBufferLoadFile
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferSaveFile
Parameters:
	In:			fileName - Specifies the name of the file 
					to saved.
	Out:		None.
	In/Out:		tBuffer - Contains a pointer to the text 
					buffer.
Return Values:	If the file is saved, this function 
				returns TBE_NO_ERROR. Otherwise, it returns
				TBE_CANT_SAVE_FILE.
Comments:		Use the TextBufferSaveFile() function to 
				save a text buffer into a file. 
*/

buffer_error TextBufferSaveFile(text_buffer *tBuffer,
							    const text_string *fileName)
{
	buffer_error errorStatus = TBE_NO_ERROR;
	FILE *outputFile = NULL;
	int currentRow;


	// If the buffer is dirty...
	if (tBuffer->isDirty)
	{
		// Try to open the file.
		outputFile = fopen(TextStringToCharPointer(fileName),"w");

		// If the file was opened...
		if (outputFile)
		{
			// While not at the last row of the buffer and 
			// there is no error...
			for (currentRow=0;
				 (errorStatus == TBE_NO_ERROR) &&
					(currentRow < tBuffer->totalRows);
				 currentRow++)
			{
				// If there is text on the line...
				if (TextStringGetLength(&(tBuffer->allRows[currentRow])) > 0)
				{
					// Write a line of text.
					fprintf(outputFile,
							"%s\n",
							tBuffer->allRows[currentRow]);
				}
				// Else the line is blank...
				else
				{
					// Print a newline.
					fprintf(outputFile,"\n");
				}

				// If there was an error...
				if (ferror(outputFile))
				{
					errorStatus = TBE_CANT_SAVE_FILE;
				}
			}

			// If the buffer was saved...
			if (errorStatus == TBE_NO_ERROR)
			{
				// Mark it as clean.
				tBuffer->isDirty = FALSE;
			}

			fclose(outputFile);
		}
		else
		{
			errorStatus = TBE_CANT_SAVE_FILE;
		}
	}

	return (errorStatus);
}

// end TextBufferSaveFile
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	AllocateRows
Parameters:
	In:			totalRows - Specifies the minimum number 
					of rows to allocate.
	Out:		None.
	In/Out:		tBuffer - Contains a pointer to the text 
					buffer.
Return Values:	This function returns the number of rows
				currently allocated to the text buffer.
Comments:		The AllocateRows() function is used only 
				by functions in this file. It increases the
				number of rows allocated to the array of 
				text strings in the buffer.

				To decrease the number of allocations,
				this function allocates increases the
				number of rows in the buffer by a 
				fixed-size block. Whenever more rows are
				requested than the buffer currently 
				contains, it increments the allocation size
				by the constant DEFAULT_ROW_ALLOCATION 
				until the buffer length is greater than or 
				equal to the number of rows requested.
				
*/

buffer_error AllocateRows(text_buffer *tBuffer,
						  int totalRows)
{
	int i;
	text_string *tempBuffer=NULL;
	int bufferSize = tBuffer->totalRows;
	buffer_error errorStatus = TBE_NO_ERROR;

	assert(tBuffer!=NULL);
	assert(totalRows>0);

	/* Increase the allocation size until it is greater 
	than the requested number of rows. */
	for (bufferSize = tBuffer->totalRows;
		 bufferSize < totalRows;
		 bufferSize += DEFAULT_ROW_ALLOCATION);

	tempBuffer = realloc(tBuffer->allRows,
						 bufferSize*sizeof(text_string));
	if (tempBuffer)
	{
		// Initialze the new rows.
		for (i=tBuffer->totalRows;i<bufferSize;i++)
		{
			TextStringInitString(&tempBuffer[i]);
		}

		// Save a pointer to the new buffer.
		tBuffer->allRows = tempBuffer;

		// Save the new buffer size.
		tBuffer->totalRows=bufferSize;
	}
	else
	{
		errorStatus = TBE_CANT_ALLOCATE_BUFFER;
	}
	return (errorStatus);
}

// End AllocateRows
//---------------------------------------------------------




// End TBuffer.c
//---------------------------------------------------------

⌨️ 快捷键说明

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