summaryrefslogtreecommitdiff
path: root/support/dktools/dk3strkv.c
blob: 1f89f03519afa69cbe1bc0272aadd458ed3fecb3 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3strkv.ctr
*/

/**	@file dk3strkv.c The dk3strkv module.
*/

/** @defgroup dkct_state_machine_dk3strkv_stm The dk3strkv_stm state machine. */
/**@{*/

#ifndef ST_DQ_EXPECT
/** State: Expect double quoted value */
#line 41 "dk3strkv.ctr"
#define ST_DQ_EXPECT 0
#else
#line 41 "dk3strkv.ctr"
#error "Redefinition of ST_DQ_EXPECT"
#endif

#ifndef ST_DQ_VALUE
/** State: Having double quoted value */
#line 43 "dk3strkv.ctr"
#define ST_DQ_VALUE 1
#else
#line 43 "dk3strkv.ctr"
#error "Redefinition of ST_DQ_VALUE"
#endif

#ifndef ST_ERROR
/** State: Error occured */
#line 44 "dk3strkv.ctr"
#define ST_ERROR 2
#else
#line 44 "dk3strkv.ctr"
#error "Redefinition of ST_ERROR"
#endif

#ifndef ST_EXPECT
/** State: Expect value */
#line 38 "dk3strkv.ctr"
#define ST_EXPECT 3
#else
#line 38 "dk3strkv.ctr"
#error "Redefinition of ST_EXPECT"
#endif

#ifndef ST_KEY
/** State: In a key */
#line 37 "dk3strkv.ctr"
#define ST_KEY 4
#else
#line 37 "dk3strkv.ctr"
#error "Redefinition of ST_KEY"
#endif

#ifndef ST_SQ_EXPECT
/** State: Expect single quoted value */
#line 40 "dk3strkv.ctr"
#define ST_SQ_EXPECT 5
#else
#line 40 "dk3strkv.ctr"
#error "Redefinition of ST_SQ_EXPECT"
#endif

#ifndef ST_SQ_VALUE
/** State: Having single quoted value */
#line 42 "dk3strkv.ctr"
#define ST_SQ_VALUE 6
#else
#line 42 "dk3strkv.ctr"
#error "Redefinition of ST_SQ_VALUE"
#endif

#ifndef ST_START
/** State: Waiting for anything to happen */
#line 36 "dk3strkv.ctr"
#define ST_START 7
#else
#line 36 "dk3strkv.ctr"
#error "Redefinition of ST_START"
#endif

#ifndef ST_VALUE
/** State: Having value */
#line 39 "dk3strkv.ctr"
#define ST_VALUE 8
#else
#line 39 "dk3strkv.ctr"
#error "Redefinition of ST_VALUE"
#endif

#ifndef I_BS
/** State machine input: Backslash */
#line 50 "dk3strkv.ctr"
#define I_BS 0
#else
#line 50 "dk3strkv.ctr"
#error "Redefinition of I_BS"
#endif

#ifndef I_DQ
/** State machine input: Double quote */
#line 49 "dk3strkv.ctr"
#define I_DQ 1
#else
#line 49 "dk3strkv.ctr"
#error "Redefinition of I_DQ"
#endif

#ifndef I_EQ
/** State machine input: Equal sign */
#line 51 "dk3strkv.ctr"
#define I_EQ 2
#else
#line 51 "dk3strkv.ctr"
#error "Redefinition of I_EQ"
#endif

#ifndef I_OT
/** State machine input: Any other character */
#line 46 "dk3strkv.ctr"
#define I_OT 3
#else
#line 46 "dk3strkv.ctr"
#error "Redefinition of I_OT"
#endif

#ifndef I_SQ
/** State machine input: Single quote */
#line 48 "dk3strkv.ctr"
#define I_SQ 4
#else
#line 48 "dk3strkv.ctr"
#error "Redefinition of I_SQ"
#endif

#ifndef I_WH
/** State machine input: White space */
#line 47 "dk3strkv.ctr"
#define I_WH 5
#else
#line 47 "dk3strkv.ctr"
#error "Redefinition of I_WH"
#endif

#ifndef O_ENDCOUNT
/** State machine output: End key string and count upwards */
#line 58 "dk3strkv.ctr"
#define O_ENDCOUNT 0
#else
#line 58 "dk3strkv.ctr"
#error "Redefinition of O_ENDCOUNT"
#endif

