Further specification of interface: message handler.
[gedcom-parse.git] / standalone.c
1 /*  This program is free software; you can redistribute it and/or modify  *
2  *  it under the terms of the GNU General Public License as published by  *
3  *  the Free Software Foundation; either version 2 of the License, or     *
4  *  (at your option) any later version.                                   *
5
6  (C) 2001 by The Genes Development Team
7  Original author: Peter Verthez (Peter.Verthez@advalvas.be)
8 */
9
10 /* $Id$ */
11 /* $Name$ */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "external.h"
17
18 void show_help ()
19 {
20   printf("gedcom-parse test program for libgedcom\n\n");
21   printf("Usage:  gedcom-parse [options] file\n");
22   printf("Options:\n");
23   printf("  -h    Show this help text\n");
24   printf("  -nc   Disable compatibility mode\n");
25   printf("  -fi   Fail immediately on errors\n");
26   printf("  -fd   Deferred fail on errors, but parse completely\n");
27   printf("  -fn   No fail on errors\n");
28   printf("  -dg   Debug setting: only libgedcom debug messages\n");
29   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
30   printf("  -2    Run the test parse 2 times instead of once\n");
31   printf("  -3    Run the test parse 3 times instead of once\n");
32 }
33
34 Gedcom_ctxt header_start(char *xreftag __attribute__ ((unused)))
35 {
36   printf("Header start\n");
37   return (Gedcom_ctxt)0;
38 }
39
40 void header_end(Gedcom_ctxt self)
41 {
42   printf("Header end, context is %d\n", (int)self);
43 }
44
45 char family_xreftags[100][255];
46 int  family_nr = 0;
47
48 Gedcom_ctxt family_start(char *xreftag)
49 {
50   printf("Family start, xreftag is %s\n", xreftag);
51   strcpy(family_xreftags[family_nr], xreftag);
52   return (Gedcom_ctxt)(family_nr++);
53 }
54
55 void family_end(Gedcom_ctxt self)
56 {
57   printf("Family end, xreftag is %s\n", family_xreftags[(int)self]);
58 }
59
60 void subscribe_callbacks()
61 {
62   subscribe_to_record(REC_HEAD, header_start, header_end);
63   subscribe_to_record(REC_FAM,  family_start, family_end);
64 }
65
66 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
67 {
68   if (type == MESSAGE)
69     fprintf(stderr, "MESSAGE: ");
70   else if (type == WARNING)
71     fprintf(stderr, "WARNING: ");
72   else if (type == ERROR)
73     fprintf(stderr, "ERROR: ");
74   fprintf(stderr, msg);
75 }
76
77 int main(int argc, char* argv[])
78 {
79   Gedcom_err_mech mech = IMMED_FAIL;
80   int compat_enabled = 1;
81   int debug_level = 0;
82   int run_times   = 1;
83   int result      = 0;
84   char* file_name = NULL;
85
86   if (argc > 1) {
87     int i;
88     for (i=1; i<argc; i++) {
89       if (!strncmp(argv[i], "-da", 4))
90         debug_level = 2;
91       else if (!strncmp(argv[i], "-dg", 4))
92         debug_level = 1;
93       else if (!strncmp(argv[i], "-fi", 4))
94         mech = IMMED_FAIL;
95       else if (!strncmp(argv[i], "-fd", 4))
96         mech = DEFER_FAIL;
97       else if (!strncmp(argv[i], "-fn", 4))
98         mech = IGNORE_ERRORS;
99       else if (!strncmp(argv[i], "-nc", 4))
100         compat_enabled = 0;
101       else if (!strncmp(argv[i], "-h", 3)) {
102         show_help();
103         exit(1);
104       }
105       else if (!strncmp(argv[i], "-2", 3)) {
106         run_times = 2;
107       }
108       else if (!strncmp(argv[i], "-3", 3)) {
109         run_times = 3;
110       }
111       else if (strncmp(argv[i], "-", 1)) {
112         file_name = argv[i];
113         break;
114       }
115       else {
116         printf ("Unrecognized option: %s\n", argv[i]);
117         show_help();
118         exit(1);
119       }
120     }
121   }
122   
123   if (!file_name) {
124     printf("No file name given\n");
125     show_help();
126     exit(1);
127   }
128
129   gedcom_set_debug_level(debug_level, NULL);
130   gedcom_set_compat_handling(compat_enabled);
131   gedcom_set_error_handling(mech);
132   gedcom_set_message_handler(gedcom_message_handler);
133   
134   subscribe_callbacks();
135   while (run_times-- > 0) {
136     result |= gedcom_parse_file(file_name);
137   }
138   if (result == 0) {
139     printf("Parse succeeded\n");
140     return 0;
141   }
142   else {
143     printf("Parse failed\n");
144     return 1;
145   }  
146 }