Copied from old documentation. Removed all Gedcom_val details.
[gedcom-parse.git] / gedcom / encoding_state.c
1 /* Encoding state.
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.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 #include "gedcom_internal.h"
22 #include "gedcom.h"
23 #include "encoding.h"
24 #include "encoding_state.h"
25 #include <string.h>
26
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 };
31
32 Enc_from write_encoding_from   = ENC_FROM_FILE;
33 Enc_from write_terminator_from = ENC_FROM_SYS;
34
35 const char* terminator[] = {
36   /* END_CR */     "\x0D",
37   /* END_LF */     "\x0A",
38   /* END_CR_LF */  "\x0D\x0A",
39   /* END_LF_CR */  "\x0A\x0D"
40 };
41
42 void set_read_encoding(const char* charset, const char* encoding)
43 {
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]);
54 }
55
56 void set_read_encoding_width(Encoding enc)
57 {
58   read_encoding.width = enc;
59 }
60
61 void set_read_encoding_bom(Enc_bom bom)
62 {
63   read_encoding.bom = bom;
64 }
65
66 void set_read_encoding_terminator(char* term)
67 {
68   strncpy(read_encoding.terminator, term, MAX_TERMINATOR_LEN);
69 }
70
71 int gedcom_write_set_encoding(Enc_from from, const char* new_charset,
72                               Encoding width, Enc_bom bom)
73 {
74   char* new_encoding = NULL;
75   if (from == ENC_FROM_SYS) {
76     return 1;
77   }
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"));
83         return 1;
84       }
85       else {
86         new_encoding = get_encoding(new_charset, width);
87         if (new_encoding) {
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);
92         }
93         else
94           return 1;
95       }
96     }
97     else {
98       new_encoding = get_encoding(new_charset, ONE_BYTE);
99       if (new_encoding) {
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);
104       }
105       else
106         return 1;
107     }
108   }
109   return 0;
110 }
111
112 void init_write_encoding()
113 {
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;
120   }
121 }
122
123 int gedcom_write_set_line_terminator(Enc_from from, Enc_line_end end)
124 {
125   const char* new_term = NULL;
126   write_terminator_from = from;
127   if (from == ENC_FROM_SYS) {
128     new_term = SYS_NEWLINE;
129   }
130   else if (from == ENC_MANUAL) {
131     new_term = terminator[end];
132   }
133   if (new_term)
134     strncpy(write_encoding.terminator, new_term, MAX_TERMINATOR_LEN);
135   return 0;
136 }
137
138 void init_write_terminator()
139 {
140   if (write_terminator_from == ENC_FROM_FILE
141       && read_encoding.terminator[0] != '\0') {
142     strncpy(write_encoding.terminator, read_encoding.terminator,
143             MAX_TERMINATOR_LEN);
144   }
145 }
146