54 #include "../App/ApplicationTools.h"
55 #include "../Text/TextTools.h"
56 #include "../Io/FileTools.h"
60 std::vector<std::string> AttributesTools::vParam_;
64 std::vector<std::string> AttributesTools::getVector(
int argc,
char* argv[])
66 size_t n =
static_cast<size_t>(argc);
67 vector<string> result(n);
68 for (
size_t i = 1; i < n; ++i)
70 result[i] = string(argv[i]);
78 std::map<std::string, std::string> AttributesTools::getAttributesMap(
79 const std::vector<std::string>& argv,
80 const std::string& delimiter)
82 map<string, string> am;
83 getAttributesMap(argv, am, delimiter);
89 void AttributesTools::getAttributesMap(
90 const std::vector<std::string>& argv,
91 std::map<std::string, std::string>& am,
92 const std::string& delimiter)
94 vector<string> argv2(argv.size());
96 for (
size_t i = 0; i < argv.size(); i++)
99 string arg = removeComments(argv[i],
string(
"#"),
string(
"\n"));
100 arg = removeComments(arg,
string(
"//"),
string(
"\n"));
101 arg = removeComments(arg,
string(
"/*"),
string(
"*/"));
106 for (
size_t i = 0; i < argv.size(); i++)
108 string arg = argv2[i];
111 while (arg[arg.size() - 1] ==
'\\')
115 arg = arg.substr(0, arg.length() - 1) + argv2[i];
118 string::size_type limit = arg.find(delimiter, 0);
119 if (limit == string::npos)
122 (*ApplicationTools::warning <<
"WARNING!!! Parameter '" << arg <<
"' has been ignored.").endLine();
126 string name = string(arg.begin(), arg.begin() +
static_cast<ptrdiff_t
>(limit));
127 string value = string(arg.begin() +
static_cast<ptrdiff_t
>(limit + delimiter.size()), arg.end());
145 void AttributesTools::getAttributesMapFromFile(
146 const std::string& file,
147 std::map<std::string, std::string>& params,
148 const std::string& delimiter)
150 cout <<
"Parsing file " << file <<
" for options." << endl;
151 ifstream input(file.c_str(), ios::in);
152 vector<string> lines = FileTools::putStreamIntoVectorOfStrings(input);
153 getAttributesMap(lines, params, delimiter);
158 std::map<std::string, std::string> AttributesTools::getAttributesMapFromFile(
159 const std::string& file,
160 const std::string& delimiter)
162 map<string, string> params;
163 getAttributesMapFromFile(file, params, delimiter);
169 void AttributesTools::actualizeAttributesMap(
170 std::map<std::string, std::string>& attMap,
171 const std::map<std::string, std::string>& atts)
173 for (map<string, string>::const_iterator i = atts.begin(); i != atts.end(); i++)
175 attMap[i->first] = i->second;
181 void AttributesTools::resolveVariables(
182 std::map<std::string, std::string>& am,
188 for (map<string, string>::iterator it = am.begin(); it != am.end(); it++)
190 string value = it->second;
192 while (index1 != string::npos)
195 if (index2 != string::npos)
197 string varName = value.substr(index1 + 2, index2 - index1 - 2);
198 map<string, string>::iterator varIt = am.find(varName);
199 string varValue =
"";
200 if (varIt == am.end())
202 if (ApplicationTools::error)
203 (*ApplicationTools::error <<
"Variable '" << varName <<
"' is undefined and was ignored.").endLine();
207 varValue = varIt->second;
210 string newValue = value.substr(0, index1) + varValue + value.substr(index2 + 1);
211 it->second = newValue;
214 throw Exception(
"Syntax error, variable name is not closed.");
223 std::string AttributesTools::removeComments(
224 const std::string& s,
225 const std::string& begin,
226 const std::string& end)
229 string::size_type last = 0;
232 string::size_type first = r.find(begin, last);
233 if (first == string::npos)
236 last = r.find(end, first);
237 if (last == string::npos)
239 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.end());
243 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.begin() +
static_cast<ptrdiff_t
>(last));
246 while (last != string::npos);
252 std::map<std::string, std::string> AttributesTools::parseOptions(
int args,
char** argv)
255 map<string, string> cmdParams = AttributesTools::getAttributesMap(
256 AttributesTools::getVector(args, argv),
"=");
260 resolveVariables(cmdParams);
261 map<string, string> params;
263 while (cmdParams.find(
"param") != cmdParams.end() || cmdParams.find(
"params") != cmdParams.end())
265 string file = (cmdParams.find(
"param") != cmdParams.end()) ? cmdParams[
"param"] : cmdParams[
"params"];
267 if (std::find(vParam_.begin(), vParam_.end(), file) != vParam_.end())
268 throw Exception(
"Param name " + file +
" already seen.");
270 if (!FileTools::fileExists(file))
271 throw Exception(
"AttributesTools::parseOptions(). Parameter file not found.: " + file);
273 params = getAttributesMapFromFile(file,
"=");
275 if (cmdParams.find(
"param") != cmdParams.end())
276 cmdParams.erase(
"param");
278 cmdParams.erase(
"params");
280 actualizeAttributesMap(params, cmdParams);
283 resolveVariables(cmdParams);
285 vParam_.push_back(file);
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
std::string toString(T t)
General template method to convert to a string.