Make _g_compute_locale_variants return a char** directly

Bug #635998.
This commit is contained in:
Christian Persch
2010-03-01 16:32:09 +01:00
parent b497220e48
commit be8899bfe6
2 changed files with 42 additions and 43 deletions

View File

@@ -1630,7 +1630,7 @@ g_key_file_set_locale_string (GKeyFile *key_file,
g_free (value); g_free (value);
} }
extern GSList *_g_compute_locale_variants (const gchar *locale); extern gchar **_g_compute_locale_variants (const gchar *locale);
/** /**
* g_key_file_get_locale_string: * g_key_file_get_locale_string:
@@ -1677,16 +1677,7 @@ g_key_file_get_locale_string (GKeyFile *key_file,
if (locale) if (locale)
{ {
GSList *l, *list; languages = _g_compute_locale_variants (locale);
list = _g_compute_locale_variants (locale);
languages = g_new (gchar *, g_slist_length (list) + 1);
for (l = list, i = 0; l; l = l->next, i++)
languages[i] = l->data;
languages[i] = NULL;
g_slist_free (list);
free_languages = TRUE; free_languages = TRUE;
} }
else else

View File

@@ -68,6 +68,7 @@
#include "gtestutils.h" #include "gtestutils.h"
#include "gunicode.h" #include "gunicode.h"
#include "gstrfuncs.h" #include "gstrfuncs.h"
#include "garray.h"
#include "glibintl.h" #include "glibintl.h"
#ifdef G_PLATFORM_WIN32 #ifdef G_PLATFORM_WIN32
@@ -3157,36 +3158,39 @@ explode_locale (const gchar *locale,
* but it is big, ugly, and complicated, so I'm reluctant * but it is big, ugly, and complicated, so I'm reluctant
* to do so when this should handle 99% of the time... * to do so when this should handle 99% of the time...
*/ */
GSList * static void
_g_compute_locale_variants (const gchar *locale) append_locale_variants (GPtrArray *array,
const gchar *locale)
{ {
GSList *retval = NULL;
gchar *language = NULL; gchar *language = NULL;
gchar *territory = NULL; gchar *territory = NULL;
gchar *codeset = NULL; gchar *codeset = NULL;
gchar *modifier = NULL; gchar *modifier = NULL;
guint mask; guint mask;
guint i; guint i, j;
g_return_val_if_fail (locale != NULL, NULL); g_return_if_fail (locale != NULL);
mask = explode_locale (locale, &language, &territory, &codeset, &modifier); mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
/* Iterate through all possible combinations, from least attractive /* Iterate through all possible combinations, from least attractive
* to most attractive. * to most attractive.
*/ */
for (i = 0; i <= mask; i++) for (j = 0; j <= mask; ++j)
if ((i & ~mask) == 0) {
{ i = mask - j;
gchar *val = g_strconcat (language,
(i & COMPONENT_TERRITORY) ? territory : "", if ((i & ~mask) == 0)
(i & COMPONENT_CODESET) ? codeset : "", {
(i & COMPONENT_MODIFIER) ? modifier : "", gchar *val = g_strconcat (language,
NULL); (i & COMPONENT_TERRITORY) ? territory : "",
retval = g_slist_prepend (retval, val); (i & COMPONENT_CODESET) ? codeset : "",
} (i & COMPONENT_MODIFIER) ? modifier : "",
NULL);
g_ptr_array_add (array, val);
}
}
g_free (language); g_free (language);
if (mask & COMPONENT_CODESET) if (mask & COMPONENT_CODESET)
@@ -3195,8 +3199,20 @@ _g_compute_locale_variants (const gchar *locale)
g_free (territory); g_free (territory);
if (mask & COMPONENT_MODIFIER) if (mask & COMPONENT_MODIFIER)
g_free (modifier); g_free (modifier);
}
return retval; gchar **
_g_compute_locale_variants (const gchar *locale)
{
GPtrArray *array;
g_return_val_if_fail (locale != NULL, NULL);
array = g_ptr_array_sized_new (8);
append_locale_variants (array, locale);
g_ptr_array_add (array, NULL);
return (gchar **) g_ptr_array_free (array, FALSE);
} }
/* The following is (partly) taken from the gettext package. /* The following is (partly) taken from the gettext package.
@@ -3305,31 +3321,23 @@ g_get_language_names (void)
if (!(cache->languages && strcmp (cache->languages, value) == 0)) if (!(cache->languages && strcmp (cache->languages, value) == 0))
{ {
gchar **languages; GPtrArray *array;
gchar **alist, **a; gchar **alist, **a;
GSList *list, *l;
gint i;
g_free (cache->languages); g_free (cache->languages);
g_strfreev (cache->language_names); g_strfreev (cache->language_names);
cache->languages = g_strdup (value); cache->languages = g_strdup (value);
array = g_ptr_array_sized_new (8);
alist = g_strsplit (value, ":", 0); alist = g_strsplit (value, ":", 0);
list = NULL;
for (a = alist; *a; a++) for (a = alist; *a; a++)
{ append_locale_variants (array, unalias_lang (*a));
gchar *b = unalias_lang (*a);
list = g_slist_concat (list, _g_compute_locale_variants (b));
}
g_strfreev (alist); g_strfreev (alist);
list = g_slist_append (list, g_strdup ("C")); g_ptr_array_add (array, g_strdup ("C"));
g_ptr_array_add (array, NULL);
cache->language_names = languages = g_new (gchar *, g_slist_length (list) + 1); cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
for (l = list, i = 0; l; l = l->next, i++)
languages[i] = l->data;
languages[i] = NULL;
g_slist_free (list);
} }
return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names; return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names;