[gnome-desktop] gnome-languages: Use g_auto* macros where applicable



commit 389a3761cf70b20be40894ce3dbb2e81b25a1a79
Author: Rui Matos <tiagomatos gmail com>
Date:   Tue Feb 9 17:38:21 2016 +0100

    gnome-languages: Use g_auto* macros where applicable
    
    https://bugzilla.gnome.org/show_bug.cgi?id=761776

 libgnome-desktop/gnome-languages.c |  144 ++++++++++--------------------------
 1 files changed, 39 insertions(+), 105 deletions(-)
---
diff --git a/libgnome-desktop/gnome-languages.c b/libgnome-desktop/gnome-languages.c
index 5ee03f8..ec23a7a 100644
--- a/libgnome-desktop/gnome-languages.c
+++ b/libgnome-desktop/gnome-languages.c
@@ -262,11 +262,11 @@ char *
 gnome_normalize_locale (const char *locale)
 {
         char *normalized_name;
-        char *language_code;
-        char *territory_code;
-        char *codeset;
-        char *modifier;
         gboolean valid;
+        g_autofree char *language_code = NULL;
+        g_autofree char *territory_code = NULL;
+        g_autofree char *codeset = NULL;
+        g_autofree char *modifier = NULL;
 
         if (locale[0] == '\0') {
                 return NULL;
@@ -282,25 +282,19 @@ gnome_normalize_locale (const char *locale)
         normalized_name = construct_language_name (language_code,
                                                    territory_code,
                                                    codeset, modifier);
-        g_free (language_code);
-        g_free (territory_code);
-        g_free (codeset);
-        g_free (modifier);
-
         return normalized_name;
 }
 
 static gboolean
 language_name_is_valid (const char *language_name)
 {
-        char     *old_locale;
         gboolean  is_valid;
         int lc_type_id = LC_MESSAGES;
+        g_autofree char *old_locale = NULL;
 
         old_locale = g_strdup (setlocale (lc_type_id, NULL));
         is_valid = setlocale (lc_type_id, language_name) != NULL;
         setlocale (lc_type_id, old_locale);
-        g_free (old_locale);
 
         return is_valid;
 }
@@ -310,15 +304,13 @@ language_name_get_codeset_details (const char  *language_name,
                                    char       **pcodeset,
                                    gboolean    *is_utf8)
 {
-        char     *old_locale;
-        char     *codeset;
+        g_autofree char *old_locale = NULL;
+        g_autofree char *codeset = NULL;
 
         old_locale = g_strdup (setlocale (LC_CTYPE, NULL));
 
-        if (setlocale (LC_CTYPE, language_name) == NULL) {
-                g_free (old_locale);
+        if (setlocale (LC_CTYPE, language_name) == NULL)
                 return;
-        }
 
         codeset = nl_langinfo (CODESET);
 
@@ -330,11 +322,9 @@ language_name_get_codeset_details (const char  *language_name,
                 codeset = normalize_codeset (codeset);
 
                 *is_utf8 = strcmp (codeset, "UTF-8") == 0;
-                g_free (codeset);
         }
 
         setlocale (LC_CTYPE, old_locale);
-        g_free (old_locale);
 }
 
 /**
@@ -351,15 +341,14 @@ gboolean
 gnome_language_has_translations (const char *code)
 {
         GDir        *dir;
-        char        *path;
         const char  *name;
         gboolean     has_translations;
+        g_autofree char *path = NULL;
 
         path = g_build_filename (GNOMELOCALEDIR, code, "LC_MESSAGES", NULL);
 
         has_translations = FALSE;
         dir = g_dir_open (path, 0, NULL);
-        g_free (path);
 
         if (dir == NULL) {
                 goto out;
@@ -389,7 +378,7 @@ add_locale (const char *language_name,
 {
         GnomeLocale *locale;
         GnomeLocale *old_locale;
-        char      *name;
+        g_autofree char *name = NULL;
         gboolean   is_utf8 = FALSE;
         gboolean   valid = FALSE;
 
@@ -412,17 +401,14 @@ add_locale (const char *language_name,
                 name = g_strdup_printf ("%s.UTF-8", language_name);
 
                 language_name_get_codeset_details (name, NULL, &is_utf8);
-                if (!is_utf8) {
-                        g_free (name);
+                if (!is_utf8)
                         return FALSE;
-                }
         } else {
                 name = g_strdup (language_name);
         }
 
         if (!language_name_is_valid (name)) {
                 g_debug ("Ignoring '%s' as a locale, since it's invalid", name);
-                g_free (name);
                 return FALSE;
         }
 
@@ -432,7 +418,6 @@ add_locale (const char *language_name,
                                     &locale->territory_code,
                                     &locale->codeset,
                                     &locale->modifier);
-        g_free (name);
         if (!valid) {
                 gnome_locale_free (locale);
                 return FALSE;
@@ -485,13 +470,12 @@ select_dirs (const struct dirent *dirent)
 #endif
                         {
                                 struct stat st;
-                                char       *path;
+                                g_autofree char *path = NULL;
 
                                 path = g_build_filename (LIBLOCALEDIR, dirent->d_name, NULL);
                                 if (g_stat (path, &st) == 0) {
                                         mode = st.st_mode;
                                 }
-                                g_free (path);
                         }
 
                 result = S_ISDIR (mode);
@@ -526,8 +510,9 @@ collect_locales_from_localebin (void)
 {
         gboolean found_locales = FALSE;
         gchar    *argv[] = { "locale", "-a", NULL };
-        gchar    *output;
-        gchar    **lines, **linep;
+        gchar    **linep;
+        g_auto (GStrv) lines = NULL;
+        g_autofree gchar *output = NULL;
 
         if (g_spawn_sync (NULL, argv, NULL,
                           G_SPAWN_SEARCH_PATH|G_SPAWN_STDERR_TO_DEV_NULL,
@@ -544,11 +529,8 @@ collect_locales_from_localebin (void)
                                 found_locales = TRUE;
                         linep++;
                 }
-                g_strfreev (lines);
         }
 
-        g_free (output);
-
         return found_locales;
 }
 
@@ -707,7 +689,7 @@ get_translated_language (const char *code,
         name = NULL;
         if (language != NULL) {
                 const char  *translated_name;
-                char        *old_locale = NULL;
+                g_autofree char *old_locale = NULL;
 
                 if (locale != NULL) {
                         old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
@@ -723,7 +705,6 @@ get_translated_language (const char *code,
 
                 if (locale != NULL) {
                         setlocale (LC_MESSAGES, old_locale);
-                        g_free (old_locale);
                 }
         }
 
@@ -760,7 +741,7 @@ get_translated_territory (const char *code,
         name = NULL;
         if (territory != NULL) {
                 const char *translated_territory;
-                char       *old_locale;
+                g_autofree char *old_locale = NULL;
 
                 if (locale != NULL) {
                         old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
@@ -772,7 +753,6 @@ get_translated_territory (const char *code,
 
                 if (locale != NULL) {
                         setlocale (LC_MESSAGES, old_locale);
-                        g_free (old_locale);
                 }
         }
 
@@ -962,11 +942,11 @@ territories_parse_start_tag (GMarkupParseContext      *ctx,
 static void
 languages_variant_init (const char *variant)
 {
-        GError  *error;
         gboolean res;
-        char    *buf;
         gsize    buf_len;
-        char    *filename;
+        g_autofree char *buf = NULL;
+        g_autofree char *filename = NULL;
+        g_autoptr (GError) error = NULL;
 
         bindtextdomain (variant, ISO_CODES_LOCALESDIR);
         bind_textdomain_codeset (variant, "UTF-8");
@@ -978,7 +958,7 @@ languages_variant_init (const char *variant)
                                    &buf_len,
                                    &error);
         if (res) {
-                GMarkupParseContext *ctx;
+                g_autoptr (GMarkupParseContext) ctx = NULL;
                 GMarkupParser        parser = { languages_parse_start_tag, NULL, NULL, NULL, NULL };
 
                 ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
@@ -990,19 +970,12 @@ languages_variant_init (const char *variant)
                         g_warning ("Failed to parse '%s': %s\n",
                                    filename,
                                    error->message);
-                        g_error_free (error);
                 }
-
-                g_markup_parse_context_free (ctx);
-                g_free (buf);
         } else {
                 g_warning ("Failed to load '%s': %s\n",
                            filename,
                            error->message);
-                g_error_free (error);
         }
-
-        g_free (filename);
 }
 
 static void
@@ -1020,10 +993,10 @@ languages_init (void)
 static void
 territories_init (void)
 {
-        GError  *error;
         gboolean res;
-        char    *buf;
         gsize    buf_len;
+        g_autofree char *buf = NULL;
+        g_autoptr (GError) error = NULL;
 
         if (gnome_territories_map)
                 return;
@@ -1039,7 +1012,7 @@ territories_init (void)
                                    &buf_len,
                                    &error);
         if (res) {
-                GMarkupParseContext *ctx;
+                g_autoptr (GMarkupParseContext) ctx = NULL;
                 GMarkupParser        parser = { territories_parse_start_tag, NULL, NULL, NULL, NULL };
 
                 ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
@@ -1051,16 +1024,11 @@ territories_init (void)
                         g_warning ("Failed to parse '%s': %s\n",
                                    ISO_CODES_DATADIR "/iso_3166.xml",
                                    error->message);
-                        g_error_free (error);
                 }
-
-                g_markup_parse_context_free (ctx);
-                g_free (buf);
         } else {
                 g_warning ("Failed to load '%s': %s\n",
                            ISO_CODES_DATADIR "/iso_3166.xml",
                            error->message);
-                g_error_free (error);
         }
 }
 
@@ -1082,30 +1050,22 @@ gnome_get_language_from_locale (const char *locale,
                                 const char *translation)
 {
         GString *full_language;
-        char *language_code;
-        char *territory_code;
-        char *codeset_code;
-        char *langinfo_codeset;
-        char *translated_language;
-        char *translated_territory;
+        g_autofree char *language_code = NULL;
+        g_autofree char *territory_code = NULL;
+        g_autofree char *codeset_code = NULL;
+        g_autofree char *langinfo_codeset = NULL;
+        g_autofree char *translated_language = NULL;
+        g_autofree char *translated_territory = NULL;
         gboolean is_utf8 = TRUE;
 
         g_return_val_if_fail (locale != NULL, NULL);
         g_return_val_if_fail (*locale != '\0', NULL);
 
-        translated_territory = NULL;
-        translated_language = NULL;
-        langinfo_codeset = NULL;
-
         full_language = g_string_new (NULL);
 
         languages_init ();
         territories_init ();
 
-        language_code = NULL;
-        territory_code = NULL;
-        codeset_code = NULL;
-
         gnome_parse_locale (locale,
                             &language_code,
                             &territory_code,
@@ -1149,13 +1109,6 @@ gnome_get_language_from_locale (const char *locale,
         }
 
  out:
-        g_free (language_code);
-        g_free (territory_code);
-        g_free (codeset_code);
-        g_free (langinfo_codeset);
-        g_free (translated_language);
-        g_free (translated_territory);
-
         if (full_language->len == 0) {
                 g_string_free (full_language, TRUE);
                 return NULL;
@@ -1182,30 +1135,22 @@ gnome_get_country_from_locale (const char *locale,
                                const char *translation)
 {
         GString *full_name;
-        char *language_code;
-        char *territory_code;
-        char *codeset_code;
-        char *langinfo_codeset;
-        char *translated_language;
-        char *translated_territory;
+        g_autofree char *language_code = NULL;
+        g_autofree char *territory_code = NULL;
+        g_autofree char *codeset_code = NULL;
+        g_autofree char *langinfo_codeset = NULL;
+        g_autofree char *translated_language = NULL;
+        g_autofree char *translated_territory = NULL;
         gboolean is_utf8 = TRUE;
 
         g_return_val_if_fail (locale != NULL, NULL);
         g_return_val_if_fail (*locale != '\0', NULL);
 
-        translated_territory = NULL;
-        translated_language = NULL;
-        langinfo_codeset = NULL;
-
         full_name = g_string_new (NULL);
 
         languages_init ();
         territories_init ();
 
-        language_code = NULL;
-        territory_code = NULL;
-        codeset_code = NULL;
-
         gnome_parse_locale (locale,
                             &language_code,
                             &territory_code,
@@ -1245,13 +1190,6 @@ gnome_get_country_from_locale (const char *locale,
         }
 
  out:
-        g_free (language_code);
-        g_free (territory_code);
-        g_free (codeset_code);
-        g_free (langinfo_codeset);
-        g_free (translated_language);
-        g_free (translated_territory);
-
         if (full_name->len == 0) {
                 g_string_free (full_name, TRUE);
                 return NULL;
@@ -1366,8 +1304,9 @@ gnome_get_input_source_from_locale (const char  *locale,
 {
         static GHashTable *table = NULL;
         DefaultInputSource *dis;
-        gchar *l_code, *c_code;
-        gchar *key;
+        g_autofree gchar *l_code = NULL;
+        g_autofree gchar *c_code = NULL;
+        g_autofree gchar *key = NULL;
         gint i;
 
         g_return_val_if_fail (locale != NULL, FALSE);
@@ -1392,10 +1331,5 @@ gnome_get_input_source_from_locale (const char  *locale,
                 *type = dis->type;
                 *id = dis->id;
         }
-
-        g_free (l_code);
-        g_free (c_code);
-        g_free (key);
-
         return dis != NULL;
 }


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