diff lib/charset-conv.c @ 989:c99b134c6185

[mq]: charset2
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 01 Feb 2010 15:17:58 +0900
parents 63555c9744c2
children
line wrap: on
line diff
--- a/lib/charset-conv.c	Fri Aug 14 07:54:55 2009 +0900
+++ b/lib/charset-conv.c	Mon Feb 01 15:17:58 2010 +0900
@@ -19,10 +19,9 @@
 /*****************************************************************************/
 
 #include "gftp.h"
+#include <langinfo.h>
 static const char cvsid[] = "$Id: protocols.c 952 2008-01-24 23:31:26Z masneyb $";
 
-#if GLIB_MAJOR_VERSION > 1
-
 static /*@null@*/ char *
 _gftp_get_next_charset (char **curpos)
 {
@@ -74,187 +73,110 @@
           str, fromset, toset, error->message);
 }
 
-/*@null@*/ static char *
-_do_convert_string (gftp_request * request, int is_filename, int force_local,
-                    const char *str, size_t *dest_len, int from_utf8)
+
+char *
+gftp_string_to_utf8 (gftp_request *request, const char *str, size_t *dest_len)
 {
-  char *remote_charset = NULL, *default_charset = NULL, *ret, *fromset,
-      *toset, *stpos, *cur_charset;
-  GError * error;
-  gsize bread;
-
-  if (request == NULL)
-    return (NULL);
-
-  if (g_utf8_validate (str, -1, NULL) != from_utf8)
-    return (NULL);
-
-  error = NULL;
-//  gftp_lookup_request_option (request, "remote_charset", &remote_charset);
-  remote_charset = request->remote_charset;
-  gftp_lookup_global_option ("default_charset", &default_charset);
-  if(!remote_charset)
-      remote_charset = (default_charset && *default_charset != '\0')
-          ? default_charset : "UTF8";
+    gchar *remote_charset = NULL, *default_charset = NULL, *ret;
+    GError *error = NULL;
 
-  if (*remote_charset == '\0' || request->use_local_encoding ||
-      force_local == 1)
-    {
-      if (from_utf8)
-        {
-          if (is_filename)
-            ret = g_filename_from_utf8 (str, -1, &bread, dest_len, &error);
-          else
-            ret = g_locale_from_utf8 (str, -1, &bread, dest_len, &error);
-        }
-      else
-        {
-          if (is_filename)
-            ret = g_filename_to_utf8 (str, -1, &bread, dest_len, &error);
-          else
-            ret = g_locale_to_utf8 (str, -1, &bread, dest_len, &error);
-        }
+    gftp_lookup_global_option ("default_charset", &default_charset);
+    if(!default_charset)
+        default_charset = "UTF-8";
 
-      if (ret == NULL)
-        _do_show_iconv_error (str, request->iconv_charset, from_utf8, error);
-
-      return (ret);
-    }
+    if(request)
+        remote_charset = request->remote_charset;
+    if(!remote_charset)
+        remote_charset = default_charset;
 
-  if (from_utf8)
-    {
-      if (request->iconv_from_initialized)
-        {
-          ret = g_convert_with_iconv (str, -1, request->iconv_from, &bread, dest_len,
-                                      &error);
-          if (ret == NULL)
-            _do_show_iconv_error (str, request->iconv_charset, from_utf8, error);
+    ret = g_convert_with_fallback(str, -1, "UTF-8", remote_charset, "?", NULL, dest_len, &error);
+
+    return ret;
+}
 
-          return (ret);
-        }
-    }
-  else
-    {
-      if (request->iconv_to_initialized)
-        {
-          ret = g_convert_with_iconv (str, -1, request->iconv_to, &bread, dest_len,
-                                      &error);
-          if (ret == NULL)
-            _do_show_iconv_error (str, request->iconv_charset, from_utf8, error);
 
-          return (ret);
-        }
-    }
-
-  stpos = remote_charset;
-  while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL)
-    {
-      if (from_utf8)
-        {
-          fromset = "UTF-8";
-          toset = cur_charset;
-          if ((request->iconv_from = g_iconv_open (toset, fromset)) == (GIConv) -1)
-            {
-              g_free (cur_charset);
-              continue;
-            }
+char *
+gftp_string_from_utf8 (gftp_request *request, int force_local, const char *str,
+                       size_t *dest_len)
+{
+    gchar *remote_charset = NULL, *default_charset = NULL, *ret;
+    GError *error = NULL;
 
-          error = NULL;
-          if ((ret = g_convert_with_iconv (str, -1, request->iconv_from, &bread,
-                                           dest_len, &error)) == NULL)
-            {
-              g_iconv_close (request->iconv_from);
-              request->iconv_from = NULL;
-              _do_show_iconv_error (str, cur_charset, from_utf8, error);
-              g_free (cur_charset);
-              continue;
-            }
+    gftp_lookup_global_option ("default_charset", &default_charset);
+    if(!default_charset)
+        default_charset = "UTF-8";
 
-          request->iconv_from_initialized = 1;
-        }
-      else
-        {
-          fromset = cur_charset;
-          toset = "UTF-8";
-          if ((request->iconv_to = g_iconv_open (toset, fromset)) == (GIConv) -1)
-            {
-              g_free (cur_charset);
-              continue;
-            }
+    if(request)
+        remote_charset = request->remote_charset;
+    if(!remote_charset)
+        remote_charset = default_charset;
 
-          error = NULL;
-          if ((ret = g_convert_with_iconv (str, -1, request->iconv_to, &bread,
-                                           dest_len, &error)) == NULL)
-            {
-              g_iconv_close (request->iconv_to);
-              request->iconv_to = NULL;
-              _do_show_iconv_error (str, cur_charset, from_utf8, error);
-              g_free (cur_charset);
-              continue;
-            }
+    ret = g_convert_with_fallback(str, -1, remote_charset, "UTF-8", "?", NULL, dest_len, &error);
 
-          request->iconv_to_initialized = 1;
-        }
-
-      request->iconv_charset = cur_charset;
-      return (ret);
-    }
-
-  return (NULL);
+    return ret;
 }
 
 
 char *
