diff options
Diffstat (limited to 'utils/hwstub/stmp/format.c')
-rw-r--r-- | utils/hwstub/stmp/format.c | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/utils/hwstub/stmp/format.c b/utils/hwstub/stmp/format.c new file mode 100644 index 0000000000..f5783159c0 --- /dev/null +++ b/utils/hwstub/stmp/format.c | |||
@@ -0,0 +1,223 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Gary Czvitkovicz | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | |||
23 | #include <stdarg.h> | ||
24 | #include <limits.h> | ||
25 | #include "stddef.h" | ||
26 | #include "string.h" | ||
27 | #include "format.h" | ||
28 | |||
29 | static const char hexdigit[] = "0123456789ABCDEF"; | ||
30 | |||
31 | void vuprintf( | ||
32 | /* call 'push()' for each output letter */ | ||
33 | int (*push)(void *userp, unsigned char data), | ||
34 | void *userp, | ||
35 | const char *fmt, | ||
36 | va_list ap) | ||
37 | { | ||
38 | char *str; | ||
39 | char tmpbuf[12], pad; | ||
40 | int ch, width, val, sign, precision; | ||
41 | long lval, lsign; | ||
42 | unsigned int uval; | ||
43 | unsigned long ulval; | ||
44 | size_t uszval; | ||
45 | ssize_t szval, szsign; | ||
46 | bool ok = true; | ||
47 | |||
48 | tmpbuf[sizeof tmpbuf - 1] = '\0'; | ||
49 | |||
50 | while ((ch = *fmt++) != '\0' && ok) | ||
51 | { | ||
52 | if (ch == '%') | ||
53 | { | ||
54 | ch = *fmt++; | ||
55 | pad = ' '; | ||
56 | if (ch == '0') | ||
57 | pad = '0'; | ||
58 | |||
59 | width = 0; | ||
60 | while (ch >= '0' && ch <= '9') | ||
61 | { | ||
62 | width = 10*width + ch - '0'; | ||
63 | ch = *fmt++; | ||
64 | } | ||
65 | |||
66 | precision = 0; | ||
67 | if(ch == '.') | ||
68 | { | ||
69 | ch = *fmt++; | ||
70 | while (ch >= '0' && ch <= '9') | ||
71 | { | ||
72 | precision = 10*precision + ch - '0'; | ||
73 | ch = *fmt++; | ||
74 | } | ||
75 | } else { | ||
76 | precision = INT_MAX; | ||
77 | } | ||
78 | |||
79 | str = tmpbuf + sizeof tmpbuf - 1; | ||
80 | switch (ch) | ||
81 | { | ||
82 | case 'c': | ||
83 | *--str = va_arg (ap, int); | ||
84 | break; | ||
85 | |||
86 | case 's': | ||
87 | str = va_arg (ap, char*); | ||
88 | break; | ||
89 | |||
90 | case 'd': | ||
91 | val = sign = va_arg (ap, int); | ||
92 | if (val < 0) | ||
93 | val = -val; | ||
94 | do | ||
95 | { | ||
96 | *--str = (val % 10) + '0'; | ||
97 | val /= 10; | ||
98 | } | ||
99 | while (val > 0); | ||
100 | if (sign < 0) | ||
101 | *--str = '-'; | ||
102 | break; | ||
103 | |||
104 | case 'u': | ||
105 | uval = va_arg(ap, unsigned int); | ||
106 | do | ||
107 | { | ||
108 | *--str = (uval % 10) + '0'; | ||
109 | uval /= 10; | ||
110 | } | ||
111 | while (uval > 0); | ||
112 | break; | ||
113 | |||
114 | case 'x': | ||
115 | case 'X': | ||
116 | pad='0'; | ||
117 | uval = va_arg (ap, int); | ||
118 | do | ||
119 | { | ||
120 | *--str = hexdigit[uval & 0xf]; | ||
121 | uval >>= 4; | ||
122 | } | ||
123 | while (uval); | ||
124 | break; | ||
125 | |||
126 | case 'l': | ||
127 | ch = *fmt++; | ||
128 | switch(ch) { | ||
129 | case 'x': | ||
130 | case 'X': | ||
131 | pad='0'; | ||
132 | ulval = va_arg (ap, long); | ||
133 | do | ||
134 | { | ||
135 | *--str = hexdigit[ulval & 0xf]; | ||
136 | ulval >>= 4; | ||
137 | } | ||
138 | while (ulval); | ||
139 | break; | ||
140 | case 'd': | ||
141 | lval = lsign = va_arg (ap, long); | ||
142 | if (lval < 0) | ||
143 | lval = -lval; | ||
144 | do | ||
145 | { | ||
146 | *--str = (lval % 10) + '0'; | ||
147 | lval /= 10; | ||
148 | } | ||
149 | while (lval > 0); | ||
150 | if (lsign < 0) | ||
151 | *--str = '-'; | ||
152 | break; | ||
153 | |||
154 | case 'u': | ||
155 | ulval = va_arg(ap, unsigned long); | ||
156 | do | ||
157 | { | ||
158 | *--str = (ulval % 10) + '0'; | ||
159 | ulval /= 10; | ||
160 | } | ||
161 | while (ulval > 0); | ||
162 | break; | ||
163 | |||
164 | default: | ||
165 | *--str = 'l'; | ||
166 | *--str = ch; | ||
167 | } | ||
168 | |||
169 | break; | ||
170 | |||
171 | case 'z': | ||
172 | ch = *fmt++; | ||
173 | switch(ch) { | ||
174 | case 'd': | ||
175 | szval = szsign = va_arg (ap, ssize_t); | ||
176 | if (szval < 0) | ||
177 | szval = -szval; | ||
178 | do | ||
179 | { | ||
180 | *--str = (szval % 10) + '0'; | ||
181 | szval /= 10; | ||
182 | } | ||
183 | while (szval > 0); | ||
184 | if (szsign < 0) | ||
185 | *--str = '-'; | ||
186 | break; | ||
187 | |||
188 | case 'u': | ||
189 | uszval = va_arg(ap, size_t); | ||
190 | do | ||
191 | { | ||
192 | *--str = (uszval % 10) + '0'; | ||
193 | uszval /= 10; | ||
194 | } | ||
195 | while (uszval > 0); | ||
196 | break; | ||
197 | |||
198 | default: | ||
199 | *--str = 'z'; | ||
200 | *--str = ch; | ||
201 | } | ||
202 | |||
203 | break; | ||
204 | |||
205 | default: | ||
206 | *--str = ch; | ||
207 | break; | ||
208 | } | ||
209 | |||
210 | if (width > 0) | ||
211 | { | ||
212 | width -= strlen (str); | ||
213 | while (width-- > 0 && ok) | ||
214 | ok=push(userp, pad); | ||
215 | } | ||
216 | while (*str != '\0' && ok && precision--) | ||
217 | ok=push(userp, *str++); | ||
218 | } | ||
219 | else | ||
220 | ok=push(userp, ch); | ||
221 | } | ||
222 | } | ||
223 | |||