1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 * Created on: 23 juil. 2009
26 * Author: Renaud BARATE - EDF R&D
34 #include <Test_config.h>
36 #include "SimpleParser.hxx"
41 ParserException::ParserException(string msg) throw()
47 ParserException::~ParserException() throw()
51 const char * ParserException::what() const throw()
57 SimpleParser::SimpleParser() throw()
61 SimpleParser::~SimpleParser() throw()
65 std::string SimpleParser::trim(const std::string & str) const throw()
67 size_t beg = str.find_first_not_of(" \t");
68 if (beg == string::npos) beg = 0;
69 size_t end = str.find_last_not_of(" \t");
70 return str.substr(beg, end-beg+1);
73 void SimpleParser::parse(const string & filename) throw(ParserException)
75 ifstream fileStream(filename.c_str());
77 throw ParserException(string("Can't open file ") + filename);
81 while (getline(fileStream, line)) {
84 size_t pos = str.find_first_of('#');
85 if (pos != string::npos) {
86 str = str.substr(0, pos);
88 // Strip leading and trailing spaces
91 // Find '=' symbol and split the line
92 pos = str.find_first_of('=');
93 if (pos == string::npos) {
95 msg << "Syntax error (missing =) on line " << lineNumber << ": " << line;
96 throw ParserException(msg.str());
98 string key = trim(str.substr(0, pos));
99 string value = trim(str.substr(pos+1));
100 // Eventually remove '"' symbols at the beginning and at the end of the string
101 if (value.size()>1 && value[0] == '"' && value[value.size()-1] == '"') {
102 value = value.substr(1, value.size()-2);
104 _configmap[key] = value;
112 void SimpleParser::parseTestConfigFile() throw(ParserException)
114 char * filename = getenv(TEST_CONFIG_FILE_ENV_VAR);
115 if (filename == NULL) {
116 throw ParserException(string("Environment variable ") + TEST_CONFIG_FILE_ENV_VAR + " is not declared.");
122 const string & SimpleParser::getValue(const string & key) const throw(ParserException)
124 map<string,string>::const_iterator iter = _configmap.find(key);
125 if (iter == _configmap.end()) {
126 throw ParserException(string("No value found for key ") + key + ".");
131 const string & SimpleParser::getTestValue(const string & bmType, const string & protocolStr,
132 const string & key) const throw(ParserException)
134 string fullkey = string("TEST_") + bmType + "_" + protocolStr + "_" + key;
136 return getValue(fullkey);
137 } catch (const ParserException &) {}
138 fullkey = string("TEST_") + bmType + "_" + key;
140 return getValue(fullkey);
141 } catch (const ParserException &) {}
142 fullkey = string("TEST_") + key;
143 return getValue(fullkey);
146 int SimpleParser::getValueAsInt(const string & key) const throw(ParserException)
148 const string & valueStr = getValue(key);
149 const char * valueCStr = valueStr.c_str();
151 int res = strtol(valueCStr, &end, 0);
152 if (*valueCStr == '\0' || *end != '\0') {
153 throw ParserException(string("Invalid value (not integer) for key ") + key + ".");
158 int SimpleParser::getTestValueAsInt(const string & bmType, const string & protocolStr,
159 const string & key) const throw(ParserException)
161 string fullkey = string("TEST_") + bmType + "_" + protocolStr + "_" + key;
163 return getValueAsInt(fullkey);
164 } catch (const ParserException &) {}
165 fullkey = string("TEST_") + bmType + "_" + key;
167 return getValueAsInt(fullkey);
168 } catch (const ParserException &) {}
169 fullkey = string("TEST_") + key;
170 return getValueAsInt(fullkey);
173 ostream & operator <<(ostream & os, const SimpleParser & parser) throw()
175 if (parser._configmap.empty()) {
176 os << "Empty map" << endl;
178 map<string,string>::const_iterator iter;
179 for (iter = parser._configmap.begin() ; iter != parser._configmap.end() ; ++iter) {
180 os << iter->first << " = " << iter->second << endl;