Mercurial > lcfOS
comparison cos/kernel/snprintf.c @ 9:92ace1ca50a8
64 bits kernel without interrupts but with printf in C
author | windel |
---|---|
date | Sun, 13 Nov 2011 12:47:47 +0100 |
parents | |
children | fcdae30b2782 |
comparison
equal
deleted
inserted
replaced
8:edd70006d3e4 | 9:92ace1ca50a8 |
---|---|
1 /* | |
2 * snprintf.c | |
3 */ | |
4 #include "kernel.h" | |
5 | |
6 #include "stdarg.h" | |
7 #include "ctype.h" | |
8 | |
9 unsigned char _ctype[] = { | |
10 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ | |
11 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ | |
12 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ | |
13 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ | |
14 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ | |
15 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ | |
16 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ | |
17 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ | |
18 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ | |
19 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ | |
20 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ | |
21 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ | |
22 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ | |
23 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ | |
24 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ | |
25 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ | |
26 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */ | |
27 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */ | |
28 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */ | |
29 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */ | |
30 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */ | |
31 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */ | |
32 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */ | |
33 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */ | |
34 | |
35 | |
36 #define MORE_THAN_YOU_WANT 1<<30 | |
37 #define MAX_STDOUT_CHARS 255 | |
38 | |
39 static char hexmap[] = { | |
40 '0', '1', '2', '3', '4', '5', '6', '7', | |
41 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' | |
42 }; | |
43 | |
44 void va_snprintf(char *b, int l, const char *fmt, va_list pvar) | |
45 { | |
46 int n, i; | |
47 uint_t u; | |
48 ulonglong_t ull; | |
49 char *t; | |
50 char d[10]; | |
51 char mod_l; | |
52 char mod_ll; | |
53 | |
54 if (!fmt || !b || (l < 1)) | |
55 return; | |
56 | |
57 mod_l = 0; | |
58 mod_ll = 0; | |
59 | |
60 while (l && *fmt) { | |
61 if (*fmt == '%') { | |
62 if (!(--l)) | |
63 break; | |
64 again: | |
65 fmt++; | |
66 | |
67 switch (*fmt) { | |
68 case '.': /* precision modifier */ | |
69 while(isdigit(fmt[1])) | |
70 fmt++; | |
71 goto again; | |
72 | |
73 case 'l': /* long modifier */ | |
74 if (!mod_l) { | |
75 mod_l = 1; | |
76 } else if (!mod_ll) { | |
77 mod_l = 0; | |
78 mod_ll = 1; | |
79 } | |
80 goto again; | |
81 | |
82 case 's': /* string */ | |
83 t = va_arg(pvar, char *); | |
84 while (l && *t) | |
85 *b++ = *t++, l--; | |
86 break; | |
87 | |
88 case 'c': /* single character */ | |
89 *b++ = va_arg(pvar, char); | |
90 l--; | |
91 break; | |
92 | |
93 case 'S': /* uint32 as a short ... */ | |
94 if (l < 4) { | |
95 l = 0; | |
96 break; | |
97 } | |
98 u = va_arg(pvar, unsigned int); | |
99 for (i = 3; i >= 0; i--) { | |
100 b[i] = hexmap[u & 0x0F]; | |
101 u >>= 4; | |
102 } | |
103 b += 4; | |
104 l -= 4; | |
105 break; | |
106 | |
107 case 'x': | |
108 case 'p': | |
109 if (!mod_ll) { /* 8 digit, unsigned 32-bit hex integer */ | |
110 if (l < 8) { | |
111 l = 0; | |
112 break; | |
113 } | |
114 u = va_arg(pvar, unsigned int); | |
115 for (i = 7; i >= 0; i--) { | |
116 b[i] = hexmap[u & 0x0F]; | |
117 u >>= 4; | |
118 } | |
119 b += 8; | |
120 l -= 8; | |
121 } else if (mod_ll) { /* 16 digit, unsigned 64-bit hex integer */ | |
122 if (l < 16) { | |
123 l = 0; | |
124 break; | |
125 } | |
126 ull = va_arg(pvar, unsigned long long); | |
127 for (i = 15; i >= 0; i--) { | |
128 b[i] = hexmap[ull & 0x0f]; | |
129 ull >>= 4; | |
130 } | |
131 b += 16; | |
132 l -= 16; | |
133 } | |
134 mod_l = mod_ll = 0; | |
135 break; | |
136 | |
137 case 'd': /* signed integer */ | |
138 n = va_arg(pvar, int); | |
139 if (n < 0) { | |
140 u = -n; | |
141 *b++ = '-'; | |
142 if (!(--l)) | |
143 break; | |
144 } else { | |
145 u = n; | |
146 } | |
147 goto u2; | |
148 | |
149 case 'u': /* unsigned integer */ | |
150 u = va_arg(pvar, unsigned int); | |
151 u2: | |
152 i = 9; | |
153 do { | |
154 d[i] = (u % 10) + '0'; | |
155 u /= 10; | |
156 i--; | |
157 } | |
158 while (u && i >= 0); | |
159 while (++i < 10) { | |
160 *b++ = d[i]; | |
161 if (!(--l)) | |
162 break; | |
163 } | |
164 break; | |
165 | |
166 case 'U': | |
167 u = va_arg(pvar, unsigned int); | |
168 i = 9; | |
169 d[8] = d[7] = d[6] = ' '; | |
170 do { | |
171 d[i] = (u % 10) + '0'; | |
172 u /= 10; | |
173 i--; | |
174 } | |
175 while (u && i >= 0); | |
176 i = 5; | |
177 while (++i < 10) { | |
178 *b++ = d[i]; | |
179 if (!(--l)) | |
180 break; | |
181 } | |
182 break; | |
183 | |
184 case 'X': /* 2 digit, unsigned 8bit hex int */ | |
185 if (l < 2) { | |
186 l = 0; | |
187 break; | |
188 } | |
189 n = va_arg(pvar, int); | |
190 *b++ = hexmap[(n & 0xF0) >> 4]; | |
191 *b++ = hexmap[n & 0x0F]; | |
192 l -= 2; | |
193 break; | |
194 default: | |
195 *b++ = *fmt; | |
196 } | |
197 } else { | |
198 *b++ = *fmt; | |
199 l--; | |
200 } | |
201 fmt++; | |
202 } | |
203 *b = 0; | |
204 } | |
205 | |
206 void snprintf(char *str, int len, char *fmt, ...) | |
207 { | |
208 va_list pvar; | |
209 va_start(pvar, fmt); | |
210 va_snprintf(str, len, fmt, pvar); | |
211 va_end(pvar); | |
212 } | |
213 | |
214 void | |
215 sprintf(char *dst, const char *fmt, ...) | |
216 { | |
217 va_list args; | |
218 va_start(args, fmt); | |
219 va_snprintf(dst, MORE_THAN_YOU_WANT, fmt, args); | |
220 va_end(args); | |
221 } | |
222 | |
223 void | |
224 printf(const char* fmt, ...) | |
225 { | |
226 static char buf[MAX_STDOUT_CHARS]; | |
227 | |
228 va_list args; | |
229 va_start(args, fmt); | |
230 va_snprintf(buf, MAX_STDOUT_CHARS, fmt, args); | |
231 va_end(args); | |
232 | |
233 print_string(buf); | |
234 } |