-
Notifications
You must be signed in to change notification settings - Fork 27
/
conffile.cpp
839 lines (565 loc) · 19.5 KB
/
conffile.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*
Copyright (C) 2009 Andrew Caudwell ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "conffile.h"
#include "logger.h"
#include "regex.h"
//section of config file
Regex ConfFile_section("^\\s*\\[([^\\]]+)\\]\\s*$");
Regex ConfFile_int_value("^\\s*-?(\\d+)\\s*$");
Regex ConfFile_float_value("^\\s*-?(\\d*\\.?\\d+)\\s*$");
// parse key value pair, seperated by an equals sign, removing white space on key and front of the value
Regex ConfFile_key_value("^\\s*([^=\\s]+)\\s*=\\s*([^\\s].*)?$");
// vec2, vec3, or vec4 with liberal allowance for whitespace
Regex ConfFile_vec2_value("^\\s*vec2\\(\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*\\)\\s*$");
Regex ConfFile_vec3_value("^\\s*vec3\\(\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*\\)\\s*$");
Regex ConfFile_vec4_value("^\\s*vec4\\(\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*,\\s*(-?[0-9.]+)\\s*\\)\\s*$");
//ConfEntry
ConfEntry::ConfEntry(const std::string& name) {
this->name = name;
}
ConfEntry::ConfEntry(const std::string& name, const std::string& value, int lineno) {
this->name = name;
this->value = value;
this->lineno = lineno;
}
ConfEntry::ConfEntry(const std::string& name, bool value) {
this->name = name;
setBool(value);
}
ConfEntry::ConfEntry(const std::string& name, int value) {
this->name = name;
setInt(value);
}
ConfEntry::ConfEntry(const std::string& name, float value) {
this->name = name;
setFloat(value);
}
ConfEntry::ConfEntry(const std::string& name, vec2 value) {
this->name = name;
setVec2(value);
}
ConfEntry::ConfEntry(const std::string& name, vec3 value) {
this->name = name;
setVec3(value);
}
ConfEntry::ConfEntry(const std::string& name, vec4 value) {
this->name = name;
setVec4(value);
}
void ConfEntry::setName(const std::string& name) {
this->name = name;
}
void ConfEntry::setString(const std::string& value) {
this->value = value;
}
void ConfEntry::setFloat(float value) {
char floattostr[256];
snprintf(floattostr, 256, "%.5f", value);
this->value = std::string(floattostr);
}
void ConfEntry::setInt(int value) {
char inttostr[256];
snprintf(inttostr, 256, "%d", value);
this->value = std::string(inttostr);
}
void ConfEntry::setBool(bool value) {
this->value = std::string(value ? "yes" : "no");
}
void ConfEntry::setVec2(vec2 value) {
char vectostr[256];
snprintf(vectostr, 256, "vec2(%.5f, %.5f)", value.x, value.y);
this->value = std::string(vectostr);
}
void ConfEntry::setVec3(vec3 value) {
char vectostr[256];
snprintf(vectostr, 256, "vec3(%.5f, %.5f, %.5f)", value.x, value.y, value.z);
this->value = std::string(vectostr);
}
void ConfEntry::setVec4(vec4 value) {
char vectostr[256];
snprintf(vectostr, 256, "vec4(%.5f, %.5f, %.5f, %.5f)", value.x, value.y, value.z, value.w);
this->value = std::string(vectostr);
}
std::string ConfEntry::getName() {
return name;
}
std::string ConfEntry::getString() {
return value;
}
int ConfEntry::getLineNumber() {
return lineno;
}
int ConfEntry::getInt() {
return atoi(value.c_str());
}
bool ConfEntry::isFloat() {
if(ConfFile_float_value.match(value)) return true;
return false;
}
bool ConfEntry::isInt() {
if(ConfFile_float_value.match(value)) return true;
return false;
}
float ConfEntry::getFloat() {
return atof(value.c_str());
}
bool ConfEntry::hasValue() {
return getString().size() > 0;
}
bool ConfEntry::isBool() {
if( value == "1" || value == "true" || value == "True" || value == "TRUE" || value == "yes" || value == "Yes" || value == "YES"
|| value == "0" || value == "false" || value == "False" || value == "FALSE" || value == "no" || value == "No" || value == "NO")
return true;
return false;
}
bool ConfEntry::getBool() {
if(value == "1" || value == "true" || value == "True" || value == "TRUE" || value == "yes" || value == "Yes" || value == "YES")
return true;
return false;
}
bool ConfEntry::isVec2() {
if(ConfFile_vec2_value.match(value)) return true;
return false;
}
vec2 ConfEntry::getVec2() {
std::vector<std::string> matches;
if(ConfFile_vec2_value.match(value, &matches)) {
return vec2(atof(matches[0].c_str()), atof(matches[1].c_str()));
}
return vec2(0.0, 0.0);
}
bool ConfEntry::isVec3() {
if(ConfFile_vec3_value.match(value)) return true;
return false;
}
vec3 ConfEntry::getVec3() {
std::vector<std::string> matches;
if(ConfFile_vec3_value.match(value, &matches)) {
return vec3(atof(matches[0].c_str()), atof(matches[1].c_str()), atof(matches[2].c_str()));
}
return vec3(0.0, 0.0, 0.0);
}
bool ConfEntry::isVec4() {
if(ConfFile_vec4_value.match(value)) return true;
return false;
}
vec4 ConfEntry::getVec4() {
std::vector<std::string> matches;
if(ConfFile_vec4_value.match(value, &matches)) {
return vec4(atof(matches[0].c_str()), atof(matches[1].c_str()), atof(matches[2].c_str()), atof(matches[3].c_str()) );
}
return vec4(0.0, 0.0, 0.0, 0.0);
}
//ConfSection
ConfSection::ConfSection() {
lineno = 0;
conf = 0;
}
ConfSection::ConfSection(const std::string& name, int lineno) {
this->name = name;
this->lineno = lineno;
this->conf = 0;
}
ConfSection::~ConfSection() {
clear();
}
std::string ConfSection::getName() {
return name;
}
ConfFile* ConfSection::getConfFile() {
return conf;
}
void ConfSection::setConfFile(ConfFile* conf) {
this->conf = conf;
}
int ConfSection::getLineNumber() {
return lineno;
}
ConfEntryList* ConfSection::getEntries(const std::string& key) {
std::map<std::string, ConfEntryList*>::iterator entry_finder = entrymap.find(key);
if(entry_finder == entrymap.end()) return 0;
return entry_finder->second;
}
ConfEntry* ConfSection::getEntry(const std::string& key) {
ConfEntryList* entryList = getEntries(key);
if(entryList==0 || entryList->size()==0) return 0;
return entryList->front();
}
void ConfSection::addEntry(ConfEntry* entry) {
ConfEntryList* entrylist = entrymap[entry->getName()];
if(entrylist==0) {
entrymap[entry->getName()] = entrylist = new ConfEntryList;
}
entrylist->push_back(entry);
}
void ConfSection::addEntry(const std::string& name, const std::string& value, int lineno) {
ConfEntry* entry = new ConfEntry(name, value, lineno);
addEntry(entry);
}
//replace first entry with that name
void ConfSection::setEntry(ConfEntry* entry) {
ConfEntryList* entrylist = entrymap[entry->getName()];
if(entrylist==0) {
entrymap[entry->getName()] = entrylist = new ConfEntryList;
}
//remove any entries with this name
while(entrylist->size()>0) {
ConfEntry* front = entrylist->front();
entrylist->pop_front();
delete front;
}
//add new entry
entrylist->push_front(entry);
}
void ConfSection::setEntry(const std::string& name, const std::string& value, int lineno) {
ConfEntry* entry = new ConfEntry(name, value, lineno);
setEntry(entry);
}
void ConfSection::clear() {
//delete entries
for(std::map<std::string, ConfEntryList*>::iterator it = entrymap.begin();
it!= entrymap.end(); it++) {
ConfEntryList* entrylist = it->second;
for(std::list<ConfEntry*>::iterator eit = entrylist->begin();
eit != entrylist->end(); eit++) {
ConfEntry* e = *eit;
delete e;
}
delete entrylist;
}
entrymap.clear();
}
bool ConfSection::hasValue(const std::string& key) {
std::string value = getString(key);
if(value.size()>0) return true;
return false;
}
std::string ConfSection::getString(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry==0) return std::string("");
return entry->getString();
}
int ConfSection::getInt(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry) return entry->getInt();
return 0;
}
float ConfSection::getFloat(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry) return entry->getFloat();
return 0.0f;
}
bool ConfSection::getBool(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry) return entry->getBool();
return false;
}
vec3 ConfSection::getVec3(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry) return entry->getVec3();
return vec3(0.0, 0.0, 0.0);
}
vec4 ConfSection::getVec4(const std::string& key) {
ConfEntry* entry = getEntry(key);
if(entry) return entry->getVec4();
return vec4(0.0, 0.0, 0.0, 0.0);
}
void ConfSection::print(std::ostream& out) {
out << "[" << getName() << "]" << std::endl;
for(std::map<std::string, ConfEntryList*>::iterator it = entrymap.begin();
it!= entrymap.end(); it++) {
ConfEntryList* entrylist = it->second;
for(std::list<ConfEntry*>::iterator eit = entrylist->begin();
eit != entrylist->end(); eit++) {
ConfEntry* e = *eit;
out << e->getName() << "=" << e->getString() << std::endl;
}
}
out << std::endl;
}
//ConfFile
ConfFile::ConfFile() {
}
ConfFile::~ConfFile() {
clear();
}
void ConfFile::clear() {
//delete sections
for(std::map<std::string, ConfSectionList*>::iterator it = sectionmap.begin();
it!= sectionmap.end(); it++) {
ConfSectionList* sectionlist = it->second;
for(std::list<ConfSection*>::iterator sit = sectionlist->begin();
sit != sectionlist->end(); sit++) {
ConfSection* s = *sit;
delete s;
}
delete sectionlist;
}
sectionmap.clear();
}
void ConfFile::setFilename(const std::string& filename) {
this->conffile = filename;
}
std::string ConfFile::getFilename() {
return conffile;
}
void ConfFile::save(const std::string& conffile) {
this->conffile = conffile;
save();
}
void ConfFile::save() {
if(conffile.size()==0) {
throw ConfFileException("filename not set", conffile.c_str(), 0);
}
//save conf file
std::ofstream out;
out.open(conffile.c_str());
if(!out.is_open()) {
std::string write_error = std::string("failed to write config to ") + conffile;
throw ConfFileException(write_error, conffile.c_str(), 0);
}
for(std::map<std::string, ConfSectionList*>::iterator it = sectionmap.begin();
it!= sectionmap.end(); it++) {
ConfSectionList* sectionlist = it->second;
for(ConfSectionList::iterator sit = sectionlist->begin();
sit != sectionlist->end(); sit++) {
ConfSection* s = *sit;
s->print(out);
}
}
out.close();
}
void ConfFile::load(const std::string& conffile) {
this->conffile = conffile;
load();
}
static const std::string conffile_whitespaces(" \t\f\v\n\r");
void ConfFile::trim(std::string& value) {
if(value.size() == 0) return;
size_t string_end = value.find_last_not_of(conffile_whitespaces);
if(string_end == std::string::npos) value.assign("");
else if(string_end != value.size()-1) value.assign(value.substr(0,string_end+1));
}
void ConfFile::load() {
debugLog("ConfFile::load(%s)", conffile.c_str());
clear();
char buff[1024];
int lineno = 0;
ConfSection* sec = 0;
std::ifstream in(conffile.c_str());
if(!in.is_open()) {
snprintf(buff, 1024, "failed to open config file %s", conffile.c_str());
std::string conf_error = std::string(buff);
throw ConfFileException(conf_error, conffile, 0);
}
std::string line;
while(std::getline(in, line)) {
lineno++;
std::vector<std::string> matches;
// blank line or commented out lines
if(line.size() == 0 || (line.size() > 0 && line[0] == '#')) {
continue;
// sections
} else if(ConfFile_section.match(line, &matches)) {
if(sec != 0) addSection(sec);
sec = new ConfSection(matches[0], lineno);
// key value pairs
} else if(ConfFile_key_value.match(line, &matches)) {
std::string key = matches[0];
std::string value = (matches.size()>1) ? matches[1] : "";
//trim whitespace
ConfFile::trim(value);
if(sec==0) sec = new ConfSection("", lineno);
sec->addEntry(key, value, lineno);
debugLog("%s: [%s] %s => %s", conffile.c_str(), sec->getName().c_str(), key.c_str(), value.c_str());
} else {
snprintf(buff, 1024, "%s, line %d: could not parse line", conffile.c_str(), lineno);
std::string conf_error = std::string(buff);
throw ConfFileException(conf_error, conffile, lineno);
}
}
if(sec != 0) addSection(sec);
in.close();
}
bool ConfFile::hasEntry(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry != 0) return true;
return false;
}
bool ConfFile::hasValue(const std::string& section, const std::string& key) {
std::string value = getString(section, key);
if(value.size()>0) return true;
return false;
}
int ConfFile::countSection(const std::string& section) {
ConfSectionList* sectionlist = getSections(section);
if(sectionlist==0) return 0;
int count = 0;
for(ConfSectionList::iterator sit = sectionlist->begin(); sit != sectionlist->end(); sit++) {
count++;
}
return count;
}
bool ConfFile::hasSection(const std::string& section) {
ConfSection* sec = getSection(section);
if(sec==0) return false;
return true;
}
ConfSection* ConfFile::addSection(const std::string& section) {
ConfSection* sec = new ConfSection(section);
addSection(sec);
return sec;
}
void ConfFile::addSection(ConfSection* section) {
ConfSectionList* sectionlist = getSections(section->getName());
if(sectionlist==0) {
sectionmap[section->getName()] = sectionlist = new ConfSectionList;
}
section->setConfFile(this);
sectionlist->push_back(section);
}
void ConfFile::setSection(ConfSection* section) {
ConfSectionList* sectionlist = getSections(section->getName());
if(sectionlist==0) {
sectionmap[section->getName()] = sectionlist = new ConfSectionList;
}
if(sectionlist->size() != 0) {
ConfSection* front = sectionlist->front();
sectionlist->pop_front();
delete front;
}
sectionlist->push_back(section);
}
//returns the list of all sections with a particular name
ConfSectionList* ConfFile::getSections(const std::string& section) {
std::map<std::string, ConfSectionList*>::iterator section_finder = sectionmap.find(section);
if(section_finder == sectionmap.end()) return 0;
return section_finder->second;
}
//returns the first section with a particular name
ConfSection* ConfFile::getSection(const std::string& section) {
ConfSectionList* sectionlist = getSections(section);
if(sectionlist==0 || sectionlist->size()==0) return 0;
return sectionlist->front();
}
//returns the first section with a particular name
void ConfFile::setEntry(const std::string& section, const std::string& key, const std::string& value) {
ConfSection* sec = getSection(section);
if(sec==0) {
sec = addSection(section);
}
sec->setEntry(key, value);
}
//returns a list of all entries in a section with a particular name
ConfEntryList* ConfFile::getEntries(const std::string& section, const std::string& key) {
ConfSection* sec = getSection(section);
if(sec==0) return 0;
ConfEntryList* entryList = sec->getEntries(key);
return entryList;
}
//get first entry in a section with a particular name
ConfEntry* ConfFile::getEntry(const std::string& section, const std::string& key) {
ConfSection* sec = getSection(section);
if(sec==0) return 0;
ConfEntry* entry = sec->getEntry(key);
return entry;
}
std::string ConfFile::getString(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry==0) return std::string("");
return entry->getString();
}
int ConfFile::getInt(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry) return entry->getInt();
return 0;
}
float ConfFile::getFloat(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry) return entry->getFloat();
return 0.0f;
}
bool ConfFile::getBool(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry) return entry->getBool();
return false;
}
vec3 ConfFile::getVec3(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry) return entry->getVec3();
return vec3(0.0, 0.0, 0.0);
}
vec4 ConfFile::getVec4(const std::string& section, const std::string& key) {
ConfEntry* entry = getEntry(section, key);
if(entry) return entry->getVec4();
return vec4(0.0, 0.0, 0.0, 0.0);
}
void ConfFile::unknownOptionException(ConfEntry* entry) {
std::string reason = "unknown option '";
reason += entry->getName();
reason += std::string("'");
entryException(entry, reason);
}
void ConfFile::missingValueException(ConfEntry* entry) {
std::string reason = std::string("no value specified for '");
reason += entry->getName();
reason += std::string("'");
entryException(entry, reason);
}
void ConfFile::invalidValueException(ConfEntry* entry) {
std::string reason = std::string("invalid '");
reason += entry->getName();
reason += std::string("' value");
entryException(entry, reason);
}
void ConfFile::missingEntryException(ConfSection* section, std::string entryname) {
std::string reason = std::string("section '");
reason += section->getName();
reason += std::string("' missing required entry '");
reason += entryname;
reason += std::string("'");
sectionException(section, reason);
}
void ConfFile::sectionException(ConfSection* section, std::string reason) {
std::string errmsg;
int lineno = 0;
if(conffile.size()) {
errmsg = conffile;
if(section != 0 && section->getLineNumber() != 0) {
lineno = section->getLineNumber();
char linebuff[256];
snprintf(linebuff, 256, ", line %d", lineno);
errmsg += std::string(linebuff);
}
errmsg += std::string(": ");
}
errmsg += reason;
throw ConfFileException(errmsg, conffile, lineno);
}
void ConfFile::entryException(ConfEntry* entry, std::string reason) {
std::string errmsg;
int lineno = 0;
if(conffile.size()) {
errmsg = conffile;
if(entry != 0 && entry->getLineNumber() != 0) {
lineno = entry->getLineNumber();
char linebuff[256];
snprintf(linebuff, 256, ", line %d", lineno);
errmsg += std::string(linebuff);
}
errmsg += std::string(": ");
}
errmsg += reason;
throw ConfFileException(errmsg, conffile, lineno);
}