4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005, 2008 Develer S.r.l. (http://www.develer.com/)
35 * \brief Basic "printf", "sprintf" and "fprintf" formatter.
37 * This module is 100% reentrant and can be adapted to user-defined routines
38 * that needs formatters with special properties like different output
39 * channels or new format specifiers.
41 * To reduce size in applications not using real numbers or long integers
42 * the formatter may be compiled to exclude certain parts. This is
43 * controlled by giving a -D option a compilation time:
46 * -D CONFIG_PRINTF=PRINTF_FULL Full ANSI printf formatter, with some C99 extensions
47 * -D CONFIG_PRINTF=PRINTF_NOFLOAT Exclude support for floats
48 * -D CONFIG_PRINTF=PRINTF_REDUCED Simplified formatter (see below)
49 * -D CONFIG_PRINTF=PRINTF_NOMODIFIERS Exclude "l", "z" and "h" modifiers in reduced version
50 * -D CONFIG_PRINTF=PRINTF_DISABLED No formatter at all
53 * Code size on AVR4 with GCC 3.4.1 (-O2):
54 * \li PRINTF_FULL 2912byte (0xB60)
55 * \li PRINTF_NOFLOAT 1684byte (0x694)
56 * \li PRINTF_REDUCED 924byte (0x39C)
57 * \li PRINTF_NOMODIFIERS 416byte (0x1A0)
59 * Code/data size in words on DSP56K with CodeWarrior 6.0:
60 * \li PRINTF_FULL 1493/45
61 * \li PRINTF_NOFLOAT 795/45
62 * \li PRINTF_REDUCED 482/0
63 * \li PRINTF_NOMODIFIERS 301/0
65 * The reduced version of formatter is suitable when program size is critical
66 * rather than formatting power. This routine uses less than 20 bytes of
67 * stack space which makes it practical even in systems with less than 256
70 * The only formatting specifiers supported by the reduced formatter are:
72 * %% %c %s %d %o %x %X and %hd %ho %hx %hX %ld %lo %lx %lX
75 * It means that real variables are not supported as well as field
76 * width and precision arguments.
82 #include "cfg/cfg_formatwr.h" /* CONFIG_ macros */
83 #include <cfg/debug.h> /* ASSERT */
86 #include <mware/hex.h>
88 #ifndef CONFIG_PRINTF_N_FORMATTER
89 /** Disable the arcane %n formatter. */
90 #define CONFIG_PRINTF_N_FORMATTER 0
93 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
94 /** Disable the %o formatter. */
95 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
98 /* True if we must keep a count of the number of characters we print. */
99 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
103 #if CONFIG_PRINTF > PRINTF_NOFLOAT
106 /* Maximum precision for floating point values */
107 typedef long double max_float_t;
109 /*luca: FIXME: this should be enough to print floating point values, must be investigated
112 #define FRMWRI_BUFSIZE 20
115 * Conservative estimate. Should be (probably) 12 (which is the size necessary
116 * to represent (2^32-1) in octal plus the sign bit.
118 #define FRMWRI_BUFSIZE 16
121 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
122 #ifndef MEM_ATTRIBUTE
123 #define MEM_ATTRIBUTE
126 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
127 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
129 #define IS_SHORT (sizeof(int) == 2)
130 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
133 #if CONFIG_PRINTF > PRINTF_NOFLOAT
135 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
136 MEM_ATTRIBUTE short nr_of_digits,
137 MEM_ATTRIBUTE char *buf,
138 MEM_ATTRIBUTE char format_flag,
139 MEM_ATTRIBUTE char g_flag,
140 MEM_ATTRIBUTE bool alternate_flag)
142 MEM_ATTRIBUTE char *cp;
143 MEM_ATTRIBUTE char *buf_pointer;
144 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
151 while (value >= 1e11) /* To speed up things a bit */
154 integral_10_log += 10;
162 else if (value) /* Not just 0.0 */
164 while (value <= 1e-10) /* To speed up things a bit */
167 integral_10_log -= 10;
177 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
180 nr_of_digits -= integral_10_log;
184 /* %#G - No removal of trailing zeros */
187 /* %G - Removal of trailing zeros */
188 alternate_flag = true;
198 /* Less than one... */
199 if (integral_10_log < 0)
201 *buf_pointer++ = '0';
202 if ((n = nr_of_digits) || alternate_flag)
203 *buf_pointer++ = '.';
205 while (--i > integral_10_log && nr_of_digits)
207 *buf_pointer++ = '0';
210 if (integral_10_log < (-n - 1))
211 /* Nothing more to do */
217 dec_point_pos = - integral_10_log;
222 while (i <= nr_of_digits )
224 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
225 value *= 10; /* Prepare for next shot */
226 *buf_pointer++ = n + '0';
227 if ( ! i++ && (nr_of_digits || alternate_flag))
228 *buf_pointer++ = '.';
231 /* Rounding possible */
235 cp = buf_pointer - 1;
240 if ( (*cp += n) == ('9' + 1) )
248 } while (cp-- > buf);
257 if (*(cp - 1) == '.')
282 /* %G - Remove trailing zeros */
285 while (*(buf_pointer - 1) == '0')
287 if (*(buf_pointer - 1) == '.')
294 *buf_pointer++ = format_flag;
295 if (integral_10_log < 0)
297 *buf_pointer++ = '-';
298 integral_10_log = -integral_10_log;
301 *buf_pointer++ = '+';
307 *buf_pointer++ = (integral_10_log % 10) + '0';
308 integral_10_log /= 10;
309 } while ( integral_10_log || n < 2 );
310 for ( i = n ; n > 0 ; n-- )
311 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
314 return (buf_pointer);
317 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
320 * This routine forms the core and entry of the formatter.
322 * The conversion performed conforms to the ANSI specification for "printf".
325 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
326 void put_one_char(char, void *),
327 void *secret_pointer,
330 #if CONFIG_PRINTF > PRINTF_REDUCED
331 MEM_ATTRIBUTE static char bad_conversion[] = "???";
332 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
334 MEM_ATTRIBUTE int precision;
336 #if CONFIG_PRINTF_COUNT_CHARS
337 MEM_ATTRIBUTE int nr_of_chars;
339 MEM_ATTRIBUTE int field_width;
340 MEM_ATTRIBUTE char format_flag;
341 enum PLUS_SPACE_FLAGS {
342 PSF_NONE, PSF_PLUS, PSF_MINUS
346 #if CONFIG_PRINTF_OCTAL_FORMATTER
350 MEM_ATTRIBUTE struct {
351 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
352 #if CONFIG_PRINTF_OCTAL_FORMATTER
353 enum DIV_FACTOR div_factor : 2;
355 enum DIV_FACTOR div_factor : 1;
357 bool left_adjust : 1;
358 bool l_L_modifier : 1;
360 bool alternate_flag : 1;
361 bool nonzero_value : 1;
367 MEM_ATTRIBUTE unsigned long ulong;
369 #if CONFIG_PRINTF > PRINTF_NOFLOAT
370 MEM_ATTRIBUTE max_float_t fvalue;
373 MEM_ATTRIBUTE char *buf_pointer;
374 MEM_ATTRIBUTE char *ptr;
375 MEM_ATTRIBUTE const char *hex;
376 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
378 #if CONFIG_PRINTF_COUNT_CHARS
381 for (;;) /* Until full format string read */
383 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
386 #if CONFIG_PRINTF_RETURN_COUNT
387 return (nr_of_chars);
391 put_one_char(format_flag, secret_pointer);
392 #if CONFIG_PRINTF_COUNT_CHARS
396 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
399 put_one_char('%', secret_pointer);
400 #if CONFIG_PRINTF_COUNT_CHARS
406 flags.left_adjust = false;
407 flags.alternate_flag = false;
408 flags.plus_space_flag = PSF_NONE;
409 flags.zeropad = false;
411 flags.progmem = false;
413 ptr = buf_pointer = &buf[0];
416 /* check for leading '-', '+', ' ','#' or '0' flags */
419 switch (PGM_READ_CHAR(format))
422 if (flags.plus_space_flag)
425 flags.plus_space_flag = PSF_PLUS;
428 flags.left_adjust = true;
431 flags.alternate_flag = true;
434 flags.zeropad = true;
442 /* Optional field width (may be '*') */
443 if (PGM_READ_CHAR(format) == '*')
445 field_width = va_arg(ap, int);
448 field_width = -field_width;
449 flags.left_adjust = true;
456 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
457 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
460 if (flags.left_adjust)
461 flags.zeropad = false;
463 /* Optional precision (or '*') */
464 if (PGM_READ_CHAR(format) == '.')
466 if (PGM_READ_CHAR(++format) == '*')
468 precision = va_arg(ap, int);
474 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
475 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
481 /* At this point, "left_adjust" is nonzero if there was
482 * a sign, "zeropad" is 1 if there was a leading zero
483 * and 0 otherwise, "field_width" and "precision"
484 * contain numbers corresponding to the digit strings
485 * before and after the decimal point, respectively,
486 * and "plus_space_flag" is either 0 (no flag) or
487 * contains a plus or space character. If there was no
488 * decimal point, "precision" will be -1.
491 flags.l_L_modifier = false;
492 flags.h_modifier = false;
494 /* Optional 'l','L','z' or 'h' modifier? */
495 switch (PGM_READ_CHAR(format))
500 flags.l_L_modifier = true;
504 flags.h_modifier = true;
510 * At exit from the following switch, we will emit
511 * the characters starting at "buf_pointer" and
514 switch (format_flag = PGM_READ_CHAR(format++))
516 #if CONFIG_PRINTF_N_FORMATTER
518 if (sizeof(short) != sizeof(int))
520 if (sizeof(int) != sizeof(long))
523 *va_arg(ap, short *) = nr_of_chars;
524 else if (flags.l_L_modifier)
525 *va_arg(ap, long *) = nr_of_chars;
527 *va_arg(ap, int *) = nr_of_chars;
532 *va_arg(ap, short *) = nr_of_chars;
534 *va_arg(ap, int *) = nr_of_chars;
539 if (flags.l_L_modifier)
540 *va_arg(ap, long *) = nr_of_chars;
542 *va_arg(ap, int *) = nr_of_chars;
547 buf[0] = va_arg(ap, int);
551 /* Custom formatter for strings in program memory. */
554 flags.progmem = true;
559 if ( !(buf_pointer = va_arg(ap, char *)) )
560 buf_pointer = null_pointer;
565 * Move `ptr' to the last character of the
566 * string that will be actually printed.
572 for (n=0; pgm_read_char(ptr) && n < precision; n++)
577 for (n=0; *ptr && n < precision; n++)
581 #if CONFIG_PRINTF_OCTAL_FORMATTER
583 if (flags.alternate_flag && !precision)
591 if (format_flag == 'p')
592 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
593 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
594 #else /* 32bit pointers */
595 ulong = (unsigned long)va_arg(ap, char *);
596 #endif /* 32bit pointers */
597 else if (flags.l_L_modifier)
598 ulong = va_arg(ap, unsigned long);
599 else if (flags.h_modifier)
600 ulong = (unsigned long)(unsigned short)va_arg(ap, unsigned int);
602 ulong = va_arg(ap, unsigned int);
605 #if CONFIG_PRINTF_OCTAL_FORMATTER
606 (format_flag == 'o') ? DIV_OCT :
608 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
609 flags.plus_space_flag = PSF_NONE;
610 goto INTEGRAL_CONVERSION;
614 if (flags.l_L_modifier)
615 ulong = (unsigned long)(long)va_arg(ap, long);
617 ulong = (unsigned long)(long)va_arg(ap, int);
620 if ((signed long)ulong < 0)
622 flags.plus_space_flag = PSF_MINUS;
623 ulong = (unsigned long)(-((signed long)ulong));
626 flags.div_factor = DIV_DEC;
628 /* Now convert to digits */
630 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
631 flags.nonzero_value = (ulong != 0);
633 /* No char if zero and zero precision */
634 if (precision != 0 || flags.nonzero_value)
636 switch (flags.div_factor)
640 *--buf_pointer = hex[ulong % 10];
646 *--buf_pointer = hex[ulong % 16];
649 #if CONFIG_PRINTF_OCTAL_FORMATTER
652 *--buf_pointer = hex[ulong % 8];
659 /* "precision" takes precedence */
662 precision = field_width - (flags.plus_space_flag != PSF_NONE);
663 while (precision > (int)(ptr - buf_pointer))
664 *--buf_pointer = '0';
666 if (flags.alternate_flag && flags.nonzero_value)
668 if (format_flag == 'x' || format_flag == 'X')
670 *--buf_pointer = format_flag;
671 *--buf_pointer = '0';
673 #if CONFIG_PRINTF_OCTAL_FORMATTER
674 else if ((format_flag == 'o') && (*buf_pointer != '0'))
676 *--buf_pointer = '0';
680 ASSERT(buf_pointer >= buf);
683 #if CONFIG_PRINTF > PRINTF_NOFLOAT
692 goto FLOATING_CONVERSION;
704 if (sizeof(double) != sizeof(max_float_t))
706 fvalue = flags.l_L_modifier ?
707 va_arg(ap,max_float_t) : va_arg(ap,double);
710 fvalue = va_arg(ap,max_float_t);
714 flags.plus_space_flag = PSF_MINUS;
717 ptr = float_conversion (fvalue,
719 buf_pointer += field_width,
722 flags.alternate_flag);
725 precision = field_width - (flags.plus_space_flag != PSF_NONE);
726 while (precision > ptr - buf_pointer)
727 *--buf_pointer = '0';
731 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
733 case '\0': /* Really bad place to find NUL in */
737 /* Undefined conversion! */
738 ptr = buf_pointer = bad_conversion;
739 ptr += sizeof(bad_conversion) - 1;
745 * This part emittes the formatted string to "put_one_char".
748 /* If field_width == 0 then nothing should be written. */
749 precision = ptr - buf_pointer;
751 if ( precision > field_width)
757 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
760 /* emit any leading pad characters */
761 if (!flags.left_adjust)
764 put_one_char(' ', secret_pointer);
765 #if CONFIG_PRINTF_COUNT_CHARS
770 /* emit flag characters (if any) */
771 if (flags.plus_space_flag)
773 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
774 #if CONFIG_PRINTF_COUNT_CHARS
782 while (--precision >= 0)
784 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
785 #if CONFIG_PRINTF_COUNT_CHARS
791 #endif /* CPU_HARVARD */
793 /* emit the string itself */
794 while (--precision >= 0)
796 put_one_char(*buf_pointer++, secret_pointer);
797 #if CONFIG_PRINTF_COUNT_CHARS
803 /* emit trailing space characters */
804 if (flags.left_adjust)
807 put_one_char(' ', secret_pointer);
808 #if CONFIG_PRINTF_COUNT_CHARS
814 #else /* PRINTF_REDUCED starts here */
816 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
817 bool l_modifier, h_modifier;
818 unsigned long u_val, div_val;
820 unsigned int u_val, div_val;
821 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
824 unsigned int nr_of_chars, base;
829 for (;;) /* Until full format string read */
831 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
834 return (nr_of_chars);
835 put_one_char(format_flag, secret_pointer);
839 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
841 * Optional 'l', 'z' or 'h' modifiers?
843 l_modifier = h_modifier = false;
844 switch (PGM_READ_CHAR(format))
848 /* for the 'z' modifier, we make this assumption */
849 STATIC_ASSERT(sizeof(size_t) == sizeof(long));
859 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
861 switch (format_flag = PGM_READ_CHAR(format++))
864 format_flag = va_arg(ap, int);
866 put_one_char(format_flag, secret_pointer);
871 ptr = va_arg(ap, char *);
872 while ((format_flag = *ptr++))
874 put_one_char(format_flag, secret_pointer);
884 div_val = 0x40000000;
885 goto CONVERSION_LOOP;
892 div_val = 1000000000;
893 goto CONVERSION_LOOP;
901 div_val = 0x10000000;
904 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
907 if (format_flag == 'd')
908 u_val = (short)va_arg(ap, int);
910 u_val = (unsigned short)va_arg(ap, int);
913 u_val = va_arg(ap, long);
916 if (format_flag == 'd')
917 u_val = va_arg(ap, int);
919 u_val = va_arg(ap, unsigned int);
922 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
923 u_val = va_arg(ap,int);
924 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
925 if (format_flag == 'd')
927 if (((int)u_val) < 0)
930 put_one_char('-', secret_pointer);
934 while (div_val > 1 && div_val > u_val)
940 outChar = (u_val / div_val) + '0';
943 if (format_flag == 'x')
944 outChar += 'a'-'9'-1;
946 outChar += 'A'-'9'-1;
948 put_one_char(outChar, secret_pointer);
955 } /* end switch(format_flag...) */
957 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
960 #endif /* CONFIG_PRINTF */