Go to the first, previous, next, last section, table of contents.


Example 2: An Annotation Translator

This program translates the `atruth' annotations for the record named in its argument into an AHA-format annotation file with the annotator name `aha'.

 1  #include <stdio.h>
 2  #include <ecg/db.h>
 3
 4  main(argc, argv)
 5  int argc;
 6  char *argv[];
 7  {
 8      DB_Anninfo an[2];
 9      DB_Annotation annot;
10
11      if (argc < 2) {
12          fprintf(stderr, "usage: %s record\n", argv[0]);
13          exit(1);
14      }
15      an[0].name = "atruth"; an[0].stat = READ;
16      an[1].name = "aha";    an[1].stat = AHA_WRITE;
17      if (annopen(argv[1], an, 2) < 0) exit(2);
18      while (getann(0, &annot) == 0 && putann(0, &annot) == 0)
19          ;
20      dbquit();
21      exit(0);
22  }

Notes:

Lines 4--6:
If this doesn't look familiar, see K&R, pp. 114--115.
Lines 11--14:
This is the standard idiom for producing those cryptic error messages for which UNIX programs are notorious; argv[0] is the name by which the program was invoked.
Lines 15--16:
These lines set up the annotator information. Input annotator 0 is the `atruth' annotation file, and output annotator 0 will be written in AHA format.
Line 17:
If we can't read the input or write the output, quit with an error message from annopen.
Line 18:
Here's where the work is done. The format translation is handled entirely by getann and putann. The loop ends normally when getann reaches the end of the input file, or prematurely if there is a read or write error.
Line 21:
Since we have carefully defined non-zero exit codes for the various errors that this program might encounter, we also define this successful exit here. If this program is run as part of a UNIX shell script, the exit codes are accessible to the shell, which can determine what to do next as a result. If this line were omitted (as in example 1), the exit code would be undefined.


Go to the first, previous, next, last section, table of contents.



George B. Moody (george@hstbme.mit.edu)