re-work to retain a copy of the cached charset rather than the actual
authorMark McLoughlin <mark@skynet.ie>
Sun, 9 Nov 2003 13:17:23 +0000 (13:17 +0000)
committerMark McLoughlin <markmc@src.gnome.org>
Sun, 9 Nov 2003 13:17:23 +0000 (13:17 +0000)
2003-11-07  Mark McLoughlin  <mark@skynet.ie>

        * glib/gconvert.c: (get_filename_charset): re-work to
        retain a copy of the cached charset rather than the
        actual return value from g_get_charset (which may
        change). Also, re-initialize the cache if it does
        change. See bug #126454.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gconvert.c

index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index ba1752873da1a2a46ffcc16475e85308bb0db70b..258afc0edbc405f37b39937401fa2bb9991949b8 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-07  Mark McLoughlin  <mark@skynet.ie>
+
+       * glib/gconvert.c: (get_filename_charset): re-work to
+       retain a copy of the cached charset rather than the
+       actual return value from g_get_charset (which may
+       change). Also, re-initialize the cache if it does
+       change. See bug #126454.
+
 Thu Nov  6 00:04:46 2003  Matthias Clasen  <maclas@gmx.de>
 
        * glib/gconvert.c (get_filename_charset): Replacement for
index 7cdc4d90ed158a817bd96076fcfd1d9c83ce2bf9..4f85a0b8f94a5ab760301cb911dc49b45da36964 100644 (file)
@@ -992,6 +992,23 @@ g_locale_from_utf8 (const gchar *utf8string,
 
 #ifndef G_PLATFORM_WIN32
 
+typedef struct _GFilenameCharsetCache GFilenameCharsetCache;
+
+struct _GFilenameCharsetCache {
+  gboolean is_utf8;
+  gchar *charset;
+  gchar *filename_charset;
+};
+
+static void
+filename_charset_cache_free (gpointer data)
+{
+  GFilenameCharsetCache *cache = data;
+  g_free (cache->charset);
+  g_free (cache->filename_charset);
+  g_free (cache);
+}
+
 /*
  * get_filename_charset:
  * @charset: return location for the name of the filename encoding 
@@ -1011,17 +1028,28 @@ g_locale_from_utf8 (const gchar *utf8string,
  * Return value: %TRUE if the charset used for filename is UTF-8.
  */
 static gboolean
-get_filename_charset (const gchar **charset)
+get_filename_charset (const gchar **filename_charset)
 {
-  static gboolean initialized = FALSE;
-  static const gchar *filename_charset;
-  static gboolean is_utf8;
+  static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
+  GFilenameCharsetCache *cache = g_static_private_get (&cache_private);
+  const gchar *charset;
   
-  if (!initialized)
+  if (!cache)
     {
+      cache = g_new0 (GFilenameCharsetCache, 1);
+      g_static_private_set (&cache_private, cache, filename_charset_cache_free);
+    }
+
+  g_get_charset (&charset);
+
+  if (!(cache->charset && strcmp (cache->charset, charset) == 0))
+    {
+      const gchar *new_charset;
       gchar *p, *q;
 
-      initialized = TRUE;
+      g_free (cache->charset);
+      g_free (cache->filename_charset);
+      cache->charset = g_strdup (charset);
       
       p = getenv ("G_FILENAME_ENCODING");
       if (p != NULL) 
@@ -1031,26 +1059,34 @@ get_filename_charset (const gchar **charset)
            q = p + strlen (p);
 
          if (strncmp ("@locale", p, q - p) == 0)
-           is_utf8 = g_get_charset (&filename_charset);
+           {
+             cache->is_utf8 = g_get_charset (&new_charset);
+             cache->filename_charset = g_strdup (new_charset);
+           }
          else
            {
-             filename_charset = g_strndup (p, q - p);
-             is_utf8 = (strcmp (filename_charset, "UTF-8") == 0);
+             cache->filename_charset = g_strndup (p, q - p);
+             cache->is_utf8 = (strcmp (cache->filename_charset, "UTF-8") == 0);
            }
        }
       else if (getenv ("G_BROKEN_FILENAMES") != NULL)
-       is_utf8 = g_get_charset (&filename_charset);
+       {
+         cache->is_utf8 = g_get_charset (&new_charset);
+         cache->filename_charset = g_strdup (new_charset);
+       }
       else 
        {
-         filename_charset = "UTF-8";
-         is_utf8 = TRUE;
+         cache->filename_charset = g_strdup ("UTF-8");
+         cache->is_utf8 = TRUE;
        }
     }
 
-  *charset = filename_charset;
+  if (filename_charset)
+    *filename_charset = cache->filename_charset;
 
-  return is_utf8;
+  return cache->is_utf8;
 }
+
 #else /* G_PLATFORM_WIN32 */
 #define get_filename_charset (charset) TRUE
 #endif /* G_PLATFORM_WIN32 */