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
|
/*
* fy-emit-accum.h - internal YAML emitter accumulator header
*
* Copyright (c) 2019 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
*
* SPDX-License-Identifier: MIT
*/
#ifndef FY_EMIT_ACCUM_H
#define FY_EMIT_ACCUM_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <libfyaml.h>
#include "fy-utf8.h"
#include "fy-event.h"
struct fy_emit_accum {
char *accum;
size_t alloc;
size_t next;
char *inplace;
size_t inplacesz;
int col, row;
int ts;
enum fy_lb_mode lb_mode;
};
static inline void
fy_emit_accum_init(struct fy_emit_accum *ea,
void *inplace, size_t inplacesz,
int ts, enum fy_lb_mode lb_mode)
{
memset(ea, 0, sizeof(*ea));
ea->inplace = inplace;
ea->inplacesz = inplacesz;
ea->accum = ea->inplace;
ea->alloc = ea->inplacesz;
ea->ts = ts ? ts : 8;
ea->lb_mode = lb_mode;
}
static inline void
fy_emit_accum_reset(struct fy_emit_accum *ea)
{
ea->next = 0;
ea->col = 0;
ea->row = 0;
}
static inline void
fy_emit_accum_cleanup(struct fy_emit_accum *ea)
{
if (ea->accum && ea->accum != ea->inplace)
free(ea->accum);
ea->accum = ea->inplace;
ea->alloc = ea->inplacesz;
fy_emit_accum_reset(ea);
}
static inline void
fy_emit_accum_start(struct fy_emit_accum *ea, int col, enum fy_lb_mode lb_mode)
{
fy_emit_accum_reset(ea);
ea->col = col;
ea->lb_mode = lb_mode;
}
static inline void
fy_emit_accum_finish(struct fy_emit_accum *ea)
{
fy_emit_accum_reset(ea);
}
static inline int
fy_emit_accum_grow(struct fy_emit_accum *ea, size_t need)
{
size_t atleast, asz;
char *new_accum;
atleast = ea->alloc + need;
asz = ea->alloc;
/* minimum buffer is 32 */
if (asz < 32)
asz = 32;
do {
asz *= 2;
} while (asz < atleast);
assert(asz > ea->inplacesz);
new_accum = realloc(ea->accum == ea->inplace ? NULL : ea->accum, asz);
if (!new_accum) /* out of memory */
return -1;
if (ea->accum && ea->accum == ea->inplace)
memcpy(new_accum, ea->accum, ea->next);
ea->alloc = asz;
ea->accum = new_accum;
return 0;
}
static inline int
fy_emit_accum_utf8_put_raw(struct fy_emit_accum *ea, int c)
{
size_t w, avail;
int ret;
/* grow if needed */
w = fy_utf8_width(c);
if (w > (avail = (ea->alloc - ea->next))) {
ret = fy_emit_accum_grow(ea, w - avail);
if (ret != 0)
return ret;
}
(void)fy_utf8_put_unchecked(ea->accum + ea->next, c);
ea->next += w;
return 0;
}
static inline int
fy_emit_accum_put_raw(struct fy_emit_accum *ea, int c)
{
int ret;
/* only lower ascii please */
if (c >= 0x80)
return -1;
/* grow if needed */
if (ea->next >= ea->alloc) {
ret = fy_emit_accum_grow(ea, 1);
if (ret != 0)
return ret;
}
*(ea->accum + ea->next) = (char)c;
ea->next++;
return 0;
}
static inline int
fy_emit_accum_utf8_put(struct fy_emit_accum *ea, int c)
{
int ret;
if (!fy_utf8_is_valid(c))
return -1;
if (fy_is_lb_m(c, ea->lb_mode)) {
ret = fy_emit_accum_put_raw(ea, '\n');
if (ret)
return ret;
ea->col = 0;
ea->row++;
} else if (fy_is_tab(c)) {
ret = fy_emit_accum_put_raw(ea, '\t');
if (ret)
return ret;
ea->col += (ea->ts - (ea->col % ea->ts));
} else {
if (c < 0x80) {
ret = fy_emit_accum_put_raw(ea, c);
if (ret)
return ret;
} else {
ret = fy_emit_accum_utf8_put_raw(ea, c);
}
ea->col++;
}
return 0;
}
static inline int
fy_emit_accum_utf8_write_raw(struct fy_emit_accum *ea, const void *data, size_t len)
{
size_t avail;
int ret;
/* grow if needed */
if (len > (avail = (ea->alloc - ea->next))) {
ret = fy_emit_accum_grow(ea, len - avail);
if (ret != 0)
return ret;
}
memcpy(ea->accum + ea->next, data, len);
ea->next += len;
return 0;
}
static inline int
fy_emit_accum_utf8_write(struct fy_emit_accum *ea, const void *data, size_t len)
{
const char *s, *e;
int c, w, ret;
for (s = data, e = s + len; (c = fy_utf8_get(s, (e - s), &w)) >= 0; s += w) {
ret = fy_emit_accum_utf8_put(ea, c);
if (ret)
break;
}
return c == FYUG_EOF ? 0 : -1;
}
static inline int
fy_emit_accum_utf8_printf_raw(struct fy_emit_accum *ea, const char *fmt, ...)
FY_ATTRIBUTE(format(printf, 2, 3));
static inline int
fy_emit_accum_utf8_printf_raw(struct fy_emit_accum *ea, const char *fmt, ...)
{
va_list ap;
size_t avail, len;
int ret;
/* get the size of the string */
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
/* grow if needed */
if ((len + 1) > (avail = (ea->alloc - ea->next))) {
ret = fy_emit_accum_grow(ea, (len + 1) - avail);
if (ret != 0)
return ret;
}
va_start(ap, fmt);
(void)vsnprintf(ea->accum + ea->next, len + 1, fmt, ap);
va_end(ap);
ea->next += len;
return 0;
}
static inline const char *
fy_emit_accum_get(struct fy_emit_accum *ea, size_t *lenp)
{
*lenp = ea->next;
if (!ea->next) {
return "";
}
return ea->accum;
}
static inline int
fy_emit_accum_make_0_terminated(struct fy_emit_accum *ea)
{
int ret;
/* the empty case is special cased */
if (!ea->next)
return 0;
/* grow if needed for the '\0' */
if (ea->next >= ea->alloc) {
ret = fy_emit_accum_grow(ea, 1);
if (ret != 0)
return ret;
}
assert(ea->next < ea->alloc);
*(ea->accum + ea->next) = '\0';
return 0;
}
static inline const char *
fy_emit_accum_get0(struct fy_emit_accum *ea)
{
int ret;
ret = fy_emit_accum_make_0_terminated(ea);
if (ret)
return NULL;
return ea->accum;
}
static inline char *
fy_emit_accum_steal(struct fy_emit_accum *ea, size_t *lenp)
{
int ret;
char *buf;
/* empty, return a malloc'ed buffer to "" */
if (!ea->next) {
buf = strdup("");
if (!buf) {
*lenp = 0;
return NULL;
}
*lenp = ea->next;
} else if (ea->inplace && ea->accum == ea->inplace) {
buf = malloc(ea->next + 1);
if (!buf) {
*lenp = 0;
return NULL;
}
memcpy(buf, ea->accum, ea->next);
buf[ea->next] = '\0';
*lenp = ea->next;
} else {
ret = fy_emit_accum_make_0_terminated(ea);
if (ret) {
*lenp = 0;
return NULL;
}
assert(ea->accum && ea->accum != ea->inplace);
buf = ea->accum;
*lenp = ea->next;
/* reset to inplace */
ea->accum = ea->inplace;
ea->alloc = ea->inplacesz;
}
fy_emit_accum_cleanup(ea);
return buf;
}
static inline char *
fy_emit_accum_steal0(struct fy_emit_accum *ea)
{
size_t len;
return fy_emit_accum_steal(ea, &len);
}
static inline bool
fy_emit_accum_empty(struct fy_emit_accum *ea)
{
return ea->next == 0;
}
static inline int
fy_emit_accum_size(struct fy_emit_accum *ea)
{
return ea->next;
}
static inline int
fy_emit_accum_column(struct fy_emit_accum *ea)
{
return ea->col;
}
static inline int
fy_emit_accum_row(struct fy_emit_accum *ea)
{
return ea->row;
}
struct fy_emit_accum_state {
int col;
int row;
size_t next;
};
static inline void
fy_emit_accum_get_state(struct fy_emit_accum *ea, struct fy_emit_accum_state *s)
{
s->col = ea->col;
s->row = ea->row;
s->next = ea->next;
}
static inline void
fy_emit_accum_rewind_state(struct fy_emit_accum *ea, const struct fy_emit_accum_state *s)
{
/* we can only go back */
assert(s->next <= ea->next);
ea->col = s->col;
ea->row = s->row;
ea->next = s->next;
}
#endif
|