Added program to convert a GEDCOM file to standard GEDCOM.
[gedcom-parse.git] / bin / gedcom-sanitize.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 "gom.h"
26 #include "utf8tools.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <libintl.h>
34
35 #define _(string) dgettext(PACKAGE, string)
36 #define N_(string) (string)
37
38 #ifdef __GNUC__
39 #define UNUSED __attribute__((unused))
40 #else
41 #define UNUSED
42 #endif
43
44 void show_help ()
45 {
46   printf("Converts a GEDCOM file to strict standard GEDCOM\n\n");
47   printf("Usage:  gedcom-sanitize [options] file\n");
48   printf("Options:\n");
49   printf("  -h    Show this help text\n");
50   printf("  -dg   Debug setting: only libgedcom debug messages\n");
51   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
52   printf("  -e <extension>   Extension to give to file name (default 'new')\n");
53   printf("Errors, warnings, ... are sent to stdout\n");
54 }
55
56 void gedcom_message_handler(Gedcom_msg_type type UNUSED, char *msg)
57 {
58   char *converted = NULL;
59   int  conv_fails = 0;
60   converted = convert_utf8_to_locale(msg, &conv_fails);
61   printf("%s\n", converted);
62 }
63
64 int main(int argc, char* argv[])
65 {
66   Gedcom_err_mech mech = DEFER_FAIL;
67   int compat_enabled   = 1;
68   int debug_level = 0;
69   char* file_name = NULL;
70   int result;
71   char* extension = "new";
72   
73   if (argc > 1) {
74     int i;
75     for (i=1; i<argc; i++) {
76       if (!strncmp(argv[i], "-da", 4))
77         debug_level = 2;
78       else if (!strncmp(argv[i], "-dg", 4))
79         debug_level = 1;
80       else if (!strncmp(argv[i], "-e", 3)) {
81         if (i<argc) {
82           extension = argv[++i];
83         }
84         else {
85           show_help();
86           exit(1);
87         }
88       }
89       else if (!strncmp(argv[i], "-h", 3)) {
90         show_help();
91         exit(1);
92       }
93       else if (strncmp(argv[i], "-", 1)) {
94         file_name = argv[i];
95         break;
96       }
97       else {
98         printf ("Unrecognized option: %s\n", argv[i]);
99         show_help();
100         exit(1);
101       }
102     }
103   }
104   
105   if (!file_name) {
106     printf("No file name given\n");
107     show_help();
108     exit(1);
109   }
110   
111   gedcom_init();
112   setlocale(LC_ALL, "");
113   gedcom_set_debug_level(debug_level, NULL);
114   gedcom_set_compat_handling(compat_enabled);
115   gedcom_set_error_handling(mech);
116   gedcom_set_message_handler(gedcom_message_handler);
117
118   result = gom_parse_file(file_name);
119   
120   if (result == 0) {
121     char* newfile = (char*)malloc(strlen(file_name) + strlen(extension) + 2);
122     sprintf(newfile, "%s.%s", file_name, extension);
123     printf(_("Parse succeeded, now writing file '%s'\n"), newfile);
124     result = gom_write_file(newfile, NULL);
125     free(newfile);
126     if (result == 0) {
127       printf(_("Write succeeded\n"));
128     }
129     else {
130       printf(_("Write failed\n"));
131     }
132   }
133   else {
134     printf(_("Parse failed\n"));
135   }
136   return result;
137 }