-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.cpp
703 lines (564 loc) · 18.6 KB
/
ini.cpp
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//---------------------------------------------------------------------------
#include "ini.h"
#include "macros.h"
//==============================================================================
bool S2::operator == (S2 & RHS)
{
return (_s1==RHS._s1)&&(_s2==RHS._s2);
}
//==============================================================================
bool S4::operator == (S4 & RHS)
{
return (_s1==RHS._s1)&&(_s2==RHS._s2)&&(_s3==RHS._s3)&&(_s4==RHS._s4);
}
//==============================================================================
INI::INI(char * file,
void (* Ccb)(void *),
void (* Ecb)(void *,int),
void (* Rcb)(void *),
void (* Scb)(void *)):UIF()
{
SetCCB(Ccb);
SetECB(Ecb);
SetRCB(Rcb);
SetSCB(Scb);
}
//------------------------------------------------------------------------------
/*
INI::INI(void (* Ccb)(void *),
void (* Ecb)(void *),
void (* Rcb)(void *),
void (* Scb)(void *)):UIF(Ccb,Ecb,Rcb,Scb)
{
}
*/
//------------------------------------------------------------------------------
INI::INI(int argc, char ** argv):UIF(argc,argv)
{
}
//------------------------------------------------------------------------------
INI::INI():UIF()
{
}
//------------------------------------------------------------------------------
INI::~INI()
{
}
//------------------------------------------------------------------------------
bool INI::Decode(void * p,Lex::Sytype & op, vector<string> & ss)
// This routine assumes it has been called by the command callback handler.
// As such, the first argument will be the section node, the last addition to
// which will be the command that triggered this call.
{
UIF::Node * pN = static_cast<UIF::Node *>(p);
if (pN==0) return false; // Sanity checks...
if (pN->Type()!=No_sect) return false;
if (pN->leaf.back()->Type()!=No_cmnd) return false;
string sinput = pN->leaf.back()->str; // At last, the string!
// So parse the command....
return Decode(sinput,op,ss);
}
//------------------------------------------------------------------------------
bool INI::Decode(string sinput,Lex::Sytype & op, vector<string> & ss)
// This routine simply decodes whatever is in the first argument.
{
bool problem = false;
Lex Lx;
Lex::tokdat Td;
Lx.SetFile(sinput); // Point the lexer at it
Lx.SetNFlag(false); // Tune lexer to *not* recognise numbers
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4} toktyp;
struct duple {int ns,ac;} next;
duple table[3][t4+1] =
// Incident symbol // Next
// 0 1 2 3 4 // state
{{{1, 0},{X, X},{X, X},{X, X},{X, X}}, // 0
{{X, X},{2, 1},{2, 2},{X, X},{X, X}}, // 1
{{X, X},{X, X},{2, 3},{R, 0},{X, X}}}; // 2
op = Lex::S_00;
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
//switch (state) { // Pre-transition (entry) actions
// case 0 : cOp = Lex::S_00; break; // Null current argument sign
//}
// No exceptional cases (EOF in table)
if (Lx.IsError(Td)) problem = true;
toktyp = t4; // 'Else' token
if (IsStr(Td.t)) toktyp = t2; // Reduction functions
if (IsOp(Td.t)) toktyp = t1;
if (Td.t==Lex::Sy_LT) toktyp = t0; // Override the reduction functions
if (Td.t==Lex::Sy_GT) toktyp = t3;
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // Got an operator
op = Td.t; break;
case 2 : // Command starts with a string
Lx.push_back(); break;
case 3 : // Store the string
ss.push_back(Td.s); break;
default : break;
}
switch (state=next.ns) {
case X : return false;
case R : return true;
}
if (problem==true) return false;
}
//return false; // Never here
}
//------------------------------------------------------------------------------
vector<UIF::Node *> INI::FindCommands(UIF::Node * pS)
// Routine to return a vector of command nodes found in the section pS
{
vector<Node *> vN;
WALKVECTOR(Node *,pS->leaf,i) {
if ((*i)->Type()==UIF::No_cmnd) vN.push_back(*i);
}
return vN;
}
//------------------------------------------------------------------------------
UIF::Node * INI::FindSection(S2 & sect)
// Return the node address of the named section
{
if (UIF_root==0) return 0;
WALKVECTOR(Node *,UIF_root->leaf,i)
if (INI::Match(GetSectName(*i),sect)) return *i;
return 0;
}
//------------------------------------------------------------------------------
vector<S2> INI::GetSections()
// Return a vector of section name(arg) pairs
{
vector<S2> ans;
if (UIF_root==0) return ans;
WALKVECTOR(Node *,UIF_root->leaf,i) {
Node * n1 = FindNode(*i,No_name);
if (n1==0) { ans.push_back(S2()); continue; }
Node * n2 = FindNode(n1,No_name);
if (n2==0) { ans.push_back(S2()); continue; }
Node * n3 = FindNode(n2,No_name);
if (n3==0) ans.push_back(S2(n2->str));
else ans.push_back(S2(n2->str,n3->str));
}
return ans;
}
//------------------------------------------------------------------------------
vector<S2> INI::GetValues(S2 & sect,S4 & vari)
{
vector<S2> ans;
Node * n = FindSection(sect);
if (n==0) return ans;
WALKVECTOR(Node *,n->leaf,i) {
if ((*i)->typ!=No_recd) continue;
S2 l = GetRecdLabl(*i);
S2 v = GetRecdVari(*i);
S4 x = S4(l,v);
if (vari==x) return GetRecdValu(*i);
}
return ans;
}
//------------------------------------------------------------------------------
vector<S2> INI::GetValues(string & sect,string & vari)
// Compiler bug? Why do I have to generate the temporaries explicitly?
{
S2 tmp2 = S2(sect);
S4 tmp4 = S4(vari,3);
return GetValues(tmp2,tmp4);
}
//------------------------------------------------------------------------------
vector<S2> INI::GetValues(char * & sect,char * & vari)
{
string s1 = string(sect);
string s2 = string(vari);
return GetValues(s1,s2);
}
//------------------------------------------------------------------------------
vector<S4> INI::GetVariables(S2 & sect)
// Return a vector of the variable signatures in the named section
{
vector<S4> ans;
S4 tmp4;
Node * n = FindSection(sect);
if (n==0) return ans;
WALKVECTOR(Node *,n->leaf,i) {
if ((*i)->typ!=No_recd) continue; // Not a record node
S2 tmp2;
S4 tmp4;
tmp4.Clear();
tmp2 = GetRecdLabl(*i);
tmp4.s1()=tmp2.s1();
tmp4.s2()=tmp2.s2();
tmp2 = GetRecdVari(*i);
tmp4.s3()=tmp2.s1();
tmp4.s4()=tmp2.s2();
ans.push_back(tmp4);
}
return ans;
}
//------------------------------------------------------------------------------
void INI::Mask(vector<S2> & target,S2 m,uchar op)
{
startagain:
WALKVECTOR(S2,target,i) {
bool f = INI::Match(*i,m);
//printf("op=%x, INI::INI_NOMATCH=%x\n",op,INI::INI_NOMATCH);
if (op==INI::INI_NOMATCH) f = !f;
if (f) {
target.erase(i);
goto startagain;
}
}
}
//------------------------------------------------------------------------------
void INI::Mask(vector<S4> & target,S4 m,uchar op)
// Remove elements from 'target' that match 'm'.
// "" is a significant string; "*" is a wildcard
{
startagain:
WALKVECTOR(S4,target,i) {
bool f = INI::Match(*i,m);
if (op==INI::INI_NOMATCH) f = !f;
if (f) {
target.erase(i);
goto startagain;
}
}
}
//------------------------------------------------------------------------------
bool INI::Match(S2 t,S2 m)
{
string w = string("*");
if ((t.s1()!=m.s1())&&(m.s1()!=w)) return false;
if ((t.s2()!=m.s2())&&(m.s2()!=w)) return false;
return true;
}
//------------------------------------------------------------------------------
bool INI::Match(S4 t,S4 m)
{
string w = string("*");
if ((t.s1()!=m.s1())&&(m.s1()!=w)) return false;
if ((t.s2()!=m.s2())&&(m.s2()!=w)) return false;
if ((t.s3()!=m.s3())&&(m.s3()!=w)) return false;
if ((t.s4()!=m.s4())&&(m.s4()!=w)) return false;
return true;
}
//------------------------------------------------------------------------------
bool INI::SectionExists(S2 sect)
// Establish whether or not the named section exists
{
if (UIF_root==0) return false;
if (FindSection(sect)==0) return false;
return true;
}
//------------------------------------------------------------------------------
bool INI::SectionExists(string s1,string s2)
{
S2 tmp = S2(s1,s2);
return SectionExists(tmp);
}
//------------------------------------------------------------------------------
bool INI::SectionExists(string sect)
// Establish whether or not the named section exists
{
return SectionExists(S2(sect));
}
//------------------------------------------------------------------------------
bool INI::VariableExists(S2 & sect,S4 & vari)
{
Node * n = FindSection(sect);
if (n==0) return false;
WALKVECTOR(Node *,n->leaf,i) {
if ((*i)->typ!=No_recd) continue;
S2 l = GetRecdLabl(*i);
S2 v = GetRecdVari(*i);
S4 x = S4(l,v);
if (vari==x) return true;
}
return false;
}
//------------------------------------------------------------------------------
bool INI::VariableExists(string & sect,string & vari)
{
S2 tmp2 = S2(sect);
S4 tmp4 = S4(vari,3);
return VariableExists(tmp2,tmp4);
}
//------------------------------------------------------------------------------
bool INI::VariableExists(char * & sect,char * & vari)
{
string s1 = string(sect);
string s2 = string(vari);
return VariableExists(s1,s2);
}
//------------------------------------------------------------------------------
S2 INI::GetSectName(Node * p)
// Get the section name of the section node p
{
S2 ans;
p = FindNode(p,No_name); // Find the name root
if (p==0) return ans; // The node with no name...
p = FindNode(p,No_name); // First name
if (p==0) return ans;
ans.s1() = p->str;
p = FindNode(p,No_name); // First argument
if (p==0) return ans;
ans.s2() = p->str;
return ans;
}
//------------------------------------------------------------------------------
S2 INI::GetRecdLabl(Node * p)
// Get the label of the record node p
{
S2 ans;
p = FindNode(p,No_body);
if (p==0) return ans;
p = FindNode(p,No_labl);
if (p==0) return ans;
p=FindNode(p,No_name);
if (p==0) return ans;
ans.s1() = p->str;
p = FindNode(p,No_name);
if (p==0) return ans;
ans.s2() = p->str;
return ans;
}
//------------------------------------------------------------------------------
vector<S2> INI::GetRecdValu(Node * p)
{
vector<S2> ans;
S2 tmp;
p = FindNode(p,No_body);
if (p==0) return ans;
p = FindNode(p,No_valu);
if (p==0) return ans;
vector<Node *> nv = FindNodes(p,No_name);
if (nv.size()==0) return ans;
WALKVECTOR(Node *,nv,i) {
tmp.s1() = (*i)->str;
p = FindNode(*i,No_name);
if (p!=0) tmp.s2() = p->str;
ans.push_back(tmp);
}
return ans;
}
//------------------------------------------------------------------------------
S2 INI::GetRecdVari(Node * p)
// Get the label of the record node p
{
S2 ans;
p = FindNode(p,No_body);
if (p==0) return ans;
p = FindNode(p,No_vari);
if (p==0) return ans;
p=FindNode(p,No_name);
if (p==0) return ans;
ans.s1() = p->str;
p = FindNode(p,No_name);
if (p==0) return ans;
ans.s2() = p->str;
return ans;
}
//------------------------------------------------------------------------------
/*
void INI::Save(string savefile)
{
}
//------------------------------------------------------------------------------
bool INI::section_exists(const string& title)
// Does the named section exist?
{
return (FindSection(title)==0) ? false : true;
}
//------------------------------------------------------------------------------
string INI::filename()
// Return the source file name
{
return fname;
}
//------------------------------------------------------------------------------
vector<string> INI::section_names(void)
// Return a vector containig all the section names
{
vector<string> ans;
WALKVECTOR(Node *,UIF_root->leaf,i)
WALKVECTOR(Node *,(*i)->leaf,j)
ans.push_back((*j)->leaf[0]->str);
return ans;
}
//------------------------------------------------------------------------------
bool INI::variable_exists(const string& section,const string variable)
// Does the variable "variable" exists in the section "section"?
{
// Find the section
UIF::Node * pSect = FindSection(section);
if (pSect==0) return false; // If that's not there...
UIF::Node * pRecd = FindRecordVari(pSect,variable);
if (pRecd==0) return false; // Nope
return true;
}
//------------------------------------------------------------------------------
vector<string> INI::variable_names(const string& section)
// Hand out the variable names in a given section
{
vector<string> ans;
UIF::Node * pSect = FindSection(section);
if (pSect==0) return ans;
WALKVECTOR(Node *,pSect->leaf,i) { // Walk the records
printf("i in variable_names is %x\n",(*i));
UIF::Node * pBody = FindNode(*i,No_body); // Find the body
if (pBody!=0) {
UIF::Node * pVari = FindNode(pBody,No_vari); // Find the variable
if (pVari!=0) WALKVECTOR(Node *,pVari->leaf,j) ans.push_back((*j)->str);
}
}
return ans;
}
//------------------------------------------------------------------------------
vector<string> INI::variable_values(const string& section,const string variable)
// Hand out the variable values for a given (section,variable)
{
vector<string> ans;
UIF::Node * pSect = FindSection(section);
if (pSect==0) return ans;
UIF::Node * pRecd = FindRecordVari(pSect,variable);
if (pRecd==0) return ans;
UIF::Node * pBody = FindNode(pRecd,No_body);
if (pBody==0) return ans;
UIF::Node * pValu = FindNode(pBody,No_valu);
if (pValu==0) return ans;
WALKVECTOR(Node *,pValu->leaf,i) ans.push_back((*i)->str);
return ans;
}
//------------------------------------------------------------------------------
UIF::Node * INI::FindSection(string name)
// Find the Node address from the name
{
if (UIF_root==0) return 0; // Empty object ?
WALKVECTOR(Node *,UIF_root->leaf,i) // Walk the sections
WALKVECTOR(Node *,(*i)->leaf,j) // Walk the section names
if ((*j)->leaf[0]->str==name) return *i;
return 0; // Not there, then ?
}
//------------------------------------------------------------------------------
UIF::Node * INI::FindRecordVari(UIF::Node * pSect,string name)
// Given the section node address, find the named variable record
{
WALKVECTOR(Node *,pSect->leaf,i) { // Walk the records
printf("i in FindRecordVari is %x\n",(*i));
UIF::Node * pBody = FindNode(*i,No_body); // Find the body
if (pBody!=0) {
UIF::Node * pVari = FindNode(pBody,No_vari); // Find the variable
if (pVari!=0) {
UIF::Node * pName = FindName(pVari,0); // Find the first name
if (pName->str==name) return *i;
}
}
}
return 0;
}
//------------------------------------------------------------------------------
UIF::Node * INI::FindName(UIF::Node * pVari,int j)
// Given the record variable header node address, find the "j"th name
// (if it exists)
{
WALKVECTOR(Node *,pVari->leaf,i) {
printf("i,j in FindName is %x,%d\n",(*i),j); // Walk the leaves
if (j-- ==0) return (*i);
}
return 0;
}
//------------------------------------------------------------------------------
bool INI::section_exists(Tcpssr)
// Does the section name(arg) exist?
{
return true;
}
//------------------------------------------------------------------------------
INI::Tvpss INI::section_names_2(void)
// Return the section name(arg) list
{
}
//------------------------------------------------------------------------------
INI::Tvs INI::section_names(string name)
// Sections can have the same name but different arguments; given a name, this
// returns the list of section name arguments
{
Tvs ans;
vector<Node *> vn = find_section_names(name);
WALKVECTOR(Node *,vn,i) {
Node * a = FindNode(n,No_name);
if (a!=0) ans.push_back(s->str);
}
return ans;
}
//------------------------------------------------------------------------------
bool INI::variable_exists(Tcpssr s,Tcpssr v)
// Does the variable name(arg) exist in the section name(arg)?
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::variable_names(Tcpssr s)
// Return a vector of variable name(arg) from the section name(arg)
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::variable_values(Tcpssr s,Tcpssr v)
// Return a vector of value name(arg) for the variable name(arg) in the section
// name(arg)
{
}
//------------------------------------------------------------------------------
bool INI::label_exists(Tcpssr s,Tcpssr l)
// Does the label name(arg) exist in the section name(arg)?
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::label_names(Tcpssr s)
// Return a list of name(arg) labels in the section name(arg)
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::label_values(Tcpssr s,Tcpssr l)
// Return a list of name(arg) values for the label name(arg) in the section
// name(arg)
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::label2variable(Tcpssr s,Tcpssr l)
// Return the vector of name(arg) variables corresponding to the name(arg)
// label in the name(arg) section
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::variable2label(Tcpssr s,Tcpssr v)
// Return the vector of name(arg) labels corresponding to the name(arg)
// variable in the name(arg) section
{
}
//------------------------------------------------------------------------------
INI::Tvpss INI::values(Tcpssr s,Tcpssr l,Tcpssr v)
// Return the vector of name(arg) values of the name(arg) variable with
// the name(arg) label in the name(arg) section
{
}
//------------------------------------------------------------------------------
vector<UIF::Node *> INI::find_section_names(string name)
// Sections can have the same name but different arguments; given a name, this
// returns the list of section nodes with that name
{
vector<Node *>ans;
vector<Node *> sects = FindNodes(UIF_root,No_sect);
WALKVECTOR(Node *,sects,i) {
Node * n = FindNode(*i,No_name);
if (n==0) continue;
n = FindNode(n,No_name);
if (n==0) continue;
if (n->str==name) ans.push_back(*i);
}
return ans;
}
//------------------------------------------------------------------------------
*/