diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-12-24 21:40:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-12-24 21:40:49 +0000 |
commit | e8d4e1edd97fb8cc6756a953681e1f6a2a5beb68 (patch) | |
tree | 555a58de8662318ef950c1c10a446c023003b7ba /src/util/strings.c | |
parent | 8feaaffac09035bd7203d456f5bf96d79fa49be8 (diff) | |
download | gnunet-e8d4e1edd97fb8cc6756a953681e1f6a2a5beb68.tar.gz gnunet-e8d4e1edd97fb8cc6756a953681e1f6a2a5beb68.zip |
-LRN: new charset conversion functions / #2031
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 43 |
1 files changed, 36 insertions, 7 deletions
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 | */ |
339 | char * | 338 | char * |
340 | GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) | 339 | GNUNET_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 | */ | ||
405 | char * | ||
406 | GNUNET_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 | */ | ||
419 | char * | ||
420 | GNUNET_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 |