4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README 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.11 2005/02/16 16:51:29 bernie
57 *#* Simplify float code.
59 *#* Revision 1.10 2004/10/26 09:01:35 bernie
62 *#* Revision 1.9 2004/09/14 21:06:23 bernie
65 *#* Revision 1.8 2004/08/25 14:12:09 rasky
66 *#* Aggiornato il comment block dei log RCS
68 *#* Revision 1.7 2004/08/04 15:53:47 rasky
69 *#* Nuove opzioni di configurazione per formatted_write e ridotto maggiormente l'utilizzo dellos tack
71 *#* Revision 1.6 2004/07/30 14:34:10 rasky
72 *#* Vari fix per documentazione e commenti
73 *#* Aggiunte PP_CATn e STATIC_ASSERT
75 *#* Revision 1.5 2004/07/29 22:57:09 bernie
76 *#* Switch to new-style config handling.
78 *#* Revision 1.4 2004/07/21 00:20:20 bernie
79 *#* Allow completely disabling printf()-like formatter.
81 *#* Revision 1.3 2004/07/18 22:00:15 bernie
82 *#* Reorganize configuration parameters to match DevLib's convention.
84 *#* Revision 1.2 2004/06/03 11:27:09 bernie
85 *#* Add dual-license information.
87 *#* Revision 1.1 2004/05/23 15:43:16 bernie
88 *#* Import mware modules.
93 #include <compiler.h> /* progmem macros */
94 #include <config.h> /* CONFIG_ macros */
95 #include <debug.h> /* ASSERT */
97 #ifndef CONFIG_PRINTF_N_FORMATTER
98 /*! Disable the arcane %n formatter. */
99 #define CONFIG_PRINTF_N_FORMATTER 0
102 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
103 /*! Disable the %o formatter. */
104 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
107 /* True if we must keep a count of the number of characters we print. */
108 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
112 #if CONFIG_PRINTF > PRINTF_NOFLOAT
114 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
116 #if CONFIG_PRINTF > PRINTF_NOFLOAT
117 /*bernie: save some memory, who cares about floats with lots of decimals? */
118 #define FRMWRI_BUFSIZE 134
119 #warning 134 is too much, the code must be fixed to have a lower precision limit
122 * Conservative estimate. Should be (probably) 12 (which is the size necessary
123 * to represent (2^32-1) in octal plus the sign bit.
125 #define FRMWRI_BUFSIZE 16
128 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
129 #ifndef MEM_ATTRIBUTE
130 #define MEM_ATTRIBUTE
133 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
134 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
136 #define IS_SHORT (sizeof(int) == 2)
137 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
140 #if CONFIG_PRINTF > PRINTF_NOFLOAT
142 static char *float_conversion(MEM_ATTRIBUTE long double value,
143 MEM_ATTRIBUTE short nr_of_digits,
144 MEM_ATTRIBUTE char *buf,
145 MEM_ATTRIBUTE char format_flag,
146 MEM_ATTRIBUTE char g_flag,
147 MEM_ATTRIBUTE bool alternate_flag)
149 MEM_ATTRIBUTE char *cp;
150 MEM_ATTRIBUTE char *buf_pointer;
151 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
158 while (value >= 1e11) /* To speed up things a bit */
161 integral_10_log += 10;
169 else if (value) /* Not just 0.0 */
171 while (value <= 1e-10) /* To speed up things a bit */
174 integral_10_log -= 10;
184 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
187 nr_of_digits -= integral_10_log;
191 /* %#G - No removal of trailing zeros */
194 /* %G - Removal of trailing zeros */
195 alternate_flag = true;
205 /* Less than one... */
206 if (integral_10_log < 0)
208 *buf_pointer++ = '0';
209 if ((n = nr_of_digits) || alternate_flag)
210 *buf_pointer++ = '.';
212 while (--i > integral_10_log && nr_of_digits)
214 *buf_pointer++ = '0';
217 if (integral_10_log < (-n - 1))
218 /* Nothing more to do */
224 dec_point_pos = - integral_10_log;
229 while (i <= nr_of_digits )
231 value -= (long double)(n = (short)value); /* n=Digit value=Remainder */
232 value *= 10; /* Prepare for next shot */
233 *buf_pointer++ = n + '0';
234 if ( ! i++ && (nr_of_digits || alternate_flag))
235 *buf_pointer++ = '.';
238 /* Rounding possible */
242 cp = buf_pointer - 1;
247 if ( (*cp += n) == ('9' + 1) )
255 } while (cp-- > buf);
264 if (*(cp - 1) == '.')
289 /* %G - Remove trailing zeros */
292 while (*(buf_pointer - 1) == '0')
294 if (*(buf_pointer - 1) == '.')
301 *buf_pointer++ = format_flag;
302 if (integral_10_log < 0)
304 *buf_pointer++ = '-';
305 integral_10_log = -integral_10_log;
308 *buf_pointer++ = '+';
314 *buf_pointer++ = (integral_10_log % 10) + '0';
315 integral_10_log /= 10;
316 } while ( integral_10_log || n < 2 );
317 for ( i = n ; n > 0 ; n-- )
318 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
321 return (buf_pointer);
324 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
327 * This routine forms the core and entry of the formatter.
329 * The conversion performed conforms to the ANSI specification for "printf".
332 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
333 void put_one_char(char, void *),
334 void *secret_pointer,
337 #if CONFIG_PRINTF > PRINTF_REDUCED
338 MEM_ATTRIBUTE static char bad_conversion[] = "???";
339 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
341 MEM_ATTRIBUTE int precision;
343 #if CONFIG_PRINTF_COUNT_CHARS
344 MEM_ATTRIBUTE int nr_of_chars;
346 MEM_ATTRIBUTE int field_width;
347 MEM_ATTRIBUTE char format_flag;
348 enum PLUS_SPACE_FLAGS {
349 PSF_NONE, PSF_PLUS, PSF_MINUS
353 #if CONFIG_PRINTF_OCTAL_FORMATTER
358 MEM_ATTRIBUTE enum PLUS_SPACE_FLAGS plus_space_flag : 2;
359 #if CONFIG_PRINTF_OCTAL_FORMATTER
360 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 2;
362 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 1;
364 MEM_ATTRIBUTE bool left_adjust : 1;
365 MEM_ATTRIBUTE bool l_L_modifier : 1;
366 MEM_ATTRIBUTE bool h_modifier : 1;
367 MEM_ATTRIBUTE bool alternate_flag : 1;
368 MEM_ATTRIBUTE bool nonzero_value : 1;
369 MEM_ATTRIBUTE bool zeropad : 1;
371 MEM_ATTRIBUTE unsigned long ulong;
373 #if CONFIG_PRINTF > PRINTF_NOFLOAT
374 MEM_ATTRIBUTE long double fvalue;
377 MEM_ATTRIBUTE char *buf_pointer;
378 MEM_ATTRIBUTE char *ptr;
379 MEM_ATTRIBUTE const char *hex;
380 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
382 #if CONFIG_PRINTF_COUNT_CHARS
385 for (;;) /* Until full format string read */
387 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
390 #if CONFIG_PRINTF_RETURN_COUNT
391 return (nr_of_chars);
395 put_one_char(format_flag, secret_pointer);
396 #if CONFIG_PRINTF_COUNT_CHARS
400 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
403 put_one_char('%', secret_pointer);
404 #if CONFIG_PRINTF_COUNT_CHARS
410 flags.left_adjust = false;
411 flags.alternate_flag = false;
412 flags.plus_space_flag = PSF_NONE;
413 flags.zeropad = false;
414 ptr = buf_pointer = &buf[0];
415 hex = "0123456789ABCDEF";
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);
552 if ( !(buf_pointer = va_arg(ap, char *)) )
553 buf_pointer = null_pointer;
556 for (n=0; *buf_pointer++ && n < precision; n++)
562 #if CONFIG_PRINTF_OCTAL_FORMATTER
564 if (flags.alternate_flag && !precision)
568 hex = "0123456789abcdef";
572 if (format_flag == 'p')
573 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
574 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
575 #else /* 32bit pointers */
576 ulong = (unsigned long)va_arg(ap, char *);
577 #endif /* 32bit pointers */
578 else if (sizeof(short) == sizeof(int))
579 ulong = flags.l_L_modifier ?
580 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
582 ulong = flags.h_modifier ?
583 (unsigned long)(unsigned short) va_arg(ap, int)
584 : (unsigned long)va_arg(ap, int);
586 #if CONFIG_PRINTF_OCTAL_FORMATTER
587 (format_flag == 'o') ? DIV_OCT :
589 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
590 flags.plus_space_flag = PSF_NONE;
591 goto INTEGRAL_CONVERSION;
595 if (sizeof(short) == sizeof(long))
597 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
599 flags.plus_space_flag = PSF_MINUS;
600 ulong = (unsigned long)(-((signed long)ulong));
603 else if (sizeof(short) == sizeof(int))
605 if ( (long)(ulong = flags.l_L_modifier ?
606 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
608 flags.plus_space_flag = PSF_MINUS;
609 ulong = (unsigned long)(-((signed long)ulong));
614 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
615 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
617 flags.plus_space_flag = PSF_MINUS;
618 ulong = (unsigned long)(-((signed long)ulong));
621 flags.div_factor = DIV_DEC;
623 /* Now convert to digits */
625 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
626 flags.nonzero_value = (ulong != 0);
628 /* No char if zero and zero precision */
629 if (precision != 0 || flags.nonzero_value)
631 switch (flags.div_factor)
635 *--buf_pointer = hex[ulong % 10];
641 *--buf_pointer = hex[ulong % 16];
644 #if CONFIG_PRINTF_OCTAL_FORMATTER
647 *--buf_pointer = hex[ulong % 8];
654 /* "precision" takes precedence */
657 precision = field_width - (flags.plus_space_flag != PSF_NONE);
658 while (precision > (int)(ptr - buf_pointer))
659 *--buf_pointer = '0';
661 if (flags.alternate_flag && flags.nonzero_value)
663 if (format_flag == 'x' || format_flag == 'X')
665 *--buf_pointer = format_flag;
666 *--buf_pointer = '0';
668 #if CONFIG_PRINTF_OCTAL_FORMATTER
669 else if ((format_flag == 'o') && (*buf_pointer != '0'))
671 *--buf_pointer = '0';
675 ASSERT(buf_pointer >= buf);
678 #if CONFIG_PRINTF > PRINTF_NOFLOAT
687 goto FLOATING_CONVERSION;
699 if (sizeof(double) != sizeof(long double))
701 fvalue = flags.l_L_modifier ?
702 va_arg(ap,long double) : va_arg(ap,double);
705 fvalue = va_arg(ap,long double);
709 flags.plus_space_flag = PSF_MINUS;
712 ptr = float_conversion (fvalue,
714 buf_pointer += field_width,
717 flags.alternate_flag);
720 precision = field_width - (flags.plus_space_flag != PSF_NONE);
721 while (precision > ptr - buf_pointer)
722 *--buf_pointer = '0';
726 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
728 case '\0': /* Really bad place to find NUL in */
732 /* Undefined conversion! */
733 ptr = buf_pointer = bad_conversion;
734 ptr += strlen(bad_conversion);
740 * This part emittes the formatted string to "put_one_char".
743 /* If field_width == 0 then nothing should be written. */
744 precision = ptr - buf_pointer;
746 if ( precision > field_width)
752 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
755 /* emit any leading pad characters */
756 if (!flags.left_adjust)
759 put_one_char(' ', secret_pointer);
760 #if CONFIG_PRINTF_COUNT_CHARS
765 /* emit flag characters (if any) */
766 if (flags.plus_space_flag)
768 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
769 #if CONFIG_PRINTF_COUNT_CHARS
774 /* emit the string itself */
775 while (--precision >= 0)
777 put_one_char(*buf_pointer++, secret_pointer);
778 #if CONFIG_PRINTF_COUNT_CHARS
783 /* emit trailing space characters */
784 if (flags.left_adjust)
787 put_one_char(' ', secret_pointer);
788 #if CONFIG_PRINTF_COUNT_CHARS
794 #else /* PRINTF_REDUCED starts here */
796 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
797 char l_modifier, h_modifier;
798 unsigned long u_val, div_val;
800 unsigned int u_val, div_val;
801 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
804 unsigned int nr_of_chars, base;
809 for (;;) /* Until full format string read */
811 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
814 return (nr_of_chars);
815 put_one_char(format_flag, secret_pointer);
819 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
820 /*=================================*/
821 /* Optional 'l' or 'h' modifiers ? */
822 /*=================================*/
823 l_modifier = h_modifier = 0;
824 switch (PGM_READ_CHAR(format))
836 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
838 switch (format_flag = PGM_READ_CHAR(format++))
841 format_flag = va_arg(ap, int);
843 put_one_char(format_flag, secret_pointer);
848 ptr = va_arg(ap, char *);
849 while ((format_flag = *ptr++))
851 put_one_char(format_flag, secret_pointer);
861 div_val = 0x40000000;
862 goto CONVERSION_LOOP;
869 div_val = 1000000000;
870 goto CONVERSION_LOOP;
878 div_val = 0x10000000;
881 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
883 u_val = (format_flag == 'd') ?
884 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
886 u_val = va_arg(ap, long);
888 u_val = (format_flag == 'd') ?
889 va_arg(ap,int) : va_arg(ap,unsigned int);
890 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
891 u_val = va_arg(ap,int);
892 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
893 if (format_flag == 'd')
895 if (((int)u_val) < 0)
898 put_one_char('-', secret_pointer);
902 while (div_val > 1 && div_val > u_val)
908 outChar = (u_val / div_val) + '0';
911 if (format_flag == 'x')
912 outChar += 'a'-'9'-1;
914 outChar += 'A'-'9'-1;
916 put_one_char(outChar, secret_pointer);
923 } /* end switch(format_flag...) */
925 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
928 #endif /* CONFIG_PRINTF */