forked from thomas-robinson/diag_manager2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfms_diag_parser.c
134 lines (128 loc) · 3.97 KB
/
fms_diag_parser.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <yaml.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fms_diag_parser.h>
enum state_value {
START,
ACCEPT_SECTION,
ACCEPT_LIST,
ACCEPT_VALUES,
ACCEPT_KEY,
ACCEPT_VALUE,
STOP,
ERROR,
};
int consume_event(struct parser_state *s, yaml_event_t *event)
{
s->accepted = 0;
switch (s->state) {
case START:
switch (event->type) {
case YAML_MAPPING_START_EVENT:
s->state = ACCEPT_SECTION;
break;
case YAML_SCALAR_EVENT:
fprintf(stderr, "Ignoring unexpected scalar: %s\n",
(char*)event->data.scalar.value);
break;
case YAML_SEQUENCE_START_EVENT:
fprintf(stderr, "Unexpected sequence.\n");
s->state = ERROR;
break;
case YAML_STREAM_END_EVENT: s->state = STOP; break;
default:
break;
}
break;
case ACCEPT_SECTION:
switch (event->type) {
case YAML_SCALAR_EVENT:
if (strcmp((char*)event->data.scalar.value, "files") == 0) {
s->state = ACCEPT_LIST;
} else {
fprintf(stderr, "Unexpected scalar: %s\n",
(char*)event->data.scalar.value);
s->state = ERROR;
}
break;
default:
fprintf(stderr, "Unexpected event while getting scalar: %d\n", event->type);
s->state = ERROR;
break;
}
break;
case ACCEPT_LIST:
switch (event->type) {
case YAML_SEQUENCE_START_EVENT: s->state = ACCEPT_VALUES; break;
default:
fprintf(stderr, "Unexpected event while getting sequence: %d\n", event->type);
s->state = ERROR;
break;
}
break;
case ACCEPT_VALUES:
switch (event->type) {
case YAML_MAPPING_START_EVENT:
memset(&(s->data), 0, sizeof(s->data));
s->state = ACCEPT_KEY;
break;
case YAML_SEQUENCE_END_EVENT: s->state = START; break;
case YAML_DOCUMENT_END_EVENT: s->state = START; break;
default:
fprintf(stderr, "Unexpected event while getting mapped values: %d\n",
event->type);
s->state = ERROR;
break;
}
break;
case ACCEPT_KEY:
switch (event->type) {
case YAML_SCALAR_EVENT:
s->key = strdup((char*)event->data.scalar.value);
s->state = ACCEPT_VALUE;
break;
case YAML_MAPPING_END_EVENT:
s->accepted = 1;
s->state = ACCEPT_VALUES;
break;
default:
fprintf(stderr, "Unexpected event while getting key: %d\n",
event->type);
s->state = ERROR;
break;
}
break;
case ACCEPT_VALUE:
switch (event->type) {
case YAML_SCALAR_EVENT:
s->value = (char*)event->data.scalar.value;
if (strcmp(s->key, "name") == 0) {
s->data.name = strdup((char*)s->value);
} else if (strcmp(s->key, "frequnit") == 0) {
s->data.frequnit = strdup((char*)s->value);
} else if (strcmp(s->key, "freq") == 0) {
s->data.freq = atoi(s->value);
} else if (strcmp(s->key, "timeunit") == 0) {
s->data.timeunit = strdup((char*)s->value);
} else if (strcmp(s->key, "unlimdim") == 0) {
s->data.unlimdim = strdup((char*)s->value);
} else {
fprintf(stderr, "Ignoring unknown key: %s\n", s->key);
}
free(s->key);
s->state = ACCEPT_KEY;
break;
default:
fprintf(stderr, "Unexpected event while getting value: %d\n",
event->type);
s->state = ERROR;
break;
}
break;
case ERROR:
case STOP:
break;
}
return (s->state == ERROR ? 0 : 1);
}