Program to check gedcom files.
[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("Errors, warnings, ... are sent to stdout\n");
51 }
52
53 void default_cb(Gedcom_elt elt UNUSED, Gedcom_ctxt ctxt UNUSED,
54                 int level UNUSED, char *tag UNUSED,
55                 char *raw_value UNUSED, int tag_value UNUSED)
56 {
57   /* do nothing */
58 }
59
60 void gedcom_message_handler(Gedcom_msg_type type UNUSED, char *msg)
61 {
62   char *converted = NULL;
63   int  conv_fails = 0;
64   converted = convert_utf8_to_locale(msg, &conv_fails);
65   printf("%s\n", converted);
66 }
67
68 int main(int argc, char* argv[])
69 {
70   Gedcom_err_mech mech = DEFER_FAIL;
71   int compat_enabled   = 0;
72   char* file_name = NULL;
73   int result;
74   
75   if (argc > 1) {
76     int i;
77     for (i=1; i<argc; i++) {
78       if (!strncmp(argv[i], "-c", 3))
79         compat_enabled = 1;
80       else if (!strncmp(argv[i], "-h", 3)) {
81         show_help();
82         exit(1);
83       }
84       else if (strncmp(argv[i], "-", 1)) {
85         file_name = argv[i];
86         break;
87       }
88       else {
89         printf ("Unrecognized option: %s\n", argv[i]);
90         show_help();
91         exit(1);
92       }
93     }
94   }
95   
96   if (!file_name) {
97     printf("No file name given\n");
98     show_help();
99     exit(1);
100   }
101   
102   gedcom_init();
103   setlocale(LC_ALL, "");
104   gedcom_set_compat_handling(compat_enabled);
105   gedcom_set_error_handling(mech);
106   gedcom_set_message_handler(gedcom_message_handler);
107   gedcom_set_default_callback(default_cb);
108
109   result = gedcom_parse_file(file_name);
110   
111   if (result == 0) {
112     printf(_("Parse succeeded\n"));
113   }
114   else {
115     printf(_("Parse failed\n"));
116   }
117   return result;
118 }