EECS 2031 - Lab 11

This question builds on the program you prepared for Lab 10.

Read the Background section below carefully. Note that the acceptable format in the database file is somewhat different than the one for the Unix portion of the course.

Background

In many computer science and engineering research projects, researchers are working with information about a set of entities and the relations between them. For instance, the set of entities may be the set of classes in a software system, and the relations may be inheritance, aggregation etc.

A simple way to store such information is in a text file, where each line has the following format:

relation someEntity anotherEntity

The three words in each line may be separated by any number of spaces or tabs. There may be spaces or tabs before the first word and after the last one as well. We can assume that the names of all relations and entities do not contain any spaces or tabs. You can download an example of a database file here.

What to do

Requirements

For this lab, you can assume that no string entered by the user will be longer than 128 characters. You can also assume that the database file does not contain more than 128 lines. However, you can not assume that the database has the correct format.

There are three commands your program must handle. If any other command is entered, the program should reply

Unrecognized command.

and then present the prompt again. The three commands are:

  1. quit - The program must exit.
  2. setdb inputfile.txt

    If inputfile.txt can be opened for reading, your program must read from the database file, and determine the number of correctly formatted lines in it. It must then output a message such as

    Read 54 facts from inputfile.txt
    

    where 54 is the number of correctly formatted lines in inputfile.txt.

    A line that does not conform to the correct format outlined in the Background section is considered malformed and is not counted in the number of facts reported above. If there are any such lines in the input file, your program must count them, and produce output as below

    Read 54 facts from inputfile.txt
    12 malformed lines were ignored.
    

    The name of the input file may be different of course. In fact, it may be a full path name, such as /eecs/home/bil/input.txt

    If the database file could not be opened for reading, your program must respond

    Unable to read from inputfile.txt
    

    If no input file is provided, your program must respond

    Missing Argument.
    

    If more than one input file is provided, your program must respond

    Read 54 facts from inputfile.txt
    12 malformed lines were ignored.
    Extra arguments ignored.
    
  3. printdb

    If no facts have been read through a successful setdb command, your program must respond

    Database is empty.
    

    Otherwise, your program must output the facts in the order that they were read from the database file. Any extraneous whitespace must be removed, i.e. the only whitespace must be two spaces between the three words.

    The expectation is that your program will store the database facts internally. In other words, printdb should not re-read the input file.

If the user enters an empty command, i.e. just hits Enter, a new prompt must be presented in the next line.

To implement this lab, you need to be able to compare and copy strings in C. Take a look at the man page for strncmp and strncpy. It should be all you need to implement this part.

Following is a sample run that shows the expected behaviour of the program.


% gcc lab11.c 
% a.out
Grock- setdb example.txt
Read 4 facts from example.txt
Grock- printdb 
inherit class1 class2
inherit class3 class4
implement class3 interface1
aggregate class3 class5
Grock- setdb malformed.txt extra
Read 2 facts from malformed.txt
4 malformed lines were ignored.
Extra arguments ignored.
Grock- printdb
rel ent1 ent2
rel ent3 ent6
Grock- quit
%