Renamed gedcom.h to gedcom_internal.h
[gedcom-parse.git] / encoding.c
index 6c342e0ccba4fe2817eab3f553287efe89a47c72..d306dfa7b347627b401f9e7e2308035db39cef2d 100644 (file)
@@ -1,8 +1,20 @@
+/*  This program is free software; you can redistribute it and/or modify  *
+ *  it under the terms of the GNU General Public License as published by  *
+ *  the Free Software Foundation; either version 2 of the License, or     *
+ *  (at your option) any later version.                                   *
+
+ (C) 2001 by The Genes Development Team
+ Original author: Peter Verthez (Peter.Verthez@advalvas.be)
+*/
+
+/* $Id$ */
+/* $Name$ */
+
 #include <string.h>
 #include <iconv.h>
 #include <search.h>
 #include <stdio.h>
-#include "gedcom.h"
+#include "gedcom_internal.h"
 #include "encoding.h"
 
 #define INTERNAL_ENCODING "UTF8"
@@ -10,7 +22,6 @@
 #define MAXBUF 255
 
 static iconv_t cd_to_internal = (iconv_t) -1;
-static char int_buf[MAXGEDCLINELEN*2];
 static void *encoding_mapping = NULL;
 static ENCODING the_enc = ONE_BYTE;
 
@@ -34,6 +45,7 @@ void add_encoding(char *gedcom_n, char* charwidth, char *iconv_n)
   nodeptr->gedcom_name = (char *) malloc(strlen(gedcom_n)
                                         + strlen(charwidth) + 3);
   nodeptr->iconv_name  = (char *) malloc(strlen(iconv_n) + 1);
+  /* sprintf is safe here (malloc'ed before) */
   sprintf(nodeptr->gedcom_name, "%s(%s)", gedcom_n, charwidth);
   strcpy(nodeptr->iconv_name, iconv_n);
   datum = tsearch(nodeptr, &encoding_mapping, node_compare);
@@ -47,10 +59,13 @@ char* get_encoding(char* gedcom_n, ENCODING enc)
 {
   void **datum;
   struct node search_node;
-  char buffer[MAXBUF + 1];
+  char *buffer;
+  buffer = (char*)malloc(strlen(gedcom_n) + strlen(charwidth_string[enc]) + 3);
+  /* sprintf is safe here (malloc'ed before) */
   sprintf(buffer, "%s(%s)", gedcom_n, charwidth_string[enc]);
   search_node.gedcom_name = buffer;
   datum = tfind(&search_node, &encoding_mapping, node_compare);
+  free(buffer);
   if (datum == NULL) {
     gedcom_error("No encoding found for '%s'", gedcom_n);
     return NULL;
@@ -76,7 +91,7 @@ void init_encodings()
                       ENCODING_CONF_FILE);
          return;
        }
-       else if (buffer[0] != '#') {
+       else if ((buffer[0] != '#') && (strcmp(buffer, "\n") != 0)) {
          if (sscanf(buffer, "%s %s %s", gedcom_n, charwidth, iconv_n) == 3) {
            add_encoding(gedcom_n, charwidth, iconv_n);
          }
@@ -102,7 +117,7 @@ void set_encoding_width(ENCODING enc)
 }
 
 static char conv_buf[MAXGEDCLINELEN * 2];
-static int conv_buf_size;
+static size_t conv_buf_size;
 
 int open_conv_to_internal(char* fromcode)
 {
@@ -116,6 +131,10 @@ int open_conv_to_internal(char* fromcode)
     memset(conv_buf, 0, sizeof(conv_buf));
     conv_buf_size = 0;
     cd_to_internal = iconv_open(INTERNAL_ENCODING, encoding);
+    if (cd_to_internal == (iconv_t) -1) {
+      gedcom_error("Error opening conversion context for encoding %s: %s",
+                  encoding, strerror(errno));
+    }
   }
   return (cd_to_internal != (iconv_t) -1);  
 }
@@ -126,25 +145,22 @@ void close_conv_to_internal()
   cd_to_internal = (iconv_t) -1;
 }
 
-char* to_internal(char* str, size_t len)
+char* to_internal(char* str, size_t len,
+                 char* output_buffer, size_t out_len)
 {
-  size_t insize;
-  size_t outsize = MAXGEDCLINELEN * 2;
-  char *wrptr = int_buf;
+  size_t outsize = out_len;
+  char *wrptr = output_buffer;
   char *rdptr = conv_buf;
   /* set up input buffer (concatenate to what was left previous time) */
   /* can't use strcpy, because possible null bytes from unicode */
   memcpy(conv_buf + conv_buf_size, str, len);
   conv_buf_size += len;
-  insize = conv_buf_size;
   /* set up output buffer (empty it) */
-  memset(int_buf, 0, sizeof(int_buf));
+  memset(output_buffer, 0, out_len);
   /* do the conversion */
-  iconv(cd_to_internal, &rdptr, &insize, &wrptr, &outsize);
+  iconv(cd_to_internal, &rdptr, &conv_buf_size, &wrptr, &outsize);
   /* then shift what is left over to the head of the input buffer */
-  memmove(conv_buf, rdptr, insize);
-  memset(conv_buf + insize, 0, sizeof(conv_buf) - insize);
-  conv_buf_size = insize;
-  return int_buf;
+  memmove(conv_buf, rdptr, conv_buf_size);
+  memset(conv_buf + conv_buf_size, 0, sizeof(conv_buf) - conv_buf_size);
+  return output_buffer;
 }
-