-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_concat_sprintf.c
92 lines (79 loc) · 2.42 KB
/
string_concat_sprintf.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
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "bstring.h"
#define STRING_CFS_SIZE 32
static int
defprintf(struct string* s, const char const* fmt, va_list exactlyonearg)
{
char buf[4096];
int len;
len = vsnprintf(buf, 4096, fmt, exactlyonearg);
return string_concatb(s, buf, len);
}
int string_concat_sprintf(struct string* s, const char *msg, ...)
{
va_list args;
char* a_s;
struct string* a_S;
char curchar;
int a_i;
char cfs[STRING_CFS_SIZE]; /* complex format string */
int cfsptr;
/* reserve one byte for the format char, one for the terminating zero */
int cfsmaxlen = STRING_CFS_SIZE - 2;
*cfs = '%';
cfs[cfsmaxlen] = 0;
va_start(args, msg);
for (;*msg;msg++) {
if (*msg == '%') {
cfsptr = 1;
switch((curchar = *++msg)) {
case '\0':
goto finished;
case 's':
a_s = va_arg(args, char*);
if (string_concat(s, a_s)) return 1;
break;
case 'S':
a_S = va_arg(args, struct string*);
if (string_concats(s, a_S)) return 1;
break;
case 'd':
a_i = va_arg(args, int);
if (string_putint(s, a_i)) return 1;
break;
case '0' ... '9':
case '.':
case ' ':
case '#':
case '+':
case '-':
case '\'': do {
if (cfsptr == cfsmaxlen) {
(void)fprintf(stderr, "Debug: Format string \"%s\" exceeds maximum length!\n", cfs);
exit(1);
}
cfs[cfsptr++] = curchar;
} while ((curchar = *++msg), ((curchar >= '0') && (curchar <= '9')) ||
(curchar == '.') || (curchar == ' ') ||
(curchar == '#') || (curchar == '+') ||
(curchar == '-') || (curchar == '\''));
/* fallthrough */
default:
/* guaranteed to have two bytes left */
cfs[cfsptr++] = curchar;
cfs[cfsptr] = 0;
if (defprintf(s, cfs, args)) return 1;
}
} else {
if (string_putc(s, *msg)) return 1;
}
}
finished:
va_end(args);
if (string_putc(s, 0)) return 1;
--s->_u._s.length;
return 0;
}