*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / configuration_manager.cpp
index 418410508fa72074f42968ebf32c85fa3d78a851..98a377822a76a3116cf9cef65675bf3254ecfc38 100644 (file)
@@ -1,3 +1,24 @@
+/******************************************************************************
+ * 
+ * LDC2 source code
+ *
+ * $Date: 2007/06/15 12:46:04 $
+ * $Author: hachti $
+ *
+ * $Log: configuration_manager.cpp,v $
+ * Revision 2.2  2007/06/15 12:46:04  hachti
+ * Some small changes in configuration_manager output - added (c/f) info.
+ * Changed order in tool.hh:add_unique....
+ *
+ * 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 <stdio.h>
 #include <stdlib.h>
@@ -25,9 +46,14 @@ 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 <filename> in -f<filename>
+ *\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 configuration_manager::add_option_value (const string & shortname, 
                                              const string & longname,
@@ -115,7 +141,7 @@ void configuration_manager::add_argument(const string & description,
 
 
 /*!
- *\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<string> configuration_manager::read_args(int argc, char ** args){
@@ -135,7 +161,8 @@ vector<string> configuration_manager::read_args(int argc, char ** args){
     string argstring;
     
     // Look for long parameters.
-    if ((argv[arg_no].substr(0,2)=="--")&&(free_parms_count==0)){
+    //    if ((argv[arg_no].substr(0,2)=="--")&&(free_parms_count==0)){
+    if ((argv[arg_no].substr(0,2)=="--")){ // Changed for comfort.
       argstring=argv[arg_no].substr(2);
 
       // Look for long switches.
@@ -144,8 +171,15 @@ vector<string> 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;
        }
       }
 
@@ -154,9 +188,16 @@ vector<string> 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) {
@@ -166,7 +207,8 @@ vector<string> configuration_manager::read_args(int argc, char ** args){
     }
 
     // Look for short parameters
-    if ((argv[arg_no].substr(0,1)=="-")&&(free_parms_count==0)&&(!found)){ // Short parameters
+    // if ((argv[arg_no].substr(0,1)=="-")&&(free_parms_count==0)&&(!found)){ // Short parameters
+    if ((argv[arg_no].substr(0,1)=="-")&&(!found)){                        // Changed for comfort.
       found=true;
       argstring=argv[arg_no].substr(1); // Reassign, with one more character now
       for (unsigned int pos=0;pos < argstring.length();pos++){
@@ -176,9 +218,16 @@ vector<string> configuration_manager::read_args(int argc, char ** args){
        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;
          }
        }
        
@@ -199,10 +248,17 @@ vector<string> 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;
              }
            }
          }
@@ -241,13 +297,31 @@ 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
@@ -258,35 +332,79 @@ vector<string> configuration_manager::read_file(string filename){
   vector<string>result;
   FILE * fp=fopen(filename.c_str(),"r");
   if (! fp) {
-    result.insert(result.end(),"Error!");
-    result.insert(result.end(),"Could not open file:"+filename);
-    return result;
-  }
-
-  char buffer[1000];
-  while(fgets(buffer,1000,fp)){
-    if (strlen (buffer)){
-      char*l2=buffer+strspn(buffer," \t");
-      if (l2[0]!='#'){
-       string line=l2;
-       unsigned int pos=line.find("=");
-       if (pos==0){
-         result.insert(result.end(),"Error!");
-         result.insert(result.end(),"In File:"+filename);
-         result.insert(result.end(),"Line beginning with '='!");
-         return result;
-       }
-       
-      } // if not #
-      
-    } // if (strlen())
-  } // while(...)
-
+    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
   
-  fclose (fp);
+  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
@@ -343,7 +461,12 @@ void configuration_manager::get_help(vector<string> & target){
     /* 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 rline="(" ;
+      if (akt_sw.allow_cmdline) rline+="c/";
+      else rline+="-/";
+      if (akt_sw.allow_conffile) rline+="f)";
+      else rline+="-)";
+      rline+=" "+akt_sw.description;
       string lline="-"+akt_sw.shortname+", --"+akt_sw.longname;
       left.insert(left.end(),lline);
       right.insert(right.end(),rline);
@@ -359,10 +482,10 @@ void configuration_manager::get_help(vector<string> & target){
       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);
+      while (nl.length()>MAX_LINE_LENGTH){ // Too long???
+       int limit=nl.find_last_of(' ',MAX_LINE_LENGTH);
        target.insert(target.end(),nl.substr(0,limit));
-       nl=string(max_width+2,' ')+nl.substr(limit+1);
+       nl=string(max_width+2+6,' ')+nl.substr(limit+1);
       } 
       target.insert(target.end(),nl);
     }
@@ -379,9 +502,15 @@ void configuration_manager::get_help(vector<string> & target){
     
     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;
+      string rline="(" ;
+      if (akt_val.allow_cmdline) rline+="c/";
+      else rline+="-/";
+      if (akt_val.allow_conffile) rline+="f)";
+      else rline+="-)";
+      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);
     }
@@ -391,18 +520,24 @@ void configuration_manager::get_help(vector<string> & target){
       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);
+      string nl(max_width,' ');               // Empty left side.
+      nl.replace(0,left[c].length(),left[c]); // Print in left side.
+      nl+="  "+right[c];                      // Add right side.
+
+      while (nl.length()>MAX_LINE_LENGTH){ // Too long???
+       int limit=nl.find_last_of(' ',MAX_LINE_LENGTH);
        target.insert(target.end(),nl.substr(0,limit));
-       nl=string(max_width+2,' ')+nl.substr(limit+1);
+       nl=string(max_width+2+6,' ')+nl.substr(limit+1);
       } 
       target.insert(target.end(),nl);
     }
   }
 
+  target.insert(target.end(),"");
+  target.insert(target.end(),"Legend (c/f):");
+  target.insert(target.end(),"              c: Allowed on command line.");
+  target.insert(target.end(),"              f: Allowed in configuration file.");
+  
   /* Output the Arguments */
   if (cmd_args.size()){
     target.insert(target.end(),"");
@@ -429,7 +564,6 @@ void configuration_manager::get_help(vector<string> & target){
       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);
       } 
@@ -488,7 +622,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;
 }
 
 
@@ -500,5 +633,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;
 }