/
misc.c
581 lines (477 loc) · 17.3 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* misc.c
* Copyright: (When this is determined...it will go here)
* CVS Info
* $Id$
* Overview:
* Miscellaneous functions
* Data Structure and Algorithms:
* History:
* Notes:
* References:
*/
#include "parrot/parrot.h"
#include <stdarg.h>
typedef long HUGEINTVAL;
typedef unsigned long UHUGEINTVAL;
typedef struct spfinfo_t {
UINTVAL flags;
UINTVAL width;
UINTVAL prec;
UINTVAL type;
UINTVAL phase;
} *SpfInfo;
#define cstr2pstr(cstr) string_make(interpreter, cstr, strlen(cstr), NULL, 0, NULL)
#define char2pstr(ch) string_make(interpreter, &ch, 1, NULL, 0, NULL)
#define PHASE_FLAGS 0
#define PHASE_WIDTH 1
#define PHASE_PREC 2
#define PHASE_TYPE 3
#define PHASE_TERM 4
#define PHASE_DONE 5
#define FLAG_MINUS 1
#define FLAG_PLUS 2
#define FLAG_ZERO 4
#define FLAG_SPACE 8
#define FLAG_SHARP 16
#define SIZE_REG 0
#define SIZE_SHORT 1
#define SIZE_LONG 2
#define SIZE_HUGE 3
#define SIZE_XVAL 4
#define GetInt(targ, whichone) \
switch(whichone) { \
case SIZE_REG: \
targ=(HUGEINTVAL)(int)va_arg(*args, int); \
break; \
case SIZE_SHORT: /* "'short int' is promoted to 'int' when passed through '...'" */ \
targ=(HUGEINTVAL)(short)va_arg(*args, int); \
break; \
case SIZE_LONG: \
targ=(HUGEINTVAL)(long)va_arg(*args, long); \
break; \
case SIZE_HUGE: \
targ=(HUGEINTVAL)(long /*long*/)va_arg(*args, long /*long*/); \
break; \
case SIZE_XVAL: \
targ=(HUGEINTVAL)(INTVAL)va_arg(*args, INTVAL); \
break; \
}
#define GetUInt(targ, whichone) \
switch(whichone) { \
case SIZE_REG: \
targ=(UHUGEINTVAL)(unsigned int)va_arg(*args, unsigned int); \
break; \
case SIZE_SHORT: /* short int promoted HLAGHLAGHLAGH. See note above */ \
targ=(UHUGEINTVAL)(unsigned short)va_arg(*args, int); \
break; \
case SIZE_LONG: \
targ=(UHUGEINTVAL)(unsigned long)va_arg(*args, unsigned long); \
break; \
case SIZE_HUGE: \
targ=(UHUGEINTVAL)(unsigned long /*long*/)va_arg(*args, unsigned long /*long*/); \
break; \
case SIZE_XVAL: \
targ=(UHUGEINTVAL)(UINTVAL)va_arg(*args, UINTVAL); \
break; \
}
/*
void int_to_str(char *, char *, HUGEINTVAL, INTVAL );
*/
void gen_sprintf_call(char *, char *, SpfInfo, char);
static void
uint_to_str(char *buf1, char *buf2, UHUGEINTVAL num, char base)
{
int i = 0, cur2;
char cur;
do {
cur = (char)(num % base);
if (cur < 10) {
buf2[i] = (char)('0' + cur);
}
else {
buf2[i] = (char)('a' + cur);
}
i++;
} while (num /= base);
cur2 = i;
for (i = 0; i <= cur2; i++) {
buf1[i] = buf2[cur2 - i];
}
}
static void
int_to_str(char *buf1, char *buf2, HUGEINTVAL num, char base)
{
BOOLVAL neg;
int i = 0, cur2;
char cur;
if (num < 0) {
neg = 1;
num = -num;
}
else {
neg = 0;
}
do {
cur = (char)(num % base);
if (cur < 10) {
buf2[i] = (char)('0' + cur);
}
else {
buf2[i] = (char)('a' + cur);
}
i++;
} while (num /= base);
if (neg) {
buf2[i++] = '-';
}
cur2 = i;
for (i = 0; i < cur2; i++) {
buf1[i] = buf2[cur2 - i - 1];
}
buf1[i] = 0;
}
static void
Pad_it(SpfInfo info, char *buf)
{
int i;
int len = strlen(buf);
int howmuch = info->width - len;
if (!info->width || howmuch == 0) {
return;
}
else if (howmuch < 0) {
memmove(buf, buf - howmuch, (size_t)(len + howmuch));
}
else if (info->flags & FLAG_MINUS) { /* left-align */
for (i = 0; i < howmuch; i++) {
buf[i + len] = ' ';
}
buf[i + len] = 0;
}
else { /* right-align */
memmove(buf + howmuch, buf, (size_t)len);
for (i = 0; i < howmuch; i++) {
buf[i] = ' ';
}
}
}
void
gen_sprintf_call(char *buf, char *buf2, SpfInfo info, char thingy)
{
int i = 0;
buf[i++] = '%';
if (info->flags) {
if (info->flags & FLAG_MINUS) {
buf[i++] = '-';
}
if (info->flags & FLAG_PLUS) {
buf[i++] = '+';
}
if (info->flags & FLAG_ZERO) {
buf[i++] = '0';
}
if (info->flags & FLAG_SPACE) {
buf[i++] = ' ';
}
if (info->flags & FLAG_SHARP) {
buf[i++] = '#';
}
}
if (info->width) {
uint_to_str(buf + i, buf2, info->width, 10);
i = strlen(buf);
}
if (info->prec) {
buf[i++] = '.';
uint_to_str(buf + i, buf2, info->prec, 10);
i = strlen(buf);
}
buf[i++] = thingy;
buf[i] = 0;
}
STRING *
Parrot_vsprintf_s(struct Parrot_Interp *interpreter, STRING *pat,
va_list *args)
{
INTVAL i;
STRING *targ = NULL;
register char *t1 = mem_sys_allocate(4096);
register char *t2 = mem_sys_allocate(4096);
for (i = 0; i < (INTVAL)string_length(pat); i++) {
if (string_ord(pat, i) == '%') {
if (string_ord(pat, i + 1) == '%') {
i++;
}
else {
/* hoo boy, here we go... */
char *chptr;
STRING *string;
PMC *pmc;
double dbl;
register HUGEINTVAL theint = 0;
register UHUGEINTVAL theuint = 0;
struct spfinfo_t info = { 0, 0, 0, 0, 0 };
for (i++; i < (INTVAL)string_length(pat)
&& info.phase != PHASE_DONE; i++) {
INTVAL ch = string_ord(pat, i);
switch (info.phase) {
/*@fallthrough@ */ case PHASE_FLAGS:
switch (ch) {
case '-':
info.flags |= FLAG_MINUS;
break;
case '+':
info.flags |= FLAG_PLUS;
break;
case '0':
info.flags |= FLAG_ZERO;
break;
case ' ':
info.flags |= FLAG_SPACE;
break;
case '#':
info.flags |= FLAG_SHARP;
break;
default:
info.phase = PHASE_WIDTH;
}
/*@fallthrough@ */ case PHASE_WIDTH:
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '8':
case '9':
info.width *= 10;
info.width += ch - '0';
break;
case '.':
info.phase = PHASE_PREC;
continue;
default:
info.phase = PHASE_PREC;
}
/*@fallthrough@ */ case PHASE_PREC:
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '8':
case '9':
info.prec *= 10;
info.prec += ch - '0';
continue;
default:
info.phase = PHASE_TYPE;
}
/*@fallthrough@ */ case PHASE_TYPE:
switch (ch) {
case 'h':
info.type = SIZE_SHORT;
break;
case 'l':
info.type = SIZE_LONG;
break;
case 'H':
info.type = SIZE_HUGE;
break;
case 'v':
info.type = SIZE_XVAL;
break;
default:
info.phase = PHASE_TERM;
}
info.phase = PHASE_TERM;
continue;
/*@fallthrough@ */ case PHASE_TERM:
switch (ch) {
/* INTEGERS */
case 'c':
targ = string_concat(interpreter, targ,
char2pstr(ch), 0);
break;
case 'd':
case 'i':
GetInt(theint, info.type);
int_to_str(t1, t2, theint, 10);
targ = string_concat(interpreter, targ,
cstr2pstr(t1), 0);
break;
case 'o':
GetInt(theint, info.type);
int_to_str(t1, t2, theint, 8);
targ = string_concat(interpreter, targ,
cstr2pstr(t1), 0);
break;
case 'x':
GetInt(theint, info.type);
int_to_str(t1, t2, theint, 16);
targ = string_concat(interpreter, targ,
cstr2pstr(t1), 0);
break;
case 'u':
GetUInt(theuint, info.type);
uint_to_str(t1, t2, theuint, 10);
targ = string_concat(interpreter, targ,
cstr2pstr(t1), 0);
break;
case 'p':
chptr = va_arg(*args, void *);
int_to_str(t1, t2, (HUGEINTVAL)chptr, 16);
targ = string_concat(interpreter, targ,
cstr2pstr(t1), 0);
break;
/* FLOATS - We cheat on these and use the system sprintf. */
case 'e':
dbl = va_arg(*args, double);
gen_sprintf_call(t1, t2, &info, 'e');
sprintf(t2, t1, dbl);
targ = string_concat(interpreter, targ,
cstr2pstr(t2), 0);
break;
case 'E':
dbl = va_arg(*args, double);
gen_sprintf_call(t1, t2, &info, 'E');
sprintf(t2, t1, dbl);
targ = string_concat(interpreter, targ,
cstr2pstr(t2), 0);
break;
case 'f':
dbl = va_arg(*args, double);
gen_sprintf_call(t1, t2, &info, 'f');
sprintf(t2, t1, dbl);
targ = string_concat(interpreter, targ,
cstr2pstr(t2), 0);
break;
case 'g':
dbl = va_arg(*args, double);
gen_sprintf_call(t1, t2, &info, 'g');
sprintf(t2, t1, dbl);
targ = string_concat(interpreter, targ,
cstr2pstr(t2), 0);
break;
case 'G':
dbl = va_arg(*args, double);
gen_sprintf_call(t1, t2, &info, 'G');
sprintf(t2, t1, dbl);
targ = string_concat(interpreter, targ,
cstr2pstr(t2), 0);
break;
/* STRINGS */
case 's':
chptr = va_arg(*args, char *);
targ = string_concat(interpreter, targ,
cstr2pstr(chptr), 0);
break;
case 'S':
string = va_arg(*args, STRING *);
targ = string_concat(interpreter, targ, string, 0);
break;
case 'P':
pmc = va_arg(*args, PMC *);
targ =
string_concat(interpreter, targ,
pmc->vtable->
get_string(interpreter, pmc), 0);
break;
}
info.phase = PHASE_DONE;
}
}
}
i--;
}
else {
STRING *substr = NULL;
string_substr(interpreter, pat, i, 1, &substr);
targ = string_concat(interpreter, targ, substr, 0);
}
}
mem_sys_free(t1);
mem_sys_free(t2);
return targ;
}
STRING *
Parrot_vsprintf_c(struct Parrot_Interp *interpreter, const char *pat,
va_list *args)
{
STRING *realpat = string_make(interpreter, pat, strlen(pat),
NULL, 0, NULL);
return Parrot_vsprintf_s(interpreter, realpat, args);
}
void
Parrot_vsprintf(struct Parrot_Interp *interpreter, char *targ, const char *pat,
va_list *args)
{
STRING *ret = Parrot_vsprintf_c(interpreter, pat, args);
/* string_transcode(interpreter, ret, NULL, NULL, &ret);*/
memcpy(targ, ret->bufstart, ret->bufused);
targ[ret->bufused + 1] = 00;
}
void
Parrot_vsnprintf(struct Parrot_Interp *interpreter, char *targ,
UINTVAL len, const char *pat, va_list *args)
{
STRING *ret = Parrot_vsprintf_c(interpreter, pat, args);
string_transcode(interpreter, ret, NULL, NULL, &ret);
if (len > ret->bufused) {
len = ret->bufused;
}
memcpy(targ, ret->bufstart, len);
targ[len + 1] = 0;
}
STRING *
Parrot_sprintf_s(struct Parrot_Interp *interpreter, STRING *pat, ...)
{
STRING *ret;
va_list args;
va_start(args, pat);
ret = Parrot_vsprintf_s(interpreter, pat, &args);
va_end(args);
return ret;
}
STRING *
Parrot_sprintf_c(struct Parrot_Interp *interpreter, const char *pat, ...)
{
STRING *ret;
va_list args;
va_start(args, pat);
ret = Parrot_vsprintf_c(interpreter, pat, &args);
va_end(args);
return ret;
}
void
Parrot_sprintf(struct Parrot_Interp *interpreter, char *targ, const char *pat,
...)
{
va_list args;
va_start(args, pat);
Parrot_vsprintf(interpreter, targ, pat, &args);
va_end(args);
}
void
Parrot_snprintf(struct Parrot_Interp *interpreter, char *targ, UINTVAL len,
const char *pat, ...)
{
va_list args;
va_start(args, pat);
Parrot_vsnprintf(interpreter, targ, len, pat, &args);
va_end(args);
}
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/