Add interface function gedcom_init(), to avoid problem with GCONV_PATH
[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 void gedcom_init()
109 {
110   update_gconv_search_path();
111 }
112
113 int gedcom_parse_file(char* file_name)
114 {
115   ENCODING enc;
116   int result = 1;
117   FILE* file;
118   
119   char *save_locale  = strdup(setlocale(LC_ALL, NULL));
120   char *save_textdom = textdomain(NULL);
121   setlocale(LC_ALL, "");
122   bindtextdomain(PACKAGE, LOCALEDIR);
123   bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING);
124   textdomain(PACKAGE);
125
126   line_no = 1;
127   file = fopen(file_name, "r");
128   if (!file) {
129     gedcom_error(_("Could not open file '%s'"), file_name);
130   }
131   else {
132     init_encodings();
133     enc = determine_encoding(file);
134     
135     if (lexer_init(enc, file)) {
136       line_no = 1;
137       make_xref_table();
138       result = gedcom_parse();
139       if (result == 0)
140         result = check_xref_table();
141     }
142     lexer_close();
143     fclose(file);
144   }
145
146   textdomain(save_textdom);
147   setlocale(LC_ALL, save_locale);
148   free(save_locale);
149   return result;
150 }
151