12 #define BUFFER_SIZE 65536
13 #define MAX_DOCUMENTS 16
15 int copy_document(yaml_document_t *document_to, yaml_document_t *document_from)
18 yaml_node_item_t *item;
19 yaml_node_pair_t *pair;
21 if (!yaml_document_initialize(document_to, document_from->version_directive,
22 document_from->tag_directives.start,
23 document_from->tag_directives.end,
24 document_from->start_implicit, document_from->end_implicit))
27 for (node = document_from->nodes.start;
28 node < document_from->nodes.top; node ++) {
30 case YAML_SCALAR_NODE:
31 if (!yaml_document_add_scalar(document_to, node->tag,
32 node->data.scalar.value, node->data.scalar.length,
33 node->data.scalar.style)) goto error;
35 case YAML_SEQUENCE_NODE:
36 if (!yaml_document_add_sequence(document_to, node->tag,
37 node->data.sequence.style)) goto error;
39 case YAML_MAPPING_NODE:
40 if (!yaml_document_add_mapping(document_to, node->tag,
41 node->data.mapping.style)) goto error;
49 for (node = document_from->nodes.start;
50 node < document_from->nodes.top; node ++) {
52 case YAML_SEQUENCE_NODE:
53 for (item = node->data.sequence.items.start;
54 item < node->data.sequence.items.top; item ++) {
55 if (!yaml_document_append_sequence_item(document_to,
56 node - document_from->nodes.start + 1,
60 case YAML_MAPPING_NODE:
61 for (pair = node->data.mapping.pairs.start;
62 pair < node->data.mapping.pairs.top; pair ++) {
63 if (!yaml_document_append_mapping_pair(document_to,
64 node - document_from->nodes.start + 1,
65 pair->key, pair->value)) goto error;
75 yaml_document_delete(document_to);
79 int compare_nodes(yaml_document_t *document1, int index1,
80 yaml_document_t *document2, int index2)
82 yaml_node_t *node1 = yaml_document_get_node(document1, index1);
83 yaml_node_t *node2 = yaml_document_get_node(document2, index2);
89 if (node1->type != node2->type)
92 if (strcmp((char *)node1->tag, (char *)node2->tag) != 0) return 0;
94 switch (node1->type) {
95 case YAML_SCALAR_NODE:
96 if (node1->data.scalar.length != node2->data.scalar.length)
98 if (strncmp((char *)node1->data.scalar.value, (char *)node2->data.scalar.value,
99 node1->data.scalar.length) != 0) return 0;
101 case YAML_SEQUENCE_NODE:
102 if ((node1->data.sequence.items.top - node1->data.sequence.items.start) !=
103 (node2->data.sequence.items.top - node2->data.sequence.items.start))
105 for (k = 0; k < (node1->data.sequence.items.top - node1->data.sequence.items.start); k ++) {
106 if (!compare_nodes(document1, node1->data.sequence.items.start[k],
107 document2, node2->data.sequence.items.start[k])) return 0;
110 case YAML_MAPPING_NODE:
111 if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) !=
112 (node2->data.mapping.pairs.top - node2->data.mapping.pairs.start))
114 for (k = 0; k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start); k ++) {
115 if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].key,
116 document2, node2->data.mapping.pairs.start[k].key)) return 0;
117 if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].value,
118 document2, node2->data.mapping.pairs.start[k].value)) return 0;
128 int compare_documents(yaml_document_t *document1, yaml_document_t *document2)
132 if ((document1->version_directive && !document2->version_directive)
133 || (!document1->version_directive && document2->version_directive)
134 || (document1->version_directive && document2->version_directive
135 && (document1->version_directive->major != document2->version_directive->major
136 || document1->version_directive->minor != document2->version_directive->minor)))
139 if ((document1->tag_directives.end - document1->tag_directives.start) !=
140 (document2->tag_directives.end - document2->tag_directives.start))
142 for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) {
143 if ((strcmp((char *)document1->tag_directives.start[k].handle,
144 (char *)document2->tag_directives.start[k].handle) != 0)
145 || (strcmp((char *)document1->tag_directives.start[k].prefix,
146 (char *)document2->tag_directives.start[k].prefix) != 0))
150 if ((document1->nodes.top - document1->nodes.start) !=
151 (document2->nodes.top - document2->nodes.start))
154 if (document1->nodes.top != document1->nodes.start) {
155 if (!compare_nodes(document1, 1, document2, 1))
162 int print_output(char *name, unsigned char *buffer, size_t size, int count)
165 char data[BUFFER_SIZE];
166 size_t data_size = 1;
167 size_t total_size = 0;
169 printf("FAILED (at the document #%d)\nSOURCE:\n", count+1);
171 file = fopen(name, "rb");
173 while (data_size > 0) {
174 data_size = fread(data, 1, BUFFER_SIZE, file);
175 assert(!ferror(file));
176 if (!data_size) break;
177 assert(fwrite(data, 1, data_size, stdout) == data_size);
178 total_size += data_size;
179 if (feof(file)) break;
182 printf("#### (length: %d)\n", total_size);
183 printf("OUTPUT:\n%s#### (length: %d)\n", buffer, size);
188 main(int argc, char *argv[])
195 while (number < argc) {
196 if (strcmp(argv[number], "-c") == 0) {
199 else if (strcmp(argv[number], "-u") == 0) {
202 else if (argv[number][0] == '-') {
203 printf("Unknown option: '%s'\n", argv[number]);
206 if (argv[number][0] == '-') {
207 if (number < argc-1) {
208 memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *));
218 printf("Usage: %s [-c] [-u] file1.yaml ...\n", argv[0]);
222 for (number = 1; number < argc; number ++)
225 yaml_parser_t parser;
226 yaml_emitter_t emitter;
228 yaml_document_t document;
229 unsigned char buffer[BUFFER_SIZE];
231 yaml_document_t documents[MAX_DOCUMENTS];
232 size_t document_number = 0;
237 memset(buffer, 0, BUFFER_SIZE);
238 memset(documents, 0, MAX_DOCUMENTS*sizeof(yaml_document_t));
240 printf("[%d] Loading, dumping, and loading again '%s': ", number, argv[number]);
243 file = fopen(argv[number], "rb");
246 assert(yaml_parser_initialize(&parser));
247 yaml_parser_set_input_file(&parser, file);
248 assert(yaml_emitter_initialize(&emitter));
250 yaml_emitter_set_canonical(&emitter, 1);
253 yaml_emitter_set_unicode(&emitter, 1);
255 yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written);
256 yaml_emitter_open(&emitter);
260 if (!yaml_parser_load(&parser, &document)) {
265 done = (!yaml_document_get_root_node(&document));
267 assert(document_number < MAX_DOCUMENTS);
268 assert(copy_document(&(documents[document_number++]), &document));
269 assert(yaml_emitter_dump(&emitter, &document) ||
270 (yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count)));
274 yaml_document_delete(&document);
278 yaml_parser_delete(&parser);
279 assert(!fclose(file));
280 yaml_emitter_close(&emitter);
281 yaml_emitter_delete(&emitter);
286 assert(yaml_parser_initialize(&parser));
287 yaml_parser_set_input_string(&parser, buffer, written);
291 assert(yaml_parser_load(&parser, &document) || print_output(argv[number], buffer, written, count));
292 done = (!yaml_document_get_root_node(&document));
294 assert(compare_documents(documents+count, &document) || print_output(argv[number], buffer, written, count));
297 yaml_document_delete(&document);
299 yaml_parser_delete(&parser);
302 for (k = 0; k < document_number; k ++) {
303 yaml_document_delete(documents+k);
306 printf("PASSED (length: %d)\n", written);
307 print_output(argv[number], buffer, written, -1);