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)
17 #include "gedcom_internal.h"
20 #define INTERNAL_ENCODING "UTF8"
21 #define ENCODING_CONF_FILE "gedcom.enc"
24 static iconv_t cd_to_internal = (iconv_t) -1;
25 static void *encoding_mapping = NULL;
26 static ENCODING the_enc = ONE_BYTE;
33 char* charwidth_string[] = { "1", "2_HILO", "2_LOHI" };
35 int node_compare(const void *node1, const void *node2)
37 return strcmp(((const struct node *) node1)->gedcom_name,
38 ((const struct node *) node2)->gedcom_name);
41 void add_encoding(char *gedcom_n, char* charwidth, char *iconv_n)
44 struct node *nodeptr = (struct node *) malloc(sizeof *nodeptr);
45 nodeptr->gedcom_name = (char *) malloc(strlen(gedcom_n)
46 + strlen(charwidth) + 3);
47 nodeptr->iconv_name = (char *) malloc(strlen(iconv_n) + 1);
48 /* sprintf is safe here (malloc'ed before) */
49 sprintf(nodeptr->gedcom_name, "%s(%s)", gedcom_n, charwidth);
50 strcpy(nodeptr->iconv_name, iconv_n);
51 datum = tsearch(nodeptr, &encoding_mapping, node_compare);
52 if ((datum == NULL) || (*datum != nodeptr)) {
53 gedcom_warning("Duplicate entry found for encoding '%s', ignoring",
58 char* get_encoding(char* gedcom_n, ENCODING enc)
61 struct node search_node;
63 buffer = (char*)malloc(strlen(gedcom_n) + strlen(charwidth_string[enc]) + 3);
64 /* sprintf is safe here (malloc'ed before) */
65 sprintf(buffer, "%s(%s)", gedcom_n, charwidth_string[enc]);
66 search_node.gedcom_name = buffer;
67 datum = tfind(&search_node, &encoding_mapping, node_compare);
70 gedcom_error("No encoding found for '%s'", gedcom_n);
74 return ((const struct node *) *datum)->iconv_name;
80 if (encoding_mapping == NULL) {
82 char buffer[MAXBUF + 1];
83 char gedcom_n[MAXBUF + 1];
84 char charwidth[MAXBUF + 1];
85 char iconv_n[MAXBUF + 1];
86 in = fopen(ENCODING_CONF_FILE, "r");
88 while (fgets(buffer, sizeof(buffer), in) != NULL) {
89 if (buffer[strlen(buffer) - 1] != '\n') {
90 gedcom_error("Line too long in encoding configuration file '%s'",
94 else if ((buffer[0] != '#') && (strcmp(buffer, "\n") != 0)) {
95 if (sscanf(buffer, "%s %s %s", gedcom_n, charwidth, iconv_n) == 3) {
96 add_encoding(gedcom_n, charwidth, iconv_n);
99 gedcom_error("Missing data in encoding configuration file '%s'",
108 gedcom_warning("Could not open encoding configuration file '%s'",
114 void set_encoding_width(ENCODING enc)
119 static char conv_buf[MAXGEDCLINELEN * 2];
120 static size_t conv_buf_size;
122 int open_conv_to_internal(char* fromcode)
124 char *encoding = get_encoding(fromcode, the_enc);
125 if (cd_to_internal != (iconv_t) -1)
126 iconv_close(cd_to_internal);
127 if (encoding == NULL) {
128 cd_to_internal = (iconv_t) -1;
131 memset(conv_buf, 0, sizeof(conv_buf));
133 cd_to_internal = iconv_open(INTERNAL_ENCODING, encoding);
134 if (cd_to_internal == (iconv_t) -1) {
135 gedcom_error("Error opening conversion context for encoding %s: %s",
136 encoding, strerror(errno));
139 return (cd_to_internal != (iconv_t) -1);
142 void close_conv_to_internal()
144 iconv_close(cd_to_internal);
145 cd_to_internal = (iconv_t) -1;
148 char* to_internal(char* str, size_t len,
149 char* output_buffer, size_t out_len)
151 size_t outsize = out_len;
152 char *wrptr = output_buffer;
153 char *rdptr = conv_buf;
154 /* set up input buffer (concatenate to what was left previous time) */
155 /* can't use strcpy, because possible null bytes from unicode */
156 memcpy(conv_buf + conv_buf_size, str, len);
157 conv_buf_size += len;
158 /* set up output buffer (empty it) */
159 memset(output_buffer, 0, out_len);
160 /* do the conversion */
161 iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
162 /* then shift what is left over to the head of the input buffer */
163 memmove(conv_buf, rdptr, conv_buf_size);
164 memset(conv_buf + conv_buf_size, 0, sizeof(conv_buf) - conv_buf_size);
165 return output_buffer;