aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-12-24 21:40:49 +0000
committerChristian Grothoff <christian@grothoff.org>2011-12-24 21:40:49 +0000
commite8d4e1edd97fb8cc6756a953681e1f6a2a5beb68 (patch)
tree555a58de8662318ef950c1c10a446c023003b7ba /src
parent8feaaffac09035bd7203d456f5bf96d79fa49be8 (diff)
downloadgnunet-e8d4e1edd97fb8cc6756a953681e1f6a2a5beb68.tar.gz
gnunet-e8d4e1edd97fb8cc6756a953681e1f6a2a5beb68.zip
-LRN: new charset conversion functions / #2031
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_strings_lib.h23
-rw-r--r--src/util/strings.c43
2 files changed, 59 insertions, 7 deletions
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index 4efecc863..edeccaf3b 100644
--- a/src/include/gnunet_strings_lib.h
+++ b/src/include/gnunet_strings_lib.h
@@ -87,6 +87,18 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
87 87
88/** 88/**
89 * Convert the len characters long character sequence 89 * Convert the len characters long character sequence
90 * given in input that is in the given input charset
91 * to a string in given output charset.
92 * @return the converted string (0-terminated),
93 * if conversion fails, a copy of the orignal
94 * string is returned.
95 */
96char *
97GNUNET_STRINGS_conv (const char *input, size_t len,
98 const char *input_charset, const char *output_charset);
99
100/**
101 * Convert the len characters long character sequence
90 * given in input that is in the given charset 102 * given in input that is in the given charset
91 * to UTF-8. 103 * to UTF-8.
92 * 104 *
@@ -98,6 +110,17 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
98char * 110char *
99GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset); 111GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset);
100 112
113/**
114 * Convert the len bytes-long UTF-8 string
115 * given in input to the given charset.
116
117 * @return the converted string (0-terminated),
118 * if conversion fails, a copy of the orignal
119 * string is returned.
120 */
121char *
122GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset);
123
101 124
102/** 125/**
103 * Complete filename (a la shell) from abbrevition. 126 * Complete filename (a la shell) from abbrevition.
diff --git a/src/util/strings.c b/src/util/strings.c
index 2b5538b35..bd0a8eb52 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -327,17 +327,16 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
327 return GNUNET_OK; 327 return GNUNET_OK;
328} 328}
329 329
330
331/** 330/**
332 * Convert the len characters long character sequence 331 * Convert the len characters long character sequence
333 * given in input that is in the given charset 332 * given in input that is in the given input charset
334 * to UTF-8. 333 * to a string in given output charset.
335 * @return the converted string (0-terminated), 334 * @return the converted string (0-terminated),
336 * if conversion fails, a copy of the orignal 335 * if conversion fails, a copy of the orignal
337 * string is returned. 336 * string is returned.
338 */ 337 */
339char * 338char *
340GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) 339GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
341{ 340{
342 char *ret; 341 char *ret;
343 342
@@ -348,12 +347,12 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
348 char *itmp; 347 char *itmp;
349 iconv_t cd; 348 iconv_t cd;
350 349
351 cd = iconv_open ("UTF-8", charset); 350 cd = iconv_open (output_charset, input_charset);
352 if (cd == (iconv_t) - 1) 351 if (cd == (iconv_t) - 1)
353 { 352 {
354 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open"); 353 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
355 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character set requested was `%s'\n"), 354 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character sets requested were `%s'->`%s'\n"),
356 charset); 355 input_charset, output_charset);
357 ret = GNUNET_malloc (len + 1); 356 ret = GNUNET_malloc (len + 1);
358 memcpy (ret, input, len); 357 memcpy (ret, input, len);
359 ret[len] = '\0'; 358 ret[len] = '\0';
@@ -396,6 +395,36 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
396 395
397 396
398/** 397/**
398 * Convert the len characters long character sequence
399 * given in input that is in the given charset
400 * to UTF-8.
401 * @return the converted string (0-terminated),
402 * if conversion fails, a copy of the orignal
403 * string is returned.
404 */
405char *
406GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
407{
408 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
409}
410
411/**
412 * Convert the len bytes-long UTF-8 string
413 * given in input to the given charset.
414
415 * @return the converted string (0-terminated),
416 * if conversion fails, a copy of the orignal
417 * string is returned.
418 */
419char *
420GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
421{
422 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
423}
424
425
426
427/**
399 * Complete filename (a la shell) from abbrevition. 428 * Complete filename (a la shell) from abbrevition.
400 * @param fil the name of the file, may contain ~/ or 429 * @param fil the name of the file, may contain ~/ or
401 * be relative to the current directory 430 * be relative to the current directory