Move to LGPL license.
[gedcom-parse.git] / gedcom / multilex.c
1 /* The lexer multiplexer for Gedcom.
2    Copyright (C) 2001 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
28 int line_no;
29
30 typedef int (*lex_func)(void);
31 lex_func lf;
32
33 int lexer_init(ENCODING enc, FILE* f)
34 {
35   if (enc == ONE_BYTE) {
36     gedcom_1byte_in = f;
37     lf = &gedcom_1byte_lex;
38     set_encoding_width(enc);
39     return open_conv_to_internal("ASCII");
40   }
41   else if (enc == TWO_BYTE_HILO) {
42     gedcom_hilo_in = f;
43     lf = &gedcom_hilo_lex;
44     set_encoding_width(enc);
45     return open_conv_to_internal("UNICODE");
46   }
47   else if (enc == TWO_BYTE_LOHI) {
48     gedcom_lohi_in = f;
49     lf = &gedcom_lohi_lex;
50     set_encoding_width(enc);
51     return open_conv_to_internal("UNICODE");
52   }
53   else {
54     return 0;
55   }
56 }
57
58 void lexer_close()
59 {
60   close_conv_to_internal();
61 }
62
63 int gedcom_lex()
64 {
65   return (*lf)();
66 }
67
68 int determine_encoding(FILE* f)
69 {
70   char first[2];
71
72   fread(first, 1, 2, f);
73   if ((first[0] == '0') && (first[1] == ' ')) {
74     gedcom_message("One-byte encoding");
75     fseek(f, 0, 0);
76     return ONE_BYTE;
77   }
78   else if ((first[0] == '\0') && (first[1] == '0'))
79   {
80     gedcom_message("Two-byte encoding, high-low");
81     fseek(f, 0, 0);
82     return TWO_BYTE_HILO;
83   }
84   else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
85   {
86     gedcom_message("Two-byte encoding, high-low, with BOM");
87     return TWO_BYTE_HILO;
88   }
89   else if ((first[0] == '0') && (first[1] == '\0'))
90   {
91     gedcom_message("Two-byte encoding, low-high");
92     fseek(f, 0, 0);
93     return TWO_BYTE_LOHI;
94   }
95   else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
96   {
97     gedcom_message("Two-byte encoding, low-high, with BOM");
98     return TWO_BYTE_LOHI;
99   }
100   else {
101     gedcom_message("Unknown encoding, falling back to one-byte");
102     fseek(f, 0, 0);
103     return ONE_BYTE;
104   }
105 }
106
107 int gedcom_parse_file(char* file_name)
108 {
109   ENCODING enc;
110   int result = 1;
111   FILE* file = fopen (file_name, "r");
112   line_no = 1;
113   if (!file) {
114     gedcom_error("Could not open file '%s'\n", file_name);
115     return 1;
116   }
117
118   init_encodings();
119   enc = determine_encoding(file);
120   
121   if (lexer_init(enc, file)) {
122     result = gedcom_parse();
123   }
124   lexer_close();
125   fclose(file);
126   
127   return result;
128 }
129