aboutsummaryrefslogtreecommitdiff
path: root/libmm-common/mm-common-helpers.c
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-03-17 17:46:06 +0100
committerAleksander Morgado <aleksander@lanedo.com>2012-03-18 17:28:14 +0100
commit46d16d5c995dc2e8f954dd1c27a6fd29e03cedef (patch)
tree0dbf0687b79b9f8c7fbd1c16f107f33e835b0552 /libmm-common/mm-common-helpers.c
parent3046059affaee447923bb8b3e70d06ec02ac9d96 (diff)
libmm-common: new common uint/int/str parsers
Diffstat (limited to 'libmm-common/mm-common-helpers.c')
-rw-r--r--libmm-common/mm-common-helpers.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/libmm-common/mm-common-helpers.c b/libmm-common/mm-common-helpers.c
index d7209cb2..efc86d34 100644
--- a/libmm-common/mm-common-helpers.c
+++ b/libmm-common/mm-common-helpers.c
@@ -14,6 +14,8 @@
*/
#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
#include <gio/gio.h>
#include <ModemManager.h>
@@ -500,3 +502,97 @@ mm_common_parse_key_value_string (const gchar *str,
return TRUE;
}
+
+/*****************************************************************************/
+
+gboolean
+mm_get_int_from_str (const gchar *str,
+ gint *out)
+{
+ glong num;
+
+ errno = 0;
+ num = strtol (str, NULL, 10);
+ if (!errno && num >= G_MININT && num <= G_MAXINT) {
+ *out = (gint)num;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+gboolean
+mm_get_int_from_match_info (GMatchInfo *match_info,
+ guint32 match_index,
+ gint *out)
+{
+ gchar *s;
+ gboolean ret;
+
+ s = g_match_info_fetch (match_info, match_index);
+ g_return_val_if_fail (s != NULL, FALSE);
+
+ ret = mm_get_int_from_str (s, out);
+ g_free (s);
+
+ return ret;
+}
+
+gboolean
+mm_get_uint_from_str (const gchar *str,
+ guint *out)
+{
+ gulong num;
+
+ errno = 0;
+ num = strtoul (str, NULL, 10);
+ if (!errno && num <= G_MAXUINT) {
+ *out = (guint)num;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+gboolean
+mm_get_uint_from_match_info (GMatchInfo *match_info,
+ guint32 match_index,
+ guint *out)
+{
+ gchar *s;
+ gboolean ret;
+
+ s = g_match_info_fetch (match_info, match_index);
+ g_return_val_if_fail (s != NULL, FALSE);
+
+ ret = mm_get_uint_from_str (s, out);
+ g_free (s);
+
+ return ret;
+}
+
+gchar *
+mm_get_string_unquoted_from_match_info (GMatchInfo *match_info,
+ guint32 match_index)
+{
+ gchar *str;
+ gsize len;
+
+ str = g_match_info_fetch (match_info, match_index);
+ if (!str)
+ return NULL;
+
+ len = strlen (str);
+
+ /* Unquote the item if needed */
+ if ((len >= 2) && (str[0] == '"') && (str[len - 1] == '"')) {
+ str[0] = ' ';
+ str[len - 1] = ' ';
+ str = g_strstrip (str);
+ }
+
+ if (!str[0]) {
+ g_free (str);
+ return NULL;
+ }
+
+ return str;
+}