Put GEDCOM_PARSE as source program (unless disabled).
[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     result = gom_delete_address(&head->source.corporation.address);
94     if (result != 0)
95       return 1;
96
97     for (i=0; i<3; i++) {
98       if (head->source.corporation.phone[i]) {
99         value = gom_set_string(&head->source.corporation.phone[i], NULL);
100         if (value != NULL)
101           return 1;
102       }
103     }
104     
105     return 0;
106   }
107 }
108
109 int main(int argc, char* argv[])
110 {
111   Gedcom_err_mech mech = DEFER_FAIL;
112   int compat_enabled   = 1;
113   int debug_level = 0;
114   int keep_source = 0;
115   char* file_name = NULL;
116   int result;
117   char* extension = "new";
118   
119   if (argc > 1) {
120     int i;
121     for (i=1; i<argc; i++) {
122       if (!strncmp(argv[i], "-da", 4))
123         debug_level = 2;
124       else if (!strncmp(argv[i], "-dg", 4))
125         debug_level = 1;
126       else if (!strncmp(argv[i], "-s", 3))
127         keep_source = 1;
128       else if (!strncmp(argv[i], "-e", 3)) {
129         if (i<argc) {
130           extension = argv[++i];
131         }
132         else {
133           show_help();
134           exit(1);
135         }
136       }
137       else if (!strncmp(argv[i], "-h", 3)) {
138         show_help();
139         exit(1);
140       }
141       else if (strncmp(argv[i], "-", 1)) {
142         file_name = argv[i];
143         break;
144       }
145       else {
146         printf ("Unrecognized option: %s\n", argv[i]);
147         show_help();
148         exit(1);
149       }
150     }
151   }
152   
153   if (!file_name) {
154     printf("No file name given\n");
155     show_help();
156     exit(1);
157   }
158   
159   gedcom_init();
160   setlocale(LC_ALL, "");
161   gedcom_set_debug_level(debug_level, NULL);
162   gedcom_set_compat_handling(compat_enabled);
163   gedcom_set_error_handling(mech);
164   gedcom_set_message_handler(gedcom_message_handler);
165
166   result = gom_parse_file(file_name);
167   
168   if (result == 0) {
169     char* newfile = (char*)malloc(strlen(file_name) + strlen(extension) + 2);
170     sprintf(newfile, "%s.%s", file_name, extension);
171     printf(_("Parse succeeded, now writing file '%s'\n"), newfile);
172     if (! keep_source) {
173       result = update_header();
174     }
175     if (result == 0)
176       result = gom_write_file(newfile, NULL);
177     free(newfile);
178     if (result == 0) {
179       printf(_("Write succeeded\n"));
180     }
181     else {
182       printf(_("Write failed\n"));
183     }
184   }
185   else {
186     printf(_("Parse failed\n"));
187   }
188   return result;
189 }