opencv/modules/core/src/cmdparser.cpp

168 lines
4.6 KiB
C++
Raw Normal View History

#include "precomp.hpp"
using namespace std;
using namespace cv;
vector<string> split_string(const string& str, const string& delimiters)
{
vector<string> res;
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
res.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return res;
}
void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv)
{
std::vector<std::string> buffer_vector;
std::string buffer_string;
std::string buffer2_string;
int find_symbol;
for (int i = 0; i < _argc; i++)
{
buffer_string = _argv[i];
find_symbol = buffer_string.find('=');
if (find_symbol == -1)
buffer_vector.push_back(buffer_string);
2011-05-28 14:55:41 +08:00
else if (find_symbol == 0 || find_symbol == (buffer_string.length() - 1))
{
buffer_string.erase(find_symbol, (find_symbol + 1));
if(!buffer_string.empty())
buffer_vector.push_back(buffer_string);
}
else
{
buffer2_string = buffer_string;
buffer_string.erase(find_symbol);
buffer_vector.push_back(buffer_string);
buffer2_string.erase(0, find_symbol + 1);
buffer_vector.push_back(buffer2_string);
2011-05-28 14:55:41 +08:00
}
}
argc = buffer_vector.size();
argv = new char* [argc];
for (int i=0; i < argc; i++)
{
argv[i] = new char[buffer_vector[i].length() + 1];
memcpy(argv[i], buffer_vector[i].c_str(), buffer_vector[i].length() + 1);
}
}
2011-05-28 14:55:41 +08:00
CommandLineParser::CommandLineParser(int _argc, const char* _argv[])
{
std::string cur_name;
bool was_pushed=false;
int argc;
char** argv;
PreprocessArgs(_argc, _argv, argc, argv);
2011-05-28 14:55:41 +08:00
for(int i=1; i < argc; i++) {
2011-05-28 14:55:41 +08:00
if(!argv[i])
break;
2011-05-28 14:55:41 +08:00
if( (argv[i][0]== '-') && (strlen(argv[i]) > 1)
&&
( (argv[i][1] < '0') || (argv[i][1] > '9')) )
{
2011-05-28 14:55:41 +08:00
if (!cur_name.empty() && !was_pushed) {
data[cur_name].push_back("");
}
cur_name=argv[i];
while (cur_name.find('-') == 0)
{
cur_name.erase(0,1);
}
was_pushed=false;
if (data.find(cur_name) != data.end()) {
string str_exception="dublicating parameters for name='" + cur_name + "'";
CV_Error(CV_StsParseError, str_exception);
}
continue;
}
2011-05-28 14:55:41 +08:00
data[cur_name].push_back(argv[i]);
was_pushed=true;
}
if (!cur_name.empty() && !was_pushed)
2011-05-28 14:55:41 +08:00
data[cur_name].push_back("");
}
bool CommandLineParser::has(const std::string& keys) const
{
vector<string> names=split_string(keys, " |");
2011-05-28 14:55:41 +08:00
for(size_t j=0; j < names.size(); j++) {
if (data.find(names[j])!=data.end())
return true;
}
return false;
}
2011-05-28 14:55:41 +08:00
std::string CommandLineParser::getString(const std::string& keys) const
{
vector<string> names=split_string(keys, " |");
int found_index=-1;
2011-05-28 14:55:41 +08:00
for(size_t j=0; j < names.size(); j++) {
const string& cur_name=names[j];
bool is_cur_found=has(cur_name);
2011-05-28 14:55:41 +08:00
if (is_cur_found && (found_index >= 0)) {
string str_exception="dublicating parameters for "
"name='" + names[found_index] + "' and name='"+cur_name+"'";
CV_Error(CV_StsParseError, str_exception);
}
if (is_cur_found)
found_index=j;
}
if (found_index<0)
2011-05-28 14:55:41 +08:00
return string();
return data.find(names[found_index])->second[0];
}
2011-05-28 14:55:41 +08:00
template<>
2011-05-28 14:55:41 +08:00
std::string CommandLineParser::analizeValue<std::string>(const std::string& str)
{
return str;
}
template<>
2011-05-28 14:55:41 +08:00
int CommandLineParser::analizeValue<int>(const std::string& str)
{
return fromStringNumber<int>(str);
}
template<>
2011-05-28 14:55:41 +08:00
unsigned int CommandLineParser::analizeValue<unsigned int>(const std::string& str)
{
return fromStringNumber<unsigned int>(str);
}
template<>
2011-05-28 14:55:41 +08:00
float CommandLineParser::analizeValue<float>(const std::string& str)
{
2011-05-28 14:55:41 +08:00
return fromStringNumber<float>(str);
}
template<>
2011-05-28 14:55:41 +08:00
double CommandLineParser::analizeValue<double>(const std::string& str)
{
2011-05-28 14:55:41 +08:00
return fromStringNumber<double>(str);
}