Full unicode support.
[gedcom-parse.git] / encoding.c
1 #include <string.h>
2 #include <iconv.h>
3 #include "gedcom.h"
4 #include "encoding.h"
5
6 #define INTERNAL_ENCODING "UTF8"
7
8 static iconv_t cd_to_internal = (iconv_t) -1;
9 static char int_buf[MAXGEDCLINELEN*2];
10
11 int open_conv_to_internal(char* fromcode)
12 {
13   if (cd_to_internal != (iconv_t) -1)
14     iconv_close(cd_to_internal);
15   cd_to_internal = iconv_open(INTERNAL_ENCODING, fromcode);
16   return (cd_to_internal != (iconv_t) -1);  
17 }
18
19 void close_conv_to_internal()
20 {
21   iconv_close(cd_to_internal);
22 }
23
24 char* to_internal(char* str, size_t len)
25 {
26   size_t insize = len;
27   size_t outsize = MAXGEDCLINELEN * 2;
28   char *wrptr = int_buf;
29   char *rdptr = str;
30   memset(int_buf, 0, sizeof(int_buf));
31   iconv(cd_to_internal, &rdptr, &insize, &wrptr, &outsize);
32   return int_buf;
33 }
34