
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>