mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-04 02:30:24 +01:00
cli: nmc_string_is_valid() utility function
This function checks whether a string is contained among allowed strings. It performs case-insensitive comparison and supports shortcuts if they are unique. It returns a pointer to the found string in allowed array, or NULL.
This commit is contained in:
parent
ced61dfc7f
commit
8598ee1139
2 changed files with 49 additions and 0 deletions
|
|
@ -302,6 +302,54 @@ nmc_string_to_arg_array (const char *line, const char *delim, char ***argv, int
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check whether 'input' is contained in 'allowed' array. It performs case
|
||||
* insensitive comparison and supports shortcut strings if they are unique.
|
||||
* Returns: a pointer to found string in allowed array on success or NULL.
|
||||
* On failure: error->code : 0 - string not found; 1 - string is ambiguous
|
||||
*/
|
||||
const char *
|
||||
nmc_string_is_valid (const char *input, const char **allowed, GError **error)
|
||||
{
|
||||
const char **p;
|
||||
size_t input_ln, p_len;
|
||||
gboolean prev_match = FALSE;
|
||||
const char *ret = NULL;
|
||||
|
||||
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
||||
|
||||
if (!input || !*input)
|
||||
goto finish;
|
||||
|
||||
input_ln = strlen (input);
|
||||
for (p = allowed; p && *p; p++) {
|
||||
p_len = strlen (*p);
|
||||
if (g_ascii_strncasecmp (input, *p, input_ln) == 0) {
|
||||
if (input_ln == p_len) {
|
||||
ret = *p;
|
||||
break;
|
||||
}
|
||||
if (!prev_match)
|
||||
ret = *p;
|
||||
else {
|
||||
g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"),
|
||||
input, ret, *p);
|
||||
return NULL;
|
||||
}
|
||||
prev_match = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
finish:
|
||||
if (ret == NULL) {
|
||||
char *valid_vals = g_strjoinv (", ", (char **) allowed);
|
||||
g_set_error (error, 1, 0, _("'%s' not among [%s]"),
|
||||
input ? input : "", valid_vals);
|
||||
g_free (valid_vals);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find out how many columns an UTF-8 string occupies on the screen
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ void nmc_terminal_erase_line (void);
|
|||
void nmc_terminal_show_progress (const char *str);
|
||||
char *nmc_get_user_input (const char *ask_str);
|
||||
int nmc_string_to_arg_array (const char *line, const char *delim, char ***argv, int *argc);
|
||||
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
|
||||
int nmc_string_screen_width (const char *start, const char *end);
|
||||
void set_val_str (NmcOutputField fields_array[], guint32 index, char *value);
|
||||
void set_val_arr (NmcOutputField fields_array[], guint32 index, char **value);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue