8584474dd76ec31cbd3f79cf11513716c14703bf
[fw/sdcc] / device / lib / pic16 / libc / stdio / printf_small.c
1 /*-----------------------------------------------------------------
2     printf_small.c - source file for reduced version of printf
3
4     Modified for pic16 port, by Vangelis Rokas, 2004 (vrokas@otenet.gr)
5     
6     Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
7
8     This library is free software; you can redistribute it and/or modify it
9     under the terms of the GNU Library General Public License as published by the
10     Free Software Foundation; either version 2, or (at your option) any
11     later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Library General Public License for more details.
17
18     You should have received a copy of the GNU Library General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22     In other words, you are welcome to use, share and improve this program.
23     You are forbidden to forbid anyone else to use, share and improve
24     what you give them.   Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
26
27 /*
28 ** $Id$
29 */
30
31 /* This function uses function putchar() to dump a character
32  * to standard output. putchar() is defined in libc18f.lib
33  * as dummy function, which will be linked if no putchar()
34  * function is provided by the user.
35  * The user can write his own putchar() function and link it
36  * with the source *BEFORE* the libc18f.lib library. This way
37  * the linker will link the first function (i.e. the user's function) */
38
39 /* following formats are supported :-
40    format     output type       argument-type
41      %d        decimal             int
42      %ld       decimal             long
43      %hd       decimal             char
44      %x        hexadecimal         int
45      %lx       hexadecimal         long
46      %hx       hexadecimal         char
47      %o        octal               int
48      %lo       octal               long
49      %ho       octal               char
50      %c        character           char
51      %s        character           generic pointer
52      %f        float               float
53 */
54
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58
59 void printf_small(char *fmt, ...) __reentrant
60 {
61   char *ch;
62   char radix;
63   char flong;
64   char fstr;
65   char fchar;
66   char ffloat;
67   float flt;
68   char *str;
69   __data char *str1;
70   long val;
71   static char buffer[16];
72   va_list ap ;
73
74     ch = fmt;
75     va_start(ap,fmt);
76
77     while( *ch ) {                      //for (; *fmt ; fmt++ )
78         if (*ch == '%') {
79             flong = fstr = fchar = ffloat = 0;
80             radix = 0;
81             ch++;
82
83             if(*ch == 'l') {
84               flong = 1;
85               ch++;
86             } else
87             if(*ch == 'h') {
88               fchar = 1;
89               ch++;
90             }
91             
92             if(*ch == 's')fstr = 1;
93             else if(*ch == 'f')ffloat = 1;
94             else if(*ch == 'd')radix = 10;
95             else if(*ch == 'x')radix = 16;
96             else if(*ch == 'c')radix = 0;
97             else if(*ch == 'o')radix = 8;
98             
99             if(fstr) {
100                 str = va_arg(ap, char *);
101                 while (*str) putchar(*str++);
102             } else
103             if(ffloat) {
104                 flt = va_arg(ap, float);
105                 x_ftoa(flt, buffer, 32, 6);
106                 str1 = buffer;
107                 while( *str1 )str1++; str1--;
108                 while( *str1 == '0' )str1--; str1++;
109                 *str1 = 0; str1 = buffer;
110                 while( *str1 )putchar(*str1++);
111             } else {
112               if(flong)val = va_arg(ap, long);
113               else
114               if(fchar)val = (char)va_arg(ap, int); // FIXME: SDCC casts char arguments into ints
115               else {
116                   val = va_arg(ap, int);
117               }
118             
119               if(radix) {
120                 ltoa (val, buffer, radix);
121
122                 str1 = buffer;
123                 while ( *str1 ) {
124                   putchar ( *str1++ );
125                 }
126               } else
127                 putchar((char)val);
128             }
129           } else
130             putchar(*ch);
131
132         ch++;
133     }
134 }