-gftp_string_to_utf8 (gftp_request * request, const char *str, size_t *dest_len)
-{
-  return (_do_convert_string (request, 0, 0, str, dest_len, 0));
-}
-
-
-char *
-gftp_string_from_utf8 (gftp_request * request, int force_local, const char *str,
+gftp_filename_to_utf8 (gftp_request *request, const char *str,
                        size_t *dest_len)
 {
-  return (_do_convert_string (request, 0, force_local, str, dest_len, 1));
+    char *codeset = nl_langinfo(CODESET);
+    gchar *ret;
+    GError *error = NULL;
+
+    ret = g_filename_to_utf8 (str, -1, NULL, dest_len, &error);
+    if(!ret) {
+        error = NULL;
+        ret = g_convert_with_fallback(str, -1, "UTF-8", codeset, "?", NULL, dest_len, &error);
+    }
+
+    if(!ret) {
+        error = NULL;
+        ret = g_strdup(str);
+    }
+
+    return ret;
 }
 
 
 char *
-gftp_filename_to_utf8 (gftp_request * request, const char *str,
-                       size_t *dest_len)
+gftp_filename_from_utf8 (gftp_request *request, const char *str,
+                         size_t *dest_len)
 {
-  return (_do_convert_string (request, 1, 0, str, dest_len, 0));
+    char *codeset = nl_langinfo(CODESET);
+    gchar *ret;
+    GError *error = NULL;
+
+    ret = g_filename_from_utf8 (str, -1, NULL, dest_len, &error);
+    if(!ret) {
+        error = NULL;
+        ret = g_convert_with_fallback(str, -1, codeset, "UTF-8", "?", NULL, dest_len, &error);
+    }
+
+    return ret;
 }
 
 
 char *
-gftp_filename_from_utf8 (gftp_request * request, const char *str,
-                         size_t *dest_len)
-{
-  return (_do_convert_string (request, 1, 0, str, dest_len, 1));
-}
-
-
-char *
-gftp_remote_filename_to_utf8 (gftp_request * request, const char *str,
+gftp_remote_filename_to_utf8 (gftp_request *request, const char *str,
                        size_t *dest_len)
 {
     char *remote_charset = NULL, *default_charset = NULL;
+    gchar *ret;
     GError *error = NULL;
-    gchar *ret = NULL;
 
     if(request == NULL)
         return (NULL);
 
     /* get remote_charset */
-//    gftp_lookup_request_option (request, "remote_charset", &remote_charset);
     remote_charset = request->remote_charset;
     gftp_lookup_global_option ("default_charset", &default_charset);
     if(!remote_charset)
        remote_charset = (default_charset && *default_charset != '\0')
            ? default_charset : "UTF8";
 
-    ret = g_convert(str, -1, "UTF8", remote_charset, NULL, dest_len, error);
+    ret = g_convert(str, -1, "UTF8", remote_charset, NULL, dest_len, &error);
     return ret;
 }
 
@@ -264,8 +186,8 @@
                          size_t *dest_len)
 {
     char *remote_charset = NULL, *default_charset = NULL;
+    gchar *ret;
     GError *error = NULL;
-    gchar *ret = NULL;
 
     if(request == NULL)
         return (NULL);
@@ -274,62 +196,12 @@
         return (NULL);
 
     /* get remote_charset */
-//    gftp_lookup_request_option (request, "remote_charset", &remote_charset);
     remote_charset = request->remote_charset;
     gftp_lookup_global_option ("default_charset", &default_charset);
     if(!remote_charset)
         remote_charset = (default_charset && *default_charset != '\0')
             ? default_charset : "UTF8";
 
-    ret = g_convert(str, -1, remote_charset, "UTF8", NULL, dest_len, error);
+    ret = g_convert(str, -1, remote_charset, "UTF8", NULL, dest_len, &error);
     return ret;
 }
-
-#else
-
-char *
-gftp_string_to_utf8 (gftp_request * request, const char *str, size_t dest_len)
-{
-  return (NULL);
-}
-
-
-char *
-gftp_string_from_utf8 (gftp_request * request, int force_local, const char *str,
-                       size_t dest_len)
-{
-  return (NULL);
-}
-
-
-char *
-gftp_filename_to_utf8 (gftp_request * request, const char *str, size_t dest_len)
-{
-  return (NULL);
-}
-
-
-char *
-gftp_filename_from_utf8 (gftp_request * request, int force_local,
-                         const char *str, size_t dest_len)
-{
-  return (NULL);
-}
-
-char *
-gftp_remote_filename_to_utf8 (gftp_request * request, const char *str, size_t dest_len)
-{
-  return (NULL);
-}
-
-
-char *
-gftp_remote_filename_from_utf8 (gftp_request * request, int force_local,
-                         const char *str, size_t dest_len)
-{
-  return (NULL);
-}
-
-#endif
-
-