Use def_elt_end instead of destroying the context directly.
[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 #define PROG_NAME "GEDCOM_PARSE"
45
46 void show_help ()
47 {
48   printf("Converts a GEDCOM file to strict standard GEDCOM\n\n");
49   printf("Usage:  gedcom-sanitize [options] file\n");
50   printf("Options:\n");
51   printf("  -h    Show this help text\n");
52   printf("  -dg   Debug setting: only libgedcom debug messages\n");
53   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
54   printf("  -e <extension>   Extension to give to file name (default 'new')\n");
55   printf("  -s    Keep source string (otherwise: changed to GEDCOM_PARSE)\n");
56   printf("Errors, warnings, ... are sent to stdout\n");
57 }
58
59 void gedcom_message_handler(Gedcom_msg_type type UNUSED, char *msg)
60 {
61   char *converted = NULL;
62   int  conv_fails = 0;
63   converted = convert_utf8_to_locale(msg, &conv_fails);
64   printf("%s\n", converted);
65 }
66
67 int update_header()
68 {
69   struct header* head = NULL;
70   head = gom_get_header();
71   if (head == NULL)
72     return 1;
73   else {
74     char* value;
75     int result, i;
76     
77     value = gom_set_string(&head->source.id, PROG_NAME);
78     if (value == NULL || strcmp (value, PROG_NAME))
79       return 1;
80
81     value = gom_set_string(&head->source.name, NULL);
82     if (value != NULL)
83       return 1;
84
85     value = gom_set_string(&head->source.version, VERSION);
86     if (value == NULL || strcmp (value, VERSION))
87       return 1;
88
89     value = gom_set_string(&head->source.corporation.name, NULL);
90     if (value != NULL)
91       return 1;
92
93     if (head->source.corporation.address) {
94       result = gom_delete_address(&head->source.corporation.address);
95       if (result != 0)
96         return 1;
97     }
98
99     for (i=0; i<3; i++) {
100       if (head->source.corporation.phone[i]) {
101         value = gom_set_string(&head->source.corporation.phone[i], NULL);
102         if (value != NULL)
103           return 1;
104       }
105     }
106     
107     return 0;
108   }
109 }
110
111 int main(int argc, char* argv[])
112 {
113   Gedcom_err_mech mech = DEFER_FAIL;
114   int compat_enabled   = 1;
115   int debug_level = 0;
116   int keep_source = 0;
117   char* file_name = NULL;
118   int result;
119   char* extension = "new";
120   
121   if (argc > 1) {
122     int i;
123     for (i=1; i<argc; i++) {
124       if (!strncmp(argv[i], "-da", 4))
125         debug_level = 2;
126       else if (!strncmp(argv[i], "-dg", 4))
127         debug_level = 1;
128       else if (!strncmp(argv[i], "-s", 3))
129         keep_source = 1;
130       else if (!strncmp(argv[i], "-e", 3)) {
131         if (i<argc) {
132           extension = argv[++i];
133         }
134         else {
135           show_help();
136           exit(1);
137         }
138       }
139       else if (!strncmp(argv[i], "-h", 3)) {
140         show_help();
141         exit(1);
142       }
143       else if (strncmp(argv[i], "-", 1)) {
144         file_name = argv[i];
145         break;
146       }
147       else {
148         printf ("Unrecognized option: %s\n", argv[i]);
149         show_help();
150         exit(1);
151       }
152     }
153   }
154   
155   if (!file_name) {
156     printf("No file name given\n");
157     show_help();
158     exit(1);
159   }
160   
161   gedcom_init();
162   setlocale(LC_ALL, "");
163   gedcom_set_debug_level(debug_level, NULL);
164   gedcom_set_compat_handling(compat_enabled);
165   gedcom_set_error_handling(mech);
166   gedcom_set_message_handler(gedcom_message_handler);
167
168   result = gom_parse_file(file_name);
169   
170   if (result == 0) {
171     char* newfile = (char*)malloc(strlen(file_name) + strlen(extension) + 2);
172     sprintf(newfile, "%s.%s", file_name, extension);
173     printf(_("Parse succeeded, now writing file '%s'\n"), newfile);
174     if (! keep_source) {
175       result = update_header();
176     }
177     if (result == 0)
178       result = gom_write_file(newfile, NULL);
179     free(newfile);
180     if (result == 0) {
181       printf(_("Write succeeded\n"));
182     }
183     else {
184       printf(_("Write failed\n"));
185     }
186   }
187   else {
188     printf(_("Parse failed\n"));
189   }
190   return result;
191 }