-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny_printf.c
467 lines (443 loc) · 24.2 KB
/
tiny_printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**********************************************************************************************************************/
/** Minimal printf implementation
***********************************************************************************************************************
* Copyright 2001 Georges Menie
* https://www.menie.org/georges/embedded/small_printf_source_code.html
*
* Modified by Thuffir in 2019
* https://github.com/Thuffir/printf
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* All functions are thread safe.
*
* @file tiny_printf.c
*
**********************************************************************************************************************/
#include "tiny_printf.h"
/***********************************************************************************************************************
* Formatting options
**********************************************************************************************************************/
/// Pad to the right
#define FORMAT_PAD_RIGHT (1 << 0)
/// Pad with zeros
#define FORMAT_PAD_ZERO (1 << 1)
/// Format as long long
#define FORMAT_LONG_LONG (1 << 2)
/**********************************************************************************************************************/
/** Print String
***********************************************************************************************************************
*
* @returns number of bytes printed
*
**********************************************************************************************************************/
static int TinyPrintString(
/// [in] printchar function pointer
printchar_t printchar,
/// [in] context for printchar function
void* ctx,
/// [in] String to print
const char *string,
/// [in] Desired Width
int width,
/// [in] Formatting options
int options)
{
int pc = 0, padchar = ' ';
if(width > 0) {
int len = 0;
const char *ptr;
for(ptr = string; *ptr; ++ptr) {
++len;
}
if(len >= width) {
width = 0;
}
else {
width -= len;
}
if(options & FORMAT_PAD_ZERO) {
padchar = '0';
}
}
if(!(options & FORMAT_PAD_RIGHT)) {
for(; width > 0; --width) {
printchar(ctx, padchar);
++pc;
}
}
for(; *string; ++string) {
printchar(ctx, *string);
++pc;
}
for(; width > 0; --width) {
printchar(ctx, padchar);
++pc;
}
return pc;
}
/**********************************************************************************************************************/
/** Print Integer Value Template
***********************************************************************************************************************
*
* @param FUNC_NAME - Name of the function
* @param INT_TYPE - Type of the integer
* @param PRINT_BUF_SIZE - Size of the print buffer
*
* @returns number of bytes printed
*
**********************************************************************************************************************/
#define TINY_PRINT_INTEGER(FUNC_NAME, INT_TYPE, PRINT_BUF_SIZE) \
static int FUNC_NAME( \
/** [in] printchar function pointer */ \
printchar_t printchar, \
/** [in] context for printchar function */ \
void* ctx, \
/** [in] Integer to print */ \
INT_TYPE i, \
/** [in] Integer base to print */ \
int b, \
/** [in] Is integer signed? */ \
int sg, \
/** [in] Desired Width */ \
int width, \
/** [in] Formatting options */ \
int options, \
/** [in] Hexadecimal base character (lower / uppercase) */ \
int letbase) \
{ \
char print_buf[PRINT_BUF_SIZE]; \
char *s; \
int t, neg = 0, pc = 0; \
unsigned INT_TYPE u = i; \
\
if(i == 0) { \
print_buf[0] = '0'; \
print_buf[1] = '\0'; \
return TinyPrintString(printchar, ctx, print_buf, width, options); \
} \
\
if(sg && b == 10 && i < 0) { \
neg = 1; \
u = -i; \
} \
\
s = print_buf + sizeof(print_buf) - 1; \
*s = '\0'; \
\
while(u) { \
t = u % b; \
if(t >= 10) { \
t += letbase - '0' - 10; \
} \
*--s = t + '0'; \
u /= b; \
} \
\
if(neg) { \
if(width && (options & FORMAT_PAD_ZERO)) { \
printchar(ctx, '-'); \
++pc; \
--width; \
} \
else { \
*--s = '-'; \
} \
} \
\
return pc + TinyPrintString(printchar, ctx, s, width, options); \
}
/// Integer print function 32 bit version (Most digits at -2^31 = -2147483648)
TINY_PRINT_INTEGER(TinyPrintInteger, int, sizeof("-2147483648"))
#ifdef TINY_PRINTF_LONG_LONG
/// Integer print function 64 bit version (Most digits at -2^63 = -9223372036854775808)
TINY_PRINT_INTEGER(TinyPrintIntegerLongLong, long long int, sizeof("-9223372036854775808"))
#endif
/**********************************************************************************************************************/
/** Print Floating Point Value Template
***********************************************************************************************************************
*
* @param FUNC_NAME - Name of the function
* @param FLOAT_TYPE - Type of the floating point
*
* @returns number of bytes printed
*
* @TODO: Make it work with width and options!
*
**********************************************************************************************************************/
#define TINY_PRINT_FLOAT(FUNC_NAME, FLOAT_TYPE) \
static unsigned int FUNC_NAME( \
/** [in] printchar function pointer */ \
printchar_t printchar, \
/** [in] context for printchar function */ \
void* ctx, \
/** [in] Number to print */ \
FLOAT_TYPE number, \
/** [in] Precision (number of fractional digits to print, can be zero) */ \
unsigned int precision) \
{ \
unsigned int pc = 0, intPart, multiplier, i; \
\
/* If number is negative */ \
if(number < 0) { \
/* make it positive */ \
number = -number; \
/* and print sign */ \
printchar(ctx, '-'); \
pc++; \
} \
\
/* calculate multiplier */ \
for(i = 0, multiplier = 1; i < precision; i++) { \
multiplier *= 10; \
} \
\
/* Round number */ \
number += (FLOAT_TYPE)0.5 / (FLOAT_TYPE)multiplier; \
\
/* convert integer part to integer and print it */ \
intPart = (unsigned int)number; \
pc += TinyPrintInteger(printchar, ctx, intPart, 10, 0, 0, 0, 'a'); \
\
/* If we have to print fractional digits */ \
if(precision != 0) { \
/* calculate fractional part as integer */ \
unsigned int fracPart = (unsigned int)((number - (FLOAT_TYPE)intPart) * (FLOAT_TYPE)multiplier); \
/* and print it along the period sign */ \
printchar(ctx, '.'); \
pc++; \
pc += TinyPrintInteger(printchar, ctx, fracPart, 10, 0, precision, FORMAT_PAD_ZERO, 'a'); \
} \
\
return pc; \
}
#ifdef TINY_PRINTF_FLOAT
// Floating point print function with requested type
TINY_PRINT_FLOAT(TinyPrintFloat, TINY_PRINTF_FLOAT)
#endif
/**********************************************************************************************************************/
/** The printf function
***********************************************************************************************************************
*
* A format specifier follows this prototype:
* %[flags][width][.precision][length]specifier
*
* @returns number of bytes printed
*
**********************************************************************************************************************/
int TinyPrintf(
/// [in] printchar function pointer
printchar_t printchar,
/// [in] context for printchar function
void* ctx,
/// [in] Format string
const char *format,
/// [in] Format arguments
va_list args)
{
int width, options, precision;
int pc = 0;
for(; *format != 0; ++format) {
if(*format == '%') {
++format;
width = options = 0;
// Default precision (at the moment only for floating point)
precision = 6;
if(*format == '\0') {
break;
}
if(*format == '%') {
goto out;
}
if(*format == '-') {
++format;
options = FORMAT_PAD_RIGHT;
}
while(*format == '0') {
++format;
options |= FORMAT_PAD_ZERO;
}
for(; *format >= '0' && *format <= '9'; ++format) {
width *= 10;
width += *format - '0';
}
if(*format == '.') {
++format;
precision = 0;
for(; *format >= '0' && *format <= '9'; ++format) {
precision *= 10;
precision += *format - '0';
}
}
if(*format == 'l') {
++format;
if(*format == 'l') {
++format;
options |= FORMAT_LONG_LONG;
}
}
if(*format == 's') {
char *s = va_arg(args, char *);
pc += TinyPrintString(printchar, ctx, s ? s : "(null)", width, options);
continue;
}
if(*format == 'd') {
pc +=
#ifdef TINY_PRINTF_LONG_LONG
(options & FORMAT_LONG_LONG) ?
TinyPrintIntegerLongLong(printchar, ctx, va_arg(args, long long int), 10, 1, width, options, 'a') :
#endif
TinyPrintInteger(printchar, ctx, va_arg(args, int), 10, 1, width, options, 'a');
continue;
}
if(*format == 'x') {
pc +=
#ifdef TINY_PRINTF_LONG_LONG
(options & FORMAT_LONG_LONG) ?
TinyPrintIntegerLongLong(printchar, ctx, va_arg(args, long long int), 16, 0, width, options, 'a') :
#endif
TinyPrintInteger(printchar, ctx, va_arg(args, int), 16, 0, width, options, 'a');
continue;
}
if(*format == 'X') {
pc +=
#ifdef TINY_PRINTF_LONG_LONG
(options & FORMAT_LONG_LONG) ?
TinyPrintIntegerLongLong(printchar, ctx, va_arg(args, long long int), 16, 0, width, options, 'A') :
#endif
TinyPrintInteger(printchar, ctx, va_arg(args, int), 16, 0, width, options, 'A');
continue;
}
if(*format == 'p') {
pc += TinyPrintInteger(printchar, ctx, va_arg(args, int), 16, 0, sizeof(void *) * 2, FORMAT_PAD_ZERO, 'A');
continue;
}
if(*format == 'u') {
pc +=
#ifdef TINY_PRINTF_LONG_LONG
(options & FORMAT_LONG_LONG) ?
TinyPrintIntegerLongLong(printchar, ctx, va_arg(args, long long int), 10, 0, width, options, 'a') :
#endif
TinyPrintInteger(printchar, ctx, va_arg(args, int), 10, 0, width, options, 'a');
continue;
}
#ifdef TINY_PRINTF_FLOAT
if(*format == 'f') {
pc += TinyPrintFloat(printchar, ctx, va_arg(args, double), precision);
continue;
}
#endif
if(*format == 'c') {
// This works with any byte, even with zero
if(width <= 1) {
printchar(ctx, (char)va_arg(args, int));
++pc;
}
// This works with width, but not with zero byte
else {
// chars are converted to int then pushed on the stack
char scr[2];
scr[0] = (char)va_arg(args, int);
scr[1] = '\0';
pc += TinyPrintString(printchar, ctx, scr, width, options);
}
continue;
}
}
else {
out:
printchar(ctx, *format);
++pc;
}
}
return pc;
}
/**********************************************************************************************************************/
/** Context for the TinySNPrintChar() function
**********************************************************************************************************************/
typedef struct {
/// Current string write pointer
char *string;
/// Number of bytes left in the string buffer
int length;
} TinySNPrintCharContext;
/**********************************************************************************************************************/
/** Put one byte into sized buffer
**********************************************************************************************************************/
static void TinySNPrintChar(
/// [in] Pointer to the context
void *context,
/// [in] Character to print
char c)
{
// This will spare us a lots of casts.
TinySNPrintCharContext *ctx = context;
// Do we still have space in the buffer left?
if(ctx->length != 0) {
// Put character into the string buffer
*(ctx->string) = c;
// Increment write pointer
(ctx->string)++;
// Decrement space left
(ctx->length)--;
}
}
/**********************************************************************************************************************/
/** Write formatted output to sized buffer
***********************************************************************************************************************
*
* printf a string into the buffer pointed by string (taking length as the maximum buffer capacity to fill).
*
* If the resulting string would be longer than n-1 characters, the remaining characters are discarded and not stored,
* but counted for the value returned by the function.
*
* A terminating null character is automatically appended after the content written.
*
* @returns
* The number of characters that would have been written if n had been sufficiently large, not counting the terminating
* null character.
*
**********************************************************************************************************************/
int TinySNprintf(
/// [in] String to print into
char *string,
/// [in] Maximum number of bytes to be used in the buffer. (The generated string has a length of at most n-1, leaving
/// space for the additional terminating null character.)
int length,
/// [in] Format string
const char *fmt,
/// [in] Format arguments
...)
{
// printchar context
TinySNPrintCharContext context;
// Return value
int ret;
// additional parameters
va_list va;
// We store the string starting address
context.string = string;
// And the maximum length (we reserve space for the terminating null character)
context.length = length - 1;
// Do the formatted printing
va_start(va, fmt);
ret = TinyPrintf(TinySNPrintChar, &context, fmt, va);
va_end(va);
// Now we free up the reserved space for the the terminating null character
context.length++;
// And put it also into the string
TinySNPrintChar(&context, '\0');
return ret;
}