1 // Copyright (C) 2007-2008 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.
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
33 #include <Test_config.h>
35 #include "SimpleParser.hxx"
40 ParserException::ParserException(string msg) throw()
46 ParserException::~ParserException() throw()
50 const char * ParserException::what() const throw()
56 SimpleParser::SimpleParser() throw()
60 SimpleParser::~SimpleParser() throw()
64 std::string SimpleParser::trim(const std::string & str) const throw()
66 size_t beg = str.find_first_not_of(" \t");
67 if (beg == string::npos) beg = 0;
68 size_t end = str.find_last_not_of(" \t");
69 return str.substr(beg, end-beg+1);
72 void SimpleParser::parse(const string & filename) throw(ParserException)
74 ifstream fileStream(filename.c_str());
76 throw ParserException(string("Can't open file ") + filename);
80 while (getline(fileStream, line)) {
83 size_t pos = str.find_first_of('#');
84 if (pos != string::npos) {
85 str = str.substr(0, pos);
87 // Strip leading and trailing spaces
90 // Find '=' symbol and split the line
91 pos = str.find_first_of('=');
92 if (pos == string::npos) {
94 msg << "Syntax error (missing =) on line " << lineNumber << ": " << line;
95 throw ParserException(msg.str());
97 string key = trim(str.substr(0, pos));
98 string value = trim(str.substr(pos+1));
99 // Eventually remove '"' symbols at the beginning and at the end of the string
100 if (value.size()>1 && value[0] == '"' && value[value.size()-1] == '"') {
101 value = value.substr(1, value.size()-2);
103 _configmap[key] = value;
111 void SimpleParser::parseTestConfigFile() throw(ParserException)
113 char * filename = getenv(TEST_CONFIG_FILE_ENV_VAR);
114 if (filename == NULL) {
115 throw ParserException(string("Environment variable ") + TEST_CONFIG_FILE_ENV_VAR + " is not declared.");
121 const string & SimpleParser::getValue(const string & key) const throw(ParserException)
123 map<string,string>::const_iterator iter = _configmap.find(key);
124 if (iter == _configmap.end()) {
125 throw ParserException(string("No value found for key ") + key + ".");
130 int SimpleParser::getValueAsInt(const string & key) const throw(ParserException)
132 const string & valueStr = getValue(key);
133 const char * valueCStr = valueStr.c_str();
135 int res = strtol(valueCStr, &end, 0);
136 if (*valueCStr == '\0' || *end != '\0') {
137 throw ParserException(string("Invalid value (not integer) for key ") + key + ".");
142 ostream & operator <<(ostream & os, const SimpleParser & parser) throw()
144 if (parser._configmap.empty()) {
145 os << "Empty map" << endl;
147 map<string,string>::const_iterator iter;
148 for (iter = parser._configmap.begin() ; iter != parser._configmap.end() ; ++iter) {
149 os << iter->first << " = " << iter->second << endl;