re-work to retain a copy of the cached charset rather than the actual

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.
This commit is contained in:
Mark McLoughlin 2003-11-09 13:17:23 +00:00 committed by Mark McLoughlin
parent d7626f52be
commit 57f22f15be
7 changed files with 98 additions and 14 deletions

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View 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> Thu Nov 6 00:04:46 2003 Matthias Clasen <maclas@gmx.de>
* glib/gconvert.c (get_filename_charset): Replacement for * glib/gconvert.c (get_filename_charset): Replacement for

View File

@ -992,6 +992,23 @@ g_locale_from_utf8 (const gchar *utf8string,
#ifndef G_PLATFORM_WIN32 #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: * get_filename_charset:
* @charset: return location for the name of the filename encoding * @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. * Return value: %TRUE if the charset used for filename is UTF-8.
*/ */
static gboolean static gboolean
get_filename_charset (const gchar **charset) get_filename_charset (const gchar **filename_charset)
{ {
static gboolean initialized = FALSE; static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
static const gchar *filename_charset; GFilenameCharsetCache *cache = g_static_private_get (&cache_private);
static gboolean is_utf8; 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; gchar *p, *q;
initialized = TRUE; g_free (cache->charset);
g_free (cache->filename_charset);
cache->charset = g_strdup (charset);
p = getenv ("G_FILENAME_ENCODING"); p = getenv ("G_FILENAME_ENCODING");
if (p != NULL) if (p != NULL)
@ -1031,26 +1059,34 @@ get_filename_charset (const gchar **charset)
q = p + strlen (p); q = p + strlen (p);
if (strncmp ("@locale", p, q - p) == 0) 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 else
{ {
filename_charset = g_strndup (p, q - p); cache->filename_charset = g_strndup (p, q - p);
is_utf8 = (strcmp (filename_charset, "UTF-8") == 0); cache->is_utf8 = (strcmp (cache->filename_charset, "UTF-8") == 0);
} }
} }
else if (getenv ("G_BROKEN_FILENAMES") != NULL) 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 else
{ {
filename_charset = "UTF-8"; cache->filename_charset = g_strdup ("UTF-8");
is_utf8 = TRUE; 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 */ #else /* G_PLATFORM_WIN32 */
#define get_filename_charset (charset) TRUE #define get_filename_charset (charset) TRUE
#endif /* G_PLATFORM_WIN32 */ #endif /* G_PLATFORM_WIN32 */