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.12 2005/02/16 20:28:03 bernie
59 *#* Revision 1.11 2005/02/16 16:51:29 bernie
60 *#* Simplify float code.
62 *#* Revision 1.10 2004/10/26 09:01:35 bernie
65 *#* Revision 1.9 2004/09/14 21:06:23 bernie
68 *#* Revision 1.8 2004/08/25 14:12:09 rasky
69 *#* Aggiornato il comment block dei log RCS
74 //#include <compiler.h> /* progmem macros */
75 #include <config.h> /* CONFIG_ macros */
76 #include <debug.h> /* ASSERT */
78 #ifndef CONFIG_PRINTF_N_FORMATTER
79 /*! Disable the arcane %n formatter. */
80 #define CONFIG_PRINTF_N_FORMATTER 0
83 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
84 /*! Disable the %o formatter. */
85 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
88 /* True if we must keep a count of the number of characters we print. */
89 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
93 #if CONFIG_PRINTF > PRINTF_NOFLOAT
96 /* Maximum precision for floating point values */
97 typedef long double max_float_t;
99 /*bernie: save some memory, who cares about floats with lots of decimals? */
100 #define FRMWRI_BUFSIZE 134
101 #warning 134 is too much, the code must be fixed to have a lower precision limit
104 * Conservative estimate. Should be (probably) 12 (which is the size necessary
105 * to represent (2^32-1) in octal plus the sign bit.
107 #define FRMWRI_BUFSIZE 16
110 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
111 #ifndef MEM_ATTRIBUTE
112 #define MEM_ATTRIBUTE
115 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
116 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
118 #define IS_SHORT (sizeof(int) == 2)
119 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
122 #if CONFIG_PRINTF > PRINTF_NOFLOAT
124 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
125 MEM_ATTRIBUTE short nr_of_digits,
126 MEM_ATTRIBUTE char *buf,
127 MEM_ATTRIBUTE char format_flag,
128 MEM_ATTRIBUTE char g_flag,
129 MEM_ATTRIBUTE bool alternate_flag)
131 MEM_ATTRIBUTE char *cp;
132 MEM_ATTRIBUTE char *buf_pointer;
133 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
140 while (value >= 1e11) /* To speed up things a bit */
143 integral_10_log += 10;
151 else if (value) /* Not just 0.0 */
153 while (value <= 1e-10) /* To speed up things a bit */
156 integral_10_log -= 10;
166 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
169 nr_of_digits -= integral_10_log;
173 /* %#G - No removal of trailing zeros */
176 /* %G - Removal of trailing zeros */
177 alternate_flag = true;
187 /* Less than one... */
188 if (integral_10_log < 0)
190 *buf_pointer++ = '0';
191 if ((n = nr_of_digits) || alternate_flag)
192 *buf_pointer++ = '.';
194 while (--i > integral_10_log && nr_of_digits)
196 *buf_pointer++ = '0';
199 if (integral_10_log < (-n - 1))
200 /* Nothing more to do */
206 dec_point_pos = - integral_10_log;
211 while (i <= nr_of_digits )
213 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
214 value *= 10; /* Prepare for next shot */
215 *buf_pointer++ = n + '0';
216 if ( ! i++ && (nr_of_digits || alternate_flag))
217 *buf_pointer++ = '.';
220 /* Rounding possible */
224 cp = buf_pointer - 1;
229 if ( (*cp += n) == ('9' + 1) )
237 } while (cp-- > buf);
246 if (*(cp - 1) == '.')
271 /* %G - Remove trailing zeros */
274 while (*(buf_pointer - 1) == '0')
276 if (*(buf_pointer - 1) == '.')
283 *buf_pointer++ = format_flag;
284 if (integral_10_log < 0)
286 *buf_pointer++ = '-';
287 integral_10_log = -integral_10_log;
290 *buf_pointer++ = '+';
296 *buf_pointer++ = (integral_10_log % 10) + '0';
297 integral_10_log /= 10;
298 } while ( integral_10_log || n < 2 );
299 for ( i = n ; n > 0 ; n-- )
300 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
303 return (buf_pointer);
306 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
309 * This routine forms the core and entry of the formatter.
311 * The conversion performed conforms to the ANSI specification for "printf".
314 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
315 void put_one_char(char, void *),
316 void *secret_pointer,
319 #if CONFIG_PRINTF > PRINTF_REDUCED
320 MEM_ATTRIBUTE static char bad_conversion[] = "???";
321 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
323 MEM_ATTRIBUTE int precision;
325 #if CONFIG_PRINTF_COUNT_CHARS
326 MEM_ATTRIBUTE int nr_of_chars;
328 MEM_ATTRIBUTE int field_width;
329 MEM_ATTRIBUTE char format_flag;
330 enum PLUS_SPACE_FLAGS {
331 PSF_NONE, PSF_PLUS, PSF_MINUS
335 #if CONFIG_PRINTF_OCTAL_FORMATTER
339 MEM_ATTRIBUTE struct {
340 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
341 #if CONFIG_PRINTF_OCTAL_FORMATTER
342 enum DIV_FACTOR div_factor : 2;
344 enum DIV_FACTOR div_factor : 1;
346 bool left_adjust : 1;
347 bool l_L_modifier : 1;
349 bool alternate_flag : 1;
350 bool nonzero_value : 1;
356 MEM_ATTRIBUTE unsigned long ulong;
358 #if CONFIG_PRINTF > PRINTF_NOFLOAT
359 MEM_ATTRIBUTE max_float_t fvalue;
362 MEM_ATTRIBUTE char *buf_pointer;
363 MEM_ATTRIBUTE char *ptr;
364 MEM_ATTRIBUTE const char *hex;
365 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
367 #if CONFIG_PRINTF_COUNT_CHARS
370 for (;;) /* Until full format string read */
372 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
375 #if CONFIG_PRINTF_RETURN_COUNT
376 return (nr_of_chars);
380 put_one_char(format_flag, secret_pointer);
381 #if CONFIG_PRINTF_COUNT_CHARS
385 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
388 put_one_char('%', secret_pointer);
389 #if CONFIG_PRINTF_COUNT_CHARS
395 flags.left_adjust = false;
396 flags.alternate_flag = false;
397 flags.plus_space_flag = PSF_NONE;
398 flags.zeropad = false;
400 flags.progmem = false;
402 ptr = buf_pointer = &buf[0];
403 hex = "0123456789ABCDEF";
405 /* check for leading '-', '+', ' ','#' or '0' flags */
408 switch (PGM_READ_CHAR(format))
411 if (flags.plus_space_flag)
414 flags.plus_space_flag = PSF_PLUS;
417 flags.left_adjust = true;
420 flags.alternate_flag = true;
423 flags.zeropad = true;
431 /* Optional field width (may be '*') */
432 if (PGM_READ_CHAR(format) == '*')
434 field_width = va_arg(ap, int);
437 field_width = -field_width;
438 flags.left_adjust = true;
445 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
446 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
449 if (flags.left_adjust)
450 flags.zeropad = false;
452 /* Optional precision (or '*') */
453 if (PGM_READ_CHAR(format) == '.')
455 if (PGM_READ_CHAR(++format) == '*')
457 precision = va_arg(ap, int);
463 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
464 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
470 /* At this point, "left_adjust" is nonzero if there was
471 * a sign, "zeropad" is 1 if there was a leading zero
472 * and 0 otherwise, "field_width" and "precision"
473 * contain numbers corresponding to the digit strings
474 * before and after the decimal point, respectively,
475 * and "plus_space_flag" is either 0 (no flag) or
476 * contains a plus or space character. If there was no
477 * decimal point, "precision" will be -1.
480 flags.l_L_modifier = false;
481 flags.h_modifier = false;
483 /* Optional 'l','L' r 'h' modifier? */
484 switch (PGM_READ_CHAR(format))
488 flags.l_L_modifier = true;
492 flags.h_modifier = true;
498 * At exit from the following switch, we will emit
499 * the characters starting at "buf_pointer" and
502 switch (format_flag = PGM_READ_CHAR(format++))
504 #if CONFIG_PRINTF_N_FORMATTER
506 if (sizeof(short) != sizeof(int))
508 if (sizeof(int) != sizeof(long))
511 *va_arg(ap, short *) = nr_of_chars;
512 else if (flags.l_L_modifier)
513 *va_arg(ap, long *) = nr_of_chars;
515 *va_arg(ap, int *) = nr_of_chars;
520 *va_arg(ap, short *) = nr_of_chars;
522 *va_arg(ap, int *) = nr_of_chars;
527 if (flags.l_L_modifier)
528 *va_arg(ap, long *) = nr_of_chars;
530 *va_arg(ap, int *) = nr_of_chars;
535 buf[0] = va_arg(ap, int);
539 /* Custom formatter for strings in program memory. */
542 flags.progmem = true;
547 if ( !(buf_pointer = va_arg(ap, char *)) )
548 buf_pointer = null_pointer;
553 * Move `ptr' to the last character of the
554 * string that will be actually printed.
560 for (n=0; pgm_read_char(ptr) && n < precision; n++)
565 for (n=0; *ptr && n < precision; n++)
569 #if CONFIG_PRINTF_OCTAL_FORMATTER
571 if (flags.alternate_flag && !precision)
575 hex = "0123456789abcdef";
579 if (format_flag == 'p')
580 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
581 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
582 #else /* 32bit pointers */
583 ulong = (unsigned long)va_arg(ap, char *);
584 #endif /* 32bit pointers */
585 else if (sizeof(short) == sizeof(int))
586 ulong = flags.l_L_modifier ?
587 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
589 ulong = flags.h_modifier ?
590 (unsigned long)(unsigned short) va_arg(ap, int)
591 : (unsigned long)va_arg(ap, int);
593 #if CONFIG_PRINTF_OCTAL_FORMATTER
594 (format_flag == 'o') ? DIV_OCT :
596 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
597 flags.plus_space_flag = PSF_NONE;
598 goto INTEGRAL_CONVERSION;
602 if (sizeof(short) == sizeof(long))
604 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
606 flags.plus_space_flag = PSF_MINUS;
607 ulong = (unsigned long)(-((signed long)ulong));
610 else if (sizeof(short) == sizeof(int))
612 if ( (long)(ulong = flags.l_L_modifier ?
613 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
615 flags.plus_space_flag = PSF_MINUS;
616 ulong = (unsigned long)(-((signed long)ulong));
621 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
622 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
624 flags.plus_space_flag = PSF_MINUS;
625 ulong = (unsigned long)(-((signed long)ulong));
628 flags.div_factor = DIV_DEC;
630 /* Now convert to digits */
632 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
633 flags.nonzero_value = (ulong != 0);
635 /* No char if zero and zero precision */
636 if (precision != 0 || flags.nonzero_value)
638 switch (flags.div_factor)
642 *--buf_pointer = hex[ulong % 10];
648 *--buf_pointer = hex[ulong % 16];
651 #if CONFIG_PRINTF_OCTAL_FORMATTER
654 *--buf_pointer = hex[ulong % 8];
661 /* "precision" takes precedence */
664 precision = field_width - (flags.plus_space_flag != PSF_NONE);
665 while (precision > (int)(ptr - buf_pointer))
666 *--buf_pointer = '0';
668 if (flags.alternate_flag && flags.nonzero_value)
670 if (format_flag == 'x' || format_flag == 'X')
672 *--buf_pointer = format_flag;
673 *--buf_pointer = '0';
675 #if CONFIG_PRINTF_OCTAL_FORMATTER
676 else if ((format_flag == 'o') && (*buf_pointer != '0'))
678 *--buf_pointer = '0';
682 ASSERT(buf_pointer >= buf);
685 #if CONFIG_PRINTF > PRINTF_NOFLOAT
694 goto FLOATING_CONVERSION;
706 if (sizeof(double) != sizeof(max_float_t))
708 fvalue = flags.l_L_modifier ?
709 va_arg(ap,max_float_t) : va_arg(ap,double);
712 fvalue = va_arg(ap,max_float_t);
716 flags.plus_space_flag = PSF_MINUS;
719 ptr = float_conversion (fvalue,
721 buf_pointer += field_width,
724 flags.alternate_flag);
727 precision = field_width - (flags.plus_space_flag != PSF_NONE);
728 while (precision > ptr - buf_pointer)
729 *--buf_pointer = '0';
733 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
735 case '\0': /* Really bad place to find NUL in */
739 /* Undefined conversion! */
740 ptr = buf_pointer = bad_conversion;
741 ptr += strlen(bad_conversion);
747 * This part emittes the formatted string to "put_one_char".
750 /* If field_width == 0 then nothing should be written. */
751 precision = ptr - buf_pointer;
753 if ( precision > field_width)
759 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
762 /* emit any leading pad characters */
763 if (!flags.left_adjust)
766 put_one_char(' ', secret_pointer);
767 #if CONFIG_PRINTF_COUNT_CHARS
772 /* emit flag characters (if any) */
773 if (flags.plus_space_flag)
775 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
776 #if CONFIG_PRINTF_COUNT_CHARS
784 while (--precision >= 0)
786 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
787 #if CONFIG_PRINTF_COUNT_CHARS
793 #endif /* CPU_HARVARD */
795 /* emit the string itself */
796 while (--precision >= 0)
798 put_one_char(*buf_pointer++, secret_pointer);
799 #if CONFIG_PRINTF_COUNT_CHARS
805 /* emit trailing space characters */
806 if (flags.left_adjust)
809 put_one_char(' ', secret_pointer);
810 #if CONFIG_PRINTF_COUNT_CHARS
816 #else /* PRINTF_REDUCED starts here */
818 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
819 char l_modifier, h_modifier;
820 unsigned long u_val, div_val;
822 unsigned int u_val, div_val;
823 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
826 unsigned int nr_of_chars, base;
831 for (;;) /* Until full format string read */
833 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
836 return (nr_of_chars);
837 put_one_char(format_flag, secret_pointer);
841 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
842 /*=================================*/
843 /* Optional 'l' or 'h' modifiers ? */
844 /*=================================*/
845 l_modifier = h_modifier = 0;
846 switch (PGM_READ_CHAR(format))
858 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
860 switch (format_flag = PGM_READ_CHAR(format++))
863 format_flag = va_arg(ap, int);
865 put_one_char(format_flag, secret_pointer);
870 ptr = va_arg(ap, char *);
871 while ((format_flag = *ptr++))
873 put_one_char(format_flag, secret_pointer);
883 div_val = 0x40000000;
884 goto CONVERSION_LOOP;
891 div_val = 1000000000;
892 goto CONVERSION_LOOP;
900 div_val = 0x10000000;
903 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
905 u_val = (format_flag == 'd') ?
906 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
908 u_val = va_arg(ap, long);
910 u_val = (format_flag == 'd') ?
911 va_arg(ap,int) : va_arg(ap,unsigned int);
912 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
913 u_val = va_arg(ap,int);
914 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
915 if (format_flag == 'd')
917 if (((int)u_val) < 0)
920 put_one_char('-', secret_pointer);
924 while (div_val > 1 && div_val > u_val)
930 outChar = (u_val / div_val) + '0';
933 if (format_flag == 'x')
934 outChar += 'a'-'9'-1;
936 outChar += 'A'-'9'-1;
938 put_one_char(outChar, secret_pointer);
945 } /* end switch(format_flag...) */
947 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
950 #endif /* CONFIG_PRINTF */