X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=libvisor%2Fsrc%2Fviprintf.c;h=315a7e1594563ffac4ddd816b82a0222157cbae3;hp=5f2b3128c13445533901cd75f2dfbd230d4fa22c;hb=9f0326a07930f909b44433f5d0e3b556d0b16a51;hpb=4f957b16f77eb7761afd9e0b9064c7f08deb45be diff --git a/libvisor/src/viprintf.c b/libvisor/src/viprintf.c index 5f2b312..315a7e1 100644 --- a/libvisor/src/viprintf.c +++ b/libvisor/src/viprintf.c @@ -6,6 +6,8 @@ enum { static int intern_printf(int out, char *buf, unsigned long sz, const char *fmt, va_list ap); static void bwrite(int out, char *buf, unsigned long buf_sz, char *str, int sz); +static void utoa(unsigned int val, char *buf, int base); +static void itoa(int val, char *buf, int base); int sprintf(char *buf, const char *fmt, ...) { @@ -239,11 +241,66 @@ static int intern_printf(int out, char *buf, unsigned long sz, const char *fmt, */ static void bwrite(int out, char *buf, unsigned long buf_sz, char *str, int sz) { - int i; - if(out == OUT_BUF) { if(buf_sz && buf_sz <= sz) sz = buf_sz; buf[sz] = 0; memcpy(buf, str, sz); } } + +static void utoa(unsigned int val, char *buf, int base) +{ + static char rbuf[16]; + char *ptr = rbuf; + + if(val == 0) { + *ptr++ = '0'; + } + + while(val) { + unsigned int digit = val % base; + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); + val /= base; + } + + ptr--; + + while(ptr >= rbuf) { + *buf++ = *ptr--; + } + *buf = 0; +} + +static void itoa(int val, char *buf, int base) +{ + static char rbuf[16]; + char *ptr = rbuf; + int neg = 0; + + if(val < 0) { + neg = 1; + val = -val; + } + + if(val == 0) { + *ptr++ = '0'; + } + + while(val) { + int digit = val % base; + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); + val /= base; + } + + if(neg) { + *ptr++ = '-'; + } + + ptr--; + + while(ptr >= rbuf) { + *buf++ = *ptr--; + } + *buf = 0; +} +