50 #include "../App/ApplicationTools.h" 51 #include "../Text/TextTools.h" 52 #include "../Io/FileTools.h" 58 std::vector<std::string> AttributesTools::getVector(
int argc,
char* argv[])
60 size_t n =
static_cast<size_t>(argc);
61 vector<string> result(n);
62 for (
size_t i = 1; i < n; ++i)
64 result[i] = string(argv[i]);
72 std::map<std::string, std::string> AttributesTools::getAttributesMap(
73 const std::vector<std::string>& argv,
74 const std::string& delimiter)
76 map<string, string> am;
77 getAttributesMap(argv, am, delimiter);
83 void AttributesTools::getAttributesMap(
84 const std::vector<std::string>& argv,
85 std::map<std::string, std::string>& am,
86 const std::string& delimiter)
88 vector<string> argv2(argv.size());
90 for (
size_t i = 0; i < argv.size(); i++)
93 string arg = removeComments(argv[i],
string(
"#"),
string(
"\n"));
94 arg = removeComments(arg,
string(
"//"),
string(
"\n"));
95 arg = removeComments(arg,
string(
"/*"),
string(
"*/"));
96 arg = TextTools::removeWhiteSpaces(arg);
100 for (
size_t i = 0; i < argv.size(); i++)
102 string arg = argv2[i];
105 while (arg[arg.size() - 1] ==
'\\')
109 arg = arg.substr(0, arg.length() - 1) + argv2[i];
112 string::size_type limit = arg.find(delimiter, 0);
113 if (limit == string::npos)
116 (*ApplicationTools::warning <<
"WARNING!!! Parameter '" << arg <<
"' has been ignored.").endLine();
120 string name = string(arg.begin(), arg.begin() +
static_cast<ptrdiff_t
>(limit));
121 string value = string(arg.begin() +
static_cast<ptrdiff_t
>(limit + delimiter.size()), arg.end());
135 void AttributesTools::getAttributesMapFromFile(
136 const std::string& file,
137 std::map<std::string, std::string>& params,
138 const std::string& delimiter)
140 cout <<
"Parsing file " << file <<
" for options." << endl;
141 ifstream input(file.c_str(), ios::in);
142 vector<string> lines = FileTools::putStreamIntoVectorOfStrings(input);
143 getAttributesMap(lines, params, delimiter);
148 std::map<std::string, std::string> AttributesTools::getAttributesMapFromFile(
149 const std::string& file,
150 const std::string& delimiter)
152 map<string, string> params;
153 getAttributesMapFromFile(file, params, delimiter);
159 void AttributesTools::actualizeAttributesMap(
160 std::map<std::string, std::string>& attMap,
161 const std::map<std::string, std::string>& atts)
163 for (map<string, string>::const_iterator i = atts.begin(); i != atts.end(); i++)
165 if ((i->first !=
"param") && (i->first !=
"params"))
166 attMap[i->first] = i->second;
172 void AttributesTools::resolveVariables(
173 std::map<std::string, std::string>& am,
180 for (map<string, string>::iterator it = am.begin(); it != am.end(); it++)
182 string value = it->second;
183 string::size_type index1 = value.find(TextTools::toString(varCode) + TextTools::toString(varBeg));
184 while (index1 != string::npos)
186 string::size_type index2 = value.find(TextTools::toString(varEnd), index1);
187 if (index2 != string::npos)
189 string varName = value.substr(index1 + 2, index2 - index1 - 2);
190 map<string, string>::iterator varIt = am.find(varName);
191 string varValue =
"";
192 if (varIt == am.end())
194 if (ApplicationTools::error)
195 (*ApplicationTools::error <<
"Variable '" << varName <<
"' is undefined and was ignored.").endLine();
199 varValue = varIt->second;
202 string newValue = value.substr(0, index1) + varValue + value.substr(index2 + 1);
203 it->second = newValue;
206 throw Exception(
"Syntax error, variable name is not closed.");
208 index1 = value.find(TextTools::toString(varCode) + TextTools::toString(varBeg));
215 std::string AttributesTools::removeComments(
216 const std::string& s,
217 const std::string& begin,
218 const std::string& end)
221 string::size_type last = 0;
224 string::size_type first = r.find(begin, last);
225 if (first == string::npos)
228 last = r.find(end, first);
229 if (last == string::npos)
231 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.end());
235 r.erase(r.begin() +
static_cast<ptrdiff_t
>(first), r.begin() +
static_cast<ptrdiff_t
>(last));
238 while (last != string::npos);
244 std::map<std::string, std::string> AttributesTools::parseOptions(
int args,
char** argv)
throw (
Exception)
247 map<string, string> cmdParams = AttributesTools::getAttributesMap(
248 AttributesTools::getVector(args, argv),
"=");
251 map<string, string> params;
252 std::map<std::string, std::string>::iterator it;
254 if (cmdParams.find(
"param") != cmdParams.end())
256 string file = cmdParams[
"param"];
257 if (!FileTools::fileExists(file))
259 throw Exception(
"AttributesTools::parseOptions(). Parameter file not found.");
263 params = getAttributesMapFromFile(file,
"=");
265 actualizeAttributesMap(params, cmdParams);
273 resolveVariables(params);
275 std::vector<string> mapfile;
276 std::vector<string>::iterator imapfile;
281 it = params.find(
"param");
282 if (it != params.end())
285 if (std::find(mapfile.begin(), mapfile.end(), file) == mapfile.end())
288 mapfile.push_back(file);
289 getAttributesMapFromFile(file, params,
"=");
290 resolveVariables(params);
294 throw Exception(
"parsing error : Already used file " + file);
296 it = params.find(
"params");
297 if (it != params.end())
300 if (find(mapfile.begin(), mapfile.end(), file) == mapfile.end())
303 mapfile.push_back(file);
304 getAttributesMapFromFile(file, params,
"=");
305 resolveVariables(params);
309 throw Exception(
"parsing error : Already used file " + file);
This class allows to perform a correspondence analysis.