Make the use of gedcom_init mandatory.
[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
73   fread(first, 1, 2, f);
74   if ((first[0] == '0') && (first[1] == ' ')) {
75     gedcom_debug_print(_("One-byte encoding"));
76     fseek(f, 0, 0);
77     return ONE_BYTE;
78   }
79   else if ((first[0] == '\0') && (first[1] == '0'))
80   {
81     gedcom_debug_print(_("Two-byte encoding, high-low"));
82     fseek(f, 0, 0);
83     return TWO_BYTE_HILO;
84   }
85   else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
86   {
87     gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
88     return TWO_BYTE_HILO;
89   }
90   else if ((first[0] == '0') && (first[1] == '\0'))
91   {
92     gedcom_debug_print(_("Two-byte encoding, low-high"));
93     fseek(f, 0, 0);
94     return TWO_BYTE_LOHI;
95   }
96   else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
97   {
98     gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
99     return TWO_BYTE_LOHI;
100   }
101   else {
102     gedcom_warning(_("Unknown encoding, falling back to one-byte"));
103     fseek(f, 0, 0);
104     return ONE_BYTE;
105   }
106 }
107
108 static int init_called = 0;
109
110 void gedcom_init()
111 {
112   init_called = 1;
113   update_gconv_search_path();
114 }
115
116 int gedcom_parse_file(char* file_name)
117 {
118   ENCODING enc;
119   int result = 1;
120   FILE* file;
121   
122   char *save_locale  = strdup(setlocale(LC_ALL, NULL));
123   char *save_textdom = textdomain(NULL);
124   setlocale(LC_ALL, "");
125   bindtextdomain(PACKAGE, LOCALEDIR);
126   bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING);
127   textdomain(PACKAGE);
128
129   if (!init_called) {
130     gedcom_error(_("Internal error: GEDCOM parser not initialized"));
131   }
132   else {
133     line_no = 1;
134     file = fopen(file_name, "r");
135     if (!file) {
136       gedcom_error(_("Could not open file '%s'"), file_name);
137     }
138     else {
139       init_encodings();
140       enc = determine_encoding(file);
141       
142       if (lexer_init(enc, file)) {
143         line_no = 1;
144         make_xref_table();
145         result = gedcom_parse();
146         if (result == 0)
147           result = check_xref_table();
148       }
149       lexer_close();
150       fclose(file);
151     }
152   }
153
154   textdomain(save_textdom);
155   setlocale(LC_ALL, save_locale);
156   free(save_locale);
157   return result;
158 }
159