| 1 | |
|---|
| 2 | #include "yaml_private.h" |
|---|
| 3 | |
|---|
| 4 | /* |
|---|
| 5 | * Get the library version. |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | YAML_DECLARE(const char *) |
|---|
| 9 | yaml_get_version_string(void) |
|---|
| 10 | { |
|---|
| 11 | return YAML_VERSION_STRING; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | /* |
|---|
| 15 | * Get the library version numbers. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | YAML_DECLARE(void) |
|---|
| 19 | yaml_get_version(int *major, int *minor, int *patch) |
|---|
| 20 | { |
|---|
| 21 | *major = YAML_VERSION_MAJOR; |
|---|
| 22 | *minor = YAML_VERSION_MINOR; |
|---|
| 23 | *patch = YAML_VERSION_PATCH; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | /* |
|---|
| 27 | * Allocate a dynamic memory block. |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | YAML_DECLARE(void *) |
|---|
| 31 | yaml_malloc(size_t size) |
|---|
| 32 | { |
|---|
| 33 | return malloc(size ? size : 1); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /* |
|---|
| 37 | * Reallocate a dynamic memory block. |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | YAML_DECLARE(void *) |
|---|
| 41 | yaml_realloc(void *ptr, size_t size) |
|---|
| 42 | { |
|---|
| 43 | return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /* |
|---|
| 47 | * Free a dynamic memory block. |
|---|
| 48 | */ |
|---|
| 49 | |
|---|
| 50 | YAML_DECLARE(void) |
|---|
| 51 | yaml_free(void *ptr) |
|---|
| 52 | { |
|---|
| 53 | if (ptr) free(ptr); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* |
|---|
| 57 | * Duplicate a string. |
|---|
| 58 | */ |
|---|
| 59 | |
|---|
| 60 | YAML_DECLARE(yaml_char_t *) |
|---|
| 61 | yaml_strdup(const yaml_char_t *str) |
|---|
| 62 | { |
|---|
| 63 | if (!str) |
|---|
| 64 | return NULL; |
|---|
| 65 | |
|---|
| 66 | return (yaml_char_t *)strdup((char *)str); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /* |
|---|
| 70 | * Extend a string. |
|---|
| 71 | */ |
|---|
| 72 | |
|---|
| 73 | YAML_DECLARE(int) |
|---|
| 74 | yaml_string_extend(yaml_char_t **start, |
|---|
| 75 | yaml_char_t **pointer, yaml_char_t **end) |
|---|
| 76 | { |
|---|
| 77 | void *new_start = yaml_realloc(*start, (*end - *start)*2); |
|---|
| 78 | |
|---|
| 79 | if (!new_start) return 0; |
|---|
| 80 | |
|---|
| 81 | memset(new_start + (*end - *start), 0, *end - *start); |
|---|
| 82 | |
|---|
| 83 | *pointer = new_start + (*pointer - *start); |
|---|
| 84 | *end = new_start + (*end - *start)*2; |
|---|
| 85 | *start = new_start; |
|---|
| 86 | |
|---|
| 87 | return 1; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /* |
|---|
| 91 | * Append a string B to a string A. |
|---|
| 92 | */ |
|---|
| 93 | |
|---|
| 94 | YAML_DECLARE(int) |
|---|
| 95 | yaml_string_join( |
|---|
| 96 | yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, |
|---|
| 97 | yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end) |
|---|
| 98 | { |
|---|
| 99 | if (*b_start == *b_pointer) |
|---|
| 100 | return 1; |
|---|
| 101 | |
|---|
| 102 | while (*a_end - *a_pointer <= *b_pointer - *b_start) { |
|---|
| 103 | if (!yaml_string_extend(a_start, a_pointer, a_end)) |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | memcpy(*a_pointer, *b_start, *b_pointer - *b_start); |
|---|
| 108 | *a_pointer += *b_pointer - *b_start; |
|---|
| 109 | |
|---|
| 110 | return 1; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /* |
|---|
| 114 | * Extend a stack. |
|---|
| 115 | */ |
|---|
| 116 | |
|---|
| 117 | YAML_DECLARE(int) |
|---|
| 118 | yaml_stack_extend(void **start, void **top, void **end) |
|---|
| 119 | { |
|---|
| 120 | void *new_start = yaml_realloc(*start, (*end - *start)*2); |
|---|
| 121 | |
|---|
| 122 | if (!new_start) return 0; |
|---|
| 123 | |
|---|
| 124 | *top = new_start + (*top - *start); |
|---|
| 125 | *end = new_start + (*end - *start)*2; |
|---|
| 126 | *start = new_start; |
|---|
| 127 | |
|---|
| 128 | return 1; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /* |
|---|
| 132 | * Extend or move a queue. |
|---|
| 133 | */ |
|---|
| 134 | |
|---|
| 135 | YAML_DECLARE(int) |
|---|
| 136 | yaml_queue_extend(void **start, void **head, void **tail, void **end) |
|---|
| 137 | { |
|---|
| 138 | /* Check if we need to resize the queue. */ |
|---|
| 139 | |
|---|
| 140 | if (*start == *head && *tail == *end) { |
|---|
| 141 | void *new_start = yaml_realloc(*start, (*end - *start)*2); |
|---|
| 142 | |
|---|
| 143 | if (!new_start) return 0; |
|---|
| 144 | |
|---|
| 145 | *head = new_start + (*head - *start); |
|---|
| 146 | *tail = new_start + (*tail - *start); |
|---|
| 147 | *end = new_start + (*end - *start)*2; |
|---|
| 148 | *start = new_start; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /* Check if we need to move the queue at the beginning of the buffer. */ |
|---|
| 152 | |
|---|
| 153 | if (*tail == *end) { |
|---|
| 154 | if (*head != *tail) { |
|---|
| 155 | memmove(*start, *head, *tail - *head); |
|---|
| 156 | } |
|---|
| 157 | *tail -= *head - *start; |
|---|
| 158 | *head = *start; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | return 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | /* |
|---|
| 166 | * Create a new parser object. |
|---|
| 167 | */ |
|---|
| 168 | |
|---|
| 169 | YAML_DECLARE(int) |
|---|
| 170 | yaml_parser_initialize(yaml_parser_t *parser) |
|---|
| 171 | { |
|---|
| 172 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 173 | |
|---|
| 174 | memset(parser, 0, sizeof(yaml_parser_t)); |
|---|
| 175 | if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE)) |
|---|
| 176 | goto error; |
|---|
| 177 | if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) |
|---|
| 178 | goto error; |
|---|
| 179 | if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE)) |
|---|
| 180 | goto error; |
|---|
| 181 | if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE)) |
|---|
| 182 | goto error; |
|---|
| 183 | if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE)) |
|---|
| 184 | goto error; |
|---|
| 185 | if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE)) |
|---|
| 186 | goto error; |
|---|
| 187 | if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE)) |
|---|
| 188 | goto error; |
|---|
| 189 | if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE)) |
|---|
| 190 | goto error; |
|---|
| 191 | |
|---|
| 192 | return 1; |
|---|
| 193 | |
|---|
| 194 | error: |
|---|
| 195 | |
|---|
| 196 | BUFFER_DEL(parser, parser->raw_buffer); |
|---|
| 197 | BUFFER_DEL(parser, parser->buffer); |
|---|
| 198 | QUEUE_DEL(parser, parser->tokens); |
|---|
| 199 | STACK_DEL(parser, parser->indents); |
|---|
| 200 | STACK_DEL(parser, parser->simple_keys); |
|---|
| 201 | STACK_DEL(parser, parser->states); |
|---|
| 202 | STACK_DEL(parser, parser->marks); |
|---|
| 203 | STACK_DEL(parser, parser->tag_directives); |
|---|
| 204 | |
|---|
| 205 | return 0; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /* |
|---|
| 209 | * Destroy a parser object. |
|---|
| 210 | */ |
|---|
| 211 | |
|---|
| 212 | YAML_DECLARE(void) |
|---|
| 213 | yaml_parser_delete(yaml_parser_t *parser) |
|---|
| 214 | { |
|---|
| 215 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 216 | |
|---|
| 217 | BUFFER_DEL(parser, parser->raw_buffer); |
|---|
| 218 | BUFFER_DEL(parser, parser->buffer); |
|---|
| 219 | while (!QUEUE_EMPTY(parser, parser->tokens)) { |
|---|
| 220 | yaml_token_delete(&DEQUEUE(parser, parser->tokens)); |
|---|
| 221 | } |
|---|
| 222 | QUEUE_DEL(parser, parser->tokens); |
|---|
| 223 | STACK_DEL(parser, parser->indents); |
|---|
| 224 | STACK_DEL(parser, parser->simple_keys); |
|---|
| 225 | STACK_DEL(parser, parser->states); |
|---|
| 226 | STACK_DEL(parser, parser->marks); |
|---|
| 227 | while (!STACK_EMPTY(parser, parser->tag_directives)) { |
|---|
| 228 | yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); |
|---|
| 229 | yaml_free(tag_directive.handle); |
|---|
| 230 | yaml_free(tag_directive.prefix); |
|---|
| 231 | } |
|---|
| 232 | STACK_DEL(parser, parser->tag_directives); |
|---|
| 233 | |
|---|
| 234 | memset(parser, 0, sizeof(yaml_parser_t)); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /* |
|---|
| 238 | * String read handler. |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | static int |
|---|
| 242 | yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, |
|---|
| 243 | size_t *size_read) |
|---|
| 244 | { |
|---|
| 245 | yaml_parser_t *parser = data; |
|---|
| 246 | |
|---|
| 247 | if (parser->input.string.current == parser->input.string.end) { |
|---|
| 248 | *size_read = 0; |
|---|
| 249 | return 1; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | if (size > (parser->input.string.end - parser->input.string.current)) { |
|---|
| 253 | size = parser->input.string.end - parser->input.string.current; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | memcpy(buffer, parser->input.string.current, size); |
|---|
| 257 | parser->input.string.current += size; |
|---|
| 258 | *size_read = size; |
|---|
| 259 | return 1; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /* |
|---|
| 263 | * File read handler. |
|---|
| 264 | */ |
|---|
| 265 | |
|---|
| 266 | static int |
|---|
| 267 | yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, |
|---|
| 268 | size_t *size_read) |
|---|
| 269 | { |
|---|
| 270 | yaml_parser_t *parser = data; |
|---|
| 271 | |
|---|
| 272 | *size_read = fread(buffer, 1, size, parser->input.file); |
|---|
| 273 | return !ferror(parser->input.file); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /* |
|---|
| 277 | * Set a string input. |
|---|
| 278 | */ |
|---|
| 279 | |
|---|
| 280 | YAML_DECLARE(void) |
|---|
| 281 | yaml_parser_set_input_string(yaml_parser_t *parser, |
|---|
| 282 | const unsigned char *input, size_t size) |
|---|
| 283 | { |
|---|
| 284 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 285 | assert(!parser->read_handler); /* You can set the source only once. */ |
|---|
| 286 | assert(input); /* Non-NULL input string expected. */ |
|---|
| 287 | |
|---|
| 288 | parser->read_handler = yaml_string_read_handler; |
|---|
| 289 | parser->read_handler_data = parser; |
|---|
| 290 | |
|---|
| 291 | parser->input.string.start = input; |
|---|
| 292 | parser->input.string.current = input; |
|---|
| 293 | parser->input.string.end = input+size; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /* |
|---|
| 297 | * Set a file input. |
|---|
| 298 | */ |
|---|
| 299 | |
|---|
| 300 | YAML_DECLARE(void) |
|---|
| 301 | yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) |
|---|
| 302 | { |
|---|
| 303 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 304 | assert(!parser->read_handler); /* You can set the source only once. */ |
|---|
| 305 | assert(file); /* Non-NULL file object expected. */ |
|---|
| 306 | |
|---|
| 307 | parser->read_handler = yaml_file_read_handler; |
|---|
| 308 | parser->read_handler_data = parser; |
|---|
| 309 | |
|---|
| 310 | parser->input.file = file; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /* |
|---|
| 314 | * Set a generic input. |
|---|
| 315 | */ |
|---|
| 316 | |
|---|
| 317 | YAML_DECLARE(void) |
|---|
| 318 | yaml_parser_set_input(yaml_parser_t *parser, |
|---|
| 319 | yaml_read_handler_t *handler, void *data) |
|---|
| 320 | { |
|---|
| 321 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 322 | assert(!parser->read_handler); /* You can set the source only once. */ |
|---|
| 323 | assert(handler); /* Non-NULL read handler expected. */ |
|---|
| 324 | |
|---|
| 325 | parser->read_handler = handler; |
|---|
| 326 | parser->read_handler_data = data; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /* |
|---|
| 330 | * Set the source encoding. |
|---|
| 331 | */ |
|---|
| 332 | |
|---|
| 333 | YAML_DECLARE(void) |
|---|
| 334 | yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) |
|---|
| 335 | { |
|---|
| 336 | assert(parser); /* Non-NULL parser object expected. */ |
|---|
| 337 | assert(!parser->encoding); /* Encoding is already set or detected. */ |
|---|
| 338 | |
|---|
| 339 | parser->encoding = encoding; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | /* |
|---|
| 343 | * Create a new emitter object. |
|---|
| 344 | */ |
|---|
| 345 | |
|---|
| 346 | YAML_DECLARE(int) |
|---|
| 347 | yaml_emitter_initialize(yaml_emitter_t *emitter) |
|---|
| 348 | { |
|---|
| 349 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 350 | |
|---|
| 351 | memset(emitter, 0, sizeof(yaml_emitter_t)); |
|---|
| 352 | if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE)) |
|---|
| 353 | goto error; |
|---|
| 354 | if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) |
|---|
| 355 | goto error; |
|---|
| 356 | if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE)) |
|---|
| 357 | goto error; |
|---|
| 358 | if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE)) |
|---|
| 359 | goto error; |
|---|
| 360 | if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE)) |
|---|
| 361 | goto error; |
|---|
| 362 | if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE)) |
|---|
| 363 | goto error; |
|---|
| 364 | |
|---|
| 365 | return 1; |
|---|
| 366 | |
|---|
| 367 | error: |
|---|
| 368 | |
|---|
| 369 | BUFFER_DEL(emitter, emitter->buffer); |
|---|
| 370 | BUFFER_DEL(emitter, emitter->raw_buffer); |
|---|
| 371 | STACK_DEL(emitter, emitter->states); |
|---|
| 372 | QUEUE_DEL(emitter, emitter->events); |
|---|
| 373 | STACK_DEL(emitter, emitter->indents); |
|---|
| 374 | STACK_DEL(emitter, emitter->tag_directives); |
|---|
| 375 | |
|---|
| 376 | return 0; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | /* |
|---|
| 380 | * Destroy an emitter object. |
|---|
| 381 | */ |
|---|
| 382 | |
|---|
| 383 | YAML_DECLARE(void) |
|---|
| 384 | yaml_emitter_delete(yaml_emitter_t *emitter) |
|---|
| 385 | { |
|---|
| 386 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 387 | |
|---|
| 388 | BUFFER_DEL(emitter, emitter->buffer); |
|---|
| 389 | BUFFER_DEL(emitter, emitter->raw_buffer); |
|---|
| 390 | STACK_DEL(emitter, emitter->states); |
|---|
| 391 | while (!QUEUE_EMPTY(emitter, emitter->events)) { |
|---|
| 392 | yaml_event_delete(&DEQUEUE(emitter, emitter->events)); |
|---|
| 393 | } |
|---|
| 394 | QUEUE_DEL(emitter, emitter->events); |
|---|
| 395 | STACK_DEL(emitter, emitter->indents); |
|---|
| 396 | while (!STACK_EMPTY(empty, emitter->tag_directives)) { |
|---|
| 397 | yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); |
|---|
| 398 | yaml_free(tag_directive.handle); |
|---|
| 399 | yaml_free(tag_directive.prefix); |
|---|
| 400 | } |
|---|
| 401 | STACK_DEL(emitter, emitter->tag_directives); |
|---|
| 402 | |
|---|
| 403 | memset(emitter, 0, sizeof(yaml_emitter_t)); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | /* |
|---|
| 407 | * String write handler. |
|---|
| 408 | */ |
|---|
| 409 | |
|---|
| 410 | static int |
|---|
| 411 | yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) |
|---|
| 412 | { |
|---|
| 413 | yaml_emitter_t *emitter = data; |
|---|
| 414 | |
|---|
| 415 | if (emitter->output.string.size + *emitter->output.string.size_written |
|---|
| 416 | < size) { |
|---|
| 417 | memcpy(emitter->output.string.buffer |
|---|
| 418 | + *emitter->output.string.size_written, |
|---|
| 419 | buffer, |
|---|
| 420 | emitter->output.string.size |
|---|
| 421 | - *emitter->output.string.size_written); |
|---|
| 422 | *emitter->output.string.size_written = emitter->output.string.size; |
|---|
| 423 | return 0; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | memcpy(emitter->output.string.buffer |
|---|
| 427 | + *emitter->output.string.size_written, buffer, size); |
|---|
| 428 | *emitter->output.string.size_written += size; |
|---|
| 429 | return 1; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | /* |
|---|
| 433 | * File write handler. |
|---|
| 434 | */ |
|---|
| 435 | |
|---|
| 436 | static int |
|---|
| 437 | yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) |
|---|
| 438 | { |
|---|
| 439 | yaml_emitter_t *emitter = data; |
|---|
| 440 | |
|---|
| 441 | return (fwrite(buffer, 1, size, emitter->output.file) == size); |
|---|
| 442 | } |
|---|
| 443 | /* |
|---|
| 444 | * Set a string output. |
|---|
| 445 | */ |
|---|
| 446 | |
|---|
| 447 | YAML_DECLARE(void) |
|---|
| 448 | yaml_emitter_set_output_string(yaml_emitter_t *emitter, |
|---|
| 449 | unsigned char *output, size_t size, size_t *size_written) |
|---|
| 450 | { |
|---|
| 451 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 452 | assert(!emitter->write_handler); /* You can set the output only once. */ |
|---|
| 453 | assert(output); /* Non-NULL output string expected. */ |
|---|
| 454 | |
|---|
| 455 | emitter->write_handler = yaml_string_write_handler; |
|---|
| 456 | emitter->write_handler_data = emitter; |
|---|
| 457 | |
|---|
| 458 | emitter->output.string.buffer = output; |
|---|
| 459 | emitter->output.string.size = size; |
|---|
| 460 | emitter->output.string.size_written = size_written; |
|---|
| 461 | *size_written = 0; |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /* |
|---|
| 465 | * Set a file output. |
|---|
| 466 | */ |
|---|
| 467 | |
|---|
| 468 | YAML_DECLARE(void) |
|---|
| 469 | yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file) |
|---|
| 470 | { |
|---|
| 471 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 472 | assert(!emitter->write_handler); /* You can set the output only once. */ |
|---|
| 473 | assert(file); /* Non-NULL file object expected. */ |
|---|
| 474 | |
|---|
| 475 | emitter->write_handler = yaml_file_write_handler; |
|---|
| 476 | emitter->write_handler_data = emitter; |
|---|
| 477 | |
|---|
| 478 | emitter->output.file = file; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | /* |
|---|
| 482 | * Set a generic output handler. |
|---|
| 483 | */ |
|---|
| 484 | |
|---|
| 485 | YAML_DECLARE(void) |
|---|
| 486 | yaml_emitter_set_output(yaml_emitter_t *emitter, |
|---|
| 487 | yaml_write_handler_t *handler, void *data) |
|---|
| 488 | { |
|---|
| 489 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 490 | assert(!emitter->write_handler); /* You can set the output only once. */ |
|---|
| 491 | assert(handler); /* Non-NULL handler object expected. */ |
|---|
| 492 | |
|---|
| 493 | emitter->write_handler = handler; |
|---|
| 494 | emitter->write_handler_data = data; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | /* |
|---|
| 498 | * Set the output encoding. |
|---|
| 499 | */ |
|---|
| 500 | |
|---|
| 501 | YAML_DECLARE(void) |
|---|
| 502 | yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding) |
|---|
| 503 | { |
|---|
| 504 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 505 | assert(!emitter->encoding); /* You can set encoding only once. */ |
|---|
| 506 | |
|---|
| 507 | emitter->encoding = encoding; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | /* |
|---|
| 511 | * Set the canonical output style. |
|---|
| 512 | */ |
|---|
| 513 | |
|---|
| 514 | YAML_DECLARE(void) |
|---|
| 515 | yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical) |
|---|
| 516 | { |
|---|
| 517 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 518 | |
|---|
| 519 | emitter->canonical = (canonical != 0); |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | /* |
|---|
| 523 | * Set the indentation increment. |
|---|
| 524 | */ |
|---|
| 525 | |
|---|
| 526 | YAML_DECLARE(void) |
|---|
| 527 | yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) |
|---|
| 528 | { |
|---|
| 529 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 530 | |
|---|
| 531 | emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | /* |
|---|
| 535 | * Set the preferred line width. |
|---|
| 536 | */ |
|---|
| 537 | |
|---|
| 538 | YAML_DECLARE(void) |
|---|
| 539 | yaml_emitter_set_width(yaml_emitter_t *emitter, int width) |
|---|
| 540 | { |
|---|
| 541 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 542 | |
|---|
| 543 | emitter->best_width = (width >= 0) ? width : -1; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | /* |
|---|
| 547 | * Set if unescaped non-ASCII characters are allowed. |
|---|
| 548 | */ |
|---|
| 549 | |
|---|
| 550 | YAML_DECLARE(void) |
|---|
| 551 | yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode) |
|---|
| 552 | { |
|---|
| 553 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 554 | |
|---|
| 555 | emitter->unicode = (unicode != 0); |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | /* |
|---|
| 559 | * Set the preferred line break character. |
|---|
| 560 | */ |
|---|
| 561 | |
|---|
| 562 | YAML_DECLARE(void) |
|---|
| 563 | yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break) |
|---|
| 564 | { |
|---|
| 565 | assert(emitter); /* Non-NULL emitter object expected. */ |
|---|
| 566 | |
|---|
| 567 | emitter->line_break = line_break; |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | /* |
|---|
| 571 | * Destroy a token object. |
|---|
| 572 | */ |
|---|
| 573 | |
|---|
| 574 | YAML_DECLARE(void) |
|---|
| 575 | yaml_token_delete(yaml_token_t *token) |
|---|
| 576 | { |
|---|
| 577 | assert(token); /* Non-NULL token object expected. */ |
|---|
| 578 | |
|---|
| 579 | switch (token->type) |
|---|
| 580 | { |
|---|
| 581 | case YAML_TAG_DIRECTIVE_TOKEN: |
|---|
| 582 | yaml_free(token->data.tag_directive.handle); |
|---|
| 583 | yaml_free(token->data.tag_directive.prefix); |
|---|
| 584 | break; |
|---|
| 585 | |
|---|
| 586 | case YAML_ALIAS_TOKEN: |
|---|
| 587 | yaml_free(token->data.alias.value); |
|---|
| 588 | break; |
|---|
| 589 | |
|---|
| 590 | case YAML_ANCHOR_TOKEN: |
|---|
| 591 | yaml_free(token->data.anchor.value); |
|---|
| 592 | break; |
|---|
| 593 | |
|---|
| 594 | case YAML_TAG_TOKEN: |
|---|
| 595 | yaml_free(token->data.tag.handle); |
|---|
| 596 | yaml_free(token->data.tag.suffix); |
|---|
| 597 | break; |
|---|
| 598 | |
|---|
| 599 | case YAML_SCALAR_TOKEN: |
|---|
| 600 | yaml_free(token->data.scalar.value); |
|---|
| 601 | break; |
|---|
| 602 | |
|---|
| 603 | default: |
|---|
| 604 | break; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | memset(token, 0, sizeof(yaml_token_t)); |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | /* |
|---|
| 611 | * Check if a string is a valid UTF-8 sequence. |
|---|
| 612 | * |
|---|
| 613 | * Check 'reader.c' for more details on UTF-8 encoding. |
|---|
| 614 | */ |
|---|
| 615 | |
|---|
| 616 | static int |
|---|
| 617 | yaml_check_utf8(yaml_char_t *start, size_t length) |
|---|
| 618 | { |
|---|
| 619 | yaml_char_t *end = start+length; |
|---|
| 620 | yaml_char_t *pointer = start; |
|---|
| 621 | |
|---|
| 622 | while (pointer < end) { |
|---|
| 623 | unsigned char octet; |
|---|
| 624 | unsigned int width; |
|---|
| 625 | unsigned int value; |
|---|
| 626 | int k; |
|---|
| 627 | |
|---|
| 628 | octet = pointer[0]; |
|---|
| 629 | width = (octet & 0x80) == 0x00 ? 1 : |
|---|
| 630 | (octet & 0xE0) == 0xC0 ? 2 : |
|---|
| 631 | (octet & 0xF0) == 0xE0 ? 3 : |
|---|
| 632 | (octet & 0xF8) == 0xF0 ? 4 : 0; |
|---|
| 633 | value = (octet & 0x80) == 0x00 ? octet & 0x7F : |
|---|
| 634 | (octet & 0xE0) == 0xC0 ? octet & 0x1F : |
|---|
| 635 | (octet & 0xF0) == 0xE0 ? octet & 0x0F : |
|---|
| 636 | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; |
|---|
| 637 | if (!width) return 0; |
|---|
| 638 | if (pointer+width > end) return 0; |
|---|
| 639 | for (k = 1; k < width; k ++) { |
|---|
| 640 | octet = pointer[k]; |
|---|
| 641 | if ((octet & 0xC0) != 0x80) return 0; |
|---|
| 642 | value = (value << 6) + (octet & 0x3F); |
|---|
| 643 | } |
|---|
| 644 | if (!((width == 1) || |
|---|
| 645 | (width == 2 && value >= 0x80) || |
|---|
| 646 | (width == 3 && value >= 0x800) || |
|---|
| 647 | (width == 4 && value >= 0x10000))) return 0; |
|---|
| 648 | |
|---|
| 649 | pointer += width; |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | return 1; |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | /* |
|---|
| 656 | * Create STREAM-START. |
|---|
| 657 | */ |
|---|
| 658 | |
|---|
| 659 | YAML_DECLARE(int) |
|---|
| 660 | yaml_stream_start_event_initialize(yaml_event_t *event, |
|---|
| 661 | yaml_encoding_t encoding) |
|---|
| 662 | { |
|---|
| 663 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 664 | |
|---|
| 665 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 666 | |
|---|
| 667 | STREAM_START_EVENT_INIT(*event, encoding, mark, mark); |
|---|
| 668 | |
|---|
| 669 | return 1; |
|---|
| 670 | } |
|---|
| 671 | |
|---|
| 672 | /* |
|---|
| 673 | * Create STREAM-END. |
|---|
| 674 | */ |
|---|
| 675 | |
|---|
| 676 | YAML_DECLARE(int) |
|---|
| 677 | yaml_stream_end_event_initialize(yaml_event_t *event) |
|---|
| 678 | { |
|---|
| 679 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 680 | |
|---|
| 681 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 682 | |
|---|
| 683 | STREAM_END_EVENT_INIT(*event, mark, mark); |
|---|
| 684 | |
|---|
| 685 | return 1; |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | /* |
|---|
| 689 | * Create DOCUMENT-START. |
|---|
| 690 | */ |
|---|
| 691 | |
|---|
| 692 | YAML_DECLARE(int) |
|---|
| 693 | yaml_document_start_event_initialize(yaml_event_t *event, |
|---|
| 694 | yaml_version_directive_t *version_directive, |
|---|
| 695 | yaml_tag_directive_t *tag_directives_start, |
|---|
| 696 | yaml_tag_directive_t *tag_directives_end, |
|---|
| 697 | int implicit) |
|---|
| 698 | { |
|---|
| 699 | struct { |
|---|
| 700 | yaml_error_type_t error; |
|---|
| 701 | } context; |
|---|
| 702 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 703 | yaml_version_directive_t *version_directive_copy = NULL; |
|---|
| 704 | struct { |
|---|
| 705 | yaml_tag_directive_t *start; |
|---|
| 706 | yaml_tag_directive_t *end; |
|---|
| 707 | yaml_tag_directive_t *top; |
|---|
| 708 | } tag_directives_copy = { NULL, NULL, NULL }; |
|---|
| 709 | yaml_tag_directive_t value = { NULL, NULL }; |
|---|
| 710 | |
|---|
| 711 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 712 | assert((tag_directives_start && tag_directives_end) || |
|---|
| 713 | (tag_directives_start == tag_directives_end)); |
|---|
| 714 | /* Valid tag directives are expected. */ |
|---|
| 715 | |
|---|
| 716 | if (version_directive) { |
|---|
| 717 | version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); |
|---|
| 718 | if (!version_directive_copy) goto error; |
|---|
| 719 | version_directive_copy->major = version_directive->major; |
|---|
| 720 | version_directive_copy->minor = version_directive->minor; |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | if (tag_directives_start != tag_directives_end) { |
|---|
| 724 | yaml_tag_directive_t *tag_directive; |
|---|
| 725 | if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) |
|---|
| 726 | goto error; |
|---|
| 727 | for (tag_directive = tag_directives_start; |
|---|
| 728 | tag_directive != tag_directives_end; tag_directive ++) { |
|---|
| 729 | assert(tag_directive->handle); |
|---|
| 730 | assert(tag_directive->prefix); |
|---|
| 731 | if (!yaml_check_utf8(tag_directive->handle, |
|---|
| 732 | strlen((char *)tag_directive->handle))) |
|---|
| 733 | goto error; |
|---|
| 734 | if (!yaml_check_utf8(tag_directive->prefix, |
|---|
| 735 | strlen((char *)tag_directive->prefix))) |
|---|
| 736 | goto error; |
|---|
| 737 | value.handle = yaml_strdup(tag_directive->handle); |
|---|
| 738 | value.prefix = yaml_strdup(tag_directive->prefix); |
|---|
| 739 | if (!value.handle || !value.prefix) goto error; |
|---|
| 740 | if (!PUSH(&context, tag_directives_copy, value)) |
|---|
| 741 | goto error; |
|---|
| 742 | value.handle = NULL; |
|---|
| 743 | value.prefix = NULL; |
|---|
| 744 | } |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | DOCUMENT_START_EVENT_INIT(*event, version_directive_copy, |
|---|
| 748 | tag_directives_copy.start, tag_directives_copy.top, |
|---|
| 749 | implicit, mark, mark); |
|---|
| 750 | |
|---|
| 751 | return 1; |
|---|
| 752 | |
|---|
| 753 | error: |
|---|
| 754 | yaml_free(version_directive_copy); |
|---|
| 755 | while (!STACK_EMPTY(context, tag_directives_copy)) { |
|---|
| 756 | yaml_tag_directive_t value = POP(context, tag_directives_copy); |
|---|
| 757 | yaml_free(value.handle); |
|---|
| 758 | yaml_free(value.prefix); |
|---|
| 759 | } |
|---|
| 760 | STACK_DEL(context, tag_directives_copy); |
|---|
| 761 | yaml_free(value.handle); |
|---|
| 762 | yaml_free(value.prefix); |
|---|
| 763 | |
|---|
| 764 | return 0; |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | /* |
|---|
| 768 | * Create DOCUMENT-END. |
|---|
| 769 | */ |
|---|
| 770 | |
|---|
| 771 | YAML_DECLARE(int) |
|---|
| 772 | yaml_document_end_event_initialize(yaml_event_t *event, int implicit) |
|---|
| 773 | { |
|---|
| 774 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 775 | |
|---|
| 776 | assert(event); /* Non-NULL emitter object is expected. */ |
|---|
| 777 | |
|---|
| 778 | DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark); |
|---|
| 779 | |
|---|
| 780 | return 1; |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | /* |
|---|
| 784 | * Create ALIAS. |
|---|
| 785 | */ |
|---|
| 786 | |
|---|
| 787 | YAML_DECLARE(int) |
|---|
| 788 | yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor) |
|---|
| 789 | { |
|---|
| 790 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 791 | yaml_char_t *anchor_copy = NULL; |
|---|
| 792 | |
|---|
| 793 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 794 | assert(anchor); /* Non-NULL anchor is expected. */ |
|---|
| 795 | |
|---|
| 796 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0; |
|---|
| 797 | |
|---|
| 798 | anchor_copy = yaml_strdup(anchor); |
|---|
| 799 | if (!anchor_copy) |
|---|
| 800 | return 0; |
|---|
| 801 | |
|---|
| 802 | ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark); |
|---|
| 803 | |
|---|
| 804 | return 1; |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | /* |
|---|
| 808 | * Create SCALAR. |
|---|
| 809 | */ |
|---|
| 810 | |
|---|
| 811 | YAML_DECLARE(int) |
|---|
| 812 | yaml_scalar_event_initialize(yaml_event_t *event, |
|---|
| 813 | yaml_char_t *anchor, yaml_char_t *tag, |
|---|
| 814 | yaml_char_t *value, int length, |
|---|
| 815 | int plain_implicit, int quoted_implicit, |
|---|
| 816 | yaml_scalar_style_t style) |
|---|
| 817 | { |
|---|
| 818 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 819 | yaml_char_t *anchor_copy = NULL; |
|---|
| 820 | yaml_char_t *tag_copy = NULL; |
|---|
| 821 | yaml_char_t *value_copy = NULL; |
|---|
| 822 | |
|---|
| 823 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 824 | assert(value); /* Non-NULL anchor is expected. */ |
|---|
| 825 | |
|---|
| 826 | if (anchor) { |
|---|
| 827 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
|---|
| 828 | anchor_copy = yaml_strdup(anchor); |
|---|
| 829 | if (!anchor_copy) goto error; |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | if (tag) { |
|---|
| 833 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 834 | tag_copy = yaml_strdup(tag); |
|---|
| 835 | if (!tag_copy) goto error; |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | if (length < 0) { |
|---|
| 839 | length = strlen((char *)value); |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | if (!yaml_check_utf8(value, length)) goto error; |
|---|
| 843 | value_copy = yaml_malloc(length+1); |
|---|
| 844 | if (!value_copy) goto error; |
|---|
| 845 | memcpy(value_copy, value, length); |
|---|
| 846 | value_copy[length] = '\0'; |
|---|
| 847 | |
|---|
| 848 | SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length, |
|---|
| 849 | plain_implicit, quoted_implicit, style, mark, mark); |
|---|
| 850 | |
|---|
| 851 | return 1; |
|---|
| 852 | |
|---|
| 853 | error: |
|---|
| 854 | yaml_free(anchor_copy); |
|---|
| 855 | yaml_free(tag_copy); |
|---|
| 856 | yaml_free(value_copy); |
|---|
| 857 | |
|---|
| 858 | return 0; |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | /* |
|---|
| 862 | * Create SEQUENCE-START. |
|---|
| 863 | */ |
|---|
| 864 | |
|---|
| 865 | YAML_DECLARE(int) |
|---|
| 866 | yaml_sequence_start_event_initialize(yaml_event_t *event, |
|---|
| 867 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 868 | yaml_sequence_style_t style) |
|---|
| 869 | { |
|---|
| 870 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 871 | yaml_char_t *anchor_copy = NULL; |
|---|
| 872 | yaml_char_t *tag_copy = NULL; |
|---|
| 873 | |
|---|
| 874 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 875 | |
|---|
| 876 | if (anchor) { |
|---|
| 877 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
|---|
| 878 | anchor_copy = yaml_strdup(anchor); |
|---|
| 879 | if (!anchor_copy) goto error; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | if (tag) { |
|---|
| 883 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 884 | tag_copy = yaml_strdup(tag); |
|---|
| 885 | if (!tag_copy) goto error; |
|---|
| 886 | } |
|---|
| 887 | |
|---|
| 888 | SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy, |
|---|
| 889 | implicit, style, mark, mark); |
|---|
| 890 | |
|---|
| 891 | return 1; |
|---|
| 892 | |
|---|
| 893 | error: |
|---|
| 894 | yaml_free(anchor_copy); |
|---|
| 895 | yaml_free(tag_copy); |
|---|
| 896 | |
|---|
| 897 | return 0; |
|---|
| 898 | } |
|---|
| 899 | |
|---|
| 900 | /* |
|---|
| 901 | * Create SEQUENCE-END. |
|---|
| 902 | */ |
|---|
| 903 | |
|---|
| 904 | YAML_DECLARE(int) |
|---|
| 905 | yaml_sequence_end_event_initialize(yaml_event_t *event) |
|---|
| 906 | { |
|---|
| 907 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 908 | |
|---|
| 909 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 910 | |
|---|
| 911 | SEQUENCE_END_EVENT_INIT(*event, mark, mark); |
|---|
| 912 | |
|---|
| 913 | return 1; |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | /* |
|---|
| 917 | * Create MAPPING-START. |
|---|
| 918 | */ |
|---|
| 919 | |
|---|
| 920 | YAML_DECLARE(int) |
|---|
| 921 | yaml_mapping_start_event_initialize(yaml_event_t *event, |
|---|
| 922 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 923 | yaml_mapping_style_t style) |
|---|
| 924 | { |
|---|
| 925 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 926 | yaml_char_t *anchor_copy = NULL; |
|---|
| 927 | yaml_char_t *tag_copy = NULL; |
|---|
| 928 | |
|---|
| 929 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 930 | |
|---|
| 931 | if (anchor) { |
|---|
| 932 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
|---|
| 933 | anchor_copy = yaml_strdup(anchor); |
|---|
| 934 | if (!anchor_copy) goto error; |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | if (tag) { |
|---|
| 938 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 939 | tag_copy = yaml_strdup(tag); |
|---|
| 940 | if (!tag_copy) goto error; |
|---|
| 941 | } |
|---|
| 942 | |
|---|
| 943 | MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy, |
|---|
| 944 | implicit, style, mark, mark); |
|---|
| 945 | |
|---|
| 946 | return 1; |
|---|
| 947 | |
|---|
| 948 | error: |
|---|
| 949 | yaml_free(anchor_copy); |
|---|
| 950 | yaml_free(tag_copy); |
|---|
| 951 | |
|---|
| 952 | return 0; |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | /* |
|---|
| 956 | * Create MAPPING-END. |
|---|
| 957 | */ |
|---|
| 958 | |
|---|
| 959 | YAML_DECLARE(int) |
|---|
| 960 | yaml_mapping_end_event_initialize(yaml_event_t *event) |
|---|
| 961 | { |
|---|
| 962 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 963 | |
|---|
| 964 | assert(event); /* Non-NULL event object is expected. */ |
|---|
| 965 | |
|---|
| 966 | MAPPING_END_EVENT_INIT(*event, mark, mark); |
|---|
| 967 | |
|---|
| 968 | return 1; |
|---|
| 969 | } |
|---|
| 970 | |
|---|
| 971 | /* |
|---|
| 972 | * Destroy an event object. |
|---|
| 973 | */ |
|---|
| 974 | |
|---|
| 975 | YAML_DECLARE(void) |
|---|
| 976 | yaml_event_delete(yaml_event_t *event) |
|---|
| 977 | { |
|---|
| 978 | yaml_tag_directive_t *tag_directive; |
|---|
| 979 | |
|---|
| 980 | assert(event); /* Non-NULL event object expected. */ |
|---|
| 981 | |
|---|
| 982 | switch (event->type) |
|---|
| 983 | { |
|---|
| 984 | case YAML_DOCUMENT_START_EVENT: |
|---|
| 985 | yaml_free(event->data.document_start.version_directive); |
|---|
| 986 | for (tag_directive = event->data.document_start.tag_directives.start; |
|---|
| 987 | tag_directive != event->data.document_start.tag_directives.end; |
|---|
| 988 | tag_directive++) { |
|---|
| 989 | yaml_free(tag_directive->handle); |
|---|
| 990 | yaml_free(tag_directive->prefix); |
|---|
| 991 | } |
|---|
| 992 | yaml_free(event->data.document_start.tag_directives.start); |
|---|
| 993 | break; |
|---|
| 994 | |
|---|
| 995 | case YAML_ALIAS_EVENT: |
|---|
| 996 | yaml_free(event->data.alias.anchor); |
|---|
| 997 | break; |
|---|
| 998 | |
|---|
| 999 | case YAML_SCALAR_EVENT: |
|---|
| 1000 | yaml_free(event->data.scalar.anchor); |
|---|
| 1001 | yaml_free(event->data.scalar.tag); |
|---|
| 1002 | yaml_free(event->data.scalar.value); |
|---|
| 1003 | break; |
|---|
| 1004 | |
|---|
| 1005 | case YAML_SEQUENCE_START_EVENT: |
|---|
| 1006 | yaml_free(event->data.sequence_start.anchor); |
|---|
| 1007 | yaml_free(event->data.sequence_start.tag); |
|---|
| 1008 | break; |
|---|
| 1009 | |
|---|
| 1010 | case YAML_MAPPING_START_EVENT: |
|---|
| 1011 | yaml_free(event->data.mapping_start.anchor); |
|---|
| 1012 | yaml_free(event->data.mapping_start.tag); |
|---|
| 1013 | break; |
|---|
| 1014 | |
|---|
| 1015 | default: |
|---|
| 1016 | break; |
|---|
| 1017 | } |
|---|
| 1018 | |
|---|
| 1019 | memset(event, 0, sizeof(yaml_event_t)); |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | #if 0 |
|---|
| 1023 | |
|---|
| 1024 | /* |
|---|
| 1025 | * Create a SCALAR node. |
|---|
| 1026 | */ |
|---|
| 1027 | |
|---|
| 1028 | YAML_DECLARE(int) |
|---|
| 1029 | yaml_scalar_node_initialize(yaml_node_t *node, |
|---|
| 1030 | yaml_char_t *tag, yaml_char_t *value, int length, |
|---|
| 1031 | yaml_scalar_style_t style) |
|---|
| 1032 | { |
|---|
| 1033 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 1034 | yaml_char_t *tag_copy = NULL; |
|---|
| 1035 | yaml_char_t *value_copy = NULL; |
|---|
| 1036 | |
|---|
| 1037 | assert(node); /* Non-NULL node object is expected. */ |
|---|
| 1038 | assert(value); /* Non-NULL anchor is expected. */ |
|---|
| 1039 | |
|---|
| 1040 | if (!tag) { |
|---|
| 1041 | tag = YAML_DEFAULT_SCALAR_TAG; |
|---|
| 1042 | } |
|---|
| 1043 | |
|---|
| 1044 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 1045 | tag_copy = yaml_strdup(tag); |
|---|
| 1046 | if (!tag_copy) goto error; |
|---|
| 1047 | |
|---|
| 1048 | if (length < 0) { |
|---|
| 1049 | length = strlen((char *)value); |
|---|
| 1050 | } |
|---|
| 1051 | |
|---|
| 1052 | if (!yaml_check_utf8(value, length)) goto error; |
|---|
| 1053 | value_copy = yaml_malloc(length+1); |
|---|
| 1054 | if (!value_copy) goto error; |
|---|
| 1055 | memcpy(value_copy, value, length); |
|---|
| 1056 | value_copy[length] = '\0'; |
|---|
| 1057 | |
|---|
| 1058 | SCALAR_NODE_INIT(*node, tag_copy, value_copy, length, style, mark, mark); |
|---|
| 1059 | |
|---|
| 1060 | return 1; |
|---|
| 1061 | |
|---|
| 1062 | error: |
|---|
| 1063 | yaml_free(tag_copy); |
|---|
| 1064 | yaml_free(value_copy); |
|---|
| 1065 | |
|---|
| 1066 | return 0; |
|---|
| 1067 | } |
|---|
| 1068 | |
|---|
| 1069 | /* |
|---|
| 1070 | * Create a SEQUENCE node. |
|---|
| 1071 | */ |
|---|
| 1072 | |
|---|
| 1073 | YAML_DECLARE(int) |
|---|
| 1074 | yaml_sequence_node_initialize(yaml_node_t *node, |
|---|
| 1075 | yaml_char_t *tag, yaml_sequence_style_t style) |
|---|
| 1076 | { |
|---|
| 1077 | struct { |
|---|
| 1078 | yaml_error_type_t error; |
|---|
| 1079 | } context; |
|---|
| 1080 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 1081 | yaml_char_t *tag_copy = NULL; |
|---|
| 1082 | struct { |
|---|
| 1083 | yaml_node_item_t *start; |
|---|
| 1084 | yaml_node_item_t *end; |
|---|
| 1085 | yaml_node_item_t *top; |
|---|
| 1086 | } items = { NULL, NULL, NULL }; |
|---|
| 1087 | |
|---|
| 1088 | assert(node); /* Non-NULL node object is expected. */ |
|---|
| 1089 | |
|---|
| 1090 | if (!tag) { |
|---|
| 1091 | tag = YAML_DEFAULT_SEQUENCE_TAG; |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | if (tag) { |
|---|
| 1095 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 1096 | tag_copy = yaml_strdup(tag); |
|---|
| 1097 | if (!tag_copy) goto error; |
|---|
| 1098 | } |
|---|
| 1099 | |
|---|
| 1100 | if (!STACK_INIT(context, items, INITIAL_STACK_SIZE)) goto error; |
|---|
| 1101 | |
|---|
| 1102 | SEQUENCE_NODE_INIT(*node, tag_copy, items.start, item.end, style, |
|---|
| 1103 | mark, mark); |
|---|
| 1104 | |
|---|
| 1105 | return 1; |
|---|
| 1106 | |
|---|
| 1107 | error: |
|---|
| 1108 | yaml_free(tag_copy); |
|---|
| 1109 | STACK_DEL(context, items); |
|---|
| 1110 | |
|---|
| 1111 | return 0; |
|---|
| 1112 | } |
|---|
| 1113 | |
|---|
| 1114 | /* |
|---|
| 1115 | * Create a MAPPING node. |
|---|
| 1116 | */ |
|---|
| 1117 | |
|---|
| 1118 | YAML_DECLARE(int) |
|---|
| 1119 | yaml_mapping_node_initialize(yaml_node_t *node, |
|---|
| 1120 | yaml_char_t *tag, yaml_mapping_style_t style) |
|---|
| 1121 | { |
|---|
| 1122 | struct { |
|---|
| 1123 | yaml_error_type_t error; |
|---|
| 1124 | } context; |
|---|
| 1125 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 1126 | yaml_char_t *tag_copy = NULL; |
|---|
| 1127 | struct { |
|---|
| 1128 | yaml_node_pair_t *start; |
|---|
| 1129 | yaml_node_pair_t *end; |
|---|
| 1130 | yaml_node_pair_t *top; |
|---|
| 1131 | } pairs = { NULL, NULL, NULL }; |
|---|
| 1132 | |
|---|
| 1133 | assert(node); /* Non-NULL node object is expected. */ |
|---|
| 1134 | |
|---|
| 1135 | if (!tag) { |
|---|
| 1136 | tag = YAML_DEFAULT_MAPPING_TAG; |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | if (tag) { |
|---|
| 1140 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
|---|
| 1141 | tag_copy = yaml_strdup(tag); |
|---|
| 1142 | if (!tag_copy) goto error; |
|---|
| 1143 | } |
|---|
| 1144 | |
|---|
| 1145 | if (!STACK_INIT(context, pairs, INITIAL_STACK_SIZE)) goto error; |
|---|
| 1146 | |
|---|
| 1147 | MAPPING_NODE_INIT(*node, tag_copy, pairs.start, pairs.end, style, |
|---|
| 1148 | mark, mark); |
|---|
| 1149 | |
|---|
| 1150 | return 1; |
|---|
| 1151 | |
|---|
| 1152 | error: |
|---|
| 1153 | yaml_free(tag_copy); |
|---|
| 1154 | STACK_DEL(context, pairs); |
|---|
| 1155 | |
|---|
| 1156 | return 0; |
|---|
| 1157 | } |
|---|
| 1158 | |
|---|
| 1159 | /* |
|---|
| 1160 | * Delete a node and its subnodes. |
|---|
| 1161 | */ |
|---|
| 1162 | |
|---|
| 1163 | YAML_DECLARE(void) |
|---|
| 1164 | yaml_node_delete(yaml_node_t *node) |
|---|
| 1165 | { |
|---|
| 1166 | struct { |
|---|
| 1167 | yaml_error_type_t error; |
|---|
| 1168 | } context; |
|---|
| 1169 | struct { |
|---|
| 1170 | yaml_node_item_t *start; |
|---|
| 1171 | yaml_node_item_t *end; |
|---|
| 1172 | yaml_node_item_t *head; |
|---|
| 1173 | yaml_node_item_t *tail; |
|---|
| 1174 | } queue = { NULL, NULL, NULL, NULL }; |
|---|
| 1175 | |
|---|
| 1176 | assert(node); /* Non-NULL node object is expected. */ |
|---|
| 1177 | |
|---|
| 1178 | if (node->type == YAML_SCALAR_NODE) { |
|---|
| 1179 | yaml_free(node->data.scalar.tag); |
|---|
| 1180 | yaml_free(node->data.scalar.value); |
|---|
| 1181 | memset(node, 0, sizeof(yaml_node_t)); |
|---|
| 1182 | return; |
|---|
| 1183 | } |
|---|
| 1184 | |
|---|
| 1185 | if (!QUEUE_INIT(context, queue, INITIAL_QUEUE_SIZE)) goto error; |
|---|
| 1186 | if (!ENQUEUE(context, queue, node)) goto error; |
|---|
| 1187 | |
|---|
| 1188 | while (!QUEUE_EMPTY(context, queue)) { |
|---|
| 1189 | yaml_node_t node = DEQUEUE(context, queue); |
|---|
| 1190 | if (node.type == YAML_SCALAR_NODE) { |
|---|
| 1191 | if (!node->reference) |
|---|
| 1192 | } |
|---|
| 1193 | if (node->type == YAML_SEQUENCE_NODE) { |
|---|
| 1194 | while (!STACK_EMPTY(context, node->data.sequence.items)) { |
|---|
| 1195 | yaml_node_t *item = |
|---|
| 1196 | } |
|---|
| 1197 | } |
|---|
| 1198 | } |
|---|
| 1199 | } |
|---|
| 1200 | |
|---|
| 1201 | #endif |
|---|
| 1202 | |
|---|