From 5a872fa8fbd0923ffb1761ced3dc4194497e16f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Tue, 7 Mar 2017 11:50:04 +0100 Subject: [PATCH] add other format specifiers --- lib/print.c | 198 +++++++++++++++++++++++++++++++++++++++------------- lib/print.h | 5 +- 2 files changed, 154 insertions(+), 49 deletions(-) diff --git a/lib/print.c b/lib/print.c index 5a2ec97..585b38d 100644 --- a/lib/print.c +++ b/lib/print.c @@ -19,40 +19,50 @@ */ /* standard libraries */ #include // standard integer types +#include // boolean type #include // 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 */ -#define CRLF 1 +static const bool crlf = true; -void puts(const char* s) +uint8_t putc(char c) { - while (*s) { // stop at end of string -#if CRLF - static char newline = 0; // to remember on which character we sent the newline - - if ('\r' == *s || '\n' == *s) { // send CR+LF newline for most carriage return and line feed combination - if (0==newline || *s==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 - newline = *s; // remember on which character we sent the newline - } - s++; // go to next character - continue; // don't continue processing this character - } else { - newline = 0; // clear new line + 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 } -#endif - putc(*(s++)); // send character + } else { + _putc(c); // print character + newline = 0; // clear new line + length++; // remember we printed 1 character } + return length; // return number of characters printed } -static uint8_t print_unsigned(uint32_t u, uint8_t padding) { +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 pad = 0; // to count how much padding has been printed + uint8_t length = 0; // number of characters printed do { number[digits++] = '0'+(u%10); // store digit u /= 10; // go to next digit @@ -60,46 +70,117 @@ static uint8_t print_unsigned(uint32_t u, uint8_t padding) { 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; zeros0x00ffffff) { + digits = 8; + } else if (hex>0x0000ffff) { + digits = 6; + } else if (hex>0x000000ff) { + digits = 4; + } else { + digits = 2; + } + for (uint8_t zeros = digits; zeros>((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