inlib
1.2.0
|
Public Types | |
typedef std::pair< std::string, std::string > | arg |
Public Member Functions | |
args () | |
args (int a_argc, char *a_argv[]) | |
args (const std::vector< std::string > &a_args) | |
args (const std::vector< arg > &a_args) | |
args (const std::string &a_args, const std::string &a_sep=" ", bool a_strip=false) | |
virtual | ~args () |
args (const args &a_from) | |
args & | operator= (const args &a_from) |
const std::vector< arg > & | get_args () const |
bool | is_arg (const std::string &a_string) const |
unsigned int | size () const |
unsigned int | number () const |
bool | find (const std::string &a_key, std::string &a_value) const |
std::vector< std::string > | find (const std::string &a_key) const |
bool | find (const std::string &a_string, bool &a_value) const |
template<class aT > | |
bool | find (const std::string &a_string, aT &a_value) const |
std::vector< std::string > | tovector () const |
bool | add (const std::string &a_key, const std::string &a_value, bool a_override=true) |
void | add (const std::vector< std::string > &a_args, bool a_strip=false) |
int | remove (const std::string &a_key) |
void | remove_first () |
bool | last (std::string &a_key, std::string &a_value) const |
bool | file (std::string &a_file) const |
std::vector< std::string > | files (bool a_skip_first=true) const |
bool | argcv (int &a_argc, char **&a_argv) const |
void | delete_argcv (int &a_argc, char **&a_argv) const |
bool | known_options (const std::vector< std::string > &a_opts) const |
void | files_at_end (bool a_skip_first=true) |
void | dump (std::ostream &a_out) const |
typedef std::pair<std::string,std::string> inlib::args::arg |
inlib::args::args | ( | int | a_argc, |
char * | a_argv[] | ||
) | [inline] |
Definition at line 21 of file args.
{ for(int index=0;index<a_argc;index++) { std::string s(a_argv[index]); std::string::size_type pos = s.find('='); if(pos==std::string::npos) { m_args.push_back(arg(s,"")); } else { std::string key = s.substr(0,pos); pos++; std::string value = s.substr(pos,s.size()-pos); m_args.push_back(arg(key,value)); } } }
inlib::args::args | ( | const std::vector< std::string > & | a_args | ) | [inline] |
inlib::args::args | ( | const std::vector< arg > & | a_args | ) | [inline] |
inlib::args::args | ( | const std::string & | a_args, |
const std::string & | a_sep = " " , |
||
bool | a_strip = false |
||
) | [inline] |
inlib::args::args | ( | const args & | a_from | ) | [inline] |
bool inlib::args::add | ( | const std::string & | a_key, |
const std::string & | a_value, | ||
bool | a_override = true |
||
) | [inline] |
void inlib::args::add | ( | const std::vector< std::string > & | a_args, |
bool | a_strip = false |
||
) | [inline] |
Definition at line 124 of file args.
{ for(std::vector<std::string>::const_iterator it = a_args.begin(); it!=a_args.end();++it) { std::vector<std::string> ws; words((*it),"=",false,ws); if(ws.size()==1) { if(a_strip) { m_args.push_back(arg(strp(ws[0]),"")); } else { m_args.push_back(arg(ws[0],"")); } } else if(ws.size()>=2) { if(a_strip) { m_args.push_back(arg(strp(ws[0]),strp(ws[1]))); } else { m_args.push_back(arg(ws[0],ws[1])); } } } }
bool inlib::args::argcv | ( | int & | a_argc, |
char **& | a_argv | ||
) | const [inline] |
Definition at line 201 of file args.
{ // If using with : // int argc; // char** argv; // args.argcv(argc,argv); // you can delete with : // args.delete_argcv(argc,argv); if(m_args.empty()) {a_argc = 0;a_argv = 0;return true;} typedef char* cstr_t; cstr_t* av = new cstr_t[m_args.size()]; if(!av) {a_argc = 0;a_argv = 0;return false;} a_argv = av; for(std::vector<arg>::const_iterator it = m_args.begin(); it!=m_args.end();++it,av++) { std::string::size_type lf = (*it).first.length(); std::string::size_type ls = (*it).second.length(); std::string::size_type sz = 0; if(ls) { sz = lf + 1 + ls; } else { sz = lf; } char* p = new char[sz+1]; if(!p) {a_argc = 0;a_argv = 0;return false;} //some delete are lacking. *av = p; {char* pf = (char*)(*it).first.c_str(); for(std::string::size_type i=0;i<lf;i++) {*p++ = *pf++;} *p = 0;} if(ls) {*p = '=';p++;} {char* ps = (char*)(*it).second.c_str(); for(std::string::size_type i=0;i<ls;i++) {*p++ = *ps++;} *p = 0;} } a_argc = (int)m_args.size(); return true; }
void inlib::args::delete_argcv | ( | int & | a_argc, |
char **& | a_argv | ||
) | const [inline] |
void inlib::args::dump | ( | std::ostream & | a_out | ) | const [inline] |
bool inlib::args::file | ( | std::string & | a_file | ) | const [inline] |
std::vector<std::string> inlib::args::files | ( | bool | a_skip_first = true | ) | const [inline] |
Definition at line 183 of file args.
{ // Get the serie of trailing args not beginning with '-' // and without a value (not of the form [-]xxx=yyy). // Note that an argument like that in between arguments // is NOT taken into account. std::vector<std::string> files; if(m_args.empty()) return files; std::vector<arg>::const_iterator it = m_args.begin(); if(a_skip_first) it++; for(;it!=m_args.end();++it) { if( ((*it).first.find('-')==0) || (*it).second.size() ) { files.clear(); } else { files.push_back((*it).first); } } return files; }
void inlib::args::files_at_end | ( | bool | a_skip_first = true | ) | [inline] |
Definition at line 261 of file args.
{ // reorder to have "file" arguments at end. if(m_args.empty()) return; std::vector<arg> args; if(a_skip_first) args.push_back(*(m_args.begin())); //first pass : //FIXME : Android : with inlib/stl/vector having // const_iterator does not compile. {std::vector<arg>::iterator it = m_args.begin(); if(a_skip_first) it++; for(;it!=m_args.end();++it) { if( ((*it).first.find('-')==0) || (*it).second.size() ) { args.push_back(*it); } }} //second pass : //FIXME : Android : with inlib/stl/vector having // const_iterator does not compile. {std::vector<arg>::iterator it = m_args.begin(); if(a_skip_first) it++; for(;it!=m_args.end();++it) { if( ((*it).first.find('-')==0) || (*it).second.size() ) { } else { args.push_back(*it); } }} m_args = args; }
bool inlib::args::find | ( | const std::string & | a_key, |
std::string & | a_value | ||
) | const [inline] |
std::vector<std::string> inlib::args::find | ( | const std::string & | a_key | ) | const [inline] |
bool inlib::args::find | ( | const std::string & | a_string, |
bool & | a_value | ||
) | const [inline] |
bool inlib::args::find | ( | const std::string & | a_string, |
aT & | a_value | ||
) | const [inline] |
const std::vector<arg>& inlib::args::get_args | ( | ) | const [inline] |
bool inlib::args::is_arg | ( | const std::string & | a_string | ) | const [inline] |
bool inlib::args::known_options | ( | const std::vector< std::string > & | a_opts | ) | const [inline] |
Definition at line 243 of file args.
{ for(std::vector<arg>::const_iterator it = m_args.begin(); it!=m_args.end();++it) { if((*it).first.find('-')==0) { //find '-' at first pos. bool found = false; for(std::vector<std::string>::const_iterator it2 = a_opts.begin(); it2!=a_opts.end();++it2) { if((*it).first==(*it2)) { found = true; break; } } if(!found) return false; } } return true; }
bool inlib::args::last | ( | std::string & | a_key, |
std::string & | a_value | ||
) | const [inline] |
unsigned int inlib::args::number | ( | ) | const [inline] |
int inlib::args::remove | ( | const std::string & | a_key | ) | [inline] |
void inlib::args::remove_first | ( | ) | [inline] |
unsigned int inlib::args::size | ( | ) | const [inline] |
std::vector<std::string> inlib::args::tovector | ( | ) | const [inline] |
Definition at line 91 of file args.
{ // Return a vector of string <name=value> std::vector<std::string> vec; for(std::vector<arg>::const_iterator it = m_args.begin(); it!=m_args.end();++it) { std::string s; if((*it).second.empty()) { s = (*it).first; } else { s = (*it).first; s += "="; s += (*it).second; } vec.push_back(s); } return vec; }