[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

Example 2: An Annotation Translator

This program translates the ‘atr’ 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 <wfdb/wfdb.h>
 3
 4  main(argc, argv)
 5  int argc;
 6  char *argv[];
 7  {
 8      WFDB_Anninfo an[2];
 9      WFDB_Annotation annot;
10
11      if (argc < 2) {
12          fprintf(stderr, "usage: %s record\n", argv[0]);
13          exit(1);
14      }
15      an[0].name = "atr"; an[0].stat = WFDB_READ;
16      an[1].name = "aha"; an[1].stat = WFDB_AHA_WRITE;
17      if (annopen(argv[1], an, 2) < 0) exit(2);
18      while (getann(0, &annot) == 0 && putann(0, &annot) == 0)
19          ;
20      wfdbquit();
21      exit(0);
22  }

(See http://physionet.org/physiotools/wfdb/examples/example2.c for a copy of this program.)

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 ‘atr’ 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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

George B. Moody (george@mit.edu)