CS 215 ­ Fundamentals of Programming II Spring 2011­ Project 3 30 points Out: February 11, 2011

advertisement
CS 215 ­ Fundamentals of Programming II
Spring 2011­ Project 3
30 points
Out: February 11, 2011
Due: February 21, 2011 (Monday)
Note that this assignment is worth 50% more than the previous projects. It is also about 50% larger than the previous projects, so it is due in 10 days rather than 7 days. Do not waste the extra days...
A line editor is a text editor that operates on lines of a textfile. A line editor manipulates text one line at a time. The editor keeps track of a "current line" at all times, and the commands of the editor are performed relative to this current line. In the past, this type of editor was necessary for printing terminals and dumb video terminals where one could not move around on a screen. Now such editors are used in batch mode where editing commands are read from a file and applied to a target file. For example, source code patches often are distributed this way.
Your assignment is to write a Document class (specification given below) that supports line editing of a textfile and to write a (main) program that allows a user to interactively edit a textfile.
Specifications for Document Class Attributes Object
Type
Name
vector of lines
vector<string>
lines
index of current line
size_t
curr
The Document class stores a document as a vector of lines (i.e., strings that may contain non­newline whitespace). It uses an index, curr, to keep track of the current line that its operations may act upon, similarly to the sequence container covered in Lecture 11 and Homework 6. Unlike the sequence container, there is always a valid current line index unless the document is empty. Minimally, a document might look like the following:
curr
1
lines
[0] "line 1"
[1] "line 2"
[2] "line 3"
Document d;
02/10/2011
Page 1 of 7
This document consists of the three lines:
line 1
line 2
line 3
and the current line is "line 2". You may use these attributes somewhat differently than shown, but the data storage must be a vector and the indicator of the current line must be an index into that vector. Adding additional attributes to aid in implementing the Document operations must receive prior approval from the instructor.
Operations Default constructor ­ creates an empty document.
●
Analysis ­ no objects
Explicit­value constructor ­ creates a document from an input stream. Assumes the input stream is open and valid. Each line of the stream is stored as a line of the document. The current line is set to the first line.
●
Analysis Objects
Type
Movement
Name
input stream
istream
received & passed back
in
IsEmpty ­ returns true if the document has no lines; false otherwise
●
Analysis Objects
Type
Movement
Name
boolean result
bool
returned
­­­­­
Insert ­ inserts a line in front of the current line; the inserted line becomes the current line. If the document is empty, the line becomes the first line in the document.
●
Analysis Objects
Type
Movement
Name
line to insert
string
received
newLine
Append ­ inserts a line at the end of the document making it the current line. If the document is empty, the line becomes the first line in the document.
●
Analysis Objects
Type
Movement
Name
line to insert
string
received
newLine
02/10/2011
Page 2 of 7
Replace ­ replaces the contents of the current line. If the document is empty, this operation is ignored.
●
Analysis Objects
Type
Movement
Name
replacement line
string
received
newLine
Delete ­ removes the current line from the document; the current line becomes the next line after the deleted line unless the last line is deleted, then the last line becomes the current line. If the document is empty, this operation is ignored.
●
Analysis ­ No objects
Find ­ finds the first line in the document containing the target string (as a substring) and makes it the current line. If no line contains the target string, then the current line remains the same. Returns true if a matching line is found; false otherwise.
●
Analysis Objects
Type
Movement
Name
target string
string
received
target
boolean result
bool
returned
­­­­­
SetCurrent ­ sets the current line (index) to the nth line from the beginning of the document with the first line numbered 1. If the document has fewer than n lines or n < 1, the current line remains the same. Returns true when successful; false otherwise.
●
Analysis Objects
Type
Movement
Name
line number of the new current line
int
received
n
boolean result
bool
returns
­­­­­
MoveCurrent ­ moves the current line (index) |n| places. If n > 0, move in the forward direction (toward the end). If n < 0, move in the backward direction (toward the beginning). If there are fewer than n lines in the appropriate direction, the current line remains the same. Returns true when successful; false otherwise.
●
Analysis Objects
Type
Movement
Name
number of lines to move the current line
int
received
n
boolean result
bool
returned
­­­­
02/10/2011
Page 3 of 7
WriteLine ­ writes the current line to an output stream. If the document is empty, this operation is ignored.
●
Analysis Objects
Type
Movement
Name
output stream
ostream
received & passed back
out
WriteAll ­ writes all the lines in the document starting with the first line to an output stream; the current line remains the same.
●
Analysis Objects
Type
Movement
Name
output stream
ostream
received & passed back
out
Assignment (20 points) Write the implementation of the Document class. The Document class definition should be put in header file document.h with suitable compilation guards. The implementations of the Document class functions should be put in source file document.cpp. This project must be implemented using an STL vector and an index into this vector. Projects that do not do so will be returned for resubmission with late penalties. (10 points) Write a (main) program in file editor.cpp that has one command­line argument, the name of a textfile to edit. The program should construct a Document object using the given textfile with the current line set to the first line (if the file is not empty), then allow the user to interactively edit the file by using the following command set. Commands may be entered in upper or lower case. An example run is shown below.
Command
Description
I line
Insert a line in front of the current line making it the current line. A line
Append a line to end of the document making it the current line.
R line
Replace the current line. If the document is empty, an error message is displayed.
D
Delete the current line making the next line the current line, unless the current line is the last line, then the new last line becomes the current line. If the document is empty, an error message is displayed.
F target
Find the first line in the document containing target as a substring, making it the current line. If not found, a warning message is displayed and the current line remains the same.
S n
Set the current line to the nth line from the beginning of the document with the first line numbered 1. If the document has fewer than n lines or n < 1, an error message is displayed and the current line remains the same.
02/10/2011
Page 4 of 7
Command
Description
M n
Move the current line |n| places. If n > 0, move in the forward direction (toward the end). If n < 0, move in the backward direction (toward the beginning). If there are fewer than n lines in the appropriate direction, an error message is displayed and the current line remains the same.
C
Display the current line to the screen. If the document is empty, display an error message
L
Display (i.e., list) the entire document to the screen.
W filename
Write the entire document to file named filename.
H
Display command menu (i.e., help)
Q
Quit the editor without saving any changes
Implementation notes: ● A reminder that string indexes are of type string::size_type Also, string operations that return an index or a size use the value string::npos to indicate end of string or not found.
●
For commands I, A, R, and F, we will assume that the argument will follow the command letter after exactly one space and any other leading (and trailing) non­newline whitespace are intended to be part of the line. To get rid of the separating space, use
inputStream.ignore();
which will remove one character from the input stream without needing to store it into a variable.
You must submit a makefile named Makefile.project3 that creates an executable named editor for your project. It should conform to the examples given in lecture and demonstrated in class. REMINDER: Your project must compile for it to be graded. Submissions that do not compile will be returned for resubmission and assessed a late penalty. Submissions that do not substantially work also will be returned for resubmission and assessed a late penalty.
Follow the guidelines in the C++ Programming Style Guideline handout. As stated in the syllabus, part of the grade on a programming project depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines. Note that all of the external names ( class and member function names, file names) must be as specified above, including case. All other identifiers must have consistent case usage, but do not have to match the style of these names.
What to submit Electronically submit a tarfile containing Makefile.project3, document.h, document.cpp, and editor.cpp as explained in the handout Submission Instructions for CS 215. Do not submit object files or executable files. The submission system will compile and test the Document implementation against an autotest driver program. It will compile the project main program and check that an executable named editor is created, but will not run it. Note: The submission system will start accepting submissions no earlier than Wednesday evening.
02/10/2011
Page 5 of 7
Example Run
user@csserver:~/cs215/projects$ more sampleedit.txt
line 1 line 2 line 3 user@csserver:~/cs215/projects$ ./editor sampleedit.txt > h This line editor can do the following commands: I line ­ insert line in front of current line A line ­ append line to end of document R line ­ replace current line D ­ delete the current line F target ­ makes line containing target current line S n ­ set current line to nth line M n ­ move current line n places C ­ display current line to screen L ­ display entire document to screen W file ­ write document to file H ­ display this list of commands
Q ­ quit the editor (does not save changes) > l line 1 line 2 line 3 > c line 1 > i first line > c first line > l first line line 1 line 2 line 3 > a last line > c last line > l first line line 1 line 2 line 3 last line > m ­2 > c line 2
> r middle line
> l
first line line 1 middle line
line 3 last line > s 1
> c
first line
> d
02/10/2011
Page 6 of 7
> c
line 1
> l
line 1 middle line
line 3 last line > f mid
> c
middle line
> w sample2.txt
> q
user@csserver:~/cs215/projects$ more sample2.txt
line 1 middle line
line 3 last line user@csserver:~/cs215/projects$
02/10/2011
Page 7 of 7
Download