mirror of https://github.com/opencv/opencv.git
parent
4ba6793568
commit
aadb1669a7
9 changed files with 460 additions and 437 deletions
@ -1,172 +1,181 @@ |
|||||||
#include "precomp.hpp" |
#include "precomp.hpp" |
||||||
|
|
||||||
using namespace std; |
using namespace std; |
||||||
using namespace cv; |
using namespace cv; |
||||||
|
|
||||||
|
|
||||||
vector<string> split_string(const string& str, const string& delimiters) |
vector<string> split_string(const string& str, const string& delimiters) |
||||||
{ |
{ |
||||||
vector<string> res; |
vector<string> res; |
||||||
string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
||||||
string::size_type pos = str.find_first_of(delimiters, lastPos); |
string::size_type pos = str.find_first_of(delimiters, lastPos); |
||||||
while (string::npos != pos || string::npos != lastPos) |
while (string::npos != pos || string::npos != lastPos) |
||||||
{ |
{ |
||||||
res.push_back(str.substr(lastPos, pos - lastPos)); |
res.push_back(str.substr(lastPos, pos - lastPos)); |
||||||
lastPos = str.find_first_not_of(delimiters, pos); |
lastPos = str.find_first_not_of(delimiters, pos); |
||||||
pos = str.find_first_of(delimiters, lastPos); |
pos = str.find_first_of(delimiters, lastPos); |
||||||
} |
} |
||||||
|
|
||||||
return res; |
return res; |
||||||
} |
} |
||||||
|
|
||||||
void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv) |
void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv) |
||||||
{ |
{ |
||||||
std::vector<std::string> buffer_vector; |
std::vector<std::string> buffer_vector; |
||||||
std::string buffer_string; |
std::string buffer_string; |
||||||
std::string buffer2_string; |
std::string buffer2_string; |
||||||
int find_symbol; |
int find_symbol; |
||||||
|
|
||||||
for (int i = 0; i < _argc; i++) |
for (int i = 0; i < _argc; i++) |
||||||
{ |
{ |
||||||
buffer_string = _argv[i]; |
buffer_string = _argv[i]; |
||||||
find_symbol = buffer_string.find('='); |
find_symbol = buffer_string.find('='); |
||||||
if (find_symbol == -1) |
if (find_symbol == -1) |
||||||
buffer_vector.push_back(buffer_string); |
buffer_vector.push_back(buffer_string); |
||||||
else if (find_symbol == 0 || find_symbol == ((int)buffer_string.length() - 1)) |
else if (find_symbol == 0 || find_symbol == ((int)buffer_string.length() - 1)) |
||||||
{ |
{ |
||||||
buffer_string.erase(find_symbol, (find_symbol + 1)); |
buffer_string.erase(find_symbol, (find_symbol + 1)); |
||||||
buffer_vector.push_back(buffer_string); |
if(!buffer_string.empty()) |
||||||
} |
buffer_vector.push_back(buffer_string); |
||||||
else |
} |
||||||
{ |
else |
||||||
buffer2_string = buffer_string; |
{ |
||||||
buffer_string.erase(find_symbol); |
buffer2_string = buffer_string; |
||||||
buffer_vector.push_back(buffer_string); |
buffer_string.erase(find_symbol); |
||||||
buffer2_string.erase(0, find_symbol + 1); |
buffer_vector.push_back(buffer_string); |
||||||
buffer_vector.push_back(buffer2_string); |
buffer2_string.erase(0, find_symbol + 1); |
||||||
} |
buffer_vector.push_back(buffer2_string); |
||||||
} |
} |
||||||
|
} |
||||||
argc = buffer_vector.size(); |
|
||||||
argv = new char* [argc]; |
argc = buffer_vector.size(); |
||||||
for (int i=0; i < argc; i++) |
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); |
argv[i] = new char[buffer_vector[i].length() + 1]; |
||||||
} |
memcpy(argv[i], buffer_vector[i].c_str(), buffer_vector[i].length() + 1); |
||||||
} |
} |
||||||
|
} |
||||||
CommandLineParser::CommandLineParser(int _argc, const char* _argv[]) |
|
||||||
{ |
CommandLineParser::CommandLineParser(int _argc, const char* _argv[]) |
||||||
std::string cur_name; |
{ |
||||||
bool was_pushed=false; |
std::string cur_name; |
||||||
int argc; |
bool was_pushed=false; |
||||||
char** argv; |
int argc; |
||||||
|
char** argv; |
||||||
PreprocessArgs(_argc, _argv, argc, argv); |
|
||||||
|
PreprocessArgs(_argc, _argv, argc, argv); |
||||||
for(int i=1; i < argc; i++) |
|
||||||
{ |
for(int i=1; i < argc; i++) |
||||||
if(!argv[i]) |
{ |
||||||
break; |
if(!argv[i]) |
||||||
|
break; |
||||||
if( (argv[i][0]== '-') && (strlen(argv[i]) > 1) && |
|
||||||
((argv[i][1] < '0') || (argv[i][1] > '9')) ) |
if( (argv[i][0]== '-') && (strlen(argv[i]) > 1) && |
||||||
{ |
((argv[i][1] < '0') || (argv[i][1] > '9')) ) |
||||||
if (!cur_name.empty() && !was_pushed) |
{ |
||||||
{ |
if (!cur_name.empty() && !was_pushed) |
||||||
data[cur_name].push_back(""); |
{ |
||||||
} |
data[cur_name].push_back(""); |
||||||
cur_name=argv[i]; |
} |
||||||
was_pushed=false; |
|
||||||
|
cur_name=argv[i]; |
||||||
if (data.find(cur_name) != data.end()) |
|
||||||
{ |
while (cur_name.find('-') == 0) |
||||||
string str_exception = "dublicating parameters for name='" + cur_name + "'"; |
{ |
||||||
CV_Error(CV_StsParseError, str_exception); |
cur_name.erase(0,1); |
||||||
} |
} |
||||||
continue; |
|
||||||
} |
was_pushed=false; |
||||||
|
|
||||||
data[cur_name].push_back(argv[i]); |
if (data.find(cur_name) != data.end()) |
||||||
was_pushed=true; |
{ |
||||||
} |
string str_exception = "dublicating parameters for name='" + cur_name + "'"; |
||||||
if (!cur_name.empty() && !was_pushed) |
CV_Error(CV_StsParseError, str_exception); |
||||||
data[cur_name].push_back(""); |
} |
||||||
} |
continue; |
||||||
|
} |
||||||
bool CommandLineParser::has(const std::string& keys) const |
|
||||||
{ |
data[cur_name].push_back(argv[i]); |
||||||
vector<string> names=split_string(keys, " |"); |
was_pushed=true; |
||||||
for(size_t j=0; j < names.size(); j++) |
} |
||||||
{ |
if (!cur_name.empty() && !was_pushed) |
||||||
if (data.find(names[j])!=data.end()) |
data[cur_name].push_back(""); |
||||||
return true; |
} |
||||||
} |
|
||||||
return false; |
bool CommandLineParser::has(const std::string& keys) const |
||||||
} |
{ |
||||||
|
vector<string> names=split_string(keys, " |"); |
||||||
template<> |
for(size_t j=0; j < names.size(); j++) |
||||||
std::vector<std::string> CommandLineParser::getVec<std::string>(const std::string& keys) |
{ |
||||||
{ |
if (data.find(names[j])!=data.end()) |
||||||
vector<string> names=split_string(keys, " |"); |
return true; |
||||||
|
} |
||||||
int found_index=-1; |
return false; |
||||||
for(size_t j=0; j < names.size(); j++) |
} |
||||||
{ |
|
||||||
const string& cur_name=names[j]; |
template<> |
||||||
bool is_cur_found=has(cur_name); |
std::vector<std::string> CommandLineParser::getVec<std::string>(const std::string& keys) |
||||||
|
{ |
||||||
if (is_cur_found && (found_index >= 0)) |
vector<string> names=split_string(keys, " |"); |
||||||
{ |
|
||||||
string str_exception = "dublicating parameters for " |
int found_index=-1; |
||||||
"name='" + names[found_index] + "' and name='"+cur_name+"'"; |
for(size_t j=0; j < names.size(); j++) |
||||||
CV_Error(CV_StsParseError, str_exception); |
{ |
||||||
} |
const string& cur_name=names[j]; |
||||||
|
bool is_cur_found=has(cur_name); |
||||||
if (is_cur_found) |
|
||||||
found_index=j; |
if (is_cur_found && (found_index >= 0)) |
||||||
} |
{ |
||||||
|
string str_exception = "dublicating parameters for " |
||||||
if (found_index<0) |
"name='" + names[found_index] + "' and name='"+cur_name+"'"; |
||||||
return vector<string>(); |
CV_Error(CV_StsParseError, str_exception); |
||||||
|
} |
||||||
return data.find(names[found_index])->second; |
|
||||||
} |
if (is_cur_found) |
||||||
|
found_index=j; |
||||||
template<> |
} |
||||||
std::string CommandLineParser::fromString<std::string>(const std::string& str) |
|
||||||
{ |
if (found_index<0) |
||||||
return str; |
return vector<string>(); |
||||||
} |
|
||||||
|
return data.find(names[found_index])->second; |
||||||
template<> |
} |
||||||
int CommandLineParser::fromString<int>(const std::string& str) |
|
||||||
{ |
template<> |
||||||
return fromStringNumber<int>(str); |
std::string CommandLineParser::fromString<std::string>(const std::string& str) |
||||||
} |
{ |
||||||
|
return str; |
||||||
template<> |
} |
||||||
unsigned int CommandLineParser::fromString<unsigned int>(const std::string& str) |
|
||||||
{ |
template<> |
||||||
return fromStringNumber<unsigned int>(str); |
int CommandLineParser::fromString<int>(const std::string& str) |
||||||
} |
{ |
||||||
|
return fromStringNumber<int>(str); |
||||||
template<> |
} |
||||||
double CommandLineParser::fromString<double>(const std::string& str) |
|
||||||
{ |
template<> |
||||||
return fromStringNumber<double>(str); |
unsigned int CommandLineParser::fromString<unsigned int>(const std::string& str) |
||||||
} |
{ |
||||||
|
return fromStringNumber<unsigned int>(str); |
||||||
template<> |
} |
||||||
cv::Size CommandLineParser::fromStringsVec<cv::Size>(const std::vector<std::string>& vec_str) |
|
||||||
{ |
template<> |
||||||
if (vec_str.size() < 2) |
double CommandLineParser::fromString<double>(const std::string& str) |
||||||
CV_Error(CV_StsParseError, "Cannot convert vector of string to cv::Size : less than two strings"); |
{ |
||||||
|
return fromStringNumber<double>(str); |
||||||
cv::Size res; |
} |
||||||
res.width=fromString<int>(vec_str[0]); |
|
||||||
res.height=fromString<int>(vec_str[1]); |
template<> |
||||||
|
cv::Size CommandLineParser::fromStringsVec<cv::Size>(const std::vector<std::string>& vec_str) |
||||||
return res; |
{ |
||||||
} |
if (vec_str.size() < 2) |
||||||
|
CV_Error(CV_StsParseError, "Cannot convert vector of string to cv::Size : less than two strings"); |
||||||
|
|
||||||
|
cv::Size res; |
||||||
|
res.width=fromString<int>(vec_str[0]); |
||||||
|
res.height=fromString<int>(vec_str[1]); |
||||||
|
|
||||||
|
return res; |
||||||
|
} |
||||||
|
|
Loading…
Reference in new issue