Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/ArgReader.h @ 1710

Last change on this file since 1710 was 1535, checked in by rgrieder, 16 years ago

merged input branch back to trunk

  • Property svn:eol-style set to native
File size: 2.7 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[1535]23 *      Reto Grieder
24 *   Co-authors:
[1505]25 *      Benjamin Knecht <beni_at_orxonox.net>
26 *
27 */
28
[945]29/**
30 @file  Argreader.h
31 @brief reads arguments
32 @author Benjamin Knecht <beni_at_orxonox.net>
33 */
34
35#ifndef _ArgReader_H__
36#define _ArgReader_H__
[1062]37
[1505]38#include "UtilPrereqs.h"
[945]39
[1505]40#include <string>
[1535]41#include <vector>
42#include "Convert.h"
[1505]43
[1535]44struct _UtilExport CmdLineArg
45{
46  std::string name_;
47  std::string value_;
48  bool bChecked_;
49};
50
[945]51class _UtilExport ArgReader
52{
53  public:
54    ArgReader(int argc, char **argv);
[1535]55    template <class T>
56    void checkArgument(std::string option, T* value, bool must = false);
[945]57    bool errorHandling();
[1535]58    const std::string& getErrorString();
[945]59
60  private:
[1535]61    std::vector<CmdLineArg> arguments_;
62    bool failure_;
63    std::string errorString_;
[945]64};
65
[1535]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
[945]116#endif /* _ArgReader_H__ */
Note: See TracBrowser for help on using the repository browser.