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

📄 tbuffer.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
📖 第 1 页 / 共 2 页
字号:
					be opened.
				TBE_CANT_WRITE_TO_FILE - The file was opened, 
					but could not be written to.
*/

void TextBufferSaveToFile(text_buffer *textBuffer,
						  text_string fileName)
{
	FILE *outputFile = NULL;
	char *fileNameArray = NULL;
	int i;

	assert(textBuffer);
	assert(TextStringGetLength(fileName) > 0);

	if (textBuffer->isDirty)
	{
		if (TextStringCopyAsCharArray(
				fileName,
				&fileNameArray) == TSE_NO_ERROR)
		{
			// Open the file.
			outputFile = fopen(fileNameArray,"w");

			free(fileNameArray);
		}
		
		// If the file could be opened...
		if (outputFile)
		{
			text_string_error tStringError = TSE_NO_ERROR;
			text_string tempString;

			// While not at the end of the buffer and there is no error...
			for (i = 0; 
				 (i < textBuffer->totalRows) && 
					(textBuffer->errorStatus == TBE_NO_ERROR) &&
					(tStringError == TSE_NO_ERROR);
				  i++)
			{
				// Get text from the buffer.
				TextBufferGetRow(textBuffer,i,&tempString);

				// Write it to the file.
				tStringError = TextStringFPutString(tempString,
													outputFile);
			}

			if (tStringError != TSE_NO_ERROR)
			{
				textBuffer->errorStatus = TBE_CANT_WRITE_TO_FILE;
			}

			fclose(outputFile);
		}
		// Else the file could not be opened...
		else
		{
			// Set the error status.
			textBuffer->errorStatus = TBE_CANT_OPEN_FILE;
		}
	}
}

// End TextBufferSaveToFile
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	TextBufferLoadFromFile
Parameters:
	In:			fileName - Specifies the name of the file
					where the text will be saved.
	Out:		None.
	In/Out:		tBuffer - Contains a pointer to the text 
					buffer.
Return Values:	None.
Comments:		Applications invoke the TextBufferLoadFromFile()
				function to load the contents of a file into 
				the text buffer. The file can be on any 
				accessible storage medium for which the user 
				has read permission. All path names must be 
				in a form that is acceptable to the operating
				system being used. 

				If this function fails, it sets the text buffer's
				error status member to one of the following 
				values:
				TBE_OUT_OF_MEMORY - Memory could not be 
					allocated for the incoming text.
				TBE_CANT_ALLOCATE_BUFFER - Memory could not 
					be allocated for the text buffer.
*/

void TextBufferLoadFromFile(text_buffer *textBuffer,
							text_string fileName)
{
	FILE *inputFile = NULL;
	int i;
	text_string tempString;
	char *fileNameArray=NULL;

	assert(textBuffer);
	assert(TextStringGetLength(fileName) > 0);

	// If the file name can be converted to a char array...
	if (TextStringCopyAsCharArray(fileName,
								  &fileNameArray) == TSE_NO_ERROR)
	{
		// Open the file.
		inputFile = fopen(fileNameArray,"r");

		free(fileNameArray);
	}
	// Else the file name can't be converted to a char array...
	else
	{
		// Set an error condition.
		textBuffer->errorStatus = TBE_OUT_OF_MEMORY;
	}

	// If the file could be opened...
	if (inputFile)
	{
		text_string_error tStringError = TSE_NO_ERROR;

		// While not at end of file and there is no error...
		for (i=0;
			 (!feof(inputFile)) && (tStringError == TSE_NO_ERROR);
			 i++)
		{
			// Create a text_string.
			tempString = TextStringCreate(&tStringError);

			// If there was no error...
			if (tStringError == TSE_NO_ERROR)
			{
				// Read a line of text from the file.
				tStringError = TextStringFGetString(tempString,inputFile);
			}

			// Store the text_string in the buffer.
			TextBufferSetRow(textBuffer,i,tempString);
		}

		// If there was no error...
		if ((tStringError == TSE_NO_ERROR) && 
			(textBuffer->errorStatus == TBE_NO_ERROR))
		{
			// Set the length of the buffer.
			textBuffer->totalRows = i;
			textBuffer->isDirty = TRUE;
			fclose(inputFile);
		}
		// Else there was an error...
		else
		{
			textBuffer->errorStatus = TBE_CANT_ALLOCATE_BUFFER;
		}
	}
	// Else the file could not be opened...
	else
	{
		// Set the error status.
		textBuffer->errorStatus = TBE_CANT_OPEN_FILE;
	}
}

// End TextBufferLoadFromFile
//---------------------------------------------------------



//---------------------------------------------------------
/* 
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:	TextBufferGetError
Parameters:
	In:			tBuffer - Contains the address of the 
					text buffer 
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		Use this function to retrieve the current
				error status of a text buffer.
*/

text_buffer_error TextBufferGetError(text_buffer *tBuffer)
{
	assert(tBuffer);

	return(tBuffer->errorStatus);
}

//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	TextBufferClearError
Parameters:
	In:			tBuffer - Contains the address of the 
					text buffer 
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function resets the error status of a 
				text buffer to TBE_NO_ERROR.
*/

void TextBufferClearError(text_buffer *tBuffer)
{
	assert(tBuffer);

	tBuffer->errorStatus = TBE_NO_ERROR;
}

//---------------------------------------------------------



//---------------------------------------------------------
/* 
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;
	text_string_error tStringError = TSE_NO_ERROR;

	assert(tBuffer!=NULL);

	// Free each row.
	for (i=0;
		 (i<tBuffer->totalRows) && 
			(tStringError == TSE_NO_ERROR);
		 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:	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.
*/

static text_buffer_error AllocateRows(text_buffer *tBuffer,
									  int totalRows)
{
	int i;
	text_string *tempBuffer=NULL;
	text_buffer_error errorStatus = TBE_NO_ERROR;

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

	tempBuffer = (text_string *)realloc(
									tBuffer->allRows,
									totalRows*sizeof(text_string));
	if (tempBuffer)
	{
		text_string_error tStringError = TSE_NO_ERROR;

		// Initialze the new rows.
		for (i=tBuffer->totalRows;
			 (i<totalRows) && (tStringError == TSE_NO_ERROR);
			 i++)
		{
			tempBuffer[i] = TextStringCreate(&tStringError);
		}

		if (tStringError == TSE_NO_ERROR)
		{
			// Save a pointer to the new buffer.
			tBuffer->allRows = tempBuffer;

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

	return (errorStatus);
}

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

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

⌨️ 快捷键说明

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