-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
77 lines (68 loc) · 2.23 KB
/
test.c
File metadata and controls
77 lines (68 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include "libyaml/include/yaml.h"
static void parse_yaml(char *filename) {
FILE *fh = fopen(filename, "r");
yaml_parser_t parser;
/* Initialize parser */
if (!yaml_parser_initialize(&parser))
fputs("Failed to initialize parser!\n", stderr);
if (fh == NULL) fputs("Failed to open file!\n", stderr);
/* Set input file */
yaml_parser_set_input_file(&parser, fh);
yaml_token_t token;
do {
yaml_parser_scan(&parser, &token);
switch (token.type) {
/* Stream start/end */
case YAML_STREAM_START_TOKEN:
puts("STREAM START");
break;
case YAML_STREAM_END_TOKEN:
puts("STREAM END");
break;
/* Token types (read before actual token) */
case YAML_KEY_TOKEN:
printf("(Key token) ");
break;
case YAML_VALUE_TOKEN:
printf("(Value token) ");
break;
/* Block delimeters */
case YAML_BLOCK_SEQUENCE_START_TOKEN:
puts("<b>Start Block (Sequence)</b>");
break;
case YAML_BLOCK_ENTRY_TOKEN:
puts("<b>Start Block (Entry)</b>");
break;
case YAML_BLOCK_END_TOKEN:
puts("<b>End block</b>");
break;
/* Data */
case YAML_BLOCK_MAPPING_START_TOKEN:
puts("[Block mapping]");
break;
case YAML_SCALAR_TOKEN:
printf("scalar %s \n", token.data.scalar.value);
break;
/* Others */
default:
printf("Got token of type %d\n", token.type);
}
if (token.type != YAML_STREAM_END_TOKEN) yaml_token_delete(&token);
} while (token.type != YAML_STREAM_END_TOKEN);
yaml_token_delete(&token);
/* Cleanup */
yaml_parser_delete(&parser);
fclose(fh);
}
void print_yaml_version(void) {
int major, minor, patch;
yaml_get_version(&major, &minor, &patch);
printf("yaml lib: %d.%d.%d\n", major, minor, patch);
}
int main(int argc, char **argv) {
if (argc > 1) {
parse_yaml(argv[1]);
}
return 0;
}