*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / configuration_manager.cpp
index 4ded4f77a5ee36dfdfe1ed85597a53c79b8e5ddd..418beab22d3f4c69f7a2052d627621520cf9c836 100644 (file)
@@ -1,6 +1,8 @@
 #include "configuration_manager.hh"
 #include <stdio.h>
 
+#define MAX_LINE_LENGTH 80
+
 /*!
  *\brief Constructor.
  *
@@ -16,34 +18,78 @@ configuration_manager::configuration_manager(string  name){
 /*!
  *\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.
  *
@@ -51,9 +97,18 @@ void configuration_manager::add_option_value (string shortname, string longname,
  *      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));
 }
 
 
@@ -62,81 +117,134 @@ void configuration_manager::add_argument(string placeholder, string description,
  *\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);
+         *(option_switches[switch_no].status)=result;
+         found=true;
+       }
+      }
+
+      // 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;
+         *(option_values[value_no].target)=value;
+         found=true;
        }
-      } // 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){
+           *(option_switches[switch_no].status)=1;
+           short_found=true;
+           pos+=short_name.length()-1;
+         }
+       }
+       
+       // 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 {
+               *(option_values[value_no].status)=1;
+               *(option_values[value_no].target)=argstring.substr(pos+1);
+               pos=argstring.length(); // Do not analyse other characters.
+               short_found=true;
+             }
+           }
+         }
+       }
+       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 (!result.empty()){
-    result.insert(result.begin(),"Error!");
-    result.insert(result.begin(),"");
+    }
+  }
+  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.
+ */
+bool configuration_manager::analyse_bool(const string & data){
+  if ((data=="false")||(data=="0")||(data=="no")) return false;
+  return true;
+}
+
 /*!
  *\brief Generate help.
  *\arg target Reference to a vector<string> to which lots of helpful
@@ -145,19 +253,30 @@ vector<string> configuration_manager::read_args(int argc, char ** args){
 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(),' ');
     }
@@ -165,70 +284,117 @@ void configuration_manager::get_help(vector<string> & target){
   }
   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(),"");
+  }
 }
 
 /*!
@@ -244,22 +410,53 @@ vector<string> configuration_manager::get_help(){
 
 /**************************************************/
 
-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;
+
+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;
   if (status) *status=0;
 }
+
+
+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;
+  if (this->status) *(this->status)=0;
+}