1 /* This program is free software; you can redistribute it and/or modify *
2 * it under the terms of the GNU General Public License as published by *
3 * the Free Software Foundation; either version 2 of the License, or *
4 * (at your option) any later version. *
6 (C) 2001 by The Genes Development Team
7 Original author: Peter Verthez (Peter.Verthez@advalvas.be)
13 #include "gedcom_internal.h"
19 typedef int (*lex_func)(void);
22 int lexer_init(ENCODING enc, FILE* f)
24 if (enc == ONE_BYTE) {
26 lf = &gedcom_1byte_lex;
27 set_encoding_width(enc);
28 return open_conv_to_internal("ASCII");
30 else if (enc == TWO_BYTE_HILO) {
32 lf = &gedcom_hilo_lex;
33 set_encoding_width(enc);
34 return open_conv_to_internal("UNICODE");
36 else if (enc == TWO_BYTE_LOHI) {
38 lf = &gedcom_lohi_lex;
39 set_encoding_width(enc);
40 return open_conv_to_internal("UNICODE");
49 close_conv_to_internal();
57 int determine_encoding(FILE* f)
61 fread(first, 1, 2, f);
62 if ((first[0] == '0') && (first[1] == ' ')) {
63 gedcom_message("One-byte encoding");
67 else if ((first[0] == '\0') && (first[1] == '0'))
69 gedcom_message("Two-byte encoding, high-low");
73 else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
75 gedcom_message("Two-byte encoding, high-low, with BOM");
78 else if ((first[0] == '0') && (first[1] == '\0'))
80 gedcom_message("Two-byte encoding, low-high");
84 else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
86 gedcom_message("Two-byte encoding, low-high, with BOM");
90 gedcom_message("Unknown encoding, falling back to one-byte");
96 int gedcom_parse_file(char* file_name)
100 FILE* file = fopen (file_name, "r");
103 gedcom_error("Could not open file '%s'\n", file_name);
108 enc = determine_encoding(file);
110 if (lexer_init(enc, file)) {
111 result = gedcom_parse();