Ruby
2.0.0p247(2013-06-27revision41674)
|
00001 00002 #include "yaml_private.h" 00003 00004 /* 00005 * Flush the buffer if needed. 00006 */ 00007 00008 #define FLUSH(emitter) \ 00009 ((emitter->buffer.pointer+5 < emitter->buffer.end) \ 00010 || yaml_emitter_flush(emitter)) 00011 00012 /* 00013 * Put a character to the output buffer. 00014 */ 00015 00016 #define PUT(emitter,value) \ 00017 (FLUSH(emitter) \ 00018 && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ 00019 emitter->column ++, \ 00020 1)) 00021 00022 /* 00023 * Put a line break to the output buffer. 00024 */ 00025 00026 #define PUT_BREAK(emitter) \ 00027 (FLUSH(emitter) \ 00028 && ((emitter->line_break == YAML_CR_BREAK ? \ 00029 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ 00030 emitter->line_break == YAML_LN_BREAK ? \ 00031 (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ 00032 emitter->line_break == YAML_CRLN_BREAK ? \ 00033 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \ 00034 *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \ 00035 emitter->column = 0, \ 00036 emitter->line ++, \ 00037 1)) 00038 00039 /* 00040 * Copy a character from a string into buffer. 00041 */ 00042 00043 #define WRITE(emitter,string) \ 00044 (FLUSH(emitter) \ 00045 && (COPY(emitter->buffer,string), \ 00046 emitter->column ++, \ 00047 1)) 00048 00049 /* 00050 * Copy a line break character from a string into buffer. 00051 */ 00052 00053 #define WRITE_BREAK(emitter,string) \ 00054 (FLUSH(emitter) \ 00055 && (CHECK(string,'\n') ? \ 00056 (PUT_BREAK(emitter), \ 00057 string.pointer ++, \ 00058 1) : \ 00059 (COPY(emitter->buffer,string), \ 00060 emitter->column = 0, \ 00061 emitter->line ++, \ 00062 1))) 00063 00064 /* 00065 * API functions. 00066 */ 00067 00068 YAML_DECLARE(int) 00069 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); 00070 00071 /* 00072 * Utility functions. 00073 */ 00074 00075 static int 00076 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem); 00077 00078 static int 00079 yaml_emitter_need_more_events(yaml_emitter_t *emitter); 00080 00081 static int 00082 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, 00083 yaml_tag_directive_t value, int allow_duplicates); 00084 00085 static int 00086 yaml_emitter_increase_indent(yaml_emitter_t *emitter, 00087 int flow, int indentless); 00088 00089 /* 00090 * State functions. 00091 */ 00092 00093 static int 00094 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event); 00095 00096 static int 00097 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, 00098 yaml_event_t *event); 00099 00100 static int 00101 yaml_emitter_emit_document_start(yaml_emitter_t *emitter, 00102 yaml_event_t *event, int first); 00103 00104 static int 00105 yaml_emitter_emit_document_content(yaml_emitter_t *emitter, 00106 yaml_event_t *event); 00107 00108 static int 00109 yaml_emitter_emit_document_end(yaml_emitter_t *emitter, 00110 yaml_event_t *event); 00111 00112 static int 00113 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, 00114 yaml_event_t *event, int first); 00115 00116 static int 00117 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, 00118 yaml_event_t *event, int first); 00119 00120 static int 00121 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, 00122 yaml_event_t *event, int simple); 00123 00124 static int 00125 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, 00126 yaml_event_t *event, int first); 00127 00128 static int 00129 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, 00130 yaml_event_t *event, int first); 00131 00132 static int 00133 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, 00134 yaml_event_t *event, int simple); 00135 00136 static int 00137 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, 00138 int root, int sequence, int mapping, int simple_key); 00139 00140 static int 00141 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); 00142 00143 static int 00144 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event); 00145 00146 static int 00147 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event); 00148 00149 static int 00150 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event); 00151 00152 /* 00153 * Checkers. 00154 */ 00155 00156 static int 00157 yaml_emitter_check_empty_document(yaml_emitter_t *emitter); 00158 00159 static int 00160 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter); 00161 00162 static int 00163 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter); 00164 00165 static int 00166 yaml_emitter_check_simple_key(yaml_emitter_t *emitter); 00167 00168 static int 00169 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event); 00170 00171 /* 00172 * Processors. 00173 */ 00174 00175 static int 00176 yaml_emitter_process_anchor(yaml_emitter_t *emitter); 00177 00178 static int 00179 yaml_emitter_process_tag(yaml_emitter_t *emitter); 00180 00181 static int 00182 yaml_emitter_process_scalar(yaml_emitter_t *emitter); 00183 00184 /* 00185 * Analyzers. 00186 */ 00187 00188 static int 00189 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, 00190 yaml_version_directive_t version_directive); 00191 00192 static int 00193 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, 00194 yaml_tag_directive_t tag_directive); 00195 00196 static int 00197 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, 00198 yaml_char_t *anchor, int alias); 00199 00200 static int 00201 yaml_emitter_analyze_tag(yaml_emitter_t *emitter, 00202 yaml_char_t *tag); 00203 00204 static int 00205 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, 00206 yaml_char_t *value, size_t length); 00207 00208 static int 00209 yaml_emitter_analyze_event(yaml_emitter_t *emitter, 00210 yaml_event_t *event); 00211 00212 /* 00213 * Writers. 00214 */ 00215 00216 static int 00217 yaml_emitter_write_bom(yaml_emitter_t *emitter); 00218 00219 static int 00220 yaml_emitter_write_indent(yaml_emitter_t *emitter); 00221 00222 static int 00223 yaml_emitter_write_indicator(yaml_emitter_t *emitter, 00224 const char *indicator, int need_whitespace, 00225 int is_whitespace, int is_indention); 00226 00227 static int 00228 yaml_emitter_write_anchor(yaml_emitter_t *emitter, 00229 yaml_char_t *value, size_t length); 00230 00231 static int 00232 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, 00233 yaml_char_t *value, size_t length); 00234 00235 static int 00236 yaml_emitter_write_tag_content(yaml_emitter_t *emitter, 00237 yaml_char_t *value, size_t length, int need_whitespace); 00238 00239 static int 00240 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, 00241 yaml_char_t *value, size_t length, int allow_breaks); 00242 00243 static int 00244 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, 00245 yaml_char_t *value, size_t length, int allow_breaks); 00246 00247 static int 00248 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, 00249 yaml_char_t *value, size_t length, int allow_breaks); 00250 00251 static int 00252 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, 00253 yaml_string_t string); 00254 00255 static int 00256 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, 00257 yaml_char_t *value, size_t length); 00258 00259 static int 00260 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, 00261 yaml_char_t *value, size_t length); 00262 00263 /* 00264 * Set an emitter error and return 0. 00265 */ 00266 00267 static int 00268 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem) 00269 { 00270 emitter->error = YAML_EMITTER_ERROR; 00271 emitter->problem = problem; 00272 00273 return 0; 00274 } 00275 00276 /* 00277 * Emit an event. 00278 */ 00279 00280 YAML_DECLARE(int) 00281 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) 00282 { 00283 if (!ENQUEUE(emitter, emitter->events, *event)) { 00284 yaml_event_delete(event); 00285 return 0; 00286 } 00287 00288 while (!yaml_emitter_need_more_events(emitter)) { 00289 if (!yaml_emitter_analyze_event(emitter, emitter->events.head)) 00290 return 0; 00291 if (!yaml_emitter_state_machine(emitter, emitter->events.head)) 00292 return 0; 00293 yaml_event_delete(&DEQUEUE(emitter, emitter->events)); 00294 } 00295 00296 return 1; 00297 } 00298 00299 /* 00300 * Check if we need to accumulate more events before emitting. 00301 * 00302 * We accumulate extra 00303 * - 1 event for DOCUMENT-START 00304 * - 2 events for SEQUENCE-START 00305 * - 3 events for MAPPING-START 00306 */ 00307 00308 static int 00309 yaml_emitter_need_more_events(yaml_emitter_t *emitter) 00310 { 00311 int level = 0; 00312 int accumulate = 0; 00313 yaml_event_t *event; 00314 00315 if (QUEUE_EMPTY(emitter, emitter->events)) 00316 return 1; 00317 00318 switch (emitter->events.head->type) { 00319 case YAML_DOCUMENT_START_EVENT: 00320 accumulate = 1; 00321 break; 00322 case YAML_SEQUENCE_START_EVENT: 00323 accumulate = 2; 00324 break; 00325 case YAML_MAPPING_START_EVENT: 00326 accumulate = 3; 00327 break; 00328 default: 00329 return 0; 00330 } 00331 00332 if (emitter->events.tail - emitter->events.head > accumulate) 00333 return 0; 00334 00335 for (event = emitter->events.head; event != emitter->events.tail; event ++) { 00336 switch (event->type) { 00337 case YAML_STREAM_START_EVENT: 00338 case YAML_DOCUMENT_START_EVENT: 00339 case YAML_SEQUENCE_START_EVENT: 00340 case YAML_MAPPING_START_EVENT: 00341 level += 1; 00342 break; 00343 case YAML_STREAM_END_EVENT: 00344 case YAML_DOCUMENT_END_EVENT: 00345 case YAML_SEQUENCE_END_EVENT: 00346 case YAML_MAPPING_END_EVENT: 00347 level -= 1; 00348 break; 00349 default: 00350 break; 00351 } 00352 if (!level) 00353 return 0; 00354 } 00355 00356 return 1; 00357 } 00358 00359 /* 00360 * Append a directive to the directives stack. 00361 */ 00362 00363 static int 00364 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, 00365 yaml_tag_directive_t value, int allow_duplicates) 00366 { 00367 yaml_tag_directive_t *tag_directive; 00368 yaml_tag_directive_t copy = { NULL, NULL }; 00369 00370 for (tag_directive = emitter->tag_directives.start; 00371 tag_directive != emitter->tag_directives.top; tag_directive ++) { 00372 if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { 00373 if (allow_duplicates) 00374 return 1; 00375 return yaml_emitter_set_emitter_error(emitter, 00376 "duplicate %TAG directive"); 00377 } 00378 } 00379 00380 copy.handle = yaml_strdup(value.handle); 00381 copy.prefix = yaml_strdup(value.prefix); 00382 if (!copy.handle || !copy.prefix) { 00383 emitter->error = YAML_MEMORY_ERROR; 00384 goto error; 00385 } 00386 00387 if (!PUSH(emitter, emitter->tag_directives, copy)) 00388 goto error; 00389 00390 return 1; 00391 00392 error: 00393 yaml_free(copy.handle); 00394 yaml_free(copy.prefix); 00395 return 0; 00396 } 00397 00398 /* 00399 * Increase the indentation level. 00400 */ 00401 00402 static int 00403 yaml_emitter_increase_indent(yaml_emitter_t *emitter, 00404 int flow, int indentless) 00405 { 00406 if (!PUSH(emitter, emitter->indents, emitter->indent)) 00407 return 0; 00408 00409 if (emitter->indent < 0) { 00410 emitter->indent = flow ? emitter->best_indent : 0; 00411 } 00412 else if (!indentless) { 00413 emitter->indent += emitter->best_indent; 00414 } 00415 00416 return 1; 00417 } 00418 00419 /* 00420 * State dispatcher. 00421 */ 00422 00423 static int 00424 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) 00425 { 00426 switch (emitter->state) 00427 { 00428 case YAML_EMIT_STREAM_START_STATE: 00429 return yaml_emitter_emit_stream_start(emitter, event); 00430 00431 case YAML_EMIT_FIRST_DOCUMENT_START_STATE: 00432 return yaml_emitter_emit_document_start(emitter, event, 1); 00433 00434 case YAML_EMIT_DOCUMENT_START_STATE: 00435 return yaml_emitter_emit_document_start(emitter, event, 0); 00436 00437 case YAML_EMIT_DOCUMENT_CONTENT_STATE: 00438 return yaml_emitter_emit_document_content(emitter, event); 00439 00440 case YAML_EMIT_DOCUMENT_END_STATE: 00441 return yaml_emitter_emit_document_end(emitter, event); 00442 00443 case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: 00444 return yaml_emitter_emit_flow_sequence_item(emitter, event, 1); 00445 00446 case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE: 00447 return yaml_emitter_emit_flow_sequence_item(emitter, event, 0); 00448 00449 case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: 00450 return yaml_emitter_emit_flow_mapping_key(emitter, event, 1); 00451 00452 case YAML_EMIT_FLOW_MAPPING_KEY_STATE: 00453 return yaml_emitter_emit_flow_mapping_key(emitter, event, 0); 00454 00455 case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: 00456 return yaml_emitter_emit_flow_mapping_value(emitter, event, 1); 00457 00458 case YAML_EMIT_FLOW_MAPPING_VALUE_STATE: 00459 return yaml_emitter_emit_flow_mapping_value(emitter, event, 0); 00460 00461 case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: 00462 return yaml_emitter_emit_block_sequence_item(emitter, event, 1); 00463 00464 case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE: 00465 return yaml_emitter_emit_block_sequence_item(emitter, event, 0); 00466 00467 case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: 00468 return yaml_emitter_emit_block_mapping_key(emitter, event, 1); 00469 00470 case YAML_EMIT_BLOCK_MAPPING_KEY_STATE: 00471 return yaml_emitter_emit_block_mapping_key(emitter, event, 0); 00472 00473 case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: 00474 return yaml_emitter_emit_block_mapping_value(emitter, event, 1); 00475 00476 case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE: 00477 return yaml_emitter_emit_block_mapping_value(emitter, event, 0); 00478 00479 case YAML_EMIT_END_STATE: 00480 return yaml_emitter_set_emitter_error(emitter, 00481 "expected nothing after STREAM-END"); 00482 00483 default: 00484 assert(1); /* Invalid state. */ 00485 } 00486 00487 return 0; 00488 } 00489 00490 /* 00491 * Expect STREAM-START. 00492 */ 00493 00494 static int 00495 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, 00496 yaml_event_t *event) 00497 { 00498 if (event->type == YAML_STREAM_START_EVENT) 00499 { 00500 if (!emitter->encoding) { 00501 emitter->encoding = event->data.stream_start.encoding; 00502 } 00503 00504 if (!emitter->encoding) { 00505 emitter->encoding = YAML_UTF8_ENCODING; 00506 } 00507 00508 if (emitter->best_indent < 2 || emitter->best_indent > 9) { 00509 emitter->best_indent = 2; 00510 } 00511 00512 if (emitter->best_width >= 0 00513 && emitter->best_width <= emitter->best_indent*2) { 00514 emitter->best_width = 80; 00515 } 00516 00517 if (emitter->best_width < 0) { 00518 emitter->best_width = INT_MAX; 00519 } 00520 00521 if (!emitter->line_break) { 00522 emitter->line_break = YAML_LN_BREAK; 00523 } 00524 00525 emitter->indent = -1; 00526 00527 emitter->line = 0; 00528 emitter->column = 0; 00529 emitter->whitespace = 1; 00530 emitter->indention = 1; 00531 00532 if (emitter->encoding != YAML_UTF8_ENCODING) { 00533 if (!yaml_emitter_write_bom(emitter)) 00534 return 0; 00535 } 00536 00537 emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE; 00538 00539 return 1; 00540 } 00541 00542 return yaml_emitter_set_emitter_error(emitter, 00543 "expected STREAM-START"); 00544 } 00545 00546 /* 00547 * Expect DOCUMENT-START or STREAM-END. 00548 */ 00549 00550 static int 00551 yaml_emitter_emit_document_start(yaml_emitter_t *emitter, 00552 yaml_event_t *event, int first) 00553 { 00554 if (event->type == YAML_DOCUMENT_START_EVENT) 00555 { 00556 yaml_tag_directive_t default_tag_directives[] = { 00557 {(yaml_char_t *)"!", (yaml_char_t *)"!"}, 00558 {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"}, 00559 {NULL, NULL} 00560 }; 00561 yaml_tag_directive_t *tag_directive; 00562 int implicit; 00563 00564 if (event->data.document_start.version_directive) { 00565 if (!yaml_emitter_analyze_version_directive(emitter, 00566 *event->data.document_start.version_directive)) 00567 return 0; 00568 } 00569 00570 for (tag_directive = event->data.document_start.tag_directives.start; 00571 tag_directive != event->data.document_start.tag_directives.end; 00572 tag_directive ++) { 00573 if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive)) 00574 return 0; 00575 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) 00576 return 0; 00577 } 00578 00579 for (tag_directive = default_tag_directives; 00580 tag_directive->handle; tag_directive ++) { 00581 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1)) 00582 return 0; 00583 } 00584 00585 implicit = event->data.document_start.implicit; 00586 if (!first || emitter->canonical) { 00587 implicit = 0; 00588 } 00589 00590 if ((event->data.document_start.version_directive || 00591 (event->data.document_start.tag_directives.start 00592 != event->data.document_start.tag_directives.end)) && 00593 emitter->open_ended) 00594 { 00595 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) 00596 return 0; 00597 if (!yaml_emitter_write_indent(emitter)) 00598 return 0; 00599 } 00600 00601 if (event->data.document_start.version_directive) { 00602 implicit = 0; 00603 if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) 00604 return 0; 00605 if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) 00606 return 0; 00607 if (!yaml_emitter_write_indent(emitter)) 00608 return 0; 00609 } 00610 00611 if (event->data.document_start.tag_directives.start 00612 != event->data.document_start.tag_directives.end) { 00613 implicit = 0; 00614 for (tag_directive = event->data.document_start.tag_directives.start; 00615 tag_directive != event->data.document_start.tag_directives.end; 00616 tag_directive ++) { 00617 if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0)) 00618 return 0; 00619 if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle, 00620 strlen((char *)tag_directive->handle))) 00621 return 0; 00622 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix, 00623 strlen((char *)tag_directive->prefix), 1)) 00624 return 0; 00625 if (!yaml_emitter_write_indent(emitter)) 00626 return 0; 00627 } 00628 } 00629 00630 if (yaml_emitter_check_empty_document(emitter)) { 00631 implicit = 0; 00632 } 00633 00634 if (!implicit) { 00635 if (!yaml_emitter_write_indent(emitter)) 00636 return 0; 00637 if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) 00638 return 0; 00639 if (emitter->canonical) { 00640 if (!yaml_emitter_write_indent(emitter)) 00641 return 0; 00642 } 00643 } 00644 00645 emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE; 00646 00647 return 1; 00648 } 00649 00650 else if (event->type == YAML_STREAM_END_EVENT) 00651 { 00652 if (emitter->open_ended) 00653 { 00654 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) 00655 return 0; 00656 if (!yaml_emitter_write_indent(emitter)) 00657 return 0; 00658 } 00659 00660 if (!yaml_emitter_flush(emitter)) 00661 return 0; 00662 00663 emitter->state = YAML_EMIT_END_STATE; 00664 00665 return 1; 00666 } 00667 00668 return yaml_emitter_set_emitter_error(emitter, 00669 "expected DOCUMENT-START or STREAM-END"); 00670 } 00671 00672 /* 00673 * Expect the root node. 00674 */ 00675 00676 static int 00677 yaml_emitter_emit_document_content(yaml_emitter_t *emitter, 00678 yaml_event_t *event) 00679 { 00680 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE)) 00681 return 0; 00682 00683 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0); 00684 } 00685 00686 /* 00687 * Expect DOCUMENT-END. 00688 */ 00689 00690 static int 00691 yaml_emitter_emit_document_end(yaml_emitter_t *emitter, 00692 yaml_event_t *event) 00693 { 00694 if (event->type == YAML_DOCUMENT_END_EVENT) 00695 { 00696 if (!yaml_emitter_write_indent(emitter)) 00697 return 0; 00698 if (!event->data.document_end.implicit) { 00699 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) 00700 return 0; 00701 if (!yaml_emitter_write_indent(emitter)) 00702 return 0; 00703 } 00704 if (!yaml_emitter_flush(emitter)) 00705 return 0; 00706 00707 emitter->state = YAML_EMIT_DOCUMENT_START_STATE; 00708 00709 while (!STACK_EMPTY(emitter, emitter->tag_directives)) { 00710 yaml_tag_directive_t tag_directive = POP(emitter, 00711 emitter->tag_directives); 00712 yaml_free(tag_directive.handle); 00713 yaml_free(tag_directive.prefix); 00714 } 00715 00716 return 1; 00717 } 00718 00719 return yaml_emitter_set_emitter_error(emitter, 00720 "expected DOCUMENT-END"); 00721 } 00722 00723 /* 00724 * 00725 * Expect a flow item node. 00726 */ 00727 00728 static int 00729 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, 00730 yaml_event_t *event, int first) 00731 { 00732 if (first) 00733 { 00734 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0)) 00735 return 0; 00736 if (!yaml_emitter_increase_indent(emitter, 1, 0)) 00737 return 0; 00738 emitter->flow_level ++; 00739 } 00740 00741 if (event->type == YAML_SEQUENCE_END_EVENT) 00742 { 00743 emitter->flow_level --; 00744 emitter->indent = POP(emitter, emitter->indents); 00745 if (emitter->canonical && !first) { 00746 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) 00747 return 0; 00748 if (!yaml_emitter_write_indent(emitter)) 00749 return 0; 00750 } 00751 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0)) 00752 return 0; 00753 emitter->state = POP(emitter, emitter->states); 00754 00755 return 1; 00756 } 00757 00758 if (!first) { 00759 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) 00760 return 0; 00761 } 00762 00763 if (emitter->canonical || emitter->column > emitter->best_width) { 00764 if (!yaml_emitter_write_indent(emitter)) 00765 return 0; 00766 } 00767 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE)) 00768 return 0; 00769 00770 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); 00771 } 00772 00773 /* 00774 * Expect a flow key node. 00775 */ 00776 00777 static int 00778 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, 00779 yaml_event_t *event, int first) 00780 { 00781 if (first) 00782 { 00783 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0)) 00784 return 0; 00785 if (!yaml_emitter_increase_indent(emitter, 1, 0)) 00786 return 0; 00787 emitter->flow_level ++; 00788 } 00789 00790 if (event->type == YAML_MAPPING_END_EVENT) 00791 { 00792 emitter->flow_level --; 00793 emitter->indent = POP(emitter, emitter->indents); 00794 if (emitter->canonical && !first) { 00795 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) 00796 return 0; 00797 if (!yaml_emitter_write_indent(emitter)) 00798 return 0; 00799 } 00800 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0)) 00801 return 0; 00802 emitter->state = POP(emitter, emitter->states); 00803 00804 return 1; 00805 } 00806 00807 if (!first) { 00808 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) 00809 return 0; 00810 } 00811 if (emitter->canonical || emitter->column > emitter->best_width) { 00812 if (!yaml_emitter_write_indent(emitter)) 00813 return 0; 00814 } 00815 00816 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) 00817 { 00818 if (!PUSH(emitter, emitter->states, 00819 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) 00820 return 0; 00821 00822 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); 00823 } 00824 else 00825 { 00826 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0)) 00827 return 0; 00828 if (!PUSH(emitter, emitter->states, 00829 YAML_EMIT_FLOW_MAPPING_VALUE_STATE)) 00830 return 0; 00831 00832 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); 00833 } 00834 } 00835 00836 /* 00837 * Expect a flow value node. 00838 */ 00839 00840 static int 00841 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, 00842 yaml_event_t *event, int simple) 00843 { 00844 if (simple) { 00845 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) 00846 return 0; 00847 } 00848 else { 00849 if (emitter->canonical || emitter->column > emitter->best_width) { 00850 if (!yaml_emitter_write_indent(emitter)) 00851 return 0; 00852 } 00853 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0)) 00854 return 0; 00855 } 00856 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE)) 00857 return 0; 00858 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); 00859 } 00860 00861 /* 00862 * Expect a block item node. 00863 */ 00864 00865 static int 00866 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, 00867 yaml_event_t *event, int first) 00868 { 00869 if (first) 00870 { 00871 if (!yaml_emitter_increase_indent(emitter, 0, 00872 (emitter->mapping_context && !emitter->indention))) 00873 return 0; 00874 } 00875 00876 if (event->type == YAML_SEQUENCE_END_EVENT) 00877 { 00878 emitter->indent = POP(emitter, emitter->indents); 00879 emitter->state = POP(emitter, emitter->states); 00880 00881 return 1; 00882 } 00883 00884 if (!yaml_emitter_write_indent(emitter)) 00885 return 0; 00886 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1)) 00887 return 0; 00888 if (!PUSH(emitter, emitter->states, 00889 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE)) 00890 return 0; 00891 00892 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); 00893 } 00894 00895 /* 00896 * Expect a block key node. 00897 */ 00898 00899 static int 00900 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, 00901 yaml_event_t *event, int first) 00902 { 00903 if (first) 00904 { 00905 if (!yaml_emitter_increase_indent(emitter, 0, 0)) 00906 return 0; 00907 } 00908 00909 if (event->type == YAML_MAPPING_END_EVENT) 00910 { 00911 emitter->indent = POP(emitter, emitter->indents); 00912 emitter->state = POP(emitter, emitter->states); 00913 00914 return 1; 00915 } 00916 00917 if (!yaml_emitter_write_indent(emitter)) 00918 return 0; 00919 00920 if (yaml_emitter_check_simple_key(emitter)) 00921 { 00922 if (!PUSH(emitter, emitter->states, 00923 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)) 00924 return 0; 00925 00926 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); 00927 } 00928 else 00929 { 00930 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1)) 00931 return 0; 00932 if (!PUSH(emitter, emitter->states, 00933 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE)) 00934 return 0; 00935 00936 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); 00937 } 00938 } 00939 00940 /* 00941 * Expect a block value node. 00942 */ 00943 00944 static int 00945 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, 00946 yaml_event_t *event, int simple) 00947 { 00948 if (simple) { 00949 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) 00950 return 0; 00951 } 00952 else { 00953 if (!yaml_emitter_write_indent(emitter)) 00954 return 0; 00955 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1)) 00956 return 0; 00957 } 00958 if (!PUSH(emitter, emitter->states, 00959 YAML_EMIT_BLOCK_MAPPING_KEY_STATE)) 00960 return 0; 00961 00962 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); 00963 } 00964 00965 /* 00966 * Expect a node. 00967 */ 00968 00969 static int 00970 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, 00971 int root, int sequence, int mapping, int simple_key) 00972 { 00973 emitter->root_context = root; 00974 emitter->sequence_context = sequence; 00975 emitter->mapping_context = mapping; 00976 emitter->simple_key_context = simple_key; 00977 00978 switch (event->type) 00979 { 00980 case YAML_ALIAS_EVENT: 00981 return yaml_emitter_emit_alias(emitter, event); 00982 00983 case YAML_SCALAR_EVENT: 00984 return yaml_emitter_emit_scalar(emitter, event); 00985 00986 case YAML_SEQUENCE_START_EVENT: 00987 return yaml_emitter_emit_sequence_start(emitter, event); 00988 00989 case YAML_MAPPING_START_EVENT: 00990 return yaml_emitter_emit_mapping_start(emitter, event); 00991 00992 default: 00993 return yaml_emitter_set_emitter_error(emitter, 00994 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); 00995 } 00996 00997 return 0; 00998 } 00999 01000 /* 01001 * Expect ALIAS. 01002 */ 01003 01004 static int 01005 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event) 01006 { 01007 if (!yaml_emitter_process_anchor(emitter)) 01008 return 0; 01009 emitter->state = POP(emitter, emitter->states); 01010 01011 return 1; 01012 } 01013 01014 /* 01015 * Expect SCALAR. 01016 */ 01017 01018 static int 01019 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event) 01020 { 01021 if (!yaml_emitter_select_scalar_style(emitter, event)) 01022 return 0; 01023 if (!yaml_emitter_process_anchor(emitter)) 01024 return 0; 01025 if (!yaml_emitter_process_tag(emitter)) 01026 return 0; 01027 if (!yaml_emitter_increase_indent(emitter, 1, 0)) 01028 return 0; 01029 if (!yaml_emitter_process_scalar(emitter)) 01030 return 0; 01031 emitter->indent = POP(emitter, emitter->indents); 01032 emitter->state = POP(emitter, emitter->states); 01033 01034 return 1; 01035 } 01036 01037 /* 01038 * Expect SEQUENCE-START. 01039 */ 01040 01041 static int 01042 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) 01043 { 01044 if (!yaml_emitter_process_anchor(emitter)) 01045 return 0; 01046 if (!yaml_emitter_process_tag(emitter)) 01047 return 0; 01048 01049 if (emitter->flow_level || emitter->canonical 01050 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE 01051 || yaml_emitter_check_empty_sequence(emitter)) { 01052 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; 01053 } 01054 else { 01055 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; 01056 } 01057 01058 return 1; 01059 } 01060 01061 /* 01062 * Expect MAPPING-START. 01063 */ 01064 01065 static int 01066 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) 01067 { 01068 if (!yaml_emitter_process_anchor(emitter)) 01069 return 0; 01070 if (!yaml_emitter_process_tag(emitter)) 01071 return 0; 01072 01073 if (emitter->flow_level || emitter->canonical 01074 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE 01075 || yaml_emitter_check_empty_mapping(emitter)) { 01076 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; 01077 } 01078 else { 01079 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; 01080 } 01081 01082 return 1; 01083 } 01084 01085 /* 01086 * Check if the document content is an empty scalar. 01087 */ 01088 01089 static int 01090 yaml_emitter_check_empty_document(yaml_emitter_t *emitter) 01091 { 01092 return 0; 01093 } 01094 01095 /* 01096 * Check if the next events represent an empty sequence. 01097 */ 01098 01099 static int 01100 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter) 01101 { 01102 if (emitter->events.tail - emitter->events.head < 2) 01103 return 0; 01104 01105 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT 01106 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT); 01107 } 01108 01109 /* 01110 * Check if the next events represent an empty mapping. 01111 */ 01112 01113 static int 01114 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter) 01115 { 01116 if (emitter->events.tail - emitter->events.head < 2) 01117 return 0; 01118 01119 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT 01120 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT); 01121 } 01122 01123 /* 01124 * Check if the next node can be expressed as a simple key. 01125 */ 01126 01127 static int 01128 yaml_emitter_check_simple_key(yaml_emitter_t *emitter) 01129 { 01130 yaml_event_t *event = emitter->events.head; 01131 size_t length = 0; 01132 01133 switch (event->type) 01134 { 01135 case YAML_ALIAS_EVENT: 01136 length += emitter->anchor_data.anchor_length; 01137 break; 01138 01139 case YAML_SCALAR_EVENT: 01140 if (emitter->scalar_data.multiline) 01141 return 0; 01142 length += emitter->anchor_data.anchor_length 01143 + emitter->tag_data.handle_length 01144 + emitter->tag_data.suffix_length 01145 + emitter->scalar_data.length; 01146 break; 01147 01148 case YAML_SEQUENCE_START_EVENT: 01149 if (!yaml_emitter_check_empty_sequence(emitter)) 01150 return 0; 01151 length += emitter->anchor_data.anchor_length 01152 + emitter->tag_data.handle_length 01153 + emitter->tag_data.suffix_length; 01154 break; 01155 01156 case YAML_MAPPING_START_EVENT: 01157 if (!yaml_emitter_check_empty_mapping(emitter)) 01158 return 0; 01159 length += emitter->anchor_data.anchor_length 01160 + emitter->tag_data.handle_length 01161 + emitter->tag_data.suffix_length; 01162 break; 01163 01164 default: 01165 return 0; 01166 } 01167 01168 if (length > 128) 01169 return 0; 01170 01171 return 1; 01172 } 01173 01174 /* 01175 * Determine an acceptable scalar style. 01176 */ 01177 01178 static int 01179 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event) 01180 { 01181 yaml_scalar_style_t style = event->data.scalar.style; 01182 int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix); 01183 01184 if (no_tag && !event->data.scalar.plain_implicit 01185 && !event->data.scalar.quoted_implicit) { 01186 return yaml_emitter_set_emitter_error(emitter, 01187 "neither tag nor implicit flags are specified"); 01188 } 01189 01190 if (style == YAML_ANY_SCALAR_STYLE) 01191 style = YAML_PLAIN_SCALAR_STYLE; 01192 01193 if (emitter->canonical) 01194 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; 01195 01196 if (emitter->simple_key_context && emitter->scalar_data.multiline) 01197 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; 01198 01199 if (style == YAML_PLAIN_SCALAR_STYLE) 01200 { 01201 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed) 01202 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed)) 01203 style = YAML_SINGLE_QUOTED_SCALAR_STYLE; 01204 if (!emitter->scalar_data.length 01205 && (emitter->flow_level || emitter->simple_key_context)) 01206 style = YAML_SINGLE_QUOTED_SCALAR_STYLE; 01207 if (no_tag && !event->data.scalar.plain_implicit) 01208 style = YAML_SINGLE_QUOTED_SCALAR_STYLE; 01209 } 01210 01211 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE) 01212 { 01213 if (!emitter->scalar_data.single_quoted_allowed) 01214 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; 01215 } 01216 01217 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE) 01218 { 01219 if (!emitter->scalar_data.block_allowed 01220 || emitter->flow_level || emitter->simple_key_context) 01221 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; 01222 } 01223 01224 if (no_tag && !event->data.scalar.quoted_implicit 01225 && style != YAML_PLAIN_SCALAR_STYLE) 01226 { 01227 emitter->tag_data.handle = (yaml_char_t *)"!"; 01228 emitter->tag_data.handle_length = 1; 01229 } 01230 01231 emitter->scalar_data.style = style; 01232 01233 return 1; 01234 } 01235 01236 /* 01237 * Write an achor. 01238 */ 01239 01240 static int 01241 yaml_emitter_process_anchor(yaml_emitter_t *emitter) 01242 { 01243 if (!emitter->anchor_data.anchor) 01244 return 1; 01245 01246 if (!yaml_emitter_write_indicator(emitter, 01247 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0)) 01248 return 0; 01249 01250 return yaml_emitter_write_anchor(emitter, 01251 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length); 01252 } 01253 01254 /* 01255 * Write a tag. 01256 */ 01257 01258 static int 01259 yaml_emitter_process_tag(yaml_emitter_t *emitter) 01260 { 01261 if (!emitter->tag_data.handle && !emitter->tag_data.suffix) 01262 return 1; 01263 01264 if (emitter->tag_data.handle) 01265 { 01266 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle, 01267 emitter->tag_data.handle_length)) 01268 return 0; 01269 if (emitter->tag_data.suffix) { 01270 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, 01271 emitter->tag_data.suffix_length, 0)) 01272 return 0; 01273 } 01274 } 01275 else 01276 { 01277 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0)) 01278 return 0; 01279 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, 01280 emitter->tag_data.suffix_length, 0)) 01281 return 0; 01282 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0)) 01283 return 0; 01284 } 01285 01286 return 1; 01287 } 01288 01289 /* 01290 * Write a scalar. 01291 */ 01292 01293 static int 01294 yaml_emitter_process_scalar(yaml_emitter_t *emitter) 01295 { 01296 switch (emitter->scalar_data.style) 01297 { 01298 case YAML_PLAIN_SCALAR_STYLE: 01299 return yaml_emitter_write_plain_scalar(emitter, 01300 emitter->scalar_data.value, emitter->scalar_data.length, 01301 !emitter->simple_key_context); 01302 01303 case YAML_SINGLE_QUOTED_SCALAR_STYLE: 01304 return yaml_emitter_write_single_quoted_scalar(emitter, 01305 emitter->scalar_data.value, emitter->scalar_data.length, 01306 !emitter->simple_key_context); 01307 01308 case YAML_DOUBLE_QUOTED_SCALAR_STYLE: 01309 return yaml_emitter_write_double_quoted_scalar(emitter, 01310 emitter->scalar_data.value, emitter->scalar_data.length, 01311 !emitter->simple_key_context); 01312 01313 case YAML_LITERAL_SCALAR_STYLE: 01314 return yaml_emitter_write_literal_scalar(emitter, 01315 emitter->scalar_data.value, emitter->scalar_data.length); 01316 01317 case YAML_FOLDED_SCALAR_STYLE: 01318 return yaml_emitter_write_folded_scalar(emitter, 01319 emitter->scalar_data.value, emitter->scalar_data.length); 01320 01321 default: 01322 assert(1); /* Impossible. */ 01323 } 01324 01325 return 0; 01326 } 01327 01328 /* 01329 * Check if a %YAML directive is valid. 01330 */ 01331 01332 static int 01333 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, 01334 yaml_version_directive_t version_directive) 01335 { 01336 if (version_directive.major != 1 || version_directive.minor != 1) { 01337 return yaml_emitter_set_emitter_error(emitter, 01338 "incompatible %YAML directive"); 01339 } 01340 01341 return 1; 01342 } 01343 01344 /* 01345 * Check if a %TAG directive is valid. 01346 */ 01347 01348 static int 01349 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, 01350 yaml_tag_directive_t tag_directive) 01351 { 01352 yaml_string_t handle; 01353 yaml_string_t prefix; 01354 size_t handle_length; 01355 size_t prefix_length; 01356 01357 handle_length = strlen((char *)tag_directive.handle); 01358 prefix_length = strlen((char *)tag_directive.prefix); 01359 STRING_ASSIGN(handle, tag_directive.handle, handle_length); 01360 STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length); 01361 01362 if (handle.start == handle.end) { 01363 return yaml_emitter_set_emitter_error(emitter, 01364 "tag handle must not be empty"); 01365 } 01366 01367 if (handle.start[0] != '!') { 01368 return yaml_emitter_set_emitter_error(emitter, 01369 "tag handle must start with '!'"); 01370 } 01371 01372 if (handle.end[-1] != '!') { 01373 return yaml_emitter_set_emitter_error(emitter, 01374 "tag handle must end with '!'"); 01375 } 01376 01377 handle.pointer ++; 01378 01379 while (handle.pointer < handle.end-1) { 01380 if (!IS_ALPHA(handle)) { 01381 return yaml_emitter_set_emitter_error(emitter, 01382 "tag handle must contain alphanumerical characters only"); 01383 } 01384 MOVE(handle); 01385 } 01386 01387 if (prefix.start == prefix.end) { 01388 return yaml_emitter_set_emitter_error(emitter, 01389 "tag prefix must not be empty"); 01390 } 01391 01392 return 1; 01393 } 01394 01395 /* 01396 * Check if an anchor is valid. 01397 */ 01398 01399 static int 01400 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, 01401 yaml_char_t *anchor, int alias) 01402 { 01403 size_t anchor_length; 01404 yaml_string_t string; 01405 01406 anchor_length = strlen((char *)anchor); 01407 STRING_ASSIGN(string, anchor, anchor_length); 01408 01409 if (string.start == string.end) { 01410 return yaml_emitter_set_emitter_error(emitter, alias ? 01411 "alias value must not be empty" : 01412 "anchor value must not be empty"); 01413 } 01414 01415 while (string.pointer != string.end) { 01416 if (!IS_ALPHA(string)) { 01417 return yaml_emitter_set_emitter_error(emitter, alias ? 01418 "alias value must contain alphanumerical characters only" : 01419 "anchor value must contain alphanumerical characters only"); 01420 } 01421 MOVE(string); 01422 } 01423 01424 emitter->anchor_data.anchor = string.start; 01425 emitter->anchor_data.anchor_length = string.end - string.start; 01426 emitter->anchor_data.alias = alias; 01427 01428 return 1; 01429 } 01430 01431 /* 01432 * Check if a tag is valid. 01433 */ 01434 01435 static int 01436 yaml_emitter_analyze_tag(yaml_emitter_t *emitter, 01437 yaml_char_t *tag) 01438 { 01439 size_t tag_length; 01440 yaml_string_t string; 01441 yaml_tag_directive_t *tag_directive; 01442 01443 tag_length = strlen((char *)tag); 01444 STRING_ASSIGN(string, tag, tag_length); 01445 01446 if (string.start == string.end) { 01447 return yaml_emitter_set_emitter_error(emitter, 01448 "tag value must not be empty"); 01449 } 01450 01451 for (tag_directive = emitter->tag_directives.start; 01452 tag_directive != emitter->tag_directives.top; tag_directive ++) { 01453 size_t prefix_length = strlen((char *)tag_directive->prefix); 01454 if (prefix_length < (size_t)(string.end - string.start) 01455 && strncmp((char *)tag_directive->prefix, (char *)string.start, 01456 prefix_length) == 0) 01457 { 01458 emitter->tag_data.handle = tag_directive->handle; 01459 emitter->tag_data.handle_length = 01460 strlen((char *)tag_directive->handle); 01461 emitter->tag_data.suffix = string.start + prefix_length; 01462 emitter->tag_data.suffix_length = 01463 (string.end - string.start) - prefix_length; 01464 return 1; 01465 } 01466 } 01467 01468 emitter->tag_data.suffix = string.start; 01469 emitter->tag_data.suffix_length = string.end - string.start; 01470 01471 return 1; 01472 } 01473 01474 /* 01475 * Check if a scalar is valid. 01476 */ 01477 01478 static int 01479 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, 01480 yaml_char_t *value, size_t length) 01481 { 01482 yaml_string_t string; 01483 01484 int block_indicators = 0; 01485 int flow_indicators = 0; 01486 int line_breaks = 0; 01487 int special_characters = 0; 01488 01489 int leading_space = 0; 01490 int leading_break = 0; 01491 int trailing_space = 0; 01492 int trailing_break = 0; 01493 int break_space = 0; 01494 int space_break = 0; 01495 01496 int preceeded_by_whitespace = 0; 01497 int followed_by_whitespace = 0; 01498 int previous_space = 0; 01499 int previous_break = 0; 01500 01501 STRING_ASSIGN(string, value, length); 01502 01503 emitter->scalar_data.value = value; 01504 emitter->scalar_data.length = length; 01505 01506 if (string.start == string.end) 01507 { 01508 emitter->scalar_data.multiline = 0; 01509 emitter->scalar_data.flow_plain_allowed = 0; 01510 emitter->scalar_data.block_plain_allowed = 1; 01511 emitter->scalar_data.single_quoted_allowed = 1; 01512 emitter->scalar_data.block_allowed = 0; 01513 01514 return 1; 01515 } 01516 01517 if ((CHECK_AT(string, '-', 0) 01518 && CHECK_AT(string, '-', 1) 01519 && CHECK_AT(string, '-', 2)) 01520 || (CHECK_AT(string, '.', 0) 01521 && CHECK_AT(string, '.', 1) 01522 && CHECK_AT(string, '.', 2))) { 01523 block_indicators = 1; 01524 flow_indicators = 1; 01525 } 01526 01527 preceeded_by_whitespace = 1; 01528 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); 01529 01530 while (string.pointer != string.end) 01531 { 01532 if (string.start == string.pointer) 01533 { 01534 if (CHECK(string, '#') || CHECK(string, ',') 01535 || CHECK(string, '[') || CHECK(string, ']') 01536 || CHECK(string, '{') || CHECK(string, '}') 01537 || CHECK(string, '&') || CHECK(string, '*') 01538 || CHECK(string, '!') || CHECK(string, '|') 01539 || CHECK(string, '>') || CHECK(string, '\'') 01540 || CHECK(string, '"') || CHECK(string, '%') 01541 || CHECK(string, '@') || CHECK(string, '`')) { 01542 flow_indicators = 1; 01543 block_indicators = 1; 01544 } 01545 01546 if (CHECK(string, '?') || CHECK(string, ':')) { 01547 flow_indicators = 1; 01548 if (followed_by_whitespace) { 01549 block_indicators = 1; 01550 } 01551 } 01552 01553 if (CHECK(string, '-') && followed_by_whitespace) { 01554 flow_indicators = 1; 01555 block_indicators = 1; 01556 } 01557 } 01558 else 01559 { 01560 if (CHECK(string, ',') || CHECK(string, '?') 01561 || CHECK(string, '[') || CHECK(string, ']') 01562 || CHECK(string, '{') || CHECK(string, '}')) { 01563 flow_indicators = 1; 01564 } 01565 01566 if (CHECK(string, ':')) { 01567 flow_indicators = 1; 01568 if (followed_by_whitespace) { 01569 block_indicators = 1; 01570 } 01571 } 01572 01573 if (CHECK(string, '#') && preceeded_by_whitespace) { 01574 flow_indicators = 1; 01575 block_indicators = 1; 01576 } 01577 } 01578 01579 if (!IS_PRINTABLE(string) 01580 || (!IS_ASCII(string) && !emitter->unicode)) { 01581 special_characters = 1; 01582 } 01583 01584 if (IS_BREAK(string)) { 01585 line_breaks = 1; 01586 } 01587 01588 if (IS_SPACE(string)) 01589 { 01590 if (string.start == string.pointer) { 01591 leading_space = 1; 01592 } 01593 if (string.pointer+WIDTH(string) == string.end) { 01594 trailing_space = 1; 01595 } 01596 if (previous_break) { 01597 break_space = 1; 01598 } 01599 previous_space = 1; 01600 previous_break = 0; 01601 } 01602 else if (IS_BREAK(string)) 01603 { 01604 if (string.start == string.pointer) { 01605 leading_break = 1; 01606 } 01607 if (string.pointer+WIDTH(string) == string.end) { 01608 trailing_break = 1; 01609 } 01610 if (previous_space) { 01611 space_break = 1; 01612 } 01613 previous_space = 0; 01614 previous_break = 1; 01615 } 01616 else 01617 { 01618 previous_space = 0; 01619 previous_break = 0; 01620 } 01621 01622 preceeded_by_whitespace = IS_BLANKZ(string); 01623 MOVE(string); 01624 if (string.pointer != string.end) { 01625 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); 01626 } 01627 } 01628 01629 emitter->scalar_data.multiline = line_breaks; 01630 01631 emitter->scalar_data.flow_plain_allowed = 1; 01632 emitter->scalar_data.block_plain_allowed = 1; 01633 emitter->scalar_data.single_quoted_allowed = 1; 01634 emitter->scalar_data.block_allowed = 1; 01635 01636 if (leading_space || leading_break || trailing_space || trailing_break) { 01637 emitter->scalar_data.flow_plain_allowed = 0; 01638 emitter->scalar_data.block_plain_allowed = 0; 01639 } 01640 01641 if (trailing_space) { 01642 emitter->scalar_data.block_allowed = 0; 01643 } 01644 01645 if (break_space) { 01646 emitter->scalar_data.flow_plain_allowed = 0; 01647 emitter->scalar_data.block_plain_allowed = 0; 01648 emitter->scalar_data.single_quoted_allowed = 0; 01649 } 01650 01651 if (space_break || special_characters) { 01652 emitter->scalar_data.flow_plain_allowed = 0; 01653 emitter->scalar_data.block_plain_allowed = 0; 01654 emitter->scalar_data.single_quoted_allowed = 0; 01655 emitter->scalar_data.block_allowed = 0; 01656 } 01657 01658 if (line_breaks) { 01659 emitter->scalar_data.flow_plain_allowed = 0; 01660 emitter->scalar_data.block_plain_allowed = 0; 01661 } 01662 01663 if (flow_indicators) { 01664 emitter->scalar_data.flow_plain_allowed = 0; 01665 } 01666 01667 if (block_indicators) { 01668 emitter->scalar_data.block_plain_allowed = 0; 01669 } 01670 01671 return 1; 01672 } 01673 01674 /* 01675 * Check if the event data is valid. 01676 */ 01677 01678 static int 01679 yaml_emitter_analyze_event(yaml_emitter_t *emitter, 01680 yaml_event_t *event) 01681 { 01682 emitter->anchor_data.anchor = NULL; 01683 emitter->anchor_data.anchor_length = 0; 01684 emitter->tag_data.handle = NULL; 01685 emitter->tag_data.handle_length = 0; 01686 emitter->tag_data.suffix = NULL; 01687 emitter->tag_data.suffix_length = 0; 01688 emitter->scalar_data.value = NULL; 01689 emitter->scalar_data.length = 0; 01690 01691 switch (event->type) 01692 { 01693 case YAML_ALIAS_EVENT: 01694 if (!yaml_emitter_analyze_anchor(emitter, 01695 event->data.alias.anchor, 1)) 01696 return 0; 01697 return 1; 01698 01699 case YAML_SCALAR_EVENT: 01700 if (event->data.scalar.anchor) { 01701 if (!yaml_emitter_analyze_anchor(emitter, 01702 event->data.scalar.anchor, 0)) 01703 return 0; 01704 } 01705 if (event->data.scalar.tag && (emitter->canonical || 01706 (!event->data.scalar.plain_implicit 01707 && !event->data.scalar.quoted_implicit))) { 01708 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag)) 01709 return 0; 01710 } 01711 if (!yaml_emitter_analyze_scalar(emitter, 01712 event->data.scalar.value, event->data.scalar.length)) 01713 return 0; 01714 return 1; 01715 01716 case YAML_SEQUENCE_START_EVENT: 01717 if (event->data.sequence_start.anchor) { 01718 if (!yaml_emitter_analyze_anchor(emitter, 01719 event->data.sequence_start.anchor, 0)) 01720 return 0; 01721 } 01722 if (event->data.sequence_start.tag && (emitter->canonical || 01723 !event->data.sequence_start.implicit)) { 01724 if (!yaml_emitter_analyze_tag(emitter, 01725 event->data.sequence_start.tag)) 01726 return 0; 01727 } 01728 return 1; 01729 01730 case YAML_MAPPING_START_EVENT: 01731 if (event->data.mapping_start.anchor) { 01732 if (!yaml_emitter_analyze_anchor(emitter, 01733 event->data.mapping_start.anchor, 0)) 01734 return 0; 01735 } 01736 if (event->data.mapping_start.tag && (emitter->canonical || 01737 !event->data.mapping_start.implicit)) { 01738 if (!yaml_emitter_analyze_tag(emitter, 01739 event->data.mapping_start.tag)) 01740 return 0; 01741 } 01742 return 1; 01743 01744 default: 01745 return 1; 01746 } 01747 } 01748 01749 /* 01750 * Write the BOM character. 01751 */ 01752 01753 static int 01754 yaml_emitter_write_bom(yaml_emitter_t *emitter) 01755 { 01756 if (!FLUSH(emitter)) return 0; 01757 01758 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF'; 01759 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB'; 01760 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF'; 01761 01762 return 1; 01763 } 01764 01765 static int 01766 yaml_emitter_write_indent(yaml_emitter_t *emitter) 01767 { 01768 int indent = (emitter->indent >= 0) ? emitter->indent : 0; 01769 01770 if (!emitter->indention || emitter->column > indent 01771 || (emitter->column == indent && !emitter->whitespace)) { 01772 if (!PUT_BREAK(emitter)) return 0; 01773 } 01774 01775 while (emitter->column < indent) { 01776 if (!PUT(emitter, ' ')) return 0; 01777 } 01778 01779 emitter->whitespace = 1; 01780 emitter->indention = 1; 01781 01782 return 1; 01783 } 01784 01785 static int 01786 yaml_emitter_write_indicator(yaml_emitter_t *emitter, 01787 const char *indicator, int need_whitespace, 01788 int is_whitespace, int is_indention) 01789 { 01790 size_t indicator_length; 01791 yaml_string_t string; 01792 01793 indicator_length = strlen(indicator); 01794 STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); 01795 01796 if (need_whitespace && !emitter->whitespace) { 01797 if (!PUT(emitter, ' ')) return 0; 01798 } 01799 01800 while (string.pointer != string.end) { 01801 if (!WRITE(emitter, string)) return 0; 01802 } 01803 01804 emitter->whitespace = is_whitespace; 01805 emitter->indention = (emitter->indention && is_indention); 01806 emitter->open_ended = 0; 01807 01808 return 1; 01809 } 01810 01811 static int 01812 yaml_emitter_write_anchor(yaml_emitter_t *emitter, 01813 yaml_char_t *value, size_t length) 01814 { 01815 yaml_string_t string; 01816 STRING_ASSIGN(string, value, length); 01817 01818 while (string.pointer != string.end) { 01819 if (!WRITE(emitter, string)) return 0; 01820 } 01821 01822 emitter->whitespace = 0; 01823 emitter->indention = 0; 01824 01825 return 1; 01826 } 01827 01828 static int 01829 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, 01830 yaml_char_t *value, size_t length) 01831 { 01832 yaml_string_t string; 01833 STRING_ASSIGN(string, value, length); 01834 01835 if (!emitter->whitespace) { 01836 if (!PUT(emitter, ' ')) return 0; 01837 } 01838 01839 while (string.pointer != string.end) { 01840 if (!WRITE(emitter, string)) return 0; 01841 } 01842 01843 emitter->whitespace = 0; 01844 emitter->indention = 0; 01845 01846 return 1; 01847 } 01848 01849 static int 01850 yaml_emitter_write_tag_content(yaml_emitter_t *emitter, 01851 yaml_char_t *value, size_t length, 01852 int need_whitespace) 01853 { 01854 yaml_string_t string; 01855 STRING_ASSIGN(string, value, length); 01856 01857 if (need_whitespace && !emitter->whitespace) { 01858 if (!PUT(emitter, ' ')) return 0; 01859 } 01860 01861 while (string.pointer != string.end) { 01862 if (IS_ALPHA(string) 01863 || CHECK(string, ';') || CHECK(string, '/') 01864 || CHECK(string, '?') || CHECK(string, ':') 01865 || CHECK(string, '@') || CHECK(string, '&') 01866 || CHECK(string, '=') || CHECK(string, '+') 01867 || CHECK(string, '$') || CHECK(string, ',') 01868 || CHECK(string, '_') || CHECK(string, '.') 01869 || CHECK(string, '~') || CHECK(string, '*') 01870 || CHECK(string, '\'') || CHECK(string, '(') 01871 || CHECK(string, ')') || CHECK(string, '[') 01872 || CHECK(string, ']')) { 01873 if (!WRITE(emitter, string)) return 0; 01874 } 01875 else { 01876 int width = WIDTH(string); 01877 unsigned int value; 01878 while (width --) { 01879 value = *(string.pointer++); 01880 if (!PUT(emitter, '%')) return 0; 01881 if (!PUT(emitter, (value >> 4) 01882 + ((value >> 4) < 10 ? '0' : 'A' - 10))) 01883 return 0; 01884 if (!PUT(emitter, (value & 0x0F) 01885 + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) 01886 return 0; 01887 } 01888 } 01889 } 01890 01891 emitter->whitespace = 0; 01892 emitter->indention = 0; 01893 01894 return 1; 01895 } 01896 01897 static int 01898 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, 01899 yaml_char_t *value, size_t length, int allow_breaks) 01900 { 01901 yaml_string_t string; 01902 int spaces = 0; 01903 int breaks = 0; 01904 01905 STRING_ASSIGN(string, value, length); 01906 01907 if (!emitter->whitespace) { 01908 if (!PUT(emitter, ' ')) return 0; 01909 } 01910 01911 while (string.pointer != string.end) 01912 { 01913 if (IS_SPACE(string)) 01914 { 01915 if (allow_breaks && !spaces 01916 && emitter->column > emitter->best_width 01917 && !IS_SPACE_AT(string, 1)) { 01918 if (!yaml_emitter_write_indent(emitter)) return 0; 01919 MOVE(string); 01920 } 01921 else { 01922 if (!WRITE(emitter, string)) return 0; 01923 } 01924 spaces = 1; 01925 } 01926 else if (IS_BREAK(string)) 01927 { 01928 if (!breaks && CHECK(string, '\n')) { 01929 if (!PUT_BREAK(emitter)) return 0; 01930 } 01931 if (!WRITE_BREAK(emitter, string)) return 0; 01932 emitter->indention = 1; 01933 breaks = 1; 01934 } 01935 else 01936 { 01937 if (breaks) { 01938 if (!yaml_emitter_write_indent(emitter)) return 0; 01939 } 01940 if (!WRITE(emitter, string)) return 0; 01941 emitter->indention = 0; 01942 spaces = 0; 01943 breaks = 0; 01944 } 01945 } 01946 01947 emitter->whitespace = 0; 01948 emitter->indention = 0; 01949 if (emitter->root_context) 01950 { 01951 emitter->open_ended = 1; 01952 } 01953 01954 return 1; 01955 } 01956 01957 static int 01958 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, 01959 yaml_char_t *value, size_t length, int allow_breaks) 01960 { 01961 yaml_string_t string; 01962 int spaces = 0; 01963 int breaks = 0; 01964 01965 STRING_ASSIGN(string, value, length); 01966 01967 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) 01968 return 0; 01969 01970 while (string.pointer != string.end) 01971 { 01972 if (IS_SPACE(string)) 01973 { 01974 if (allow_breaks && !spaces 01975 && emitter->column > emitter->best_width 01976 && string.pointer != string.start 01977 && string.pointer != string.end - 1 01978 && !IS_SPACE_AT(string, 1)) { 01979 if (!yaml_emitter_write_indent(emitter)) return 0; 01980 MOVE(string); 01981 } 01982 else { 01983 if (!WRITE(emitter, string)) return 0; 01984 } 01985 spaces = 1; 01986 } 01987 else if (IS_BREAK(string)) 01988 { 01989 if (!breaks && CHECK(string, '\n')) { 01990 if (!PUT_BREAK(emitter)) return 0; 01991 } 01992 if (!WRITE_BREAK(emitter, string)) return 0; 01993 emitter->indention = 1; 01994 breaks = 1; 01995 } 01996 else 01997 { 01998 if (breaks) { 01999 if (!yaml_emitter_write_indent(emitter)) return 0; 02000 } 02001 if (CHECK(string, '\'')) { 02002 if (!PUT(emitter, '\'')) return 0; 02003 } 02004 if (!WRITE(emitter, string)) return 0; 02005 emitter->indention = 0; 02006 spaces = 0; 02007 breaks = 0; 02008 } 02009 } 02010 02011 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) 02012 return 0; 02013 02014 emitter->whitespace = 0; 02015 emitter->indention = 0; 02016 02017 return 1; 02018 } 02019 02020 static int 02021 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, 02022 yaml_char_t *value, size_t length, int allow_breaks) 02023 { 02024 yaml_string_t string; 02025 int spaces = 0; 02026 02027 STRING_ASSIGN(string, value, length); 02028 02029 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) 02030 return 0; 02031 02032 while (string.pointer != string.end) 02033 { 02034 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) 02035 || IS_BOM(string) || IS_BREAK(string) 02036 || CHECK(string, '"') || CHECK(string, '\\')) 02037 { 02038 unsigned char octet; 02039 unsigned int width; 02040 unsigned int value; 02041 int k; 02042 02043 octet = string.pointer[0]; 02044 width = (octet & 0x80) == 0x00 ? 1 : 02045 (octet & 0xE0) == 0xC0 ? 2 : 02046 (octet & 0xF0) == 0xE0 ? 3 : 02047 (octet & 0xF8) == 0xF0 ? 4 : 0; 02048 value = (octet & 0x80) == 0x00 ? octet & 0x7F : 02049 (octet & 0xE0) == 0xC0 ? octet & 0x1F : 02050 (octet & 0xF0) == 0xE0 ? octet & 0x0F : 02051 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 02052 for (k = 1; k < (int)width; k ++) { 02053 octet = string.pointer[k]; 02054 value = (value << 6) + (octet & 0x3F); 02055 } 02056 string.pointer += width; 02057 02058 if (!PUT(emitter, '\\')) return 0; 02059 02060 switch (value) 02061 { 02062 case 0x00: 02063 if (!PUT(emitter, '0')) return 0; 02064 break; 02065 02066 case 0x07: 02067 if (!PUT(emitter, 'a')) return 0; 02068 break; 02069 02070 case 0x08: 02071 if (!PUT(emitter, 'b')) return 0; 02072 break; 02073 02074 case 0x09: 02075 if (!PUT(emitter, 't')) return 0; 02076 break; 02077 02078 case 0x0A: 02079 if (!PUT(emitter, 'n')) return 0; 02080 break; 02081 02082 case 0x0B: 02083 if (!PUT(emitter, 'v')) return 0; 02084 break; 02085 02086 case 0x0C: 02087 if (!PUT(emitter, 'f')) return 0; 02088 break; 02089 02090 case 0x0D: 02091 if (!PUT(emitter, 'r')) return 0; 02092 break; 02093 02094 case 0x1B: 02095 if (!PUT(emitter, 'e')) return 0; 02096 break; 02097 02098 case 0x22: 02099 if (!PUT(emitter, '\"')) return 0; 02100 break; 02101 02102 case 0x5C: 02103 if (!PUT(emitter, '\\')) return 0; 02104 break; 02105 02106 case 0x85: 02107 if (!PUT(emitter, 'N')) return 0; 02108 break; 02109 02110 case 0xA0: 02111 if (!PUT(emitter, '_')) return 0; 02112 break; 02113 02114 case 0x2028: 02115 if (!PUT(emitter, 'L')) return 0; 02116 break; 02117 02118 case 0x2029: 02119 if (!PUT(emitter, 'P')) return 0; 02120 break; 02121 02122 default: 02123 if (value <= 0xFF) { 02124 if (!PUT(emitter, 'x')) return 0; 02125 width = 2; 02126 } 02127 else if (value <= 0xFFFF) { 02128 if (!PUT(emitter, 'u')) return 0; 02129 width = 4; 02130 } 02131 else { 02132 if (!PUT(emitter, 'U')) return 0; 02133 width = 8; 02134 } 02135 for (k = (width-1)*4; k >= 0; k -= 4) { 02136 int digit = (value >> k) & 0x0F; 02137 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) 02138 return 0; 02139 } 02140 } 02141 spaces = 0; 02142 } 02143 else if (IS_SPACE(string)) 02144 { 02145 if (allow_breaks && !spaces 02146 && emitter->column > emitter->best_width 02147 && string.pointer != string.start 02148 && string.pointer != string.end - 1) { 02149 if (!yaml_emitter_write_indent(emitter)) return 0; 02150 if (IS_SPACE_AT(string, 1)) { 02151 if (!PUT(emitter, '\\')) return 0; 02152 } 02153 MOVE(string); 02154 } 02155 else { 02156 if (!WRITE(emitter, string)) return 0; 02157 } 02158 spaces = 1; 02159 } 02160 else 02161 { 02162 if (!WRITE(emitter, string)) return 0; 02163 spaces = 0; 02164 } 02165 } 02166 02167 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) 02168 return 0; 02169 02170 emitter->whitespace = 0; 02171 emitter->indention = 0; 02172 02173 return 1; 02174 } 02175 02176 static int 02177 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, 02178 yaml_string_t string) 02179 { 02180 char indent_hint[2]; 02181 const char *chomp_hint = NULL; 02182 02183 if (IS_SPACE(string) || IS_BREAK(string)) 02184 { 02185 indent_hint[0] = '0' + (char)emitter->best_indent; 02186 indent_hint[1] = '\0'; 02187 if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) 02188 return 0; 02189 } 02190 02191 emitter->open_ended = 0; 02192 02193 string.pointer = string.end; 02194 if (string.start == string.pointer) 02195 { 02196 chomp_hint = "-"; 02197 } 02198 else 02199 { 02200 do { 02201 string.pointer --; 02202 } while ((*string.pointer & 0xC0) == 0x80); 02203 if (!IS_BREAK(string)) 02204 { 02205 chomp_hint = "-"; 02206 } 02207 else if (string.start == string.pointer) 02208 { 02209 chomp_hint = "+"; 02210 emitter->open_ended = 1; 02211 } 02212 else 02213 { 02214 do { 02215 string.pointer --; 02216 } while ((*string.pointer & 0xC0) == 0x80); 02217 if (IS_BREAK(string)) 02218 { 02219 chomp_hint = "+"; 02220 emitter->open_ended = 1; 02221 } 02222 } 02223 } 02224 02225 if (chomp_hint) 02226 { 02227 if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) 02228 return 0; 02229 } 02230 02231 return 1; 02232 } 02233 02234 static int 02235 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, 02236 yaml_char_t *value, size_t length) 02237 { 02238 yaml_string_t string; 02239 int breaks = 1; 02240 02241 STRING_ASSIGN(string, value, length); 02242 02243 if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0)) 02244 return 0; 02245 if (!yaml_emitter_write_block_scalar_hints(emitter, string)) 02246 return 0; 02247 if (!PUT_BREAK(emitter)) return 0; 02248 emitter->indention = 1; 02249 emitter->whitespace = 1; 02250 02251 while (string.pointer != string.end) 02252 { 02253 if (IS_BREAK(string)) 02254 { 02255 if (!WRITE_BREAK(emitter, string)) return 0; 02256 emitter->indention = 1; 02257 breaks = 1; 02258 } 02259 else 02260 { 02261 if (breaks) { 02262 if (!yaml_emitter_write_indent(emitter)) return 0; 02263 } 02264 if (!WRITE(emitter, string)) return 0; 02265 emitter->indention = 0; 02266 breaks = 0; 02267 } 02268 } 02269 02270 return 1; 02271 } 02272 02273 static int 02274 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, 02275 yaml_char_t *value, size_t length) 02276 { 02277 yaml_string_t string; 02278 int breaks = 1; 02279 int leading_spaces = 1; 02280 02281 STRING_ASSIGN(string, value, length); 02282 02283 if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0)) 02284 return 0; 02285 if (!yaml_emitter_write_block_scalar_hints(emitter, string)) 02286 return 0; 02287 if (!PUT_BREAK(emitter)) return 0; 02288 emitter->indention = 1; 02289 emitter->whitespace = 1; 02290 02291 while (string.pointer != string.end) 02292 { 02293 if (IS_BREAK(string)) 02294 { 02295 if (!breaks && !leading_spaces && CHECK(string, '\n')) { 02296 int k = 0; 02297 while (IS_BREAK_AT(string, k)) { 02298 k += WIDTH_AT(string, k); 02299 } 02300 if (!IS_BLANKZ_AT(string, k)) { 02301 if (!PUT_BREAK(emitter)) return 0; 02302 } 02303 } 02304 if (!WRITE_BREAK(emitter, string)) return 0; 02305 emitter->indention = 1; 02306 breaks = 1; 02307 } 02308 else 02309 { 02310 if (breaks) { 02311 if (!yaml_emitter_write_indent(emitter)) return 0; 02312 leading_spaces = IS_BLANK(string); 02313 } 02314 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) 02315 && emitter->column > emitter->best_width) { 02316 if (!yaml_emitter_write_indent(emitter)) return 0; 02317 MOVE(string); 02318 } 02319 else { 02320 if (!WRITE(emitter, string)) return 0; 02321 } 02322 emitter->indention = 0; 02323 breaks = 0; 02324 } 02325 } 02326 02327 return 1; 02328 } 02329 02330