]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virStringParseYesNo helper
authorShotaro Gotanda <g.sho1500@gmail.com>
Wed, 13 Mar 2019 06:30:10 +0000 (15:30 +0900)
committerErik Skultety <eskultet@redhat.com>
Wed, 13 Mar 2019 13:24:18 +0000 (14:24 +0100)
This helper performs a conversion from a "yes|no" string to a
corresponding boolean. This allows us to drop several repetitive
if-then-else string->bool conversion blocks.

Signed-off-by: Shotaro Gotanda <g.sho1500@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/conf/domain_conf.c
src/conf/secret_conf.c
src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h

index 778c386ee258863cb890906b498e5c426ff367de..cfd72215389dd757370b68b5a2bbe3c4a1ad1abd 100644 (file)
@@ -8465,11 +8465,7 @@ virSecurityLabelDefParseXML(xmlXPathContextPtr ctxt,
     p = virXMLPropStringLimit(ctxt->node, "relabel",
                               VIR_SECURITY_LABEL_BUFLEN-1);
     if (p) {
-        if (STREQ(p, "yes")) {
-            seclabel->relabel = true;
-        } else if (STREQ(p, "no")) {
-            seclabel->relabel = false;
-        } else {
+        if (virStringParseYesNo(p, &seclabel->relabel) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("invalid security relabel value %s"), p);
             goto error;
@@ -8699,11 +8695,7 @@ virSecurityDeviceLabelDefParseXML(virSecurityDeviceLabelDefPtr **seclabels_rtn,
 
         relabel = virXMLPropString(list[i], "relabel");
         if (relabel != NULL) {
-            if (STREQ(relabel, "yes")) {
-                seclabels[i]->relabel = true;
-            } else if (STREQ(relabel, "no")) {
-                seclabels[i]->relabel = false;
-            } else {
+            if (virStringParseYesNo(relabel, &seclabels[i]->relabel) < 0) {
                 virReportError(VIR_ERR_XML_ERROR,
                                _("invalid security relabel value %s"),
                                relabel);
@@ -13706,11 +13698,7 @@ virDomainGraphicsDefParseXMLSDL(virDomainGraphicsDefPtr def,
     ctxt->node = node;
 
     if (fullscreen != NULL) {
-        if (STREQ(fullscreen, "yes")) {
-            def->data.sdl.fullscreen = true;
-        } else if (STREQ(fullscreen, "no")) {
-            def->data.sdl.fullscreen = false;
-        } else {
+        if (virStringParseYesNo(fullscreen, &def->data.sdl.fullscreen) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("unknown fullscreen value '%s'"), fullscreen);
             goto cleanup;
@@ -13798,11 +13786,7 @@ virDomainGraphicsDefParseXMLDesktop(virDomainGraphicsDefPtr def,
     VIR_AUTOFREE(char *) fullscreen = virXMLPropString(node, "fullscreen");
 
     if (fullscreen != NULL) {
-        if (STREQ(fullscreen, "yes")) {
-            def->data.desktop.fullscreen = true;
-        } else if (STREQ(fullscreen, "no")) {
-            def->data.desktop.fullscreen = false;
-        } else {
+        if (virStringParseYesNo(fullscreen, &def->data.desktop.fullscreen) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("unknown fullscreen value '%s'"), fullscreen);
             return -1;
@@ -15511,11 +15495,7 @@ virDomainRedirFilterUSBDevDefParseXML(xmlNodePtr node)
 
     allow = virXMLPropString(node, "allow");
     if (allow) {
-        if (STREQ(allow, "yes")) {
-            def->allow = true;
-        } else if (STREQ(allow, "no")) {
-            def->allow = false;
-        } else {
+        if (virStringParseYesNo(allow, &def->allow) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Invalid allow value, either 'yes' or 'no'"));
             goto error;
index ca6cc194a215a921d3474be35cbc0e2b9b783d53..5b85a7c0be5442e14e323fa7a55dc3412bcbfd44 100644 (file)
@@ -147,11 +147,7 @@ secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
 
     prop = virXPathString("string(./@ephemeral)", ctxt);
     if (prop != NULL) {
-        if (STREQ(prop, "yes")) {
-            def->isephemeral = true;
-        } else if (STREQ(prop, "no")) {
-            def->isephemeral = false;
-        } else {
+        if (virStringParseYesNo(prop, &def->isephemeral) < 0) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("invalid value of 'ephemeral'"));
             goto cleanup;
@@ -161,11 +157,7 @@ secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
 
     prop = virXPathString("string(./@private)", ctxt);
     if (prop != NULL) {
-        if (STREQ(prop, "yes")) {
-            def->isprivate = true;
-        } else if (STREQ(prop, "no")) {
-            def->isprivate = false;
-        } else {
+        if (virStringParseYesNo(prop, &def->isprivate) < 0) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("invalid value of 'private'"));
             goto cleanup;
index 960a97cf1d0deace049bc43ec3350334ee609961..92a86943b1643015cc8c3d658a065fd2c4c2b7ad 100644 (file)
@@ -2988,6 +2988,7 @@ virStringListRemove;
 virStringMatch;
 virStringMatchesNameSuffix;
 virStringParsePort;
+virStringParseYesNo;
 virStringReplace;
 virStringSearch;
 virStringSortCompare;
index 33f8191f456f3493a835b56a770b70588648c3ff..95bd7d225ecee37957d9b732248f00ee3c47809f 100644 (file)
@@ -1548,3 +1548,25 @@ virStringParsePort(const char *str,
 
     return 0;
 }
+
+
+/**
+ * virStringParseYesNo:
+ * @str: "yes|no" to parse, must not be NULL.
+ * @result: pointer to the boolean result of @str conversion
+ *
+ * Parses a "yes|no" string and converts it into a boolean.
+ *
+ * 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;
+}
index 1e36ac459c23de46e514f430652988b54f6386e0..b0fedf96a3529169c9d9ac1cbfbb2d8b787cfec8 100644 (file)
@@ -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:
  *