texteditor.cpp

来自「editor c++开发 课程设计要用到 不错的」· C++ 代码 · 共 146 行

CPP
146
字号
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

#include "TextEditor.h"


//---吃掉cin中空白字符的工具函数
void eatBlanks()
{
	char blank;
	while(cin.peek() == ' ')
		cin.get(blank);
}

//---构造函数定义
TextEditor::TextEditor(string inFileName,string outFileName)
{
	//inStream.open(inFileName.c_str());
	//outStream.open(outFileName.c_str());
	inStream.open(inFileName.data());
	outStream.open(outFileName.data());
	if ( !inStream.is_open() || !outStream.is_open())
	{
		//cout<<"Fail to Open file"<<endl;
		cerr<<"Fail to Open file"<<endl;	
		exit(-1);
	}
	else
	{
		cout<<"Open File Succseeful..\n";
	}

}

void TextEditor::run()
{
	showMunu();
	cout<<"Enter an editing command following each prompt >\n\n";
	getline(inStream,currentLine);
	cout<<"TEXT: "<<currentLine<<endl;
	char command;
	string str1,str2;
	for(;;)
	{
		if(inStream.eof()) break;
		cout << '>';
		cin >> command;
		cin.ignore(1,'\n');
		switch(toupper(command))
		{
		case 'I':
			eatBlanks();
			getline(cin,str1);
			cout << "Insert before what string : ";
			getline(cin,str2);
			insert(str1,str2);
			break;
		case 'D':
			eatBlanks();
			getline(cin,str1);
			erase(str1);
			break;
		case 'R':
			eatBlanks();
			getline(cin,str1);
			cout << "With What? ";
			getline(cin,str2);
			replace(str1,str2);
			break;
		case 'N':
			next();
			break;
		case 'Q':
			quit();
			break;
		default :
			cout << "\n*** Illegal command ***\n";
			showMunu();
			cout << "TEXT: "<<currentLine<<endl;
		}//End of switch
		if(!inStream.eof())
			cout << "TEXT: "<<currentLine<<endl;
	}//End of for
	cout<< "\n*** Editing complete ***\n";
}

//---显示command
void TextEditor::showMunu()
{
	//cout<<"showMunu"<<endl;
	cout<<"Editing commands are:\n"
		"I str: Insert string str before another string\n"
		"D str: Delete string str\n"
		"R str: Replace string str with another string\n"
		"N :	Get next line of text\n"
		"Q :	Quit editing\n";
}

//---插入 str1 在str2之前
void TextEditor::insert(string str1,string str2)
{
	size_t position = currentLine.find(str2);
	if( position != string::npos )
		currentLine.insert(position,str1);
	else
		cout<<"\""<< str2<<"\"" <<" not found\n";
}
//---清除str
void TextEditor::erase(string str)
{
	size_t position = currentLine.find(str);
	if (position != string::npos)
		currentLine.erase(position,str.length());
	else
		cout<<"\""<< str<<"\"" <<"not found\n";
}
//---用str2替换str1
void TextEditor::replace(string str1,string str2)
{
	size_t position = currentLine.find(str1);
	if (position != string::npos)
		currentLine.replace(position,str1.length(),str2);
	else
		cout<<"\""<< str1 <<"\"" <<" not found\n";
}
//---读取下一行
void TextEditor::next()
{
	outStream << currentLine<<endl;
	getline(inStream,currentLine);
	cout << "\nNext Line:\n";
}
//---退出程序
void TextEditor::quit()
{
	outStream << currentLine<<endl;
	for(;;)
	{
		getline(inStream,currentLine);
		if(inStream.eof()) break;
		outStream << currentLine <<endl;
	}//End of for
}

⌨️ 快捷键说明

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