libassa 3.5.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes

ASSA::CmdLineOpts Class Reference

Class CmdLineOpts. More...

#include <CmdLineOpts.h>

Inheritance diagram for ASSA::CmdLineOpts:
ASSA::GenServer

List of all members.

Public Types

typedef void(* OPTS_FUNC )(void)
typedef void(* OPTS_FUNC_ONE )(const string &)
typedef vector< OptionOptionSet

Public Member Functions

 CmdLineOpts ()
 Default constructor.
virtual ~CmdLineOpts ()
 Do-nothing destructor.
bool add_flag_opt (const char c, const string &s, bool *f)
 Add binary flag option.
bool add_opt (const char c, const string &s, string *str)
 Add an option with STL string argument.
bool add_opt (const char c, const string &s, int *i)
 Add an option with integer argument.
bool add_opt (const char c, const string &s, unsigned int *ui)
 Add an option with unsigned integer argument.
bool add_opt (const char c, const string &s, long *l)
 Add an option with long argument.
bool add_opt (const char c, const string &s, unsigned long *ul)
 Add an option with unsigned long argument.
bool add_opt (const char c, const string &s, double *d)
 Add an option with double argument.
bool add_opt (const char c, const string &s, float *f)
 Add an option with float argument.
bool add_opt (const char c_, const string &s_, OPTS_FUNC f_)
 Add an option with static function argument.
bool add_opt (const char c_, const string &s_, OPTS_FUNC_ONE f_)
 Add an option with static function argument.
bool rm_opt (const char c_, const string &s_)
 Remove option for the option list.
bool parse_args (const char *argv[])
 Parse command line arguments based on installed options set.
int parse_config_file (IniFile &inifile_)
 Parse configuration parameters found in [options] section of the INI file.
const char * get_opt_error () const
 If previous call to one of member functions returned false, retrieve detailed error message.
void dump () const
 Write options set to the log file.

Static Public Member Functions

static void str_to_argv (const string &src_, int &argc_, char **&argv_)
 Static function.
static void free_argv (char **&argv_)
 Free up memory allocated by str_to_argv() function.

Protected Member Functions

bool is_valid (const char sopt_, const string &lopt_)
 Detect if supplied option is valid.
void set_error_none ()
 Reset error message to an empty string.
bool assign (Option *node_, const char *op_)
 Perform value assignment to the node.
Optionfind_option (const char *str_)
 Locate option in the options set.
Optionfind_option (const char letter_)
 Locate option in the options set.
virtual void pos_arg (const char *arg_)
 Process positional argument arg_.

Private Attributes

OptionSet m_opts_set
 Options set.
string m_error
 Last reported error.

Detailed Description

Class CmdLineOpts.

CmdLineOpts class parsers the command line arguments. It is a base class, and to use it, it has to be inherited from. See "ASSA Library User's Guide" for further details.

Definition at line 113 of file CmdLineOpts.h.


Member Typedef Documentation

Definition at line 119 of file CmdLineOpts.h.

typedef void(* ASSA::CmdLineOpts::OPTS_FUNC)(void)

Definition at line 116 of file CmdLineOpts.h.

typedef void(* ASSA::CmdLineOpts::OPTS_FUNC_ONE)(const string &)

Definition at line 117 of file CmdLineOpts.h.


Constructor & Destructor Documentation

ASSA::CmdLineOpts::CmdLineOpts ( ) [inline]

Default constructor.

Definition at line 295 of file CmdLineOpts.h.

References ASSA::CMDLINEOPTS, set_error_none(), and trace_with_mask.

                          : m_opts_set (), m_error ("")
{
    trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS);
    set_error_none ();
}
virtual ASSA::CmdLineOpts::~CmdLineOpts ( ) [inline, virtual]

Do-nothing destructor.

Definition at line 125 of file CmdLineOpts.h.

References ASSA::CMDLINEOPTS, and trace_with_mask.

                            { 
        trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS); 
    }

Member Function Documentation

bool CmdLineOpts::add_flag_opt ( const char  c,
const string &  s,
bool *  f 
)

Add binary flag option.

Parameters:
cshort name
slong name
fpointer to bool flag variable
Returns:
true on success, false on error

Definition at line 206 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, ASSA::Option::flag_t, is_valid(), m_opts_set, and trace_with_mask.

Referenced by ASSA::GenServer::GenServer().

{
    trace_with_mask ("CmdLineOpts::add_flag_opt", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) 
        return (false);

    Option o (sopt_, lopt_, Option::flag_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
string *  str 
)

Add an option with STL string argument.

Parameters:
cshort name
slong name
strpointer to string variable
Returns:
true on success, false on error

Definition at line 220 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, is_valid(), m_opts_set, ASSA::Option::string_t, and trace_with_mask.

Referenced by ASSA::GenServer::GenServer().

{
    trace_with_mask ("CmdLineOpts::add_opt(string*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) 
        return (false);

    Option o (sopt_, lopt_, Option::string_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
unsigned int *  ui 
)

Add an option with unsigned integer argument.

Parameters:
cshort name
slong name
uipointer to u_int variable
Returns:
true on success, false on error

Definition at line 248 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, is_valid(), m_opts_set, trace_with_mask, and ASSA::Option::uint_t.

{
    trace_with_mask ("CmdLineOpts::add_opt(u_int*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::uint_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
float *  f 
)

Add an option with float argument.

Parameters:
cshort name
slong name
fpointer to float variable
Returns:
true on success, false on error

Definition at line 304 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, ASSA::Option::float_t, is_valid(), m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(float*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::float_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c_,
const string &  s_,
OPTS_FUNC  f_ 
)

Add an option with static function argument.

This void function with no arguments will be called when command line option is processed. An option installed is treated as binary flag option.

Parameters:
c_short name
s_long name
f_pointer to the static function
Returns:
true on success, false on error

Definition at line 318 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, ASSA::Option::func_t, is_valid(), m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(OPTS_FUNC)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::func_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
long *  l 
)

Add an option with long argument.

Parameters:
cshort name
slong name
lpointer to long variable
Returns:
true on success, false on error

Definition at line 262 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, is_valid(), ASSA::Option::long_t, m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(long*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::long_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool ASSA::CmdLineOpts::add_opt ( const char  c_,
const string &  s_,
OPTS_FUNC_ONE  f_ 
)

Add an option with static function argument.

This void function with STL string arguments will be called when command line option is processed. The option value is delivered via function's argument.

Parameters:
c_short name
s_long name
f_pointer to the static function
Returns:
true on success, false on error
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
int *  i 
)

Add an option with integer argument.

Parameters:
cshort name
slong name
ipointer to int variable
Returns:
true on success, false on error

Definition at line 234 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, ASSA::Option::int_t, is_valid(), m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(int*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::int_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
unsigned long *  ul 
)

Add an option with unsigned long argument.

Parameters:
cshort name
slong name
ulpointer to unsigned long variable
Returns:
true on success, false on error

Definition at line 276 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, is_valid(), ASSA::Option::long_t, m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(u_long*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::long_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
double *  d 
)

Add an option with double argument.

Parameters:
cshort name
slong name
dpointer to double variable
Returns:
true on success, false on error

Definition at line 290 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, ASSA::Option::double_t, is_valid(), m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::add_opt(double*)", CMDLINEOPTS);

    if (!is_valid (sopt_, lopt_)) {
        return (false);
    }
    Option o (sopt_, lopt_, Option::double_t, (void*) v_);
    m_opts_set.push_back (o);
    return (true);
}
bool CmdLineOpts::assign ( Option node_,
const char *  op_ 
) [protected]

Perform value assignment to the node.

Data conversion happens here.

Definition at line 536 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, DL, ASSA::Option::double_t, ASSA::Option::flag_t, ASSA::Option::float_t, ASSA::Option::func_one_t, ASSA::Option::func_t, ASSA::Option::int_t, ASSA::Option::long_t, m_error, ASSA::Option::m_long_name, ASSA::Option::m_short_name, ASSA::Option::m_type, ASSA::Option::m_val, ASSA::Option::none_t, ASSA::Option::string_t, trace_with_mask, ASSA::Option::type_c_str(), ASSA::Option::uint_t, and ASSA::Option::ulong_t.

Referenced by parse_args(), and parse_config_file().

{
    trace_with_mask ("CmdLineOpts::assign", CMDLINEOPTS);

    long l;
    double d;

    if (node_ && op_) {
        DL ((CMDLINEOPTS, "Assign '%s' to {-%c, --%s, t=%s}\n", 
             op_, node_->m_short_name, node_->m_long_name.c_str (),
             node_->type_c_str ()));
    }

    /*---
      From strtol(3C) man page:

      "Because 0 is returned on error and is also a valid return on
      success, an application wishing to check for error situations 
      should set 'errno' to 0, then call strtol(3C), then check 'errno'
      and if it is non-zero, assume an error has occured."
      ---*/
      
    switch (node_->m_type) {
    case Option::string_t:
        *(string*) node_->m_val = op_;
        break;

    case Option::int_t:
    case Option::long_t:
        errno = 0;
        l = strtol (op_, NULL, 0);

        if (errno != 0) {
            m_error = "Error: '" + string (strerror (errno)) + "',";
            m_error += " in converting to integer from '";
            m_error += string (op_) + "'.";
            return (false);
        }

        if (node_->m_type == Option::int_t) {
            *(int*) node_->m_val = int (l);
        }
        else {
            *(long*) node_->m_val = l;
        }
        break;

    case Option::uint_t:
    case Option::ulong_t:
        errno = 0;
        l = strtol (op_, NULL, 0);

        if (errno != 0) {
            m_error = "Error: '" + string (strerror (errno)) + "',";
            m_error += " in converting to unsinged integer from '";
            m_error += string (op_) + "'.";
            return (false);
        }

        if (node_->m_type == Option::uint_t) {
            *(unsigned int*) node_->m_val = int (l);
        }
        else {
            *(unsigned long*) node_->m_val = l;
        }
        break;

    case Option::double_t:
    case Option::float_t:
        errno = 0;
        d = strtod (op_, NULL);

        if (errno != 0) {
            m_error = "Error: '" + string (strerror (errno)) + "',";
            m_error += " in converting to double/float from '";
            m_error += string (op_) + "'.";
            return (false);
        }

        if (node_->m_type == Option::double_t) {
            *(double*) node_->m_val = d;
        }
        else {
            *(float*) node_->m_val = float (d);
        }
        break;

    case Option::flag_t:
        *(bool*) node_->m_val = true; // no more flipping!
        break;
        
    case Option::func_t:
        (*(OPTS_FUNC)(node_->m_val)) ();
        break;

    case Option::func_one_t:
        (*(OPTS_FUNC_ONE)(node_->m_val)) (op_);
        break;

    case Option::none_t:
    default:
        m_error = "Undefined type for option '"+string (op_)+"'.";
        return (false);
    } /*-- switch () --*/

    return (true);
}
void CmdLineOpts::dump ( ) const

Write options set to the log file.

Definition at line 646 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, DL, m_error, and m_opts_set.

Referenced by ASSA::GenServer::init_internals().

{
    OptionSet::const_iterator i;

    for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
        i->dump ();
    }

    if (!m_error.empty ()) {
        DL((CMDLINEOPTS, "Last error: '%s'\n", m_error.c_str ()));
    }
}
Option * CmdLineOpts::find_option ( const char *  str_) [protected]

Locate option in the options set.

Definition at line 173 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

Referenced by parse_args().

{
    trace_with_mask ("CmdLineOpts::find_option(char*)", CMDLINEOPTS);

    OptionSet::iterator i;

    for ( i = m_opts_set.begin (); i != m_opts_set.end (); i++) 
    {
        if (i->m_long_name == str_) {
            return &(*i);
        }
    }
    return (NULL);
}
Option * CmdLineOpts::find_option ( const char  letter_) [protected]

Locate option in the options set.

Definition at line 190 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::find_option(char)", CMDLINEOPTS);

    OptionSet::iterator i;

    for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) 
    {
        if (i->m_short_name == letter_) 
            return &(*i);
    }
    return (NULL);
}
void CmdLineOpts::free_argv ( char **&  argv_) [static]

