Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1535 for code/trunk/src/util


Ignore:
Timestamp:
Jun 4, 2008, 8:54:43 PM (16 years ago)
Author:
rgrieder
Message:

merged input branch back to trunk

Location:
code/trunk/src/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/util/ArgReader.cc

    r1505 r1535  
    2121 *
    2222 *   Author:
    23  *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
     23 *      Reto Grieder
    2424 *   Co-authors:
    25  *      ...
     25 *      Benjamin Knecht <beni_at_orxonox.net>
    2626 *
    2727 */
     
    3333
    3434#include "ArgReader.h"
     35#include "SubString.h"
    3536
    36 #include <iostream>
     37ArgReader::ArgReader(int argc, char **argv)
     38{
     39  failure_ = false;
     40  errorString_ = "";
     41  CmdLineArg arg;
    3742
    38 ArgReader::ArgReader(int argc, char** argv)
    39 {
    40   counter_ = argc;
    41   arguments_ = argv;
    42   fail_ = false;
    43   errorStr_ = "";
    44 }
     43  int i = 1;
     44  while (i < argc)
     45  {
     46    if (argv[i][0] == '-' && argv[i][1] == '-') // name
     47    {
     48      if (argv[i][2] == '\0')
     49      {
     50        failure_ = true;
     51        errorString_ += "Expected word after \"--\".\n";
     52      }
     53      arg.bChecked_ = false;
     54      arg.name_ = argv[i] + 2;
     55      arg.value_ = "";
     56      arguments_.push_back(arg);
     57    }
     58    else // value
     59    {
     60      if (arguments_.size() == 0)
     61      {
     62        failure_ = true;
     63        errorString_ += "Expected \"--\" in command line arguments.\n";
     64        arg.bChecked_ = false;
     65        arg.name_ = "";
     66        arg.value_ = "";
     67        arguments_.push_back(arg);
     68      }
    4569
    46 void ArgReader::checkArgument(std::string option, std::string &string, bool must)
    47 {
    48   int argpos = checkOption(option) + 1;
    49   if(argpos != 0)
    50   {
    51     string = arguments_[argpos];
     70      if (arguments_.back().value_ != "")
     71        arguments_.back().value_ += " " + std::string(argv[i]);
     72      else
     73        arguments_.back().value_ = argv[i];
     74    }
     75    ++i;
    5276  }
    53   else
    54   {
    55     if(must) {
    56       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";
    57       fail_ = true;
    58     }
    59   }
    60 
    61 }
    62 
    63 void ArgReader::checkArgument(std::string option, int &integer, bool must)
    64 {
    65   int argpos = checkOption(option) + 1;
    66   if(argpos != 0)
    67   {
    68     integer = atoi(arguments_[argpos]);
    69   }
    70   else
    71   {
    72     if(must) {
    73       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";
    74       fail_ = true;
    75     }
    76   }
    77 }
    78 
    79 void ArgReader::checkArgument(std::string option, float &floating, bool must)
    80 {
    81   int argpos = checkOption(option) + 1;
    82   if(argpos != 0)
    83   {
    84     floating = (float)atof(arguments_[argpos]);
    85   }
    86   else
    87   {
    88     if(must) {
    89       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";
    90       fail_ = true;
    91     }
    92   }
    93 }
    94 
    95 int ArgReader::checkOption(std::string option)
    96 {
    97   for(int i = 1; i < counter_; i++)
    98   {
    99     if(arguments_[i] == "--" + option)
    100         return i;
    101   }
    102   return -1;
    10377}
    10478
    10579bool ArgReader::errorHandling()
    10680{
    107   if(fail_)
    108     std::cout << errorStr_;
    109   return fail_;
     81  bool argumentsChecked = true;
     82  for (unsigned int i = 1; i < arguments_.size(); ++i)
     83    argumentsChecked &= arguments_[i].bChecked_;
     84
     85  if (!argumentsChecked)
     86    errorString_ += "Not all arguments could be matched.\n";
     87
     88  return !argumentsChecked || failure_;
    11089}
     90
     91const std::string& ArgReader::getErrorString()
     92{
     93  return errorString_;
     94}
  • code/trunk/src/util/ArgReader.h

    r1505 r1535  
    2121 *
    2222 *   Author:
     23 *      Reto Grieder
     24 *   Co-authors:
    2325 *      Benjamin Knecht <beni_at_orxonox.net>
    24  *   Co-authors:
    25  *      ...
    2626 *
    2727 */
     
    3939
    4040#include <string>
     41#include <vector>
     42#include "Convert.h"
     43
     44struct _UtilExport CmdLineArg
     45{
     46  std::string name_;
     47  std::string value_;
     48  bool bChecked_;
     49};
    4150
    4251class _UtilExport ArgReader
     
    4453  public:
    4554    ArgReader(int argc, char **argv);
    46     void checkArgument(std::string option, std::string& string, bool must=false);
    47     void checkArgument(std::string option, int& integer, bool must=false);
    48     void checkArgument(std::string option, float& floating, bool must=false);
     55    template <class T>
     56    void checkArgument(std::string option, T* value, bool must = false);
    4957    bool errorHandling();
    50   private:
    51     int checkOption(std::string option);
     58    const std::string& getErrorString();
    5259
    5360  private:
    54     int counter_;
    55     char **arguments_;
    56     bool fail_;
    57     std::string errorStr_;
     61    std::vector<CmdLineArg> arguments_;
     62    bool failure_;
     63    std::string errorString_;
    5864};
    5965
     66template <class T>
     67void ArgReader::checkArgument(std::string option, T* value, bool must)
     68{
     69  unsigned int iArg = 0;
     70  while (iArg < arguments_.size())
     71  {
     72    if (arguments_[iArg].name_ == option)
     73      break;
     74    ++iArg;
     75  }
     76  if (iArg == arguments_.size())
     77  {
     78    if (must)
     79    {
     80      failure_ = true;
     81      errorString_ += "Cannot find mandatory argument \"" + option + "\"\n";
     82      return;
     83    }
     84    else
     85      return;
     86  }
     87
     88  arguments_[iArg].bChecked_ = true;
     89
     90  if (!convertValue(value, arguments_[iArg].value_))
     91  {
     92    failure_ = true;
     93    errorString_ += "Cannot convert argument value for option \"" + option + "\"\n";
     94  }
     95}
     96
     97template <>
     98void ArgReader::checkArgument(std::string option, bool* value, bool must)
     99{
     100  // for type bool, only check whether the option was set or not
     101  unsigned int iArg = 0;
     102  while (iArg < arguments_.size())
     103  {
     104    if (arguments_[iArg].name_ == option)
     105    {
     106      arguments_[iArg].bChecked_ = true;
     107      *value = true;
     108      break;
     109    }
     110    ++iArg;
     111  }
     112  if (iArg == arguments_.size())
     113    *value = false;
     114}
     115
    60116#endif /* _ArgReader_H__ */
Note: See TracChangeset for help on using the changeset viewer.