summaryrefslogtreecommitdiff
path: root/src/api.c
blob: 2af17c36bd3a9e1e31118e5dd49ead3c459bc555 (plain)
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

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <yaml/yaml.h>

#include <assert.h>

/*
 * Allocate a dynamic memory block.
 */

YAML_DECLARE(void *)
yaml_malloc(size_t size)
{
    return malloc(size ? size : 1);
}

/*
 * Reallocate a dynamic memory block.
 */

YAML_DECLARE(void *)
yaml_realloc(void *ptr, size_t size)
{
    return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1);
}

/*
 * Free a dynamic memory block.
 */

YAML_DECLARE(void)
yaml_free(void *ptr)
{
    if (ptr) free(ptr);
}

/*
 * Create a new parser object.
 */

YAML_DECLARE(yaml_parser_t *)
yaml_parser_new(void)
{
    yaml_parser_t *parser;

    /* Allocate the parser structure. */

    parser = yaml_malloc(sizeof(yaml_parser_t));
    if (!parser) goto error;

    memset(parser, 0, sizeof(yaml_parser_t));

    /* Allocate the raw buffer. */

    parser->raw_buffer = yaml_malloc(YAML_RAW_BUFFER_SIZE);
    if (!parser->raw_buffer) goto error;
    memset(parser->raw_buffer, 0, YAML_RAW_BUFFER_SIZE);

    parser->raw_pointer = parser->raw_buffer;
    parser->raw_unread = 0;

    /* Allocate the character buffer. */

    parser->buffer = yaml_malloc(YAML_BUFFER_SIZE);
    if (!parser->buffer) goto error;
    memset(parser->buffer, 0, YAML_BUFFER_SIZE);

    parser->buffer_end = parser->buffer;
    parser->pointer = parser->buffer;
    parser->unread = 0;

    /* Allocate the tokens queue. */

    parser->tokens = yaml_malloc(YAML_DEFAULT_SIZE*sizeof(yaml_token_t *));
    if (!parser->tokens) goto error;
    memset(parser->tokens, 0, YAML_DEFAULT_SIZE*sizeof(yaml_token_t *));

    parser->tokens_size = YAML_DEFAULT_SIZE;
    parser->tokens_head = 0;
    parser->tokens_tail = 0;
    parser->tokens_parsed = 0;

    /* Allocate the indents stack. */

    parser->indents = yaml_malloc(YAML_DEFAULT_SIZE*sizeof(int));
    if (!parser->indents) goto error;
    memset(parser->indents, 0, YAML_DEFAULT_SIZE*sizeof(int));

    parser->indents_size = YAML_DEFAULT_SIZE;
    parser->indents_length = 0;

    /* Allocate the stack of potential simple keys. */

    parser->simple_keys = yaml_malloc(YAML_DEFAULT_SIZE*sizeof(yaml_simple_key_t *));
    if (!parser->simple_keys) goto error;
    memset(parser->simple_keys, 0, YAML_DEFAULT_SIZE*sizeof(yaml_simple_key_t *));

    parser->simple_keys_size = YAML_DEFAULT_SIZE;

    /* Done. */

    return parser;

    /* On error, free allocated buffers. */

error:

    if (!parser) return NULL;

    yaml_free(parser->simple_keys);
    yaml_free(parser->indents);
    yaml_free(parser->tokens);
    yaml_free(parser->buffer);
    yaml_free(parser->raw_buffer);

    yaml_free(parser);

    return NULL;
}

/*
 * Destroy a parser object.
 */

YAML_DECLARE(void)
yaml_parser_delete(yaml_parser_t *parser)
{
    assert(parser); /* Non-NULL parser object expected. */

    yaml_free(parser->simple_keys);
    yaml_free(parser->indents);
    yaml_free(parser->tokens);
    yaml_free(parser->buffer);
    yaml_free(parser->raw_buffer);

    memset(parser, 0, sizeof(yaml_parser_t));

    yaml_free(parser);
}

/*
 * String read handler.
 */

static int
yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
        size_t *size_read)
{
    yaml_string_input_t *input = data;

    if (input->current == input->end) {
        *size_read = 0;
        return 1;
    }

    if (size > (input->end - input->current)) {
        size = input->end - input->current;
    }

    memcpy(buffer, input->current, size);
    input->current += size;
    *size_read = size;
    return 1;
}

/*
 * File read handler.
 */

static int
yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
        size_t *size_read)
{
    *size_read = fread(buffer, 1, size, (FILE *)data);
    return !ferror((FILE *)data);
}

/*
 * Set a string input.
 */

