fix uhub_itoa() and uhub_ulltoa()

This commit is contained in:
Tilka 2012-05-10 23:24:42 +02:00
parent eb49174ab3
commit 8365278cbf
3 changed files with 10 additions and 33 deletions

View File

@ -73,6 +73,7 @@
#include <time.h> #include <time.h>
#if !defined(WIN32) #if !defined(WIN32)
#include <inttypes.h>
#include <unistd.h> #include <unistd.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>

View File

@ -303,47 +303,19 @@ int is_number(const char* value, int* num)
} }
/*
* FIXME: -INTMIN is wrong!
*/
const char* uhub_itoa(int val) const char* uhub_itoa(int val)
{ {
size_t i;
int value;
static char buf[22]; static char buf[22];
memset(buf, 0, sizeof(buf));
if (!val)
{
buf[0] = '0';
buf[1] = '\0';
return buf;
}
i = sizeof(buf) - 1;
for (value = abs(val); value && i > 0; value /= 10)
buf[--i] = "0123456789"[value % 10];
if (val < 0 && i > 0) return snprintf(buf, sizeof(buf), "%d", val) < 0 ? NULL : buf;
buf[--i] = '-';
return buf+i;
} }
const char* uhub_ulltoa(uint64_t val) const char* uhub_ulltoa(uint64_t val)
{ {
size_t i; static char buf[22];
static char buf[22] = { 0, };
memset(buf, 0, sizeof(buf));
if (!val) return snprintf(buf, sizeof(buf), "%"PRIu64, val) < 0 ? NULL : buf;
{
buf[0] = '0';
buf[1] = '\0';
return buf;
}
i = sizeof(buf) - 1;
for (; val && i > 0; val /= 10)
buf[--i] = "0123456789"[val % 10];
return buf+i;
} }

View File

@ -53,7 +53,11 @@ extern int file_read_lines(const char* file, void* data, file_line_handler_t han
*/ */
extern int string_to_boolean(const char* str, int* boolean); extern int string_to_boolean(const char* str, int* boolean);
/**
* Convert number to string.
* Note: these functions are neither thread-safe nor reentrant.
* @return pointer to the resulting string, NULL on error
*/
extern const char* uhub_itoa(int val); extern const char* uhub_itoa(int val);
extern const char* uhub_ulltoa(uint64_t val); extern const char* uhub_ulltoa(uint64_t val);