?? getpot.h
字號:
// convert string to DOUBLE, if not possible return Defaultinline doubleGetPot::__convert_to_type(const std::string& String, double Default) const{ double tmp; if( sscanf(String.c_str(),"%lf", &tmp) != 1 ) return Default; return tmp;}// convert string to INT, if not possible return Defaultinline intGetPot::__convert_to_type(const std::string& String, int Default) const{ // NOTE: intermediate results may be floating points, so that the string // may look like 2.0e1 (i.e. float format) => use float conversion // in any case. return (int)__convert_to_type(String, (double)Default);}//////////////////////////////////////////////////////////////////////////////// (*) cursor oriented functions//.............................................................................inline const std::stringGetPot::__get_remaining_string(const std::string& String, const std::string& Start) const // Checks if 'String' begins with 'Start' and returns the remaining String. // Returns None if String does not begin with Start.{ if( Start == "" ) return String; // note: java.lang.String: substring(a,b) = from a to b-1 // C++ string: substr(a,b) = from a to a + b if( String.find(Start) == 0 ) return String.substr(Start.length()); else return "";}// -- search for a certain argument and set cursor to positioninline boolGetPot::search(const char* Option){ unsigned OldCursor = cursor; const std::string SearchTerm = prefix + Option; // (*) record requested arguments for later ufo detection __record_argument_request(SearchTerm); if( OldCursor >= 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( argv[c] == SearchTerm ) { cursor = c; search_failed_f = false; return true; } } if( ! search_loop_f ) return false; // (*) second loop from 0 to old cursor position for(c = 1; c < OldCursor; c++) { if( argv[c] == SearchTerm ) { cursor = c; search_failed_f = false; return true; } } // in case nothing is found the cursor stays where it was return false;}inline boolGetPot::search(unsigned No, const char* P, ...){ // (*) recording the requested arguments happens in subroutine 'search' if( No == 0 ) return false; // search for the first argument if( search(P) == true ) return true; // start interpreting variable argument list va_list ap; va_start(ap, P); unsigned i = 1; for(; i < No; ++i) { char* Opt = va_arg(ap, char *); if( search(Opt) == true ) break; } if( i < No ) { ++i; // loop was left before end of array --> hit but // make sure that the rest of the search terms is marked // as requested. for(; i < No; ++i) { char* Opt = va_arg(ap, char *); // (*) record requested arguments for later ufo detection __record_argument_request(Opt); } va_end(ap); return true; } va_end(ap); // loop was left normally --> no hit return false;}inline voidGetPot::reset_cursor(){ search_failed_f = false; cursor = 0; }inline voidGetPot::init_multiple_occurrence(){ disable_loop(); reset_cursor(); }///////////////////////////////////////////////////////////////////////////////// (*) direct access to command line arguments//.............................................................................//inline const std::stringGetPot::operator[](unsigned idx) const{ return idx < argv.size() ? argv[idx] : ""; }inline intGetPot::get(unsigned Idx, int Default) const{ if( Idx >= argv.size() ) return Default; return __convert_to_type(argv[Idx], Default);}inline doubleGetPot::get(unsigned Idx, const double& Default) const{ if( Idx >= argv.size() ) return Default; return __convert_to_type(argv[Idx], Default);}inline const std::stringGetPot::get(unsigned Idx, const char* Default) const{ if( Idx >= argv.size() ) return Default; else return argv[Idx];}inline unsignedGetPot::size() const{ return static_cast<unsigned int>(argv.size()); }// -- next() function groupinline intGetPot::next(int Default){ if( search_failed_f ) return Default; cursor++; if( cursor >= argv.size() ) { cursor = static_cast<unsigned int>(argv.size()); return Default; } // (*) record requested argument for later ufo detection __record_argument_request(argv[cursor]); const std::string Remain = __get_remaining_string(argv[cursor], prefix); return Remain != "" ? __convert_to_type(Remain, Default) : Default;}inline doubleGetPot::next(const double& Default){ if( search_failed_f ) return Default; cursor++; if( cursor >= argv.size() ) { cursor = static_cast<unsigned int>(argv.size()); return Default; } // (*) record requested argument for later ufo detection __record_argument_request(argv[cursor]); std::string Remain = __get_remaining_string(argv[cursor], prefix); return Remain != "" ? __convert_to_type(Remain, Default) : Default;}inline const std::stringGetPot::next(const char* Default){ if( search_failed_f ) return Default; cursor++; if( cursor >= argv.size() ) { cursor = static_cast<unsigned int>(argv.size()); return Default; } // (*) record requested argument for later ufo detection __record_argument_request(argv[cursor]); const std::string Remain = __get_remaining_string(argv[cursor], prefix); if( Remain == "" ) return Default; // (*) function returns a pointer to a char array (inside Remain) // this array will be deleted, though after this function call. // To ensure propper functioning, create a copy inside *this // object and only delete it, when *this is deleted. char* result = new char[Remain.length()+1]; strncpy(result, Remain.c_str(), Remain.length()+1); // store the created string internally, delete if when object deleted __internal_string_container.push_back(result); return result;}// -- follow() function group// distinct option to be searched forinline intGetPot::follow(int Default, const char* Option){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( search(Option) == false ) return Default; return next(Default);}inline doubleGetPot::follow(const double& Default, const char* Option){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( search(Option) == false ) return Default; return next(Default);}inline const std::stringGetPot::follow(const char* Default, const char* Option){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( search(Option) == false ) return Default; return next(Default);}// -- second follow() function group// multiple option to be searched forinline intGetPot::follow(int Default, unsigned No, const char* P, ...){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( No == 0 ) return Default; if( search(P) == true ) return next(Default); va_list ap; va_start(ap, P); unsigned i=1; for(; i<No; ++i) { char* Opt = va_arg(ap, char *); if( search(Opt) == true ) { va_end(ap); return next(Default); } } va_end(ap); return Default;}inline doubleGetPot::follow(const double& Default, unsigned No, const char* P, ...){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( No == 0 ) return Default; if( search(P) == true ) return next(Default); va_list ap; va_start(ap, P); for(unsigned i=1; i<No; ++i) { char* Opt = va_arg(ap, char *); if( search(Opt) == true ) { va_end(ap); return next(Default); } } va_end(ap); return Default;}inline const std::stringGetPot::follow(const char* Default, unsigned No, const char* P, ...){ // (*) record requested of argument is entirely handled in 'search()' and 'next()' if( No == 0 ) return Default; if( search(P) == true ) return next(Default); va_list ap; va_start(ap, P); unsigned i=1; for(; i<No; ++i) { char* Opt = va_arg(ap, char *); if( search(Opt) == true ) { va_end(ap); return next(Default); } } va_end(ap); return Default;}///////////////////////////////////////////////////////////////////////////////// (*) lists of nominus following an option//.............................................................................//inline std::vector<std::string> GetPot::nominus_followers(const char* Option){ std::vector<std::string> result_list; if( search(Option) == false ) return result_list; while( 1 + 1 == 2 ) { ++cursor; if( cursor >= argv.size() ) { cursor = argv.size() - 1; return result_list; } if( argv[cursor].length() >= 1 ) { if( argv[cursor][0] == '-' ) { return result_list; } // -- record for later ufo-detection __record_argument_request(argv[cursor]); // -- append to the result list result_list.push_back(argv[cursor]); } }}inline std::vector<std::string> GetPot::nominus_followers(unsigned No, ...){ std::vector<std::string> result_list; // (*) record requested of argument is entirely handled in 'search()' // and 'nominus_followers()' if( No == 0 ) return result_list; va_list ap; va_start(ap, No); for(unsigned i=0; i<No; ++i) { char* Option = va_arg(ap, char *); std::vector<std::string> tmp = nominus_followers(Option); result_list.insert(result_list.end(), tmp.begin(), tmp.end()); // std::cerr << "option = '" << Option << "'" << std::endl; // std::cerr << "length = " << tmp.size() << std::endl; // std::cerr << "new result list = <"; // for(std::vector<std::string>::const_iterator it = result_list.begin(); // it != result_list.end(); ++it) // std::cerr << *it << ", "; // std::cerr << ">\n"; } va_end(ap); return result_list;}///////////////////////////////////////////////////////////////////////////////// (*) directly connected options//.............................................................................//inline intGetPot::direct_follow(int Default, const char* Option){ const char* FollowStr = __match_starting_string(Option); if( FollowStr == 0x0 ) return Default; // (*) record requested of argument for later ufo-detection __record_argument_request(std::string(Option) + FollowStr); if( ++cursor >= static_cast<unsigned int>(argv.size()) ) cursor = static_cast<unsigned int>(argv.size()); return __convert_to_type(FollowStr, Default);}inline doubleGetPot::direct_follow(const double& Default, const char* Option){ const char* FollowStr = __match_starting_string(Option); if( FollowStr == 0 ) return Default; // (*) record requested of argument for later ufo-detection __record_argument_request(std::string(Option) + FollowStr); if( ++cursor >= static_cast<unsigned int>(argv.size()) ) cursor = static_cast<unsigned int>(argv.size()); return __convert_to_type(FollowStr, Default);}inline const std::string
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -