X-Git-Url: http://gitweb.hachti.de/?a=blobdiff_plain;f=pc-tools%2Fldc2%2Fsrc%2Fconfiguration_manager.cpp;h=3ca21020a70c0f237d502c686e200e76da15702d;hb=16b4d690e1a97837a883abe60d30b5cd032be935;hp=418beab22d3f4c69f7a2052d627621520cf9c836;hpb=909d36034504f2511a5bc4ef7e50d407964e247a;p=h316.git diff --git a/pc-tools/ldc2/src/configuration_manager.cpp b/pc-tools/ldc2/src/configuration_manager.cpp index 418beab..3ca2102 100644 --- a/pc-tools/ldc2/src/configuration_manager.cpp +++ b/pc-tools/ldc2/src/configuration_manager.cpp @@ -1,5 +1,7 @@ #include "configuration_manager.hh" #include +#include +#include #define MAX_LINE_LENGTH 80 @@ -23,8 +25,8 @@ configuration_manager::configuration_manager(string name){ * 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 string to put the value in. - *\placeholder A placeholder for the documentation. + *\param target Pointer to string to put the value in. + *\param placeholder A placeholder for the documentation. * For example in -f */ void configuration_manager::add_option_value (const string & shortname, @@ -142,8 +144,15 @@ vector configuration_manager::read_args(int argc, char ** args){ if (argstring.compare(0,switch_name.length(),switch_name,0,switch_name.length())==0){ string value=analyse_string(argstring); bool result=analyse_bool(value); - *(option_switches[switch_no].status)=result; + if(option_switches[switch_no].allow_cmdline==true){ + *(option_switches[switch_no].status)=result; + }else{ + messages.insert(messages.end(),"Switch \"--" + +option_switches[switch_no].longname + +"\" is not allowed on the command line!"); + } found=true; + break; } } @@ -152,9 +161,16 @@ vector configuration_manager::read_args(int argc, char ** args){ string value_name=option_values[value_no].longname; if (argstring.compare(0,value_name.length(),value_name,0,value_name.length())==0){ string value=analyse_string(argstring); - *(option_values[value_no].status)=1; - *(option_values[value_no].target)=value; + if(option_values[value_no].allow_cmdline==true){ + *(option_values[value_no].status)=1; + *(option_values[value_no].target)=value; + }else{ + messages.insert(messages.end(),"Option value \"--" + +option_values[value_no].longname + +"\" is not allowed on the command line!"); + } found=true; + break; } } if (! found) { @@ -174,9 +190,16 @@ vector configuration_manager::read_args(int argc, char ** args){ for (unsigned int switch_no=0;switch_no configuration_manager::read_args(int argc, char ** args){ short_found=true; } } else { - *(option_values[value_no].status)=1; - *(option_values[value_no].target)=argstring.substr(pos+1); + if(option_values[value_no].allow_cmdline==true){ + *(option_values[value_no].status)=1; + *(option_values[value_no].target)=argstring.substr(pos+1); + }else{ + messages.insert(messages.end(),"Option value \"-" + +option_values[value_no].shortname + +"\" is not allowed on the command line!"); + } pos=argstring.length(); // Do not analyse other characters. short_found=true; + break; } } } @@ -225,6 +255,7 @@ vector configuration_manager::read_args(int argc, char ** args){ } return messages; } + /*! *\brief Extract a value from a configuration file line or @@ -238,13 +269,114 @@ string configuration_manager::analyse_string(const string & line){ } /*! - *\brief Extract a boolean value out of a string. + *\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=="false")||(data=="0")||(data=="no")) return false; - return true; + 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 to which lots of helpful @@ -339,7 +471,7 @@ void configuration_manager::get_help(vector & target){ opt_value_t akt_val=option_values[val]; string rline=akt_val.description; string lline=" -"+akt_val.shortname+akt_val.placeholder+", --"+ - akt_val.longname+akt_val.placeholder; + akt_val.longname+"="+akt_val.placeholder; left.insert(left.end(),lline); right.insert(right.end(),rline); } @@ -446,7 +578,6 @@ configuration_manager::opt_switch_t::opt_switch_t(const string & shortname, this->placeholder=placeholder; this->allow_conffile=allow_conffile; this->allow_cmdline=allow_cmdline; - if (status) *status=0; } @@ -458,5 +589,4 @@ configuration_manager::cmd_arg_t::cmd_arg_t(const string & placeholder, this->description=description; this->status=status; this->target=target; - if (this->status) *(this->status)=0; }