/* editor2.cpp is a program that performs several basic text-editing
 * operations on lines of text read from a file; after editing has
 * been completed, each line is written to a new file whose name
 * is that of the input file with the suffix ".out" appended.
 *
 * Input (keyboard): names of files, editing commands
 * Input (file):     lines of text
 * Output (screen):  a menu of editing commands, unedited and edited
 *                   lines of text.
 * Output (file):    edited lines of text
 -------------------------------------------------------------------*/

#include <iostream.h>
#include <fstream.h>
#include <string>

int main()
{
   const char COMMAND_MENU[] =
         "Editing commands are:\n"
         " I(nsert)   : insert a substring\n"
         " D(elete)   : delete a substring\n"
         " R(eplace)  : replace a substring with another string\n"
         " L(ength)   : determine the length of current line\n"
         " P(osition) : find position of a substring\n"
         " N(ewLine)  : get next line of text\n"
         " Q(uit)     : quit editing\n";

   string inFileName,
          outFileName;
   cout << "Enter the name of the input file: ";
   getline(cin, inFileName);
   outFileName = inFileName;
   outFileName.append(".out");
   ifstream inFile(inFileName.data());
   ofstream outFile(outFileName.data());
   if (!inFile.is_open() || !outFile.is_open())
   {
      cout << "Error in opening files.";
      exit(-1);
   }

   cout << COMMAND_MENU << endl
        << "Enter a 1-character editing command following each prompt >\n";
   char command;               // editing command

   string line,                // line of text to be edited
          str1, str2, str3;    // strings used in editing
   int position,               // position of a string in line
       len;                    // length of some string
   char eol;                   // to gobble end-of-lines so getstring works
   
   getline(inFile, line);
   cout << line << endl;

   for (;;)
   {
      if (inFile.eof()) break;
      cout << '>';
      cin.get(command); cin.ignore(80, '\n');
      switch(toupper(command))
      {
         case 'L' : cout << "Length = " << line.length() << endl;
                    break;
         case 'P' : cout << "Position of? ";
		    getline(cin, str1);
		    position = line.find(str1);
                    if (position != string::npos)
		       cout << "   is " << 1 + position << endl;
		    else
		       cout << str1 << " not found\n";
		    break;
         case 'I' : cout << "Insert what? ";
		    getline(cin, str1);
		    cout << "Insert where? ";
		    cin >> position; cin.get(eol);
		    line.insert(position - 1, str1);
		    break;
         case 'D' : cout << "Delete where? ";
		    cin >> position; cin.get(eol);
		    cout << "How many characters? ";
		    cin >> len; cin.get(eol);
		    line.erase(position - 1, len);
		    break;
         case 'R' : cout << "Replace what? ";
		    getline(cin, str1);
	            position = line.find(str1);
		    if (position == string::npos)
                    {
		       cout << str1 << " not found\n";
		       break;
		     }
		    cout << "   With what? ";
		    getline(cin, str2);
		    line.replace(position, str1.length(), str2);
		    break;
         case 'N' : outFile << line << endl;
		    getline(inFile, line);
		    cout << "Next line:\n";
		    break;
         case 'Q' : outFile << line << endl;
		    for (;;)
                    {
		       getline(inFile, line);
		       if (inFile.eof()) break;
		       outFile << line << endl;
		    }
		    break;
         default :  cout << "\n*** Illegal command ***\n"
			 << COMMAND_MENU << line << endl;
      } // end of switch
      
      if (!inFile.eof())
	 cout << line << endl;
   }
   cout << "\n*** Editing complete ***\n";
}


