Stream Editor Assignment


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. These line numbers do not change when lines are added or deleted by editing.
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 several fields.

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:
L1 To have text prepended
L2 To have text prepended
L3 To have text prepended
L4 Unmodified except appended text
L5 Unmodified except appended text
L6 Unmodified except appended text
L7 This is one line for substitution
L8 This is one line for substitution
L9 This is to be deleted
L10 This is to be deleted
L11 The last line of the file
then the standard output is:
This must appear as the first line
Prepended to 1,2 and 3 :L1 To have text prepended : Appended to all
Prepended to 1,2 and 3 :L2 To have text prepended : Appended to all
Prepended to 1,2 and 3 :L3 To have text prepended : Appended to all
L4 Unmodified except appended text : Appended to all
L5 Unmodified except appended text : Appended to all
L6 Unmodified except appended text : Appended to all
L7 This has been substituted on a line for substitution : Appended to all
L8 This has been substituted on a line for substitution : Appended to all
L11 The last line of the file : Appended to all

Submission

Please review the policies on assessment in the administration document.

Solution

More Test Files

Input Command file Expected output