Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_txt / ecore_txt.c
1
2 #ifdef HAVE_CONFIG_H
3 # include <config.h>
4 #endif
5
6 #include <iconv.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "Ecore_Txt.h"
14 #include "ecore_txt_private.h"
15
16
17 /**
18  * To be documented.
19  * 
20  * FIXME: Finish this.
21  */
22 EAPI char *
23 ecore_txt_convert(const char *enc_from, const char *enc_to, const char *text)
24 {
25    iconv_t ic;
26    char *new_txt, *inp, *outp;
27    size_t inb, outb, outlen, tob, outalloc;
28    
29    if (!text) return NULL;
30    ic = iconv_open(enc_to, enc_from);
31    if (ic == (iconv_t)(-1)) return NULL;
32    new_txt  = malloc(64);
33    inb      = strlen(text);
34    outb     = 64;
35    inp      = (char*)text;
36    outp     = new_txt;
37    outalloc = 64;
38    outlen   = 0;
39    tob      = 0;
40
41    for (;;)
42      {
43         size_t count;
44
45         tob = outb;
46         count = iconv(ic, &inp, &inb, &outp, &outb);
47         outlen += tob - outb;
48         if (count == (size_t)(-1))
49           {
50              if (errno == E2BIG)
51                {
52                   new_txt = realloc(new_txt, outalloc + 64);
53                   outp = new_txt + outlen;
54                   outalloc += 64;
55                   outb += 64;
56                }
57              else if (errno == EILSEQ)
58                {
59                   if (new_txt) free(new_txt);
60                   new_txt = NULL;
61                   break;
62                }
63              else if (errno == EINVAL)
64                {
65                   if (new_txt) free(new_txt);
66                   new_txt = NULL;
67                   break;
68                }
69              else
70                {
71                   if (new_txt) free(new_txt);
72                   new_txt = NULL;
73                   break;
74                }
75           }
76         if (inb == 0)
77           {
78              if (outalloc == outlen) new_txt = realloc(new_txt, outalloc + 1);
79              new_txt[outlen] = 0;
80              break;
81           }
82      }
83    iconv_close(ic);
84    return new_txt;
85 }