Added help text; better debug options.
[gedcom-parse.git] / standalone.c
1 /* $Id$ */
2 /* $Name$ */
3
4 #include "gedcom.h"
5
6 void show_help ()
7 {
8   printf("gedcom-parse test program for libgedcom\n");
9   printf("Options:\n");
10   printf("  -h    Show this help text\n");
11   printf("  -nc   Disable compatibility mode\n");
12   printf("  -fi   Fail immediately on errors\n");
13   printf("  -fd   Deferred fail on errors, but parse completely\n");
14   printf("  -fn   No fail on errors\n");
15   printf("  -dg   Debug setting: only libgedcom debug messages\n");
16   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
17 }
18
19 int main(int argc, char* argv[])
20 {
21   MECHANISM mech = IMMED_FAIL;
22   int compat_enabled = 1;
23   int debug_level = 0;
24   if (argc > 1) {
25     int i;
26     for (i=1; i<argc; i++) {
27       if (!strncmp(argv[i], "-da", 4))
28         debug_level = 2;
29       else if (!strncmp(argv[i], "-dg", 4))
30         debug_level = 1;
31       else if (!strncmp(argv[i], "-fi", 4))
32         mech = IMMED_FAIL;
33       else if (!strncmp(argv[i], "-fd", 4))
34         mech = DEFER_FAIL;
35       else if (!strncmp(argv[i], "-fn", 4))
36         mech = IGNORE_ERRORS;
37       else if (!strncmp(argv[i], "-nc", 4))
38         compat_enabled = 0;
39       else if (!strncmp(argv[i], "-h", 3)) {
40         show_help();
41         exit(1);
42       }
43       else {
44         printf ("Unrecognized option: %s\n", argv[i]);
45         show_help();
46         exit(1);
47       }
48     }
49   }
50   gedcom_set_debug_level(debug_level);
51   gedcom_set_compat_handling(compat_enabled);
52   gedcom_set_error_handling(mech);
53   if (gedcom_parse() == 0) {
54     printf("Parse succeeded\n");
55     return 0;
56   }
57   else {
58     printf("Parse failed\n");
59     return 1;
60   }
61 }
62
63 int gedcom_warning(char* s, ...)
64 {
65   int res;
66   va_list ap;
67
68   va_start(ap, s);
69   fprintf(stderr, "Warning on line %d: ", line_no);
70   res = vfprintf(stderr, s, ap);
71   fprintf(stderr, "\n");
72   va_end(ap);
73   
74   return res;
75 }
76
77 int gedcom_error(char* s, ...)
78 {
79   int res;
80   va_list ap;
81
82   va_start(ap, s);
83   fprintf(stderr, "Error on line %d: ", line_no);
84   res = vfprintf(stderr, s, ap);
85   fprintf(stderr, "\n");
86   va_end(ap);
87   
88   return res;
89 }