-/* This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
+/* The lexer multiplexer for Gedcom.
+ Copyright (C) 2001,2002 The Genes Development Team
+ This file is part of the Gedcom parser library.
+ Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
- (C) 2001 by The Genes Development Team
- Original author: Peter Verthez (Peter.Verthez@advalvas.be)
-*/
+ The Gedcom parser library is free software; you can redistribute it
+ and/or modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The Gedcom parser library is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the Gedcom parser library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
/* $Id$ */
/* $Name$ */
#include "gedcom_internal.h"
#include "multilex.h"
#include "encoding.h"
+#include "xref.h"
int line_no;
int determine_encoding(FILE* f)
{
char first[2];
+ int read;
- fread(first, 1, 2, f);
- if ((first[0] == '0') && (first[1] == ' ')) {
- gedcom_message("One-byte encoding");
- fseek(f, 0, 0);
+ read = fread(first, 1, 2, f);
+ if (read != 2) {
+ gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
+ return ONE_BYTE;
+ }
+ else if ((first[0] == '0') && (first[1] == ' ')) {
+ gedcom_debug_print(_("One-byte encoding"));
+ if (fseek(f, 0, 0) != 0)
+ gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
return ONE_BYTE;
}
else if ((first[0] == '\0') && (first[1] == '0'))
{
- gedcom_message("Two-byte encoding, high-low");
- fseek(f, 0, 0);
+ gedcom_debug_print(_("Two-byte encoding, high-low"));
+ if (fseek(f, 0, 0) != 0)
+ gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
return TWO_BYTE_HILO;
}
else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
{
- gedcom_message("Two-byte encoding, high-low, with BOM");
+ gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
return TWO_BYTE_HILO;
}
else if ((first[0] == '0') && (first[1] == '\0'))
{
- gedcom_message("Two-byte encoding, low-high");
- fseek(f, 0, 0);
+ gedcom_debug_print(_("Two-byte encoding, low-high"));
+ if (fseek(f, 0, 0) != 0)
+ gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
return TWO_BYTE_LOHI;
}
else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
{
- gedcom_message("Two-byte encoding, low-high, with BOM");
+ gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
return TWO_BYTE_LOHI;
}
else {
- gedcom_message("Unknown encoding, falling back to one-byte");
- fseek(f, 0, 0);
+ gedcom_warning(_("Unknown encoding, falling back to one-byte"));
+ if (fseek(f, 0, 0) != 0)
+ gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
return ONE_BYTE;
}
}
-int gedcom_parse_file(char* file_name)
+static int init_called = 0;
+
+void gedcom_init()
+{
+ init_called = 1;
+ update_gconv_search_path();
+}
+
+int gedcom_parse_file(const char* file_name)
{
ENCODING enc;
int result = 1;
- FILE* file = fopen (file_name, "r");
- line_no = 1;
- if (!file) {
- gedcom_error("Could not open file '%s'\n", file_name);
- return 1;
- }
+ FILE* file;
+ char *locale, *save_locale, *save_textdom;
- init_encodings();
- enc = determine_encoding(file);
+ locale = setlocale(LC_ALL, NULL);
+ if (! locale) {
+ gedcom_error(_("Could not retrieve locale information"));
+ return result;
+ }
- if (lexer_init(enc, file)) {
- result = gedcom_parse();
+ save_locale = strdup(locale);
+ if (! save_locale) {
+ MEMORY_ERROR;
+ return result;
}
- lexer_close();
- fclose(file);
+ save_textdom = textdomain(NULL);
+ if (!save_textdom) {
+ gedcom_error(_("Could not retrieve locale domain: %s"), strerror(errno));
+ return result;
+ }
+
+ if (! setlocale(LC_ALL, "")
+ || ! bindtextdomain(PACKAGE, LOCALEDIR)
+ || ! bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING)
+ || ! textdomain(PACKAGE)) {
+ gedcom_error(_("Could not set locale: %s"), strerror(errno));
+ return result;
+ }
+
+ if (!init_called) {
+ gedcom_error(_("Internal error: GEDCOM parser not initialized"));
+ }
+ else {
+ line_no = 1;
+ file = fopen(file_name, "r");
+ if (!file) {
+ gedcom_error(_("Could not open file '%s': %s"),
+ file_name, strerror(errno));
+ }
+ else {
+ init_encodings();
+ enc = determine_encoding(file);
+
+ if (lexer_init(enc, file)) {
+ line_no = 1;
+ make_xref_table();
+ result = gedcom_parse();
+ if (result == 0)
+ result = check_xref_table();
+ }
+ lexer_close();
+ fclose(file);
+ }
+ }
+
+ if (! textdomain(save_textdom)
+ || ! setlocale(LC_ALL, save_locale)) {
+ gedcom_error(_("Could not restore locale: %s"), strerror(errno));
+ return result;
+ }
+ free(save_locale);
return result;
}