Converted to separate package.
[gedcom-parse.git] / utf8 / utf8-convert.c
index 9db6fdf77fd2859d9b50ca90656c086b0c44d959..9dbfaf50e8b15d1ca03613e458faa07ca0913f01 100644 (file)
@@ -1,15 +1,26 @@
 /* Encoding utility from UTF-8 to another charset and vice versa
    Copyright (C) 2001, 2002 Peter Verthez
 
-   Permission granted to do anything with this file that you want, as long
-   as the above copyright is retained in all copies.
-   THERE IS NO WARRANTY - USE AT YOUR OWN RISK
+   The UTF8 tools library is free software; you can redistribute it
+   and/or modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The Gedcom parser library is distributed in the hope that it will be
+   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the Gedcom parser library; if not, write to the
+   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.
 */
 
 /* $Id$ */
 /* $Name$ */
 
-#include "utf8.h"
+#include "utf8tools.h"
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #define INTERNAL_BUFFER 0
 #define EXTERNAL_BUFFER 1
 
-void reset_conv_buffer(struct conv_buffer* buf)
+struct conv_buffer {
+  char*   buffer;
+  size_t  size;
+  int     type;  /* For internal use */
+};
+
+struct convert {
+  iconv_t from_utf8;
+  iconv_t to_utf8;
+  struct conv_buffer* inbuf;
+  size_t  insize;
+  struct conv_buffer* outbuf;
+  char*   unknown;
+};
+
+void reset_conv_buffer(conv_buffer_t buf)
 {
   memset(buf->buffer, 0, buf->size);
 }
 
-struct conv_buffer* create_conv_buffer(int size)
+conv_buffer_t create_conv_buffer(int size)
 {
   struct conv_buffer* buf = NULL;
 
@@ -47,7 +73,7 @@ struct conv_buffer* create_conv_buffer(int size)
   return buf;
 }
 
-void free_conv_buffer(struct conv_buffer* buf)
+void free_conv_buffer(conv_buffer_t buf)
 {
   if (buf) {
     free(buf->buffer);
@@ -55,7 +81,7 @@ void free_conv_buffer(struct conv_buffer* buf)
   }
 }
 
-char* grow_conv_buffer(struct conv_buffer* buf, char* curr_pos)
+char* grow_conv_buffer(conv_buffer_t buf, char* curr_pos)
 {
   size_t outlen, new_size;
   char*  new_buffer;
@@ -142,7 +168,7 @@ int conversion_set_unknown(convert_t conv, const char* unknown)
   return result;
 }
 
-int conversion_set_output_buffer(convert_t conv, struct conv_buffer* buf)
+int conversion_set_output_buffer(convert_t conv, conv_buffer_t buf)
 {
   if (!conv)
     return 0;
@@ -172,7 +198,8 @@ void cleanup_utf8_conversion(convert_t conv)
   }
 }
 
-char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails)
+char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails,
+                       size_t* output_len)
 {
   size_t insize = strlen(input);
   size_t outsize;
@@ -231,12 +258,12 @@ char* convert_from_utf8(convert_t conv, const char* input, int* conv_fails)
     }
     nconv = iconv(conv->from_utf8, &inptr, &insize, &outptr, &outsize);
   }
+  if (output_len) *output_len = outptr - outbuf->buffer;
   return outbuf->buffer;
 }
 
-char* convert_to_utf8(convert_t conv, const char* input)
+char* convert_to_utf8(convert_t conv, const char* input, size_t input_len)
 {
-  size_t insize  = strlen(input);
   size_t outsize;
   ICONV_CONST char *inptr  = (ICONV_CONST char*) input;
   char   *outptr;
@@ -252,7 +279,7 @@ char* convert_to_utf8(convert_t conv, const char* input)
   outptr  = outbuf->buffer;
   outsize = outbuf->size;
   reset_conv_buffer(conv->outbuf);
-  nconv = iconv(conv->to_utf8, &inptr, &insize, &outptr, &outsize);
+  nconv = iconv(conv->to_utf8, &inptr, &input_len, &outptr, &outsize);
   while (nconv == (size_t)-1) {
     if (errno == E2BIG) {
       /* grow the output buffer */
@@ -271,7 +298,7 @@ char* convert_to_utf8(convert_t conv, const char* input)
       /* EBADF is an error which should be captured by the first if above */
       return NULL;
     }
-    nconv = iconv(conv->to_utf8, &inptr, &insize, &outptr, &outsize);
+    nconv = iconv(conv->to_utf8, &inptr, &input_len, &outptr, &outsize);
   }
   return outbuf->buffer;  
 }