add minimal printf library

This commit is contained in:
King Kévin 2017-03-07 10:30:57 +01:00
parent aa1aa7162d
commit 05542449e4
2 changed files with 156 additions and 0 deletions

132
lib/print.c Normal file
View File

@ -0,0 +1,132 @@
/* 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 <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 */
#define CRLF 1
void puts(const char* s)
{
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
}
#endif
putc(*(s++)); // send character
}
}
static uint8_t print_unsigned(uint32_t u, uint8_t padding) {
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
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;
}
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
putc('0'); // print 0
pad++; // count printed 0 padding
}
for (uint8_t digit = 0; digit < digits; digit++) { // go through all digits
putc(number[digits-digit-1]); // print digit (in reverse order)
}
return digits+pad; // return number of digits printed
}
uint32_t printf(const char *fmt, ...)
{
uint32_t length = 0; // number of characters printed
uint8_t padding = 0; // number of padding 0's
va_list va; // variable argument list
va_start(va, fmt); // initialise variable argument list
while (*fmt) { // go through format string
if ('%'!=*fmt) { // check for format specifier prefix
#if CRLF
static char newline = 0; // to remember on which character we sent the newline
if ('\r' == *fmt || '\n' == *fmt) { // send CR+LF newline for most carriage return and line feed combination
if (0==newline || *fmt==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 = *fmt; // remember on which character we sent the newline
length += 2; // remember we printer the new line
}
fmt++; // go to next character
continue; // don't continue processing this character
} else {
newline = 0; // clear new line
}
#endif
putc(*fmt++); // print character (no interpretation needed)
length++; // count printed character
} else {
fmt++; // go to format specifier
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':
length += print_unsigned(va_arg(va,uint32_t), padding);
break;
default:
putc(*fmt++); // print character (unknown format specifier)
length++; // count printed character
}
}
padding = 0; // reset padding
}
end:
va_end(va);
return length;
}

24
lib/print.h Normal file
View File

@ -0,0 +1,24 @@
/* 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 (API)
* @file print.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
*/
#pragma once
void putc(char c);
void puts(const char* s);
uint32_t printf(const char *fmt, ...);