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.8 2004/08/25 14:12:09 rasky
57 *#* Aggiornato il comment block dei log RCS
59 *#* Revision 1.7 2004/08/04 15:53:47 rasky
60 *#* Nuove opzioni di configurazione per formatted_write e ridotto maggiormente l'utilizzo dellos tack
62 *#* Revision 1.6 2004/07/30 14:34:10 rasky
63 *#* Vari fix per documentazione e commenti
64 *#* Aggiunte PP_CATn e STATIC_ASSERT
66 *#* Revision 1.5 2004/07/29 22:57:09 bernie
67 *#* Switch to new-style config handling.
69 *#* Revision 1.4 2004/07/21 00:20:20 bernie
70 *#* Allow completely disabling printf()-like formatter.
72 *#* Revision 1.3 2004/07/18 22:00:15 bernie
73 *#* Reorganize configuration parameters to match DevLib's convention.
75 *#* Revision 1.2 2004/06/03 11:27:09 bernie
76 *#* Add dual-license information.
78 *#* Revision 1.1 2004/05/23 15:43:16 bernie
79 *#* Import mware modules.
84 #include <compiler.h> /* progmem macros */
85 #include <config.h> /* CONFIG_ macros */
86 #include <drv/kdebug.h> /* ASSERT */
88 #ifndef CONFIG_PRINTF_N_FORMATTER
89 /*! Enable arcane %n formatter */
90 #define CONFIG_PRINTF_N_FORMATTER 0
93 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
94 /*! Enable %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
105 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
107 #if CONFIG_PRINTF > PRINTF_NOFLOAT
108 /*bernie: save some memory, who cares about floats with lots of decimals? */
109 /*#define FRMWRI_BUFSIZE 134*/
110 #error 134 is too much, the code must be fixed to have a lower precision limit
113 * Conservative estimate. Should be (probably) 12 (which is the size necessary
114 * to represent (2^32-1) in octal plus the sign bit
116 #define FRMWRI_BUFSIZE 16
119 #ifndef MEM_ATTRIBUTE
120 #define MEM_ATTRIBUTE
123 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
124 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
126 #define IS_SHORT (sizeof(int) == 2)
127 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
130 #if CONFIG_PRINTF > PRINTF_NOFLOAT
132 static char *float_conversion(MEM_ATTRIBUTE long double value,
133 MEM_ATTRIBUTE short nr_of_digits,
134 MEM_ATTRIBUTE char *buf,
135 MEM_ATTRIBUTE char format_flag,
136 MEM_ATTRIBUTE char g_flag,
137 MEM_ATTRIBUTE bool alternate_flag)
139 MEM_ATTRIBUTE char *cp;
140 MEM_ATTRIBUTE char *buf_pointer;
141 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
148 while (value >= 1e11) /* To speed up things a bit */
151 integral_10_log += 10;
159 else if (value) /* Not just 0.0 */
161 while (value <= 1e-10) /* To speed up things a bit */
164 integral_10_log -= 10;
174 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
177 nr_of_digits -= integral_10_log;
181 /* %#G - No removal of trailing zeros */
184 /* %G - Removal of trailing zeros */
185 alternate_flag = true;
195 /* Less than one... */
196 if (integral_10_log < 0)
198 *buf_pointer++ = '0';
199 if ((n = nr_of_digits) || alternate_flag)
200 *buf_pointer++ = '.';
202 while (--i > integral_10_log && nr_of_digits)
204 *buf_pointer++ = '0';
207 if (integral_10_log < (-n - 1))
208 /* Nothing more to do */
214 dec_point_pos = - integral_10_log;
219 while (i <= nr_of_digits )
221 value -= (long double)(n = (short)value); /* n=Digit value=Remainder */
222 value *= 10; /* Prepare for next shot */
223 *buf_pointer++ = n + '0';
224 if ( ! i++ && (nr_of_digits || alternate_flag))
225 *buf_pointer++ = '.';
228 /* Rounding possible */
232 cp = buf_pointer - 1;
237 if ( (*cp += n) == ('9' + 1) )
245 } while (cp-- > buf);
254 if (*(cp - 1) == '.')
279 /* %G - Remove trailing zeros */
282 while (*(buf_pointer - 1) == '0')
284 if (*(buf_pointer - 1) == '.')
291 *buf_pointer++ = format_flag;
292 if (integral_10_log < 0)
294 *buf_pointer++ = '-';
295 integral_10_log = -integral_10_log;
298 *buf_pointer++ = '+';
304 *buf_pointer++ = (integral_10_log % 10) + '0';
305 integral_10_log /= 10;
306 } while ( integral_10_log || n < 2 );
307 for ( i = n ; n > 0 ; n-- )
308 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
311 return (buf_pointer);
314 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
317 * This routine forms the core and entry of the formatter.
319 * The conversion performed conforms to the ANSI specification for "printf".
322 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
323 void put_one_char(char, void *),
324 void *secret_pointer,
327 #if CONFIG_PRINTF > PRINTF_REDUCED
328 MEM_ATTRIBUTE static char bad_conversion[] = "???";
329 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
331 MEM_ATTRIBUTE int precision;
333 #if CONFIG_PRINTF_COUNT_CHARS
334 MEM_ATTRIBUTE int nr_of_chars;
336 MEM_ATTRIBUTE int field_width;
337 MEM_ATTRIBUTE char format_flag;
338 enum PLUS_SPACE_FLAGS {
339 PSF_NONE, PSF_PLUS, PSF_MINUS
343 #if CONFIG_PRINTF_OCTAL_FORMATTER
348 MEM_ATTRIBUTE enum PLUS_SPACE_FLAGS plus_space_flag : 2;
349 #if CONFIG_PRINTF_OCTAL_FORMATTER
350 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 2;
352 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 1;
354 MEM_ATTRIBUTE bool left_adjust : 1;
355 MEM_ATTRIBUTE bool l_L_modifier : 1;
356 MEM_ATTRIBUTE bool h_modifier : 1;
357 MEM_ATTRIBUTE bool alternate_flag : 1;
358 MEM_ATTRIBUTE bool nonzero_value : 1;
359 MEM_ATTRIBUTE bool zeropad : 1;
361 MEM_ATTRIBUTE unsigned long ulong;
363 #if CONFIG_PRINTF > PRINTF_NOFLOAT
364 MEM_ATTRIBUTE long double fvalue;
367 MEM_ATTRIBUTE char *buf_pointer;
368 MEM_ATTRIBUTE char *ptr;
369 MEM_ATTRIBUTE const char *hex;
370 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
372 #if CONFIG_PRINTF_COUNT_CHARS
375 for (;;) /* Until full format string read */
377 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
380 #if CONFIG_PRINTF_RETURN_COUNT
381 return (nr_of_chars);
385 put_one_char(format_flag, secret_pointer);
386 #if CONFIG_PRINTF_COUNT_CHARS
390 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
393 put_one_char('%', secret_pointer);
394 #if CONFIG_PRINTF_COUNT_CHARS
400 flags.left_adjust = false;
401 flags.alternate_flag = false;
402 flags.plus_space_flag = PSF_NONE;
403 flags.zeropad = false;
404 ptr = buf_pointer = &buf[0];
405 hex = "0123456789ABCDEF";
407 /* check for leading '-', '+', ' ','#' or '0' flags */
410 switch (PGM_READ_CHAR(format))
413 if (flags.plus_space_flag)
416 flags.plus_space_flag = PSF_PLUS;
419 flags.left_adjust = true;
422 flags.alternate_flag = true;
425 flags.zeropad = true;
433 /* Optional field width (may be '*') */
434 if (PGM_READ_CHAR(format) == '*')
436 field_width = va_arg(ap, int);
439 field_width = -field_width;
440 flags.left_adjust = true;
447 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
448 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
451 if (flags.left_adjust)
452 flags.zeropad = false;
454 /* Optional precision (or '*') */
455 if (PGM_READ_CHAR(format) == '.')
457 if (PGM_READ_CHAR(++format) == '*')
459 precision = va_arg(ap, int);
465 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
466 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
472 /* At this point, "left_adjust" is nonzero if there was
473 * a sign, "zeropad" is 1 if there was a leading zero
474 * and 0 otherwise, "field_width" and "precision"
475 * contain numbers corresponding to the digit strings
476 * before and after the decimal point, respectively,
477 * and "plus_space_flag" is either 0 (no flag) or
478 * contains a plus or space character. If there was no
479 * decimal point, "precision" will be -1.
482 flags.l_L_modifier = false;
483 flags.h_modifier = false;
485 /* Optional 'l','L' r 'h' modifier? */
486 switch (PGM_READ_CHAR(format))
490 flags.l_L_modifier = true;
494 flags.h_modifier = true;
500 * At exit from the following switch, we will emit
501 * the characters starting at "buf_pointer" and
504 switch (format_flag = PGM_READ_CHAR(format++))
506 #if CONFIG_PRINTF_N_FORMATTER
508 if (sizeof(short) != sizeof(int))
510 if (sizeof(int) != sizeof(long))
513 *va_arg(ap, short *) = nr_of_chars;
514 else if (flags.l_L_modifier)
515 *va_arg(ap, long *) = nr_of_chars;
517 *va_arg(ap, int *) = nr_of_chars;
522 *va_arg(ap, short *) = nr_of_chars;
524 *va_arg(ap, int *) = nr_of_chars;
529 if (flags.l_L_modifier)
530 *va_arg(ap, long *) = nr_of_chars;
532 *va_arg(ap, int *) = nr_of_chars;
537 buf[0] = va_arg(ap, int);
542 if ( !(buf_pointer = va_arg(ap, char *)) )
543 buf_pointer = null_pointer;
546 for (n=0; *buf_pointer++ && n < precision; n++)
552 #if CONFIG_PRINTF_OCTAL_FORMATTER
554 if (flags.alternate_flag && !precision)
558 hex = "0123456789abcdef";
562 if (format_flag == 'p')
563 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
564 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
565 #else /* 32bit pointers */
566 ulong = (unsigned long)va_arg(ap, char *);
567 #endif /* 32bit pointers */
568 else if (sizeof(short) == sizeof(int))
569 ulong = flags.l_L_modifier ?
570 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
572 ulong = flags.h_modifier ?
573 (unsigned long)(unsigned short) va_arg(ap, int)
574 : (unsigned long)va_arg(ap, int);
576 #if CONFIG_PRINTF_OCTAL_FORMATTER
577 (format_flag == 'o') ? DIV_OCT :
579 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
580 flags.plus_space_flag = PSF_NONE;
581 goto INTEGRAL_CONVERSION;
585 if (sizeof(short) == sizeof(long))
587 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
589 flags.plus_space_flag = PSF_MINUS;
590 ulong = (unsigned long)(-((signed long)ulong));
593 else if (sizeof(short) == sizeof(int))
595 if ( (long)(ulong = flags.l_L_modifier ?
596 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
598 flags.plus_space_flag = PSF_MINUS;
599 ulong = (unsigned long)(-((signed long)ulong));
604 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
605 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
607 flags.plus_space_flag = PSF_MINUS;
608 ulong = (unsigned long)(-((signed long)ulong));
611 flags.div_factor = DIV_DEC;
613 /* Now convert to digits */
615 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
616 flags.nonzero_value = (ulong != 0);
618 /* No char if zero and zero precision */
619 if (precision != 0 || flags.nonzero_value)
621 switch (flags.div_factor)
625 *--buf_pointer = hex[ulong % 10];
631 *--buf_pointer = hex[ulong % 16];
634 #if CONFIG_PRINTF_OCTAL_FORMATTER
637 *--buf_pointer = hex[ulong % 8];
644 /* "precision" takes precedence */
647 precision = field_width - (flags.plus_space_flag != PSF_NONE);
648 while (precision > (int)(ptr - buf_pointer))
649 *--buf_pointer = '0';
651 if (flags.alternate_flag && flags.nonzero_value)
653 if (format_flag == 'x' || format_flag == 'X')
655 *--buf_pointer = format_flag;
656 *--buf_pointer = '0';
658 #if CONFIG_PRINTF_OCTAL_FORMATTER
659 else if ((format_flag == 'o') && (*buf_pointer != '0'))
661 *--buf_pointer = '0';
665 ASSERT(buf_pointer >= buf);
668 #if CONFIG_PRINTF > PRINTF_NOFLOAT
677 goto FLOATING_CONVERSION;
688 if (sizeof(double) != sizeof(long double))
690 if ( (fvalue = flags.l_L_modifier ?
691 va_arg(ap,long double) : va_arg(ap,double)) < 0)
693 flags.plus_space_flag = PSF_MINUS;
697 else if ( (fvalue = va_arg(ap,long double)) < 0)
699 flags.plus_space_flag = PSF_MINUS;
702 ptr = float_conversion (fvalue,
704 buf_pointer += field_width,
707 flags.alternate_flag);
710 precision = field_width - (flags.plus_space_flag != PSF_NONE);
711 while (precision > ptr - buf_pointer)
712 *--buf_pointer = '0';
716 #else /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
722 ptr = buf_pointer = bad_conversion;
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;
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 */