[libvirt] [PATCH v3 1/2] util: add virStringParseYesNo()

Shotaro Gotanda g.sho1500 at gmail.com
Wed Mar 13 06:30:10 UTC 2019


This function parse the string "yes" into bool true and
"no" into false, and returns 0.
If the string is anything other than "yes|no",
this function returns -1.

Signed-off-by: Shotaro Gotanda <g.sho1500 at gmail.com>
---
 src/util/virstring.c | 21 +++++++++++++++++++++
 src/util/virstring.h |  3 +++
 2 files changed, 24 insertions(+)

diff --git a/src/util/virstring.c b/src/util/virstring.c
index 33f8191f45..3bbe36ea25 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1548,3 +1548,24 @@ virStringParsePort(const char *str,
 
     return 0;
 }
+
+
+/**
+ * virStringParseYesNo:
+ * @str: "yes|no" to parse, and the value must not be NULL.
+ * @port: pointer to parse and convert "yes|no" into
+ *
+ * Parses a string "yes|no" and convert it into true|false.
+ * Returns 0 on success and -1 on error.
+ */
+int virStringParseYesNo(const char *str, bool *result)
+{
+    if (STREQ(str, "yes"))
+        *result = true;
+    else if (STREQ(str, "no"))
+        *result = false;
+    else
+        return -1;
+
+    return 0;
+}
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 1e36ac459c..9b01e8568a 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -316,6 +316,9 @@ int virStringParsePort(const char *str,
                        unsigned int *port)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
+int virStringParseYesNo(const char *str,
+                       bool *result)
+    ATTRIBUTE_RETURN_CHECK;
 /**
  * VIR_AUTOSTRINGLIST:
  *
-- 
2.19.1




More information about the libvir-list mailing list