cli: Convert SSID to a printable form

If SSID is a UTF-8 string, it is printed as it is, but enclosed in quotes.
Otherwise the bytes are converted to hex string (in uppercase).
The added quotes for UTF-8 string allow to disambiguate the two forms.
This commit is contained in:
Jiří Klimeš
2010-03-24 23:28:00 +01:00
parent a91da03b65
commit 1e329c5c0c
3 changed files with 29 additions and 1 deletions

View File

@@ -345,7 +345,7 @@ detail_access_point (gpointer data, gpointer user_data)
strength = nm_access_point_get_strength (ap); strength = nm_access_point_get_strength (ap);
/* Convert to strings */ /* Convert to strings */
ssid_str = g_strdup_printf ("%s", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : _("(none)")); ssid_str = ssid_to_printable ((const char *) ssid->data, ssid->len);
freq_str = g_strdup_printf (_("%u MHz"), freq); freq_str = g_strdup_printf (_("%u MHz"), freq);
bitrate_str = g_strdup_printf (_("%u MB/s"), bitrate/1000); bitrate_str = g_strdup_printf (_("%u MB/s"), bitrate/1000);
strength_str = g_strdup_printf ("%u", strength); strength_str = g_strdup_printf ("%u", strength);

View File

@@ -47,6 +47,33 @@ next_arg (int *argc, char ***argv)
return 0; return 0;
} }
/*
* Convert SSID to a printable form.
* If it is an UTF-8 string, enclose it in quotes and return it.
* Otherwise convert it to a hex string representation.
* Caller has to free the returned string using g_free()
*/
char *
ssid_to_printable (const char *str, gsize len)
{
GString *printable;
char *printable_str;
int i;
if (str == NULL || len == 0)
return NULL;
if (g_utf8_validate (str, len, NULL))
return g_strdup_printf ("'%.*s'", (int) len, str);
printable = g_string_new (NULL);
for (i = 0; i < len; i++) {
g_string_append_printf (printable, "%02X", (unsigned char) str[i]);
}
printable_str = g_string_free (printable, FALSE);
return printable_str;
}
/* /*
* Parse comma separated fields in 'fields_str' according to 'fields_array'. * Parse comma separated fields in 'fields_str' according to 'fields_array'.
* IN: 'field_str': comma-separated fields names * IN: 'field_str': comma-separated fields names

View File

@@ -27,6 +27,7 @@
/* === Functions === */ /* === Functions === */
int matches (const char *cmd, const char *pattern); int matches (const char *cmd, const char *pattern);
int next_arg (int *argc, char ***argv); int next_arg (int *argc, char ***argv);
char *ssid_to_printable (const char *str, gsize len);
GArray *parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error); GArray *parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error);
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error); gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]); void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]);