X-Git-Url: http://gitweb.hachti.de/?a=blobdiff_plain;f=pc-tools%2Fldc2%2Fsrc%2Fconfiguration_manager.cpp;h=ea95bf81961ae056bcc18114b9a5d8a53e770dcd;hb=ea78fc91fca804d41e7caf94025a4616e4f2daad;hp=94a6adf20853a1ddc56bbe3a3da3d04f033e1da5;hpb=040533c5d1dece81aaaf2194b5ea24d908bea481;p=h316.git diff --git a/pc-tools/ldc2/src/configuration_manager.cpp b/pc-tools/ldc2/src/configuration_manager.cpp index 94a6adf..ea95bf8 100644 --- a/pc-tools/ldc2/src/configuration_manager.cpp +++ b/pc-tools/ldc2/src/configuration_manager.cpp @@ -1,51 +1,119 @@ -#include "argument_reader.hh" +/****************************************************************************** + * + * LDC2 source code + * + * $Date: 2007/03/26 04:05:37 $ + * $Author: hachti $ + * + * $Log: configuration_manager.cpp,v $ + * Revision 2.1 2007/03/26 04:05:37 hachti + * *** empty log message *** + * + * Revision 2.0 2007-03-26 01:00:38 hachti + * *** empty log message *** + * + * + ******************************************************************************/ + +#include "configuration_manager.hh" #include +#include +#include + +#define MAX_LINE_LENGTH 80 /*! *\brief Constructor. * - * This constructor makes a new argument_reader ready to use. + * This constructor makes a new configuration_manager ready to use. *\arg name Name of the application as mentioned in the * "Use: ..." help message. */ -argument_reader::argument_reader(string name){ +configuration_manager::configuration_manager(string name){ app_name=name; } /*! - *\brief Add a new parameter to be searched for. + *\brief Add a new configuration value to be searched for. *\param shortname A character for the short form. - * For example the 'h' in -h + * For example the o in -o *\param longname The double dash longname. For example - * "input_file=" in --input_file= or - * "ignore_errors" in --ignore--errors - *\param description A detailed parameter description. + * output_file in --output_file= + *\param description A detailed description of the value. *\param status Pointer to an integer. Will be set to 1 if arg found. - *\target Pointer to a value should be read. - * If no value is needed, pass NULL. - *\placeholder A placeholder for the documentation. - * For example "" in -f + *\param target Pointer to string to put the value in. + *\param placeholder A placeholder for the documentation. + * For example in -f + *\param allow_cmdline Specifies if this value may be specified on the + * command line. + *\param allow_conffile Specifies if this value may be specified in a + * configuration file. + * */ -void argument_reader::add_param (string shortname, string longname, - string description, - bool allow_cmdline, - bool allow_conffile, - int * status, - string * target, string placeholder){ +void configuration_manager::add_option_value (const string & shortname, + const string & longname, + const string & description, + int * status, + string * target, + const string & placeholder, + const bool & allow_cmdline, + const bool & allow_conffile + ){ - if(status!=NULL) opt_v.insert(opt_v.end(), - opt_t(shortname,longname, description, - status,target,placeholder) - ); + if(status!=NULL) if (target!=NULL) + option_values.insert(option_values.end(), + opt_value_t(shortname, + longname, + description, + status, + target, + placeholder, + allow_cmdline, + allow_conffile + ) + ); } + +/*! + *\brief Add a new configuration switch to be searched for. + *\param shortname A character for the short form. + * For example the h in -h + *\param longname The double dash longname. For example + * help in --help + *\param description A detailed description of the value. + *\param status Pointer to an integer. Will be set to 1 if arg found. + *\param allow_cmdline Specifies whether the switch is acceptable on the + * command line. + *\param allow_conffile Specifies whether the switch is acceptable + * in a configuration file. + */ +void configuration_manager::add_option_switch (const string & shortname, + const string & longname, + const string & description, + int * status, + const bool & allow_cmdline, + const bool & allow_conffile + ){ + + if(status!=NULL) + option_switches.insert(option_switches.end(), + opt_switch_t(shortname, + longname, + description, + status, + allow_cmdline, + allow_conffile) + ); +} + /*! *\brief Add an accepted argument to the argument reader. *\param placeholder Something like "". *\param description Text describing the argument. - *\param status A pointer to a status variable. Will be set to 0 immediately. + *\param status A pointer to a status variable. Will be set to 0 by default. * If the argument is filled in, it will be set to 1. *\param target A pointer to a c++ string where the argument goes to. * @@ -53,85 +121,276 @@ void argument_reader::add_param (string shortname, string longname, * argument reader. * There would be no other way to determine the order. */ -void argument_reader::add_argument(string placeholder, string description, int * status, string * target){ +void configuration_manager::add_argument(const string & description, + int * status, + string * target, + const string & placeholder){ + + + if (target!=NULL) if(status!=NULL) - arg_v.insert(arg_v.end(),arg_t(placeholder,description,status,target)); + cmd_args.insert(cmd_args.end(),cmd_arg_t(placeholder, + description, + status, + target)); } /*! - *\Read in the args passed to main(). + *\brief Read in the args passed to main(). *\returns empty vector on success or the error messages to be output. */ -vector argument_reader::read_args(int argc, char ** args){ - vector result; +vector configuration_manager::read_args(int argc, char ** args){ + + vector messages; vector argv; - for (char ** akt=args; *akt ;akt++) argv.insert(argv.end(),string(*akt)); + // First we make a C++ string vector out of the **argv. + for (char ** akt=args+1; *akt ;akt++) argv.insert(argv.end(),string(*akt)); + + // Counter for the free command line parameters. unsigned int free_parms_count=0; - for (vector::iterator akt_p=argv.begin()+1;akt_psubstr(0,2)=="--")&&(free_parms_count==0)){ - int found=0; - for (vector::iterator parm_p=opt_v.begin();parm_psubstr(2,parm_p->longname.length())==parm_p->longname){ - found=1; - *(parm_p->status)=1; - if (parm_p->target){ - if (akt_p->length()>2+parm_p->longname.length()){ - *(parm_p->target)=akt_p->substr(2+parm_p->longname.length()); - - } else // Word not long enough - if (akt_p+1status)=1; - *(parm_p->target)=*(++akt_p); - } else { // No next word :-( - result.insert(result.end(), - "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); - } - } // arg needed + // Look for long parameters. + if ((argv[arg_no].substr(0,2)=="--")&&(free_parms_count==0)){ + argstring=argv[arg_no].substr(2); + + // Look for long switches. + for (unsigned int switch_no=0;switch_nolength()&& !stop_char_loop ;pos++){ - int found=0; - for (vector::iterator parm_p=opt_v.begin();parm_pshortname[0]==(*akt_p)[pos]){ - found=1; - (*parm_p->status)=1; - if (parm_p->target){ // Need argument - if (akt_p->length()>pos+1){ - *(parm_p->target)=akt_p->substr(pos+1); - stop_char_loop=1; - } else { // Word not long enough - if (akt_p+1target)=*(++akt_p); - stop_char_loop=1; - } else { // No next word :-( - result.insert(result.end(), - "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); - } - } - } // arg needed - } //if match - } //args loop - if (!found) result.insert(result.end(),"Unknown switch: "+akt_p->substr(pos,1)); - } // char loop - }// switch found - else{ // no switch found - if (free_parms_countarg_v.size()) result.insert(result.begin(),"Too many arguments!"); + } + } + if (!messages.empty()){ + messages.insert(messages.begin(),"Error!"); + messages.insert(messages.begin(),""); + } + return messages; +} + + +/*! + *\brief Extract a value from a configuration file line or + * a command line argument + */ +string configuration_manager::analyse_string(const string & line){ + string result=""; + unsigned int pos=line.find("="); + if (pos!=string::npos) result=line.substr(pos+1); + return result; +} + +/*! + *\brief Extract a boolean value out of a string, defaulting to true + * + * Used for commandline switches + */ +bool configuration_manager::analyse_bool(const string & data){ + if ((data.find("false",0)!=string::npos)|| + (data.find("0",0)!=string::npos)|| + (data.find("no",0)!=string::npos)) return false; + else return true; +} + + +/*! + *\brief Extract a boolean value out of a string, defaulting to false + * + * Used for configuration file switches + */ +bool configuration_manager::analyse_bool_false(const string & data){ + if ((data.find("true",0)!=string::npos)|| + (data.find("1",0)!=string::npos)|| + (data.find("yes",0)!=string::npos)) return true; + else return false; +} + + +/*! + *brief Read in and parse a configuration file. + *\arg file String containing the filename + *\return Empty Vector or Vector full of strings containing + * the error message(s). + */ +vector configuration_manager::read_file(string filename){ + vectorresult; + FILE * fp=fopen(filename.c_str(),"r"); + if (! fp) { + result.insert(result.end(),"Could not open file: "+filename); + } else { + char buffer[1000]; + while(fgets(buffer,1000,fp)){ + if (buffer[strlen(buffer)-1]=='\n')// Cut trailing newline + buffer[strlen(buffer)-1]=0; + if (strlen (buffer)){ + char*l2=buffer+strspn(buffer," \t"); + if (l2[0]!='#'){ + string argstring=l2; + unsigned int pos=argstring.find("="); + if (pos==0){ + result.insert(result.end(),"In File "+filename+": Line beginning with '='!"); + } + + bool found=false; + + // Look for long switches. + for (unsigned int switch_no=0;switch_no argument_reader::read_args(int argc, char ** args){ return result; } + /*! *\brief Generate help. *\arg target Reference to a vector to which lots of helpful * strings are appended. */ -void argument_reader::get_help(vector & target){ +void configuration_manager::get_help(vector & target){ target.insert(target.end(),""); string line="Usage: "+app_name; - for (vector::iterator parm_p=opt_v.begin();parm_p::iterator parm_p=option_switches.begin();parm_pshortname; - if (parm_p->target!=0) addstr+=parm_p->placeholder; addstr+="]"; - if (line.length()+addstr.length()>79){ + if (line.length()+addstr.length()>MAX_LINE_LENGTH){ target.insert(target.end(),line); line=string(7+app_name.length(),' '); } line+=addstr; } - - for (vector::iterator parm_p=arg_v.begin();parm_pplaceholder.length()>79){ + + for (vector::iterator parm_p=option_values.begin();parm_pshortname; + addstr+=parm_p->placeholder; + addstr+="]"; + if (line.length()+addstr.length()>MAX_LINE_LENGTH){ + target.insert(target.end(),line); + line=string(7+app_name.length(),' '); + } + line+=addstr; + } + + for (vector::iterator parm_p=cmd_args.begin();parm_pplaceholder.length()>MAX_LINE_LENGTH){ target.insert(target.end(),line); line=string(7+app_name.length(),' '); } @@ -167,77 +438,124 @@ void argument_reader::get_help(vector & target){ } target.insert(target.end(),line); - /*******************************/ - + /* Here comes the documentation output. */ + vector left,right; - for (vector::iterator parm_p=opt_v.begin();parm_pdescription; - string st2=" -"+parm_p->shortname; - if (parm_p->target)st2+=" "+parm_p->placeholder; - st2+=", --"+parm_p->longname; - if (parm_p->target)st2+=parm_p->placeholder; - left.insert(left.end(),st2); - right.insert(right.end(),line); - } + unsigned int max_width; - for (vector::iterator parm_p=arg_v.begin();parm_pdescription; - st2+=" "+parm_p->placeholder; - left.insert(left.end(),st2); - right.insert(right.end(),line); + /* Output switches? */ + if (option_switches.size()){ + target.insert(target.end(),""); + target.insert(target.end(),"Switches:"); + + left=vector(); + right=vector(); + max_width=0; + + /* Now lets insert switches into left and right */ + for (unsigned int sw=0; swmax_width) max_width=left[c].length(); + + /* output all the mess */ + for (unsigned int c=0; c80){ // Too long??? + int limit=nl.find_last_of(' ',MAX_LINE_LENGTH+1); + target.insert(target.end(),nl.substr(0,limit)); + nl=string(max_width+2,' ')+nl.substr(limit+1); + } + target.insert(target.end(),nl); + } } - if (opt_v.size()){ + // Output values + if (option_values.size()){ target.insert(target.end(),""); - target.insert(target.end(),"Options:"); + target.insert(target.end(),"Option values:"); + + left=vector(); + right=vector(); + max_width=0; + + for (unsigned int val=0; val80){ // Too long??? + int limit=nl.find_last_of(' ',MAX_LINE_LENGTH+1); + target.insert(target.end(),nl.substr(0,limit)); + nl=string(max_width+2,' ')+nl.substr(limit+1); + } + target.insert(target.end(),nl); + } } - unsigned int max_width=0; - for (unsigned int c=0; cmax_width) max_width=left[c].length(); - for (unsigned int c=0; c80){ // Too long??? - int limit=nl.find_last_of(' ',80); - target.insert(target.end(),nl.substr(0,limit)); - nl=string(max_width+2,' ')+nl.substr(limit+1); - } - target.insert(target.end(),nl); - } - - if (arg_v.size()){ + /* Output the Arguments */ + if (cmd_args.size()){ target.insert(target.end(),""); target.insert(target.end(),"Arguments:"); - } - - max_width=0; - for (unsigned int c=opt_v.size(); cmax_width) max_width=left[c].length(); - for (unsigned int c=opt_v.size(); c80){ // Too long??? - int limit=nl.find_last_of(' ',80); - printf("limit:%i\n",limit); - target.insert(target.end(),nl.substr(0,limit)); - nl=string(max_width+2,' ')+nl.substr(limit+1); - } - - target.insert(target.end(),nl); - } + left=vector(); + right=vector(); + max_width=0; + + for (unsigned int arg=0; argmax_width) max_width=left[c].length(); + + for (unsigned int c=0; cMAX_LINE_LENGTH){ // Too long??? + int limit=nl.find_last_of(' ',MAX_LINE_LENGTH+1); + // printf("limit:%i\n",limit); + target.insert(target.end(),nl.substr(0,limit)); + nl=string(max_width+2,' ')+nl.substr(limit+1); + } + target.insert(target.end(),nl); + } target.insert(target.end(),""); + } } /*! *\brief Generate help. *\return A vector containing many helpful strings for the user. */ -vector argument_reader::get_help(){ +vector configuration_manager::get_help(){ vector result; get_help(result); return result; @@ -246,22 +564,51 @@ vector argument_reader::get_help(){ /**************************************************/ -argument_reader::opt_t::opt_t(string n_shortname, string n_longname,string n_description, int * n_status, - string * n_target, string n_placeholder){ - shortname=n_shortname; - longname=n_longname; - description=n_description; - status=n_status; - target=n_target; - placeholder=n_placeholder; + +configuration_manager::opt_value_t::opt_value_t(const string & shortname, + const string & longname, + const string & description, + int * status, + string * target, + const string & placeholder, + const bool & allow_conffile, + const bool & allow_cmdline + ){ + this->shortname=shortname; + this->longname=longname; + this->description=description; + this->status=status; + this->target=target; + this->placeholder=placeholder; + this->allow_conffile=allow_conffile; + this->allow_cmdline=allow_cmdline; if (status) *status=0; } -argument_reader::arg_t::arg_t( string n_placeholder, string n_description, - int * n_status, string * n_target){ - description=n_description; - status=n_status; - target=n_target; - placeholder=n_placeholder; - if (status) *status=0; + +configuration_manager::opt_switch_t::opt_switch_t(const string & shortname, + const string & longname, + const string & description, + int * status, + const bool & allow_conffile, + const bool & allow_cmdline){ + this->shortname=shortname; + this->longname=longname; + this->description=description; + this->status=status; + this->target=target; + this->placeholder=placeholder; + this->allow_conffile=allow_conffile; + this->allow_cmdline=allow_cmdline; +} + + +configuration_manager::cmd_arg_t::cmd_arg_t(const string & placeholder, + const string & description, + int * status, + string * target){ + this->placeholder=placeholder; + this->description=description; + this->status=status; + this->target=target; }