/*
* AnyOption 1.3
*
* kishan at hackorama dot com www.hackorama.com JULY 2001
*
* + Acts as a common facade class for reading
* commandline options as well as options from
* an optionfile with delimited type value pairs
*
* + Handles the POSIX style single character options ( -w )
* as well as the newer GNU long options ( --width )
*
* + The option file assumes the traditional format of
* first character based comment lines and type value
* pairs with a delimiter , and flags which are not pairs
*
* # this is a coment
* # next line is an option value pair
* width : 100
* # next line is a flag
* noimages
*
* + Supports printing out Help and Usage
*
* + Why not just use getopt() ?
*
* getopt() Its a POSIX standard not part of ANSI-C.
* So it may not be available on platforms like Windows.
*
* + Why it is so long ?
*
* The actual code which does command line parsing
* and option file parsing are done in few methods.
* Most of the extra code are for providing a flexible
* common public interface to both a resourcefile and
* and command line supporting POSIX style and
* GNU long option as well as mixing of both.
*
* + Please see "anyoption.h" for public method descriptions
*
*/
/* Updated Auguest 2004
* Fix from Michael D Peters (mpeters at sandia.gov)
* to remove static local variables, allowing multiple instantiations
* of the reader (for using multiple configuration files). There is
* an error in the destructor when using multiple instances, so you
* cannot delete your objects (it will crash), but not calling the
* destructor only introduces a small memory leak, so I
* have not bothered tracking it down.
*
* Also updated to use modern C++ style headers, rather than
* depricated iostream.h (it was causing my compiler problems)
*/
/*
* Updated September 2006
* Fix from Boyan Asenov for a bug in mixing up option indexes
* leading to exception when mixing different options types
*/
#include "anyoption.h"
#include <string.h>
AnyOption::AnyOption()
{
init();
}
AnyOption::AnyOption(int maxopt)
{
init( maxopt , maxopt );
}
AnyOption::AnyOption(int maxopt, int maxcharopt)
{
init( maxopt , maxcharopt );
}
AnyOption::~AnyOption()
{
if( mem_allocated )
cleanup();
}
void
AnyOption::init()
{
init( DEFAULT_MAXOPTS , DEFAULT_MAXOPTS );
}
void
AnyOption::init(int maxopt, int maxcharopt )
{
max_options = maxopt;
max_char_options = maxcharopt;
max_usage_lines = DEFAULT_MAXUSAGE;
usage_lines = 0 ;
argc = 0;
argv = NULL;
posix_style = true;
verbose = false;
filename = NULL;
appname = NULL;
option_counter = 0;
optchar_counter = 0;
new_argv = NULL;
new_argc = 0 ;
max_legal_args = 0 ;
command_set = false;
file_set = false;
values = NULL;
g_value_counter = 0;
mem_allocated = false;
command_set = false;
file_set = false;
opt_prefix_char = '-';
file_delimiter_char = ':';
file_comment_char = '#';
equalsign = '=';
comment = '#' ;
delimiter = ':' ;
endofline = '\n';
whitespace = ' ' ;
nullterminate = '\0';
set = false;
once = true;
hasoptions = false;
autousage = false;
strcpy( long_opt_prefix , "--" );
if( alloc() == false ){
cout << endl << "OPTIONS ERROR : Failed allocating memory" ;
cout << endl ;
cout << "Exiting." << endl ;
exit (0);
}
}
bool
AnyOption::alloc()
{
int i = 0 ;
int size = 0 ;
if( mem_allocated )
return true;
size = (max_options+1) * sizeof(const char*);