Lab 6: Input and Output

In this lab you'll extend your simple database to include "save" and "load" capabilities and also interact with a user.

Goals:

  1. Extend your database program to load and store its contents
  2. Interact with users through a small set of commands
  3. Create some automated tests for your database program

Part 1: Make dbe_print more flexible

Right now you can only print to the screen, but today you'll be writing data to a file as well. To make this happen, the first step is to change dbe_print to print to a file of your choice.

  1. Add a FILE* parameter to dbe_print.
  2. Change dbe_print to use fprintf and print to that file. You'll have to modify both data.c and data.h. For info on how fprintf works, see the manpage (man 3 fprintf).
  3. Find everywhere that calls dbe_print and change it to work with your new parameter.
    HINT: use grep -n to find locations in files that use dbe_print.
    HINT 2: In nano, you can use ^-_ and enter 12 to go to line 12. In vi, hit escape and type 12gg to go to line 12.
    HINT 3: You probably want your dbe_print calls to continue to print to stdout.

Part 2: Implement getALine() to read input

  1. Implement getALine(). The comments in data.c should guide you. So will the manpage for fgets.
  2. Check if you're on the right track by making and running the test executable.
  3. Once you've implemented getALine(), open up test.c and look at the test_getALine(void) function. It tests two cases. Add a third test.

Part 3: Adding an interaction prompt

Now that you can get input and write output, it's time to interact with a user. In this part of the lab, you'll be creating a run loop that repeatedly asks for input, processes the input (the command), then repeats.

To get started, open up local.c and read the comment block above handleLocalInput. It explains all the commands you will implement!

The TODO comments in the body of the function will tell you where to implement things. Make sure the parts you implement work by running the local executable to test each new command you implement.

Some more information about the commands and suggestions below. We recommend completing the commands in this order:

The q command

To quit, find a way to break out of the loop!

The l command

When a user issues the l, command, list all the database entries using dbe_print.

(Local)> l
x => y
w => z
(Local)>

The a command

When a user issues the a, command, prompt for two more inputs (one at a time). A session should look like this:

(Local)> a
       name? myname
       value? myvalue
(Local)>

In the last lab you implemented a helper function in data.c that will do the hard work. HINT: you can use the character buffers already declared inside the function body.

The r command

When a user issues the r, command, prompt for one more input: the name to remove. Find the first match and remove that one. A session should look like this:

(Local)> r
       name? bob
(Local)>

In the last lab you implemented a helper function in data.c that will do the hard work!

Part 4: Adding an export capability

The e command

For this command, you need to implement the user-interaction parts in local.c, but also will need to implement some functionality in data.c.

Part 5: Adding an import capability

The i command

This is very similar to the e command.

Part 6: Going farther: Finding matches

This part is optional, though you'll need to complete it in the next lab if you don't do it here.

The f command

Implement do_find_all_matches, which is triggered by the 'f' command.

Hint: you use db_find_one repeatedly until you run out of matches.

Finishing the Lab

  1. Make sure any code files you've created are added to SVN. (you can check using svn status).
  2. Ensure your name and your partner's names are on all the files you edited.
  3. Commit your files to SVN. Only one of you two must commit the code to SVN, but both your names must be on all files you changed.