📄 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 <stdio.h>
#include <stdlib.h>
#include "TBuffer.h"
// End Include files
//---------------------------------------------------------
//---------------------------------------------------------
// Constants
const int DEFAULT_ROW_ALLOCATION = 10;
// End Constants
//---------------------------------------------------------
//---------------------------------------------------------
// Prototypes
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->isDirty = FALSE;
}
// 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.
*/
buffer_error TextBufferSetRow(
int rowNumber,
const text_string * const textString,
text_buffer *tBuffer)
{
buffer_error errorStatus = TBE_NO_ERROR;
assert(tBuffer!=NULL);
assert(rowNumber >= 0);
// If the row is in range...
if (rowNumber<tBuffer->totalRows)
{
/* If the the characters from the text string can't
be copied to the specified row... */
if (TextStringSetFromTextString(
&tBuffer->allRows[rowNumber],
textString) == TSE_CANT_ALLOCATE_STRING)
{
errorStatus = TBE_CANT_ALLOCATE_BUFFER;
}
}
// Else there are not that many rows in the buffer...
else
{
// Allocate memory for the new row.
errorStatus = AllocateRows(tBuffer,rowNumber+1);
// If the memory could be allocated...
if (errorStatus==TBE_NO_ERROR)
{
/* If the the characters from the text string can't
be copied to the specified row... */
if (TextStringSetFromTextString(
&tBuffer->allRows[rowNumber],
textString) == TSE_CANT_ALLOCATE_STRING)
{
errorStatus = TBE_CANT_ALLOCATE_BUFFER;
}
}
}
if (errorStatus == TBE_NO_ERROR)
{
tBuffer->isDirty = TRUE;
}
return (errorStatus);
}
// 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.
*/
buffer_error TextBufferGetRow(
int rowNumber,
text_string *textString,
const text_buffer * const tBuffer)
{
buffer_error errorStatus = TBE_NO_ERROR;
assert((rowNumber>=0) && (rowNumber<tBuffer->totalRows));
assert(textString!=NULL);
assert(tBuffer!=NULL);
// If the source string is not empty...
if (TextStringGetLength(&tBuffer->allRows[rowNumber]) > 0)
{
/* If the the characters from the text string can't
be copied to the specified row... */
if (TextStringSetFromTextString(
textString,
&tBuffer->allRows[rowNumber]) != TSE_NO_ERROR)
{
errorStatus = TBE_CANT_ALLOCATE_BUFFER;
}
}
// Else the row of text is empty...
else
{
errorStatus = TextStringSetFromCharArray(textString,"");
}
return (errorStatus);
}
// 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(int linesToScroll,
text_buffer *tBuffer)
{
assert(tBuffer!=NULL);
assert(linesToScroll>0);
// 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.
displayAreaHeight - Specifies the number of
rows of text contained in the on-screen
display area.
Out: None.
In/Out: tBuffer - Contains the address of the
buffer to be scrolled.
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 displayable area, the top row is set
to the last line of the buffer minus the
number of rows in the displayable area,
*/
void TextBufferScrollDown(int linesToScroll,
int displayAreaHeight,
text_buffer *tBuffer)
{
assert(tBuffer!=NULL);
assert(linesToScroll>0);
assert(displayAreaHeight>0);
// 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 displayable
area... */
if (tBuffer->topLine >
tBuffer->totalRows-displayAreaHeight)
{
/* Set the top row to the last line of the buffer
minus the number of rows in the displayable area. */
tBuffer->topLine =
tBuffer->totalRows-displayAreaHeight;
}
/* If the current buffer size is less than the number
of lines in one screenful of text, it is possible for
this function to set the top line to a number less
than 0. Check for this and adjust it. */
if (tBuffer->topLine < 0)
{
tBuffer->topLine = 0;
}
}
// 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 current top row
number.
Comments: See Return Values.
*/
int TextBufferGetTopLine(const text_buffer * const tBuffer)
{
assert(tBuffer!=NULL);
// Return the top row number.
return (tBuffer->topLine);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -