📄 tbuffer.c
字号:
//-----------------------------------------------------------
/*
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 "assert.h"
#include "stdlib.h"
#include "TBuffer.h"
//End Include files
//------------------------------------------------------------
//------------------------------------------------------------
//prototypes
static text_buffer_error AllocateRows(text_buffer *tBuffer,
int totalRows);
//End prototypes
//------------------------------------------------------------
//------------------------------------------------------------
/*
Function Name: TextBufferInitialize
Parameters:
In: None
Out: tBuffer - Contains the address of the
text buffer to initialize.
In/Out: None
Return Values: None.
Comments: This function sets the text buffer into a
known state. If this function is not called
before the text_buffer is used, it is
likely that there will be a program error.
*/
void TextBufferInitialize(text_buffer *tBuffer)
{
assert(tBuffer!=NULL);
//Initialize the first row to zero.
tBuffer->topLine = 0;
tBuffer->allRows = NULL;
tBuffer->totalRows = 0;
tBuffer->errorStatus = TBE_NO_ERROR;
}
// 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.
If this function fails, it sets the text
buffer's error status to
TBE_CANT_ALLOCATE_BUFFER,which indicates
that there is not enough memory to copy
the characters from the string into
the specified row.
*/
void TextBufferSetRow(text_buffer * tBuffer,
int rowNumber,
text_string textString)
{
text_buffer_error errorStatus = TBE_NO_ERROR;
assert(tBuffer!=NULL);
assert(rowNumber >= 0);
//If the row is in range...
if (rowNumber < tBuffer->totalRows)
{
/*If the characters from the text string can't
be copied to the specified row...*/
if (TextStringSetFromTextString(
tBuffer->allRows[rowNumber],
textString) == TSE_CANT_ALLOCATE_STRING)
{
tBuffer->errorStatus = TBE_CANT_ALLOCATE_BUFFER;
}
}
//Else there are not that many rows in the buffer...
else
{
//Allocate memory for the new row.
tBuffer->errorStatus = AllocateRows(tBuffer,rowNumber+1);
//If the memory could be allocated...
if (tBuffer->errorStatus==TBE_NO_ERROR)
{
/*If the characters from the text string can't
be copied to the specified row...*/
if (TextStringSetFromTextString(
tBuffer->allRows[rowNumber],
textString) == TSE_CANT_ALLOCATE_STRING)
{
tBuffer->errorStatus = TBE_CANT_ALLOCATE_BUFFER;
}
}
}
}
//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 retrieved from.
Out: textString - Upon return, will contain the
text_string in the specified row.
In/Out: None.
Return Values: None.
Comments: This function retrieves the text_string
contained in the specified row number. It
does not create an additional instance of
the string. Nor does it copy any characters.
*/
void TextBufferGetRow(const text_buffer * const tBuffer,
int rowNumber,
text_string *textString)
{
assert((rowNumber >= 0) && (rowNumber < tBuffer->totalRows));
assert(textString!=NULL);
assert(tBuffer!=NULL);
*textString = tBuffer->allRows[rowNumber];
}
//End TextBufferGetRow
//---------------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: TextBufferScrollUp
Parameters:
In: linesToScroll - Contains the number of
lines to scroll up.
Out: None.
In/Out: tBuffer - Contains the address of the
buffer to be scrolled.
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(text_buffer *tBuffer,
int linesToScroll)
{
assert(tBuffer!=NULL);
assert(linesToScroll>0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -