1
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
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
153
154
159
160
167
168
173
174
175
179
180
184
185
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
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
273
293
294
295
296
297
298
303
304
305
306
309
310
311
314
315
316
317
318
319
329
330
341
342
343
344
/* ... */
/* ... */
#include "argtable3.h"
#ifndef ARG_AMALGAMATION
#include "argtable3_private.h"
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996)/* ... */
#endif
#define START_VSNBUFF 16
/* ... */
typedef struct _internal_arg_dstr {
char* data;
arg_dstr_freefn* free_proc;
char sbuf[ARG_DSTR_SIZE + 1];
char* append_data;
int append_data_size;
int append_used;
}{ ... } _internal_arg_dstr_t;
static void setup_append_buf(arg_dstr_t res, int newSpace);
arg_dstr_t arg_dstr_create(void) {
_internal_arg_dstr_t* h = (_internal_arg_dstr_t*)xmalloc(sizeof(_internal_arg_dstr_t));
memset(h, 0, sizeof(_internal_arg_dstr_t));
h->sbuf[0] = 0;
h->data = h->sbuf;
h->free_proc = ARG_DSTR_STATIC;
return h;
}{ ... }
void arg_dstr_destroy(arg_dstr_t ds) {
if (ds == NULL)
return;
arg_dstr_reset(ds);
xfree(ds);
return;
}{ ... }
void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc) {
int length;
register arg_dstr_freefn* old_free_proc = ds->free_proc;
char* old_result = ds->data;
if (str == NULL) {
ds->sbuf[0] = 0;
ds->data = ds->sbuf;
ds->free_proc = ARG_DSTR_STATIC;
}{...} else if (free_proc == ARG_DSTR_VOLATILE) {
length = (int)strlen(str);
if (length > ARG_DSTR_SIZE) {
ds->data = (char*)xmalloc((unsigned)length + 1);
ds->free_proc = ARG_DSTR_DYNAMIC;
}{...} else {
ds->data = ds->sbuf;
ds->free_proc = ARG_DSTR_STATIC;
}{...}
strcpy(ds->data, str);
}{...} else {
ds->data = str;
ds->free_proc = free_proc;
}{...}
/* ... */
if ((old_free_proc != 0) && (old_result != ds->data)) {
if (old_free_proc == ARG_DSTR_DYNAMIC) {
xfree(old_result);
}{...} else {
(*old_free_proc)(old_result);
}{...}
}{...}
if ((ds->append_data != NULL) && (ds->append_data_size > 0)) {
xfree(ds->append_data);
ds->append_data = NULL;
ds->append_data_size = 0;
}{...}
}{ ... }
char* arg_dstr_cstr(arg_dstr_t ds)
{
return ds->data;
}{...}
void arg_dstr_cat(arg_dstr_t ds, const char* str) {
setup_append_buf(ds, (int)strlen(str) + 1);
memcpy(ds->data + strlen(ds->data), str, strlen(str));
}{ ... }
void arg_dstr_catc(arg_dstr_t ds, char c) {
setup_append_buf(ds, 2);
memcpy(ds->data + strlen(ds->data), &c, 1);
}{ ... }
/* ... */
void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...) {
va_list arglist;
char* buff;
int n, r;
size_t slen;
if (fmt == NULL)
return;
/* ... */
if ((n = (int)(2 * strlen(fmt))) < START_VSNBUFF)
n = START_VSNBUFF;
buff = (char*)xmalloc((size_t)(n + 2));
memset(buff, 0, (size_t)(n + 2));
for (;;) {
va_start(arglist, fmt);
r = vsnprintf(buff, (size_t)(n + 1), fmt, arglist);
va_end(arglist);
slen = strlen(buff);
if (slen < (size_t)n)
break;
if (r > n)
n = r;
else
n += n;
xfree(buff);
buff = (char*)xmalloc((size_t)(n + 2));
memset(buff, 0, (size_t)(n + 2));
}{...}
arg_dstr_cat(ds, buff);
xfree(buff);
}{ ... }
static void setup_append_buf(arg_dstr_t ds, int new_space) {
int total_space;
/* ... */
if (ds->data != ds->append_data) {
/* ... */
if (ds->append_data_size > 500) {
xfree(ds->append_data);
ds->append_data = NULL;
ds->append_data_size = 0;
}{...}
ds->append_used = (int)strlen(ds->data);
}{...} else if (ds->data[ds->append_used] != 0) {
/* ... */
ds->append_used = (int)strlen(ds->data);
}{...}
total_space = new_space + ds->append_used;
if (total_space >= ds->append_data_size) {
char* newbuf;
if (total_space < 100) {
total_space = 200;
}{...} else {
total_space *= 2;
}{...}
newbuf = (char*)xmalloc((unsigned)total_space);
memset(newbuf, 0, (size_t)total_space);
strcpy(newbuf, ds->data);
if (ds->append_data != NULL) {
xfree(ds->append_data);
}{...}
ds->append_data = newbuf;
ds->append_data_size = total_space;
}{...} else if (ds->data != ds->append_data) {
strcpy(ds->append_data, ds->data);
}{...}
arg_dstr_free(ds);
ds->data = ds->append_data;
}{ ... }
void arg_dstr_free(arg_dstr_t ds) {
if (ds->free_proc != NULL) {
if (ds->free_proc == ARG_DSTR_DYNAMIC) {
xfree(ds->data);
}{...} else {
(*ds->free_proc)(ds->data);
}{...}
ds->free_proc = NULL;
}{...}
}{ ... }
void arg_dstr_reset(arg_dstr_t ds) {
arg_dstr_free(ds);
if ((ds->append_data != NULL) && (ds->append_data_size > 0)) {
xfree(ds->append_data);
ds->append_data = NULL;
ds->append_data_size = 0;
}{...}
ds->data = ds->sbuf;
ds->sbuf[0] = 0;
}{ ... }
#if defined(_MSC_VER)
#pragma warning(pop)
#endif