[RFC 08/29] util: Add a helper function virStrToLong_u8p

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Meta 'U8' corresponds to 'uint8_t'.
Add virStrToLong_u8p when parsing uint8_t.

Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx>
---
 src/util/virstring.c | 15 +++++++++++++++
 src/util/virstring.h |  5 +++++
 2 files changed, 20 insertions(+)

diff --git a/src/util/virstring.c b/src/util/virstring.c
index 2d0bb23..83d13ff 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -460,6 +460,21 @@ virStrToLong_uip(char const *s, char **end_ptr, int base, unsigned int *result)
     return 0;
 }
 
+/* Just like virStrToLong_uip, above, but produce an "uint8_t" value.
+ * This version rejects any negative signs.  */
+int
+virStrToLong_u8p(char const *s, char **end_ptr, int base, uint8_t *result)
+{
+    unsigned int val;
+    int ret;
+    ret = virStrToLong_uip(s, end_ptr, base, &val);
+    if (ret < 0 || val > 0xff)
+        return -1;
+
+    *result = (uint8_t) val;
+    return 0;
+}
+
 /* Just like virStrToLong_i, above, but produce a "long" value.  */
 int
 virStrToLong_l(char const *s, char **end_ptr, int base, long *result)
diff --git a/src/util/virstring.h b/src/util/virstring.h
index c528e44..da73f86 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -77,6 +77,11 @@ int virStrToLong_uip(char const *s,
                      int base,
                      unsigned int *result)
     G_GNUC_WARN_UNUSED_RESULT;
+int virStrToLong_u8p(char const *s,
+                     char **end_ptr,
+                     int base,
+                     uint8_t *result)
+    G_GNUC_WARN_UNUSED_RESULT;
 int virStrToLong_l(char const *s,
                    char **end_ptr,
                    int base,
-- 
2.17.1







[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux