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.14 2005/03/01 23:26:22 bernie
57 *#* Use shared hextab.
59 *#* Revision 1.13 2005/02/18 12:33:25 bernie
62 *#* Revision 1.12 2005/02/16 20:28:03 bernie
65 *#* Revision 1.11 2005/02/16 16:51:29 bernie
66 *#* Simplify float code.
68 *#* Revision 1.10 2004/10/26 09:01:35 bernie
71 *#* Revision 1.9 2004/09/14 21:06:23 bernie
74 *#* Revision 1.8 2004/08/25 14:12:09 rasky
75 *#* Aggiornato il comment block dei log RCS
79 #include <mware/pgm.h>
80 #include <mware/hex.h>
81 #include <config.h> /* CONFIG_ macros */
82 #include <debug.h> /* ASSERT */
84 #ifndef CONFIG_PRINTF_N_FORMATTER
85 /*! Disable the arcane %n formatter. */
86 #define CONFIG_PRINTF_N_FORMATTER 0
89 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
90 /*! Disable the %o formatter. */
91 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
94 /* True if we must keep a count of the number of characters we print. */
95 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
99 #if CONFIG_PRINTF > PRINTF_NOFLOAT
102 /* Maximum precision for floating point values */
103 typedef long double max_float_t;
105 /*bernie: save some memory, who cares about floats with lots of decimals? */
106 #define FRMWRI_BUFSIZE 134
107 #warning 134 is too much, the code must be fixed to have a lower precision limit
110 * Conservative estimate. Should be (probably) 12 (which is the size necessary
111 * to represent (2^32-1) in octal plus the sign bit.
113 #define FRMWRI_BUFSIZE 16
116 /* Probably useful for fancy microcontrollers such as the PIC, nobody knows. */
117 #ifndef MEM_ATTRIBUTE
118 #define MEM_ATTRIBUTE
121 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
122 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
124 #define IS_SHORT (sizeof(int) == 2)
125 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
128 #if CONFIG_PRINTF > PRINTF_NOFLOAT
130 static char *float_conversion(MEM_ATTRIBUTE max_float_t value,
131 MEM_ATTRIBUTE short nr_of_digits,
132 MEM_ATTRIBUTE char *buf,
133 MEM_ATTRIBUTE char format_flag,
134 MEM_ATTRIBUTE char g_flag,
135 MEM_ATTRIBUTE bool alternate_flag)
137 MEM_ATTRIBUTE char *cp;
138 MEM_ATTRIBUTE char *buf_pointer;
139 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
146 while (value >= 1e11) /* To speed up things a bit */
149 integral_10_log += 10;
157 else if (value) /* Not just 0.0 */
159 while (value <= 1e-10) /* To speed up things a bit */
162 integral_10_log -= 10;
172 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
175 nr_of_digits -= integral_10_log;
179 /* %#G - No removal of trailing zeros */
182 /* %G - Removal of trailing zeros */
183 alternate_flag = true;
193 /* Less than one... */
194 if (integral_10_log < 0)
196 *buf_pointer++ = '0';
197 if ((n = nr_of_digits) || alternate_flag)
198 *buf_pointer++ = '.';
200 while (--i > integral_10_log && nr_of_digits)
202 *buf_pointer++ = '0';
205 if (integral_10_log < (-n - 1))
206 /* Nothing more to do */
212 dec_point_pos = - integral_10_log;
217 while (i <= nr_of_digits )
219 value -= (max_float_t)(n = (short)value); /* n=Digit value=Remainder */
220 value *= 10; /* Prepare for next shot */
221 *buf_pointer++ = n + '0';
222 if ( ! i++ && (nr_of_digits || alternate_flag))
223 *buf_pointer++ = '.';
226 /* Rounding possible */
230 cp = buf_pointer - 1;
235 if ( (*cp += n) == ('9' + 1) )
243 } while (cp-- > buf);
252 if (*(cp - 1) == '.')
277 /* %G - Remove trailing zeros */
280 while (*(buf_pointer - 1) == '0')
282 if (*(buf_pointer - 1) == '.')
289 *buf_pointer++ = format_flag;
290 if (integral_10_log < 0)
292 *buf_pointer++ = '-';
293 integral_10_log = -integral_10_log;
296 *buf_pointer++ = '+';
302 *buf_pointer++ = (integral_10_log % 10) + '0';
303 integral_10_log /= 10;
304 } while ( integral_10_log || n < 2 );
305 for ( i = n ; n > 0 ; n-- )
306 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
309 return (buf_pointer);
312 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
315 * This routine forms the core and entry of the formatter.
317 * The conversion performed conforms to the ANSI specification for "printf".
320 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
321 void put_one_char(char, void *),
322 void *secret_pointer,
325 #if CONFIG_PRINTF > PRINTF_REDUCED
326 MEM_ATTRIBUTE static char bad_conversion[] = "???";
327 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
329 MEM_ATTRIBUTE int precision;
331 #if CONFIG_PRINTF_COUNT_CHARS
332 MEM_ATTRIBUTE int nr_of_chars;
334 MEM_ATTRIBUTE int field_width;
335 MEM_ATTRIBUTE char format_flag;
336 enum PLUS_SPACE_FLAGS {
337 PSF_NONE, PSF_PLUS, PSF_MINUS
341 #if CONFIG_PRINTF_OCTAL_FORMATTER
345 MEM_ATTRIBUTE struct {
346 enum PLUS_SPACE_FLAGS plus_space_flag : 2;
347 #if CONFIG_PRINTF_OCTAL_FORMATTER
348 enum DIV_FACTOR div_factor : 2;
350 enum DIV_FACTOR div_factor : 1;
352 bool left_adjust : 1;
353 bool l_L_modifier : 1;
355 bool alternate_flag : 1;
356 bool nonzero_value : 1;
362 MEM_ATTRIBUTE unsigned long ulong;
364 #if CONFIG_PRINTF > PRINTF_NOFLOAT
365 MEM_ATTRIBUTE max_float_t fvalue;
368 MEM_ATTRIBUTE char *buf_pointer;
369 MEM_ATTRIBUTE char *ptr;
370 MEM_ATTRIBUTE const char *hex;
371 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
373 #if CONFIG_PRINTF_COUNT_CHARS
376 for (;;) /* Until full format string read */
378 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
381 #if CONFIG_PRINTF_RETURN_COUNT
382 return (nr_of_chars);
386 put_one_char(format_flag, secret_pointer);
387 #if CONFIG_PRINTF_COUNT_CHARS
391 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
394 put_one_char('%', secret_pointer);
395 #if CONFIG_PRINTF_COUNT_CHARS
401 flags.left_adjust = false;
402 flags.alternate_flag = false;
403 flags.plus_space_flag = PSF_NONE;
404 flags.zeropad = false;
406 flags.progmem = false;
408 ptr = buf_pointer = &buf[0];
411 /* check for leading '-', '+', ' ','#' or '0' flags */
414 switch (PGM_READ_CHAR(format))
417 if (flags.plus_space_flag)
420 flags.plus_space_flag = PSF_PLUS;
423 flags.left_adjust = true;
426 flags.alternate_flag = true;
429 flags.zeropad = true;
437 /* Optional field width (may be '*') */
438 if (PGM_READ_CHAR(format) == '*')
440 field_width = va_arg(ap, int);
443 field_width = -field_width;
444 flags.left_adjust = true;
451 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
452 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
455 if (flags.left_adjust)
456 flags.zeropad = false;
458 /* Optional precision (or '*') */
459 if (PGM_READ_CHAR(format) == '.')
461 if (PGM_READ_CHAR(++format) == '*')
463 precision = va_arg(ap, int);
469 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
470 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
476 /* At this point, "left_adjust" is nonzero if there was
477 * a sign, "zeropad" is 1 if there was a leading zero
478 * and 0 otherwise, "field_width" and "precision"
479 * contain numbers corresponding to the digit strings
480 * before and after the decimal point, respectively,
481 * and "plus_space_flag" is either 0 (no flag) or
482 * contains a plus or space character. If there was no
483 * decimal point, "precision" will be -1.
486 flags.l_L_modifier = false;
487 flags.h_modifier = false;
489 /* Optional 'l','L' r 'h' modifier? */
490 switch (PGM_READ_CHAR(format))
494 flags.l_L_modifier = true;
498 flags.h_modifier = true;
504 * At exit from the following switch, we will emit
505 * the characters starting at "buf_pointer" and
508 switch (format_flag = PGM_READ_CHAR(format++))
510 #if CONFIG_PRINTF_N_FORMATTER
512 if (sizeof(short) != sizeof(int))
514 if (sizeof(int) != sizeof(long))
517 *va_arg(ap, short *) = nr_of_chars;
518 else if (flags.l_L_modifier)
519 *va_arg(ap, long *) = nr_of_chars;
521 *va_arg(ap, int *) = nr_of_chars;
526 *va_arg(ap, short *) = nr_of_chars;
528 *va_arg(ap, int *) = nr_of_chars;
533 if (flags.l_L_modifier)
534 *va_arg(ap, long *) = nr_of_chars;
536 *va_arg(ap, int *) = nr_of_chars;
541 buf[0] = va_arg(ap, int);
545 /* Custom formatter for strings in program memory. */
548 flags.progmem = true;
553 if ( !(buf_pointer = va_arg(ap, char *)) )
554 buf_pointer = null_pointer;
559 * Move `ptr' to the last character of the
560 * string that will be actually printed.
566 for (n=0; pgm_read_char(ptr) && n < precision; n++)
571 for (n=0; *ptr && n < precision; n++)
575 #if CONFIG_PRINTF_OCTAL_FORMATTER
577 if (flags.alternate_flag && !precision)
585 if (format_flag == 'p')
586 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
587 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
588 #else /* 32bit pointers */
589 ulong = (unsigned long)va_arg(ap, char *);
590 #endif /* 32bit pointers */
591 else if (sizeof(short) == sizeof(int))
592 ulong = flags.l_L_modifier ?
593 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
595 ulong = flags.h_modifier ?
596 (unsigned long)(unsigned short) va_arg(ap, int)
597 : (unsigned long)va_arg(ap, int);
599 #if CONFIG_PRINTF_OCTAL_FORMATTER
600 (format_flag == 'o') ? DIV_OCT :
602 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
603 flags.plus_space_flag = PSF_NONE;
604 goto INTEGRAL_CONVERSION;
608 if (sizeof(short) == sizeof(long))
610 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
612 flags.plus_space_flag = PSF_MINUS;
613 ulong = (unsigned long)(-((signed long)ulong));
616 else if (sizeof(short) == sizeof(int))
618 if ( (long)(ulong = flags.l_L_modifier ?
619 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
621 flags.plus_space_flag = PSF_MINUS;
622 ulong = (unsigned long)(-((signed long)ulong));
627 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
628 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
630 flags.plus_space_flag = PSF_MINUS;
631 ulong = (unsigned long)(-((signed long)ulong));
634 flags.div_factor = DIV_DEC;
636 /* Now convert to digits */
638 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
639 flags.nonzero_value = (ulong != 0);
641 /* No char if zero and zero precision */
642 if (precision != 0 || flags.nonzero_value)
644 switch (flags.div_factor)
648 *--buf_pointer = hex[ulong % 10];
654 *--buf_pointer = hex[ulong % 16];
657 #if CONFIG_PRINTF_OCTAL_FORMATTER
660 *--buf_pointer = hex[ulong % 8];
667 /* "precision" takes precedence */
670 precision = field_width - (flags.plus_space_flag != PSF_NONE);
671 while (precision > (int)(ptr - buf_pointer))
672 *--buf_pointer = '0';
674 if (flags.alternate_flag && flags.nonzero_value)
676 if (format_flag == 'x' || format_flag == 'X')
678 *--buf_pointer = format_flag;
679 *--buf_pointer = '0';
681 #if CONFIG_PRINTF_OCTAL_FORMATTER
682 else if ((format_flag == 'o') && (*buf_pointer != '0'))
684 *--buf_pointer = '0';
688 ASSERT(buf_pointer >= buf);
691 #if CONFIG_PRINTF > PRINTF_NOFLOAT
700 goto FLOATING_CONVERSION;
712 if (sizeof(double) != sizeof(max_float_t))
714 fvalue = flags.l_L_modifier ?
715 va_arg(ap,max_float_t) : va_arg(ap,double);
718 fvalue = va_arg(ap,max_float_t);
722 flags.plus_space_flag = PSF_MINUS;
725 ptr = float_conversion (fvalue,
727 buf_pointer += field_width,
730 flags.alternate_flag);
733 precision = field_width - (flags.plus_space_flag != PSF_NONE);
734 while (precision > ptr - buf_pointer)
735 *--buf_pointer = '0';
739 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
741 case '\0': /* Really bad place to find NUL in */
745 /* Undefined conversion! */
746 ptr = buf_pointer = bad_conversion;
747 ptr += sizeof(bad_conversion) - 1;
753 * This part emittes the formatted string to "put_one_char".
756 /* If field_width == 0 then nothing should be written. */
757 precision = ptr - buf_pointer;
759 if ( precision > field_width)
765 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
768 /* emit any leading pad characters */
769 if (!flags.left_adjust)
772 put_one_char(' ', secret_pointer);
773 #if CONFIG_PRINTF_COUNT_CHARS
778 /* emit flag characters (if any) */
779 if (flags.plus_space_flag)
781 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
782 #if CONFIG_PRINTF_COUNT_CHARS
790 while (--precision >= 0)
792 put_one_char(pgm_read_char(buf_pointer++), secret_pointer);
793 #if CONFIG_PRINTF_COUNT_CHARS
799 #endif /* CPU_HARVARD */
801 /* emit the string itself */
802 while (--precision >= 0)
804 put_one_char(*buf_pointer++, secret_pointer);
805 #if CONFIG_PRINTF_COUNT_CHARS
811 /* emit trailing space characters */
812 if (flags.left_adjust)
815 put_one_char(' ', secret_pointer);
816 #if CONFIG_PRINTF_COUNT_CHARS
822 #else /* PRINTF_REDUCED starts here */
824 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
825 char l_modifier, h_modifier;
826 unsigned long u_val, div_val;
828 unsigned int u_val, div_val;
829 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
832 unsigned int nr_of_chars, base;
837 for (;;) /* Until full format string read */
839 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
842 return (nr_of_chars);
843 put_one_char(format_flag, secret_pointer);
847 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
848 /*=================================*/
849 /* Optional 'l' or 'h' modifiers ? */
850 /*=================================*/
851 l_modifier = h_modifier = 0;
852 switch (PGM_READ_CHAR(format))
864 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
866 switch (format_flag = PGM_READ_CHAR(format++))
869 format_flag = va_arg(ap, int);
871 put_one_char(format_flag, secret_pointer);
876 ptr = va_arg(ap, char *);
877 while ((format_flag = *ptr++))
879 put_one_char(format_flag, secret_pointer);
889 div_val = 0x40000000;
890 goto CONVERSION_LOOP;
897 div_val = 1000000000;
898 goto CONVERSION_LOOP;
906 div_val = 0x10000000;
909 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
911 u_val = (format_flag == 'd') ?
912 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
914 u_val = va_arg(ap, long);
916 u_val = (format_flag == 'd') ?
917 va_arg(ap,int) : va_arg(ap,unsigned int);
918 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
919 u_val = va_arg(ap,int);
920 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
921 if (format_flag == 'd')
923 if (((int)u_val) < 0)
926 put_one_char('-', secret_pointer);
930 while (div_val > 1 && div_val > u_val)
936 outChar = (u_val / div_val) + '0';
939 if (format_flag == 'x')
940 outChar += 'a'-'9'-1;
942 outChar += 'A'-'9'-1;
944 put_one_char(outChar, secret_pointer);
951 } /* end switch(format_flag...) */
953 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
956 #endif /* CONFIG_PRINTF */