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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
%% options
%% header
#ifdef __cplusplus
extern "C" {
#endif
/** Convert input line into key value pairs.
@param kvp Array of key value structures for results.
@param szp Size of array (in), used elements (out).
@param il Input line to process.
@param app Application structure for diagnostics, may be NULL.
@return 1 on success (number of elements in sz), 0 on error.
*/
int
dk3str_to_key_value(
dk3_key_value_t *kvp,
size_t *szp,
dkChar *il,
dk3_app_t *app
);
#ifdef __cplusplus
}
#endif
%% state machine
[options]
name = dk3strkv_stm
write header = no
[states]
ST_START # Waiting for anything to happen
ST_KEY # In a key
ST_EXPECT # Expect value
ST_VALUE # Having value
ST_SQ_EXPECT # Expect single quoted value
ST_DQ_EXPECT # Expect double quoted value
ST_SQ_VALUE # Having single quoted value
ST_DQ_VALUE # Having double quoted value
ST_ERROR # Error occured
[inputs]
I_OT # Any other character
I_WH # White space
I_SQ # Single quote
I_DQ # Double quote
I_BS # Backslash
I_EQ # Equal sign
[output]
O_ERROR # Error occured
O_NO # Do nothing
O_SQ # Squeeze
O_SKEY # Start key
O_ENDKEY # End key string (change to finalizer)
O_ENDCOUNT # End key string and count upwards
O_ENDVAL # End value string (change to finalizer)
O_SVAL # Start value
O_SQ_SVAL # Squeeze and start value
O_SEVAL # Start and end value
[rules]
* * ST_ERROR O_ERROR
ST_START I_WH ST_START O_NO
ST_START I_OT ST_KEY O_SKEY
ST_KEY * ST_KEY O_NO
ST_KEY I_WH ST_START O_ENDCOUNT
ST_KEY I_EQ ST_EXPECT O_ENDKEY
ST_EXPECT I_WH ST_EXPECT O_NO
ST_EXPECT * ST_VALUE O_SVAL
ST_EXPECT I_BS ST_VALUE O_SQ_SVAL
ST_EXPECT I_SQ ST_SQ_EXPECT O_NO
ST_EXPECT I_DQ ST_DQ_EXPECT O_NO
ST_VALUE * ST_VALUE O_NO
ST_VALUE I_BS ST_VALUE O_SQ
ST_VALUE I_WH ST_START O_ENDVAL
ST_SQ_EXPECT * ST_SQ_VALUE O_SVAL
ST_SQ_EXPECT I_BS ST_SQ_VALUE O_SQ_SVAL
ST_SQ_EXPECT I_SQ ST_START O_SEVAL
ST_SQ_VALUE * ST_SQ_VALUE O_NO
ST_SQ_VALUE I_SQ ST_START O_ENDVAL
ST_SQ_VALUE I_BS ST_SQ_VALUE O_SQ
ST_DQ_EXPECT * ST_DQ_VALUE O_SVAL
ST_DQ_EXPECT I_BS ST_DQ_VALUE O_SQ_SVAL
ST_DQ_EXPECT I_DQ ST_START O_SEVAL
ST_DQ_VALUE * ST_DQ_VALUE O_NO
ST_DQ_VALUE I_BS ST_DQ_VALUE O_SQ
ST_DQ_VALUE I_DQ ST_START O_ENDVAL
%% module
#include "dk3all.h"
#include "dk3strkv.h"
/** Classify input character
@param c Character to classify.
@return Character class.
*/
static
int
dk3strkv_classify(dkChar c)
{
int back = I_OT;
$? "+ dk3strkv_classify %d", (int)c
switch(c) {
case dkT(' '): case dkT('\t'): {
back = I_WH;
} break;
case dkT('"'): {
back = I_DQ;
} break;
case dkT('\''): {
back = I_SQ;
} break;
case dkT('\\'): {
back = I_BS;
} break;
case dkT('='): {
back = I_EQ;
} break;
} $? "- dk3strkv_classify %d", back
return back;
}
/** Empty line.
*/
static dkChar const dk3strkv_empty_line[] = { dkT("") };
/** Report error for unexpected end of text.
@param app Application structure for diagnostics, may be NULL.
@param il Input line to complain about.
*/
static
void
dk3strkv_error_unexpected_eot(dk3_app_t *app, dkChar *il)
{
dkChar const *ptr;
if(app) {
ptr = il;
if(!(ptr)) { ptr = dk3strkv_empty_line; }
dk3app_log_i3(app, DK3_LL_ERROR, 389, 390, ptr);
}
}
/** Report error, too many key value items.
@param app Application structure for diagnostics, may be NULL.
@param il Input line to complain about.
@param pos Position in line.
*/
static
void
dk3strkv_error_too_many(dk3_app_t *app, dkChar *il, unsigned long pos)
{
dkChar buffer[64];
dkChar const *ptr;
if(app) {
ptr = il;
if(!(ptr)) { ptr = dk3strkv_empty_line; }
#if VERSION_BEFORE_20140716
dk3sf_sprintf3(buffer, dkT("%lu"), pos);
dk3app_log_i5(app, DK3_LL_ERROR, 386, 387, 388, buffer, ptr);
#else
if (dk3ma_um_to_string(buffer, DK3_SIZEOF(buffer, dkChar), (dk3_um_t)pos)) {
dk3app_log_i5(app, DK3_LL_ERROR, 386, 387, 388, buffer, ptr);
}
#endif
}
}
int
dk3str_to_key_value(
dk3_key_value_t *kvp,
size_t *szp,
dkChar *il,
dk3_app_t *app
)
{
dkChar *ptr; /* Traverse string. */
dkChar *mycopy; /* Private copy for diagnostics. */
unsigned long charno; /* Character number. */
size_t i; /* Current key value pair index. */
int icl; /* Input class. */
int act; /* Action to take. */
int stm; /* State machine. */
int cc; /* Flag: Can continue. */
int rtm = 0; /* Reported too many. */
int error = 0; /* Flag: Have error. */
int back = 0;
$? "+ dk3str_to_key_value \"%!ds\"", il
if((kvp) && (szp)) {
for(i = 0; i < *szp; i++) { kvp[i].key = NULL; kvp[i].val = NULL; }
}
if((kvp) && (szp) && (il)) {
if(*szp) {
mycopy = dk3str_dup_app(il, app);
i = 0;
ptr = il; cc = 1; charno = 1UL;
dk3strkv_stm_reset(&stm);
while(cc) {
if(*ptr) { $? ". begin of loop \"%!ds\"", ptr
icl = dk3strkv_classify(*ptr); $? ". icl = %d", icl
act = dk3strkv_stm_step(&stm, icl); $? ". act = %d", act
switch(act) {
case O_ERROR: { $? ". error"
error = 1;
} break;
case O_SQ: { $? ". O_SQ"
dk3str_cpy(ptr, &(ptr[1]));
if(*ptr) {
charno++;
} else {
cc = 0;
error = 1;
dk3strkv_error_unexpected_eot(app, mycopy);
}
} break;
case O_SKEY: { $? ". O_SKEY"
if(i < (*szp)) {
kvp[i].key = ptr;
} else {
error = 1;
if(0 == rtm) {
rtm = 1;
dk3strkv_error_too_many(app, mycopy, charno);
}
}
} break;
case O_ENDKEY: { $? ". O_ENDKEY"
*ptr = dkT('\0');
} break;
case O_ENDCOUNT: { $? ". O_ENDCOUNT"
*ptr = dkT('\0');
i++;
} break;
case O_ENDVAL: { $? ". O_ENDVAL"
*ptr = dkT('\0');
i++;
} break;
case O_SVAL: { $? ". O_SVAL"
if(i < (*szp)) {
kvp[i].val = ptr;
} else {
error = 1;
if(0 == rtm) {
rtm = 1;
dk3strkv_error_too_many(app, mycopy, charno);
}
}
} break;
case O_SQ_SVAL: { $? ". O_SQ_SVAL"
dk3str_cpy(ptr, &(ptr[1]));
if(*ptr) {
charno++;
if(i < (*szp)) {
kvp[i].val = ptr;
} else {
error = 1;
if(0 == rtm) {
rtm = 1;
dk3strkv_error_too_many(app, mycopy, charno);
}
}
} else {
cc = 0;
error = 1;
dk3strkv_error_unexpected_eot(app, mycopy);
}
} break;
case O_SEVAL: { $? ". O_SEVAL"
*ptr = dkT('\0');
if(i < (*szp)) {
kvp[i].val = ptr;
i++;
} else {
error = 1;
if(0 == rtm) {
rtm = 1;
dk3strkv_error_too_many(app, mycopy, charno);
}
}
} break;
}
ptr++;
charno++;
} else {
cc = 0;
}
}
switch(stm) {
case ST_START: { $? ". nothing to finish"
if(0 == error) {
back = 1;
*szp = i;
}
} break;
case ST_KEY: { $? ". finish key"
if(0 == error) {
i++;
back = 1;
*szp = i;
}
} break;
case ST_VALUE: { $? ". finish value"
if(0 == error) {
i++;
back = 1;
*szp = i;
}
} break;
}
dk3_release(mycopy);
}
} else {
if((kvp) && (szp) && (!(il))) {
*szp = 0;
back = 1;
}
} $? "- dk3str_to_key_value %d", back
return back;
}
|