| 1 |
#include <yaml.h> |
|---|
| 2 |
|
|---|
| 3 |
#include <stdlib.h> |
|---|
| 4 |
#include <stdio.h> |
|---|
| 5 |
#include <string.h> |
|---|
| 6 |
|
|---|
| 7 |
#ifdef NDEBUG |
|---|
| 8 |
#undef NDEBUG |
|---|
| 9 |
#endif |
|---|
| 10 |
#include <assert.h> |
|---|
| 11 |
|
|---|
| 12 |
#define BUFFER_SIZE 65536 |
|---|
| 13 |
#define MAX_DOCUMENTS 16 |
|---|
| 14 |
|
|---|
| 15 |
#if 0 |
|---|
| 16 |
|
|---|
| 17 |
int copy_document(yaml_document_t *document_to, yaml_document_t *document_from) |
|---|
| 18 |
{ |
|---|
| 19 |
yaml_node_t *node; |
|---|
| 20 |
yaml_node_item_t *item; |
|---|
| 21 |
yaml_node_pair_t *pair; |
|---|
| 22 |
|
|---|
| 23 |
if (!yaml_document_initialize(document_to, document_from->version_directive, |
|---|
| 24 |
document_from->tag_directives.start, |
|---|
| 25 |
document_from->tag_directives.end, |
|---|
| 26 |
document_from->start_implicit, document_from->end_implicit)) |
|---|
| 27 |
return 0; |
|---|
| 28 |
|
|---|
| 29 |
for (node = document_from->nodes.start; |
|---|
| 30 |
node < document_from->nodes.top; node ++) { |
|---|
| 31 |
switch (node->type) { |
|---|
| 32 |
case YAML_SCALAR_NODE: |
|---|
| 33 |
if (!yaml_document_add_scalar(document_to, node->tag, |
|---|
| 34 |
node->data.scalar.value, node->data.scalar.length, |
|---|
| 35 |
node->data.scalar.style)) goto error; |
|---|
| 36 |
break; |
|---|
| 37 |
case YAML_SEQUENCE_NODE: |
|---|
| 38 |
if (!yaml_document_add_sequence(document_to, node->tag, |
|---|
| 39 |
node->data.sequence.style)) goto error; |
|---|
| 40 |
break; |
|---|
| 41 |
case YAML_MAPPING_NODE: |
|---|
| 42 |
if (!yaml_document_add_mapping(document_to, node->tag, |
|---|
| 43 |
node->data.mapping.style)) goto error; |
|---|
| 44 |
break; |
|---|
| 45 |
default: |
|---|
| 46 |
assert(0); |
|---|
| 47 |
break; |
|---|
| 48 |
} |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
for (node = document_from->nodes.start; |
|---|
| 52 |
node < document_from->nodes.top; node ++) { |
|---|
| 53 |
switch (node->type) { |
|---|
| 54 |
case YAML_SEQUENCE_NODE: |
|---|
| 55 |
for (item = node->data.sequence.items.start; |
|---|
| 56 |
item < node->data.sequence.items.top; item ++) { |
|---|
| 57 |
if (!yaml_document_append_sequence_item(document_to, |
|---|
| 58 |
node - document_from->nodes.start + 1, |
|---|
| 59 |
*item)) goto error; |
|---|
| 60 |
} |
|---|
| 61 |
break; |
|---|
| 62 |
case YAML_MAPPING_NODE: |
|---|
| 63 |
for (pair = node->data.mapping.pairs.start; |
|---|
| 64 |
pair < node->data.mapping.pairs.top; pair ++) { |
|---|
| 65 |
if (!yaml_document_append_mapping_pair(document_to, |
|---|
| 66 |
node - document_from->nodes.start + 1, |
|---|
| 67 |
pair->key, pair->value)) goto error; |
|---|
| 68 |
} |
|---|
| 69 |
break; |
|---|
| 70 |
default: |
|---|
| 71 |
break; |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
return 1; |
|---|
| 75 |
|
|---|
| 76 |
error: |
|---|
| 77 |
yaml_document_delete(document_to); |
|---|
| 78 |
return 0; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
int compare_nodes(yaml_document_t *document1, int index1, |
|---|
| 82 |
yaml_document_t *document2, int index2) |
|---|
| 83 |
{ |
|---|
| 84 |
yaml_node_t *node1 = yaml_document_get_node(document1, index1); |
|---|
| 85 |
yaml_node_t *node2 = yaml_document_get_node(document2, index2); |
|---|
| 86 |
int k; |
|---|
| 87 |
|
|---|
| 88 |
assert(node1); |
|---|
| 89 |
assert(node2); |
|---|
| 90 |
|
|---|
| 91 |
if (node1->type != node2->type) |
|---|
| 92 |
return 0; |
|---|
| 93 |
|
|---|
| 94 |
if (strcmp((char *)node1->tag, (char *)node2->tag) != 0) return 0; |
|---|
| 95 |
|
|---|
| 96 |
switch (node1->type) { |
|---|
| 97 |
case YAML_SCALAR_NODE: |
|---|
| 98 |
if (node1->data.scalar.length != node2->data.scalar.length) |
|---|
| 99 |
return 0; |
|---|
| 100 |
if (strncmp((char *)node1->data.scalar.value, (char *)node2->data.scalar.value, |
|---|
| 101 |
node1->data.scalar.length) != 0) return 0; |
|---|
| 102 |
break; |
|---|
| 103 |
case YAML_SEQUENCE_NODE: |
|---|
| 104 |
if ((node1->data.sequence.items.top - node1->data.sequence.items.start) != |
|---|
| 105 |
(node2->data.sequence.items.top - node2->data.sequence.items.start)) |
|---|
| 106 |
return 0; |
|---|
| 107 |
for (k = 0; k < (node1->data.sequence.items.top - node1->data.sequence.items.start); k ++) { |
|---|
| 108 |
if (!compare_nodes(document1, node1->data.sequence.items.start[k], |
|---|
| 109 |
document2, node2->data.sequence.items.start[k])) return 0; |
|---|
| 110 |
} |
|---|
| 111 |
break; |
|---|
| 112 |
case YAML_MAPPING_NODE: |
|---|
| 113 |
if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) != |
|---|
| 114 |
(node2->data.mapping.pairs.top - node2->data.mapping.pairs.start)) |
|---|
| 115 |
return 0; |
|---|
| 116 |
for (k = 0; k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start); k ++) { |
|---|
| 117 |
if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].key, |
|---|
| 118 |
document2, node2->data.mapping.pairs.start[k].key)) return 0; |
|---|
| 119 |
if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].value, |
|---|
| 120 |
document2, node2->data.mapping.pairs.start[k].value)) return 0; |
|---|
| 121 |
} |
|---|
| 122 |
break; |
|---|
| 123 |
default: |
|---|
| 124 |
assert(0); |
|---|
| 125 |
break; |
|---|
| 126 |
} |
|---|
| 127 |
return 1; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
int compare_documents(yaml_document_t *document1, yaml_document_t *document2) |
|---|
| 131 |
{ |
|---|
| 132 |
int k; |
|---|
| 133 |
|
|---|
| 134 |
if ((document1->version_directive && !document2->version_directive) |
|---|
| 135 |
|| (!document1->version_directive && document2->version_directive) |
|---|
| 136 |
|| (document1->version_directive && document2->version_directive |
|---|
| 137 |
&& (document1->version_directive->major != document2->version_directive->major |
|---|
| 138 |
|| document1->version_directive->minor != document2->version_directive->minor))) |
|---|
| 139 |
return 0; |
|---|
| 140 |
|
|---|
| 141 |
if ((document1->tag_directives.end - document1->tag_directives.start) != |
|---|
| 142 |
(document2->tag_directives.end - document2->tag_directives.start)) |
|---|
| 143 |
return 0; |
|---|
| 144 |
for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) { |
|---|
| 145 |
if ((strcmp((char *)document1->tag_directives.start[k].handle, |
|---|
| 146 |
(char *)document2->tag_directives.start[k].handle) != 0) |
|---|
| 147 |
|| (strcmp((char *)document1->tag_directives.start[k].prefix, |
|---|
| 148 |
(char *)document2->tag_directives.start[k].prefix) != 0)) |
|---|
| 149 |
return 0; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if ((document1->nodes.top - document1->nodes.start) != |
|---|
| 153 |
(document2->nodes.top - document2->nodes.start)) |
|---|
| 154 |
return 0; |
|---|
| 155 |
|
|---|
| 156 |
if (document1->nodes.top != document1->nodes.start) { |
|---|
| 157 |
if (!compare_nodes(document1, 1, document2, 1)) |
|---|
| 158 |
return 0; |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
return 1; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
int print_output(char *name, unsigned char *buffer, size_t size, int count) |
|---|
| 165 |
{ |
|---|
| 166 |
FILE *file; |
|---|
| 167 |
char data[BUFFER_SIZE]; |
|---|
| 168 |
size_t data_size = 1; |
|---|
| 169 |
size_t total_size = 0; |
|---|
| 170 |
if (count >= 0) { |
|---|
| 171 |
printf("FAILED (at the document #%d)\nSOURCE:\n", count+1); |
|---|
| 172 |
} |
|---|
| 173 |
file = fopen(name, "rb"); |
|---|
| 174 |
assert(file); |
|---|
| 175 |
while (data_size > 0) { |
|---|
| 176 |
data_size = fread(data, 1, BUFFER_SIZE, file); |
|---|
| 177 |
assert(!ferror(file)); |
|---|
| 178 |
if (!data_size) break; |
|---|
| 179 |
assert(fwrite(data, 1, data_size, stdout) == data_size); |
|---|
| 180 |
total_size += data_size; |
|---|
| 181 |
if (feof(file)) break; |
|---|
| 182 |
} |
|---|
| 183 |
fclose(file); |
|---|
| 184 |
printf("#### (length: %d)\n", total_size); |
|---|
| 185 |
printf("OUTPUT:\n%s#### (length: %d)\n", buffer, size); |
|---|
| 186 |
return 0; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
#endif |
|---|
| 190 |
|
|---|
| 191 |
int |
|---|
| 192 |
main(int argc, char *argv[]) |
|---|
| 193 |
{ |
|---|
| 194 |
#if 0 |
|---|
| 195 |
int number; |
|---|
| 196 |
int canonical = 0; |
|---|
| 197 |
int unicode = 0; |
|---|
| 198 |
|
|---|
| 199 |
number = 1; |
|---|
| 200 |
while (number < argc) { |
|---|
| 201 |
if (strcmp(argv[number], "-c") == 0) { |
|---|
| 202 |
canonical = 1; |
|---|
| 203 |
} |
|---|
| 204 |
else if (strcmp(argv[number], "-u") == 0) { |
|---|
| 205 |
unicode = 1; |
|---|
| 206 |
} |
|---|
| 207 |
else if (argv[number][0] == '-') { |
|---|
| 208 |
printf("Unknown option: '%s'\n", argv[number]); |
|---|
| 209 |
return 0; |
|---|
| 210 |
} |
|---|
| 211 |
if (argv[number][0] == '-') { |
|---|
| 212 |
if (number < argc-1) { |
|---|
| 213 |
memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *)); |
|---|
| 214 |
} |
|---|
| 215 |
argc --; |
|---|
| 216 |
} |
|---|
| 217 |
else { |
|---|
| 218 |
number ++; |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
if (argc < 2) { |
|---|
| 223 |
printf("Usage: %s [-c] [-u] file1.yaml ...\n", argv[0]); |
|---|
| 224 |
return 0; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
for (number = 1; number < argc; number ++) |
|---|
| 228 |
{ |
|---|
| 229 |
FILE *file; |
|---|
| 230 |
yaml_parser_t parser; |
|---|
| 231 |
yaml_emitter_t emitter; |
|---|
| 232 |
|
|---|
| 233 |
yaml_document_t document; |
|---|
| 234 |
unsigned char buffer[BUFFER_SIZE]; |
|---|
| 235 |
size_t written = 0; |
|---|
| 236 |
yaml_document_t documents[MAX_DOCUMENTS]; |
|---|
| 237 |
size_t document_number = 0; |
|---|
| 238 |
int done = 0; |
|---|
| 239 |
int count = 0; |
|---|
| 240 |
int error = 0; |
|---|
| 241 |
int k; |
|---|
| 242 |
memset(buffer, 0, BUFFER_SIZE); |
|---|
| 243 |
memset(documents, 0, MAX_DOCUMENTS*sizeof(yaml_document_t)); |
|---|
| 244 |
|
|---|
| 245 |
printf("[%d] Loading, dumping, and loading again '%s': ", number, argv[number]); |
|---|
| 246 |
fflush(stdout); |
|---|
| 247 |
|
|---|
| 248 |
file = fopen(argv[number], "rb"); |
|---|
| 249 |
assert(file); |
|---|
| 250 |
|
|---|
| 251 |
assert(yaml_parser_initialize(&parser)); |
|---|
| 252 |
yaml_parser_set_input_file(&parser, file); |
|---|
| 253 |
assert(yaml_emitter_initialize(&emitter)); |
|---|
| 254 |
if (canonical) { |
|---|
| 255 |
yaml_emitter_set_canonical(&emitter, 1); |
|---|
| 256 |
} |
|---|
| 257 |
if (unicode) { |
|---|
| 258 |
yaml_emitter_set_unicode(&emitter, 1); |
|---|
| 259 |
} |
|---|
| 260 |
yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written); |
|---|
| 261 |
yaml_emitter_open(&emitter); |
|---|
| 262 |
|
|---|
| 263 |
while (!done) |
|---|
| 264 |
{ |
|---|
| 265 |
if (!yaml_parser_load(&parser, &document)) { |
|---|
| 266 |
error = 1; |
|---|
| 267 |
break; |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
done = (!yaml_document_get_root_node(&document)); |
|---|
| 271 |
if (!done) { |
|---|
| 272 |
assert(document_number < MAX_DOCUMENTS); |
|---|
| 273 |
assert(copy_document(&(documents[document_number++]), &document)); |
|---|
| 274 |
assert(yaml_emitter_dump(&emitter, &document) || |
|---|
| 275 |
(yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count))); |
|---|
| 276 |
count ++; |
|---|
| 277 |
} |
|---|
| 278 |
else { |
|---|
| 279 |
yaml_document_delete(&document); |
|---|
| 280 |
} |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
yaml_parser_delete(&parser); |
|---|
| 284 |
assert(!fclose(file)); |
|---|
| 285 |
yaml_emitter_close(&emitter); |
|---|
| 286 |
yaml_emitter_delete(&emitter); |
|---|
| 287 |
|
|---|
| 288 |
if (!error) |
|---|
| 289 |
{ |
|---|
| 290 |
count = done = 0; |
|---|
| 291 |
assert(yaml_parser_initialize(&parser)); |
|---|
| 292 |
yaml_parser_set_input_string(&parser, buffer, written); |
|---|
| 293 |
|
|---|
| 294 |
while (!done) |
|---|
| 295 |
{ |
|---|
| 296 |
assert(yaml_parser_load(&parser, &document) || print_output(argv[number], buffer, written, count)); |
|---|
| 297 |
done = (!yaml_document_get_root_node(&document)); |
|---|
| 298 |
if (!done) { |
|---|
| 299 |
assert(compare_documents(documents+count, &document) || print_output(argv[number], buffer, written, count)); |
|---|
| 300 |
count ++; |
|---|
| 301 |
} |
|---|
| 302 |
yaml_document_delete(&document); |
|---|
| 303 |
} |
|---|
| 304 |
yaml_parser_delete(&parser); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
for (k = 0; k < document_number; k ++) { |
|---|
| 308 |
yaml_document_delete(documents+k); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
printf("PASSED (length: %d)\n", written); |
|---|
| 312 |
print_output(argv[number], buffer, written, -1); |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
#endif |
|---|
| 316 |
return 0; |
|---|
| 317 |
} |
|---|