blob: 37e6abf1e56e7743180c91559efc5014af623dfd [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifdef UEMF
2 #include "uemf.h"
3#else
4 #include "basic/basicInternal.h"
5#endif
6
7#define kUseMemcopy
8
9#define STR_REALLOC 0x1
10#define STR_INC 64
11
12typedef struct {
13 char_t *s;
14 int size;
15 int max;
16 int count;
17 int flags;
18} strbuf_t;
19
20enum flag {
21 flag_none = 0,
22 flag_minus = 1,
23 flag_plus = 2,
24 flag_space = 4,
25 flag_hash = 8,
26 flag_zero = 16,
27 flag_short = 32,
28 flag_long = 64
29};
30
31static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg, int msize);
32#ifndef HAVE_STRNLEN
33static int strnlen(char_t *s, unsigned int n);
34#endif
35
36static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
37 int upper, char_t *prefix, int width, int prec, enum flag f);
38
39static void put_char(strbuf_t *buf, char_t c);
40static void put_string(strbuf_t *buf, char_t *s, int len,
41 int width, int prec, enum flag f);
42
43static void put_char(strbuf_t *buf, char_t c)
44{
45 if (buf->count >= (buf->size - 1)) {
46 if (! (buf->flags & STR_REALLOC)) {
47 return;
48 }
49 buf->size += STR_INC;
50 if (buf->size > buf->max && buf->size > STR_INC) {
51
52 buf->size -= STR_INC;
53 return;
54 }
55 if (buf->s == NULL) {
56 buf->s = balloc(B_L, buf->size * sizeof(char_t));
57 } else {
58 buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t));
59 }
60 }
61 if(buf->s){
62 buf->s[buf->count] = c;
63 if (c != '\0') {
64 ++buf->count;
65 }
66 }
67}
68
69static void put_string(strbuf_t *buf, char_t *s, int len, int width,
70 int prec, enum flag f)
71{
72 int index;
73
74 if (len < 0) {
75 len = strnlen(s, prec >= 0 ? prec : ULONG_MAX);
76 } else if (prec >= 0 && prec < len) {
77 len = prec;
78 }
79 if (width > len && !(f & flag_minus)) {
80 for (index = len; index < width; ++index) {
81 put_char(buf, ' ');
82 }
83 }
84 for (index = 0; index < len; ++index) {
85 put_char(buf, s[index]);
86 }
87 if (width > len && f & flag_minus) {
88 for (index = len; index < width; ++index) {
89 put_char(buf, ' ');
90 }
91 }
92}
93
94int fmtAlloc(char_t **s, int n, char_t *fmt, ...)
95{
96 va_list ap = {0};
97 int result;
98
99 a_assert(s);
100 a_assert(fmt);
101
102 *s = NULL;
103 va_start(ap, fmt);
104 result = dsnprintf(s, n, fmt, ap, 0);
105 va_end(ap);
106 return result;
107}
108
109int fmtValloc(char_t **s, int n, char_t *fmt, va_list arg)
110{
111 a_assert(s);
112 a_assert(fmt);
113
114 *s = NULL;
115 return dsnprintf(s, n, fmt, arg, 0);
116}
117
118int fmtRealloc(char_t **s, int n, int msize, char_t *fmt, ...)
119{
120 va_list ap = {0};
121 int result;
122
123 a_assert(s);
124 a_assert(fmt);
125
126 if (msize == -1) {
127 *s = NULL;
128 }
129 va_start(ap, fmt);
130 result = dsnprintf(s, n, fmt, ap, msize);
131 va_end(ap);
132 return result;
133}
134
135static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg, int msize)
136{
137 char_t tmp_c;
138 strbuf_t tmp_buf;
139
140 a_assert(s);
141 a_assert(fmt);
142
143 memset(&tmp_buf, 0, sizeof(tmp_buf));
144 tmp_buf.s = *s;
145
146 if (*s == NULL || msize != 0) {
147 tmp_buf.max = size;
148 tmp_buf.flags |= STR_REALLOC;
149 if (msize != 0) {
150 tmp_buf.size = max(msize, 0);
151 }
152 if (*s != NULL && msize != 0) {
153 tmp_buf.count = gstrlen(*s);
154 }
155 } else {
156 tmp_buf.size = size;
157 }
158
159 while ((tmp_c = *fmt++) != '\0') {
160 if (tmp_c != '%' || (tmp_c = *fmt++) == '%') {
161 put_char(&tmp_buf, tmp_c);
162 } else {
163 enum flag f = flag_none;
164 int width = 0;
165 int prec = -1;
166 for ( ; tmp_c != '\0'; tmp_c = *fmt++) {
167 if (tmp_c == '-') {
168 f |= flag_minus;
169 } else if (tmp_c == '+') {
170 f |= flag_plus;
171 } else if (tmp_c == ' ') {
172 f |= flag_space;
173 } else if (tmp_c == '#') {
174 f |= flag_hash;
175 } else if (tmp_c == '0') {
176 f |= flag_zero;
177 } else {
178 break;
179 }
180 }
181 if (tmp_c == '*') {
182 width = va_arg(arg, int);
183 if (width < 0) {
184 f |= flag_minus;
185 width = -width;
186 }
187 tmp_c = *fmt++;
188 } else {
189 for ( ; gisdigit((int)tmp_c); tmp_c = *fmt++) {
190 width = width * 10 + (tmp_c - '0');
191 }
192 }
193 if (tmp_c == '.') {
194 f &= ~flag_zero;
195 tmp_c = *fmt++;
196 if (tmp_c == '*') {
197 prec = va_arg(arg, int);
198 tmp_c = *fmt++;
199 } else {
200 for (prec = 0; gisdigit((int)tmp_c); tmp_c = *fmt++) {
201 prec = prec * 10 + (tmp_c - '0');
202 }
203 }
204 }
205 if (tmp_c == 'h' || tmp_c == 'l') {
206 f |= (tmp_c == 'h' ? flag_short : flag_long);
207 tmp_c = *fmt++;
208 }
209 if (tmp_c == 'd' || tmp_c == 'i') {
210 long int value;
211 if (f & flag_short) {
212 value = (short int) va_arg(arg, int);
213 } else if (f & flag_long) {
214 value = va_arg(arg, long int);
215 } else {
216 value = va_arg(arg, int);
217 }
218 if (value >= 0) {
219 if (f & flag_plus) {
220 put_ulong(&tmp_buf, value, 10, 0, T("+"), width, prec, f);
221 } else if (f & flag_space) {
222 put_ulong(&tmp_buf, value, 10, 0, T(" "), width, prec, f);
223 } else {
224 put_ulong(&tmp_buf, value, 10, 0, NULL, width, prec, f);
225 }
226 } else {
227 put_ulong(&tmp_buf, -value, 10, 0, T("-"), width, prec, f);
228 }
229 } else if (tmp_c == 'o' || tmp_c == 'u' || tmp_c == 'x' || tmp_c == 'X') {
230 unsigned long int value;
231 if (f & flag_short) {
232 value = (unsigned short int) va_arg(arg, unsigned int);
233 } else if (f & flag_long) {
234 value = va_arg(arg, unsigned long int);
235 } else {
236 value = va_arg(arg, unsigned int);
237 }
238 if (tmp_c == 'o') {
239 if (f & flag_hash && value != 0) {
240 put_ulong(&tmp_buf, value, 8, 0, T("0"), width, prec, f);
241 } else {
242 put_ulong(&tmp_buf, value, 8, 0, NULL, width, prec, f);
243 }
244 } else if (tmp_c == 'u') {
245 put_ulong(&tmp_buf, value, 10, 0, NULL, width, prec, f);
246 } else {
247 if (f & flag_hash && value != 0) {
248 if (tmp_c == 'x') {
249 put_ulong(&tmp_buf, value, 16, 0, T("0x"), width, prec, f);
250 } else {
251 put_ulong(&tmp_buf, value, 16, 1, T("0X"), width, prec, f);
252 }
253 } else {
254 put_ulong(&tmp_buf, value, 16, ('X' == tmp_c) , NULL, width, prec, f);
255 }
256 }
257
258 } else if (tmp_c == 'c') {
259 char_t value = va_arg(arg, int);
260 put_char(&tmp_buf, value);
261
262 } else if (tmp_c == 's' || tmp_c == 'S') {
263 char_t *value = va_arg(arg, char_t *);
264 if (value == NULL) {
265 put_string(&tmp_buf, T("(null)"), -1, width, prec, f);
266 } else if (f & flag_hash) {
267 put_string(&tmp_buf,
268 value + 1, (char_t) *value, width, prec, f);
269 } else {
270 put_string(&tmp_buf, value, -1, width, prec, f);
271 }
272 } else if (tmp_c == 'p') {
273 void *value = va_arg(arg, void *);
274 put_ulong(&tmp_buf,
275 (unsigned long int) value, 16, 0, T("0x"), width, prec, f);
276 } else if (tmp_c == 'n') {
277 if (f & flag_short) {
278 short int *value = va_arg(arg, short int *);
279 *value = tmp_buf.count;
280 } else if (f & flag_long) {
281 long int *value = va_arg(arg, long int *);
282 *value = tmp_buf.count;
283 } else {
284 int *value = va_arg(arg, int *);
285 *value = tmp_buf.count;
286 }
287 } else {
288 put_char(&tmp_buf, tmp_c);
289 }
290 }
291 }
292 if (tmp_buf.s == NULL) {
293 put_char(&tmp_buf, '\0');
294 }
295
296 if (*s == NULL || msize != 0) {
297 *s = tmp_buf.s;
298 }
299
300 if (*s != NULL && size > 0) {
301 if (tmp_buf.count < size) {
302 (*s)[tmp_buf.count] = '\0';
303 } else {
304 (*s)[tmp_buf.size - 1] = '\0';
305 }
306 }
307
308 if (msize != 0) {
309 return tmp_buf.size;
310 }
311 return tmp_buf.count;
312}
313#ifndef HAVE_STRNLEN
314static int strnlen(char_t *s, unsigned int n)
315{
316 unsigned int len;
317
318 len = gstrlen(s);
319 return min(len, n);
320}
321#endif
322
323
324static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
325 int upper, char_t *prefix, int width, int prec, enum flag f)
326{
327 unsigned long x, x2;
328 int len, zeros, index;
329
330 for (len = 1, x = 1; x < ULONG_MAX / base; ++len, x = x2) {
331 x2 = x * base;
332 if (x2 > value) {
333 break;
334 }
335 }
336 zeros = (prec > len) ? prec - len : 0;
337 width -= zeros + len;
338 if (prefix != NULL) {
339 width -= strnlen(prefix, ULONG_MAX);
340 }
341 if (!(f & flag_minus)) {
342 if (f & flag_zero) {
343 for (index = 0; index < width; ++index) {
344 put_char(buf, '0');
345 }
346 } else {
347 for (index = 0; index < width; ++index) {
348 put_char(buf, ' ');
349 }
350 }
351 }
352 if (prefix != NULL) {
353 put_string(buf, prefix, -1, 0, -1, flag_none);
354 }
355 for (index = 0; index < zeros; ++index) {
356 put_char(buf, '0');
357 }
358 for ( ; x > 0; x /= base) {
359 int digit = (value / x) % base;
360 put_char(buf, (char)((digit < 10 ? '0' : (upper ? 'A' : 'a') - 10) + digit));
361 }
362 if (f & flag_minus) {
363 for (index = 0; index < width; ++index) {
364 put_char(buf, ' ');
365 }
366 }
367}
368
369char_t *ascToUni(char_t *ubuf, char *str, int nBytes)
370{
371 memcpy(ubuf, str, nBytes);
372 return ubuf;
373}
374
375
376char_t *ballocAscToUni(char *cp, int alen)
377{
378 char_t *unip;
379 int ulen;
380
381 ulen = (alen + 1) * sizeof(char_t);
382 if ((unip = balloc(B_L, ulen)) == NULL) {
383 return NULL;
384 }
385 ascToUni(unip, cp, ulen);
386 unip[alen] = 0;
387 return unip;
388}
389
390unsigned int hextoi(char_t *hexstring)
391{
392 register char_t *h;
393 register unsigned int c, v;
394
395 v = 0;
396 h = hexstring;
397 if (*h == '0' && (*(h+1) == 'x' || *(h+1) == 'X')) {
398 h += 2;
399 }
400 while ((c = (unsigned int)*h++) != 0) {
401 if (c >= '0' && c <= '9') {
402 c -= '0';
403 } else if (c >= 'a' && c <= 'f') {
404 c = (c - 'a') + 10;
405 } else if (c >= 'A' && c <= 'F') {
406 c = (c - 'A') + 10;
407 } else {
408 break;
409 }
410 v = (v * 0x10) + c;
411 }
412 return v;
413}
414
415char *uniToAsc(char *buf, char_t *ustr, int nBytes)
416{
417#ifdef UNICODE
418 if (WideCharToMultiByte(CP_ACP, 0, ustr, nBytes, buf, nBytes,
419 NULL, NULL) < 0)
420 {
421 return (char*) ustr;
422 }
423#else
424#ifdef kUseMemcopy
425 memcpy(buf, ustr, nBytes);
426#else
427 strncpy(buf, ustr, nBytes);
428#endif /* kUseMemcopy */
429#endif
430 return (char*) buf;
431}
432
433char *ballocUniToAsc(char_t *unip, int ulen)
434{
435 char * cp;
436
437 if ((cp = balloc(B_L, ulen+1)) == NULL) {
438 return NULL;
439 }
440 uniToAsc(cp, unip, ulen);
441 cp[ulen] = '\0';
442 return cp;
443}
444
445unsigned int gstrtoi(char_t *s)
446{
447 if (*s == '0' && (*(s+1) == 'x' || *(s+1) == 'X')) {
448 s += 2;
449 return hextoi(s);
450 }
451 return gatoi(s);
452}
453