Re: g_filename_to_utf8



On 22 Oct 2000, Owen Taylor wrote:

> Would it make sense to have an environment variable to control this?
> That is, something like G_UTF8_FILENAMES, to allow people without
> UTF-8 locale support to still have GLib handle files as UTF-8.
> (A slight generalization would be G_FILENAME_ENCODING, to allow
> maximally flexible handling.)

This is already supported via g_get_charset, using CHARSET (a bad choice
of name in retrospect, but I think a few other things use it)

If filename encoding != locale's encoding we need a parallel set of
functions which just honour the locale's encoding, to allow the output of
things like strftime to be converted to UTF-8.

Perhaps g_locale_to_utf8 and g_locale_from_utf8?

> (I consider non-UCS/non-tagged filenames a really bad idea and almost
> feel that we should have an environment variable with the opposite
> meaning - something like G_BROKEN_FILENAMES)

After all, that's what the kernel documents filenames as being in. :)

Executive summary of new patch -

  g_locale_to_utf8       converts to/from UTF-8 and nl_langinfo(CODESET)
  g_locale_from_utf8     (a rename of g_filename_*)

  g_filename_to_utf8     if G_BROKEN_FILENAMES set, call the _locale fn,
  g_filename_from_utf8   otherwise just g_strdup

-- 
Robert Brady
robert suse co uk

Index: gstrfuncs.c
===================================================================
RCS file: /cvs/gnome/glib/gstrfuncs.c,v
retrieving revision 1.45
diff -u -r1.45 gstrfuncs.c
--- gstrfuncs.c	2000/09/29 13:37:01	1.45
+++ gstrfuncs.c	2000/10/23 02:04:44
@@ -47,6 +47,9 @@
 #include <windows.h>
 #endif
 
+#include "gconvert.h"
+#include "gunicode.h"
+
 /* do not include <unistd.h> in this place since it
  * inteferes with g_strsignal() on some OSes
  */
@@ -1231,15 +1234,15 @@
 }
 
 /*
- * g_filename_to_utf8
+ * g_locale_to_utf8
  *
- * Converts a string which is in the encoding used for file names by
+ * Converts a string which is in the encoding used for strings by
  * the C runtime (usually the same as that used by the operating
  * system) in the current locale into a UTF-8 string.
  */
 
 gchar *
-g_filename_to_utf8 (const gchar *opsysstring)
+g_locale_to_utf8 (const gchar *opsysstring)
 {
 #ifdef G_OS_WIN32
 
@@ -1331,20 +1334,26 @@
   return result;
 
 #else
+
+  char *charset;
+
+  if ( g_get_charset (&charset))
+    return g_strdup (opsysstring);
 
-  return g_strdup (opsysstring);
+  return g_convert (opsysstring, strlen (opsysstring), 
+		    "UTF-8", charset, NULL, NULL, NULL);
 
 #endif
 }
 
 /*
- * g_filename_from_utf8
+ * g_locale_from_utf8
  *
- * The reverse of g_filename_to_utf8.
+ * The reverse of g_locale_to_utf8.
  */
 
 gchar *
-g_filename_from_utf8 (const gchar *utf8string)
+g_locale_from_utf8 (const gchar *utf8string)
 {
 #ifdef G_OS_WIN32
 
@@ -1442,9 +1451,43 @@
   return result;
 
 #else
+
+  char *charset;
+
+  if ( g_get_charset (&charset))
+    return g_strdup (utf8string);
+
+  return g_convert (utf8string, strlen (utf8string), 
+		    charset, "UTF-8", NULL, NULL, NULL);
+
+#endif
+}
+
+/* Filenames are in UTF-8 unless specificially requested otherwise */
+
+gchar*
+g_filename_to_utf8 (const gchar *string)
+{
+#ifdef G_OS_WIN32
+  return g_locale_to_utf8 (string);
+#else
+  if (getenv ("G_BROKEN_FILENAMES"))
+    return g_locale_to_utf8 (string);
 
-  return g_strdup (utf8string);
+  return g_strdup (string);
+#endif
+}
+
+gchar*
+g_filename_from_utf8 (const gchar *string)
+{
+#ifdef G_OS_WIN32
+  return g_locale_from_utf8 (string);
+#else
+  if (getenv ("G_BROKEN_FILENAMES"))
+    return g_locale_from_utf8 (string);
 
+  return g_strdup (string);
 #endif
 }
 
Index: gstrfuncs.h
===================================================================
RCS file: /cvs/gnome/glib/gstrfuncs.h,v
retrieving revision 1.1
diff -u -r1.1 gstrfuncs.h
--- gstrfuncs.h	2000/10/12 11:52:07	1.1
+++ gstrfuncs.h	2000/10/23 02:04:44
@@ -103,6 +103,11 @@
 gpointer g_memdup		(gconstpointer mem,
 				 guint	       byte_size);
 
+/* Convert between libc's idea of strings and UTF-8.
+ */
+gchar*   g_locale_to_utf8 (const gchar *opsysstring);
+gchar*   g_locale_from_utf8 (const gchar *utf8string);
+
 /* Convert between the operating system (or C runtime)
  * representation of file names and UTF-8.
  */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]