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
21 #include "gedcom_internal.h"
24 #include "encoding_state.h"
27 struct encoding_state read_encoding;
28 /* SYS_NEWLINE is defined in config.h */
29 struct encoding_state write_encoding =
30 { "ASCII", "ASCII", ONE_BYTE, WITHOUT_BOM, SYS_NEWLINE };
32 Enc_from write_encoding_from = ENC_FROM_FILE;
33 Enc_from write_terminator_from = ENC_FROM_SYS;
35 const char* terminator[] = {
38 /* END_CR_LF */ "\x0D\x0A",
39 /* END_LF_CR */ "\x0A\x0D"
42 void set_read_encoding(const char* charset, const char* encoding)
44 strncpy(read_encoding.charset, charset, MAX_CHARSET_LEN);
45 read_encoding.encoding = encoding;
46 gedcom_debug_print("Encoding state is now: ");
47 gedcom_debug_print(" charset : %s", read_encoding.charset);
48 gedcom_debug_print(" encoding : %s", read_encoding.encoding);
49 gedcom_debug_print(" width : %d", read_encoding.width);
50 gedcom_debug_print(" BOM : %d", read_encoding.bom);
51 gedcom_debug_print(" terminator: 0x%02x 0x%02x",
52 read_encoding.terminator[0],
53 read_encoding.terminator[1]);
56 void set_read_encoding_width(Encoding enc)
58 read_encoding.width = enc;
61 void set_read_encoding_bom(Enc_bom bom)
63 read_encoding.bom = bom;
66 void set_read_encoding_terminator(char* term)
68 strncpy(read_encoding.terminator, term, MAX_TERMINATOR_LEN);
71 int gedcom_write_set_encoding(Enc_from from, const char* new_charset,
72 Encoding width, Enc_bom bom)
74 char* new_encoding = NULL;
75 if (from == ENC_FROM_SYS) {
78 write_encoding_from = from;
79 if (from == ENC_MANUAL) {
80 if (!strcmp(new_charset, "UNICODE")) {
81 if (width == ONE_BYTE) {
82 gedcom_error(_("Unicode cannot be encoded into one byte"));
86 new_encoding = get_encoding(new_charset, width);
88 write_encoding.encoding = new_encoding;
89 write_encoding.width = width;
90 write_encoding.bom = bom;
91 strncpy(write_encoding.charset, new_charset, MAX_CHARSET_LEN);
98 new_encoding = get_encoding(new_charset, ONE_BYTE);
100 write_encoding.encoding = new_encoding;
101 write_encoding.width = ONE_BYTE;
102 write_encoding.bom = bom;
103 strncpy(write_encoding.charset, new_charset, MAX_CHARSET_LEN);
112 void init_write_encoding()
114 if (write_encoding_from == ENC_FROM_FILE
115 && read_encoding.charset[0] != '\0') {
116 strncpy(write_encoding.charset, read_encoding.charset, MAX_CHARSET_LEN);
117 write_encoding.encoding = read_encoding.encoding;
118 write_encoding.width = read_encoding.width;
119 write_encoding.bom = read_encoding.bom;
123 int gedcom_write_set_line_terminator(Enc_from from, Enc_line_end end)
125 const char* new_term = NULL;
126 write_terminator_from = from;
127 if (from == ENC_FROM_SYS) {
128 new_term = SYS_NEWLINE;
130 else if (from == ENC_MANUAL) {
131 new_term = terminator[end];
134 strncpy(write_encoding.terminator, new_term, MAX_TERMINATOR_LEN);
138 void init_write_terminator()
140 if (write_terminator_from == ENC_FROM_FILE
141 && read_encoding.terminator[0] != '\0') {
142 strncpy(write_encoding.terminator, read_encoding.terminator,