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

Erik Skultety eskultet at redhat.com
Wed Mar 13 11:57:02 UTC 2019


On Wed, Mar 13, 2019 at 03:30:10PM +0900, Shotaro Gotanda wrote:
> 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

^This one looks like a copy-paste from virStringParsePort ;)

> + *
> + * Parses a string "yes|no" and convert it into true|false.

...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;
> +}
> 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)

^misaligned

The function should also be added to src/libvirt_private.syms

I tweaked the commit message a tiny bit and I'll perform the adjustments I
noted before pushing by squashing the following:

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 960a97cf1d..92a86943b1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2988,6 +2988,7 @@ virStringListRemove;
 virStringMatch;
 virStringMatchesNameSuffix;
 virStringParsePort;
+virStringParseYesNo;
 virStringReplace;
 virStringSearch;
 virStringSortCompare;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index d9d6c0a33e..95bd7d225e 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1552,10 +1552,11 @@ virStringParsePort(const char *str,

 /**
  * virStringParseYesNo:
- * @str: "yes|no" to parse, and the value must not be NULL.
- * @port: pointer to parse and convert "yes|no" into
+ * @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.
  *
- * 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)
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 9b01e8568a..b0fedf96a3 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -317,7 +317,7 @@ int virStringParsePort(const char *str,
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;

 int virStringParseYesNo(const char *str,
-                       bool *result)
+                        bool *result)
     ATTRIBUTE_RETURN_CHECK;
 /**

Reviewed-by: Erik Skultety <eskultet at redhat.com>




More information about the libvir-list mailing list