tbuffer.c

来自「Software Development in C: A Practical A」· C语言 代码 · 共 252 行

C
252
字号
//---------------------------------------------------------
/*
File Name:	TBuffer.c
Comments:	This file contains the functions which 
			implement all of the valid operations on the 
			text_buffer type.
*/
//---------------------------------------------------------



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

#include <string.h>
#include "TBuffer.h"

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



//---------------------------------------------------------
/* 
Function Name:	TextBufferInitialize
Parameters:
	In:			None
	Out:		None.
	In/Out:		tBuffer - Contains the address of the 
					text buffer to initialize
Return Values:	None.
Comments:		This function sets the text buffer into a
				known state. It does this by initializing
				the top line to zero and setting the first
				character in each row to the null 
				character.
*/

void TextBufferInitialize(text_buffer *tBuffer)
{
	int i;

	// Initialize the first row to zero.
	tBuffer->topLine = 0;

	// For each row of the buffer...
	for (i=0;i<TBUFFER_LENGTH;i++)
	{
		/* Set the first character in each row to the
		null character. */
		tBuffer->allRows[i][0]='\0';
	}
}

// TextBufferInitialize
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	TextBufferSetRow
Parameters:
	In:			rowNumber - Specifies the index number of 
					the row to set.
				textString - Contains a string of 
					characters that will be copied into 
					the specified row.
	Out:		None.
	In/Out:		tBuffer - Contains the address of the 
					text buffer that the text string
					will be copied into.
Return Values:	None.
Comments:		Use this function to copy a string of 
				characters into a row in the text buffer.
*/

void TextBufferSetRow(int rowNumber,
					  const char * const textString,
					  text_buffer *tBuffer)
{
	// If the row is in range...
	if ((rowNumber >= 0) && (rowNumber<TBUFFER_LENGTH))
	{
		/* Copy the characters from the text string to the
		specified row. */
		strcpy(tBuffer->allRows[rowNumber],textString);
	}
}

// End TextBufferSetRow
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferGetRow
Parameters:
	In:			rowNumber - Specifies the index number of 
					the row to get.
				tBuffer - Contains the address of the 
					text buffer that the text string
					will be copied from.
	Out:		textString - Contains a string of 
					characters that the specified row will 
					be copied into.
	In/Out:		None.
Return Values:	None.
Comments:		Applications use this function to copy 
				the specified row from the text buffer
				into a string of characters.
*/

void TextBufferGetRow(int rowNumber,
					  char *textString,
					  const text_buffer * const tBuffer)
{
	// If the row is in range...
	if ((rowNumber>=0) && (rowNumber<TBUFFER_LENGTH))
	{
		/* Copy the characters from the specified row into 
		the text string.*/
		strcpy(textString,tBuffer->allRows[rowNumber]);
	}
}
// End TextBufferGetRow
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferScrollUp
Parameters:
	In:			linesToScroll - Contains the number of 
					lines to scroll up.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function scrolls the buffer up by 
				the number of lines specified in the 
				linestoScroll parameter. Scrolling the
				buffer up means moving the displayable
				area toward the beginning of the buffer.
				This is done by setting the top row of 
				the buffer.

				If scrolling the buffer results in the 
				top row of the buffer taking on a value
				that is less than zero, the top row is
				set to zero. 
*/

void TextBufferScrollUp(int linesToScroll,
					    text_buffer *tBuffer)
{
	// Move the top row up.
	tBuffer->topLine-=linesToScroll;

	// If the top row is less than zero...
	if (tBuffer->topLine < 0)
	{
		// Set the top row to zero.
		tBuffer->topLine=0;
	}
}

// End TextBufferScrollUp
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	TextBufferScrollDown
Parameters:
	In:			linesToScroll - Contains the number of 
					lines to scroll down.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function scrolls the buffer down by 
				the number of lines specified in the 
				linestoScroll parameter. Scrolling the
				buffer down means moving the displayable
				area toward the end of the buffer.
				This is done by setting the top row of 
				the buffer.

				If scrolling the buffer results in the 
				top row of the buffer taking on a value
				that is greater than the last line of 
				the buffer minus the number of rows in
				the on-screen text area, the top row is set
				to the last line of the buffer minus the 
				number of rows in the on-screen text area,
*/

void TextBufferScrollDown(int linesToScroll,
						  int displayAreaHeight,
						  text_buffer *tBuffer)
{
	// Move the top row down.
	tBuffer->topLine+=linesToScroll;

	/* If the top row is greater than the last line of 
	the buffer minus the number of rows in the on-screen text 
	area... */
	if (tBuffer->topLine > TBUFFER_LENGTH-displayAreaHeight)
	{
		/* Set the top row to the last line of the buffer 
		minus the number of rows in the on-screen text area. */
		tBuffer->topLine = TBUFFER_LENGTH-displayAreaHeight;
	}
}

// End TextBufferScrollDown
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	TextBufferGetTopLine
Parameters:
	In:			tBuffer - Contains a pointer to the text 
					buffer.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns the curren top row
				number.
Comments:		See Return Values.
*/

int TextBufferGetTopLine(const text_buffer * const tBuffer)
{
	// Return the top row number.
	return (tBuffer->topLine);
}

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



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

⌨️ 快捷键说明

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