1 /***************************************************************************
2  *   copyright           : (C) 2002 by Hendrik Sattler                     *
3  *   mail                : post@hendrik-sattler.de                         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  ***************************************************************************/
11 
12 //own headers
13 #include <charsets.h>
14 #include <helper.h>
15 #include <gtincl.h>
16 #include "ucs4.h"
17 
18 //standard headers
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <iconv.h>
23 
convert_to_internal(const char * from_code,char * input,size_t insize)24 ucs4char_t* convert_to_internal (const char* from_code,
25 				 char* input,
26 				 size_t insize)
27 {
28   iconv_t cd;
29 
30   ucs4char_t* outbuf;
31   char* outptr;
32   size_t outsize;
33 
34   if (from_code == NULL || input == NULL || insize == 0) return NULL;
35 
36   cd = iconv_open(ucs4_get_iconv_charset(),from_code);
37   if (cd == (iconv_t)-1) {
38     fprintf(stderr,"%s: %s\n",_("Error on text conversion"), strerror(errno));
39     exit(EXIT_FAILURE);
40   }
41 
42   outsize = insize*sizeof(ucs4char_t);
43   outbuf  = mem_alloc(outsize+sizeof(ucs4char_t),1);
44   outptr  = (char*)outbuf;
45 
46   if (iconv(cd,(ICONV_CAST)&input,&insize,&outptr,&outsize) == (size_t)-1) {
47     fprintf(stderr,_("Error on text conversion from charset \"%s\" to charset \"%s\": %s\n"),
48 	    from_code,ucs4_get_iconv_charset(),strerror(errno));
49     exit(EXIT_FAILURE);
50   }
51   iconv_close(cd);
52 
53   return mem_realloc(outbuf,(ucs4len(outbuf)+1)*sizeof(ucs4char_t));
54 }
55