From 2b3d150620970b011372749df22c03651c0e8208 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?=
 =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= <lrn1986@gmail.com>
Date: Sat, 24 Dec 2011 23:09:04 +0400
Subject: [PATCH] New charset conversion functions

---
 src/include/gnunet_strings_lib.h |   23 ++++++++++++++++++++
 src/util/strings.c               |   43 +++++++++++++++++++++++++++++++------
 2 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index 4efecc8..edeccaf 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);
 
 /**
  * Convert the len characters long character sequence
+ * given in input that is in the given input charset
+ * to a string in given output charset.
+ * @return the converted string (0-terminated),
+ *  if conversion fails, a copy of the orignal
+ *  string is returned.
+ */
+char *
+GNUNET_STRINGS_conv (const char *input, size_t len,
+    const char *input_charset, const char *output_charset);
+
+/**
+ * Convert the len characters long character sequence
  * given in input that is in the given charset
  * to UTF-8.
  *
@@ -98,6 +110,17 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
 char *
 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset);
 
+/**
+ * Convert the len bytes-long UTF-8 string
+ * given in input to the given charset.
+
+ * @return the converted string (0-terminated),
+ *  if conversion fails, a copy of the orignal
+ *  string is returned.
+ */
+char *
+GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset);
+
 
 /**
  * Complete filename (a la shell) from abbrevition.
diff --git a/src/util/strings.c b/src/util/strings.c
index 2b5538b..bd0a8eb 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,
   return GNUNET_OK;
 }
 
-
 /**
  * Convert the len characters long character sequence
- * given in input that is in the given charset
- * to UTF-8.
+ * given in input that is in the given input charset
+ * to a string in given output charset.
  * @return the converted string (0-terminated),
  *  if conversion fails, a copy of the orignal
  *  string is returned.
  */
 char *
-GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
+GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
 {
   char *ret;
 
@@ -348,12 +347,12 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
   char *itmp;
   iconv_t cd;
 
-  cd = iconv_open ("UTF-8", charset);
+  cd = iconv_open (output_charset, input_charset);
   if (cd == (iconv_t) - 1)
   {
     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
-    LOG (GNUNET_ERROR_TYPE_WARNING, _("Character set requested was `%s'\n"),
-         charset);
+    LOG (GNUNET_ERROR_TYPE_WARNING, _("Character sets requested were `%s'->`%s'\n"),
+         input_charset, output_charset);
     ret = GNUNET_malloc (len + 1);
     memcpy (ret, input, len);
     ret[len] = '\0';
@@ -396,6 +395,36 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
 
 
 /**
+ * Convert the len characters long character sequence
+ * given in input that is in the given charset
+ * to UTF-8.
+ * @return the converted string (0-terminated),
+ *  if conversion fails, a copy of the orignal
+ *  string is returned.
+ */
+char *
+GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
+{
+  return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
+}
+
+/**
+ * Convert the len bytes-long UTF-8 string
+ * given in input to the given charset.
+
+ * @return the converted string (0-terminated),
+ *  if conversion fails, a copy of the orignal
+ *  string is returned.
+ */
+char *
+GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
+{
+  return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
+}
+
+
+
+/**
  * Complete filename (a la shell) from abbrevition.
  * @param fil the name of the file, may contain ~/ or
  *        be relative to the current directory
-- 
1.7.4

