From b7a3d36729ee3595dc002d9b25ba604593c3782d Mon Sep 17 00:00:00 2001 From: Andrey Morozov Date: Sat, 1 Oct 2011 10:50:50 +0000 Subject: [PATCH] fixed several bugs in CommandLineParser --- modules/core/src/cmdparser.cpp | 47 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/modules/core/src/cmdparser.cpp b/modules/core/src/cmdparser.cpp index f85917f566..f957419dea 100644 --- a/modules/core/src/cmdparser.cpp +++ b/modules/core/src/cmdparser.cpp @@ -78,6 +78,17 @@ vector split_string(const string& str, const string& delimiters) return res; } +string del_space(string name) +{ + while ((name.find_first_of(' ') == 0) && (name.length() > 0)) + name.erase(0, 1); + + while ((name.find_last_of(' ') == (name.length() - 1)) && (name.length() > 0)) + name.erase(name.end() - 1, name.end()); + + return name; +} + CommandLineParser::CommandLineParser(int argc, const char* const argv[], const char* keys) { std::string keys_buffer; @@ -145,12 +156,15 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const c buffer.erase(0, (buffer.find('=') + 1)); } + values_buffer = del_space(values_buffer); + for(it = data.begin(); it != data.end(); it++) { keys_buffer = it->first; keysVector = split_string(keys_buffer, "|"); - if (keysVector.size() == 1) - keysVector.push_back(""); + + for (int j = 0; j < keysVector.size(); j++) keysVector[j] = del_space(keysVector[j]); + values_buffer = it->second[0]; if (((curName == keysVector[0]) || (curName == keysVector[1])) && hasValueThroughEq) { @@ -159,24 +173,26 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const c break; } - if (!hasValueThroughEq && (values_buffer.find("false") == values_buffer.npos) && - ((curName == keysVector[0]) || (curName == keysVector[1]))) - + if (!hasValueThroughEq && ((curName == keysVector[0]) || (curName == keysVector[1])) + && ( + values_buffer.find("false") != values_buffer.npos || + values_buffer == "" + )) { - it->second[0] = argv[++i]; + it->second[0] = "true"; isFound = true; break; } - if (!hasValueThroughEq && (values_buffer.find("false") != values_buffer.npos) - && ((curName == keysVector[0]) || (curName == keysVector[1]))) - + if (!hasValueThroughEq && (values_buffer.find("false") == values_buffer.npos) && + ((curName == keysVector[0]) || (curName == keysVector[1]))) { - it->second[0] = "true"; + it->second[0] = argv[++i]; isFound = true; break; } + if (withNoKey) { std::string noKeyStr = it->first; @@ -198,17 +214,6 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const c } } -string del_space(string name) -{ - while ((name.find_first_of(' ') == 0) && (name.length() > 0)) - name.erase(0, 1); - - while ((name.find_last_of(' ') == (name.length() - 1)) && (name.length() > 0)) - name.erase(name.end() - 1, name.end()); - - return name; -} - bool CommandLineParser::has(const std::string& keys) { std::map >::iterator it;