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)
20 #define INTERNAL_ENCODING "UTF8"
21 #define ENCODING_CONF_FILE "gedcom.enc"
24 static iconv_t cd_to_internal = (iconv_t) -1;
25 static char int_buf[MAXGEDCLINELEN*2];
26 static void *encoding_mapping = NULL;
27 static ENCODING the_enc = ONE_BYTE;
34 char* charwidth_string[] = { "1", "2_HILO", "2_LOHI" };
36 int node_compare(const void *node1, const void *node2)
38 return strcmp(((const struct node *) node1)->gedcom_name,
39 ((const struct node *) node2)->gedcom_name);
42 void add_encoding(char *gedcom_n, char* charwidth, char *iconv_n)
45 struct node *nodeptr = (struct node *) malloc(sizeof *nodeptr);
46 nodeptr->gedcom_name = (char *) malloc(strlen(gedcom_n)
47 + strlen(charwidth) + 3);
48 nodeptr->iconv_name = (char *) malloc(strlen(iconv_n) + 1);
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;
62 char buffer[MAXBUF + 1];
63 sprintf(buffer, "%s(%s)", gedcom_n, charwidth_string[enc]);
64 search_node.gedcom_name = buffer;
65 datum = tfind(&search_node, &encoding_mapping, node_compare);
67 gedcom_error("No encoding found for '%s'", gedcom_n);
71 return ((const struct node *) *datum)->iconv_name;
77 if (encoding_mapping == NULL) {
79 char buffer[MAXBUF + 1];
80 char gedcom_n[MAXBUF + 1];
81 char charwidth[MAXBUF + 1];
82 char iconv_n[MAXBUF + 1];
83 in = fopen(ENCODING_CONF_FILE, "r");
85 while (fgets(buffer, sizeof(buffer), in) != NULL) {
86 if (buffer[strlen(buffer) - 1] != '\n') {
87 gedcom_error("Line too long in encoding configuration file '%s'",
91 else if ((buffer[0] != '#') && (strcmp(buffer, "\n") != 0)) {
92 if (sscanf(buffer, "%s %s %s", gedcom_n, charwidth, iconv_n) == 3) {
93 add_encoding(gedcom_n, charwidth, iconv_n);
96 gedcom_error("Missing data in encoding configuration file '%s'",
105 gedcom_warning("Could not open encoding configuration file '%s'",
111 void set_encoding_width(ENCODING enc)
116 static char conv_buf[MAXGEDCLINELEN * 2];
117 static int conv_buf_size;
119 int open_conv_to_internal(char* fromcode)
121 char *encoding = get_encoding(fromcode, the_enc);
122 if (cd_to_internal != (iconv_t) -1)
123 iconv_close(cd_to_internal);
124 if (encoding == NULL) {
125 cd_to_internal = (iconv_t) -1;
128 memset(conv_buf, 0, sizeof(conv_buf));
130 cd_to_internal = iconv_open(INTERNAL_ENCODING, encoding);
131 if (cd_to_internal == (iconv_t) -1) {
132 gedcom_error("Error opening conversion context for encoding %s: %s",
133 encoding, strerror(errno));
136 return (cd_to_internal != (iconv_t) -1);
139 void close_conv_to_internal()
141 iconv_close(cd_to_internal);
142 cd_to_internal = (iconv_t) -1;
145 char* to_internal(char* str, size_t len)
148 size_t outsize = MAXGEDCLINELEN * 2;
149 char *wrptr = int_buf;
150 char *rdptr = conv_buf;
151 /* set up input buffer (concatenate to what was left previous time) */
152 /* can't use strcpy, because possible null bytes from unicode */
153 memcpy(conv_buf + conv_buf_size, str, len);
154 conv_buf_size += len;
155 insize = conv_buf_size;
156 /* set up output buffer (empty it) */
157 memset(int_buf, 0, sizeof(int_buf));
158 /* do the conversion */
159 iconv(cd_to_internal, &rdptr, &insize, &wrptr, &outsize);
160 /* then shift what is left over to the head of the input buffer */
161 memmove(conv_buf, rdptr, insize);
162 memset(conv_buf + insize, 0, sizeof(conv_buf) - insize);
163 conv_buf_size = insize;