X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=libvisor%2Fsrc%2Fviprintf.c;h=73276cb2441baf75058fadc4c2fe2809ff30b45d;hp=5f2b3128c13445533901cd75f2dfbd230d4fa22c;hb=a3d913263bd55389a60c8db96d1ea1fc11acf178;hpb=4f957b16f77eb7761afd9e0b9064c7f08deb45be diff --git a/libvisor/src/viprintf.c b/libvisor/src/viprintf.c index 5f2b312..73276cb 100644 --- a/libvisor/src/viprintf.c +++ b/libvisor/src/viprintf.c @@ -1,3 +1,20 @@ +/* +visor - lightweight system-independent embeddable text editor framework +Copyright (C) 2019 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . +*/ #include "vilibc.h" enum { @@ -6,6 +23,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 +258,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; +} +