root readme, directory structure, and copyright headers
[visor] / libvisor / src / viprintf.c
index 5f2b312..73276cb 100644 (file)
@@ -1,3 +1,20 @@
+/*
+visor - lightweight system-independent embeddable text editor framework
+Copyright (C)  2019 John Tsiombikas <nuclear@member.fsf.org>
+
+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 <https://www.gnu.org/licenses/>.
+*/
 #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;
+}
+