diff --git a/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx b/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx index 33f56d24f15a6517ed0f33043321b1880a67d563..56d7a855027da55049f005577796039cfce295d1 100644 --- a/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx +++ b/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx @@ -18,6 +18,7 @@ #include "otbWrapperCommandLineLauncher.h" #include "otbTinyXML.h" +#include <vector> const std::string GetChildNodeTextOf(TiXmlElement *parentElement, std::string key); std::string PrepareExpressionFromXML(std::string filename); @@ -153,6 +154,8 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } + std::vector<std::string> vexp; + std::string exp; if (strcmp(argv[1], "-inxml") == 0) { @@ -163,7 +166,7 @@ int main(int argc, char* argv[]) // Construct the string expression for (int i = 1; i < argc; i++) { - if (i != argc - 1) + /*if (i != argc - 1) { exp.append(argv[i]); exp.append(" "); @@ -171,16 +174,19 @@ int main(int argc, char* argv[]) else { exp.append(argv[i]); - } + }*/ + std::string strarg (argv[i]); + std::cout << strarg << std::endl; + vexp.push_back(strarg); } - } // std::cerr << exp << ":\n"; typedef otb::Wrapper::CommandLineLauncher LauncherType; LauncherType::Pointer launcher = LauncherType::New(); - if (launcher->Load(exp) == true) + //if (launcher->Load(exp) == true) + if (launcher->Load(vexp) == true) { if (launcher->ExecuteAndWriteOutput() == false) { diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx index c6374f145487157830ace5756b226cdc6a9be488..da844d416e6145347454165f9feb2cf4362fa208 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx @@ -58,7 +58,7 @@ namespace Wrapper { CommandLineLauncher::CommandLineLauncher() : - m_Expression(""), m_WatcherList(), m_ReportProgress(true), m_MaxKeySize(0) + /*m_Expression(""),*/ m_WatcherList(), m_ReportProgress(true), m_MaxKeySize(0),m_VExpression() { m_Application = NULL; m_Parser = CommandLineParser::New(); @@ -70,8 +70,8 @@ CommandLineLauncher::CommandLineLauncher() : m_AddProcessCommand->SetCallbackFunction(this, &CommandLineLauncher::LinkWatchers); } -CommandLineLauncher::CommandLineLauncher(const char * exp) : - m_Expression(exp) +CommandLineLauncher::CommandLineLauncher(const char * exp) /*: + m_Expression(exp)*/ { m_Application = NULL; m_Parser = CommandLineParser::New(); @@ -94,15 +94,23 @@ void CommandLineLauncher::DeleteWatcherList() bool CommandLineLauncher::Load(const std::string & exp) { - m_Expression = exp; + /*m_Expression = exp; + return this->Load();*/ + return false; +} + +bool CommandLineLauncher::Load(std::vector<std::string> vexp) +{ + m_VExpression = vexp; return this->Load(); } + bool CommandLineLauncher::Load() { // Add a space to clarify output logs std::cerr << std::endl; - if (m_Expression == "") + if (m_VExpression.empty()) { itkExceptionMacro("No expression specified..."); } @@ -121,9 +129,9 @@ bool CommandLineLauncher::Load() if (this->LoadPath() == false) { - if (m_Parser->GetPathsAsString(m_Expression).size() != 0) + if (m_Parser->GetPathsAsString(m_VExpression).size() != 0) { - std::cerr << "ERROR: At least one specified path within \"" << m_Parser->GetPathsAsString(m_Expression) + std::cerr << "ERROR: At least one specified path within \"" << m_Parser->GetPathsAsString(m_VExpression) << "\" is invalid or doesn't exist..." << std::endl; return false; } @@ -193,7 +201,7 @@ bool CommandLineLauncher::BeforeExecute() // Check if there's keys in the expression if the application takes // at least 1 mandatory parameter const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); - std::vector<std::string> keyList = m_Parser->GetKeyList( m_Expression ); + std::vector<std::string> keyList = m_Parser->GetKeyList( m_VExpression ); if( appKeyList.size()!=0 && keyList.size()==0 ) { @@ -203,21 +211,21 @@ bool CommandLineLauncher::BeforeExecute() } // if help is asked... - if (m_Parser->IsAttributExists("-help", m_Expression) == true) + if (m_Parser->IsAttributExists("-help", m_VExpression) == true) { this->DisplayHelp(); return false; } //display OTB version - if (m_Parser->IsAttributExists("-version", m_Expression) == true) + if (m_Parser->IsAttributExists("-version", m_VExpression) == true) { std::cerr << "This is the "<<m_Application->GetName() << " application, version " << OTB_VERSION_STRING <<std::endl; return false; } // if we want to load test environnement - if (m_Parser->IsAttributExists("-testenv", m_Expression) == true) + if (m_Parser->IsAttributExists("-testenv", m_VExpression) == true) { this->LoadTestEnv(); } @@ -256,10 +264,10 @@ bool CommandLineLauncher::BeforeExecute() } // Check for the progress report - if (m_Parser->IsAttributExists("-progress", m_Expression) == true) + if (m_Parser->IsAttributExists("-progress", m_VExpression) == true) { std::vector<std::string> val; - val = m_Parser->GetAttribut("-progress", m_Expression); + val = m_Parser->GetAttribut("-progress", m_VExpression); if (val.size() != 1) { std::cerr << "ERROR: Invalid progress argument, must be unique value..." << std::endl; @@ -292,7 +300,8 @@ bool CommandLineLauncher::LoadPath() { std::vector<std::string> pathList; // If users has set path... - if (m_Parser->GetPaths(pathList, m_Expression) == CommandLineParser::OK) + //if (m_Parser->GetPaths(pathList, m_Expression) == CommandLineParser::OK) + if (m_Parser->GetPaths(pathList, m_VExpression) == CommandLineParser::OK) { for (unsigned i = 0; i < pathList.size(); i++) { @@ -311,7 +320,8 @@ void CommandLineLauncher::LoadApplication() { // Look for the module name std::string moduleName; - if (m_Parser->GetModuleName(moduleName, m_Expression) != CommandLineParser::OK) + //if (m_Parser->GetModuleName(moduleName, m_Expression) != CommandLineParser::OK) + if (m_Parser->GetModuleName(moduleName, m_VExpression) != CommandLineParser::OK) { std::cerr << "ERROR: LoadApplication, no module found..." << std::endl; return; @@ -365,11 +375,11 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() */ const char *inXMLKey = "inxml"; const char *attrib = "-inxml"; - const bool paramInXMLExists(m_Parser->IsAttributExists(attrib, m_Expression)); + const bool paramInXMLExists(m_Parser->IsAttributExists(attrib, m_VExpression)); if(paramInXMLExists) { std::vector<std::string> inXMLValues; - inXMLValues = m_Parser->GetAttribut(attrib, m_Expression); + inXMLValues = m_Parser->GetAttribut(attrib, m_VExpression); m_Application->SetParameterString(inXMLKey, inXMLValues[0]); m_Application->UpdateParameters(); } @@ -384,13 +394,13 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() Parameter::Pointer param = m_Application->GetParameterByKey(paramKey); ParameterType type = m_Application->GetParameterType(paramKey); - const bool paramExists(m_Parser->IsAttributExists(std::string("-").append(paramKey), m_Expression)); + const bool paramExists(m_Parser->IsAttributExists(std::string("-").append(paramKey), m_VExpression)); // if param is a Group, dont do anything, ParamGroup dont have values if (type != ParameterType_Group) { // Get the attribute relative to this key as vector - values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_Expression); + values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_VExpression); // If the param does not exists in the cli, dont try to set a // value on it, an exception will be thrown later in this function @@ -430,7 +440,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() if (type == ParameterType_String) { dynamic_cast<StringParameter *> (param.GetPointer())->SetValue( - m_Parser->GetAttributAsString(std::string("-").append(paramKey), m_Expression) ); + m_Parser->GetAttributAsString(std::string("-").append(paramKey), m_VExpression) ); } else if (type == ParameterType_OutputImage) @@ -586,7 +596,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() } else { - values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_Expression); + values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_VExpression); if (values.size() == 0 && !m_Application->HasValue(paramKey)) { std::cerr << "ERROR: Missing mandatory parameter: " << paramKey << std::endl; @@ -599,7 +609,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() { if( paramExists ) { - values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_Expression); + values = m_Parser->GetAttribut(std::string("-").append(paramKey), m_VExpression); if (values.size() == 0) { std::cerr << "ERROR: Missing non-mandatory parameter: " << paramKey << std::endl; @@ -708,7 +718,7 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer & // - The param is not root and belonging to a Mandatory Group // wich is activated bool isMissing = false; - if (!m_Parser->IsAttributExists(std::string("-").append(paramKey), m_Expression)) + if (!m_Parser->IsAttributExists(std::string("-").append(paramKey), m_VExpression)) { if (!m_Application->HasValue(paramKey)) { @@ -847,7 +857,8 @@ bool CommandLineLauncher::CheckUnicity() { bool res = true; // Extract expression keys - std::vector<std::string> keyList = m_Parser->GetKeyList(m_Expression); + //std::vector<std::string> keyList = m_Parser->GetKeyList(m_Expression); + std::vector<std::string> keyList = m_Parser->GetKeyList(m_VExpression); // Check Unicity for (unsigned int i = 0; i < keyList.size(); i++) @@ -872,21 +883,22 @@ bool CommandLineLauncher::CheckUnicity() bool CommandLineLauncher::CheckParametersPrefix() { - bool res = true; - // Check if the chain " --" appears in the expression, could be a common mistake - if (m_Expression.find(" --") != std::string::npos ) + // Check if the chain " --" appears in the args, could be a common mistake + for (std::vector<std::string>::iterator it = m_VExpression.begin() ; it != m_VExpression.end(); ++it) { - res = false; + if (it->find("--") != std::string::npos ) + { + return false; + } } - - return res; + return true; } bool CommandLineLauncher::CheckKeyValidity(std::string& refKey) { bool res = true; // Extract expression keys - std::vector<std::string> expKeyList = m_Parser->GetKeyList(m_Expression); + std::vector<std::string> expKeyList = m_Parser->GetKeyList(m_VExpression); // Extract application keys std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); @@ -895,13 +907,18 @@ bool CommandLineLauncher::CheckKeyValidity(std::string& refKey) appKeyList.push_back("testenv"); appKeyList.push_back("version"); + std::cout<< "toto" << std::endl; + // Check if each key in the expression exists in the application for (unsigned int i = 0; i < expKeyList.size(); i++) { - refKey = expKeyList[i]; + //remove first character + std::cout<< "expKey= " << expKeyList[i] << std::endl; + refKey = expKeyList[i].substr(1,expKeyList[i].length()-1); bool keyExist = false; for (unsigned int j = 0; j < appKeyList.size(); j++) { + std::cout<< "appKey= " << appKeyList[j] << std::endl; if (refKey == appKeyList[j]) { keyExist = true; @@ -935,9 +952,9 @@ void CommandLineLauncher::DisplayOutputParameters() } - if ( m_Parser->IsAttributExists("-testenv", m_Expression) ) + if ( m_Parser->IsAttributExists("-testenv", m_VExpression) ) { - std::vector<std::string> val = m_Parser->GetAttribut("-testenv", m_Expression); + std::vector<std::string> val = m_Parser->GetAttribut("-testenv", m_VExpression); if( val.size() == 1 ) { std::ofstream ofs(val[0].c_str()); diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h index 0d10133f64559854642d37f7327146581c47f3b8..4ce5917b27fa392ffb39f90b480520a7b8ba9bc6 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h @@ -33,6 +33,8 @@ #include "itkCommand.h" +#include <vector> + namespace otb { namespace Wrapper @@ -84,6 +86,9 @@ public: /** same as Load method but set the expression before. */ bool Load( const std::string & exp ); + + /** same as Load method but set the expression before. */ + bool Load( std::vector<std::string> vexp); /** Launch the process, using the Execute application method * The method will check if the user asked for help (looking at @@ -168,7 +173,8 @@ private: std::string m_Path; Application::Pointer m_Application; - std::string m_Expression; + //std::string m_Expression; + std::vector<std::string> m_VExpression; CommandLineParser::Pointer m_Parser; WatcherListType m_WatcherList; diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx index 7ee818b0f416ac8c26a4d87f14c96325bd4e3b40..e76b2974c6c959d59661af0e8ff263fdc4c355f1 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx @@ -39,6 +39,24 @@ CommandLineParser::~CommandLineParser() { } +std::string +CommandLineParser::GetPathsAsString(std::vector<std::string> & vexp ) +{ + std::string res; + // The first element must be the module name, non " -" allowed. + // The module path list element are the strings between the first + // element and the first key (ie. " -" string). + std::vector<std::string>::iterator it = vexp.begin(); + ++it; // first element is module name + + while (it->find("-") != std::string::npos) + { + ++it; + } + + return res; +} + std::string CommandLineParser::GetPathsAsString( const std::string & exp ) { @@ -68,6 +86,34 @@ CommandLineParser::GetPathsAsString( const std::string & exp ) return res; } +CommandLineParser::ParseResultType +CommandLineParser::GetPaths( std::vector<std::string> & paths, std::vector<std::string> & exp ) +{ + // The first element must be the module name, non " -" allowed. + // The module path list elements are the strings between the first + // element and the first key (ie. string which begins with "-"). + std::vector<std::string>::iterator it = exp.begin(); + ++it; // first element is module name + + std::vector<itksys::String> pathlist; + + while (it->find("-") == std::string::npos) + { + std::string fullPath = itksys::SystemTools::CollapseFullPath((*it).c_str()); + + if( !itksys::SystemTools::FileIsDirectory(fullPath.c_str()) ) + { + std::cerr<<"Invalid module path: "<<fullPath<<std::endl; + return INVALIDMODULEPATH; + } + paths.push_back(fullPath); + + ++it; + } + + return OK; +} + CommandLineParser::ParseResultType CommandLineParser::GetPaths( std::vector<std::string> & paths, const std::string & exp ) { @@ -118,6 +164,29 @@ CommandLineParser::GetPaths( std::vector<std::string> & paths, const std::string return OK; } +CommandLineParser::ParseResultType +CommandLineParser::GetModuleName( std::string & modName, std::vector<std::string> & exp ) +{ + itksys::RegularExpression reg; + reg.compile("([^0-9a-zA-Z])"); + // The first element must be the module path, non " -" allowed. + if( exp[0].find(" -") != std::string::npos) + { + return NOMODULENAME; + } + + // It must contain only alphanumerical character + if(reg.find(exp[0])) + { + return INVALIDMODULENAME; + } + else + { + modName = exp[0]; + } + + return OK; +} CommandLineParser::ParseResultType CommandLineParser::GetModuleName( std::string & modName, const std::string & exp ) @@ -162,6 +231,45 @@ CommandLineParser::GetModuleName( std::string & modName, const std::string & exp return OK; } +std::vector<std::string> +CommandLineParser::GetAttribut( const std::string & key, std::vector<std::string> & exp ) +{ + std::cout << "here1" << std::endl; + std::vector<std::string> res; + if (!this->IsAttributExists(key,exp)) + return res; + + std::cout << "here2: " << exp.size()<< std::endl; + + bool foundKey=false; + std::vector<std::string>::iterator it = exp.begin(); + while(it!=exp.end() ) + { + std::cout << key << "//" << *it<< std::endl; + + if (foundKey) + { + if (it->find("-") == 0) + { + std::cout << res.size() << std::endl; + return res; + } + else + res.push_back(*it); + } + + if (it->find(key) != std::string::npos) + { foundKey=true; + std::cout << "foundkey true" << std::endl; + } + else + { + std::cout << "foundkey false" << std::endl; + } + ++it; + } + return res; +} std::vector<std::string> CommandLineParser::GetAttribut( const std::string & key, const std::string & exp ) @@ -253,6 +361,36 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp return res; } +std::string +CommandLineParser::GetAttributAsString( const std::string & key, std::vector<std::string> & exp ) +{ + std::string res(""); + std::vector<std::string> values = this->GetAttribut( key, exp ); + + if( values.size() == 0 ) + { + return ""; + } + else if( values.size() == 1 && values[0] == " " ) + { + return ""; + } + + for( unsigned int i=0; i<values.size(); i++) + { + if( i<values.size()-1 ) + { + res.append(values[i]); + res.append(" "); + } + else + { + res.append(values[i]); + } + } + return res; +} + std::string CommandLineParser::GetAttributAsString( const std::string & key, const std::string & exp ) { @@ -296,6 +434,31 @@ CommandLineParser::IsAttributExists( const std::string key, const std::string & return (found != std::string::npos); } +bool +CommandLineParser::IsAttributExists( const std::string key, std::vector<std::string> & exp ) +{ + for (std::vector<std::string>::iterator it = exp.begin() ; it != exp.end(); ++it) + { + if (it->find(key) != std::string::npos) + return true; + } + return false; +} + +std::vector<std::string> +CommandLineParser::GetKeyList( std::vector<std::string> & exp ) +{ + std::vector<std::string> keyList; + for (std::vector<std::string>::iterator it = exp.begin() ; it != exp.end(); ++it) + { + if (it->find("-") == 0 && this->IsAValidKey(*it)) + { + keyList.push_back(*it); + } + } + + return keyList; +} std::vector<std::string> CommandLineParser::GetKeyList( const std::string & exp ) diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h index 804617e919a74ab59743c8e12141c3ad7d1a20bc..6f747680ccd66b85487f68e11a7c002cc34e0ead 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h @@ -57,22 +57,30 @@ public: /** Get the attribut associatd to a key (list of element after the key and before the next "--") as a vector of string. */ std::vector<std::string> GetAttribut( const std::string & key, const std::string & exp ); + std::vector<std::string> GetAttribut( const std::string & key, std::vector<std::string> & exp ); /** Get the attribut associatd to a key (list of element after the key and before the next "--") as a string separated by spaces. */ std::string GetAttributAsString( const std::string & key, const std::string & exp ); - +std::string GetAttributAsString( const std::string & key, std::vector<std::string> & exp ); /** Look if a given key is in an expression. */ bool IsAttributExists(const std::string key, const std::string & exp ); - +bool IsAttributExists(const std::string key, std::vector<std::string> & exp); + /** Get the paths executables in an expression. Store the list in a vector of string. */ CommandLineParser::ParseResultType GetPaths( std::vector<std::string> & paths, const std::string & exp ); + CommandLineParser::ParseResultType GetPaths( std::vector<std::string> & paths, std::vector<std::string> & exp ); + /** Get the paths executables in an expression. Store the list in a vector of string. */ std::string GetPathsAsString( const std::string & exp ); + std::string GetPathsAsString( std::vector<std::string> & vexp ); + /** Get the module name in an expression. It can be the first element of the expression (if the expression doesn't start with a "--" or the attribut associated to the key m_ModuleNameKey). */ CommandLineParser::ParseResultType GetModuleName( std::string & modName, const std::string & exp ); - +CommandLineParser::ParseResultType GetModuleName( std::string & modName, std::vector<std::string> & exp ); + /** Get the list of keys in an expression. That is to say each word starting by "--". */ std::vector<std::string> GetKeyList( const std::string & exp ); +std::vector<std::string> GetKeyList( std::vector<std::string> & exp); protected: /** Constructor */