Created
March 8, 2012 11:56
-
-
Save rogerz/2000693 to your computer and use it in GitHub Desktop.
yaml scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <yaml.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#ifdef NDEBUG | |
#undef NDEBUG | |
#endif | |
#include <assert.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
int number; | |
if (argc < 2) { | |
printf("Usage: %s file1.yaml ...\n", argv[0]); | |
return 0; | |
} | |
for (number = 1; number < argc; number ++) | |
{ | |
FILE *file; | |
yaml_parser_t parser; | |
yaml_parser_t parser_target; | |
char query[] = "title: $"; | |
char result[20] = ""; | |
yaml_token_t token, token_target; | |
int done = 0; | |
int count = 0; | |
int error = 0; | |
int begin = 0; | |
int match = 0; | |
printf("[%d] Scanning '%s': ", number, argv[number]); | |
fflush(stdout); | |
file = fopen(argv[number], "rb"); | |
assert(file); | |
assert(yaml_parser_initialize(&parser)); | |
assert(yaml_parser_initialize(&parser_target)); | |
yaml_parser_set_input_file(&parser, file); | |
yaml_parser_set_input_string(&parser_target, query, sizeof(query) - 1); | |
while (!done) | |
{ | |
if (!begin || match) { | |
if (!yaml_parser_scan(&parser_target, &token_target)) { | |
error = 1; | |
break; | |
} | |
if (token_target.type == YAML_STREAM_END_TOKEN) | |
break; | |
// skip ignored tokens | |
if (!(token_target.type == YAML_KEY_TOKEN | |
|| token_target.type == YAML_VALUE_TOKEN | |
|| token_target.type == YAML_SCALAR_TOKEN)) | |
continue; | |
begin = 1; | |
} | |
if (!yaml_parser_scan(&parser, &token)) { | |
error = 1; | |
break; | |
} | |
count++; | |
if (token.type == YAML_STREAM_END_TOKEN) | |
break; | |
if (!(token_target.type == YAML_KEY_TOKEN | |
|| token_target.type == YAML_VALUE_TOKEN | |
|| token_target.type == YAML_SCALAR_TOKEN)) | |
continue; | |
if (token.type == token_target.type) { // TODO: ignore flow and block difference | |
if (memcmp(&token.data, &token_target.data, sizeof(token.data)) == 0) { | |
match = 1; | |
} else if (token_target.type == YAML_SCALAR_TOKEN && token_target.data.scalar.value[0] == '$') { | |
strncpy(result, token_target.data.scalar.value, sizeof(result)); | |
} | |
} else if (match) { | |
yaml_token_delete(&token_target); | |
yaml_parser_delete(&parser_target); | |
assert(yaml_parser_initialize(&parser_target)); | |
yaml_parser_set_input_string(&parser_target, query, sizeof(query)); | |
begin = 0; | |
match = 0; | |
} | |
} | |
yaml_token_delete(&token); | |
yaml_parser_delete(&parser); | |
yaml_token_delete(&token_target); | |
yaml_parser_delete(&parser_target); | |
assert(!fclose(file)); | |
printf("%s (%d tokens)\nresult %s\n", (error || !match ? "FAILURE" : "SUCCESS"), count, result); | |
} | |
return 0; | |
} | |
int yaml_scanf(FILE *stream, const char *format, ...) | |
{ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment