Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

merged input branch back to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.