| [212] | 1 | |
|---|
| 2 | #include "yaml_private.h" |
|---|
| 3 | |
|---|
| 4 | /* |
|---|
| 5 | * API functions. |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | YAML_DECLARE(int) |
|---|
| 9 | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 10 | |
|---|
| 11 | YAML_DECLARE(int) |
|---|
| 12 | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
|---|
| 13 | yaml_encoding_t encoding); |
|---|
| 14 | |
|---|
| 15 | YAML_DECLARE(int) |
|---|
| 16 | yaml_emitter_emit_stream_end(yaml_emitter_t *emitter); |
|---|
| 17 | |
|---|
| 18 | YAML_DECLARE(int) |
|---|
| 19 | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
|---|
| 20 | yaml_version_directive_t *version_directive, |
|---|
| 21 | yaml_tag_directive_t *tag_directives_start, |
|---|
| 22 | yaml_tag_directive_t *tag_directives_end, |
|---|
| 23 | int implicit); |
|---|
| 24 | |
|---|
| 25 | YAML_DECLARE(int) |
|---|
| 26 | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, int implicit); |
|---|
| 27 | |
|---|
| 28 | YAML_DECLARE(int) |
|---|
| 29 | yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_char_t *anchor); |
|---|
| 30 | |
|---|
| 31 | YAML_DECLARE(int) |
|---|
| 32 | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, |
|---|
| 33 | yaml_char_t *anchor, yaml_char_t *tag, |
|---|
| 34 | yaml_char_t *value, size_t length, |
|---|
| 35 | int plain_implicit, int quoted_implicit, |
|---|
| 36 | yaml_scalar_style_t style); |
|---|
| 37 | |
|---|
| 38 | YAML_DECLARE(int) |
|---|
| 39 | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, |
|---|
| 40 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 41 | yaml_sequence_style_t style); |
|---|
| 42 | |
|---|
| 43 | YAML_DECLARE(int) |
|---|
| 44 | yaml_emitter_emit_sequence_end(yaml_emitter_t *emitter); |
|---|
| 45 | |
|---|
| 46 | YAML_DECLARE(int) |
|---|
| 47 | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, |
|---|
| 48 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 49 | yaml_mapping_style_t style); |
|---|
| 50 | |
|---|
| 51 | YAML_DECLARE(int) |
|---|
| 52 | yaml_emitter_emit_mapping_end(yaml_emitter_t *emitter); |
|---|
| 53 | |
|---|
| 54 | /* |
|---|
| 55 | * Emit STREAM-START. |
|---|
| 56 | */ |
|---|
| 57 | |
|---|
| 58 | YAML_DECLARE(int) |
|---|
| 59 | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
|---|
| 60 | yaml_encoding_t encoding) |
|---|
| 61 | { |
|---|
| 62 | yaml_event_t event; |
|---|
| 63 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 64 | |
|---|
| 65 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 66 | |
|---|
| 67 | STREAM_START_EVENT_INIT(event, encoding, mark, mark); |
|---|
| 68 | |
|---|
| 69 | return yaml_emitter_emit(emitter, &event); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /* |
|---|
| 73 | * Emit STREAM-END. |
|---|
| 74 | */ |
|---|
| 75 | |
|---|
| 76 | YAML_DECLARE(int) |
|---|
| 77 | yaml_emitter_emit_stream_end(yaml_emitter_t *emitter) |
|---|
| 78 | { |
|---|
| 79 | yaml_event_t event; |
|---|
| 80 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 81 | |
|---|
| 82 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 83 | |
|---|
| 84 | STREAM_END_EVENT_INIT(event, mark, mark); |
|---|
| 85 | |
|---|
| 86 | return yaml_emitter_emit(emitter, &event); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /* |
|---|
| 90 | * Emit DOCUMENT-START. |
|---|
| 91 | */ |
|---|
| 92 | |
|---|
| 93 | YAML_DECLARE(int) |
|---|
| 94 | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
|---|
| 95 | yaml_version_directive_t *version_directive, |
|---|
| 96 | yaml_tag_directive_t *tag_directives_start, |
|---|
| 97 | yaml_tag_directive_t *tag_directives_end, |
|---|
| 98 | int implicit) |
|---|
| 99 | { |
|---|
| 100 | yaml_event_t event; |
|---|
| 101 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 102 | yaml_version_directive_t *version_directive_copy = NULL; |
|---|
| 103 | struct { |
|---|
| 104 | yaml_tag_directive_t *start; |
|---|
| 105 | yaml_tag_directive_t *end; |
|---|
| 106 | yaml_tag_directive_t *top; |
|---|
| 107 | } tag_directives_copy = { NULL, NULL, NULL }; |
|---|
| 108 | yaml_tag_directive_t value = { NULL, NULL }; |
|---|
| 109 | |
|---|
| 110 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 111 | assert((tag_directives_start && tag_directives_end) || |
|---|
| 112 | (tag_directives_start == tag_directives_end)); |
|---|
| 113 | /* Valid tag directives are expected. */ |
|---|
| 114 | |
|---|
| 115 | if (version_directive) { |
|---|
| 116 | version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); |
|---|
| 117 | if (!version_directive_copy) { |
|---|
| 118 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 119 | goto error; |
|---|
| 120 | } |
|---|
| 121 | version_directive_copy->major = version_directive->major; |
|---|
| 122 | version_directive_copy->minor = version_directive->minor; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (tag_directives_start != tag_directives_end) { |
|---|
| 126 | yaml_tag_directive_t *tag_directive; |
|---|
| 127 | if (!STACK_INIT(emitter, tag_directives_copy, INITIAL_STACK_SIZE)) |
|---|
| 128 | goto error; |
|---|
| 129 | for (tag_directive = tag_directives_start; |
|---|
| 130 | tag_directive != tag_directives_end; tag_directive ++) { |
|---|
| 131 | value.handle = yaml_strdup(tag_directive->handle); |
|---|
| 132 | value.prefix = yaml_strdup(tag_directive->prefix); |
|---|
| 133 | if (!value.handle || !value.prefix) { |
|---|
| 134 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 135 | goto error; |
|---|
| 136 | } |
|---|
| 137 | if (!PUSH(emitter, tag_directives_copy, value)) |
|---|
| 138 | goto error; |
|---|
| 139 | value.handle = NULL; |
|---|
| 140 | value.prefix = NULL; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | DOCUMENT_START_EVENT_INIT(event, version_directive_copy, |
|---|
| 145 | tag_directives_copy.start, tag_directives_copy.end, |
|---|
| 146 | implicit, mark, mark); |
|---|
| 147 | |
|---|
| 148 | if (yaml_emitter_emit(emitter, &event)) { |
|---|
| 149 | return 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | error: |
|---|
| 153 | yaml_free(version_directive_copy); |
|---|
| 154 | while (!STACK_EMPTY(emitter, tag_directives_copy)) { |
|---|
| 155 | yaml_tag_directive_t value = POP(emitter, tag_directives_copy); |
|---|
| 156 | yaml_free(value.handle); |
|---|
| 157 | yaml_free(value.prefix); |
|---|
| 158 | } |
|---|
| 159 | STACK_DEL(emitter, tag_directives_copy); |
|---|
| 160 | yaml_free(value.handle); |
|---|
| 161 | yaml_free(value.prefix); |
|---|
| 162 | |
|---|
| 163 | return 0; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /* |
|---|
| 167 | * Emit DOCUMENT-END. |
|---|
| 168 | */ |
|---|
| 169 | |
|---|
| 170 | YAML_DECLARE(int) |
|---|
| 171 | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, int implicit) |
|---|
| 172 | { |
|---|
| 173 | yaml_event_t event; |
|---|
| 174 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 175 | |
|---|
| 176 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 177 | |
|---|
| 178 | DOCUMENT_END_EVENT_INIT(event, implicit, mark, mark); |
|---|
| 179 | |
|---|
| 180 | return yaml_emitter_emit(emitter, &event); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /* |
|---|
| 184 | * Emit ALIAS. |
|---|
| 185 | */ |
|---|
| 186 | |
|---|
| 187 | YAML_DECLARE(int) |
|---|
| 188 | yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_char_t *anchor) |
|---|
| 189 | { |
|---|
| 190 | yaml_event_t event; |
|---|
| 191 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 192 | yaml_char_t *anchor_copy = NULL; |
|---|
| 193 | |
|---|
| 194 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 195 | assert(anchor); /* Non-NULL anchor is expected. */ |
|---|
| 196 | |
|---|
| 197 | anchor_copy = yaml_strdup(anchor); |
|---|
| 198 | if (!anchor_copy) { |
|---|
| 199 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 200 | return 0; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | ALIAS_EVENT_INIT(event, anchor_copy, mark, mark); |
|---|
| 204 | |
|---|
| 205 | if (yaml_emitter_emit(emitter, &event)) { |
|---|
| 206 | return 1; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | yaml_free(anchor_copy); |
|---|
| 210 | |
|---|
| 211 | return 0; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /* |
|---|
| 215 | * Emit SCALAR. |
|---|
| 216 | */ |
|---|
| 217 | |
|---|
| 218 | YAML_DECLARE(int) |
|---|
| 219 | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, |
|---|
| 220 | yaml_char_t *anchor, yaml_char_t *tag, |
|---|
| 221 | yaml_char_t *value, size_t length, |
|---|
| 222 | int plain_implicit, int quoted_implicit, |
|---|
| 223 | yaml_scalar_style_t style) |
|---|
| 224 | { |
|---|
| 225 | yaml_event_t event; |
|---|
| 226 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 227 | yaml_char_t *anchor_copy = NULL; |
|---|
| 228 | yaml_char_t *tag_copy = NULL; |
|---|
| 229 | yaml_char_t *value_copy = NULL; |
|---|
| 230 | |
|---|
| 231 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 232 | assert(value); /* Non-NULL anchor is expected. */ |
|---|
| 233 | |
|---|
| 234 | if (anchor) { |
|---|
| 235 | anchor_copy = yaml_strdup(anchor); |
|---|
| 236 | if (!anchor_copy) { |
|---|
| 237 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 238 | goto error; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | if (tag) { |
|---|
| 243 | tag_copy = yaml_strdup(tag); |
|---|
| 244 | if (!tag_copy) { |
|---|
| 245 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 246 | goto error; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | value_copy = yaml_malloc(length+1); |
|---|
| 251 | if (!value_copy) { |
|---|
| 252 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 253 | goto error; |
|---|
| 254 | } |
|---|
| 255 | memcpy(value_copy, value, length); |
|---|
| 256 | value_copy[length] = '\0'; |
|---|
| 257 | |
|---|
| 258 | SCALAR_EVENT_INIT(event, anchor_copy, tag_copy, value_copy, length, |
|---|
| 259 | plain_implicit, quoted_implicit, style, mark, mark); |
|---|
| 260 | |
|---|
| 261 | if (yaml_emitter_emit(emitter, &event)) { |
|---|
| 262 | return 1; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | error: |
|---|
| 266 | yaml_free(anchor_copy); |
|---|
| 267 | yaml_free(tag_copy); |
|---|
| 268 | yaml_free(value_copy); |
|---|
| 269 | |
|---|
| 270 | return 0; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /* |
|---|
| 274 | * Emit SEQUENCE-START. |
|---|
| 275 | */ |
|---|
| 276 | |
|---|
| 277 | YAML_DECLARE(int) |
|---|
| 278 | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, |
|---|
| 279 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 280 | yaml_sequence_style_t style) |
|---|
| 281 | { |
|---|
| 282 | yaml_event_t event; |
|---|
| 283 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 284 | yaml_char_t *anchor_copy = NULL; |
|---|
| 285 | yaml_char_t *tag_copy = NULL; |
|---|
| 286 | |
|---|
| 287 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 288 | |
|---|
| 289 | if (anchor) { |
|---|
| 290 | anchor_copy = yaml_strdup(anchor); |
|---|
| 291 | if (!anchor_copy) { |
|---|
| 292 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 293 | goto error; |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | if (tag) { |
|---|
| 298 | tag_copy = yaml_strdup(tag); |
|---|
| 299 | if (!tag_copy) { |
|---|
| 300 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 301 | goto error; |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | SEQUENCE_START_EVENT_INIT(event, anchor_copy, tag_copy, |
|---|
| 306 | implicit, style, mark, mark); |
|---|
| 307 | |
|---|
| 308 | if (yaml_emitter_emit(emitter, &event)) { |
|---|
| 309 | return 1; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | error: |
|---|
| 313 | yaml_free(anchor_copy); |
|---|
| 314 | yaml_free(tag_copy); |
|---|
| 315 | |
|---|
| 316 | return 0; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | /* |
|---|
| 320 | * Emit SEQUENCE-END. |
|---|
| 321 | */ |
|---|
| 322 | |
|---|
| 323 | YAML_DECLARE(int) |
|---|
| 324 | yaml_emitter_emit_sequence_end(yaml_emitter_t *emitter) |
|---|
| 325 | { |
|---|
| 326 | yaml_event_t event; |
|---|
| 327 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 328 | |
|---|
| 329 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 330 | |
|---|
| 331 | SEQUENCE_END_EVENT_INIT(event, mark, mark); |
|---|
| 332 | |
|---|
| 333 | return yaml_emitter_emit(emitter, &event); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | /* |
|---|
| 337 | * Emit MAPPING-START. |
|---|
| 338 | */ |
|---|
| 339 | |
|---|
| 340 | YAML_DECLARE(int) |
|---|
| 341 | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, |
|---|
| 342 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, |
|---|
| 343 | yaml_mapping_style_t style) |
|---|
| 344 | { |
|---|
| 345 | yaml_event_t event; |
|---|
| 346 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 347 | yaml_char_t *anchor_copy = NULL; |
|---|
| 348 | yaml_char_t *tag_copy = NULL; |
|---|
| 349 | |
|---|
| 350 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 351 | |
|---|
| 352 | if (anchor) { |
|---|
| 353 | anchor_copy = yaml_strdup(anchor); |
|---|
| 354 | if (!anchor_copy) { |
|---|
| 355 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 356 | goto error; |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | if (tag) { |
|---|
| 361 | tag_copy = yaml_strdup(tag); |
|---|
| 362 | if (!tag_copy) { |
|---|
| 363 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 364 | goto error; |
|---|
| 365 | } |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | MAPPING_START_EVENT_INIT(event, anchor_copy, tag_copy, |
|---|
| 369 | implicit, style, mark, mark); |
|---|
| 370 | |
|---|
| 371 | if (yaml_emitter_emit(emitter, &event)) { |
|---|
| 372 | return 1; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | error: |
|---|
| 376 | yaml_free(anchor_copy); |
|---|
| 377 | yaml_free(tag_copy); |
|---|
| 378 | |
|---|
| 379 | return 0; |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | /* |
|---|
| 383 | * Emit MAPPING-END. |
|---|
| 384 | */ |
|---|
| 385 | |
|---|
| 386 | YAML_DECLARE(int) |
|---|
| 387 | yaml_emitter_emit_mapping_end(yaml_emitter_t *emitter) |
|---|
| 388 | { |
|---|
| 389 | yaml_event_t event; |
|---|
| 390 | yaml_mark_t mark = { 0, 0, 0 }; |
|---|
| 391 | |
|---|
| 392 | assert(emitter); /* Non-NULL emitter object is expected. */ |
|---|
| 393 | |
|---|
| 394 | MAPPING_END_EVENT_INIT(event, mark, mark); |
|---|
| 395 | |
|---|
| 396 | return yaml_emitter_emit(emitter, &event); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /* |
|---|
| 400 | * Emit an event. |
|---|
| 401 | */ |
|---|
| 402 | |
|---|
| 403 | YAML_DECLARE(int) |
|---|
| 404 | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 405 | { |
|---|
| 406 | return 0; |
|---|
| 407 | } |
|---|
| 408 | |
|---|