#include "configuration_manager.hh"
#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
#define MAX_LINE_LENGTH 80
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;
}
}
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) {
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){
- *(option_switches[switch_no].status)=1;
+ 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;
}
}
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;
}
}
}
}
return messages;
}
+
/*!
*\brief Extract a value from a configuration file line or
}
/*!
- *\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<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
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);
}
this->placeholder=placeholder;
this->allow_conffile=allow_conffile;
this->allow_cmdline=allow_cmdline;
- if (status) *status=0;
}
this->description=description;
this->status=status;
this->target=target;
- if (this->status) *(this->status)=0;
}