📄 getpot.h
字号:
GetPot::direct_follow(const char* Default, const char* Option){ if( search_failed_f ) return Default; const char* FollowStr = __match_starting_string(Option); if( FollowStr == 0 ) return Default; // (*) record requested of argument for later ufo-detection if( FollowStr ) __record_argument_request(std::string(Option) + FollowStr); if( ++cursor >= static_cast<unsigned int>(argv.size()) ) cursor = static_cast<unsigned int>(argv.size()); return std::string(FollowStr);}inline std::vector<std::string>GetPot::string_tails(const char* StartString){ std::vector<std::string> result; const unsigned N = static_cast<unsigned int>(strlen(StartString)); std::vector<std::string>::iterator it = argv.begin(); unsigned idx = 0; while( it != argv.end() ) { // (*) does start string match the given option? // NO -> goto next option if( strncmp(StartString, (*it).c_str(), N) != 0) { ++it; ++idx; continue; } // append the found tail to the result vector result.push_back((*it).substr(N)); // adapt the nominus vector std::vector<unsigned>::iterator nit = idx_nominus.begin(); for(; nit != idx_nominus.end(); ++nit) { if( *nit == idx ) { idx_nominus.erase(nit); for(; nit != idx_nominus.end(); ++nit) *nit -= 1; break; } } // erase the found option argv.erase(it); // 100% safe solution: set iterator back to the beginning. // (normally, 'it--' would be enough, but who knows how the // iterator is implemented and .erase() definitely invalidates // the current iterator position. if( argv.empty() ) break; it = argv.begin(); } cursor = 0; nominus_cursor = -1; return result;}inline std::vector<int>GetPot::int_tails(const char* StartString, const int Default /* = -1 */){ std::vector<int> result; const unsigned N = static_cast<unsigned int>(strlen(StartString)); std::vector<std::string>::iterator it = argv.begin(); unsigned idx = 0; while( it != argv.end() ) { // (*) does start string match the given option? // NO -> goto next option if( strncmp(StartString, (*it).c_str(), N) != 0) { ++it; ++idx; continue; } // append the found tail to the result vector result.push_back(__convert_to_type((*it).substr(N), Default)); // adapt the nominus vector std::vector<unsigned>::iterator nit = idx_nominus.begin(); for(; nit != idx_nominus.end(); ++nit) { if( *nit == idx ) { idx_nominus.erase(nit); for(; nit != idx_nominus.end(); ++nit) *nit -= 1; break; } } // erase the found option argv.erase(it); // 100% safe solution: set iterator back to the beginning. // (normally, 'it--' would be enough, but who knows how the // iterator is implemented and .erase() definitely invalidates // the current iterator position. if( argv.empty() ) break; it = argv.begin(); } cursor = 0; nominus_cursor = -1; return result;}inline std::vector<double>GetPot::double_tails(const char* StartString, const double Default /* = -1.0 */){ std::vector<double> result; const unsigned N = static_cast<unsigned int>(strlen(StartString)); std::vector<std::string>::iterator it = argv.begin(); unsigned idx = 0; while( it != argv.end() ) { // (*) does start string match the given option? // NO -> goto next option if( strncmp(StartString, (*it).c_str(), N) != 0) { ++it; ++idx; continue; } // append the found tail to the result vector result.push_back(__convert_to_type((*it).substr(N), Default)); // adapt the nominus vector std::vector<unsigned>::iterator nit = idx_nominus.begin(); for(; nit != idx_nominus.end(); ++nit) { if( *nit == idx ) { idx_nominus.erase(nit); for(; nit != idx_nominus.end(); ++nit) *nit -= 1; break; } } // erase the found option argv.erase(it); // 100% safe solution: set iterator back to the beginning. // (normally, 'it--' would be enough, but who knows how the // iterator is implemented and .erase() definitely invalidates // the current iterator position. if( argv.empty() ) break; it = argv.begin(); } cursor = 0; nominus_cursor = -1; return result;}inline const char*GetPot::__match_starting_string(const char* StartString) // pointer to the place where the string after // the match inside the found argument starts. // 0 no argument matches the starting string.{ const unsigned N = static_cast<unsigned int>(strlen(StartString)); unsigned OldCursor = cursor; if( OldCursor >= static_cast<unsigned int>(argv.size()) ) OldCursor = static_cast<unsigned int>(argv.size()) - 1; search_failed_f = true; // (*) first loop from cursor position until end unsigned c = cursor; for(; c < argv.size(); c++) { if( strncmp(StartString, argv[c].c_str(), N) == 0) { cursor = c; search_failed_f = false; return &(argv[c].c_str()[N]); } } if( ! search_loop_f ) return false; // (*) second loop from 0 to old cursor position for(c = 1; c < OldCursor; c++) { if( strncmp(StartString, argv[c].c_str(), N) == 0) { cursor = c; search_failed_f = false; return &(argv[c].c_str()[N]); } } return 0;}///////////////////////////////////////////////////////////////////////////////// (*) search for flags//.............................................................................//inline boolGetPot::options_contain(const char* FlagList) const{ // go through all arguments that start with a '-' (but not '--') std::string str; STRING_VECTOR::const_iterator it = argv.begin(); for(; it != argv.end(); ++it) { str = __get_remaining_string(*it, prefix); if( str.length() >= 2 && str[0] == '-' && str[1] != '-' ) if( __check_flags(str, FlagList) ) return true; } return false;}inline boolGetPot::argument_contains(unsigned Idx, const char* FlagList) const{ if( Idx >= argv.size() ) return false; // (*) record requested of argument for later ufo-detection // an argument that is checked for flags is considered to be 'requested' ((GetPot*)this)->__record_argument_request(argv[Idx]); if( prefix == "" ) // search argument for any flag in flag list return __check_flags(argv[Idx], FlagList); // if a prefix is set, then the argument index is the index // inside the 'namespace' // => only check list of arguments that start with prefix unsigned no_matches = 0; unsigned i=0; for(; i<argv.size(); ++i) { const std::string Remain = __get_remaining_string(argv[i], prefix); if( Remain != "") { no_matches += 1; if( no_matches == Idx) return __check_flags(Remain, FlagList); } } // no argument in this namespace return false;}inline boolGetPot::__check_flags(const std::string& Str, const char* FlagList) const{ const char* p=FlagList; for(; *p != '\0' ; p++) if( Str.find(*p) != std::string::npos ) return true; // found something return false;}///////////////////////////////////////////////////////////////////////////////// (*) nominus argumentsinline STRING_VECTORGetPot::nominus_vector() const // return vector of nominus arguments{ STRING_VECTOR nv; std::vector<unsigned>::const_iterator it = idx_nominus.begin(); for(; it != idx_nominus.end(); ++it) { nv.push_back(argv[*it]); // (*) record for later ufo-detection // when a nominus vector is requested, the entire set of nominus arguments are // tagged as 'requested' ((GetPot*)this)->__record_argument_request(argv[*it]); } return nv;}inline std::stringGetPot::next_nominus(){ if( nominus_cursor < int(idx_nominus.size()) - 1 ) { const std::string Tmp = argv[idx_nominus[++nominus_cursor]]; // (*) record for later ufo-detection __record_argument_request(Tmp); // -- cannot use the Tmp variable, since it is temporary and c_str() will return a pointer // to something that does no longer exist. return Tmp; } return std::string("");}///////////////////////////////////////////////////////////////////////////////// (*) variables//.............................................................................//inline intGetPot::operator()(const char* VarName, int Default) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; return __convert_to_type(sv->original, Default);}inline doubleGetPot::operator()(const char* VarName, const double& Default) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; return __convert_to_type(sv->original, Default);}inline const std::stringGetPot::operator()(const char* VarName, const char* Default) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; // -- returning a c_str() pointer is OK here, since the variable remains existant, // while 'sv' of course is delete at the end of the function. return sv->original;}inline intGetPot::operator()(const char* VarName, int Default, unsigned Idx) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; const std::string* element = sv->get_element(Idx); if( element == 0 ) return Default; return __convert_to_type(*element, Default);}inline doubleGetPot::operator()(const char* VarName, const double& Default, unsigned Idx) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; const std::string* element = sv->get_element(Idx); if( element == 0 ) return Default; return __convert_to_type(*element, Default);}inline const std::stringGetPot::operator()(const char* VarName, const char* Default, unsigned Idx) const{ // (*) recording of requested variables happens in '__find_variable()' const variable* sv = __find_variable(VarName); if( sv == 0 ) return Default; const std::string* element = sv->get_element(Idx); if( element == 0 ) return Default; return *element;}inline void GetPot::__record_argument_request(const std::string& Name){ if( ! __request_recording_f ) return; // (*) record requested variable for later ufo detection _requested_arguments.push_back(Name); // (*) record considered section for ufo detection STRING_VECTOR STree = __get_section_tree(Name); victorate(std::string, STree, it) if( find(_requested_sections.begin(), _requested_sections.end(), *it) == _requested_sections.end() ) if( section.length() != 0 ) _requested_sections.push_back(*it);}inline void GetPot::__record_variable_request(const std::string& Name){ if( ! __request_recording_f ) return; // (*) record requested variable for later ufo detection _requested_variables.push_back(Name); // (*) record considered section for ufo detection STRING_VECTOR STree = __get_section_tree(Name); victorate(std::string, STree, it) if( find(_requested_sections.begin(), _requested_sections.end(), *it) == _requested_sections.end() ) if( section.length() != 0 ) _requested_sections.push_back(*it);}// (*) following functions are to be used from 'outside', after getpot has parsed its// arguments => append an argument in the argument vector that reflects the additioninline voidGetPot::__set_variable(const char* VarName, const char* Value){ const GetPot::variable* Var = __find_variable(VarName); if( Var == 0 ) variables.push_back(variable(VarName, Value, _field_separator.c_str())); else ((GetPot::variable*)Var)->take(Value, _field_separator.c_str()); }inline voidGetPot::set(const char* VarName, const char* Value, const bool Requested /* = yes */){ const std::string Arg = prefix + std::string(VarName) + std::string("=") + std::string(Value); argv.push_back(Arg); __set_variable(VarName, Value); // if user does not specify the variable as 'not being requested' it will be // considered amongst the requested variables if( Requested ) __record_variable_request(Arg);}inline voidGetPot::set(const char* VarName, const double& Value, const bool Requested /* = yes */)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -