-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvnlog-parser.c
281 lines (234 loc) · 7.59 KB
/
vnlog-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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#define _GNU_SOURCE // for tdestroy()
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <search.h>
#include "vnlog-parser.h"
#define MSG(fmt, ...) \
fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
typedef struct
{
// internal
char* line;
size_t n;
void* dict_key_index;
} vnlog_parser_internal_t;
_Static_assert( sizeof(vnlog_parser_internal_t) <=
sizeof(vnlog_parser_t) - offsetof(vnlog_parser_t, _internal),
"vnlog_parser_internal_t must fit in the allotted space");
static
bool accumulate_legend(// out
int* Ncolumns_allocated,
int* i_col,
vnlog_keyvalue_t** record,
// in
char* str)
{
if(*str == '\0')
// Empty string. Nothing to do
return true;
if(*Ncolumns_allocated <= *i_col)
{
(*Ncolumns_allocated) += 1;
(*Ncolumns_allocated) *= 2;
*record = (vnlog_keyvalue_t*)realloc(*record,
(*Ncolumns_allocated) * sizeof((*record)[0]));
if(*record == NULL)
{
MSG("Couldn't allocate record");
return false;
}
}
(*record)[*i_col].value = NULL;
(*record)[*i_col].key = strdup(str);
if((*record)[*i_col].key == NULL)
return false;
(*i_col)++;
return true;
}
static
bool accumulate_data_row(// out
vnlog_keyvalue_t* record,
int* i_col,
// in
int Ncolumns,
char* str)
{
if(*str == '\0')
// Empty string. Nothing to do
return true;
if(Ncolumns == 0)
{
MSG("Saw data line before a legend line");
return false;
}
if((*i_col) >= Ncolumns)
{
MSG("legend said we have %d columns, but saw a data line that has too many", Ncolumns);
return false;
}
record[(*i_col)++].value = str;
return true;
}
static
vnlog_parser_result_t read_line(vnlog_parser_t* ctx, FILE* fp)
{
int Ncolumns_allocated = 0;
vnlog_parser_internal_t* internal = (vnlog_parser_internal_t*)ctx->_internal;
while(true)
{
if(0 > getline(&internal->line, &internal->n, fp))
{
if(feof(fp))
// done reading file
return VNL_EOF;
MSG("vnl_error reading file: %d", errno);
return VNL_ERROR;
}
// Have one line. Parse it.
char* token;
char* string_to_tokenize = internal->line;
int i_col = 0;
const bool legend_is_done = (ctx->record != NULL);
bool parsing_legend_now = false;
while(NULL != (token = strtok(string_to_tokenize, " \t\n")))
{
string_to_tokenize = NULL;
if(token[0] == '#')
{
if(token[1] == '#' || token[1] == '!')
// hard comment
break;
if(i_col > 0)
// The legend can only appear in the first column
break;
if(legend_is_done)
// Already parsed the legend. The rest of this line is
// comments to be ignored
break;
if(parsing_legend_now)
// We already started the legend. This comment finishes it
break;
// This is the start of the legend. Parse all the columns
parsing_legend_now = true;
if(!accumulate_legend(&Ncolumns_allocated,
&i_col,
&ctx->record,
&token[1]))
return VNL_ERROR;
// grab next token from this line
continue;
}
if(parsing_legend_now)
{
if(!accumulate_legend(&Ncolumns_allocated,
&i_col,
&ctx->record,
token))
return VNL_ERROR;
continue;
}
// Data token
if(!accumulate_data_row(ctx->record,
&i_col,
ctx->Ncolumns,
token))
return VNL_ERROR;
}
// Finished line
if(i_col == 0)
{
// Empty line. Get another.
parsing_legend_now = false;
continue;
}
if(parsing_legend_now)
ctx->Ncolumns = i_col;
else if(i_col != ctx->Ncolumns)
{
MSG("Legend has %d columns, but just saw a data line of %d columns",
ctx->Ncolumns, i_col);
return VNL_ERROR;
}
// Done. All good!
return VNL_OK;
}
MSG("Getting here is a bug");
return VNL_ERROR;
}
static
int compare_record(const vnlog_keyvalue_t* a, const vnlog_keyvalue_t* b)
{
return strcmp(a->key, b->key);
}
static void noop_free(void* _dummy __attribute__((unused)) )
{
}
vnlog_parser_result_t vnlog_parser_init(vnlog_parser_t* ctx, FILE* fp)
{
*ctx = (vnlog_parser_t){};
vnlog_parser_result_t result = read_line(ctx, fp);
if(result != VNL_OK)
{
vnlog_parser_free(ctx);
return result;
}
vnlog_parser_internal_t* internal = (vnlog_parser_internal_t*)ctx->_internal;
// Parsed the legend. Now create a tree to make it easy to look up the
// specific column by key name. Probably these will be called once per run,
// so it could be a simple linear search, but a binary tree is easy-enough
// to use, so I do that
for(int i=0; i<ctx->Ncolumns; i++)
{
if(NULL ==
tsearch( (const void*)&ctx->record[i],
&internal->dict_key_index,
(int(*)(const void*,const void*))compare_record))
return VNL_ERROR;
}
return VNL_OK;
}
void vnlog_parser_free(vnlog_parser_t* ctx)
{
if(ctx != NULL)
{
vnlog_parser_internal_t* internal = (vnlog_parser_internal_t*)ctx->_internal;
free(internal->line);
if(ctx->record != NULL)
{
for(int i=0; i<ctx->Ncolumns; i++)
free(ctx->record[i].key);
}
free(ctx->record);
tdestroy(internal->dict_key_index, &noop_free);
}
*ctx = (vnlog_parser_t){};
}
vnlog_parser_result_t vnlog_parser_read_record(vnlog_parser_t* ctx, FILE* fp)
{
if(ctx == NULL || ctx->record == NULL)
{
MSG("Legend hasn't been read. Call vnlog_parser_init() first");
return VNL_ERROR;
}
vnlog_parser_result_t result = read_line(ctx, fp);
if(result != VNL_OK)
vnlog_parser_free(ctx);
return result;
}
// pointer to the pointer to the string for the record corresponding to the
// given key in the most-recently-parsed row. NULL if the given key isn't found
const char*const* vnlog_parser_record_from_key(vnlog_parser_t* ctx, const char* key)
{
vnlog_parser_internal_t* internal = (vnlog_parser_internal_t*)ctx->_internal;
const vnlog_keyvalue_t*const* keyvalue =
tfind( (const void*)&(const vnlog_keyvalue_t){.key = (char*)key},
&internal->dict_key_index,
(int(*)(const void*,const void*))compare_record);
if(keyvalue == NULL)
return NULL;
return (const char*const*)&(*keyvalue)->value;
}