Free up memory allocated by str_to_argv() function.

Definition at line 693 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::free_argv", CMDLINEOPTS);

    /* If argument is empty (which should never be the case),
     * then freeing the memory would core dump application.
     */
    if (argv_ == NULL) {
        return;
    }

    for (int i = 0; argv_[i]; i++) {
        delete [] argv_[i];
    }
    delete [] argv_;
    argv_ = NULL;
}
const char * ASSA::CmdLineOpts::get_opt_error ( ) const [inline]

If previous call to one of member functions returned false, retrieve detailed error message.

Definition at line 309 of file CmdLineOpts.h.

References m_error.

Referenced by ASSA::GenServer::init().

{
    return (m_error.c_str ());
}
bool CmdLineOpts::is_valid ( const char  sopt_,
const string &  lopt_ 
) [protected]

Detect if supplied option is valid.

Definition at line 145 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, m_error, m_opts_set, set_error_none(), and trace_with_mask.

Referenced by add_flag_opt(), and add_opt().

{
    trace_with_mask ("CmdLineOpts::is_valid", CMDLINEOPTS);

    set_error_none ();
    OptionSet::const_iterator i;

    for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
        if (sopt_ == '\0' && lopt_.empty ()) {
            m_error = "Ignore empty option";
            return (false);
        }
        else if (sopt_ != '\0' && i->m_short_name == sopt_) {
            m_error = "Ignored multiple option '-";
            m_error += sopt_ + string ("'");
            return (false);
        }
        else if (!lopt_.empty () && i->m_long_name == lopt_) {
            m_error = "Ignore multiple option '--";
            m_error += lopt_ + string ("'");
            return (false);
        }
    }
    return (true);
}
bool CmdLineOpts::parse_args ( const char *  argv[])

