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

📄 tedit.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------
/*
File Name:	TEdit.c
Comments:	This file contains the functions which 
			implement the text editor.
*/
//---------------------------------------------------------




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

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Platform.h"
#include "TBuffer.h"
#include "MiscType.h"

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



//---------------------------------------------------------
// Constants

const int SCREEN_HEIGHT = 25;
const int TEXT_AREA_HEIGHT = 15;

#define SCROLL_LINE_UP_COMMAND		'U'
#define SCROLL_LINE_DOWN_COMMAND	'D'
#define SCROLL_PAGE_UP_COMMAND		'B'
#define SCROLL_PAGE_DOWN_COMMAND	'F'
#define SCROLL_TO_BEGINNING			'A'
#define SCROLL_TO_END				'Z'
#define EDIT_TEXT_COMMAND			'E'
#define LOAD_FILE_COMMAND			'L'
#define SAVE_FILE_COMMAND			'S'
#define HELP_COMMAND				'H'
#define QUIT_COMMAND				'Q'

#define BLANK_INPUT_STRING								\
			"                                        "

// End Constants
//---------------------------------------------------------



//---------------------------------------------------------
// Enumerated Types

typedef enum
{
	PE_NO_ERROR,
	PE_CANT_SET_STRING,
	PE_CANT_ALLOCATE_MEMORY
} program_error;

// End Enumerated Types
//---------------------------------------------------------



//---------------------------------------------------------
// Prototypes

static void ClearScreen(void);
static void DisplayText(const text_buffer * const tBuffer);
static void PrintMenu(void);
static void PrintHelp(void);
static char GetUserCommand(void);
static program_error MessageLoop(text_buffer *textBuffer);
static program_error EditBuffer(text_buffer *textBuffer);
static void LoadFile(text_buffer *textBuffer);
static void SaveFile(text_buffer *textBuffer);
static int GetFileName(text_string *fileName);

// End Prototypes
//---------------------------------------------------------




int main()
{
	text_buffer textBuffer;
	program_error errorStatus = PE_NO_ERROR;

	// Initialize the text buffer.
	TextBufferInitialize(&textBuffer);

	errorStatus = MessageLoop(&textBuffer);

	// If there was an error...
	if (errorStatus)
	{
		// Print an error message.
		printf("Fatal Error: Program aborting...\n");
	}

	TextBufferFree(&textBuffer);

	return ((int)errorStatus);
}




//---------------------------------------------------------
/* 
Function Name:	MessageLoop
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	This function returns PE_NO_ERROR if no error
				occurred. Otherwise, it returns the error 
				codes it receives from the functions it 
				calls.
Comments:		The MessageLoop() function is the main message 
				loop of the program. It displays the program's
				output and reacts to the user's input.
*/

static program_error MessageLoop(text_buffer *textBuffer)
{
	program_error errorStatus = PE_NO_ERROR;
	char userCommand;
	boolean done;

	// While the user does not want to quit...
	for (done=FALSE;(!done) && (!errorStatus);)
	{
		// Clear the screen.
		ClearScreen();

		// Display a page of text.
		DisplayText(textBuffer);

		// Display the menu.
		PrintMenu();

		// Get the user's command.
		userCommand = GetUserCommand();

		switch (userCommand)
		{
			// If the user wants to scroll up one line...
			case SCROLL_LINE_UP_COMMAND:
				// Scroll the buffer up one line.
				TextBufferScrollUp(textBuffer,1);
			break;

			// If the user wants to scroll up one screenful...
			case SCROLL_PAGE_UP_COMMAND:
				// Scroll the buffer up one screenful.
				// Leave an overlap of one line.
				TextBufferScrollUp(textBuffer,TEXT_AREA_HEIGHT-1);
			break;

			// If the user wants to scroll down one line...
			case SCROLL_LINE_DOWN_COMMAND:
				// Scroll the buffer down one line.
				TextBufferScrollDown(textBuffer,1,TEXT_AREA_HEIGHT);
			break;

			// If the user wants to scroll down one screenful...
			case SCROLL_PAGE_DOWN_COMMAND:
				// Scroll the buffer down one screenful.
				TextBufferScrollDown(textBuffer,
									 TEXT_AREA_HEIGHT-1,
									 TEXT_AREA_HEIGHT);
			break;

			// If the user wants to edit text...
			case EDIT_TEXT_COMMAND:
				errorStatus = EditBuffer(textBuffer);
			break;

			/* If the user wants to scroll to the 
			beginning of the buffer... */
			case SCROLL_TO_BEGINNING:
				TextBufferScrollToBeginning(textBuffer);
			break;

			/* If the user wants to scroll to the end of the 
			buffer... */
			case SCROLL_TO_END:
				TextBufferScrollToEnd(textBuffer);
			break;

			// If the user wants to load a file...
			case LOAD_FILE_COMMAND:
				LoadFile(textBuffer);
			break;

			// If the user wants to save a file...
			case SAVE_FILE_COMMAND:
				SaveFile(textBuffer);
			break;

			// If the user needs help...
			case HELP_COMMAND:
				PrintHelp();
			break;

			// If the user wants to quit...
			case QUIT_COMMAND:
				// Set the loop control variable to exit.
				done = TRUE;
			break;

			// Else the command is not recognized...
			default:
				// Print an error message.
				printf("Command not recognized. ");
				printf("Press Enter to continue.");
				getchar();
				rewind(stdin);
			break;
		}
	}

	return (errorStatus);
}

