Forgot variable declaration...
[gedcom-parse.git] / standalone.c
1 /* Test program for the Gedcom library.
2    Copyright (C) 2001 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 "gedcom.h"
28
29 void show_help ()
30 {
31   printf("gedcom-parse test program for libgedcom\n\n");
32   printf("Usage:  gedcom-parse [options] file\n");
33   printf("Options:\n");
34   printf("  -h    Show this help text\n");
35   printf("  -nc   Disable compatibility mode\n");
36   printf("  -fi   Fail immediately on errors\n");
37   printf("  -fd   Deferred fail on errors, but parse completely\n");
38   printf("  -fn   No fail on errors\n");
39   printf("  -dg   Debug setting: only libgedcom debug messages\n");
40   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
41   printf("  -2    Run the test parse 2 times instead of once\n");
42   printf("  -3    Run the test parse 3 times instead of once\n");
43 }
44
45 Gedcom_ctxt header_start(int level, char *xref, char *tag)
46 {
47   printf("Header start\n");
48   return (Gedcom_ctxt)0;
49 }
50
51 void header_end(Gedcom_ctxt self)
52 {
53   printf("Header end, context is %d\n", (int)self);
54 }
55
56 char family_xreftags[100][255];
57 int  family_nr = 0;
58
59 Gedcom_ctxt family_start(int level, char *xref, char *tag)
60 {
61   printf("Family start, xref is %s\n", xref);
62   strcpy(family_xreftags[family_nr], xref);
63   return (Gedcom_ctxt)(family_nr++);
64 }
65
66 void family_end(Gedcom_ctxt self)
67 {
68   printf("Family end, xref is %s\n", family_xreftags[(int)self]);
69 }
70
71 Gedcom_ctxt submit_start(int level, char *xref, char *tag)
72 {
73   printf("Submitter, xref is %s\n", xref);
74   return (Gedcom_ctxt)10000;
75 }
76
77 Gedcom_ctxt source_start(Gedcom_ctxt parent, int level, char *tag,
78                          char* raw_value, Gedcom_val parsed_value)
79 {
80   Gedcom_ctxt self = (Gedcom_ctxt)((int) parent + 1000);
81   printf("Source is %s (ctxt is %d, parent is %d)\n",
82          GEDCOM_STRING(parsed_value), (int) self, (int) parent);
83   return self;
84 }
85
86 void source_end(Gedcom_ctxt parent, Gedcom_ctxt self, Gedcom_val parsed_value)
87 {
88   printf("Source context %d in parent %d\n", (int)self, (int)parent);
89 }
90
91 Gedcom_ctxt source_date_start(Gedcom_ctxt parent, int level, char *tag,
92                               char* raw_value, Gedcom_val parsed_value)
93 {
94   struct date_value dv;
95   Gedcom_ctxt self = (Gedcom_ctxt)((int) parent + 1000);
96   dv = GEDCOM_DATE(parsed_value);
97   printf("Contents of the date_value:\n");
98   printf("  raw value: %s\n", raw_value);
99   printf("  type: %d\n", dv.type);
100   printf("  date1:\n");
101   printf("    calendar type: %d\n", dv.date1.cal);
102   printf("    day: %s\n", dv.date1.day_str);
103   printf("    month: %s\n", dv.date1.month_str);
104   printf("    year: %s\n", dv.date1.year_str);
105   printf("    date type: %d\n", dv.date1.type);
106   printf("    sdn1: %ld\n", dv.date1.sdn1);
107   printf("    sdn2: %ld\n", dv.date1.sdn2);
108   printf("  date2:\n");
109   printf("    calendar type: %d\n", dv.date2.cal);
110   printf("    day: %s\n", dv.date2.day_str);
111   printf("    month: %s\n", dv.date2.month_str);
112   printf("    year: %s\n", dv.date2.year_str);
113   printf("    date type: %d\n", dv.date2.type);
114   printf("    sdn1: %ld\n", dv.date2.sdn1);
115   printf("    sdn2: %ld\n", dv.date2.sdn2);
116   printf("  phrase: %s\n", dv.phrase);
117   return self;
118 }
119
120 void default_cb(Gedcom_ctxt ctxt, int level, char *tag, char *raw_value)
121 {
122   printf("== %d %s %s (ctxt is %d)\n", level, tag, raw_value, (int)ctxt);
123 }
124
125 void subscribe_callbacks()
126 {
127   gedcom_subscribe_to_record(REC_HEAD, header_start, header_end);
128   gedcom_subscribe_to_record(REC_FAM,  family_start, family_end);
129   gedcom_subscribe_to_record(REC_SUBM, submit_start, NULL);
130   gedcom_subscribe_to_element(ELT_HEAD_SOUR, source_start, source_end);
131   gedcom_subscribe_to_element(ELT_SOUR_DATA_EVEN_DATE,
132                               source_date_start, NULL);
133 }
134
135 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
136 {
137   if (type == MESSAGE)
138     fprintf(stderr, "MESSAGE: ");
139   else if (type == WARNING)
140     fprintf(stderr, "WARNING: ");
141   else if (type == ERROR)
142     fprintf(stderr, "ERROR: ");
143   fprintf(stderr, msg);
144 }
145
146 int main(int argc, char* argv[])
147 {
148   Gedcom_err_mech mech = IMMED_FAIL;
149   int compat_enabled = 1;
150   int debug_level = 0;
151   int run_times   = 1;
152   int result      = 0;
153   char* file_name = NULL;
154
155   if (argc > 1) {
156     int i;
157     for (i=1; i<argc; i++) {
158       if (!strncmp(argv[i], "-da", 4))
159         debug_level = 2;
160       else if (!strncmp(argv[i], "-dg", 4))
161         debug_level = 1;
162       else if (!strncmp(argv[i], "-fi", 4))
163         mech = IMMED_FAIL;
164       else if (!strncmp(argv[i], "-fd", 4))
165         mech = DEFER_FAIL;
166       else if (!strncmp(argv[i], "-fn", 4))
167         mech = IGNORE_ERRORS;
168       else if (!strncmp(argv[i], "-nc", 4))
169         compat_enabled = 0;
170       else if (!strncmp(argv[i], "-h", 3)) {
171         show_help();
172         exit(1);
173       }
174       else if (!strncmp(argv[i], "-2", 3)) {
175         run_times = 2;
176       }
177       else if (!strncmp(argv[i], "-3", 3)) {
178         run_times = 3;
179       }
180       else if (strncmp(argv[i], "-", 1)) {
181         file_name = argv[i];
182         break;
183       }
184       else {
185         printf ("Unrecognized option: %s\n", argv[i]);
186         show_help();
187         exit(1);
188       }
189     }
190   }
191   
192   if (!file_name) {
193     printf("No file name given\n");
194     show_help();
195     exit(1);
196   }
197
198   gedcom_set_debug_level(debug_level, NULL);
199   gedcom_set_compat_handling(compat_enabled);
200   gedcom_set_error_handling(mech);
201   gedcom_set_message_handler(gedcom_message_handler);
202   gedcom_set_default_callback(default_cb);
203   
204   subscribe_callbacks();
205   while (run_times-- > 0) {
206     result |= gedcom_parse_file(file_name);
207   }
208   if (result == 0) {
209     printf("Parse succeeded\n");
210     return 0;
211   }
212   else {
213     printf("Parse failed\n");
214     return 1;
215   }  
216 }