Object Oriented Programming in C++ - Lab 6

 

In this lab you will write a simple line editor. An editor is a program that allows a user to modify a file. Some editors, such as emacs, are screen editors, because they display a window containing the file, and the user can move the cursor around the window and modify the contents of the file. Other editors, such as the Unix ed editor, are line editors. These (usally older) editors do not display a window containing the file. The user edits the file by working on one line at a time.

Since a line editor can only change one line at a time, the current line is the line that all edit commands will modify. When the user wants to edit a different line they need to enter a command which will change the current line. The user modifies the file by entering commands to perform operations such as "delete the current line", "replace the current line with this new line", "add the following characters to the current line", etc.

When the line editor starts, the current line is set to the first line of the file.

You will create a class for the line editor. The data members of the line editor are an array of string objects (each string will hold one line of the file), an int to hold the number of lines in the file, an int to hold the subscript of the current line, and a C-style string to hold the name of the file being edited. You can assume that there will be at most 100 lines in any file. Note that when we talk about line numbers in the file, the first line in the file is line 1. Also note that when we talk about positions within a string object, the first character is in position 0.

The member functions of the line editor are:

default constructor
This function initializes the current line and the number of lines in the file to zero. The array of strings and the name of the file are not initialized.
copy constructor
This function copies the data members from the parm into the invoking object.
open
This private method has a parm for the name of the file to be edited. This parm is a C-style string. If the file doesn't exist or can't be read, open returns false. Otherwise, open reads the given file into the array of strings, sets the current line to line 1, copies the name of the file into the data member for the file name, sets the number of lines in the file, closes the file, and returns true.
next
This private method has no parms and returns void. It moves the current line down one line. If the current line is already at the end of the file, it does not change the current line. The resulting current line is printed on the screen by the next method.
move
This private method has one int parm (a line number) and returns void. The function changes the current line to the line number passed. If the line number passed is invalid, the current line should not be changed. The resulting current line is printed on the screen.
del
This private method deletes the current line by shifting all lines that follow the deleted line up in the array. This will fill in the array position that was used by the deleted line. The new current line is the line after the deleted line, unless the deleted line was the last line of the file. In that case, the new current line is the new last line of the file.
replace
This private method has one parm, which is a string. The function replaces the current line with the given string, then prints the new line on the screen. The current line is not changed.
insert
This private method has two parms, an int and a string. The function inserts the given string into the current line at the position given. For example, if the current line contains "software and conferences", and the insert function is called with parms 4 and "discount", the current line will be changed to "softdiscountware and conferences". After inserting, print the current line.
take
This private method has two int parms, start and end. It takes all characters from start to end out of the current line. For example, if the current line contains "software and conferences", and start is 7 and end is 12, the current line is changed to "softwarconferences". If the start and end are not valid (start > end or the current line isn't long enough to contain them both) then the function will not change the current line. After taking, print the current line.
save
This private method has no parms and returns void. The function opens the file being edited as an output file, writes out the current version of the file being edited, and closes the file.
save
This private method returns void and has one parm, which is a C-style string. The function opens an output file with the given name, writes out the current version of the file being edited, saves the new filename in the data member for the filename, and closes the file.
run
The run method processes editing commands from the user until the user wants to quit. The run method has one parm, the name of the file to be edited, which is a C-style string. The run method operates as follows:
  1. Call the open method to read in the file to be edited.

  2. Process editing commands until the user wants to quit editing. When the user asks to quit, prompt the user to see whether the save method should be executed to save the edited file. The editing commands are shorter versions of the editor method names, to make it easier for the user. For each editing command, read any additional information that is necessary, then call the corresponding line editor method.

  3. m x
    for move (x is the line number of the new current line)
    n
    for next
    d
    for del
    r newline
    for replace (newline is the string that should replace the current line)
    i x newchars
    for insert (x is the position for inserting, and newchars is the string that should be inserted into the current line)
    t x y
    for take (x is the first position, and y is the last position to be taken from the current line)
    s
    for save (save the file in the name given by the filename data member)
    s fname
    for save (fname is a C-style string that contains the name of the file to use for the save)

Create client code to invoke your line editor. The client code should create a line editor object and call the run method.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002