Further specification of interface: message handler.
[gedcom-parse.git] / encoding.c
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.                                   *
5
6  (C) 2001 by The Genes Development Team
7  Original author: Peter Verthez (Peter.Verthez@advalvas.be)
8 */
9
10 /* $Id$ */
11 /* $Name$ */
12
13 #include <string.h>
14 #include <iconv.h>
15 #include <search.h>
16 #include <stdio.h>
17 #include "gedcom.h"
18 #include "encoding.h"
19
20 #define INTERNAL_ENCODING "UTF8"
21 #define ENCODING_CONF_FILE "gedcom.enc"
22 #define MAXBUF 255
23
24 static iconv_t cd_to_internal = (iconv_t) -1;
25 static void *encoding_mapping = NULL;
26 static ENCODING the_enc = ONE_BYTE;
27
28 struct node {
29   char *gedcom_name;
30   char *iconv_name;
31 };
32
33 char* charwidth_string[] = { "1", "2_HILO", "2_LOHI" };
34
35 int node_compare(const void *node1, const void *node2)
36 {
37   return strcmp(((const struct node *) node1)->gedcom_name,
38                 ((const struct node *) node2)->gedcom_name);
39 }
40
41 void add_encoding(char *gedcom_n, char* charwidth, char *iconv_n)
42 {
43   void **datum;
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(nodeptr->gedcom_name, "%s(%s)", gedcom_n, charwidth);
49   strcpy(nodeptr->iconv_name, iconv_n);
50   datum = tsearch(nodeptr, &encoding_mapping, node_compare);
51   if ((datum == NULL) || (*datum != nodeptr)) {
52     gedcom_warning("Duplicate entry found for encoding '%s', ignoring",
53                    gedcom_n);
54   }
55 }
56
57 char* get_encoding(char* gedcom_n, ENCODING enc)
58 {
59   void **datum;
60   struct node search_node;
61   char buffer[MAXBUF + 1];
62   sprintf(buffer, "%s(%s)", gedcom_n, charwidth_string[enc]);
63   search_node.gedcom_name = buffer;
64   datum = tfind(&search_node, &encoding_mapping, node_compare);
65   if (datum == NULL) {
66     gedcom_error("No encoding found for '%s'", gedcom_n);
67     return NULL;
68   }
69   else {
70     return ((const struct node *) *datum)->iconv_name;
71   }
72 }
73
74 void init_encodings()
75 {
76   if (encoding_mapping == NULL) {
77     FILE *in;
78     char buffer[MAXBUF + 1];
79     char gedcom_n[MAXBUF + 1];
80     char charwidth[MAXBUF + 1];
81     char iconv_n[MAXBUF + 1];
82     in = fopen(ENCODING_CONF_FILE, "r");
83     if (in != NULL) {
84       while (fgets(buffer, sizeof(buffer), in) != NULL) {
85         if (buffer[strlen(buffer) - 1] != '\n') {
86           gedcom_error("Line too long in encoding configuration file '%s'",
87                        ENCODING_CONF_FILE);
88           return;
89         }
90         else if ((buffer[0] != '#') && (strcmp(buffer, "\n") != 0)) {
91           if (sscanf(buffer, "%s %s %s", gedcom_n, charwidth, iconv_n) == 3) {
92             add_encoding(gedcom_n, charwidth, iconv_n);
93           }
94           else {
95             gedcom_error("Missing data in encoding configuration file '%s'",
96                          ENCODING_CONF_FILE);
97             return;
98           }
99         }
100       }
101       fclose(in);
102     }
103     else {
104       gedcom_warning("Could not open encoding configuration file '%s'",
105                      ENCODING_CONF_FILE);
106     }
107   }
108 }
109
110 void set_encoding_width(ENCODING enc)
111 {
112   the_enc = enc;
113 }
114
115 static char conv_buf[MAXGEDCLINELEN * 2];
116 static size_t conv_buf_size;
117
118 int open_conv_to_internal(char* fromcode)
119 {
120   char *encoding = get_encoding(fromcode, the_enc);
121   if (cd_to_internal != (iconv_t) -1)
122     iconv_close(cd_to_internal);
123   if (encoding == NULL) {
124     cd_to_internal = (iconv_t) -1;
125   }
126   else {
127     memset(conv_buf, 0, sizeof(conv_buf));
128     conv_buf_size = 0;
129     cd_to_internal = iconv_open(INTERNAL_ENCODING, encoding);
130     if (cd_to_internal == (iconv_t) -1) {
131       gedcom_error("Error opening conversion context for encoding %s: %s",
132                    encoding, strerror(errno));
133     }
134   }
135   return (cd_to_internal != (iconv_t) -1);  
136 }
137
138 void close_conv_to_internal()
139 {
140   iconv_close(cd_to_internal);
141   cd_to_internal = (iconv_t) -1;
142 }
143
144 char* to_internal(char* str, size_t len,
145                   char* output_buffer, size_t out_len)
146 {
147   size_t outsize = out_len;
148   char *wrptr = output_buffer;
149   char *rdptr = conv_buf;
150   /* set up input buffer (concatenate to what was left previous time) */
151   /* can't use strcpy, because possible null bytes from unicode */
152   memcpy(conv_buf + conv_buf_size, str, len);
153   conv_buf_size += len;
154   /* set up output buffer (empty it) */
155   memset(output_buffer, 0, out_len);
156   /* do the conversion */
157   iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
158   /* then shift what is left over to the head of the input buffer */
159   memmove(conv_buf, rdptr, conv_buf_size);
160   memset(conv_buf + conv_buf_size, 0, sizeof(conv_buf) - conv_buf_size);
161   return output_buffer;
162 }