#ifndef O_ENDKEY
/** State machine output: End key string (change to finalizer) */
#line 57 "dk3strkv.ctr"
#define O_ENDKEY 1
#else
#line 57 "dk3strkv.ctr"
#error "Redefinition of O_ENDKEY"
#endif

#ifndef O_ENDVAL
/** State machine output: End value string (change to finalizer) */
#line 59 "dk3strkv.ctr"
#define O_ENDVAL 2
#else
#line 59 "dk3strkv.ctr"
#error "Redefinition of O_ENDVAL"
#endif

#ifndef O_ERROR
/** State machine output: Error occured */
#line 53 "dk3strkv.ctr"
#define O_ERROR 3
#else
#line 53 "dk3strkv.ctr"
#error "Redefinition of O_ERROR"
#endif

#ifndef O_NO
/** State machine output: Do nothing */
#line 54 "dk3strkv.ctr"
#define O_NO 4
#else
#line 54 "dk3strkv.ctr"
#error "Redefinition of O_NO"
#endif

#ifndef O_SEVAL
/** State machine output: Start and end value */
#line 62 "dk3strkv.ctr"
#define O_SEVAL 5
#else
#line 62 "dk3strkv.ctr"
#error "Redefinition of O_SEVAL"
#endif

#ifndef O_SKEY
/** State machine output: Start key */
#line 56 "dk3strkv.ctr"
#define O_SKEY 6
#else
#line 56 "dk3strkv.ctr"
#error "Redefinition of O_SKEY"
#endif

#ifndef O_SQ
/** State machine output: Squeeze */
#line 55 "dk3strkv.ctr"
#define O_SQ 7
#else
#line 55 "dk3strkv.ctr"
#error "Redefinition of O_SQ"
#endif

#ifndef O_SQ_SVAL
/** State machine output: Squeeze and start value */
#line 61 "dk3strkv.ctr"
#define O_SQ_SVAL 8
#else
#line 61 "dk3strkv.ctr"
#error "Redefinition of O_SQ_SVAL"
#endif

#ifndef O_SVAL
/** State machine output: Start value */
#line 60 "dk3strkv.ctr"
#define O_SVAL 9
#else
#line 60 "dk3strkv.ctr"
#error "Redefinition of O_SVAL"
#endif

/**@}*/

/**	Reset dk3strkv_stm state machine.
	@param	st	Pointer to state variable.
*/
static
void
dk3strkv_stm_reset(int *st)
{
  if(st) { *st = ST_START; }
}

/**	State machine dk3strkv_stm step.
	@param	st	Pointer to state variable.
	@param	in	Input.
	@return	Transition output.
*/
static
int
dk3strkv_stm_step(int *st, int in)
{
  int back = O_ERROR;
  if(st) {
    int os;
    int nf = 1;
    int ns = ST_ERROR;
    os = *st;
    switch(os) {
      case ST_DQ_EXPECT: {
        switch(in) {
          case I_BS: {
            ns = ST_DQ_VALUE; back = O_SQ_SVAL; nf = 0;
          } break;
          case I_DQ: {
            ns = ST_START; back = O_SEVAL; nf = 0;
          } break;
        }
      } break;
      case ST_DQ_VALUE: {
        switch(in) {
          case I_BS: {
            ns = ST_DQ_VALUE; back = O_SQ; nf = 0;
          } break;
          case I_DQ: {
            ns = ST_START; back = O_ENDVAL; nf = 0;
          } break;
        }
      } break;
      case ST_EXPECT: {
        switch(in) {
          case I_BS: {
            ns = ST_VALUE; back = O_SQ_SVAL; nf = 0;
          } break;
          case I_DQ: {
            ns = ST_DQ_EXPECT; back = O_NO; nf = 0;
          } break;
          case I_SQ: {
            ns = ST_SQ_EXPECT; back = O_NO; nf = 0;
          } break;
          case I_WH: {
            ns = ST_EXPECT; back = O_NO; nf = 0;
          } break;
        }
      } break;
      case ST_KEY: {
        switch(in) {
          case I_EQ: {
            ns = ST_EXPECT; back = O_ENDKEY; nf = 0;
          } break;
          case I_WH: {
            ns = ST_START; back = O_ENDCOUNT; nf = 0;
          } break;
        }
      } break;
      case ST_SQ_EXPECT: {
        switch(in) {
          case I_BS: {
            ns = ST_SQ_VALUE; back = O_SQ_SVAL; nf = 0;
          } break;
          case I_SQ: {
            ns = ST_START; back = O_SEVAL; nf = 0;
          } break;
        }
      } break;
      case ST_SQ_VALUE: {
        switch(in) {
          case I_BS: {
            ns = ST_SQ_VALUE; back = O_SQ; nf = 0;
          } break;
          case I_SQ: {
            ns = ST_START; back = O_ENDVAL; nf = 0;
          } break;
        }
      } break;
      case ST_START: {
        switch(in) {
          case I_OT: {
            ns = ST_KEY; back = O_SKEY; nf = 0;
          } break;
          case I_WH: {
            ns = ST_START; back = O_NO; nf = 0;
          } break;
        }
      } break;
      case ST_VALUE: {
        switch(in) {
          case I_BS: {
            ns = ST_VALUE; back = O_SQ; nf = 0;
          } break;
          case I_WH: {
            ns = ST_START; back = O_ENDVAL; nf = 0;
          } break;
        }
      } break;
    }
    if(nf) {
      if(os == ST_KEY) {
      ns = ST_KEY; back = O_NO;
      } else {
      if(os == ST_EXPECT) {
      ns = ST_VALUE; back = O_SVAL;
      } else {
      if(os == ST_VALUE) {
      ns = ST_VALUE; back = O_NO;
      } else {
      if(os == ST_SQ_EXPECT) {
      ns = ST_SQ_VALUE; back = O_SVAL;
      } else {
      if(os == ST_SQ_VALUE) {
      ns = ST_SQ_VALUE; back = O_NO;
      } else {
      if(os == ST_DQ_EXPECT) {
      ns = ST_DQ_VALUE; back = O_SVAL;
      } else {
      if(os == ST_DQ_VALUE) {
      ns = ST_DQ_VALUE; back = O_NO;
      } else {
      }
      }
      }
      }
      }
      }
      }
    }
    *st = ns;
  } else {
  }
  return back;
}


#line 100 "dk3strkv.ctr"


#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;
  

#line 117 "dk3strkv.ctr"
  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;
  } 

#line 134 "dk3strkv.ctr"
  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;
  

#line 211 "dk3strkv.ctr"
  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) {	

#line 222 "dk3strkv.ctr"
	  icl = dk3strkv_classify(*ptr);	

#line 223 "dk3strkv.ctr"
	  act = dk3strkv_stm_step(&stm, icl);	

#line 224 "dk3strkv.ctr"
	  switch(act) {
	    case O_ERROR: {		

#line 226 "dk3strkv.ctr"
	      error = 1;
	    } break;
	    case O_SQ: {		

#line 229 "dk3strkv.ctr"
	      dk3str_cpy(ptr, &(ptr[1]));
	      if(*ptr) {
	        charno++;
	      } else {
	        cc = 0;
		error = 1;
		dk3strkv_error_unexpected_eot(app, mycopy);
	      }
	    } break;
	    case O_SKEY: {		

#line 239 "dk3strkv.ctr"
	      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: {		

#line 250 "dk3strkv.ctr"
	      *ptr = dkT('\0');
	    } break;
	    case O_ENDCOUNT: {		

#line 253 "dk3strkv.ctr"
	      *ptr = dkT('\0');
	      i++;
	    } break;
	    case O_ENDVAL: {		

#line 257 "dk3strkv.ctr"
	      *ptr = dkT('\0');
	      i++;
	    } break;
	    case O_SVAL: {		

#line 261 "dk3strkv.ctr"
	      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: {		

#line 272 "dk3strkv.ctr"
	      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: {		

#line 291 "dk3strkv.ctr"
	      *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: {	

#line 312 "dk3strkv.ctr"
	  if(0 == error) {
	    back = 1;
	    *szp = i;
	  }
	} break;
	case ST_KEY: {	

#line 318 "dk3strkv.ctr"
	  if(0 == error) {
	    i++;
	    back = 1;
	    *szp = i;
	  }
	} break;
	case ST_VALUE: {	

#line 325 "dk3strkv.ctr"
	  if(0 == error) {
	    i++;
	    back = 1;
	    *szp = i;
	  }
	} break;
      }
      dk3_release(mycopy);
    }
  } else {
    if((kvp) && (szp) && (!(il))) {
      *szp = 0;
      back = 1;
    }
  } 

#line 340 "dk3strkv.ctr"
  return back;
}