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