📄 getpot.h
字号:
for(++it; it != it_end; ++it, i++) { std::string arg = *it; if( arg.length() == 0 ) continue; // -- [section] labels if( (arg.length() > 1) && (arg[0] == '[' ) && (arg[arg.length()-1] == ']') ) { const std::string Name = __DBE_expand_string(arg.substr(1, arg.length()-2)); section = __process_section_label(Name, section_stack); // new section --> append to list of sections if( std::find(section_list.begin(), section_list.end(), section) == section_list.end() ) if( section.length() != 0 ) section_list.push_back(section); argv.push_back(arg); } else { arg = section + __DBE_expand_string(arg); argv.push_back(arg); } // -- separate array for nominus arguments if( arg[0] != '-' ) idx_nominus.push_back(unsigned(i)); // -- variables: does arg contain a '=' operator ? for(const char* p = arg.c_str(); *p ; p++) { if( *p == '=' ) { // set terminating 'zero' to treat first part as single string // => arg (from start to 'p') = Name of variable // p+1 (until terminating zero) = value of variable char* o = (char*)p++; *o = '\0'; const GetPot::variable* Var = __find_variable(arg.c_str()); if( Var == 0 ) variables.push_back(variable(arg.c_str(), p)); else ((GetPot::variable*)Var)->take(p); *o = '='; break; } } }}inline std::vector<std::string>GetPot::__read_in_file(const char* FileName){ std::ifstream i(FileName); if( ! i ) return std::vector<std::string>(); // argv[0] == the filename of the file that was read in return __read_in_stream(i);}inline std::vector<std::string>GetPot::__read_in_stream(std::istream& istr){ std::vector<std::string> brute_tokens; while(istr) { __skip_whitespace(istr); const std::string Token = __get_next_token(istr); if( Token.empty() || (Token[0] == static_cast<std::string::value_type>(EOF)) ) break; brute_tokens.push_back(Token); } // -- reduce expressions of token1'='token2 to a single // string 'token1=token2' // -- copy everything into 'argv' // -- arguments preceded by something like '[' name ']' (section) // produce a second copy of each argument with a prefix '[name]argument' unsigned i1 = 0; unsigned i2 = 1; unsigned i3 = 2; std::vector<std::string> arglist; while( i1 < brute_tokens.size() ) { const std::string& SRef = brute_tokens[i1]; // 1) concatinate 'abcdef' '=' 'efgasdef' to 'abcdef=efgasdef' // note: java.lang.String: substring(a,b) = from a to b-1 // C++ string: substr(a,b) = from a to a + b if( i2 < brute_tokens.size() && brute_tokens[i2] == "=" ) { if( i3 >= brute_tokens.size() ) arglist.push_back(brute_tokens[i1] + brute_tokens[i2]); else arglist.push_back(brute_tokens[i1] + brute_tokens[i2] + brute_tokens[i3]); i1 = i3+1; i2 = i3+2; i3 = i3+3; continue; } else { arglist.push_back(SRef); i1=i2; i2=i3; i3++; } } return arglist;}inline voidGetPot::__skip_whitespace(std::istream& istr) // find next non-whitespace while deleting comments{ int tmp = istr.get(); do { while( isspace(tmp) ) { tmp = istr.get(); if( ! istr ) return; } // found a non whitespace if( tmp == '#' ) { // comment => skip until end of line while( tmp != '\n' ) { if( ! istr ) { istr.unget(); return; } tmp = istr.get(); } continue; } else { istr.unget(); return; } } while( istr );}inline const std::stringGetPot::__get_next_token(std::istream& istr) // get next concatinates string token. consider quotes that embrace // whitespaces{ std::string token; int tmp = 0; int last_letter = 0; while(1+1 == 2) { last_letter = tmp; tmp = istr.get(); if( tmp == EOF || ((tmp == ' ' || tmp == '\t' || tmp == '\n') && last_letter != '\\') ) { return token; } else if( tmp == '\'' && last_letter != '\\' ) { // QUOTES: un-backslashed quotes => it's a string token += __get_string(istr); continue; } else if( tmp == '{' && last_letter == '$') { token += '{' + __get_until_closing_bracket(istr); continue; } else if( tmp == '$' && last_letter == '\\') { token += tmp; tmp = 0; // so that last_letter will become = 0, not '$'; continue; } else if( tmp == '\\' && last_letter != '\\') continue; // don't append un-backslashed backslashes token += tmp; } return token;}inline const std::stringGetPot::__get_string(std::istream& istr) // parse input until next matching '{ std::string str; int tmp = 0; int last_letter = 0; while(1 + 1 == 2) { last_letter = tmp; tmp = istr.get(); if( tmp == EOF) return str; // un-backslashed quotes => it's the end of the string else if( tmp == '\'' && last_letter != '\\') return str; else if( tmp == '\\' && last_letter != '\\') continue; // don't append str += tmp; } return str;}inline const std::stringGetPot::__get_until_closing_bracket(std::istream& istr) // parse input until next matching }{ std::string str = ""; int tmp = 0; int last_letter = 0; int brackets = 1; while(1 + 1 == 2) { last_letter = tmp; tmp = istr.get(); if( tmp == EOF) return str; else if( tmp == '{' && last_letter == '$') brackets += 1; else if( tmp == '}') { brackets -= 1; // un-backslashed brackets => it's the end of the string if( brackets == 0) return str + '}'; else if( tmp == '\\' && last_letter != '\\') continue; // do not append an unbackslashed backslash } str += tmp; } return str;}inline std::stringGetPot::__process_section_label(const std::string& Section, std::vector<std::string>& section_stack){ std::string sname = Section; // 1) subsection of actual section ('./' prefix) if( sname.length() >= 2 && sname.substr(0, 2) == "./" ) { sname = sname.substr(2); } // 2) subsection of parent section ('../' prefix) else if( sname.length() >= 3 && sname.substr(0, 3) == "../" ) { do { if( section_stack.end() != section_stack.begin() ) section_stack.pop_back(); sname = sname.substr(3); } while( sname.substr(0, 3) == "../" ); } // 3) subsection of the root-section else { section_stack.erase(section_stack.begin(), section_stack.end()); // [] => back to root section } if( sname != "" ) { // parse section name for 'slashes' unsigned i=0; while( i < sname.length() ) { if( sname[i] == '/' ) { section_stack.push_back(sname.substr(0,i)); if( i+1 < sname.length()-1 ) sname = sname.substr(i+1); i = 0; } else i++; } section_stack.push_back(sname); } std::string section_name = ""; if( section_stack.size() != 0 ) victorate(std::string, section_stack, it) { section_name += *it + "/"; } return section_name;}// convert string to DOUBLE, if not possible return Defaultinline double GetPot::__convert_to_type(const std::string& String, double Default) const{ double tmp; if( std::sscanf(String.c_str(),"%lf", &tmp) != 1 ) return Default; return tmp;}// convert string to LONG DOUBLE, if not possible return Defaultinline long double GetPot::__convert_to_type(const std::string& String, long double Default) const{ long double tmp; if( std::sscanf(String.c_str(),"%Lf", &tmp) != 1 ) return Default; return tmp;}// convert string to INT, if not possible return Defaultinline int GetPot::__convert_to_type(const std::string& String, int Default) const{ int tmp; if( std::sscanf(String.c_str(),"%i", &tmp) != 1 ) return Default; return tmp; }inline boolGetPot::__convert_to_type(const std::string& String, bool Default) const{ std::string newstring(String); //std::transform(newstring.begin(), newstring.end(), newstring.begin(), std::toupper); for (unsigned int i=0; i<newstring.length(); ++i) { newstring[i]=toupper(newstring[i]); } if (newstring.find("TRUE")!=std::string::npos) return true; if (newstring.find("FALSE")!=std::string::npos) return false; return Default;}//////////////////////////////////////////////////////////////////////////////// (*) cursor oriented functions//.............................................................................inline const std::string GetPot::__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 std::string &Option){ return search(Option.c_str());}inline boolGetPot::search(const char* Option){ unsigned OldCursor = cursor; const std::string SearchTerm = prefix + Option; if( OldCursor >= argv.size() ) OldCursor = 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 = 0; 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;}#ifdef GETPOT_ALLOW_VARGSinline boolGetPot::search(unsigned No, const char* P, ...){ if( No == 0 ) return false; // search for the first argument if( search(P) == true ) return true; // start interpreting variable argument list std::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 true; } } va_end(ap); return false;}#endif //GETPOT_ALLOW_VARGSinline void GetPot::reset_cursor(){ search_failed_f = false; cursor = 0; }inline void GetPot::init_multiple_occurrence(){ disable_loop(); reset_cursor(); }///////////////////////////////////////////////////////////////////////////////// (*) direct access to command line arguments//.............................................................................//inline const char* GetPot::operator[](unsigned idx) const{ return idx<argv.size() ? argv[idx].c_str() : 0; }inline int GetPot::get(unsigned Idx, int Default) const{ if( Idx >= argv.size() ) return Default; return __convert_to_type(argv[Idx], Default);}inline double GetPot::get(unsigned Idx, double Default) const{ if( Idx >= argv.size() ) return Default; return __convert_to_type(argv[Idx], Default);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -