renamed the package to libgedcom-dev
[gedcom-parse.git] / t / src / gomtest.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 "gom.h"
25 #include "output.h"
26 #include "dump_gom.h"
27 #include "portability.h"
28 #include <stdio.h>
29 #include <locale.h>
30 #include "gedcom.h"
31
32 void show_help ()
33 {
34   printf("gedcom-parse test program for libgedcom and libgom\n\n");
35   printf("Usage:  gom_test [options] file\n");
36   printf("Options:\n");
37   printf("  -h    Show this help text\n");
38   printf("  -nc   Disable compatibility mode\n");
39   printf("  -fi   Fail immediately on errors\n");
40   printf("  -fd   Deferred fail on errors, but parse completely\n");
41   printf("  -fn   No fail on errors\n");
42   printf("  -dg   Debug setting: only libgedcom debug messages\n");
43   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
44   printf("  -q    No output to standard output\n");
45   printf("  -o <outfile>  File to generate output to (def. testgedcom.out)\n");
46 }
47
48 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
49 {
50   output(1, "%s\n", msg);
51 }
52
53 int main(int argc, char* argv[])
54 {
55   Gedcom_err_mech mech = IMMED_FAIL;
56   int compat_enabled = 1;
57   int debug_level = 0;
58   int result      = 0;
59   char* outfilename = NULL;
60   char* file_name = NULL;
61
62   if (argc > 1) {
63     int i;
64     for (i=1; i<argc; i++) {
65       if (!strncmp(argv[i], "-da", 4))
66         debug_level = 2;
67       else if (!strncmp(argv[i], "-dg", 4))
68         debug_level = 1;
69       else if (!strncmp(argv[i], "-fi", 4))
70         mech = IMMED_FAIL;
71       else if (!strncmp(argv[i], "-fd", 4))
72         mech = DEFER_FAIL;
73       else if (!strncmp(argv[i], "-fn", 4))
74         mech = IGNORE_ERRORS;
75       else if (!strncmp(argv[i], "-nc", 4))
76         compat_enabled = 0;
77       else if (!strncmp(argv[i], "-h", 3)) {
78         show_help();
79         exit(1);
80       }
81       else if (!strncmp(argv[i], "-q", 3)) {
82         output_set_quiet(1);
83       }
84       else if (!strncmp(argv[i], "-o", 3)) {
85         i++;
86         if (i < argc) {
87           outfilename = argv[i];
88         }
89         else {
90           printf ("Missing output file name\n");
91           show_help();
92           exit(1);
93         }
94       }
95       else if (strncmp(argv[i], "-", 1)) {
96         file_name = argv[i];
97         break;
98       }
99       else {
100         printf ("Unrecognized option: %s\n", argv[i]);
101         show_help();
102         exit(1);
103       }
104     }
105   }
106   
107   if (!file_name) {
108     printf("No file name given\n");
109     show_help();
110     exit(1);
111   }
112
113   gedcom_init();
114   setlocale(LC_ALL, "");
115   gedcom_set_debug_level(debug_level, NULL);
116   gedcom_set_compat_handling(compat_enabled);
117   gedcom_set_error_handling(mech);
118   gedcom_set_message_handler(gedcom_message_handler);
119
120   output_open(outfilename);
121   output(0, "\n=== Parsing file %s\n", simple_base_name(file_name));
122   result = gom_parse_file(file_name);
123   if (result == 0) {
124     output(1, "Parse succeeded\n");
125   }
126   else {
127     output(1, "Parse failed\n");
128   }
129   show_data();
130   output_close();
131   return result;
132 }