common: Use g_auto for variables

This commit is contained in:
Robert Ancell 2018-06-01 12:45:24 +12:00 committed by Georges Basile Stavracas Neto
parent eff88ab2f1
commit 86d1d30c63
11 changed files with 134 additions and 244 deletions

View file

@ -97,15 +97,15 @@ remove_duplicate_dashes (char *input)
return input;
}
#define CHECK if (is_empty (result)) goto bail
#define CHECK if (is_empty (result)) return g_strdup ("localhost")
char *
pretty_hostname_to_static (const char *pretty,
gboolean for_display)
{
char *result;
char *valid_chars;
char *composed;
g_autofree gchar *result = NULL;
g_autofree gchar *valid_chars = NULL;
g_autofree gchar *composed = NULL;
g_return_val_if_fail (pretty != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (pretty, -1, NULL), NULL);
@ -117,56 +117,45 @@ pretty_hostname_to_static (const char *pretty,
/* Transform the pretty hostname to ASCII */
result = g_str_to_ascii (composed, NULL);
g_debug ("\ttranslit: '%s'", result);
g_free (composed);
CHECK;
/* Remove apostrophes */
result = remove_apostrophes (result);
remove_apostrophes (result);
g_debug ("\tapostrophes: '%s'", result);
CHECK;
/* Remove all the not-allowed chars */
valid_chars = allowed_chars ();
result = g_strcanon (result, valid_chars, '-');
g_free (valid_chars);
g_strcanon (result, valid_chars, '-');
g_debug ("\tcanon: '%s'", result);
CHECK;
/* Remove the leading dashes */
result = remove_leading_dashes (result);
remove_leading_dashes (result);
g_debug ("\tleading: '%s'", result);
CHECK;
/* Remove trailing dashes */
result = remove_trailing_dashes (result);
remove_trailing_dashes (result);
g_debug ("\ttrailing: '%s'", result);
CHECK;
/* Remove duplicate dashes */
result = remove_duplicate_dashes (result);
remove_duplicate_dashes (result);
g_debug ("\tduplicate: '%s'", result);
CHECK;
/* Lower case */
if (!for_display) {
char *tmp;
if (!for_display)
return g_ascii_strdown (result, -1);
tmp = g_ascii_strdown (result, -1);
g_free (result);
result = tmp;
}
return result;
bail:
g_free (result);
return g_strdup ("localhost");
return g_steal_pointer (&result);
}
#undef CHECK
@ -176,7 +165,6 @@ char *
pretty_hostname_to_ssid (const char *pretty)
{
const char *p, *prev;
char *ret = NULL;
if (pretty == NULL || *pretty == '\0') {
pretty = g_get_host_name ();
@ -186,7 +174,7 @@ pretty_hostname_to_ssid (const char *pretty)
if (pretty == NULL) {
/* translators: This is the default hotspot name, need to be less than 32-bytes */
ret = g_strdup (C_("hotspot", "Hotspot"));
gchar *ret = g_strdup (C_("hotspot", "Hotspot"));
g_assert (strlen (ret) <= SSID_MAX_LEN);
return ret;
}
@ -200,12 +188,10 @@ pretty_hostname_to_ssid (const char *pretty)
break;
if (p - pretty > SSID_MAX_LEN) {
ret = g_strndup (pretty, prev - pretty);
break;
return g_strndup (pretty, prev - pretty);
}
if (p - pretty == SSID_MAX_LEN) {
ret = g_strndup (pretty, p - pretty);
break;
return g_strndup (pretty, p - pretty);
}
if (*p == '\0')
@ -214,8 +200,5 @@ pretty_hostname_to_ssid (const char *pretty)
prev = p;
}
if (ret == NULL)
ret = g_strdup (pretty);
return ret;
return g_strdup (pretty);
}