diff options
author | LRN <lrn1986@gmail.com> | 2012-10-30 21:08:43 +0000 |
---|---|---|
committer | LRN <lrn1986@gmail.com> | 2012-10-30 21:08:43 +0000 |
commit | f74b67b30e97b871ae37575b64179668b15abab0 (patch) | |
tree | 32126352c861d69132c31314c4a3512ed5c783d5 | |
parent | c95ddb3eac0292511af734768441444098b90be9 (diff) | |
download | gnunet-f74b67b30e97b871ae37575b64179668b15abab0.tar.gz gnunet-f74b67b30e97b871ae37575b64179668b15abab0.zip |
Allow using libunistring for string conversion
-rw-r--r-- | src/util/strings.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index 08d23e5ad..36ec6d0e8 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -416,6 +416,44 @@ GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, c | |||
416 | if (0 != iconv_close (cd)) | 416 | if (0 != iconv_close (cd)) |
417 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close"); | 417 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close"); |
418 | return ret; | 418 | return ret; |
419 | #elif ENABLE_NLS /* libunistring is a mandatory dependency \o/ ! */ | ||
420 | uint8_t *u8_string; | ||
421 | char *encoded_string; | ||
422 | size_t u8_string_length; | ||
423 | size_t encoded_string_length; | ||
424 | |||
425 | u8_string = u8_conv_from_encoding (input_charset, iconveh_error, input, len, NULL, NULL, &u8_string_length); | ||
426 | if (NULL == u8_string) | ||
427 | { | ||
428 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); | ||
429 | ret = GNUNET_malloc (len + 1); | ||
430 | memcpy (ret, input, len); | ||
431 | ret[len] = '\0'; | ||
432 | return ret; | ||
433 | } | ||
434 | if (strcmp (output_charset, "UTF-8") == 0) | ||
435 | { | ||
436 | ret = GNUNET_malloc (u8_string_length + 1); | ||
437 | memcpy (ret, u8_string, u8_string_length); | ||
438 | ret[u8_string_length] = '\0'; | ||
439 | free (u8_string); | ||
440 | return ret; | ||
441 | } | ||
442 | encoded_string = u8_conv_to_encoding (output_charset, iconveh_error, u8_string, u8_string_length, NULL, NULL, &encoded_string_length); | ||
443 | free (u8_string); | ||
444 | if (NULL == encoded_string) | ||
445 | { | ||
446 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); | ||
447 | ret = GNUNET_malloc (len + 1); | ||
448 | memcpy (ret, input, len); | ||
449 | ret[len] = '\0'; | ||
450 | return ret; | ||
451 | } | ||
452 | ret = GNUNET_malloc (encoded_string_length + 1); | ||
453 | memcpy (ret, encoded_string, encoded_string_length); | ||
454 | ret[encoded_string_length] = '\0'; | ||
455 | free (encoded_string); | ||
456 | return ret; | ||
419 | #else | 457 | #else |
420 | ret = GNUNET_malloc (len + 1); | 458 | ret = GNUNET_malloc (len + 1); |
421 | memcpy (ret, input, len); | 459 | memcpy (ret, input, len); |