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