Init_encodings should be done earlier (at gedcom_init).
[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 = 0;
30
31 typedef int (*lex_func)(void);
32 lex_func lf;
33
34 #define NEW_MODEL_FILE "new.ged"
35
36 int lexer_init(Encoding enc, FILE* f)
37 {
38   if (enc == ONE_BYTE) {
39     lf  = &gedcom_1byte_lex;
40     gedcom_1byte_myinit(f);
41     set_encoding_width(enc);
42     return open_conv_to_internal("ASCII");
43   }
44   else if (enc == TWO_BYTE_HILO) {
45     lf  = &gedcom_hilo_lex;
46     gedcom_hilo_myinit(f);
47     set_encoding_width(enc);
48     return open_conv_to_internal("UNICODE");
49   }
50   else if (enc == TWO_BYTE_LOHI) {
51     lf  = &gedcom_lohi_lex;
52     gedcom_lohi_myinit(f);
53     set_encoding_width(enc);
54     return open_conv_to_internal("UNICODE");
55   }
56   else {
57     return 0;
58   }
59 }
60
61 void lexer_close()
62 {
63   close_conv_to_internal();
64 }
65
66 int gedcom_lex()
67 {
68   return (*lf)();
69 }
70
71 int determine_encoding(FILE* f)
72 {
73   char first[2];
74   int read;
75
76   read = fread(first, 1, 2, f);
77   if (read != 2) {
78     gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
79     return ONE_BYTE;
80   }
81   else if ((first[0] == '0') && (first[1] == ' ')) {
82     gedcom_debug_print(_("One-byte encoding"));
83     if (fseek(f, 0, 0) != 0)
84       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
85     return ONE_BYTE;
86   }
87   else if ((first[0] == '\0') && (first[1] == '0'))
88   {
89     gedcom_debug_print(_("Two-byte encoding, high-low"));
90     if (fseek(f, 0, 0) != 0)
91       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
92     return TWO_BYTE_HILO;
93   }
94   else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
95   {
96     gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
97     return TWO_BYTE_HILO;
98   }
99   else if ((first[0] == '0') && (first[1] == '\0'))
100   {
101     gedcom_debug_print(_("Two-byte encoding, low-high"));
102     if (fseek(f, 0, 0) != 0)
103       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
104     return TWO_BYTE_LOHI;
105   }
106   else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
107   {
108     gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
109     return TWO_BYTE_LOHI;
110   }
111   else {
112     gedcom_warning(_("Unknown encoding, falling back to one-byte"));
113     if (fseek(f, 0, 0) != 0)
114       gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
115     return ONE_BYTE;
116   }
117 }
118
119 int init_called = 0;
120
121 int gedcom_init()
122 {
123   init_called = 1;
124   update_gconv_search_path();
125   init_encodings();
126   if (!setlocale(LC_ALL, "")
127       || ! bindtextdomain(PACKAGE, LOCALEDIR)
128       || ! bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING))
129     return 1;
130   else
131     return 0;
132 }
133
134 int gedcom_parse_file(const char* file_name)
135 {
136   Encoding enc;
137   int result = 1;
138   FILE* file;
139
140   if (!init_called) {
141     gedcom_error(_("Internal error: GEDCOM parser not initialized"));
142   }
143   else {
144     file = fopen(file_name, "r");
145     if (!file) {
146       gedcom_error(_("Could not open file '%s': %s"),
147                    file_name, strerror(errno));
148     }
149     else {
150       line_no = 1;
151       enc = determine_encoding(file);
152       
153       if (lexer_init(enc, file)) {
154         line_no = 0;
155         make_xref_table();
156         result = gedcom_parse();
157         line_no = 0;
158         if (result == 0)
159           result = check_xref_table();
160       }
161       lexer_close();
162       fclose(file);
163     }
164   }
165
166   return result;
167 }
168
169 int gedcom_new_model()
170 {
171   int result = 1;
172   FILE* file;
173
174   file = fopen(NEW_MODEL_FILE, "r");
175   if (file) {
176     fclose(file);
177     result = gedcom_parse_file(NEW_MODEL_FILE);
178   }
179   else {
180     char* filename = (char*) malloc(strlen(PKGDATADIR) + strlen(NEW_MODEL_FILE)
181                                     + 2);
182     if (!filename)
183       MEMORY_ERROR;
184     else {
185       sprintf(filename, "%s/%s", PKGDATADIR, NEW_MODEL_FILE);
186       result = gedcom_parse_file(filename);
187       free(filename);
188     }
189   }
190   return result;
191 }
192
193 int gedcom_check_version(int major, int minor, int patch)
194 {
195   if (major < GEDCOM_PARSE_VERSION_MAJOR)
196     return 1;
197   else if (major > GEDCOM_PARSE_VERSION_MAJOR)
198     return 0;
199   else if (minor < GEDCOM_PARSE_VERSION_MINOR)
200     return 1;
201   else if (minor > GEDCOM_PARSE_VERSION_MINOR)
202     return 0;
203   else if (patch <= GEDCOM_PARSE_VERSION_PATCH)
204     return 1;
205   else
206     return 0;
207 }