YAML_DECLARE(void)
yaml_parser_set_input_string(yaml_parser_t *parser,
        unsigned char *input, size_t size)
{
    assert(parser); /* Non-NULL parser object expected. */
    assert(!parser->read_handler);  /* You can set the source only once. */
    assert(input);  /* Non-NULL input string expected. */

    parser->string_input.start = input;
    parser->string_input.current = input;
    parser->string_input.end = input+size;

    parser->read_handler = yaml_string_read_handler;
    parser->read_handler_data = &parser->string_input;
}

/*
 * Set a file input.
 */

YAML_DECLARE(void)
yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
{
    assert(parser); /* Non-NULL parser object expected. */
    assert(!parser->read_handler);  /* You can set the source only once. */
    assert(file);   /* Non-NULL file object expected. */

    parser->read_handler = yaml_file_read_handler;
    parser->read_handler_data = file;
}

/*
 * Set a generic input.
 */

YAML_DECLARE(void)
yaml_parser_set_input(yaml_parser_t *parser,
        yaml_read_handler_t *handler, void *data)
{
    assert(parser); /* Non-NULL parser object expected. */
    assert(!parser->read_handler);  /* You can set the source only once. */
    assert(handler);    /* Non-NULL read handler expected. */

    parser->read_handler = handler;
    parser->read_handler_data = data;
}

/*
 * Set the source encoding.
 */

YAML_DECLARE(void)
yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
{
    assert(parser); /* Non-NULL parser object expected. */
    assert(!parser->encoding); /* Encoding is already set or detected. */

    parser->encoding = encoding;
}

/*
 * Create a token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_token_new(yaml_token_type_t type,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_malloc(sizeof(yaml_token_t));

    if (!token) return NULL;

    memset(token, 0, sizeof(yaml_token_t));

    token->type = type;
    token->start_mark = start_mark;
    token->end_mark = end_mark;

    return token;
}

/*
 * Create a STREAM-START token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_stream_start_token_new(yaml_encoding_t encoding,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_STREAM_START_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.encoding = encoding;

    return token;
}

/*
 * Create a STREAM-END token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_stream_end_token_new(yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_STREAM_END_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    return token;
}

/*
 * Create a VERSION-DIRECTIVE token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_version_directive_token_new(int major, int minor,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_VERSION_DIRECTIVE_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.version_directive.major = major;
    token->data.version_directive.minor = minor;

    return token;
}

/*
 * Create a TAG-DIRECTIVE token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_tag_directive_token_new(yaml_char_t *handle, yaml_char_t *prefix,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_TAG_DIRECTIVE_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.tag_directive.handle = handle;
    token->data.tag_directive.prefix = prefix;

    return token;
}

/*
 * Create an ALIAS token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_alias_token_new(yaml_char_t *anchor,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_ALIAS_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.anchor = anchor;

    return token;
}

/*
 * Create an ANCHOR token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_anchor_token_new(yaml_char_t *anchor,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_ANCHOR_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.anchor = anchor;

    return token;
}

/*
 * Create a TAG token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_tag_token_new(yaml_char_t *handle, yaml_char_t *suffix,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_TAG_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.tag.handle = handle;
    token->data.tag.suffix = suffix;

    return token;
}

/*
 * Create a SCALAR token.
 */

YAML_DECLARE(yaml_token_t *)
yaml_scalar_token_new(yaml_char_t *value, size_t length,
        yaml_scalar_style_t style,
        yaml_mark_t start_mark, yaml_mark_t end_mark)
{
    yaml_token_t *token = yaml_token_new(YAML_SCALAR_TOKEN,
            start_mark, end_mark);

    if (!token) return NULL;

    token->data.scalar.value = value;
    token->data.scalar.length = length;
    token->data.scalar.style = style;

    return token;
}

/*
 * Destroy a token object.
 */

YAML_DECLARE(void)
yaml_token_delete(yaml_token_t *token)
{
    assert(token);  /* Non-NULL token object expected. */

    switch (token->type)
    {
        case YAML_TAG_DIRECTIVE_TOKEN:
            yaml_free(token->data.tag_directive.handle);
            yaml_free(token->data.tag_directive.prefix);
            break;

        case YAML_ALIAS_TOKEN:
        case YAML_ANCHOR_TOKEN:
            yaml_free(token->data.anchor);
            break;

        case YAML_TAG_TOKEN:
            yaml_free(token->data.tag.handle);
            yaml_free(token->data.tag.suffix);
            break;

        case YAML_SCALAR_TOKEN:
            yaml_free(token->data.scalar.value);
            break;
    }

    memset(token, 0, sizeof(yaml_token_t));

    yaml_free(token);
}