Take care of const correctness.
[gedcom-parse.git] / gedcom / multilex.c
1 /* The lexer multiplexer for Gedcom.
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_internal.h"
25 #include "multilex.h"
26 #include "encoding.h"
27 #include "xref.h"
28
29 int line_no;
30
31 typedef int (*lex_func)(void);
32 lex_func lf;
33
34 int lexer_init(ENCODING enc, FILE* f)
35 {
36   if (enc == ONE_BYTE) {
37     gedcom_1byte_in = f;
38     lf = &gedcom_1byte_lex;
39     set_encoding_width(enc);
40     return open_conv_to_internal("ASCII");
41   }
42   else if (enc == TWO_BYTE_HILO) {
43     gedcom_hilo_in = f;
44     lf = &gedcom_hilo_lex;
45     set_encoding_width(enc);
46     return open_conv_to_internal("UNICODE");
47   }
48   else if (enc == TWO_BYTE_LOHI) {
49     gedcom_lohi_in = f;
50     lf = &gedcom_lohi_lex;
51     set_encoding_width(enc);
52     return open_conv_to_internal("UNICODE");
53   }
54   else {
55     return 0;
56   }
57 }
58
59 void lexer_close()
60 {
61   close_conv_to_internal();
62 }
63
64 int gedcom_lex()
65 {
66   return (*lf)();
67 }
68
69 int determine_encoding(FILE* f)
70 {
71   char first[2];
72   int read;
73
74   read = fread(first, 1, 2, f);
75   if (read != 2) {
76     gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
77     return ONE_BYTE;
78   }
79   else if ((first[0] == '0') && (first[1] == ' ')) {
80     gedcom_debug_print(_("One-byte encoding"));
81     if (fseek(f, 0, 0) != 0)
82       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
83     return ONE_BYTE;
84   }
85   else if ((first[0] == '\0') && (first[1] == '0'))
86   {
87     gedcom_debug_print(_("Two-byte encoding, high-low"));
88     if (fseek(f, 0, 0) != 0)
89       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
90     return TWO_BYTE_HILO;
91   }
92   else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
93   {
94     gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
95     return TWO_BYTE_HILO;
96   }
97   else if ((first[0] == '0') && (first[1] == '\0'))
98   {
99     gedcom_debug_print(_("Two-byte encoding, low-high"));
100     if (fseek(f, 0, 0) != 0)
101       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
102     return TWO_BYTE_LOHI;
103   }
104   else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
105   {
106     gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
107     return TWO_BYTE_LOHI;
108   }
109   else {
110     gedcom_warning(_("Unknown encoding, falling back to one-byte"));
111     if (fseek(f, 0, 0) != 0)
112       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
113     return ONE_BYTE;
114   }
115 }
116
117 static int init_called = 0;
118
119 void gedcom_init()
120 {
121   init_called = 1;
122   update_gconv_search_path();
123 }
124
125 int gedcom_parse_file(const char* file_name)
126 {
127   ENCODING enc;
128   int result = 1;
129   FILE* file;
130   char *locale, *save_locale, *save_textdom;
131
132   locale = setlocale(LC_ALL, NULL);
133   if (! locale) {
134     gedcom_error(_("Could not retrieve locale information"));
135     return result;
136   }
137   
138   save_locale  = strdup(locale);
139   if (! save_locale) {
140     MEMORY_ERROR;
141     return result;
142   }
143   
144   save_textdom = textdomain(NULL);
145   if (!save_textdom) {
146     gedcom_error(_("Could not retrieve locale domain: %s"), strerror(errno));
147     return result;
148   }
149   
150   if (! setlocale(LC_ALL, "")
151       || ! bindtextdomain(PACKAGE, LOCALEDIR)
152       || ! bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING)
153       || ! textdomain(PACKAGE)) {
154     gedcom_error(_("Could not set locale: %s"), strerror(errno));
155     return result;
156   }
157
158   if (!init_called) {
159     gedcom_error(_("Internal error: GEDCOM parser not initialized"));
160   }
161   else {
162     line_no = 1;
163     file = fopen(file_name, "r");
164     if (!file) {
165       gedcom_error(_("Could not open file '%s': %s"),
166                    file_name, strerror(errno));
167     }
168     else {
169       init_encodings();
170       enc = determine_encoding(file);
171       
172       if (lexer_init(enc, file)) {
173         line_no = 1;
174         make_xref_table();
175         result = gedcom_parse();
176         if (result == 0)
177           result = check_xref_table();
178       }
179       lexer_close();
180       fclose(file);
181     }
182   }
183
184   if (! textdomain(save_textdom)
185       || ! setlocale(LC_ALL, save_locale)) {
186     gedcom_error(_("Could not restore locale: %s"), strerror(errno));
187     return result;
188   }
189   free(save_locale);
190   return result;
191 }
192