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

advertisement
CS 215 ­ Fundamentals of Programming II
Spring 2014­ Programming Project 3
30 points
Out: February 19, 2014
Due: March 5, 2014 (Wednesday)
Note that this assignment is worth 50% more than the previous projects. It is also about 50% larger than the previous projects, so with the practical exam taking up a lecture period, it is due in 14 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. 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;
Function delete changed to erase: 02/26/2014
Page 1 of 7
D. Hwang
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. The current line index should be initialized to 0.
●
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, i.e., index 0.
●
Analysis Objects
Type
Movement
Name
input stream
istream
received & passed back
in
is_empty ­ 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 and is the current line.
●
Analysis Objects
Type
Movement
Name
line to insert
string
received
new_line
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 and is the current line.
●
Analysis Objects
Type
Movement
Name
line to insert
string
received
new_line
Function delete changed to erase: 02/26/2014
Page 2 of 7
D. Hwang
replace ­ replaces the contents of the current line. If the document is empty, this operation should throw an out_of_range exception (defined in <stdexcept>).
●
Analysis Objects
Type
Movement
Name
replacement line
string
received
new_line
erase (note change in function name) ­ 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 should throw an out_of_range exception (defined in <stdexcept>).
●
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
returns
­­­­­
set_current ­ sets the current line (index) to n. If n < 0 or the document has fewer than n+1 lines, this operation should throw an out_of_range exception (defined in <stdexcept>), and the current line remains the same.
●
Analysis Objects
Type
Movement
Name
line number of the new current line
int
received
n
move_current ­ 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, this operation should throw an out_of_range exception (defined in <stdexcept>), and the current line remains the same.
●
Analysis Objects
Type
Movement
Name
number of lines to move the current line
int
received
n
Function delete changed to erase: 02/26/2014
Page 3 of 7
D. Hwang
write_line ­ writes the current line, including a newline, 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
write_all ­ writes the document (i.e. all the lines, including newlines, starting with the first line) to an output stream; the current line index remains the same.
●
Analysis Objects
Type
Movement
Name
output stream
ostream
received & passed back
out
Note that neither of the write functions returns a result stream.
Note: The Document class is a regular class, not a template class. It just uses the STL vector<T> template class.
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 a current line index into this vector. Projects that do not do so will not be graded. (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. 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 index to n. If the document has fewer than n+1 lines or n < 0, an error message is displayed and the current line remains the same.
Function delete changed to erase: 02/26/2014
Page 4 of 7
D. Hwang
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: ● Commands R, D, S, and M must be implemented by calling the respective Document operation and catching any exceptions that are thrown.
● A reminder that string and vector indexes and vector size are of type size_t. 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
in.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 for your project that has a first target for an executable file named editor created from linking objects files and has targets for the object files It should conform to the examples demonstrated in class.
REMINDER: Your project must compile for it to be graded. Submissions that do not compile will not be graded. Submissions that do not substantially work also will not be graded.
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.
What to submit Electronically submit a tarfile containing files Makefile, document.h, document.cpp, and editor.cpp as explained in the handout Submission Instructions for CS 215. The submission system will begin accepting submissions no earlier than Friday, February . Reminder: all public names (the class name, the operation names, and file names) must be as specified including capitalization. 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, February 26.
Function delete changed to erase: 02/26/2014
Page 5 of 7
D. Hwang
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
A
R
D
F
S
M
C
L
W
H
Q
line
line
line
target n
n
file
-
insert line in front of current line
append line to end of document
replace current line
delete the current line
makes line containing target current line
set current line to nth line
move current line n places
display current line to screen
display entire document to screen
write document to file
display this list of commands
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
Function delete changed to erase: 02/26/2014
Page 6 of 7
D. Hwang
last line
> s 0
> c
first line
> d
> 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$
Function delete changed to erase: 02/26/2014
Page 7 of 7
D. Hwang
Download