ifcfg-rh: add svGetValueInt64() utility function

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller
2014-10-06 15:45:02 +02:00
parent 24a2f9c6cd
commit 212f5dcba6
2 changed files with 39 additions and 0 deletions

View File

@@ -34,6 +34,11 @@
#include "shvar.h"
#include "NetworkManagerUtils.h"
#include "nm-logging.h"
#define PARSE_WARNING(msg...) nm_log_warn (LOGD_SETTINGS, " " msg)
/* Open the file <name>, returning a shvarFile on success and NULL on failure.
* Add a wrinkle to let the caller specify whether or not to create the file
* (actually, return a structure anyway) if it doesn't exist.
@@ -306,6 +311,38 @@ svTrueValue (shvarFile *s, const char *key, gboolean def)
return returnValue;
}
/* svGetValueInt64:
* @s: fhe file
* @key: the name of the key to read
* @base: the numeric base (usually 10). Setting to 0 means "auto". Usually you want 10.
* @min: the minimum for range-check
* @max: the maximum for range-check
* @fallback: the fallback value in any error case
*
* Reads a value @key and converts it to an integer using nm_utils_ascii_str_to_int64().
* In case of error, @errno will be set and @fallback returned. */
gint64
svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 max, gint64 fallback)
{
char *tmp;
gint64 result;
int errsv;
tmp = svGetValue (s, key, FALSE);
if (!tmp) {
errno = 0;
return fallback;
}
result = nm_utils_ascii_str_to_int64 (tmp, base, min, max, fallback);
errsv = errno;
if (errsv != 0)
PARSE_WARNING ("Error reading '%s' value '%s' as integer (%d)", key, tmp, errsv);
g_free (tmp);
return result;
}
/* Set the variable <key> equal to the value <value>.
* If <key> does not exist, and the <current> pointer is set, append

View File

@@ -63,6 +63,8 @@ char *svGetValue (shvarFile *s, const char *key, gboolean verbatim);
*/
gboolean svTrueValue (shvarFile *s, const char *key, gboolean def);
gint64 svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 max, gint64 fallback);
/* Set the variable <key> equal to the value <value>.
* If <key> does not exist, and the <current> pointer is set, append
* the key=value pair after that line. Otherwise, prepend the pair