// End MessageLoop
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	ClearScreen
Parameters:
	In:			None.
	Out:		None. 
	In/Out:		None.
Return Values:	None.
Comments:		This function clears the screen. It uses 
				conditional compilation to create versions
				specific to MS DOS/Windows and Unix/Linux.
*/

static void ClearScreen(void)
{
#if defined (PLATFORM_MSDOS)

	system("cls");

#elif defined (PLATFORM_UNIX)

	system("clear");

#else

	int i;

	// For each line on the screen...
	for (i=0;i<SCREEN_HEIGHT;i++)
	{
		// Print a newline.
		printf("\n");		
	}

#endif
}

// End ClearScreen
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	DisplayText
Parameters:
	In:			tBuffer - Specifies a pointer to the text 
					buffer that will be displayed on the 
					screen.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function starts with the current top 
				row in the text buffer and prints 
				successive rows of text from the buffer 
				to the screen. It stops when it reaches 
				the maximum number of rows to be displayed.

				Note that, to make it easier to see what's
				happening with the output, this function 
				prints the buffer line number before each row
				of text. The user sees the the buffer line 
				numbers as beginning with 1 rather than 0. 
				This is more intuitive for the user.
*/

static void DisplayText(const text_buffer * const tBuffer)
{
	int i,length;
	text_string tempString;
	int topRow;

	topRow = TextBufferGetTopLine(tBuffer);
	length = TextBufferGetBufferLength(tBuffer);

	// For each row to be displayed...
	for (i=0;i<TEXT_AREA_HEIGHT;i++)
	{
		// Print the buffer line number.
		printf("%d",topRow+i+1);

		if (topRow+i+1 < 10)
		{
			printf("    ");
		}
		else
		{
			printf("   ");
		}

		// If the row number is valid...
		if ((length > 0) && (topRow+i < length))
		{
			// Get a row of text.
			TextBufferGetRow(tBuffer,topRow+i,&tempString);

			// Print it to the screen.
			TextStringPrintString(tempString);
		}
		printf("\n");
	}
}

// End DisplayText
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	PrintMenu
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		The PrintMenu function prints the menu of
				text editor commands to the screen.
*/


static void PrintMenu(void)
{
	// Print the commands recognized by the text editor.
	printf("Press one of the following keys to issue a ");
	printf("command, then press the Enter key.\n");
	printf("%c, ",SCROLL_LINE_UP_COMMAND);
	printf("%c, ",SCROLL_LINE_DOWN_COMMAND);
	printf("%c, ",SCROLL_PAGE_UP_COMMAND);
	printf("%c, ",SCROLL_PAGE_DOWN_COMMAND);
	printf("%c, ",SCROLL_TO_BEGINNING);
	printf("%c, ",SCROLL_TO_END);
	printf("%c, ",EDIT_TEXT_COMMAND);
	printf("%c, ",LOAD_FILE_COMMAND);
	printf("%c, ",SAVE_FILE_COMMAND);
	printf("%c, ",QUIT_COMMAND);
	printf("Or Press %c for Help\n",HELP_COMMAND);
	printf("COMMAND>>>");
}

// End PrintMenu
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	PrintHelp
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	None.

⌨️ 快捷键说明

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