Also allow debugging.
[gedcom-parse.git] / bin / gedcom-check.c
1 /* Check program using 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 "gedcom.h"
25 #include "utf8tools.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <libintl.h>
33
34 #define _(string) dgettext(PACKAGE, string)
35 #define N_(string) (string)
36
37 #ifdef __GNUC__
38 #define UNUSED __attribute__((unused))
39 #else
40 #define UNUSED
41 #endif
42
43 void show_help ()
44 {
45   printf("Checks a GEDCOM file on standards compliancy\n\n");
46   printf("Usage:  gedcom-check [options] file\n");
47   printf("Options:\n");
48   printf("  -h    Show this help text\n");
49   printf("  -c    Enable compatibility mode\n");
50   printf("  -dg   Debug setting: only libgedcom debug messages\n");
51   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
52   printf("Errors, warnings, ... are sent to stdout\n");
53 }
54
55 void default_cb(Gedcom_elt elt UNUSED, Gedcom_ctxt ctxt UNUSED,
56                 int level UNUSED, char *tag UNUSED,
57                 char *raw_value UNUSED, int tag_value UNUSED)
58 {
59   /* do nothing */
60 }
61
62 void gedcom_message_handler(Gedcom_msg_type type UNUSED, char *msg)
63 {
64   char *converted = NULL;
65   int  conv_fails = 0;
66   converted = convert_utf8_to_locale(msg, &conv_fails);
67   printf("%s\n", converted);
68 }
69
70 int main(int argc, char* argv[])
71 {
72   Gedcom_err_mech mech = DEFER_FAIL;
73   int compat_enabled   = 0;
74   int debug_level = 0;
75   char* file_name = NULL;
76   int result;
77   
78   if (argc > 1) {
79     int i;
80     for (i=1; i<argc; i++) {
81       if (!strncmp(argv[i], "-da", 4))
82         debug_level = 2;
83       else if (!strncmp(argv[i], "-dg", 4))
84         debug_level = 1;
85       else if (!strncmp(argv[i], "-c", 3))
86         compat_enabled = 1;
87       else if (!strncmp(argv[i], "-h", 3)) {
88         show_help();
89         exit(1);
90       }
91       else if (strncmp(argv[i], "-", 1)) {
92         file_name = argv[i];
93         break;
94       }
95       else {
96         printf ("Unrecognized option: %s\n", argv[i]);
97         show_help();
98         exit(1);
99       }
100     }
101   }
102   
103   if (!file_name) {
104     printf("No file name given\n");
105     show_help();
106     exit(1);
107   }
108   
109   gedcom_init();
110   setlocale(LC_ALL, "");
111   gedcom_set_debug_level(debug_level, NULL);
112   gedcom_set_compat_handling(compat_enabled);
113   gedcom_set_error_handling(mech);
114   gedcom_set_message_handler(gedcom_message_handler);
115   gedcom_set_default_callback(default_cb);
116
117   result = gedcom_parse_file(file_name);
118   
119   if (result == 0) {
120     printf(_("Parse succeeded\n"));
121   }
122   else {
123     printf(_("Parse failed\n"));
124   }
125   return result;
126 }