X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gedcom%2Fmultilex.c;h=437f0bdaccc0542879da1d9ee60cb79aee6b21a0;hb=c27f416da8583f06adb78c6c23e11b43a6949f3c;hp=8473624bad70c4c75899b715de7229b241788e9f;hpb=057bd8173b055b22794d7e544a1b6ddd4f60ff96;p=gedcom-parse.git diff --git a/gedcom/multilex.c b/gedcom/multilex.c index 8473624..437f0bd 100644 --- a/gedcom/multilex.c +++ b/gedcom/multilex.c @@ -1,5 +1,5 @@ /* The lexer multiplexer for Gedcom. - Copyright (C) 2001 The Genes Development Team + Copyright (C) 2001,2002 The Genes Development Team This file is part of the Gedcom parser library. Contributed by Peter Verthez , 2001. @@ -24,6 +24,7 @@ #include "gedcom_internal.h" #include "multilex.h" #include "encoding.h" +#include "xref.h" int line_no; @@ -68,17 +69,24 @@ int gedcom_lex() int determine_encoding(FILE* f) { char first[2]; + int read; - fread(first, 1, 2, f); - if ((first[0] == '0') && (first[1] == ' ')) { + 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")); - fseek(f, 0, 0); + 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_debug_print(_("Two-byte encoding, high-low")); - fseek(f, 0, 0); + 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')) @@ -89,7 +97,8 @@ int determine_encoding(FILE* f) else if ((first[0] == '0') && (first[1] == '\0')) { gedcom_debug_print(_("Two-byte encoding, low-high")); - fseek(f, 0, 0); + 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')) @@ -99,41 +108,85 @@ int determine_encoding(FILE* f) } else { gedcom_warning(_("Unknown encoding, falling back to one-byte")); - fseek(f, 0, 0); + 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; + char *locale, *save_locale, *save_textdom; + + locale = setlocale(LC_ALL, NULL); + if (! locale) { + gedcom_error(_("Could not retrieve locale information")); + return result; + } - char *save_textdom = textdomain(NULL); - setlocale(LC_ALL, ""); /* In fact only necessary if main program doesn't - do this */ - bindtextdomain(PACKAGE, LOCALEDIR); - textdomain(PACKAGE); + save_locale = strdup(locale); + if (! save_locale) { + MEMORY_ERROR; + return result; + } + + 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; + } - line_no = 1; - file = fopen(file_name, "r"); - if (!file) { - gedcom_error(_("Could not open file '%s'"), file_name); + if (!init_called) { + gedcom_error(_("Internal error: GEDCOM parser not initialized")); } else { - init_encodings(); - enc = determine_encoding(file); - - if (lexer_init(enc, file)) { - line_no = 1; - result = gedcom_parse(); + 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); } - lexer_close(); - fclose(file); } - textdomain(save_textdom); + 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; }