4 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
10 * \brief Basic "printf", "sprintf" and "fprintf" formatter.
12 * This module is 100% reentrant and can be adapted to user-defined routines
13 * that needs formatters with special properties like different output
14 * channels or new format specifiers.
16 * To reduce size in applications not using real numbers or long integers
17 * the formatter may be compiled to exclude certain parts. This is
18 * controlled by giving a -D option a compilation time:
21 * -D CONFIG_PRINTF=PRINTF_FULL Full ANSI printf formatter
22 * -D CONFIG_PRINTF=PRINTF_NOFLOAT Exclude support for floats
23 * -D CONFIG_PRINTF=PRINTF_REDUCED Simplified formatter (see below)
24 * -D CONFIG_PRINTF=PRINTF_NOMODIFIERS Exclude 'l' and 'h' modifiers in reduced version
25 * -D CONFIG_PRINTF=PRINTF_DISABLED No formatter at all
28 * Code size on AVR4 with GCC 3.4.1 (-O2):
29 * PRINTF_FULL 2912byte (0xB60)
30 * PRINTF_NOFLOAT 1684byte (0x694)
31 * PRINTF_REDUCED 924byte (0x39C)
32 * PRINTF_NOMODIFIERS 416byte (0x1A0)
34 * Code/data size in words on DSP56K with CodeWarrior 6.0:
36 * PRINTF_NOFLOAT 795/45
37 * PRINTF_REDUCED 482/0
38 * PRINTF_NOMODIFIERS 301/0
40 * The reduced version of formatter is suitable when program size is critical
41 * rather than formatting power. This routine uses less than 20 bytes of
42 * stack space which makes it practical even in systems with less than 256
45 * The only formatting specifiers supported by the reduced formatter are:
47 * %% %c %s %d %o %x %X and %hd %ho %hx %hX %ld %lo %lx %lX
50 * It means that real variables are not supported as well as field
51 * width and precision arguments.
56 *#* Revision 1.13 2005/02/18 12:33:25 bernie
59 *#* Revision 1.12 2005/02/16 20:28:03 bernie
62 *#* Revision 1.11 2005/02/16 16:51:29 bernie
63 *#* Simplify float code.
65 *#* Revision 1.10 2004/10/26 09:01:35 bernie
68 *#* Revision 1.9 2004/09/14 21:06:23 bernie
71 *#* Revision 1.8 2004/08/25 14:12:09 rasky
72 *#* Aggiornato il comment block dei log RCS
77 //#include <compiler.h> /* progmem macros */
78 #include <config.h> /* CONFIG_ macros */
79 #include <debug.h> /* ASSERT */
81 #ifndef CONFIG_PRINTF_N_FORMATTER
82 /*! Disable the arcane %n formatter. */
83 #define CONFIG_PRINTF_N_FORMATTER 0
86 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
87 /*! Disable the %o formatter. */
88 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
91 /* True if we must keep a count of the number of characters we print. */
92 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
96 #if CONFIG_PRINTF > PRINTF_NOFLOAT
99 /* Maximum precision for floating point values */
100 typedef long double max_float_t;
102 /*bernie: save some memory, who cares about floats with lots of decimals? */
103 #define FRMWRI_BUFSIZE 134
104 #warning 134 is too much, the code must be fixed to have a lower precision limit
107 * Conservative estimate. Should be (probably) 12 (which is the size necessary
108 * to represent (2^32-1) in octal plus the sign bit.
110 #define FRMWRI_BUFSIZE 16
113 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
114 #ifndef MEM_ATTRIBUTE
115 #define MEM_ATTRIBUTE
118 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
119 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
121 #define IS_SHORT (sizeof(int) == 2)
122 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
125 #if CONFIG_PRINTF > PRINTF_NOFLOAT
127 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
128 MEM_ATTRIBUTE short nr_of_digits,
129 MEM_ATTRIBUTE char *buf,
130 MEM_ATTRIBUTE char format_flag,
131 MEM_ATTRIBUTE char g_flag,
132 MEM_ATTRIBUTE bool alternate_flag)
134 MEM_ATTRIBUTE char *cp;
135 MEM_ATTRIBUTE char *buf_pointer;
136 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
143 while (value >= 1e11) /* To speed up things a bit */
146 integral_10_log += 10;
154 else if (value) /* Not just 0.0 */
156 while (value <= 1e-10) /* To speed up things a bit */
159 integral_10_log -= 10;
169 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
172 nr_of_digits -= integral_10_log;
176 /* %#G - No removal of trailing zeros */
179 /* %G - Removal of trailing zeros */
180 alternate_flag = true;
190 /* Less than one... */
191 if (integral_10_log < 0)
193 *buf_pointer++ = '0';
194 if ((n = nr_of_digits) || alternate_flag)
195 *buf_pointer++ = '.';
197 while (--i > integral_10_log && nr_of_digits)
199 *buf_pointer++ = '0';
202 if (integral_10_log < (-n - 1))
203 /* Nothing more to do */
209 dec_point_pos = - integral_10_log;
214 while (i <= nr_of_digits )
216 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
217 value *= 10; /* Prepare for next shot */
218 *buf_pointer++ = n + '0';
219 if ( ! i++ && (nr_of_digits || alternate_flag))
220 *buf_pointer++ = '.';
223 /* Rounding possible */
227 cp = buf_pointer - 1;
232 if ( (*cp += n) == ('9' + 1) )
240 } while (cp-- > buf);
249 if (*(cp - 1) == '.')
274 /* %G - Remove trailing zeros */
277 while (*(buf_pointer - 1) == '0')
279 if (*(buf_pointer - 1) == '.')
286 *buf_pointer++ = format_flag;
287 if (integral_10_log < 0)
289 *buf_pointer++ = '-';
290 integral_10_log = -integral_10_log;
293 *buf_pointer++ = '+';
299 *buf_pointer++ = (integral_10_log % 10) + '0';
300 integral_10_log /= 10;
301 } while ( integral_10_log || n < 2 );
302 for ( i = n ; n > 0 ; n-- )
303 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
306 return (buf_pointer);
309 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
312 * This routine forms the core and entry of the formatter.
314 * The conversion performed conforms to the ANSI specification for "printf".
317 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
318 void put_one_char(char, void *),
319 void *secret_pointer,
322 #if CONFIG_PRINTF > PRINTF_REDUCED
323 MEM_ATTRIBUTE static char bad_conversion[] = "???";
324 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
326 MEM_ATTRIBUTE int precision;
328 #if CONFIG_PRINTF_COUNT_CHARS
329 MEM_ATTRIBUTE int nr_of_chars;
331 MEM_ATTRIBUTE int field_width;
332 MEM_ATTRIBUTE char format_flag;
333 enum PLUS_SPACE_FLAGS {
334 PSF_NONE, PSF_PLUS, PSF_MINUS
338 #if CONFIG_PRINTF_OCTAL_FORMATTER
342 MEM_ATTRIBUTE struct {
343 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
344 #if CONFIG_PRINTF_OCTAL_FORMATTER
345 enum DIV_FACTOR div_factor : 2;
347 enum DIV_FACTOR div_factor : 1;
349 bool left_adjust : 1;
350 bool l_L_modifier : 1;
352 bool alternate_flag : 1;
353 bool nonzero_value : 1;
359 MEM_ATTRIBUTE unsigned long ulong;
361 #if CONFIG_PRINTF > PRINTF_NOFLOAT
362 MEM_ATTRIBUTE max_float_t fvalue;
365 MEM_ATTRIBUTE char *buf_pointer;
366 MEM_ATTRIBUTE char *ptr;
367 MEM_ATTRIBUTE const char *hex;
368 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
370 #if CONFIG_PRINTF_COUNT_CHARS
373 for (;;) /* Until full format string read */
375 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
378 #if CONFIG_PRINTF_RETURN_COUNT
379 return (nr_of_chars);
383 put_one_char(format_flag, secret_pointer);
384 #if CONFIG_PRINTF_COUNT_CHARS
388 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
391 put_one_char('%', secret_pointer);
392 #if CONFIG_PRINTF_COUNT_CHARS
398 flags.left_adjust = false;
399 flags.alternate_flag = false;
400 flags.plus_space_flag = PSF_NONE;
401 flags.zeropad = false;
403 flags.progmem = false;
405 ptr = buf_pointer = &buf[0];
406 hex = "0123456789ABCDEF";
408 /* check for leading '-', '+', ' ','#' or '0' flags */
411 switch (PGM_READ_CHAR(format))
414 if (flags.plus_space_flag)
417 flags.plus_space_flag = PSF_PLUS;
420 flags.left_adjust = true;
423 flags.alternate_flag = true;
426 flags.zeropad = true;
434 /* Optional field width (may be '*') */
435 if (PGM_READ_CHAR(format) == '*')
437 field_width = va_arg(ap, int);
440 field_width = -field_width;
441 flags.left_adjust = true;
448 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
449 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
452 if (flags.left_adjust)
453 flags.zeropad = false;
455 /* Optional precision (or '*') */
456 if (PGM_READ_CHAR(format) == '.')
458 if (PGM_READ_CHAR(++format) == '*')
460 precision = va_arg(ap, int);
466 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
467 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
473 /* At this point, "left_adjust" is nonzero if there was
474 * a sign, "zeropad" is 1 if there was a leading zero
475 * and 0 otherwise, "field_width" and "precision"
476 * contain numbers corresponding to the digit strings
477 * before and after the decimal point, respectively,
478 * and "plus_space_flag" is either 0 (no flag) or
479 * contains a plus or space character. If there was no
480 * decimal point, "precision" will be -1.
483 flags.l_L_modifier = false;
484 flags.h_modifier = false;
486 /* Optional 'l','L' r 'h' modifier? */
487 switch (PGM_READ_CHAR(format))
491 flags.l_L_modifier = true;
495 flags.h_modifier = true;
501 * At exit from the following switch, we will emit
502 * the characters starting at "buf_pointer" and
505 switch (format_flag = PGM_READ_CHAR(format++))
507 #if CONFIG_PRINTF_N_FORMATTER
509 if (sizeof(short) != sizeof(int))
511 if (sizeof(int) != sizeof(long))
514 *va_arg(ap, short *) = nr_of_chars;
515 else if (flags.l_L_modifier)
516 *va_arg(ap, long *) = nr_of_chars;
518 *va_arg(ap, int *) = nr_of_chars;
523 *va_arg(ap, short *) = nr_of_chars;
525 *va_arg(ap, int *) = nr_of_chars;
530 if (flags.l_L_modifier)
531 *va_arg(ap, long *) = nr_of_chars;
533 *va_arg(ap, int *) = nr_of_chars;
538 buf[0] = va_arg(ap, int);
542 /* Custom formatter for strings in program memory. */
545 flags.progmem = true;
550 if ( !(buf_pointer = va_arg(ap, char *)) )
551 buf_pointer = null_pointer;
556 * Move `ptr' to the last character of the
557 * string that will be actually printed.
563 for (n=0; pgm_read_char(ptr) && n < precision; n++)
568 for (n=0; *ptr && n < precision; n++)
572 #if CONFIG_PRINTF_OCTAL_FORMATTER
574 if (flags.alternate_flag && !precision)
578 hex = "0123456789abcdef";
582 if (format_flag == 'p')
583 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
584 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
585 #else /* 32bit pointers */
586 ulong = (unsigned long)va_arg(ap, char *);
587 #endif /* 32bit pointers */
588 else if (sizeof(short) == sizeof(int))
589 ulong = flags.l_L_modifier ?
590 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
592 ulong = flags.h_modifier ?
593 (unsigned long)(unsigned short) va_arg(ap, int)
594 : (unsigned long)va_arg(ap, int);
596 #if CONFIG_PRINTF_OCTAL_FORMATTER
597 (format_flag == 'o') ? DIV_OCT :
599 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
600 flags.plus_space_flag = PSF_NONE;
601 goto INTEGRAL_CONVERSION;
605 if (sizeof(short) == sizeof(long))
607 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
609 flags.plus_space_flag = PSF_MINUS;
610 ulong = (unsigned long)(-((signed long)ulong));
613 else if (sizeof(short) == sizeof(int))
615 if ( (long)(ulong = flags.l_L_modifier ?
616 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
618 flags.plus_space_flag = PSF_MINUS;
619 ulong = (unsigned long)(-((signed long)ulong));
624 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
625 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
627 flags.plus_space_flag = PSF_MINUS;
628 ulong = (unsigned long)(-((signed long)ulong));
631 flags.div_factor = DIV_DEC;
633 /* Now convert to digits */
635 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
636 flags.nonzero_value = (ulong != 0);
638 /* No char if zero and zero precision */
639 if (precision != 0 || flags.nonzero_value)
641 switch (flags.div_factor)
645 *--buf_pointer = hex[ulong % 10];
651 *--buf_pointer = hex[ulong % 16];
654 #if CONFIG_PRINTF_OCTAL_FORMATTER
657 *--buf_pointer = hex[ulong % 8];
664 /* "precision" takes precedence */
667 precision = field_width - (flags.plus_space_flag != PSF_NONE);
668 while (precision > (int)(ptr - buf_pointer))
669 *--buf_pointer = '0';
671 if (flags.alternate_flag && flags.nonzero_value)
673 if (format_flag == 'x' || format_flag == 'X')
675 *--buf_pointer = format_flag;
676 *--buf_pointer = '0';
678 #if CONFIG_PRINTF_OCTAL_FORMATTER
679 else if ((format_flag == 'o') && (*buf_pointer != '0'))
681 *--buf_pointer = '0';
685 ASSERT(buf_pointer >= buf);
688 #if CONFIG_PRINTF > PRINTF_NOFLOAT
697 goto FLOATING_CONVERSION;
709 if (sizeof(double) != sizeof(max_float_t))
711 fvalue = flags.l_L_modifier ?
712 va_arg(ap,max_float_t) : va_arg(ap,double);
715 fvalue = va_arg(ap,max_float_t);
719 flags.plus_space_flag = PSF_MINUS;
722 ptr = float_conversion (fvalue,
724 buf_pointer += field_width,
727 flags.alternate_flag);
730 precision = field_width - (flags.plus_space_flag != PSF_NONE);
731 while (precision > ptr - buf_pointer)
732 *--buf_pointer = '0';
736 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
738 case '\0': /* Really bad place to find NUL in */
742 /* Undefined conversion! */
743 ptr = buf_pointer = bad_conversion;
744 ptr += sizeof(bad_conversion) - 1;
750 * This part emittes the formatted string to "put_one_char".
753 /* If field_width == 0 then nothing should be written. */
754 precision = ptr - buf_pointer;
756 if ( precision > field_width)
762 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
765 /* emit any leading pad characters */
766 if (!flags.left_adjust)
769 put_one_char(' ', secret_pointer);
770 #if CONFIG_PRINTF_COUNT_CHARS
775 /* emit flag characters (if any) */
776 if (flags.plus_space_flag)
778 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
779 #if CONFIG_PRINTF_COUNT_CHARS
787 while (--precision >= 0)
789 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
790 #if CONFIG_PRINTF_COUNT_CHARS
796 #endif /* CPU_HARVARD */
798 /* emit the string itself */
799 while (--precision >= 0)
801 put_one_char(*buf_pointer++, secret_pointer);
802 #if CONFIG_PRINTF_COUNT_CHARS
808 /* emit trailing space characters */
809 if (flags.left_adjust)
812 put_one_char(' ', secret_pointer);
813 #if CONFIG_PRINTF_COUNT_CHARS
819 #else /* PRINTF_REDUCED starts here */
821 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
822 char l_modifier, h_modifier;
823 unsigned long u_val, div_val;
825 unsigned int u_val, div_val;
826 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
829 unsigned int nr_of_chars, base;
834 for (;;) /* Until full format string read */
836 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
839 return (nr_of_chars);
840 put_one_char(format_flag, secret_pointer);
844 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
845 /*=================================*/
846 /* Optional 'l' or 'h' modifiers ? */
847 /*=================================*/
848 l_modifier = h_modifier = 0;
849 switch (PGM_READ_CHAR(format))
861 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
863 switch (format_flag = PGM_READ_CHAR(format++))
866 format_flag = va_arg(ap, int);
868 put_one_char(format_flag, secret_pointer);
873 ptr = va_arg(ap, char *);
874 while ((format_flag = *ptr++))
876 put_one_char(format_flag, secret_pointer);
886 div_val = 0x40000000;
887 goto CONVERSION_LOOP;
894 div_val = 1000000000;
895 goto CONVERSION_LOOP;
903 div_val = 0x10000000;
906 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
908 u_val = (format_flag == 'd') ?
909 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
911 u_val = va_arg(ap, long);
913 u_val = (format_flag == 'd') ?
914 va_arg(ap,int) : va_arg(ap,unsigned int);
915 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
916 u_val = va_arg(ap,int);
917 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
918 if (format_flag == 'd')
920 if (((int)u_val) < 0)
923 put_one_char('-', secret_pointer);
927 while (div_val > 1 && div_val > u_val)
933 outChar = (u_val / div_val) + '0';
936 if (format_flag == 'x')
937 outChar += 'a'-'9'-1;
939 outChar += 'A'-'9'-1;
941 put_one_char(outChar, secret_pointer);
948 } /* end switch(format_flag...) */
950 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
953 #endif /* CONFIG_PRINTF */