Parse command line arguments based on installed options set.

Returns:
true on success, false on error.

Definition at line 365 of file CmdLineOpts.cpp.

References assign(), ASSA::CMDLINEOPTS, DL, find_option(), ASSA::Option::flag_t, ASSA::Option::func_t, m_error, ASSA::Option::m_type, pos_arg(), set_error_none(), and trace_with_mask.

Referenced by ASSA::GenServer::init().

{
    trace_with_mask ("CmdLineOpts::parse_args", CMDLINEOPTS);

    register int skip = 1;
    bool pos_args_started = false;
    string param ("");
    string token ("");
    set_error_none ();
    Option* node = (Option*) NULL;

    for (argv_++; argv_[0]; argv_ += skip) {
        if (skip != 0) {
            token = argv_[0];
        }

        DL((CMDLINEOPTS, "token: \"%s\"\n", token.c_str()));

        if (pos_args_started) {
            DL((CMDLINEOPTS,"pos_args_started = true\n"));

            if (token[0] == '-' && token.size () != 1) {
                m_error = "Invalid order of arguments: '";
                m_error += token + "'.";
                goto done;
            }
            pos_arg (token.c_str ());
            continue;
        }
        skip = 1;

        if (token[0] == '-' && token.size () > 1 && token[1] != '-') {
            if (token.size () == 1 && !pos_args_started) {
                pos_arg (token.c_str ());
                pos_args_started = true;
                continue;
            }
                
            if ((node = find_option (token[1])) != NULL) {
                if (token.size () > 2) {
                    if (node->m_type == Option::flag_t ||
                        node->m_type == Option::func_t) 
                    {
                        token.erase (1, 1);
                        skip = 0;
                    }
                    else {
                        param = token.substr (2);
                    } 
                } // if (token.size()>2)
            } // if ((node = find_option ())
        } 
        else {  
            if (token.size () > 1 && token[1] == '-') {
                string op = token.substr (2);
                size_t pos;

                if ((pos = op.find ("=")) != (size_t)-1) {
                    param = op.substr (pos+1, op.length ());
                    op.replace (pos, op.length() - pos, "");
                }
                node = find_option (op.c_str ());
            }
            else {  
                pos_arg (token.c_str ());
                pos_args_started = true;
                continue;
            } 
        } // if (token[0] == '-' && token[1] != '-') 

        if (!node) {
            m_error = "Invalid option '" + token + "'.";
            goto done;
        }

        if (node->m_type != Option::flag_t &&
            node->m_type != Option::func_t) 
        {
            if (param.empty ()) {
                if (!argv_[1]) {
                    m_error = "Expecting parameter after '"
                        + string (argv_[0]) + "'.";
                    goto done;
                }
                param = argv_[1];
                skip = 2;
            }
        }
        /*--- 
         * if positional arguments only 
         ---*/
        if (!node) {
            goto done;
        }

        if (param.empty ()) {
            if (!assign (node, argv_[1])) {
                return (false);
            }
        }
        else {
            const char* str = param.c_str ();
            if (!assign (node, str)) {
                return (false);
            }
            param = "";
        }
    } // for (argv_++; argv_[0]; argv_ += skip) 

 done:
    return !m_error.empty () ? false : true;
}
int CmdLineOpts::parse_config_file ( IniFile inifile_)

Parse configuration parameters found in [options] section of the INI file.

An Option object keeps long names the way they were assigned by the add_opt() call (i.e.

File should be already loaded with load().

Parameters:
inifile_The INI file to parse.
Returns:
The number of options parsed or -1 if [options] section is missing

"mac-address"). IniFile has same options in their normalized format (i.e. "mac_address").

Check to see if [options] section is present. Try all possible matches of the section name.

Iterate throught the set of all registered Options. For each in the set, normalize its name and lookup in IniFile's [option] section for the value.

