COSC 071: Computer Science I

Project 5: A simple line editor
Spring 2000

Due: May 3 @ 5 P.M.
12 points

You're fed up with vi and pico and have decided to write your own editor. Using a doubly linked list of strings (referred to as the editor buffer), implement a simple line editor that interprets and executes the following commands:

Command  Function
l Prompts the user for a filename and loads the file into the editor buffer.
w Writes the buffer contents to disk.
e Exits the editor and writes the buffer contents to disk.
q Quits the editor without saving the buffer contents.
a Appends a new line after the current line.
i Inserts a new line before the current line.
c Changes the current line.
d Deletes the current line.
f Searches for the string from the current line forward.
j Moves down one line.
k Moves up one line.
h Displays a help screen listing commands and their function.
p Prints the current buffer contents. The current line is marked with an asterisk. Lines are numbered consecutively.

Don't forget to handle the case of when the user creates a new file. That is, if a new file is created (one is not loaded from disk), then the editor should prompt the user for a name when the user executes the write or exit command.

You'll probably need three classes for this project. The first one is the Line class, a self-referential class. It has a string object and pointers to the previous and next lines as private data members, as well as the usual accessor and observer methods.

class Line {
  public:
    Line();
    Line(string);
    void set(string);
    string get();
    void setNextPtr(Line *);
    void setPrevPtr(Line *);
    Line *getNextPtr();
    Line *getPrevPtr();

  private:
    string contents;
    Line *prev, *next;
}; // Line class
The second class is the Buffer class. It is a container class and keeps a pointer to the current line and a pointer to head of the list of lines. It implements all of the list manipulation functions, such as inserting and deleting.
class Buffer {
  public:
    Buffer();
    bool empty();
    void load(string);
    void save(string);
    void append(string);
    void insert(string);
    void change(string);
    void remove();
    void nextLine();
    void prevLine();
    void find(string);
    void print();
    ~Buffer();

  private:
    Line *head;
    Line *current;
}; // Buffer class
The third class is the Editor class and is responsible for managing all of the user's interaction. This generally involves getting a command from the user and making the right method call to the Buffer object.
class Editor {
  public:
    Editor();
    void EventLoop();
    ~Editor();

  private:
    Buffer buffer;
    string filename;
    bool saved;

    void printMenu();
}; // Editor class
Now the main function simply looks like:
int main()
{
  Editor e;

  e.EventLoop();
  return 0;
} // main

You must use this high-level design, although you can add data members or methods to the classes, if required. You cannot use the vector container, or some other container class from the STL.

Here's a sample run. Bold-faced characters are those typed by the user.

Sample Run:

gusun% a.out
:p
No lines in buffer
:l
Enter filename: testfile
:p
*1: This is a test
 2: file of text that
 3: can be changed.
:j
:p
 1: This is a test
*2: file of text that
 3: can be changed.
:a
Appended text
:p
 1: This is a test
 2: file of text that
*3: Appended text
 4: can be changed.
:k
:i
Inserted text
:p
 1: This is a test
*2: Inserted text
 3: file of text that
 4: Appended text
 5: can be changed.
:j
:c
Changed text
:p
 1: This is a test
 2: Inserted text
*3: Changed text
 4: Appended text
 5: can be changed.
:j
:j
:d
:p
 1: This is a test
 2: Inserted text
 3: Changed text
*4: Appended text
:w
'testfile' written to disk
:c
Changed again
:e
'testfile' written to disk
gusun% cat testfile
This is a test
Inserted text
Changed text
Changed again
gusun% 

Instructions for Submission: At the top of the file containing your source code, place the following header comment, with the appropriate modifications:

//
// Project 5
// Name: <your name>
// SID: <student ID>
// E-mail: <e-mail address>
// Instructor: Maloof
// TA: <TA's name>
// COSC 071-<section number>
//
// Description: <Describe your program>
//

All programs must run under UNIX and must compile using GNU g++. When you are ready to submit your program for grading, e-mail it to your TA using the last four digits of your student ID and the suffix ``.cc'' as the subject line.

For example, if the last four digits of your student ID is 1234, the name of your source file is proj1.cc, and your TA's e-mail address is ``imagoodta@georgetown.edu'', then you would type at the Unix prompt:

gusun% mailx -s "1234.cc" imagoodta@georgetown.edu < proj1.cc
You are executing the mailx command. The -s option indicates that the string "1234.cc" is the subject heading. imgagoodta@georgetown.edu is the address to which the mail will be sent. The part ``< proj1.cc'' takes your source file and directs it into the mailx command.

You can also use pine to submit your project. After filling in the To and Subject fields, type the ^R command in the MESSAGE TEXT screen. Pine will ask for a file name, which it will then load as your message text. Type ^X to send the e-mail.

You must submit your project by e-mail before 5:00 P.M. on the due date.

If you need to include a message to you TA about your submission, then type the message as a comment in the program.

Once you've submitted your project, it is important to keep an electronic copy that preserves the modification date and time. If we lose your project or the e-mail system breaks, then we will need to look at the modification date and time of your project to ensure that you submitted it before it was due.