Homework 2 - Function pointers in C++
Reading: Budd, Chapters 2&3.
Due: Because Monday is a holiday this one is due Tuesday January 22nd, at 10pm.
Program Description - A Simple Command Line Interpreter for Manipulating a Tree
For this assignment you will create a simple command line interpreter that
lets the user manipulate a simple tree using unix like directory commands.
How you implment your program is critical. A program that gives the correct output
but fails to do so using the implementation strategy specified below will little or
no credit.
The key concepts to be exercised for this assigment are arrays, function pointers,
arrays of function pointers, simple structures, pointers, and pointers to pointers.
Input
The program will accept commands from standard input, one command per line.
The commands will give a simulation of manipulating a hierarchical file system.
The "file system" will initially contain a single node called "root". The following commands
can then be issued:
- ls - print the contents of the current directory (node) on one line, each name
separated by a space. The names can appear in any order.
- mkdir name - add a new directory (node) named name in the current directory.
Do nothing and print a warning if the directory already contains an entry named name.
- cd name - change the current directory to be the one named name in the current directory.
If no such node exists, print a warning and do nothing. If name is .. then move up one
level in the directory structure. If already at the root just stay at the root (i.e. the parent of the
root is the root).
- rm name - remove the directory (node) named name from the current directory.
If no such node exits, print a warning and do nothing.
- cat name - if the current directory contains a node named name,
print the message: "printing name", otherwise print a message saying no such entry.
- quit - end the program
For anything else, print an error message about no such command.
Output
Print a prompt before each command. The prompt should be the current directory (node) name
followed by a colon and one space. Other output is specified above in the
command descriptions.
Sample Execution
os-prompt: a.out
root: mkdir a
root: mkdir b
root: ls
b a
root: cd a
a: mkdir a1
a: mkdir a2
a: mkdir a3
a: ls
a3 a2 a1
a: cd ..
root: ls
b a
root: cd b
b: mkdir b1
b: mkdir b2
b: ls
b2 b1
b: cd b2
b2: mkdir b21
b2: ls
b21
b2: rm b21
b2: ls
b2: cd ..
b: ls
b2 b1
b: rm b2
b: ls
b1
b: cd ..
root: ls
b a
root: cd a
a: ls
a3 a2 a1
a: cat a1
printing a1
a: cat foo
Error: no such entry.
a: quit
os-prompt:
Implementation Details
A command loop like the one described for this program could be implemented in many ways.
For example the body of the loop could contain a switch statement or a series of if-else-if-else statements.
However, for this assignment you must use two parallel arrays, one containing the names of the commands
and the other containing a function pointer to the function to be called to process the specified command.
This approach can be quite useful when you have long lists of commands that are all dispatched using
a function with the same signature.
You are free to implement the simple tree structure for this assignment any way you wish, although the
intended solution is a simple tree structure using the following structure:
struct node {
string name;
struct node* sib;
struct node* firstChild;
struct node* parent;
};
Note that the variable containing (or pointing to) the
root of the tree and the variable tracking the "current directory" may NOT be globals.
For this assignment you do NOT need to bother with memory management. That is, you do not need to
free the memory for tree nodes that become inaccessible due to "rm" commands. You will need to
properly manage memory for assignment 3.
The starter program cmdStarter.cpp contains examples of
several C++ standard opertions or
pieces of syntax that are not fully covered in the required reading.
Design Points
15 points - properly uses an array of function pointers to dispatch all required commands
10 points - demonstrates proper syntax for output parameters to maintain current directory
Correctness Points
6 points for each properly implemented command (ls, mkdir, cd, rm, cat) when no errors are present
4 points for each commands error conditions