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

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

 



Ah, you're absolutely right, I completely forgot this function existed. 

On Tue, Apr 18, 2017 at 5:18 PM Matthias Bolte <matthias.bolte@xxxxxxxxxxxxxx> wrote:
2017-04-18 0:36 GMT+02:00 Sri Ramanujam <sramanujam@xxxxxxxxx>:
> 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



Join the conversation! Facebook Twitter LinkedIn pinterest Blog RSS YouTube Google Plus Page

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[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