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.7 2004/08/04 15:53:47 rasky
57 * Nuove opzioni di configurazione per formatted_write e ridotto maggiormente l'utilizzo dellos tack
59 * Revision 1.6 2004/07/30 14:34:10 rasky
60 * Vari fix per documentazione e commenti
61 * Aggiunte PP_CATn e STATIC_ASSERT
63 * Revision 1.5 2004/07/29 22:57:09 bernie
64 * Switch to new-style config handling.
66 * Revision 1.4 2004/07/21 00:20:20 bernie
67 * Allow completely disabling printf()-like formatter.
69 * Revision 1.3 2004/07/18 22:00:15 bernie
70 * Reorganize configuration parameters to match DevLib's convention.
72 * Revision 1.2 2004/06/03 11:27:09 bernie
73 * Add dual-license information.
75 * Revision 1.1 2004/05/23 15:43:16 bernie
76 * Import mware modules.
81 #include <compiler.h> /* progmem macros */
82 #include <config.h> /* CONFIG_ macros */
83 #include <drv/kdebug.h> /* ASSERT */
85 #ifndef CONFIG_PRINTF_N_FORMATTER
86 /*! Enable arcane %n formatter */
87 #define CONFIG_PRINTF_N_FORMATTER 0
90 #ifndef CONFIG_PRINTF_OCTAL_FORMATTER
91 /*! Enable %o formatter */
92 #define CONFIG_PRINTF_OCTAL_FORMATTER 0
95 // True if we must keep a count of the number of characters we print
96 #define CONFIG_PRINTF_COUNT_CHARS (CONFIG_PRINTF_RETURN_COUNT || CONFIG_PRINTF_N_FORMATTER)
100 #if CONFIG_PRINTF > PRINTF_NOFLOAT
102 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
104 #if CONFIG_PRINTF > PRINTF_NOFLOAT
105 /*bernie: save some memory, who cares about floats with lots of decimals? */
106 /*#define FRMWRI_BUFSIZE 134*/
107 #error 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 #ifndef MEM_ATTRIBUTE
117 #define MEM_ATTRIBUTE
120 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
121 #define IS_SHORT (h_modifier || (sizeof(int) == 2 && !l_modifier))
123 #define IS_SHORT (sizeof(int) == 2)
124 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
127 #if CONFIG_PRINTF > PRINTF_NOFLOAT
129 static char *float_conversion(MEM_ATTRIBUTE long double value,
130 MEM_ATTRIBUTE short nr_of_digits,
131 MEM_ATTRIBUTE char *buf,
132 MEM_ATTRIBUTE char format_flag,
133 MEM_ATTRIBUTE char g_flag,
134 MEM_ATTRIBUTE bool alternate_flag)
136 MEM_ATTRIBUTE char *cp;
137 MEM_ATTRIBUTE char *buf_pointer;
138 MEM_ATTRIBUTE short n, i, dec_point_pos, integral_10_log;
145 while (value >= 1e11) /* To speed up things a bit */
148 integral_10_log += 10;
156 else if (value) /* Not just 0.0 */
158 while (value <= 1e-10) /* To speed up things a bit */
161 integral_10_log -= 10;
171 if (integral_10_log < nr_of_digits && integral_10_log >= -4)
174 nr_of_digits -= integral_10_log;
178 /* %#G - No removal of trailing zeros */
181 /* %G - Removal of trailing zeros */
182 alternate_flag = true;
192 /* Less than one... */
193 if (integral_10_log < 0)
195 *buf_pointer++ = '0';
196 if ((n = nr_of_digits) || alternate_flag)
197 *buf_pointer++ = '.';
199 while (--i > integral_10_log && nr_of_digits)
201 *buf_pointer++ = '0';
204 if (integral_10_log < (-n - 1))
205 /* Nothing more to do */
211 dec_point_pos = - integral_10_log;
216 while (i <= nr_of_digits )
218 value -= (long double)(n = (short)value); /* n=Digit value=Remainder */
219 value *= 10; /* Prepare for next shot */
220 *buf_pointer++ = n + '0';
221 if ( ! i++ && (nr_of_digits || alternate_flag))
222 *buf_pointer++ = '.';
225 /* Rounding possible */
229 cp = buf_pointer - 1;
234 if ( (*cp += n) == ('9' + 1) )
242 } while (cp-- > buf);
251 if (*(cp - 1) == '.')
276 /* %G - Remove trailing zeros */
279 while (*(buf_pointer - 1) == '0')
281 if (*(buf_pointer - 1) == '.')
288 *buf_pointer++ = format_flag;
289 if (integral_10_log < 0)
291 *buf_pointer++ = '-';
292 integral_10_log = -integral_10_log;
295 *buf_pointer++ = '+';
301 *buf_pointer++ = (integral_10_log % 10) + '0';
302 integral_10_log /= 10;
303 } while ( integral_10_log || n < 2 );
304 for ( i = n ; n > 0 ; n-- )
305 *(buf_pointer - 11 - i + n) = *(buf_pointer - n);
308 return (buf_pointer);
311 #endif /* CONFIG_PRINTF > PRINTF_NOFLOAT */
314 * This routine forms the core and entry of the formatter.
316 * The conversion performed conforms to the ANSI specification for "printf".
319 PGM_FUNC(_formatted_write)(const char * PGM_ATTR format,
320 void put_one_char(char, void *),
321 void *secret_pointer,
324 #if CONFIG_PRINTF > PRINTF_REDUCED
325 MEM_ATTRIBUTE static char bad_conversion[] = "???";
326 MEM_ATTRIBUTE static char null_pointer[] = "<NULL>";
328 MEM_ATTRIBUTE int precision;
330 #if CONFIG_PRINTF_COUNT_CHARS
331 MEM_ATTRIBUTE int nr_of_chars;
333 MEM_ATTRIBUTE int field_width;
334 MEM_ATTRIBUTE char format_flag;
335 enum PLUS_SPACE_FLAGS {
336 PSF_NONE, PSF_PLUS, PSF_MINUS
340 #if CONFIG_PRINTF_OCTAL_FORMATTER
345 MEM_ATTRIBUTE enum PLUS_SPACE_FLAGS plus_space_flag : 2;
346 #if CONFIG_PRINTF_OCTAL_FORMATTER
347 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 2;
349 MEM_ATTRIBUTE enum DIV_FACTOR div_factor : 1;
351 MEM_ATTRIBUTE bool left_adjust : 1;
352 MEM_ATTRIBUTE bool l_L_modifier : 1;
353 MEM_ATTRIBUTE bool h_modifier : 1;
354 MEM_ATTRIBUTE bool alternate_flag : 1;
355 MEM_ATTRIBUTE bool nonzero_value : 1;
356 MEM_ATTRIBUTE bool zeropad : 1;
358 MEM_ATTRIBUTE unsigned long ulong;
360 #if CONFIG_PRINTF > PRINTF_NOFLOAT
361 MEM_ATTRIBUTE long double fvalue;
364 MEM_ATTRIBUTE char *buf_pointer;
365 MEM_ATTRIBUTE char *ptr;
366 MEM_ATTRIBUTE const char *hex;
367 MEM_ATTRIBUTE char buf[FRMWRI_BUFSIZE];
369 #if CONFIG_PRINTF_COUNT_CHARS
372 for (;;) /* Until full format string read */
374 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
377 #if CONFIG_PRINTF_RETURN_COUNT
378 return (nr_of_chars);
382 put_one_char(format_flag, secret_pointer);
383 #if CONFIG_PRINTF_COUNT_CHARS
387 if (PGM_READ_CHAR(format) == '%') /* %% prints as % */
390 put_one_char('%', secret_pointer);
391 #if CONFIG_PRINTF_COUNT_CHARS
397 flags.left_adjust = false;
398 flags.alternate_flag = false;
399 flags.plus_space_flag = PSF_NONE;
400 flags.zeropad = false;
401 ptr = buf_pointer = &buf[0];
402 hex = "0123456789ABCDEF";
404 /* check for leading '-', '+', ' ','#' or '0' flags */
407 switch (PGM_READ_CHAR(format))
410 if (flags.plus_space_flag)
413 flags.plus_space_flag = PSF_PLUS;
416 flags.left_adjust = true;
419 flags.alternate_flag = true;
422 flags.zeropad = true;
430 /* Optional field width (may be '*') */
431 if (PGM_READ_CHAR(format) == '*')
433 field_width = va_arg(ap, int);
436 field_width = -field_width;
437 flags.left_adjust = true;
444 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
445 field_width = field_width * 10 + (PGM_READ_CHAR(format++) - '0');
448 if (flags.left_adjust)
449 flags.zeropad = false;
451 /* Optional precision (or '*') */
452 if (PGM_READ_CHAR(format) == '.')
454 if (PGM_READ_CHAR(++format) == '*')
456 precision = va_arg(ap, int);
462 while (PGM_READ_CHAR(format) >= '0' && PGM_READ_CHAR(format) <= '9')
463 precision = precision * 10 + (PGM_READ_CHAR(format++) - '0');
469 /* At this point, "left_adjust" is nonzero if there was
470 * a sign, "zeropad" is 1 if there was a leading zero
471 * and 0 otherwise, "field_width" and "precision"
472 * contain numbers corresponding to the digit strings
473 * before and after the decimal point, respectively,
474 * and "plus_space_flag" is either 0 (no flag) or
475 * contains a plus or space character. If there was no
476 * decimal point, "precision" will be -1.
479 flags.l_L_modifier = false;
480 flags.h_modifier = false;
482 /* Optional 'l','L' r 'h' modifier? */
483 switch (PGM_READ_CHAR(format))
487 flags.l_L_modifier = true;
491 flags.h_modifier = true;
497 * At exit from the following switch, we will emit
498 * the characters starting at "buf_pointer" and
501 switch (format_flag = PGM_READ_CHAR(format++))
503 #if CONFIG_PRINTF_N_FORMATTER
505 if (sizeof(short) != sizeof(int))
507 if (sizeof(int) != sizeof(long))
510 *va_arg(ap, short *) = nr_of_chars;
511 else if (flags.l_L_modifier)
512 *va_arg(ap, long *) = nr_of_chars;
514 *va_arg(ap, int *) = nr_of_chars;
519 *va_arg(ap, short *) = nr_of_chars;
521 *va_arg(ap, int *) = nr_of_chars;
526 if (flags.l_L_modifier)
527 *va_arg(ap, long *) = nr_of_chars;
529 *va_arg(ap, int *) = nr_of_chars;
534 buf[0] = va_arg(ap, int);
539 if ( !(buf_pointer = va_arg(ap, char *)) )
540 buf_pointer = null_pointer;
543 for (n=0; *buf_pointer++ && n < precision; n++)
549 #if CONFIG_PRINTF_OCTAL_FORMATTER
551 if (flags.alternate_flag && !precision)
555 hex = "0123456789abcdef";
559 if (format_flag == 'p')
560 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
561 ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
562 #else /* 32bit pointers */
563 ulong = (unsigned long)va_arg(ap, char *);
564 #endif /* 32bit pointers */
565 else if (sizeof(short) == sizeof(int))
566 ulong = flags.l_L_modifier ?
567 va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
569 ulong = flags.h_modifier ?
570 (unsigned long)(unsigned short) va_arg(ap, int)
571 : (unsigned long)va_arg(ap, int);
573 #if CONFIG_PRINTF_OCTAL_FORMATTER
574 (format_flag == 'o') ? DIV_OCT :
576 (format_flag == 'u') ? DIV_DEC : DIV_HEX;
577 flags.plus_space_flag = PSF_NONE;
578 goto INTEGRAL_CONVERSION;
582 if (sizeof(short) == sizeof(long))
584 if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
586 flags.plus_space_flag = PSF_MINUS;
587 ulong = (unsigned long)(-((signed long)ulong));
590 else if (sizeof(short) == sizeof(int))
592 if ( (long)(ulong = flags.l_L_modifier ?
593 va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
595 flags.plus_space_flag = PSF_MINUS;
596 ulong = (unsigned long)(-((signed long)ulong));
601 if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
602 (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
604 flags.plus_space_flag = PSF_MINUS;
605 ulong = (unsigned long)(-((signed long)ulong));
608 flags.div_factor = DIV_DEC;
610 /* Now convert to digits */
612 ptr = buf_pointer = &buf[FRMWRI_BUFSIZE - 1];
613 flags.nonzero_value = (ulong != 0);
615 /* No char if zero and zero precision */
616 if (precision != 0 || flags.nonzero_value)
618 switch (flags.div_factor)
622 *--buf_pointer = hex[ulong % 10];
628 *--buf_pointer = hex[ulong % 16];
631 #if CONFIG_PRINTF_OCTAL_FORMATTER
634 *--buf_pointer = hex[ulong % 8];
641 /* "precision" takes precedence */
644 precision = field_width - (flags.plus_space_flag != PSF_NONE);
645 while (precision > (int)(ptr - buf_pointer))
646 *--buf_pointer = '0';
648 if (flags.alternate_flag && flags.nonzero_value)
650 if (format_flag == 'x' || format_flag == 'X')
652 *--buf_pointer = format_flag;
653 *--buf_pointer = '0';
655 #if CONFIG_PRINTF_OCTAL_FORMATTER
656 else if ((format_flag == 'o') && (*buf_pointer != '0'))
658 *--buf_pointer = '0';
662 ASSERT(buf_pointer >= buf);
665 #if CONFIG_PRINTF > PRINTF_NOFLOAT
674 goto FLOATING_CONVERSION;
685 if (sizeof(double) != sizeof(long double))
687 if ( (fvalue = flags.l_L_modifier ?
688 va_arg(ap,long double) : va_arg(ap,double)) < 0)
690 flags.plus_space_flag = PSF_MINUS;
694 else if ( (fvalue = va_arg(ap,long double)) < 0)
696 flags.plus_space_flag = PSF_MINUS;
699 ptr = float_conversion (fvalue,
701 buf_pointer += field_width,
704 flags.alternate_flag);
707 precision = field_width - (flags.plus_space_flag != PSF_NONE);
708 while (precision > ptr - buf_pointer)
709 *--buf_pointer = '0';
713 #else /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
719 ptr = buf_pointer = bad_conversion;
723 #endif /* CONFIG_PRINTF <= PRINTF_NOFLOAT */
725 case '\0': /* Really bad place to find NUL in */
729 /* Undefined conversion! */
730 ptr = buf_pointer = bad_conversion;
737 * This part emittes the formatted string to "put_one_char".
740 /* If field_width == 0 then nothing should be written. */
741 precision = ptr - buf_pointer;
743 if ( precision > field_width)
749 n = field_width - precision - (flags.plus_space_flag != PSF_NONE);
752 /* emit any leading pad characters */
753 if (!flags.left_adjust)
756 put_one_char(' ', secret_pointer);
757 #if CONFIG_PRINTF_COUNT_CHARS
762 /* emit flag characters (if any) */
763 if (flags.plus_space_flag)
765 put_one_char(flags.plus_space_flag == PSF_PLUS ? '+' : '-', secret_pointer);
766 #if CONFIG_PRINTF_COUNT_CHARS
771 /* emit the string itself */
772 while (--precision >= 0)
774 put_one_char(*buf_pointer++, secret_pointer);
775 #if CONFIG_PRINTF_COUNT_CHARS
780 /* emit trailing space characters */
781 if (flags.left_adjust)
784 put_one_char(' ', secret_pointer);
785 #if CONFIG_PRINTF_COUNT_CHARS
791 #else /* PRINTF_REDUCED starts here */
793 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
794 char l_modifier, h_modifier;
795 unsigned long u_val, div_val;
797 unsigned int u_val, div_val;
798 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
801 unsigned int nr_of_chars, base;
806 for (;;) /* Until full format string read */
808 while ((format_flag = PGM_READ_CHAR(format++)) != '%') /* Until '%' or '\0' */
811 return (nr_of_chars);
812 put_one_char(format_flag, secret_pointer);
816 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
817 /*=================================*/
818 /* Optional 'l' or 'h' modifiers ? */
819 /*=================================*/
820 l_modifier = h_modifier = 0;
821 switch (PGM_READ_CHAR(format))
833 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
835 switch (format_flag = PGM_READ_CHAR(format++))
838 format_flag = va_arg(ap, int);
840 put_one_char(format_flag, secret_pointer);
845 ptr = va_arg(ap, char *);
846 while ((format_flag = *ptr++))
848 put_one_char(format_flag, secret_pointer);
858 div_val = 0x40000000;
859 goto CONVERSION_LOOP;
866 div_val = 1000000000;
867 goto CONVERSION_LOOP;
875 div_val = 0x10000000;
878 #if CONFIG_PRINTF > PRINTF_NOMODIFIERS
880 u_val = (format_flag == 'd') ?
881 (short)va_arg(ap, int) : (unsigned short)va_arg(ap, int);
883 u_val = va_arg(ap, long);
885 u_val = (format_flag == 'd') ?
886 va_arg(ap,int) : va_arg(ap,unsigned int);
887 #else /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
888 u_val = va_arg(ap,int);
889 #endif /* CONFIG_PRINTF > PRINTF_NOMODIFIERS */
890 if (format_flag == 'd')
892 if (((int)u_val) < 0)
895 put_one_char('-', secret_pointer);
899 while (div_val > 1 && div_val > u_val)
905 outChar = (u_val / div_val) + '0';
908 if (format_flag == 'x')
909 outChar += 'a'-'9'-1;
911 outChar += 'A'-'9'-1;
913 put_one_char(outChar, secret_pointer);
920 } /* end switch(format_flag...) */
922 #endif /* CONFIG_PRINTF > PRINTF_REDUCED */
925 #endif /* CONFIG_PRINTF */