TuttleOFX
1
|
00001 #ifndef _SAM_DO_COMMANDLINE_HPP_ 00002 #define _SAM_DO_COMMANDLINE_HPP_ 00003 00004 #include "global.hpp" 00005 00006 #include <iterator> 00007 #include <vector> 00008 #include <string> 00009 00010 namespace sam { 00011 namespace samdo { 00012 00013 #ifndef SAM_DO_PIPE_STR 00014 #define SAM_DO_PIPE_STR "//" 00015 #endif 00016 00017 static const std::string kpipe = SAM_DO_PIPE_STR; 00018 00019 00020 /** 00021 * @brief Decomposes command line arguments into a list of options and a list of node command lines. Groups the arguments without insterpretation at this step. 00022 * 00023 * @param[in] argc number of arguments ont the command line 00024 * @param[in] argv list of string arguments 00025 * @param[out] cl_options list of options for sam-do 00026 * @param[out] cl_commands list of node command lines (list of strings groups without insterpretation at this step) 00027 * @param[in] pipe pipe character 00028 */ 00029 void decomposeCommandLine( const int argc, char** const argv, std::vector<std::string>& cl_options, std::vector< std::vector<std::string> >& cl_commands, const std::string& pipe = kpipe ) 00030 { 00031 cl_commands.reserve(10); 00032 cl_commands.resize(1); // first node for options 00033 00034 // split the command line to identify the multiple parts 00035 for( int i = 1; i < argc; ++i ) 00036 { 00037 const std::string s( argv[i] ); 00038 if( s == pipe ) 00039 { 00040 cl_commands.resize( cl_commands.size()+1 ); 00041 cl_commands.back().reserve(10); 00042 } 00043 else 00044 { 00045 cl_commands.back().push_back( s ); 00046 } 00047 } 00048 00049 // reoganize nodes 00050 // first and last nodes may only be flags to sam-do itself 00051 { 00052 // First node 00053 std::vector<std::string>& firstNode = cl_commands.front(); 00054 std::vector<std::string>::iterator fNodeIt = firstNode.begin(); 00055 std::vector<std::string>::iterator fNodeItEnd = firstNode.end(); 00056 while( fNodeIt != fNodeItEnd && 00057 (*fNodeIt)[0] == '-' ) 00058 { 00059 ++fNodeIt; 00060 } 00061 // options in front of the first node are options for sam-do 00062 if( firstNode.begin() != fNodeIt ) 00063 { 00064 std::copy( firstNode.begin(), fNodeIt, std::back_inserter(cl_options) ); 00065 firstNode.erase( firstNode.begin(), fNodeIt ); 00066 } 00067 if( firstNode.size() == 0 ) 00068 { 00069 cl_commands.erase( cl_commands.begin() ); 00070 } 00071 } 00072 if( cl_commands.size() ) 00073 { 00074 // Last node 00075 // Is it a node or only flags? 00076 const std::vector<std::string>& notNode = cl_commands.back(); 00077 if( notNode.size() == 0 ) 00078 { 00079 // If the command line end with the split character "//", 00080 // we detect an empty node... so ignore it. 00081 // eg. "sam do reader foo.jpg // blur //" 00082 cl_commands.erase( cl_commands.end()-1 ); 00083 } 00084 else if( notNode[0][0] == '-' ) 00085 { 00086 // if the last node is not a node but a list of options 00087 // move content to command line options list. 00088 std::copy( notNode.begin(), notNode.end(), std::back_inserter(cl_options) ); 00089 cl_commands.erase( cl_commands.end()-1 ); 00090 } 00091 } 00092 } 00093 00094 } 00095 } 00096 00097 #endif 00098