📄 tedit.c
字号:
Comments: The PrintHelp function prints a description
of each of the commands in the main menu.
*/
static void PrintHelp(void)
{
ClearScreen();
printf("%c Scroll up one line.\n",
SCROLL_LINE_UP_COMMAND);
printf("%c Scroll down one line.\n",
SCROLL_LINE_DOWN_COMMAND);
printf("%c Scroll up one page.\n",
SCROLL_PAGE_UP_COMMAND);
printf("%c Scroll down one page.\n",
SCROLL_PAGE_DOWN_COMMAND);
printf("%c Scroll to the beginning of the document.\n",
SCROLL_TO_BEGINNING);
printf("%c Scroll to the end of the document.\n",
SCROLL_TO_END);
printf("%c Edit text.\n",EDIT_TEXT_COMMAND);
printf("%c Load from file.\n",LOAD_FILE_COMMAND);
printf("%c Save to file.\n",SAVE_FILE_COMMAND);
printf("%c Help\n",HELP_COMMAND);
printf("%c Quit.\n",QUIT_COMMAND);
printf("\nPress Enter to go back to the main menu.\n");
GetUserCommand();
}
// End PrintHelp
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: GetUserCommand
Parameters:
In: None.
Out: None.
In/Out: None.
Return Values: This function returns a single-character
user command.
Comments: The GetUserCommand() function retrieves a
character command from the keyboard. The
user must type the character, followed by
the Enter key. If the Enter key is not
pressed, this function waits until it is.
After the command is retrieved, there may be
additional characters left in the keyboard
buffer. This function will pull them out and
throw them away.
*/
static char GetUserCommand(void)
{
char userInput;
// Get the user's command from the keyboard.
userInput = getchar();
userInput = toupper(userInput); // Convert to upper case
// Throw away extra input.
rewind(stdin);
// Return the command.
return (userInput);
}
// End GetUserCommand
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: EditBuffer
Parameters:
In: None.
Out: None.
In/Out: textBuffer - Contains a pointer to the
buffer to be updated.
Return Values: This function returns PE_CANT_SET_STRING
if it can't edit the line of text. If
it runs out of memory, this function returns
PE_CANT_ALLOCATE_MEMORY. Otherwise, it returns
PE_NO_ERROR.
Comments: The EditBuffer function enables the user to
enter a line of text into the text buffer.
Note that when the user enters a line number
of the buffer row to edit, the line number
will always be 1 greater than the actual
buffer row number. This is because the user
sees the buffer row numbers as beginning
with 1, while the actual buffer row numbers
begin with 0.
*/
static program_error EditBuffer(text_buffer *textBuffer)
{
int lineNumber=0;
int topLine;
program_error errorStatus = PE_NO_ERROR;
boolean gotValidInput = FALSE;
boolean done = FALSE;
text_string tempString;
text_string_error tStringErrorStatus = TSE_NO_ERROR;
char *tempCharPointer = NULL;
tempString = TextStringCreate(&tStringErrorStatus);
// Set the string to a line of blanks.
if (TextStringSetFromCharArray(
tempString,
BLANK_INPUT_STRING) != TSE_NO_ERROR)
{
errorStatus = PE_CANT_SET_STRING;
}
// If the string was properly set...
if (!errorStatus)
{
topLine = TextBufferGetTopLine(textBuffer);
do
{
// Prompt the user for the line to edit.
printf("Please input the buffer line ");
printf("number of the row of text to ");
printf("edit\n>>>");
// Get the user's input.
TextStringScanString(tempString);
if (TextStringCopyAsCharArray(
tempString,
&tempCharPointer) == TSE_NO_ERROR)
{
assert(tempCharPointer != NULL);
lineNumber = atoi(tempCharPointer);
}
/* Else memory for the user's input could not
be allocated... */
else
{
errorStatus = PE_CANT_ALLOCATE_MEMORY;
}
// If the user just pressed Enter...
if ((errorStatus == PE_NO_ERROR) &&
(tempCharPointer[0] == '\0'))
{
// It's time to quit.
done = TRUE;
}
// Else if the user typed valid input...
else if ((lineNumber >= topLine+1) &&
(lineNumber < topLine+1+TEXT_AREA_HEIGHT))
{
gotValidInput = TRUE;
}
// Else the user entered invalid input...
else
{
printf("\nError: ");
printf("That is not a valid line number.\n");
printf("Press Enter to continue. ");
getchar();
rewind(stdin);
}
} while ((!gotValidInput) && (!done));
// If the user typed valid input...
if (gotValidInput)
{
// Prompt the user for a line of text.
printf("Please type in a line of text\n");
printf(">>>");
// Get the user's input.
TextStringScanString(tempString);
/* The line number the user sees is one greater than the
actual line number. */
lineNumber--;
// Store the new text in the specified row.
TextBufferSetRow(textBuffer,lineNumber,tempString);
if (TextBufferGetError(textBuffer) != TBE_NO_ERROR)
{
errorStatus = PE_CANT_SET_STRING;
}
}
}
// If the temp string was used...
if (TextStringGetLength(tempString) > 0)
{
// Its memory must be released.
TextStringFree(tempString);
}
// If the temp character pointer was used...
if (tempCharPointer)
{
// Free the characters it points at.
free(tempCharPointer);
}
return (errorStatus);
}
// End EditBuffer
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: LoadFile
Parameters:
In: None.
Out: None.
In/Out: textBuffer - Contains a pointer to the
buffer to be loaded.
Return Values: This function does not return a value.
Comments: The LoadFile() function reads a file into
the specified text buffer. If the file
cannot be read, it prints an error message.
*/
static void LoadFile(text_buffer *textBuffer)
{
text_string fileName;
text_string_error tStringError = TSE_NO_ERROR;
text_buffer_error bufferError = TBE_NO_ERROR;
fileName = TextStringCreate(&tStringError);
// If the text_string could be created...
if (tStringError == TSE_NO_ERROR)
{
// Get a file name from the user.
if (GetFileName(&fileName) > 0)
{
// Load the file.
TextBufferLoadFromFile(textBuffer,fileName);
bufferError = TextBufferGetError(textBuffer);
// If the file was not loaded...
switch (bufferError)
{
// If the file could not be opened...
case TBE_CANT_OPEN_FILE:
printf("\nThe file could not be opened. ");
printf("Either it\nis not in the specified ");
printf("directory,\nor it does not exist.\n");
printf("Press Enter to continue.");
getchar();
rewind(stdin);
break;
// If there is no more memory...
case TBE_OUT_OF_MEMORY:
case TBE_CANT_ALLOCATE_BUFFER:
printf("\nThis program is out of memory. ");
printf("Press Enter to return to the main ");
printf("menu.\nAt the main menu, press" );
printf(" %c to quit.Press Enter to continue.",QUIT_COMMAND);
getchar();
rewind(stdin);
break;
}
}
// Else the user did not type in a file name...
else
{
printf("\nNo file name was entered. Press Enter to ");
printf("continue.");
}
}
// Else the text_string could not be created...
else
{
ClearScreen();
printf("This application is out of memory. Press Enter \n");
printf("to continue. At the main menu quit the program.\n");
getchar();
rewind(stdin);
}
}
// End LoadFile
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: SaveFile
Parameters:
In: None.
Out: None.
In/Out: textBuffer - Contains a pointer to the
buffer to be loaded.
Return Values: This function does not return a value.
Comments: The SaveFile() function writes the contents of
the buffer into the specified file. If the
file cannot be written, this function prints
an error message.
*/
static void SaveFile(text_buffer *textBuffer)
{
text_string fileName;
text_string_error tStringError = TSE_NO_ERROR;
fileName = TextStringCreate(&tStringError);
if (tStringError == TSE_NO_ERROR)
{
// Prompt the user for a file name.
GetFileName(&fileName);
// Save the file.
TextBufferSaveToFile(textBuffer,fileName);
}
else
{
ClearScreen();
printf("This application is out of memory. Press any key\n");
printf("to continue. At the main menu quit the program.\n");
getchar();
rewind(stdin);
}
}
// End SaveFile
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function Name: GetFileName
Parameters:
In: None.
Out: fileName - Contains a pointer to a
text_string. Upon completion, this
parameter will contain the name of the
file.
In/Out: None.
Return Values: This function returns the length of the file
name.
Comments: The GetFileName() function prompts the user
for a file name and reads the user's input from
the keyboard.
*/
static int GetFileName(text_string *fileName)
{
int fileNameLength;
assert(fileName);
assert(TextStringIsValid(*fileName));
// Prompt the user for a file name.
printf("Please enter a file name. >>>");
// Get the file name.
fileNameLength = TextStringScanString(*fileName);
return (fileNameLength);
}
// End GetFileName
//---------------------------------------------------------
// End TEdit.c
//---------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -