File indexing completed on 2026-04-09 07:49:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #pragma once
0021 #include <cstring>
0022 #include <string>
0023 #include <sstream>
0024 #include <iostream>
0025 #include <vector>
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 const char* get_option_(int argc, char** argv, const char* option, const char* fallback)
0054 {
0055 const char* val = NULL ;
0056 for(int i=1 ; i < argc - 1 ; i++ )
0057 {
0058 const char* a0 = argv[i] ;
0059 const char* a1 = argv[i+1] ;
0060 if(a0 && strcmp(a0, option) == 0) val = a1 ;
0061 }
0062 return val ? val : fallback ;
0063 }
0064
0065 template <typename T>
0066 T lexical_cast(const char* par)
0067 {
0068 T var;
0069 std::istringstream iss;
0070 iss.str(par);
0071 iss >> var;
0072 return var;
0073 }
0074
0075 std::string get_field( const std::string& par, int field=-1, char delim=',')
0076 {
0077 if( field < 0 ) return par ;
0078 std::vector<std::string> fields ;
0079 std::stringstream ss ;
0080 int l = strlen(par.c_str());
0081 for(int i=0 ; i < l ; i++)
0082 {
0083 char c = par[i];
0084 if( c != delim )
0085 {
0086 ss << c ;
0087 }
0088
0089 if( c == delim || i == l-1 )
0090 {
0091 std::string s = ss.str();
0092 fields.push_back( s );
0093 ss.str("");
0094 ss.clear();
0095 }
0096 }
0097 return field < int(fields.size()) ? fields[field] : "" ;
0098 }
0099
0100 template <typename T> T get_option( int argc, char** argv, const char* option, const char* fallback )
0101 {
0102 std::string opt = option ;
0103 char delim = ',' ;
0104
0105 size_t pos = opt.find(delim) ;
0106 int field(-1) ;
0107
0108
0109 if( pos != std::string::npos )
0110 {
0111 std::string s_field = opt.substr(pos+1) ;
0112 opt = opt.substr(0, pos) ;
0113 field = lexical_cast<int>( s_field.c_str() );
0114
0115
0116 }
0117
0118 std::string par = get_option_( argc, argv, opt.c_str() , fallback );
0119 if( field > -1 ) par = get_field( par, field, delim );
0120 return lexical_cast<T>(par.c_str()) ;
0121 }
0122
0123