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.16 2005/07/19 07:25:46 bernie
57 *#* Use appconfig.h instead of cfg/config.h.
59 *#* Revision 1.15 2005/04/11 19:10:28 bernie
60 *#* Include top-level headers from cfg/ subdir.
62 *#* Revision 1.14 2005/03/01 23:26:22 bernie
63 *#* Use shared hextab.
65 *#* Revision 1.13 2005/02/18 12:33:25 bernie
68 *#* Revision 1.12 2005/02/16 20:28:03 bernie
71 *#* Revision 1.11 2005/02/16 16:51:29 bernie
72 *#* Simplify float code.
74 *#* Revision 1.10 2004/10/26 09:01:35 bernie
77 *#* Revision 1.9 2004/09/14 21:06:23 bernie
80 *#* Revision 1.8 2004/08/25 14:12:09 rasky
81 *#* Aggiornato il comment block dei log RCS
85 #include <mware/pgm.h>
86 #include <mware/hex.h>
87 #include <cfg/debug.h> /* ASSERT */
88 #include <appconfig.h> /* CONFIG_ macros */
90 #ifndef CONFIG_PRINTF_N_FORMATTER
91 /*! Disable the arcane %n formatter. */
92 #define CONFIG_PRINTF_N_FORMATTER 0
95 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
96 /*! Disable the %o formatter. */
97 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
100 /* True if we must keep a count of the number of characters we print. */
101 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
105 #if CONFIG_PRINTF > PRINTF_NOFLOAT
108 /* Maximum precision for floating point values */
109 typedef long double max_float_t;
111 /*bernie: save some memory, who cares about floats with lots of decimals? */
112 #define FRMWRI_BUFSIZE 134
113 #warning 134 is too much, the code must be fixed to have a lower precision limit
116 * Conservative estimate. Should be (probably) 12 (which is the size necessary
117 * to represent (2^32-1) in octal plus the sign bit.
119 #define FRMWRI_BUFSIZE 16
122 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
123 #ifndef MEM_ATTRIBUTE
124 #define MEM_ATTRIBUTE
127 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
128 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
130 #define IS_SHORT (sizeof(int) == 2)
131 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
134 #if CONFIG_PRINTF > PRINTF_NOFLOAT
136 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
137 MEM_ATTRIBUTE short nr_of_digits,
138 MEM_ATTRIBUTE char *buf,
139 MEM_ATTRIBUTE char format_flag,
140 MEM_ATTRIBUTE char g_flag,
141 MEM_ATTRIBUTE bool alternate_flag)
143 MEM_ATTRIBUTE char *cp;
144 MEM_ATTRIBUTE char *buf_pointer;
145 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
152 while (value >= 1e11) /* To speed up things a bit */
155 integral_10_log += 10;
163 else if (value) /* Not just 0.0 */
165 while (value <= 1e-10) /* To speed up things a bit */
168 integral_10_log -= 10;
178 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
181 nr_of_digits -= integral_10_log;
185 /* %#G - No removal of trailing zeros */
188 /* %G - Removal of trailing zeros */
189 alternate_flag = true;
199 /* Less than one... */
200 if (integral_10_log < 0)
202 *buf_pointer++ = '0';
203 if ((n = nr_of_digits) || alternate_flag)
204 *buf_pointer++ = '.';
206 while (--i > integral_10_log && nr_of_digits)
208 *buf_pointer++ = '0';
211 if (integral_10_log < (-n - 1))
212 /* Nothing more to do */
218 dec_point_pos = - integral_10_log;
223 while (i <= nr_of_digits )
225 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
226 value *= 10; /* Prepare for next shot */
227 *buf_pointer++ = n + '0';
228 if ( ! i++ && (nr_of_digits || alternate_flag))
229 *buf_pointer++ = '.';
232 /* Rounding possible */
236 cp = buf_pointer - 1;
241 if ( (*cp += n) == ('9' + 1) )
249 } while (cp-- > buf);
258 if (*(cp - 1) == '.')
283 /* %G - Remove trailing zeros */
286 while (*(buf_pointer - 1) == '0')
288 if (*(buf_pointer - 1) == '.')
295 *buf_pointer++ = format_flag;
296 if (integral_10_log < 0)
298 *buf_pointer++ = '-';
299 integral_10_log = -integral_10_log;
302 *buf_pointer++ = '+';
308 *buf_pointer++ = (integral_10_log % 10) + '0';
309 integral_10_log /= 10;
310 } while ( integral_10_log || n < 2 );
311 for ( i = n ; n > 0 ; n-- )
312 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
315 return (buf_pointer);
318 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
321 * This routine forms the core and entry of the formatter.
323 * The conversion performed conforms to the ANSI specification for "printf".
326 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
327 void put_one_char(char, void *),
328 void *secret_pointer,
331 #if CONFIG_PRINTF > PRINTF_REDUCED
332 MEM_ATTRIBUTE static char bad_conversion[] = "???";
333 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
335 MEM_ATTRIBUTE int precision;
337 #if CONFIG_PRINTF_COUNT_CHARS
338 MEM_ATTRIBUTE int nr_of_chars;
340 MEM_ATTRIBUTE int field_width;
341 MEM_ATTRIBUTE char format_flag;
342 enum PLUS_SPACE_FLAGS {
343 PSF_NONE, PSF_PLUS, PSF_MINUS
347 #if CONFIG_PRINTF_OCTAL_FORMATTER
351 MEM_ATTRIBUTE struct {
352 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
353 #if CONFIG_PRINTF_OCTAL_FORMATTER
354 enum DIV_FACTOR div_factor : 2;
356 enum DIV_FACTOR div_factor : 1;
358 bool left_adjust : 1;
359 bool l_L_modifier : 1;
361 bool alternate_flag : 1;
362 bool nonzero_value : 1;
368 MEM_ATTRIBUTE unsigned long ulong;
370 #if CONFIG_PRINTF > PRINTF_NOFLOAT
371 MEM_ATTRIBUTE max_float_t fvalue;
374 MEM_ATTRIBUTE char *buf_pointer;
375 MEM_ATTRIBUTE char *ptr;
376 MEM_ATTRIBUTE const char *hex;
377 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
379 #if CONFIG_PRINTF_COUNT_CHARS
382 for (;;) /* Until full format string read */
384 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
387 #if CONFIG_PRINTF_RETURN_COUNT
388 return (nr_of_chars);
392 put_one_char(format_flag, secret_pointer);
393 #if CONFIG_PRINTF_COUNT_CHARS
397 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
400 put_one_char('%', secret_pointer);
401 #if CONFIG_PRINTF_COUNT_CHARS
407 flags.left_adjust = false;
408 flags.alternate_flag = false;
409 flags.plus_space_flag = PSF_NONE;
410 flags.zeropad = false;
412 flags.progmem = false;
414 ptr = buf_pointer = &buf[0];
417 /* check for leading '-', '+', ' ','#' or '0' flags */
420 switch (PGM_READ_CHAR(format))
423 if (flags.plus_space_flag)
426 flags.plus_space_flag = PSF_PLUS;
429 flags.left_adjust = true;
432 flags.alternate_flag = true;
435 flags.zeropad = true;
443 /* Optional field width (may be '*') */
444 if (PGM_READ_CHAR(format) == '*')
446 field_width = va_arg(ap, int);
449 field_width = -field_width;
450 flags.left_adjust = true;
457 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
458 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
461 if (flags.left_adjust)
462 flags.zeropad = false;
464 /* Optional precision (or '*') */
465 if (PGM_READ_CHAR(format) == '.')
467 if (PGM_READ_CHAR(++format) == '*')
469 precision = va_arg(ap, int);
475 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
476 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
482 /* At this point, "left_adjust" is nonzero if there was
483 * a sign, "zeropad" is 1 if there was a leading zero
484 * and 0 otherwise, "field_width" and "precision"
485 * contain numbers corresponding to the digit strings
486 * before and after the decimal point, respectively,
487 * and "plus_space_flag" is either 0 (no flag) or
488 * contains a plus or space character. If there was no
489 * decimal point, "precision" will be -1.
492 flags.l_L_modifier = false;
493 flags.h_modifier = false;
495 /* Optional 'l','L' r 'h' modifier? */
496 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 (sizeof(short) == sizeof(int))
598 ulong = flags.l_L_modifier ?
599 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
601 ulong = flags.h_modifier ?
602 (unsigned long)(unsigned short) va_arg(ap, int)
603 : (unsigned long)va_arg(ap, 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 (sizeof(short) == sizeof(long))
616 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
618 flags.plus_space_flag = PSF_MINUS;
619 ulong = (unsigned long)(-((signed long)ulong));
622 else if (sizeof(short) == sizeof(int))
624 if ( (long)(ulong = flags.l_L_modifier ?
625 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
627 flags.plus_space_flag = PSF_MINUS;
628 ulong = (unsigned long)(-((signed long)ulong));
633 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
634 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
636 flags.plus_space_flag = PSF_MINUS;
637 ulong = (unsigned long)(-((signed long)ulong));
640 flags.div_factor = DIV_DEC;
642 /* Now convert to digits */
644 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
645 flags.nonzero_value = (ulong != 0);
647 /* No char if zero and zero precision */
648 if (precision != 0 || flags.nonzero_value)
650 switch (flags.div_factor)
654 *--buf_pointer = hex[ulong % 10];
660 *--buf_pointer = hex[ulong % 16];
663 #if CONFIG_PRINTF_OCTAL_FORMATTER
666 *--buf_pointer = hex[ulong % 8];
673 /* "precision" takes precedence */
676 precision = field_width - (flags.plus_space_flag != PSF_NONE);
677 while (precision > (int)(ptr - buf_pointer))
678 *--buf_pointer = '0';
680 if (flags.alternate_flag && flags.nonzero_value)
682 if (format_flag == 'x' || format_flag == 'X')
684 *--buf_pointer = format_flag;
685 *--buf_pointer = '0';
687 #if CONFIG_PRINTF_OCTAL_FORMATTER
688 else if ((format_flag == 'o') && (*buf_pointer != '0'))
690 *--buf_pointer = '0';
694 ASSERT(buf_pointer >= buf);
697 #if CONFIG_PRINTF > PRINTF_NOFLOAT
706 goto FLOATING_CONVERSION;
718 if (sizeof(double) != sizeof(max_float_t))
720 fvalue = flags.l_L_modifier ?
721 va_arg(ap,max_float_t) : va_arg(ap,double);
724 fvalue = va_arg(ap,max_float_t);
728 flags.plus_space_flag = PSF_MINUS;
731 ptr = float_conversion (fvalue,
733 buf_pointer += field_width,
736 flags.alternate_flag);
739 precision = field_width - (flags.plus_space_flag != PSF_NONE);
740 while (precision > ptr - buf_pointer)
741 *--buf_pointer = '0';
745 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
747 case '\0': /* Really bad place to find NUL in */
751 /* Undefined conversion! */
752 ptr = buf_pointer = bad_conversion;
753 ptr += sizeof(bad_conversion) - 1;
759 * This part emittes the formatted string to "put_one_char".
762 /* If field_width == 0 then nothing should be written. */
763 precision = ptr - buf_pointer;
765 if ( precision > field_width)
771 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
774 /* emit any leading pad characters */
775 if (!flags.left_adjust)
778 put_one_char(' ', secret_pointer);
779 #if CONFIG_PRINTF_COUNT_CHARS
784 /* emit flag characters (if any) */
785 if (flags.plus_space_flag)
787 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
788 #if CONFIG_PRINTF_COUNT_CHARS
796 while (--precision >= 0)
798 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
799 #if CONFIG_PRINTF_COUNT_CHARS
805 #endif /* CPU_HARVARD */
807 /* emit the string itself */
808 while (--precision >= 0)
810 put_one_char(*buf_pointer++, secret_pointer);
811 #if CONFIG_PRINTF_COUNT_CHARS
817 /* emit trailing space characters */
818 if (flags.left_adjust)
821 put_one_char(' ', secret_pointer);
822 #if CONFIG_PRINTF_COUNT_CHARS
828 #else /* PRINTF_REDUCED starts here */
830 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
831 char l_modifier, h_modifier;
832 unsigned long u_val, div_val;
834 unsigned int u_val, div_val;
835 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
838 unsigned int nr_of_chars, base;
843 for (;;) /* Until full format string read */
845 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
848 return (nr_of_chars);
849 put_one_char(format_flag, secret_pointer);
853 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
854 /*=================================*/
855 /* Optional 'l' or 'h' modifiers ? */
856 /*=================================*/
857 l_modifier = h_modifier = 0;
858 switch (PGM_READ_CHAR(format))
870 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
872 switch (format_flag = PGM_READ_CHAR(format++))
875 format_flag = va_arg(ap, int);
877 put_one_char(format_flag, secret_pointer);
882 ptr = va_arg(ap, char *);
883 while ((format_flag = *ptr++))
885 put_one_char(format_flag, secret_pointer);
895 div_val = 0x40000000;
896 goto CONVERSION_LOOP;
903 div_val = 1000000000;
904 goto CONVERSION_LOOP;
912 div_val = 0x10000000;
915 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
917 u_val = (format_flag == 'd') ?
918 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
920 u_val = va_arg(ap, long);
922 u_val = (format_flag == 'd') ?
923 va_arg(ap,int) : va_arg(ap,unsigned int);
924 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
925 u_val = va_arg(ap,int);
926 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
927 if (format_flag == 'd')
929 if (((int)u_val) < 0)
932 put_one_char('-', secret_pointer);
936 while (div_val > 1 && div_val > u_val)
942 outChar = (u_val / div_val) + '0';
945 if (format_flag == 'x')
946 outChar += 'a'-'9'-1;
948 outChar += 'A'-'9'-1;
950 put_one_char(outChar, secret_pointer);
957 } /* end switch(format_flag...) */
959 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
962 #endif /* CONFIG_PRINTF */