Makefile for gedcom subdirectory.
[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 "gedcom.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(int level, char *xref, char *tag)
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(int level, char *xref, char *tag)
49 {
50   printf("Family start, xref is %s\n", xref);
51   strcpy(family_xreftags[family_nr], xref);
52   return (Gedcom_ctxt)(family_nr++);
53 }
54
55 void family_end(Gedcom_ctxt self)
56 {
57   printf("Family end, xref is %s\n", family_xreftags[(int)self]);
58 }
59
60 Gedcom_ctxt submit_start(int level, char *xref, char *tag)
61 {
62   printf("Submitter, xref is %s\n", xref);
63   return (Gedcom_ctxt)10000;
64 }
65
66 Gedcom_ctxt source_start(Gedcom_ctxt parent, int level, char *tag,
67                          char* raw_value, Gedcom_val parsed_value)
68 {
69   Gedcom_ctxt self = (Gedcom_ctxt)((int) parent + 1000);
70   printf("Source is %s (ctxt is %d, parent is %d)\n",
71          (char*)parsed_value, (int) self, (int) parent);
72   return self;
73 }
74
75 void source_end(Gedcom_ctxt parent, Gedcom_ctxt self, Gedcom_val parsed_value)
76 {
77   printf("Source context %d in parent %d\n", (int)self, (int)parent);
78 }
79
80 void default_cb(Gedcom_ctxt ctxt, int level, char *tag, char *raw_value)
81 {
82   printf("== %d %s %s (ctxt is %d)\n", level, tag, raw_value, (int)ctxt);
83 }
84
85 void subscribe_callbacks()
86 {
87   gedcom_subscribe_to_record(REC_HEAD, header_start, header_end);
88   gedcom_subscribe_to_record(REC_FAM,  family_start, family_end);
89   gedcom_subscribe_to_record(REC_SUBM, submit_start, NULL);
90   gedcom_subscribe_to_element(ELT_HEAD_SOUR, source_start, source_end);
91 }
92
93 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
94 {
95   if (type == MESSAGE)
96     fprintf(stderr, "MESSAGE: ");
97   else if (type == WARNING)
98     fprintf(stderr, "WARNING: ");
99   else if (type == ERROR)
100     fprintf(stderr, "ERROR: ");
101   fprintf(stderr, msg);
102 }
103
104 int main(int argc, char* argv[])
105 {
106   Gedcom_err_mech mech = IMMED_FAIL;
107   int compat_enabled = 1;
108   int debug_level = 0;
109   int run_times   = 1;
110   int result      = 0;
111   char* file_name = NULL;
112
113   if (argc > 1) {
114     int i;
115     for (i=1; i<argc; i++) {
116       if (!strncmp(argv[i], "-da", 4))
117         debug_level = 2;
118       else if (!strncmp(argv[i], "-dg", 4))
119         debug_level = 1;
120       else if (!strncmp(argv[i], "-fi", 4))
121         mech = IMMED_FAIL;
122       else if (!strncmp(argv[i], "-fd", 4))
123         mech = DEFER_FAIL;
124       else if (!strncmp(argv[i], "-fn", 4))
125         mech = IGNORE_ERRORS;
126       else if (!strncmp(argv[i], "-nc", 4))
127         compat_enabled = 0;
128       else if (!strncmp(argv[i], "-h", 3)) {
129         show_help();
130         exit(1);
131       }
132       else if (!strncmp(argv[i], "-2", 3)) {
133         run_times = 2;
134       }
135       else if (!strncmp(argv[i], "-3", 3)) {
136         run_times = 3;
137       }
138       else if (strncmp(argv[i], "-", 1)) {
139         file_name = argv[i];
140         break;
141       }
142       else {
143         printf ("Unrecognized option: %s\n", argv[i]);
144         show_help();
145         exit(1);
146       }
147     }
148   }
149   
150   if (!file_name) {
151     printf("No file name given\n");
152     show_help();
153     exit(1);
154   }
155
156   gedcom_set_debug_level(debug_level, NULL);
157   gedcom_set_compat_handling(compat_enabled);
158   gedcom_set_error_handling(mech);
159   gedcom_set_message_handler(gedcom_message_handler);
160   gedcom_set_default_callback(default_cb);
161   
162   subscribe_callbacks();
163   while (run_times-- > 0) {
164     result |= gedcom_parse_file(file_name);
165   }
166   if (result == 0) {
167     printf("Parse succeeded\n");
168     return 0;
169   }
170   else {
171     printf("Parse failed\n");
172     return 1;
173   }  
174 }