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.
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"
31 typedef int (*lex_func)(void);
34 #define NEW_MODEL_FILE "new.ged"
36 int lexer_init(ENCODING enc, FILE* f)
38 if (enc == ONE_BYTE) {
39 lf = &gedcom_1byte_lex;
40 gedcom_1byte_myinit(f);
41 set_encoding_width(enc);
42 return open_conv_to_internal("ASCII");
44 else if (enc == TWO_BYTE_HILO) {
45 lf = &gedcom_hilo_lex;
46 gedcom_hilo_myinit(f);
47 set_encoding_width(enc);
48 return open_conv_to_internal("UNICODE");
50 else if (enc == TWO_BYTE_LOHI) {
51 lf = &gedcom_lohi_lex;
52 gedcom_lohi_myinit(f);
53 set_encoding_width(enc);
54 return open_conv_to_internal("UNICODE");
63 close_conv_to_internal();
71 int determine_encoding(FILE* f)
76 read = fread(first, 1, 2, f);
78 gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
81 else if ((first[0] == '0') && (first[1] == ' ')) {
82 gedcom_debug_print(_("One-byte encoding"));
83 if (fseek(f, 0, 0) != 0)
84 gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
87 else if ((first[0] == '\0') && (first[1] == '0'))
89 gedcom_debug_print(_("Two-byte encoding, high-low"));
90 if (fseek(f, 0, 0) != 0)
91 gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
94 else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
96 gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
99 else if ((first[0] == '0') && (first[1] == '\0'))
101 gedcom_debug_print(_("Two-byte encoding, low-high"));
102 if (fseek(f, 0, 0) != 0)
103 gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
104 return TWO_BYTE_LOHI;
106 else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
108 gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
109 return TWO_BYTE_LOHI;
112 gedcom_warning(_("Unknown encoding, falling back to one-byte"));
113 if (fseek(f, 0, 0) != 0)
114 gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
124 update_gconv_search_path();
125 setlocale(LC_ALL, "");
128 int gedcom_parse_file(const char* file_name)
133 char *textdom, *save_textdom;
135 textdom = textdomain(NULL);
137 gedcom_error(_("Could not retrieve text domain: %s"), strerror(errno));
141 save_textdom = strdup(textdom);
142 if (! save_textdom) {
147 if (! bindtextdomain(PACKAGE, LOCALEDIR)
148 || ! bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING)
149 || ! textdomain(PACKAGE)) {
150 gedcom_error(_("Could not set text domain: %s"), strerror(errno));
155 gedcom_error(_("Internal error: GEDCOM parser not initialized"));
158 file = fopen(file_name, "r");
160 gedcom_error(_("Could not open file '%s': %s"),
161 file_name, strerror(errno));
165 enc = determine_encoding(file);
167 if (lexer_init(enc, file)) {
170 result = gedcom_parse();
173 result = check_xref_table();
180 if (! textdomain(save_textdom)) {
181 gedcom_error(_("Could not restore text domain: %s"), strerror(errno));
187 int gedcom_new_model()
192 file = fopen(NEW_MODEL_FILE, "r");
195 result = gedcom_parse_file(NEW_MODEL_FILE);
198 char* filename = (char*) malloc(strlen(PKGDATADIR) + strlen(NEW_MODEL_FILE)
203 sprintf(filename, "%s/%s", PKGDATADIR, NEW_MODEL_FILE);
204 result = gedcom_parse_file(filename);
211 int gedcom_check_version(int major, int minor, int patch)
213 if (major < GEDCOM_PARSE_VERSION_MAJOR)
215 else if (major > GEDCOM_PARSE_VERSION_MAJOR)
217 else if (minor < GEDCOM_PARSE_VERSION_MINOR)
219 else if (minor > GEDCOM_PARSE_VERSION_MINOR)
221 else if (patch <= GEDCOM_PARSE_VERSION_PATCH)