inlib  1.2.0
Public Types | Public Member Functions
inlib::args Class Reference

List of all members.

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)
argsoperator= (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

Detailed Description

Definition at line 16 of file args.


Member Typedef Documentation

typedef std::pair<std::string,std::string> inlib::args::arg

Definition at line 18 of file args.


Constructor & Destructor Documentation

inlib::args::args ( ) [inline]

Definition at line 20 of file args.

{}
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]

Definition at line 35 of file args.

{add(a_args);}
inlib::args::args ( const std::vector< arg > &  a_args) [inline]

Definition at line 36 of file args.

:m_args(a_args){}
inlib::args::args ( const std::string &  a_args,
const std::string &  a_sep = " ",
bool  a_strip = false 
) [inline]

Definition at line 37 of file args.

                                                                                 {
    std::vector<std::string> args;
    words(a_args,a_sep,false,args);  
    add(args,a_strip);
  }
virtual inlib::args::~args ( ) [inline, virtual]

Definition at line 42 of file args.

{}
inlib::args::args ( const args a_from) [inline]

Definition at line 44 of file args.

:m_args(a_from.m_args){}

Member Function Documentation

bool inlib::args::add ( const std::string &  a_key,
const std::string &  a_value,
bool  a_override = true 
) [inline]

Definition at line 109 of file args.

                                                                                  {
    if(a_override) {
      for(std::vector<arg>::iterator it = m_args.begin();
          it!=m_args.end();++it) {
        if((*it).first==a_key) {
          (*it).second = a_value;
          return true;
        }
      }
    }
    if(a_key.empty()) return false;
    m_args.push_back(arg(a_key,a_value)); 
    return true;
  }
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]

Definition at line 237 of file args.

                                                      {
    for(int index=0;index<a_argc;index++) delete [] a_argv[index];
    delete [] a_argv;
    a_argc = 0;
    a_argv = 0;
  }
void inlib::args::dump ( std::ostream &  a_out) const [inline]

Definition at line 291 of file args.

                                     {
    for(std::vector<arg>::const_iterator it = m_args.begin();
        it!=m_args.end();++it) {
      a_out << "key = " << sout((*it).first)
            << " value = " << sout((*it).second)
            << std::endl;
    }
  }
bool inlib::args::file ( std::string &  a_file) const [inline]

Definition at line 168 of file args.

                                     {
    std::string slast;
    std::string s;
    if((m_args.size()>1) //first arg is the program name !
       && last(slast,s) 
       && (slast.find('-')!=0) 
       && (s.empty()) ) {
      a_file = slast; //Last argument is not an option.
      return true;
    } else {
      a_file.clear();
      return false;
    }
  }
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]

Definition at line 61 of file args.

                                                             {
    for(std::vector<arg>::const_iterator it = m_args.begin();
        it!=m_args.end();++it) {
      if((*it).first==a_key) {
        a_value = (*it).second;
        return true;
      }
    }
    a_value.clear();
    return false;
  }
std::vector<std::string> inlib::args::find ( const std::string &  a_key) const [inline]

Definition at line 72 of file args.

                                                          {
    std::vector<std::string> vals;
    for(std::vector<arg>::const_iterator it = m_args.begin();
      it!=m_args.end();++it) {
      if((*it).first==a_key) vals.push_back((*it).second);
    }
    return vals;
  }
bool inlib::args::find ( const std::string &  a_string,
bool &  a_value 
) const [inline]

Definition at line 80 of file args.

                                                           {
    std::string s;
    if(!find(a_string,s)) {a_value = false;return false;}
    return to(s,a_value);
  }
template<class aT >
bool inlib::args::find ( const std::string &  a_string,
aT &  a_value 
) const [inline]

Definition at line 86 of file args.

                                                         {
    std::string s;
    if(!find(a_string,s)) {a_value = aT();return false;}
    return to<aT>(s,a_value);
  }
const std::vector<arg>& inlib::args::get_args ( ) const [inline]

Definition at line 50 of file args.

{return m_args;}
bool inlib::args::is_arg ( const std::string &  a_string) const [inline]

Definition at line 52 of file args.

                                               {
    for(std::vector<arg>::const_iterator it = m_args.begin();
        it!=m_args.end();++it) {
      if((*it).first==a_string) return true;
    }
    return false;
  }
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]

Definition at line 159 of file args.

                                                       {
    a_key.clear();
    a_value.clear();
    if(m_args.empty()) return false;
    a_key = m_args.back().first;
    a_value = m_args.back().second;
    return true;
  }
unsigned int inlib::args::number ( ) const [inline]

Definition at line 60 of file args.

{return m_args.size();} //back comp.
args& inlib::args::operator= ( const args a_from) [inline]

Definition at line 45 of file args.

                                     {
    m_args = a_from.m_args;
    return *this;
  }
int inlib::args::remove ( const std::string &  a_key) [inline]

Definition at line 145 of file args.

                                    {
    unsigned int nbeg = m_args.size();
    for(std::vector<arg>::iterator it = m_args.begin();it!=m_args.end();) {
      if(a_key==(*it).first) {
        it = m_args.erase(it);
      } else {
        ++it;
      }
    }
    return nbeg - m_args.size();
  }
void inlib::args::remove_first ( ) [inline]

Definition at line 157 of file args.

{m_args.erase(m_args.begin());}
unsigned int inlib::args::size ( ) const [inline]

Definition at line 59 of file args.

{return m_args.size();}
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;
  }

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines