#include "configuration_manager.hh"
#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#define MAX_LINE_LENGTH 80
/*!
*\brief Constructor.
/*!
*\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
+ * 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 to string to put the value in.
+ *\target Pointer to string to put the value in.
*\placeholder A placeholder for the documentation.
* For example <filename> in -f<filename>
*/
-void configuration_manager::add_option_value (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) option_values.insert(opt_v.end(),
- opt_value_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 "<input-file>".
*\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.
*
* argument reader.
* There would be no other way to determine the order.
*/
-void configuration_manager::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));
}
*\returns empty vector on success or the error messages to be output.
*/
vector<string> configuration_manager::read_args(int argc, char ** args){
- vector<string> result;
+
+ vector<string> messages;
vector<string> 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<string>::iterator akt_p=argv.begin()+1;akt_p<argv.end();akt_p++){ //Argument loop
+
+ // Loop through the command line arguments
+ for (unsigned int arg_no=0;arg_no<argv.size();arg_no++){
+ bool found=false;
+ string argstring;
- // Look for long argument
- if ((akt_p->substr(0,2)=="--")&&(free_parms_count==0)){
- int found=0;
- for (vector<opt_t>::iterator parm_p=opt_v.begin();parm_p<opt_v.end();parm_p++){
- if (akt_p->substr(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+1<argv.end()) { // If next word existend
- *(parm_p->status)=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_no<option_switches.size();switch_no++){
+ string switch_name=option_switches[switch_no].longname;
+ if (argstring.compare(0,switch_name.length(),switch_name,0,switch_name.length())==0){
+ string value=analyse_string(argstring);
+ bool result=analyse_bool(value);
+ 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;
+ }
+ }
+
+ // Look for long values.
+ for (unsigned int value_no=0;value_no<option_values.size();value_no++){
+ 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);
+ 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;
}
- } // search for loop
- if (!found) result.insert(result.end(),"Unknown parameter: "+*akt_p);
- } else { // No -- param, now look for switches
- if (((*akt_p)[0]=='-')&&(free_parms_count==0)){
- int stop_char_loop=0;
- for (unsigned int pos=1; pos<akt_p->length()&& !stop_char_loop ;pos++){
- int found=0;
- for (vector<opt_t>::iterator parm_p=opt_v.begin();parm_p<opt_v.end();parm_p++){
- if (parm_p->shortname[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+1<argv.end()) { // If next word existend
- *(parm_p->target)=*(++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_count<arg_v.size()){
- *(arg_v[free_parms_count].target)=*akt_p;
- *(arg_v[free_parms_count].status)=1;
- }
+ }
+ if (! found) {
+ messages.insert(messages.begin(),"Unknown option: --"+argstring+"!");
+ found=true; // Oh!
+ }
+ }
+
+ // Look for short parameters
+ if ((argv[arg_no].substr(0,1)=="-")&&(free_parms_count==0)&&(!found)){ // Short parameters
+ found=true;
+ argstring=argv[arg_no].substr(1); // Reassign, with one more character now
+ for (unsigned int pos=0;pos < argstring.length();pos++){
+ bool short_found=false;
+
+ // First, find short switches
+ for (unsigned int switch_no=0;switch_no<option_switches.size();switch_no++){
+ string short_name=option_switches[switch_no].shortname;
+ if (short_name.compare(0,short_name.length(),argstring,pos,short_name.length())==0){
+ if(option_switches[switch_no].allow_cmdline==true){
+ *(option_switches[switch_no].status)=1;
+ }else{
+ messages.insert(messages.end(),"Switch \"-"
+ +option_switches[switch_no].shortname
+ +"\" is not allowed on the command line!");
+ }
+ short_found=true;
+ pos+=short_name.length()-1;
+ break;
+ }
+ }
+
+ // Now, find short values
+ if (!short_found){
+ for (unsigned int value_no=0;value_no<option_values.size();value_no++){
+ string short_name=option_values[value_no].shortname;
+
+ if (short_name.compare(0,short_name.length(),argstring,pos,short_name.length())==0){
+ if (pos==argstring.length()-1){ // Last character, where's the value?
+ if (arg_no<argv.size()-1){ // Take next complete argument as value!
+ *(option_values[value_no].status)=1;
+ *(option_values[value_no].target)=argv[arg_no+1];
+ arg_no+=1; // Consume the next argument
+ short_found=true;
+ } else { // No argument left!
+ messages.insert(messages.begin(),"Missing value for -"+short_name+"!");
+ short_found=true;
+ }
+ } else {
+ 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;
+ }
+ }
+ }
+ }
+ if (!short_found) messages.insert(messages.begin(),"Unknown Option: -"+argstring.substr(pos,1)+"!");
+ }
+ }
+
+ if (!found) { // Must be a free form parameter...
+ if (free_parms_count<cmd_args.size()){ // Space available
+ *(cmd_args[free_parms_count].target)=argv[arg_no];
+ *(cmd_args[free_parms_count].status)=1;
free_parms_count++;
+ } else { // No more space for free form parameters!
+ messages.insert(messages.begin(),"Too many arguments!");
}
- } //looking for not -- args
- } // argv loop
- if (free_parms_count>arg_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<string> configuration_manager::read_file(string filename){
+ vector<string>result;
+ 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<option_switches.size();switch_no++){
+ string switch_name=option_switches[switch_no].longname;
+ if (argstring.compare(0,switch_name.length(),switch_name,0,switch_name.length())==0){
+ string value=analyse_string(argstring);
+ //result.insert(result.end(),"argstring:\""+argstring+"\"");
+ bool res=analyse_bool_false(value);
+ if(option_switches[switch_no].allow_conffile==true){
+ *(option_switches[switch_no].status)=res;
+ }else{
+ result.insert(result.end(),"In File "+filename+": Switch \""
+ +option_switches[switch_no].longname
+ +"\" is not allowed in config files!");
+ }
+ found=true;
+ break;
+ }
+ }
+
+ // Look for long values.
+ for (unsigned int value_no=0;value_no<option_values.size();value_no++){
+ 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;
+
+ if(option_values[value_no].allow_conffile==true){
+ *(option_values[value_no].status)=1;
+ *(option_values[value_no].target)=value;
+ }else{
+ result.insert(result.end(),"In File "+filename+": Option value \""
+ +option_values[value_no].longname
+ +"\" is not allowed in config files!");
+ }
+ found=true;
+ break;
+ }
+ }
+ if (! found) {
+ result.insert(result.end(),"In File "+filename+": Unknown option: "+argstring+"!");
+ }
+
+ } // if not #
+ } // if (strlen())
+ } // while(...)
+ fclose (fp);
+ } // fp==0 else branch
+
if (!result.empty()){
result.insert(result.begin(),"Error!");
result.insert(result.begin(),"");
return result;
}
+
/*!
*\brief Generate help.
*\arg target Reference to a vector<string> to which lots of helpful
void configuration_manager::get_help(vector<string> & target){
target.insert(target.end(),"");
string line="Usage: "+app_name;
- for (vector<opt_t>::iterator parm_p=opt_v.begin();parm_p<opt_v.end();parm_p++){
+
+ for (vector<opt_switch_t>::iterator parm_p=option_switches.begin();parm_p<option_switches.end();parm_p++){
string addstr=" [-"+parm_p->shortname;
- 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<arg_t>::iterator parm_p=arg_v.begin();parm_p<arg_v.end();parm_p++){
- if (line.length()+parm_p->placeholder.length()>79){
+
+ for (vector<opt_value_t>::iterator parm_p=option_values.begin();parm_p<option_values.end();parm_p++){
+ string addstr=" [-"+parm_p->shortname;
+ 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<cmd_arg_t>::iterator parm_p=cmd_args.begin();parm_p<cmd_args.end();parm_p++){
+ if (line.length()+parm_p->placeholder.length()>MAX_LINE_LENGTH){
target.insert(target.end(),line);
line=string(7+app_name.length(),' ');
}
}
target.insert(target.end(),line);
- /*******************************/
-
+ /* Here comes the documentation output. */
+
vector<string> left,right;
- for (vector<opt_t>::iterator parm_p=opt_v.begin();parm_p<opt_v.end();parm_p++){
- line=parm_p->description;
- 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<arg_t>::iterator parm_p=arg_v.begin();parm_p<arg_v.end();parm_p++){
- string st2;
- line=parm_p->description;
- 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<string>();
+ right=vector<string>();
+ max_width=0;
+
+ /* Now lets insert switches into left and right */
+ for (unsigned int sw=0; sw<option_switches.size();sw++){
+ opt_switch_t akt_sw=option_switches[sw];
+ string rline=akt_sw.description;
+ string lline="-"+akt_sw.shortname+", --"+akt_sw.longname;
+ left.insert(left.end(),lline);
+ right.insert(right.end(),rline);
+ }
+
+ // Determine maximal width for left column
+ max_width=0;
+ for (unsigned int c=0; c<left.size();c++)
+ if(left[c].length()>max_width) max_width=left[c].length();
+
+ /* output all the mess */
+ for (unsigned int c=0; c<left.size();c++){
+ string nl(max_width,' ');
+ nl.replace(0,left[c].length(),left[c]);
+ nl+=" "+right[c];
+ while (nl.length()>80){ // 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<string>();
+ right=vector<string>();
+ max_width=0;
+
+ for (unsigned int val=0; val<option_values.size();val++){
+ 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;
+ left.insert(left.end(),lline);
+ right.insert(right.end(),rline);
+ }
+
+ // Determine maximal width for left column
+ for (unsigned int c=0; c<left.size();c++)
+ if(left[c].length()>max_width) max_width=left[c].length();
+ /* output all the mess */
+ for (unsigned int c=0; c<left.size();c++){
+ string nl(max_width,' ');
+ nl.replace(0,left[c].length(),left[c]);
+ nl+=" "+right[c];
+ while (nl.length()>80){ // 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; c<opt_v.size();c++)
- if(left[c].length()>max_width) max_width=left[c].length();
- for (unsigned int c=0; c<opt_v.size();c++){
- string nl(max_width,' ');
- nl.replace(0,left[c].length(),left[c]);
- nl+=" "+right[c];
- while (nl.length()>80){ // 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(); c<opt_v.size()+arg_v.size();c++)
- if(left[c].length()>max_width) max_width=left[c].length();
- for (unsigned int c=opt_v.size(); c<opt_v.size()+arg_v.size();c++){
- string nl(max_width,' ');
- nl.replace(0,left[c].length(),left[c]);
- nl+=" "+right[c];
- while (nl.length()>80){ // 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<string>();
+ right=vector<string>();
+ max_width=0;
+
+ for (unsigned int arg=0; arg<cmd_args.size();arg++){
+ cmd_arg_t akt_arg=cmd_args[arg];
+ left.insert(left.end(),akt_arg.placeholder);
+ right.insert(right.end(),akt_arg.description);
+ }
+
+ // Determine maximal width for left column
+ max_width=0;
+ for (unsigned int c=0; c<left.size();c++)
+ if(left[c].length()>max_width) max_width=left[c].length();
+
+ for (unsigned int c=0; c<left.size();c++){
+ string nl(max_width,' ');
+ nl.replace(0,left[c].length(),left[c]);
+ nl+=" "+right[c];
+ while (nl.length()>MAX_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(),"");
+ }
}
/*!
/**************************************************/
-configuration_manager::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;
}
-configuration_manager::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;
}