Starting from:
$30

$24

Write a stream editor called svi. svi

Overview:
Write a stream editor called svi. svi edits its standard input according to a list of edit 
commands that are given in a file specified as the command line argument. The edited standard 
input is output to standard output. Each line of input, and output after editing, will be maximally 
256 characters long. (svi has many similarities with the standard UNIX utility sed.)

Edit Commands:
Each edit command consists of an optional line range specification, an edit operation specifier, 
and data required by the edit operation. You may assume that all commands in command files 
are correctly formatted.  

There are three types of line range specifications:

/<text>/  
This format specifies all lines that contain the <text>. The <text> can be maximally 80 
characters.  
 
<1st line number>,<last line number>/ 
This format specifies all lines in the range <1st line number> to <last line number>, 
inclusive. Lines in the input file are considered to be numbered from 1, and this 
numbering is not changed in the editing process.  
 
No line range specification specifies all lines in the file.  
 
There are five edit operations:  
A<text>  
Appends the <text> at the end of the line. For example:  
Ahello jim  
appends hello jim to all lines in the file. The <text> can be maximally 80 characters.  
I<text>  
Inserts the <text> at the start of the line. For example:  
/blah/Ineedle noddle noo  
inserts needle noddle noo at the start of all lines that contain blah. The <text> 
can be maximally 80 characters.  
O<text>  
Inserts the <text> on a new line before the current line. For example:  
1,1/OThe Title  
Inserts a new line before the first line in the file, containing the text The Title. The 
<text> can be maximally 80 characters. The new line is not put through the editing 
process.  
d  
Deletes the line from the file. For example:  
3,6/d  
deletes lines 3 to 6 inclusive.  
s/<old text>/<new text>/  
Replaces the first occurence of <old text>, in the line, with <new text>. For 
example:  
/filename/s/.pas/.c/  
replaces the first occurrence of .pas with .c, in all lines containing filename. The 
<old text> and <new text> can be maximally 80 characters.  
Data Structure 
The edit commands must be stored in an array of structures, each edit operation being stored in 
one element of the array. A maximum of 100 edit commands may be used. Each structure 
contains four fields.  
• Line range specification type - one of none, text, or line numbers. This must be an 
enumerated type.  
• Line range specification. Because there are three possible formats for the line range 
specification, this field must be a union.  
• The edit operation specifier.  
• The data associated with the edit operation.  
Overall Algorithm  
Read in and store the edit commands from the file 
Read a line from standard input 
While not at EOF of standard input 
    For each edit command do 
        If the line is in the range specified then 
            Do the edit 
    Output the edited line (unless it has been deleted) 
    Read a line from standard input 
Sample Run  
If the file of edit commands is:  
/Never done/I--------------------------------------- 
1,3/IPrepended to 1,2 and 3 : 
1,1/OThis must appear as the first line 
A : Appended to all 
/line for substitution/s/This is one/This has been substituted 
on a/ 
9,10/d 
/deleted/IThis should not appear 
/Never done/I--------------------------------------- 
and the standard input is:  
1 To have text prepended 
2 To have text prepended 
3 To have text prepended 
4 Unmodified except appended text 
5 Unmodified except appended text 
6 Unmodified except appended text 
7 This is one line for substitution 
8 This is one line for substitution 
9 This is to be deleted 
10 This is to be deleted 
11 The last line of the file 
then the standard output is:  This must appear as the first line 
Prepended to 1,2 and 3 :1 To have text prepended : Appended to all 
Prepended to 1,2 and 3 :2 To have text prepended : Appended to all 
Prepended to 1,2 and 3 :3 To have text prepended : Appended to all 
4 Unmodified except appended text : Appended to all 
5 Unmodified except appended text : Appended to all 
6 Unmodified except appended text : Appended to all 
7 This has been substituted on a line for substitution : Appended to 
all 
8 This has been substituted on a line for substitution : Appended to 
all 
11 The last line of the file : Appended to all 
Submission  
You must use the submit2 program to submit ...  
• Your solution source code  
• Your makefile if you use one  
• A README with anything you want the TA to know. In particular, if you do not succeed 
in getting everything working perfectly, you should explain what is working, and what 
you have achieved towards the aspects that are not working.  
... with the command  
~csc322/bin/submit2 csc322 StreamEditor <your file names> 
by 11:59PM on December 10th.  
Marks will be distributed as follows:  
• Reading in the edit commands and building the data structure - 20%  
o typedefed data structures - 9%  
o Code for reading in from the file and extracting the commands - 11%  
• Performance to specification - 50%  
o Editing all lines with no line range specification - 5%  
o Editing the correct lines with a text line range specification - 5%  
o Editing the correct lines with a line number specification - 5%  
o Simple edit commands (A, I, O, d) - 5% each = 20%  
o Complex edit commands (s) - 15% each = 15%  
• Programming style - 30% (Negative grading down to 0%)  
o Formatting - 5%  
o Comments - 5%  
o Meaningful identifiers - 5%  
o Functions and parameters - 5%  
o (No) use of global variables - 5%  
It is worth 15% of the course assessment.  
 

More products