Pass the parsed tag value (integer format) together with the string value
[gedcom-parse.git] / standalone.c
1 /* Test program for the Gedcom library.
2    Copyright (C) 2001, 2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include "gedcom.h"
29
30 #define OUTFILE "testgedcom.out"
31 FILE* outfile = NULL;
32
33 void output(int to_stdout_too, char* format, ...)
34 {
35   va_list ap;
36   va_start(ap, format);
37   if (outfile) {
38     vfprintf(outfile, format, ap);
39   }
40   if (to_stdout_too) {
41     vprintf(format, ap);
42   }
43   va_end(ap);
44 }
45
46 void show_help ()
47 {
48   printf("gedcom-parse test program for libgedcom\n\n");
49   printf("Usage:  testgedcom [options] file\n");
50   printf("Options:\n");
51   printf("  -h    Show this help text\n");
52   printf("  -nc   Disable compatibility mode\n");
53   printf("  -fi   Fail immediately on errors\n");
54   printf("  -fd   Deferred fail on errors, but parse completely\n");
55   printf("  -fn   No fail on errors\n");
56   printf("  -dg   Debug setting: only libgedcom debug messages\n");
57   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
58   printf("  -2    Run the test parse 2 times instead of once\n");
59   printf("  -3    Run the test parse 3 times instead of once\n");
60 }
61
62 Gedcom_ctxt header_start(int level, Gedcom_val xref, char *tag, int tag_value)
63 {
64   output(1, "Header start\n");
65   return (Gedcom_ctxt)0;
66 }
67
68 void header_end(Gedcom_ctxt self)
69 {
70   output(1, "Header end, context is %d\n", (int)self);
71 }
72
73 char family_xreftags[100][255];
74 int  family_nr = 0;
75
76 Gedcom_ctxt family_start(int level, Gedcom_val xref, char *tag, int tag_value)
77 {
78   output(1, "Family start, xref is %s\n", GEDCOM_STRING(xref));
79   strcpy(family_xreftags[family_nr], GEDCOM_STRING(xref));
80   return (Gedcom_ctxt)(family_nr++);
81 }
82
83 void family_end(Gedcom_ctxt self)
84 {
85   output(1, "Family end, xref is %s\n", family_xreftags[(int)self]);
86 }
87
88 Gedcom_ctxt submit_start(int level, Gedcom_val xref, char *tag, int tag_value)
89 {
90   output(1, "Submitter, xref is %s\n", GEDCOM_STRING(xref));
91   return (Gedcom_ctxt)10000;
92 }
93
94 Gedcom_ctxt source_start(Gedcom_ctxt parent, int level, char *tag,
95                          char* raw_value,
96                          int tag_value, Gedcom_val parsed_value)
97 {
98   Gedcom_ctxt self = (Gedcom_ctxt)((int) parent + 1000);
99   output(1, "Source is %s (ctxt is %d, parent is %d)\n",
100          GEDCOM_STRING(parsed_value), (int) self, (int) parent);
101   return self;
102 }
103
104 void source_end(Gedcom_ctxt parent, Gedcom_ctxt self, Gedcom_val parsed_value)
105 {
106   output(1, "Source context %d in parent %d\n", (int)self, (int)parent);
107 }
108
109 Gedcom_ctxt source_date_start(Gedcom_ctxt parent, int level, char *tag,
110                               char* raw_value,
111                               int tag_value, Gedcom_val parsed_value)
112 {
113   struct date_value dv;
114   Gedcom_ctxt self = (Gedcom_ctxt)((int) parent + 1000);
115   dv = GEDCOM_DATE(parsed_value);
116   output(1, "Contents of the date_value:\n");
117   output(1, "  raw value: %s\n", raw_value);
118   output(1, "  type: %d\n", dv.type);
119   output(1, "  date1:\n");
120   output(1, "    calendar type: %d\n", dv.date1.cal);
121   output(1, "    day: %s\n", dv.date1.day_str);
122   output(1, "    month: %s\n", dv.date1.month_str);
123   output(1, "    year: %s\n", dv.date1.year_str);
124   output(1, "    date type: %d\n", dv.date1.type);
125   output(1, "    sdn1: %ld\n", dv.date1.sdn1);
126   output(1, "    sdn2: %ld\n", dv.date1.sdn2);
127   output(1, "  date2:\n");
128   output(1, "    calendar type: %d\n", dv.date2.cal);
129   output(1, "    day: %s\n", dv.date2.day_str);
130   output(1, "    month: %s\n", dv.date2.month_str);
131   output(1, "    year: %s\n", dv.date2.year_str);
132   output(1, "    date type: %d\n", dv.date2.type);
133   output(1, "    sdn1: %ld\n", dv.date2.sdn1);
134   output(1, "    sdn2: %ld\n", dv.date2.sdn2);
135   output(1, "  phrase: %s\n", dv.phrase);
136   return self;
137 }
138
139 void default_cb(Gedcom_ctxt ctxt, int level, char *tag, char *raw_value,
140                 int tag_value)
141 {
142   output(0, "== %d %s (%d) %s (ctxt is %d)\n",
143          level, tag, tag_value, raw_value, (int)ctxt);
144 }
145
146 void subscribe_callbacks()
147 {
148   gedcom_subscribe_to_record(REC_HEAD, header_start, header_end);
149   gedcom_subscribe_to_record(REC_FAM,  family_start, family_end);
150   gedcom_subscribe_to_record(REC_SUBM, submit_start, NULL);
151   gedcom_subscribe_to_element(ELT_HEAD_SOUR, source_start, source_end);
152   gedcom_subscribe_to_element(ELT_SOUR_DATA_EVEN_DATE,
153                               source_date_start, NULL);
154 }
155
156 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
157 {
158   if (type == MESSAGE)
159     fprintf(stderr, "MESSAGE: ");
160   else if (type == WARNING)
161     fprintf(stderr, "WARNING: ");
162   else if (type == ERROR)
163     fprintf(stderr, "ERROR: ");
164   fprintf(stderr, "%s\n", msg);
165 }
166
167 int main(int argc, char* argv[])
168 {
169   Gedcom_err_mech mech = IMMED_FAIL;
170   int compat_enabled = 1;
171   int debug_level = 0;
172   int run_times   = 1;
173   int result      = 0;
174   char* file_name = NULL;
175
176   if (argc > 1) {
177     int i;
178     for (i=1; i<argc; i++) {
179       if (!strncmp(argv[i], "-da", 4))
180         debug_level = 2;
181       else if (!strncmp(argv[i], "-dg", 4))
182         debug_level = 1;
183       else if (!strncmp(argv[i], "-fi", 4))
184         mech = IMMED_FAIL;
185       else if (!strncmp(argv[i], "-fd", 4))
186         mech = DEFER_FAIL;
187       else if (!strncmp(argv[i], "-fn", 4))
188         mech = IGNORE_ERRORS;
189       else if (!strncmp(argv[i], "-nc", 4))
190         compat_enabled = 0;
191       else if (!strncmp(argv[i], "-h", 3)) {
192         show_help();
193         exit(1);
194       }
195       else if (!strncmp(argv[i], "-2", 3)) {
196         run_times = 2;
197       }
198       else if (!strncmp(argv[i], "-3", 3)) {
199         run_times = 3;
200       }
201       else if (strncmp(argv[i], "-", 1)) {
202         file_name = argv[i];
203         break;
204       }
205       else {
206         printf ("Unrecognized option: %s\n", argv[i]);
207         show_help();
208         exit(1);
209       }
210     }
211   }
212   
213   if (!file_name) {
214     printf("No file name given\n");
215     show_help();
216     exit(1);
217   }
218
219   gedcom_set_debug_level(debug_level, NULL);
220   gedcom_set_compat_handling(compat_enabled);
221   gedcom_set_error_handling(mech);
222   gedcom_set_message_handler(gedcom_message_handler);
223   gedcom_set_default_callback(default_cb);
224   
225   subscribe_callbacks();
226   outfile = fopen(OUTFILE, "a");
227   if (!outfile) {
228     printf("Could not open %s for appending\n", OUTFILE);
229   }
230   while (run_times-- > 0) {
231     output(0, "\n=== Parsing file %s\n", file_name);
232     result |= gedcom_parse_file(file_name);
233   }
234   fclose(outfile);
235   if (result == 0) {
236     printf("Parse succeeded\n");
237     return 0;
238   }
239   else {
240     printf("Parse failed\n");
241     return 1;
242   }  
243 }