237 lines
7.9 KiB
C
237 lines
7.9 KiB
C
/* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
/** printing utilities to replace the large printf from the standard library (code)
|
|
* @file print.c
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
* @date 2017
|
|
*/
|
|
/* standard libraries */
|
|
#include <stdint.h> // standard integer types
|
|
#include <stdbool.h> // boolean type
|
|
#include <stdarg.h> // variadic utilities
|
|
|
|
/* own libraries */
|
|
#include "print.h" // printing utilities
|
|
|
|
/** output \r\n (Carriage Return + Line Feed) for each \r, \n, \r\n, or \n\r for better terminal compatibility */
|
|
static const bool crlf = true;
|
|
|
|
uint8_t putc(char c)
|
|
{
|
|
uint8_t length = 0; // number of characters printed
|
|
static char newline = 0; // to remember on which character we sent the newline
|
|
if (!crlf) {
|
|
_putc(c); // print character
|
|
length++; // remember we printed 1 character
|
|
} else if ('\r' == c || '\n' == c) { // send CR+LF newline for most carriage return and line feed combination
|
|
if (0==newline || c==newline) { // send newline only if not already send (and only once on \r\n or \n\r)
|
|
_putc('\r'); // send CR
|
|
_putc('\n'); // send LF
|
|
length += 2; // remember we printed 2 characters
|
|
newline = c; // remember on which character we sent the newline
|
|
}
|
|
} else {
|
|
_putc(c); // print character
|
|
newline = 0; // clear new line
|
|
length++; // remember we printed 1 character
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
uint32_t puts(const char* s)
|
|
{
|
|
uint32_t length = 0; // number of characters printed
|
|
while (*s) { // stop at end of string
|
|
length += putc(*(s++)); // send character
|
|
}
|
|
return length;
|
|
}
|
|
|
|
static uint8_t print_unsigned(uint64_t u, uint8_t padding, bool sign) {
|
|
char number[20] = {0}; // construct the number in reverse order (20 chars are required to store UINT64_MAX)
|
|
uint8_t digits = 0; // to count the number of digits
|
|
uint8_t length = 0; // number of characters printed
|
|
do {
|
|
number[digits++] = '0'+(u%10); // store digit
|
|
u /= 10; // go to next digit
|
|
} while (u>0);
|
|
if (digits>sizeof(number)) { // prevent buffer underflow
|
|
return 0;
|
|
}
|
|
if (sign) { // print sign
|
|
length += putc('+'); // we only have positive numbers
|
|
}
|
|
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
|
|
length += putc('0'); // print 0
|
|
}
|
|
for (uint8_t digit = 0; digit < digits; digit++) { // go through all digits
|
|
length += putc(number[digits-digit-1]); // print digit (in reverse order)
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
static uint8_t print_signed(int64_t d, uint8_t padding, bool sign) {
|
|
uint8_t length = 0; // number of characters printed
|
|
if (d<0) {
|
|
length += putc('-'); // print sign
|
|
length += print_unsigned((uint64_t)-d, padding, false); // print number (casting because there is one more negative value then positive value)
|
|
} else {
|
|
length += print_unsigned(d, padding, sign); // print number
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
static uint8_t print_nibble(uint8_t nibble, bool upcase) {
|
|
uint8_t length = 0; // number of characters printed
|
|
nibble &= 0x0f; // ensure we only have a nibble
|
|
if (nibble<10) {
|
|
length += putc('0'+nibble);
|
|
} else if (upcase) {
|
|
length += putc('A'+nibble-10);
|
|
} else {
|
|
length += putc('a'+nibble-10);
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
static uint8_t print_hex(uint32_t hex, uint8_t padding, bool prefix, bool upcase) {
|
|
uint8_t length = 0; // number of characters printed
|
|
if (prefix) { // print 0x prefix
|
|
length += putc('0');
|
|
length += putc('x');
|
|
}
|
|
uint8_t digits = 0; // number of digits to print
|
|
// figure out number of digits to print
|
|
if (hex>0x00ffffff) {
|
|
digits = 8;
|
|
} else if (hex>0x0000ffff) {
|
|
digits = 6;
|
|
} else if (hex>0x000000ff) {
|
|
digits = 4;
|
|
} else {
|
|
digits = 2;
|
|
}
|
|
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
|
|
length += putc('0'); // print 0
|
|
}
|
|
for (uint8_t digit = 0; digit < digits; digit++) { // go through all digits
|
|
length += print_nibble(hex>>((digits-digit-1)*4), upcase); // print nibble (in reverse order)
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
static uint8_t print_bits(uint32_t u, uint8_t padding, bool prefix) {
|
|
char bits[32] = {0}; // construct the bit string in reverse order
|
|
uint8_t digits = 0; // to count the number of digits
|
|
uint8_t length = 0; // number of characters printed
|
|
do {
|
|
bits[digits++] = '0'+(u&0x1); // store bit
|
|
u >>= 1; // go to next bit
|
|
} while (u>0);
|
|
if (digits>sizeof(bits)) { // prevent buffer underflow
|
|
return 0;
|
|
}
|
|
if (prefix) { // print prefix
|
|
length += putc('0');
|
|
length += putc('b');
|
|
}
|
|
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
|
|
length += putc('0'); // print 0
|
|
}
|
|
for (uint8_t digit = 0; digit < digits; digit++) { // go through all bits
|
|
length += putc(bits[digits-digit-1]); // print bit (in reverse order)
|
|
}
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
uint32_t printf(const char *fmt, ...)
|
|
{
|
|
uint32_t length = 0; // number of characters printed
|
|
uint8_t padding = 0; // number of padding 0's
|
|
bool sign = false; // if sign needs to be printed
|
|
va_list va; // variable argument list
|
|
va_start(va, fmt); // initialise variable argument list
|
|
while (*fmt) { // go through format string
|
|
padding = 0; // reset padding
|
|
sign = false; // reset sign
|
|
if ('%'!=*fmt) { // check for format specifier prefix
|
|
length += putc(*fmt++); // print character (no interpretation needed)
|
|
} else {
|
|
fmt++; // go to format specifier
|
|
if (0==*fmt) { // end of string detected
|
|
goto end;
|
|
}
|
|
// check if sign need to be printed
|
|
if ('+'==*fmt) { // sign required
|
|
sign = true; // remember sign is required
|
|
fmt++; // go to padding number
|
|
if (0==*fmt) { // end of string detected
|
|
goto end;
|
|
}
|
|
}
|
|
// check padding
|
|
if ('0'==*fmt) { // padding required
|
|
fmt++; // go to padding number
|
|
if (0==*fmt) { // end of string detected
|
|
goto end;
|
|
}
|
|
if (*fmt>='0' && *fmt<='9') {
|
|
padding = *fmt-'0';
|
|
fmt++; // go to format specifier
|
|
if (0==*fmt) { // end of string detected
|
|
goto end;
|
|
}
|
|
}
|
|
}
|
|
// check format specifier
|
|
switch (*fmt) {
|
|
case 'u': // for uint8_t, uint16_t, uint32_t, unsigned int, unsigned long
|
|
length += print_unsigned(va_arg(va,uint32_t), padding, sign);
|
|
break;
|
|
case 'U': // for uint64_t, unsigned long long
|
|
length += print_unsigned(va_arg(va,uint64_t), padding, sign);
|
|
break;
|
|
case 'd': // for int8_t, int16_t, int32_t, int, long
|
|
length += print_signed(va_arg(va,int32_t), padding, sign);
|
|
break;
|
|
case 'D': // for int64_t, long long
|
|
length += print_signed(va_arg(va,int64_t), padding, sign);
|
|
break;
|
|
case 'c': // for char, unsigned char
|
|
length += putc((char)(va_arg(va,int))); // needs casting because the returned value is promoted
|
|
break;
|
|
case 'x': // for downcase hexadecimal
|
|
length += print_hex(va_arg(va,uint32_t), padding, sign, false);
|
|
break;
|
|
case 'X': // for upcase hexadecimal
|
|
length += print_hex(va_arg(va,uint32_t), padding, sign, true);
|
|
break;
|
|
case 'b': // for bits
|
|
length += print_bits(va_arg(va,uint32_t), padding, sign);
|
|
break;
|
|
case 's': // for strings
|
|
length += puts(va_arg(va,char*));
|
|
break;
|
|
default:
|
|
length += putc(*fmt); // print character (unknown format specifier)
|
|
}
|
|
fmt++; // go to next character
|
|
}
|
|
}
|
|
end:
|
|
va_end(va);
|
|
return length;
|
|
}
|