1 | /******************************************
|
2 | ***
|
3 | *** Th-Shell
|
4 | *** Command-Shell for Microcontrollers
|
5 | *** ------------------------------------
|
6 | ***
|
7 | *** Author: Thorsten de Buhr
|
8 | *** eMail: thorsten@lasertechnix.de
|
9 | *** Date started: 20.08.2006
|
10 | *** last Modified: 09.11.2006
|
11 | ***
|
12 | *** Modules:
|
13 | *** --------
|
14 | ***
|
15 | *** thprintf.c, thprintf.h
|
16 | ***
|
17 | ***
|
18 | *** Description:
|
19 | *** -------------
|
20 | ***
|
21 | *** Tiny printf-function, which provides most-used opts of printf.
|
22 | *** Usage is the same as printf
|
23 | ***
|
24 | ***
|
25 | ***
|
26 | ***
|
27 | *** Use is free, could run or not ;-)
|
28 | ***
|
29 | *** U may change every printf in cour code to thprintf, the rest is the same.
|
30 | ***
|
31 | *** U may use:
|
32 | *** %i, %s, %c
|
33 | *** \n, \t
|
34 | ***
|
35 | *** More is not implemented now.
|
36 | ***
|
37 | *** Have fun with this very small print-function, it uses ~300 bytes of code (printf uses >5k!)
|
38 | ***
|
39 | ******************************************/
|
40 |
|
41 |
|
42 | /**
|
43 | * @file thprintf.c
|
44 | * @brief very small printf-func
|
45 | * @author Th. de Buhr (thorsten@lasertechnix.de)
|
46 | * @date 09.11.2006
|
47 | */
|
48 |
|
49 | #include <stdio.h>
|
50 | #include <stdarg.h>
|
51 | #include "thprintf.h"
|
52 |
|
53 |
|
54 |
|
55 | #define SHOW_ZEROS ((unsigned int)1<<30)
|
56 | #define LEAVE_PERCENT ((unsigned int)1<<31)
|
57 |
|
58 |
|
59 |
|
60 | /**
|
61 | * \author Th. de Buhr (thorsten@lasertechnix.de)
|
62 | * \param (char) *string, ... (vargs)
|
63 | * \return (int) 0 (now, perhaps something different later ?
|
64 | * \brief very small printf-function, which provides the most-used funcs of printf (see above)
|
65 | */
|
66 | int thprintf(char *string, ...)
|
67 | {
|
68 | va_list tag;
|
69 | int i=0;
|
70 | unsigned int flags=0;
|
71 | char *pointer=0;
|
72 | signed int int_tmp=0;
|
73 |
|
74 | va_start(tag, string);
|
75 |
|
76 | do
|
77 | {
|
78 | if(*string == '%')
|
79 | {
|
80 | flags = 0;
|
81 | while(!(flags & LEAVE_PERCENT))
|
82 | {
|
83 | switch(*(++string))
|
84 | {
|
85 | case '0': flags |= SHOW_ZEROS; continue;
|
86 | case '1': flags |= 10; continue;
|
87 | case '2': flags |= 100; continue;
|
88 | case '3': flags |= 1000; continue;
|
89 | case '4': flags |= 10000; continue;
|
90 | case '5': flags |= 100000; continue;
|
91 | case '6': flags |= 1000000; continue;
|
92 | case '7': flags |= 10000000; continue;
|
93 | case '8': flags |= 100000000; continue;
|
94 |
|
95 | case 'i':
|
96 | {
|
97 | int_tmp = va_arg(tag, int);
|
98 |
|
99 | if(int_tmp <0)
|
100 | {
|
101 | fputc('-', stdout);
|
102 | int_tmp=-int_tmp;
|
103 | }
|
104 |
|
105 | pointer = (char*)int_tmp;
|
106 | for(i=MAX_NUM_DEC; i; i/=10)
|
107 | {
|
108 | if((int)pointer/i)
|
109 | {
|
110 | fputc((int_tmp/i)+48, stdout);
|
111 | int_tmp%=i;
|
112 | }
|
113 | else
|
114 | {
|
115 | if(flags & SHOW_ZEROS)
|
116 | {
|
117 | if(i<(flags & 0xfffffff)) fputc('0', stdout);
|
118 | }
|
119 | else if(i<10) fputc('0', stdout);
|
120 | }
|
121 | }
|
122 | flags = LEAVE_PERCENT;
|
123 | } break;
|
124 |
|
125 | case 'c':
|
126 | {
|
127 | fputc((char)va_arg(tag, int), stdout);
|
128 | flags = LEAVE_PERCENT;
|
129 | } break;
|
130 |
|
131 | case 's':
|
132 | {
|
133 | pointer = va_arg(tag, char*);
|
134 |
|
135 | do
|
136 | {
|
137 | fputc(*pointer, stdout);
|
138 | } while(*++pointer);
|
139 |
|
140 | flags = LEAVE_PERCENT;
|
141 | } break;
|
142 | }
|
143 | } // end while flags & '%'
|
144 | }
|
145 | else if(*string == '\\')
|
146 | {
|
147 | switch(*(++string))
|
148 | {
|
149 | case 'n': fputc('\n', stdout);
|
150 | break;
|
151 |
|
152 | case 't': fputc('\t', stdout);
|
153 | break;
|
154 |
|
155 | case 'r': fputc('\r', stdout);
|
156 | break;
|
157 | }
|
158 | } // end if '\'
|
159 |
|
160 | else
|
161 | {
|
162 | fputc(*string, stdout);
|
163 | }
|
164 |
|
165 | } while(*(++string));
|
166 |
|
167 | va_end(tag);
|
168 | fflush(stdout);
|
169 |
|
170 | return(0);
|
171 | }
|