2 * Copyright (c) 2009-2010 Mika Laitio <lamikr@pilppa.org>
4 * This file and library is covered by the LGPL version 3, read LICENSE for details.
7 * - Created charencoding_internal.c on: Nov 25, 2009
23 #define SIZE_CALCULATION_TEMP_BUFFER_SIZE 8
25 static bool _locale_charset_initialized = false;
27 const char *get_locale_charset_alias()
29 if (_locale_charset_initialized == false) {
30 setlocale(LC_ALL, "");
31 _locale_charset_initialized = true;
33 return nl_langinfo(CODESET);
36 size_t calculate_target_charset_bytecount(iconv_t conversion_desc,
45 char tmp_buf[SIZE_CALCULATION_TEMP_BUFFER_SIZE];
49 // reset conversion descriptor to initial state
50 iconv(conversion_desc,
56 while (bcount_src > 0) {
58 bcount_trgt = SIZE_CALCULATION_TEMP_BUFFER_SIZE;
59 err_flg = iconv(conversion_desc,
64 if (err_flg == (size_t)(-1)) {
66 // too small tmp_buf error can be skipped
74 ret_val = ret_val + SIZE_CALCULATION_TEMP_BUFFER_SIZE - bcount_trgt;
79 printf("size: %d\n", ret_val);
83 char *str_iconv_encode(iconv_t conversion_desc,
92 size_t bcount_src_left;
93 size_t bcount_trgt_left;
98 *p_bcount = calculate_target_charset_bytecount(conversion_desc,
101 if (*p_bcount >= 0) {
102 ret_val = (char *)calloc(1, *p_bcount + 1);
103 if (ret_val != NULL) {
104 // reset conversion descriptor to initial state
105 iconv(conversion_desc,
111 bcount_src_left = bcount_src;
113 bcount_trgt_left = *p_bcount;
114 while (bcount_src_left > 0) {
116 ic_ret_val = iconv(conversion_desc,
121 if (ic_ret_val == (size_t)(-1)) {
134 // could not calculate the byte count