1 #include "Configuration.h"
2 #include "stringutilities.h"
3 #include "Report.h"
4 
5 using std::vector;
6 using std::string;
7 using std::map;
8 
9 using namespace ProtoMol::Report;
10 
11 namespace ProtoMol {
12 
13   //________________________________________________________ Configuration
registerKeyword(const string & keyword,Value value)14   void Configuration::registerKeyword(const string& keyword, Value value){
15     if(end() != myValues.find(keyword) && myAliases.end() == myAliases.find(keyword)){
16       if(!value.equalTypeAndConstraint(myValues[keyword]))
17 	report << hint << "[Configuration::registerKeyword] overwriting already registered keyword \'"
18 	       <<keyword<<"\' with diffent type/constraint."<<endr;
19       myValues[keyword] = value;
20     }
21     else if (myAliases.end() == myAliases.find(keyword)){
22       myValues[keyword] = value;
23     }
24     else {
25       report << hint << "[Configuration::registerKeyword] keyword \'"<<keyword
26 	     <<"\' already used for alias \'"<<myAliases[keyword]<<"\'."<<endr;
27     }
28   }
29 
registerAliases(const string & keyword,const vector<string> & aliases)30   void Configuration::registerAliases(const string& keyword, const vector<string>& aliases){
31     for(unsigned int i=0;i<aliases.size();++i){
32       if(!aliases[i].empty() && !keyword.empty()){
33 	if(myAliases.end() == myAliases.find(aliases[i])){
34 	  myAliases[aliases[i]] = keyword;
35 	}
36 	else {
37 	  if(!equalNocase(myAliases[aliases[i]],keyword))
38 	  report << hint << "[Configuration::registerAliases] alias \'"
39 		 << aliases[i]<<"\' already used for \'"<<myAliases[aliases[i]]<<"\'."<<endr;
40 
41 	}
42       }
43     }
44   }
45 
unregisterKeyword(const std::string & keyword)46   void Configuration::unregisterKeyword(const std::string& keyword){
47     iterator i = myValues.find(keyword);
48     if(i != end())
49       myValues.erase(i);
50     for(AliasMapType::iterator i=myAliases.begin();i != myAliases.end();){
51       if(equalNocase(i->second,keyword))
52 	myAliases.erase(i++);
53       else
54 	++i;
55     }
56 
57     TextMapType::iterator j = myTexts.find(keyword);
58     if(j != myTexts.end())
59       myTexts.erase(j);
60 
61   }
62 
empty(const string & keyword) const63   bool Configuration::empty(const string& keyword) const{
64     if(keyword == "")
65       return myValues.empty();
66 
67     return (find(keyword) == end());
68   }
69 
defined(const string & keyword) const70   bool Configuration::defined(const string& keyword) const{
71     const_iterator i = find(keyword);
72 
73     if(i != end())
74       return i->second.defined();
75 
76     return false;
77   }
78 
valid(const string & keyword) const79   bool Configuration::valid(const string& keyword) const{
80     const_iterator i = find(keyword);
81 
82     if(i != end())
83       return i->second.valid();
84 
85     return false;
86   }
87 
set(const string & keyword,Value v)88   bool Configuration::set(const string& keyword, Value v){
89     iterator i = find(keyword);
90 
91     if(i != end()){
92       i->second.set(v);
93       return i->second.valid();
94     }
95     return false;
96   }
97 
set(const string & keyword,const string & v)98   bool Configuration::set(const string& keyword, const string& v){
99     iterator i = find(keyword);
100 
101     if(i != end()){
102       if(v == ""){
103 	i->second.init();
104       }
105       else {
106 	i->second.set(v);
107       }
108       return i->second.valid();
109     }
110     return false;
111   }
112 
113 
set(const vector<vector<string>> & values)114   bool Configuration::set(const vector<vector<string> >&  values){
115     const unsigned int count = values.size();
116     bool ok = true;
117     for(unsigned int i=0;i<count;++i){
118       if(values[i].empty())
119 	continue;
120       iterator itr = find(values[i][0]);
121       if(itr == end()){
122 	report << recoverable << "Keyword \'"<<values[i][0]<<"\' not recognized."<<endr;
123 	continue;
124       }
125       string val("");
126       for(unsigned int j=1;j<values[i].size();++j){
127 	val += (j==1?"":" ")+values[i][j];
128       }
129       if(val == ""){
130 	ok &= itr->second.init();
131       }
132       else if(itr->second.set(val)){
133 	ok = false;
134       }
135       if(!itr->second.valid())
136 	report << hint << "Value \'"<<val<<"\'  could not be assigned \'"
137 	       << values[i][0]<<"\' "<<itr->second.getDefinitionTypeString()<<"."<<endr;
138     }
139     return false;
140   }
141 
set(const string & keyword,const vector<vector<string>> & values)142   bool Configuration::set(const string& keyword, const vector<vector<string> >&  values){
143     const unsigned int count = values.size();
144     for(unsigned int i=0;i<count;++i){
145       if(values[i].empty())
146 	continue;
147       if(!equalNocase(values[i][0],keyword))
148 	continue;
149       string val("");
150       for(unsigned int j=1;j<values[i].size();++j){
151 	val += (j==1?"":" ")+values[i][j];
152       }
153       return set(keyword,val);
154     }
155     return false;
156   }
157 
operator [](const string & keyword) const158   const Value& Configuration::operator[](const string& keyword) const{
159     const_iterator i = find(keyword);
160 
161     if(i == end()){
162       report << error << "Configuration index \'"<<keyword<<"\' of of range."<<endr;
163     }
164     return i->second;
165   }
166 
operator [](const string & keyword)167   Value& Configuration::operator[](const string& keyword){
168     iterator i = find(keyword);
169 
170     if(i == end()){
171       report << error << "Configuration index \'"<<keyword<<"\' of of range."<<endr;
172     }
173     return i->second;
174   }
175 
176 
get(const string & keyword) const177   Value Configuration::get(const string& keyword) const{
178     const_iterator i = find(keyword);
179 
180     if(i != end()){
181       return i->second;
182     }
183     return Value();
184   }
185 
186 
get(const vector<Parameter> & parameters) const187   vector<Value> Configuration::get(const vector<Parameter>& parameters) const{
188     vector<Value> values(parameters.size());
189 
190     for(unsigned int i=0;i<parameters.size();++i){
191       values[i] = get(parameters[i].keyword);
192     }
193 
194     return values;
195   }
196 
197 
198 
validConfiguration() const199   bool Configuration::validConfiguration() const{
200     string tmp;
201     return validConfiguration(tmp);
202   }
203 
validConfiguration(string & errMsg) const204   bool Configuration::validConfiguration(string& errMsg) const{
205 
206     errMsg = "";
207     for(const_iterator i=begin();i!=end();++i){
208       if(!i->second.valid()){
209 	if(errMsg.empty())
210 	  errMsg = "Undefined keyword(s):";
211 	errMsg += "\n"+getRightFill(i->first,Constant::PRINTMAXWIDTH)+i->second.getDefinitionTypeString();
212       }
213     }
214     return (errMsg.empty());
215   }
216 
print() const217   string Configuration::print() const{
218     string str;
219     str += "Keywords:\n\n";
220     for(const_iterator i= begin();i!=end();++i){
221       str += getRightFill(i->first,Constant::PRINTMAXWIDTH) + i->second.getDefinitionTypeString();
222       if(myTexts.find(i->first) != myTexts.end())
223 	str += " \t # "+myTexts.find(i->first)->second;
224       str +="\n";
225     }
226     str += "\nAliases:\n\n";
227     for(AliasMapType::const_iterator i= myAliases.begin();i!=myAliases.end();++i)
228       str += getRightFill(i->first,Constant::PRINTMAXWIDTH) +i->second+"\n";
229     return str;
230   }
231 
find(const string & keyword)232   Configuration::iterator Configuration::find(const string& keyword){
233     iterator i = myValues.find(keyword);
234 
235     if(i == end()){
236       AliasMapType::iterator j = myAliases.find(keyword);
237       if(j != myAliases.end()){
238 	i = myValues.find(j->second);
239       }
240     }
241     return i;
242   }
243 
find(const string & keyword) const244   Configuration::const_iterator Configuration::find(const string& keyword) const{
245     const_iterator i = myValues.find(keyword);
246 
247     if(i == end()){
248       AliasMapType::const_iterator j = myAliases.find(keyword);
249       if(j != myAliases.end()){
250 	i = myValues.find(j->second);
251       }
252     }
253     return i;
254   }
255 
setText(const std::string & keyword,const string & text)256   bool Configuration::setText(const std::string& keyword, const string& text){
257     if(!empty(keyword)){
258       myTexts[keyword] = text;
259       return true;
260     }
261     return false;
262   }
263 
getText(const std::string & keyword) const264   string Configuration::getText(const std::string& keyword) const {
265     TextMapType::const_iterator i = myTexts.find(keyword);
266     if(i != myTexts.end()){
267       return i->second;
268     }
269     else {
270       return string();
271     }
272   }
273 
getAliases(const string & keyword) const274   vector<string> Configuration::getAliases(const string& keyword) const{
275     vector<string> res;
276     for(AliasMapType::const_iterator i=myAliases.begin();i != myAliases.end();++i){
277       if(equalNocase(i->second,keyword)){
278 	res.push_back(i->first);
279       }
280     }
281     return res;
282   }
283 
284 
285 }
286