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.
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.
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.
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
24 #include "gedcom_internal.h"
30 typedef int (*lex_func)(void);
33 int lexer_init(ENCODING enc, FILE* f)
35 if (enc == ONE_BYTE) {
37 lf = &gedcom_1byte_lex;
38 set_encoding_width(enc);
39 return open_conv_to_internal("ASCII");
41 else if (enc == TWO_BYTE_HILO) {
43 lf = &gedcom_hilo_lex;
44 set_encoding_width(enc);
45 return open_conv_to_internal("UNICODE");
47 else if (enc == TWO_BYTE_LOHI) {
49 lf = &gedcom_lohi_lex;
50 set_encoding_width(enc);
51 return open_conv_to_internal("UNICODE");
60 close_conv_to_internal();
68 int determine_encoding(FILE* f)
72 fread(first, 1, 2, f);
73 if ((first[0] == '0') && (first[1] == ' ')) {
74 gedcom_debug_print(_("One-byte encoding"));
78 else if ((first[0] == '\0') && (first[1] == '0'))
80 gedcom_debug_print(_("Two-byte encoding, high-low"));
84 else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
86 gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
89 else if ((first[0] == '0') && (first[1] == '\0'))
91 gedcom_debug_print(_("Two-byte encoding, low-high"));
95 else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
97 gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
101 gedcom_warning(_("Unknown encoding, falling back to one-byte"));
107 int gedcom_parse_file(char* file_name)
113 char *save_textdom = textdomain(NULL);
114 setlocale(LC_ALL, ""); /* In fact only necessary if main program doesn't
116 bindtextdomain(PACKAGE, LOCALEDIR);
117 bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING);
121 file = fopen(file_name, "r");
123 gedcom_error(_("Could not open file '%s'"), file_name);
127 enc = determine_encoding(file);
129 if (lexer_init(enc, file)) {
131 result = gedcom_parse();
137 textdomain(save_textdom);