-
Notifications
You must be signed in to change notification settings - Fork 0
/
yoctonw.c
273 lines (244 loc) · 5.72 KB
/
yoctonw.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
//
// Copyright (c) 2022, Simon Howard
//
// Permission to use, copy, modify, and/or distribute this software
// for any purpose with or without fee is hereby granted, provided
// that the above copyright notice and this permission notice appear
// in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#include "yoctonw.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
struct yoctonw_writer {
yoctonw_write callback;
void *callback_handle;
uint8_t *buf;
size_t buf_len, buf_size;
char *printf_buf;
size_t printf_buf_size;
int indent_level;
int error;
};
struct yoctonw_writer *yoctonw_write_with(yoctonw_write callback, void *handle)
{
struct yoctonw_writer *writer = NULL;
writer = (struct yoctonw_writer *) calloc(
1, sizeof(struct yoctonw_writer));
if (writer == NULL) {
return NULL;
}
writer->callback = callback;
writer->callback_handle = handle;
writer->indent_level = 0;
writer->error = 0;
writer->buf_size = 256;
writer->buf_len = 0;
writer->buf = (uint8_t *) calloc(writer->buf_size, sizeof(char));
writer->printf_buf = NULL;
writer->printf_buf_size = 0;
if (writer->buf == NULL) {
free(writer);
return NULL;
}
return writer;
}
static int fwrite_wrapper(void *buf, size_t nbytes, void *handle)
{
return fwrite(buf, 1, nbytes, (FILE *) handle) == nbytes;
}
struct yoctonw_writer *yoctonw_write_to(FILE *fstream)
{
return yoctonw_write_with(fwrite_wrapper, fstream);
}
void yoctonw_free(struct yoctonw_writer *writer)
{
free(writer->printf_buf);
free(writer->buf);
free(writer);
}
void yoctonw_flush(struct yoctonw_writer *w)
{
int success;
if (w->buf_len == 0) {
return;
}
if (w->error) {
w->buf_len = 0;
return;
}
success = w->callback(w->buf, w->buf_len, w->callback_handle);
if (!success) {
w->error = 1;
}
w->buf_len = 0;
}
static inline void insert_char(struct yoctonw_writer *w, uint8_t c)
{
if (w->buf_len >= w->buf_size) {
yoctonw_flush(w);
}
w->buf[w->buf_len] = c;
++w->buf_len;
}
static int is_bare_string(const char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++) {
if (!isalnum(s[i]) && strchr("_-+.", s[i]) == NULL) {
return 0;
}
}
return 1;
}
static void write_string(struct yoctonw_writer *w, const char *s)
{
int i, c;
char hex[3];
if (strlen(s) > 0 && is_bare_string(s)) {
for (i = 0; s[i] != '\0'; ++i) {
insert_char(w, s[i]);
}
return;
}
// Some characters need escaping:
insert_char(w, '"');
for (i = 0; s[i] != '\0'; i++) {
c = s[i];
switch (c) {
case '\n': insert_char(w, '\\'); insert_char(w, 'n'); break;
case '\t': insert_char(w, '\\'); insert_char(w, 't'); break;
case '\\': insert_char(w, '\\'); insert_char(w, '\\'); break;
case '\"': insert_char(w, '\\'); insert_char(w, '\"'); break;
default:
if (c >= 0x20) {
insert_char(w, c);
break;
}
snprintf(hex, sizeof(hex), "%02x", c);
insert_char(w, '\\');
insert_char(w, 'x');
insert_char(w, hex[0]);
insert_char(w, hex[1]);
break;
}
}
insert_char(w, '"');
}
static void write_indent(struct yoctonw_writer *w)
{
int i;
for (i = 0; i < w->indent_level; i++) {
insert_char(w, '\t');
}
}
void yoctonw_prop(struct yoctonw_writer *w, const char *name,
const char *value)
{
if (w->error) {
return;
}
write_indent(w);
write_string(w, name);
insert_char(w, ':');
insert_char(w, ' ');
write_string(w, value);
insert_char(w, '\n');
// We flush after every top-level def is completed; this means
// output will always have been flushed before writer is freed.
if (w->indent_level == 0) {
yoctonw_flush(w);
}
}
void yoctonw_subobject(struct yoctonw_writer *w, const char *name)
{
if (w->error) {
return;
}
write_indent(w);
write_string(w, name);
insert_char(w, ' ');
insert_char(w, '{');
insert_char(w, '\n');
++w->indent_level;
}
void yoctonw_end(struct yoctonw_writer *w)
{
if (w->indent_level == 0) {
return;
}
--w->indent_level;
write_indent(w);
insert_char(w, '}');
insert_char(w, '\n');
if (w->indent_level == 0) {
yoctonw_flush(w);
}
}
int yoctonw_have_error(struct yoctonw_writer *w)
{
return w->error;
}
static int increase_buffer(struct yoctonw_writer *w, size_t min_size)
{
char *new_buf;
size_t new_buf_size;
if (w->printf_buf_size == 0) {
new_buf_size = min_size;
} else {
new_buf_size = w->printf_buf_size;
}
while (new_buf_size < min_size) {
new_buf_size *= 2;
}
new_buf = (char *) realloc(w->printf_buf, new_buf_size);
if (new_buf == NULL) {
// TODO: Better error reporting?
w->error = 1;
return 0;
}
w->printf_buf = new_buf;
w->printf_buf_size = new_buf_size;
return 1;
}
void yoctonw_printf(struct yoctonw_writer *w, const char *name,
const char *fmt, ...)
{
va_list args;
int attempt, sz;
if (w->error) {
return;
}
if (w->printf_buf_size == 0 && !increase_buffer(w, 128)) {
return;
}
// We try to snprintf and if it fails, we try again with a larger
// buffer.
for (attempt = 0; attempt < 2; ++attempt) {
va_start(args, fmt);
sz = vsnprintf(w->printf_buf, w->printf_buf_size, fmt, args);
va_end(args);
if (sz < w->printf_buf_size) {
yoctonw_prop(w, name, w->printf_buf);
return;
}
if (!increase_buffer(w, sz + 1)) {
return;
}
}
// This should never happen.
// TODO: Better error reporting?
w->error = 1;
}