[libvirt] [PATCH 5/6] util: add virNumToStr

Sri Ramanujam posted 6 patches 8 years, 9 months ago
[libvirt] [PATCH 5/6] util: add virNumToStr
Posted by Sri Ramanujam 8 years, 9 months ago
Add virNumToStr(), which safely converts numbers into their string
representation.

Functions added:
* virNumToStr_l
* virNumToStr_ul
---
 src/util/virstring.c | 34 ++++++++++++++++++++++++++++++++++
 src/util/virstring.h |  8 ++++++++
 2 files changed, 42 insertions(+)

diff --git a/src/util/virstring.c b/src/util/virstring.c
index 69abc26..f0d9e19 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -536,6 +536,40 @@ virStrToDouble(char const *s,
     return 0;
 }
 
+/**
+ * Converts signed number to string representation. The caller is responsible
+ * for freeing the result.
+ */
+int
+virNumToStr_l(long num, char **dst)
+{
+    int sz;
+
+    sz = snprintf(NULL, 0, "%ld", num);
+    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
+        return -1;
+
+    snprintf(*dst, sz + 1, "%ld", num);
+    return 0;
+}
+
+/**
+ * Converts unsigned number to string representation. The caller is responsible
+ * for freeing the result.
+ */
+int
+virNumToStr_ul(unsigned long num, char **dst)
+{
+    int sz;
+
+    sz = snprintf(NULL, 0, "%lu", num);
+    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
+        return -1;
+
+    snprintf(*dst, sz + 1, "%lu", num);
+    return 0;
+}
+
 int
 virVasprintfInternal(bool report,
                      int domcode,
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 603650a..9918e07 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -109,6 +109,13 @@ int virStrToDouble(char const *s,
                    double *result)
     ATTRIBUTE_RETURN_CHECK;
 
+int virNumToStr_l(long num,
+                  char **dst)
+    ATTRIBUTE_RETURN_CHECK;
+int virnumToStr_ul(unsigned long num,
+                    char **dst)
+    ATTRIBUTE_RETURN_CHECK;
+
 void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
 void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);
 void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
@@ -288,6 +295,7 @@ bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen);
 
 char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
 
+
 static inline void
 virStringTrimOptionalNewline(char *str)
 {
-- 
2.9.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 5/6] util: add virNumToStr
Posted by Matthias Bolte 8 years, 9 months ago
2017-04-18 0:36 GMT+02:00 Sri Ramanujam <sramanujam@datto.com>:
> Add virNumToStr(), which safely converts numbers into their string
> representation.
>
> Functions added:
> * virNumToStr_l
> * virNumToStr_ul
> ---
>  src/util/virstring.c | 34 ++++++++++++++++++++++++++++++++++
>  src/util/virstring.h |  8 ++++++++
>  2 files changed, 42 insertions(+)
>
> diff --git a/src/util/virstring.c b/src/util/virstring.c
> index 69abc26..f0d9e19 100644
> --- a/src/util/virstring.c
> +++ b/src/util/virstring.c
> @@ -536,6 +536,40 @@ virStrToDouble(char const *s,
>      return 0;
>  }
>
> +/**
> + * Converts signed number to string representation. The caller is responsible
> + * for freeing the result.
> + */
> +int
> +virNumToStr_l(long num, char **dst)
> +{
> +    int sz;
> +
> +    sz = snprintf(NULL, 0, "%ld", num);
> +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
> +        return -1;
> +
> +    snprintf(*dst, sz + 1, "%ld", num);
> +    return 0;
> +}
> +
> +/**
> + * Converts unsigned number to string representation. The caller is responsible
> + * for freeing the result.
> + */
> +int
> +virNumToStr_ul(unsigned long num, char **dst)
> +{
> +    int sz;
> +
> +    sz = snprintf(NULL, 0, "%lu", num);
> +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
> +        return -1;
> +
> +    snprintf(*dst, sz + 1, "%lu", num);
> +    return 0;
> +}
> +

What's the gain of

if (virNumToStr_ul(memory_mb, &memory_str) < 0)
   goto cleanup;

over

if (virAsprintf(&memory_str, "%lu", memory_mb) < 0)
   goto cleanup;

?

I think those two new functions are not necessary at all.

-- 
Matthias Bolte
http://photron.blogspot.com

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 5/6] util: add virNumToStr
Posted by Sri Ramanujam 8 years, 9 months ago
Ah, you're absolutely right, I completely forgot this function existed.

On Tue, Apr 18, 2017 at 5:18 PM Matthias Bolte <
matthias.bolte@googlemail.com> wrote:

> 2017-04-18 0:36 GMT+02:00 Sri Ramanujam <sramanujam@datto.com>:
> > Add virNumToStr(), which safely converts numbers into their string
> > representation.
> >
> > Functions added:
> > * virNumToStr_l
> > * virNumToStr_ul
> > ---
> >  src/util/virstring.c | 34 ++++++++++++++++++++++++++++++++++
> >  src/util/virstring.h |  8 ++++++++
> >  2 files changed, 42 insertions(+)
> >
> > diff --git a/src/util/virstring.c b/src/util/virstring.c
> > index 69abc26..f0d9e19 100644
> > --- a/src/util/virstring.c
> > +++ b/src/util/virstring.c
> > @@ -536,6 +536,40 @@ virStrToDouble(char const *s,
> >      return 0;
> >  }
> >
> > +/**
> > + * Converts signed number to string representation. The caller is
> responsible
> > + * for freeing the result.
> > + */
> > +int
> > +virNumToStr_l(long num, char **dst)
> > +{
> > +    int sz;
> > +
> > +    sz = snprintf(NULL, 0, "%ld", num);
> > +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
> > +        return -1;
> > +
> > +    snprintf(*dst, sz + 1, "%ld", num);
> > +    return 0;
> > +}
> > +
> > +/**
> > + * Converts unsigned number to string representation. The caller is
> responsible
> > + * for freeing the result.
> > + */
> > +int
> > +virNumToStr_ul(unsigned long num, char **dst)
> > +{
> > +    int sz;
> > +
> > +    sz = snprintf(NULL, 0, "%lu", num);
> > +    if (sz > 0 && VIR_ALLOC_N(*dst, sz + 1) < 0)
> > +        return -1;
> > +
> > +    snprintf(*dst, sz + 1, "%lu", num);
> > +    return 0;
> > +}
> > +
>
> What's the gain of
>
> if (virNumToStr_ul(memory_mb, &memory_str) < 0)
>    goto cleanup;
>
> over
>
> if (virAsprintf(&memory_str, "%lu", memory_mb) < 0)
>    goto cleanup;
>
> ?
>
> I think those two new functions are not necessary at all.
>
> --
> Matthias Bolte
> http://photron.blogspot.com
>
-- 
*Sri Ramanujam*
Software Engineer
Datto, Inc.
www.datto.com

<http://datto.com/datto-signature/>

Join the conversation! [image: Facebook]
<http://www.facebook.com/dattoinc> [image:
Twitter] <https://twitter.com/dattobackup> [image: LinkedIn]
<http://www.linkedin.com/company/1477873?trk=tyah> [image: pinterest]
<http://pinterest.com/dattobackup/> [image: Blog RSS]
<http://blog.dattobackup.com/blog> [image: YouTube]
<http://www.youtube.com/user/DattoInc/featured> [image: Google Plus Page]
<https://plus.google.com/u/0/108292366419623632143/posts>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list