Definition at line 492 of file CmdLineOpts.cpp.

References assign(), ASSA::CMDLINEOPTS, DL, ASSA::Utils::find_and_replace_char(), ASSA::IniFile::find_section(), ASSA::IniFile::get_value(), m_error, m_opts_set, ASSA::IniFile::sect_end(), and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::parse_config_file", CMDLINEOPTS);

    unsigned int count = 0;
    string v;
    string s;
    string optsect_name ("options");
    OptionSet::iterator pos = m_opts_set.begin ();

    if (inifile_.find_section (optsect_name) == inifile_.sect_end ()) 
    {
        optsect_name = "Options";
        if (inifile_.find_section (optsect_name) == inifile_.sect_end ()) 
        {
            optsect_name = "OPTIONS";
            if (inifile_.find_section (optsect_name) == inifile_.sect_end ()) 
            {
                m_error = "Missing [options] section in INI file!";
                return -1;
            }
        }
    }

    while (pos != m_opts_set.end ()) {
        if (pos->m_long_name.size ()) {
            s = pos->m_long_name;
            ASSA::Utils::find_and_replace_char (s, '-', '_');
            DL ((CMDLINEOPTS, "trying option \"%s\"\n", s.c_str ()));
            v = inifile_.get_value (optsect_name, s);
            if (v.size ()) {
                if (assign (&(*pos), v.c_str ())) {
                    count++;
                }
            }
        }
        pos++;
    }

    return (count);
}
void ASSA::CmdLineOpts::pos_arg ( const char *  arg_) [inline, protected, virtual]

Process positional argument arg_.

This method must be overloaded by the derived class to take advantage of it.

Parameters:
arg_positional argument value

Definition at line 292 of file CmdLineOpts.h.

Referenced by parse_args().

{ /* no-opt*/ }
bool CmdLineOpts::rm_opt ( const char  c_,
const string &  s_ 
)

Remove option for the option list.

Parameters:
c_short name
s_long name
Returns:
true if found, false if not

Definition at line 346 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

{
    trace_with_mask ("CmdLineOpts::rm_opt(string&)", CMDLINEOPTS);

    OptionSet::iterator i;

    for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) 
    {
        if (i->m_short_name == sopt_ || i->m_long_name == lopt_) 
        {
            m_opts_set.erase (i);
            return (true);
        }
    }
    return (false);
}
void ASSA::CmdLineOpts::set_error_none ( ) [inline, protected]

Reset error message to an empty string.

Definition at line 302 of file CmdLineOpts.h.

References ASSA::CMDLINEOPTS, m_error, and trace_with_mask.

Referenced by CmdLineOpts(), is_valid(), and parse_args().

{
    trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
    m_error = "";
}
void CmdLineOpts::str_to_argv ( const string &  src_,
int &  argc_,
char **&  argv_ 
) [static]

Static function.

Convert string list of command line options into dynamically allocated argv-like array. The array is terminated with NULL. This memory must be freed after it has been used. Remember that the first parameter is process name.

Parameters:
src_command line option string
argc_number of options found in the source string
argv_returns a pointer to the heap-allocated memory

Definition at line 661 of file CmdLineOpts.cpp.

References ASSA::CMDLINEOPTS, and trace_with_mask.

Referenced by ASSA::Fork::fork_exec().

{
    trace_with_mask ("CmdLineOpts::str_to_argv", CMDLINEOPTS);

    std::vector<string> vs;
    std::istringstream input (src_);
    std::string token;

    while (input >> token) {
        vs.push_back (token);
        token = "";
    }
    int i = 0;
    char* p;

    if (vs.size ()) {
        argv_ = new char* [vs.size() + 1];
        std::vector<string>::iterator it;

        for (it = vs.begin (); it != vs.end (); it++, i++) {
            p = new char [it->size() + 1];
            strcpy (p, it->c_str ());
            p[it->size()] = '\0';
            argv_[i] = p;
        }
        argv_[i] = NULL;
    }
    argc_ = i;
}

Member Data Documentation

string ASSA::CmdLineOpts::m_error [private]

Last reported error.

Definition at line 287 of file CmdLineOpts.h.

Referenced by assign(), dump(), get_opt_error(), is_valid(), parse_args(), parse_config_file(), and set_error_none().

Options set.

Definition at line 284 of file CmdLineOpts.h.

Referenced by add_flag_opt(), add_opt(), dump(), find_option(), is_valid(), parse_config_file(), and rm_opt().


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