16 #include "../App/ApplicationTools.h" 17 #include "../Text/TextTools.h" 18 #include "../Io/FileTools.h" 22 std::vector<std::string> AttributesTools::vParam_;
26 std::vector<std::string> AttributesTools::getVector(
int argc,
char* argv[])
28 size_t n =
static_cast<size_t>(argc);
29 vector<string> result(n);
30 for (
size_t i = 1; i < n; ++i)
32 result[i] = string(argv[i]);
40 std::map<std::string, std::string> AttributesTools::getAttributesMap(
41 const std::vector<std::string>& argv,
42 const std::string& delimiter)
44 map<string, string> am;
45 getAttributesMap(argv, am, delimiter);
51 void AttributesTools::getAttributesMap(
52 const std::vector<std::string>& argv,
53 std::map<std::string, std::string>& am,
54 const std::string& delimiter)
56 vector<string> argv2(argv.size());
58 for (
size_t i = 0; i < argv.size(); ++i)
61 string arg = removeComments(argv[i],
"#",
"\n");
62 arg = removeComments(arg,
"//",
"\n");
63 arg = removeComments(arg,
"/*",
"*/");
68 for (
size_t i = 0; i < argv.size(); i++)
70 string arg = argv2[i];
73 while (arg[arg.size() - 1] ==
'\\')
77 arg = arg.substr(0, arg.length() - 1) + argv2[i];
80 string::size_type limit = arg.find(delimiter, 0);
81 if (limit == string::npos)
84 (*ApplicationTools::warning <<
"WARNING!!! Parameter '" << arg <<
"' has been ignored.").endLine();
88 string name = string(arg.begin(), arg.begin() +
static_cast<ptrdiff_t
>(limit));
89 string value = string(arg.begin() +
static_cast<ptrdiff_t
>(limit + delimiter.size()), arg.end());
97 void AttributesTools::getAttributesMapFromFile(
98 const std::string& file,
99 std::map<std::string, std::string>& params,
100 const std::string& delimiter)
102 cout <<
"Parsing file " << file <<
" for options." << endl;
103 ifstream input(file.c_str(), ios::in);
104 vector<string> lines = FileTools::putStreamIntoVectorOfStrings(input);
105 getAttributesMap(lines, params, delimiter);
110 std::map<std::string, std::string> AttributesTools::getAttributesMapFromFile(
111 const std::string& file,
112 const std::string& delimiter)
114 map<string, string> params;
115 getAttributesMapFromFile(file, params, delimiter);
121 void AttributesTools::actualizeAttributesMap(
122 std::map<std::string, std::string>& attMap,
123 const std::map<std::string, std::string>& atts,
126 for (
const auto& i : atts)
128 if (replace || attMap.find(i.first) == attMap.end())
129 attMap[i.first] = i.second;
135 void AttributesTools::resolveVariables(
136 std::map<std::string, std::string>& am,
142 for (map<string, string>::iterator it = am.begin(); it != am.end(); it++)
144 string value = it->second;
146 while (index1 != string::npos)
149 if (index2 != string::npos)
151 string varName = value.substr(index1 + 2, index2 - index1 - 2);
152 map<string, string>::iterator varIt = am.find(varName);
153 string varValue =
"";
154 if (varIt == am.end())
156 if (ApplicationTools::error)
157 (*ApplicationTools::error <<
"Variable '" << varName <<
"' is undefined and was ignored.").endLine();
158 index1 = string::npos;
162 if (varIt->second == value)
164 if (ApplicationTools::error)
165 (*ApplicationTools::error <<
"Variable '" << varName <<
"' definition is cyclic and was ignored.").endLine();
166 index1 = string::npos;
170 varValue = varIt->second;
171 string newValue = value.substr(0, index1) + varValue + value.substr(index2 + 1);
172 it->second = newValue;
180 throw Exception(
"Syntax error, variable name is not closed.");
187 std::string AttributesTools::removeComments(
188 const std::string& s,
189 const std::string& begin,
190 const std::string& end)
193 string::size_type last = 0;
196 auto first = r.find(begin, last);
197 if (first == string::npos)
200 last = r.find(end, first);
201 if (last == string::npos)
203 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.end());
207 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.begin() +
static_cast<ptrdiff_t
>(last));
210 while (last != string::npos);
216 std::map<std::string, std::string> AttributesTools::parseOptions(
int args,
char** argv)
219 map<string, string> cmdParams = AttributesTools::getAttributesMap(
220 AttributesTools::getVector(args, argv),
"=");
222 resolveVariables(cmdParams);
227 map<string, string> params;
229 if (cmdParams.find(
"param") != cmdParams.end())
232 cmdParams.erase(
"param");
233 vector<string> vfile;
239 while (i != vfile.size())
241 const auto& file = vfile[i];
242 auto ii =
static_cast<vector<string>::difference_type
>(i);
243 if (std::find(vfile.begin(), vfile.begin() + ii, file) != vfile.begin() + ii)
245 cout << file <<
" already seen. Skipping." << endl;
250 if (!FileTools::fileExists(file))
251 throw Exception(
"AttributesTools::parseOptions(). Parameter file not found.: " + file);
253 params = getAttributesMapFromFile(file,
"=");
254 actualizeAttributesMap(cmdParams, params,
false);
256 resolveVariables(cmdParams);
259 if (cmdParams.find(
"param") != cmdParams.end())
262 cmdParams.erase(
"param");
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
bool hasMoreToken() const
Tell if some tokens are still available.
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
std::string toString(T t)
General template method to convert to a string.