Salome HOME
Moved test configuration from CMake variables to a configuration file
[tools/libbatch.git] / src / Core / Test / SimpleParser.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  *  SimpleParser.cpp
24  *
25  *  Created on: 23 juil. 2009
26  *  Author: Renaud BARATE - EDF R&D
27  */
28
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32
33 #include <Test_config.h>
34
35 #include "SimpleParser.hxx"
36
37 using namespace std;
38
39
40 ParserException::ParserException(string msg) throw()
41   : exception(),
42     _msg(msg)
43 {
44 }
45
46 ParserException::~ParserException() throw()
47 {
48 }
49
50 const char * ParserException::what() const throw()
51 {
52   return _msg.c_str();
53 }
54
55
56 SimpleParser::SimpleParser() throw()
57 {
58 }
59
60 SimpleParser::~SimpleParser() throw()
61 {
62 }
63
64 std::string SimpleParser::trim(const std::string & str) const throw()
65 {
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);
70 }
71
72 void SimpleParser::parse(const string & filename) throw(ParserException)
73 {
74   ifstream fileStream(filename.c_str());
75   if (!fileStream) {
76     throw ParserException(string("Can't open file ") + filename);
77   }
78   string line;
79   int lineNumber = 1;
80   while (getline(fileStream, line)) {
81     string str = line;
82     // Strip comments
83     size_t pos = str.find_first_of('#');
84     if (pos != string::npos) {
85       str = str.substr(0, pos);
86     }
87     // Strip leading and trailing spaces
88     str = trim(str);
89     if (!str.empty()) {
90       // Find '=' symbol and split the line
91       pos = str.find_first_of('=');
92       if (pos == string::npos) {
93         stringstream msg;
94         msg << "Syntax error (missing =) on line " << lineNumber << ": " << line;
95         throw ParserException(msg.str());
96       } else {
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);
102         }
103         _configmap[key] = value;
104       }
105     }
106     ++lineNumber;
107   }
108   fileStream.close();
109 }
110
111 void SimpleParser::parseTestConfigFile() throw(ParserException)
112 {
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.");
116   } else {
117     parse(filename);
118   }
119 }
120
121 const string & SimpleParser::getValue(const string & key) const throw(ParserException)
122 {
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 + ".");
126   }
127   return iter->second;
128 }
129
130 int SimpleParser::getValueAsInt(const string & key) const throw(ParserException)
131 {
132   const string & valueStr = getValue(key);
133   const char * valueCStr = valueStr.c_str();
134   char * end = NULL;
135   int res = strtol(valueCStr, &end, 0);
136   if (*valueCStr == '\0' || *end != '\0') {
137     throw ParserException(string("Invalid value (not integer) for key ") + key + ".");
138   }
139   return res;
140 }
141
142 ostream & operator <<(ostream & os, const SimpleParser & parser) throw()
143 {
144   os << "Configuration map:" << endl;
145   if (parser._configmap.empty()) {
146     os << "Empty map" << endl;
147   } else {
148     map<string,string>::const_iterator iter;
149     for (iter = parser._configmap.begin() ; iter != parser._configmap.end() ; ++iter) {
150       os << iter->first << " = " << iter->second << endl;
151     }
152   }
153   return os;
154 }