clients: replace nmc_string_to_int() by _nm_utils_ascii_str_to_int64()

This commit is contained in:
Thomas Haller 2017-05-21 13:51:12 +02:00
parent 1e5c5bd4a6
commit 8c87a4b8a8
3 changed files with 22 additions and 62 deletions

View file

@ -21,30 +21,6 @@
#include "nm-client-utils.h"
/*
* Convert string to signed integer.
* If required, the resulting number is checked to be in the <min,max> range.
*/
static gboolean
nmc_string_to_int_base (const char *str,
int base,
gboolean range_check,
long int min,
long int max,
long int *value)
{
char *end;
long int tmp;
errno = 0;
tmp = strtol (str, &end, base);
if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) {
return FALSE;
}
*value = tmp;
return TRUE;
}
/*
* Convert string to unsigned integer.
* If required, the resulting number is checked to be in the <min,max> range.
@ -69,16 +45,6 @@ nmc_string_to_uint_base (const char *str,
return TRUE;
}
gboolean
nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value)
{
return nmc_string_to_int_base (str, 10, range_check, min, max, value);
}
gboolean
nmc_string_to_uint (const char *str,
gboolean range_check,

View file

@ -33,11 +33,6 @@ const char *nmc_string_is_valid (const char *input, const char **allowed, GError
char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens);
gboolean nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value);
gboolean nmc_string_to_uint (const char *str,
gboolean range_check,
unsigned long int min,

View file

@ -791,54 +791,54 @@ _set_fcn_gobject_bool (ARGS_SET_FCN)
static gboolean
_set_fcn_gobject_trilean (ARGS_SET_FCN)
{
long int val_int;
const int INVALID = G_MININT;
int v;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (!nmc_string_to_int (value, TRUE, -1, 1, &val_int)) {
v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 1, INVALID);
if (v == INVALID) {
g_set_error (error, 1, 0, _("'%s' is not a valid value; use -1, 0 or 1"), value);
return FALSE;
}
g_object_set (setting, property_info->property_name, val_int, NULL);
g_object_set (setting, property_info->property_name, v, NULL);
return TRUE;
}
static gboolean
_set_fcn_gobject_int (ARGS_SET_FCN)
{
long int val_int;
const gint64 INVALID = G_MININT64;
gint64 v;
if (!nmc_string_to_int (value, TRUE, G_MININT, G_MAXINT, &val_int)) {
v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT, G_MAXINT, INVALID);
if (v == INVALID) {
g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value);
return FALSE;
}
/* Validate the number according to the property spec */
if (!validate_int (setting, property_info->property_name, (gint) val_int, error))
if (!validate_int (setting, property_info->property_name, v, error))
return FALSE;
g_object_set (setting, property_info->property_name, (gint) val_int, NULL);
g_object_set (setting, property_info->property_name, (int) v, NULL);
return TRUE;
}
static gboolean
_set_fcn_gobject_int64 (ARGS_SET_FCN)
{
long val_int;
gint64 v;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (!nmc_string_to_int (value, FALSE, 0, 0, &val_int)) {
v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT64, G_MAXINT64, 0);
if (errno) {
g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value);
return FALSE;
}
/* Validate the number according to the property spec */
if (!validate_int64 (setting, property_info->property_name, (gint64) val_int, error))
if (!validate_int64 (setting, property_info->property_name, v, error))
return FALSE;
g_object_set (setting, property_info->property_name, (gint64) val_int, NULL);
g_object_set (setting, property_info->property_name, v, NULL);
return TRUE;
}
@ -2717,20 +2717,19 @@ _set_fcn_dcb_flags (ARGS_SET_FCN)
static gboolean
_set_fcn_dcb_priority (ARGS_SET_FCN)
{
long int priority = 0;
const int INVALID = G_MININT;
int v;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (!nmc_string_to_int (value, FALSE, -1, 7, &priority)) {
v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 7, INVALID);
if (v == INVALID) {
g_set_error (error, 1, 0, _("'%s' is not a DCB app priority"), value);
return FALSE;
}
/* Validate the number according to the property spec */
if (!validate_int (setting, property_info->property_name, (gint) priority, error))
if (!validate_int (setting, property_info->property_name, v, error))
return FALSE;
g_object_set (setting, property_info->property_name, (gint) priority, NULL);
g_object_set (setting, property_info->property_name, v, NULL);
return TRUE;
}