/* File : barebones/ee_printf.c This file contains an implementation of ee_printf that only requires a method to output a char to a UART without pulling in library code. This code is based on a file that contains the following: Copyright (C) 2002 Michael Ringgaard. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* TuxSH's changes: add support for 64-bit numbers, remove floating-point code TODO! fix left-padding handling with > 10 characters. */ #include "fmt.h" #include "strings.h" #define ZEROPAD (1<<0) //Pad with zero #define SIGN (1<<1) //Unsigned/signed long #define PLUS (1<<2) //Show plus #define SPACE (1<<3) //Spacer #define LEFT (1<<4) //Left justified #define HEX_PREP (1<<5) //0x #define UPPERCASE (1<<6) //'ABCDEF' #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') static u32 skipAtoi(const char **s) { u32 i = 0; while(IS_DIGIT(**s)) i = i * 10 + *((*s)++) - '0'; return i; } static char *processNumber(char *str, s64 num, bool isHex, s32 size, s32 precision, u32 type) { if(type & LEFT) type &= ~ZEROPAD; char sign = 0; if(type & SIGN) { if(num < 0) { sign = '-'; num = -num; size--; } else if(type & PLUS) { sign = '+'; size--; } else if(type & SPACE) { sign = ' '; size--; } } if(type & HEX_PREP && isHex) size -= 2; static const char *lowerDigits = "0123456789abcdef", *upperDigits = "0123456789ABCDEF"; s32 i = 0; char tmp[66], c = (type & ZEROPAD) ? '0' : ' '; const char *dig = (type & UPPERCASE) ? upperDigits : lowerDigits; if(num == 0) tmp[i++] = '0'; else { while (num != 0) { u64 base = isHex ? 16ULL : 10ULL; tmp[i++] = dig[(u64)num % base]; num = (s64)((u64)num / base); } } if(i > precision) precision = i; size -= precision; if(!(type & (ZEROPAD | LEFT))) while(size-- > 0) *str++ = ' '; if(sign) *str++ = sign; if(type & HEX_PREP && isHex) { *str++ = '0'; *str++ = 'x'; } if(!(type & LEFT)) while(size-- > 0) *str++ = c; while(i < precision--) *str++ = '0'; while(i-- > 0) *str++ = tmp[i]; while(size-- > 0) *str++ = ' '; return str; } u32 vsprintf(char *buf, const char *fmt, va_list args) { char *str; for(str = buf; *fmt; fmt++) { if(*fmt != '%') { *str++ = *fmt; continue; } //Process flags u32 flags = 0; //Flags to number() bool loop = false; while(!loop) { switch(*++fmt) { case '-': flags |= LEFT; break; case '+': flags |= PLUS; break; case ' ': flags |= SPACE; break; case '#': flags |= HEX_PREP; break; case '0': flags |= ZEROPAD; break; default: loop = true; break; } } //Get field width s32 fieldWidth = -1; //Width of output field if(IS_DIGIT(*fmt)) fieldWidth = skipAtoi(&fmt); else if(*fmt == '*') { fmt++; fieldWidth = va_arg(args, s32); if(fieldWidth < 0) { fieldWidth = -fieldWidth; flags |= LEFT; } } //Get the precision s32 precision = -1; //Min. # of digits for integers; max number of chars for from string if(*fmt == '.') { fmt++; if(IS_DIGIT(*fmt)) precision = skipAtoi(&fmt); else if(*fmt == '*') { fmt++; precision = va_arg(args, s32); } if(precision < 0) precision = 0; } //Get the conversion qualifier bool isLongLong = false; if(*fmt == 'l') { if(*++fmt == 'l') { fmt++; isLongLong = true; } } //Default base bool isHex = false; switch(*fmt) { case 'c': if(!(flags & LEFT)) while(--fieldWidth > 0) *str++ = ' '; *str++ = (u8)va_arg(args, s32); while(--fieldWidth > 0) *str++ = ' '; continue; case 's': { char *s = va_arg(args, char *); if(!s) s = ""; u32 len = (precision != -1) ? strnlen(s, precision) : strlen(s); if(!(flags & LEFT)) while((s32)len < fieldWidth--) *str++ = ' '; for(u32 i = 0; i < len; i++) *str++ = *s++; while((s32)len < fieldWidth--) *str++ = ' '; continue; } case 'p': if(fieldWidth == -1) { fieldWidth = 8; flags |= ZEROPAD; } str = processNumber(str, (s64)va_arg(args, u32), true, fieldWidth, precision, flags); continue; //Integer number formats - set up the flags and "break" case 'X': flags |= UPPERCASE; case 'x': isHex = true; break; case 'd': case 'i': flags |= SIGN; case 'u': break; default: if(*fmt != '%') *str++ = '%'; if(*fmt) *str++ = *fmt; else fmt--; continue; } s64 num; if(flags & SIGN) { if(isLongLong) num = va_arg(args, s64); else num = va_arg(args, s32); } else { if(isLongLong) num = va_arg(args, u64); else num = va_arg(args, u32); } str = processNumber(str, num, isHex, fieldWidth, precision, flags); } *str = 0; return str - buf; } u32 sprintf(char *buf, const char *fmt, ...) { va_list args; va_start(args, fmt); u32 res = vsprintf(buf, fmt, args); va_end(args); return res; }