-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFile.C
142 lines (94 loc) · 2.69 KB
/
ConfigFile.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
/***********************************************************************
ConfigFile.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#include <stdlib.h>
#include <fstream>
#include "File.H"
#include "ConfigFile.H"
using namespace std;
BOOM::ConfigFile::ConfigFile(BOOM::String filename)
{
load(filename);
}
BOOM::ConfigFile::ConfigFile()
{
}
bool BOOM::ConfigFile::isDefined(BOOM::String key)
{
return dict.isDefined(key);
}
BOOM::String BOOM::ConfigFile::lookup(BOOM::String attr)
{
return dict[attr];
}
BOOM::String BOOM::ConfigFile::lookupOrDie(BOOM::String attr)
{
return charLookupOrDie(attr);
}
const char *BOOM::ConfigFile::charLookupOrDie(BOOM::String attr)
{
if(dict.find(attr)==dict.end())
throw attr+" is not defined in the configuration file";
return dict[attr].c_str();
}
double BOOM::ConfigFile::getDoubleOrDie(BOOM::String attr)
{
return atof(charLookupOrDie(attr));
}
float BOOM::ConfigFile::getFloatOrDie(BOOM::String attr)
{
return (float) atof(charLookupOrDie(attr));
}
int BOOM::ConfigFile::getIntOrDie(BOOM::String attr)
{
return atoi(charLookupOrDie(attr));
}
bool BOOM::ConfigFile::getBoolOrDie(BOOM::String attr)
{
BOOM::String b=charLookupOrDie(attr);
b.tolower();
if(b=="true" || b=="t" || b=="yes" || b=="y" || b=="1") return true;
if(b=="false" || b=="f" || b=="no" || b=="n" || b=="0") return false;
throw attr+" must be true or false in config file";
}
long BOOM::ConfigFile::getLongOrDie(BOOM::String attr)
{
return atol(charLookupOrDie(attr));
}
void BOOM::ConfigFile::enter(BOOM::String key,BOOM::String value)
{
dict[key]=value;
}
void BOOM::ConfigFile::load(BOOM::String fname)
{
BOOM::File file(fname);
if(!file.isOpen())
throw BOOM::String("Error: Can't open file ")+fname+
" in BOOM::ConfigFile::load()";
while(!file.eof())
{
BOOM::String nextline=file.getline();
if(file.eof()) break;
const char *p=nextline.c_str();
BOOM::StrTokenizer parser(p,"=\n\t \r");
processLine(parser);
}
}
void BOOM::ConfigFile::processLine(BOOM::StrTokenizer &parser)
{
if(!parser.hasMoreTokens()) return;
BOOM::String key=parser.nextToken();
if('#'==key[0]) return; // comment
if(!parser.hasMoreTokens())
throw BOOM::String("Syntax error in configuration file: "+key);
const char *value=parser.nextToken();
dict[key]=value;
}
BOOM::Map<BOOM::String,BOOM::String> &BOOM::ConfigFile::peek()
{
return dict;
}