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: |
---|
23 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file ArgReader.cc |
---|
31 | @brief Argument Reader |
---|
32 | */ |
---|
33 | |
---|
34 | #include "ArgReader.h" |
---|
35 | |
---|
36 | #include <iostream> |
---|
37 | |
---|
38 | ArgReader::ArgReader(int argc, char** argv) |
---|
39 | { |
---|
40 | counter_ = argc; |
---|
41 | arguments_ = argv; |
---|
42 | fail_ = false; |
---|
43 | errorStr_ = ""; |
---|
44 | } |
---|
45 | |
---|
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]; |
---|
52 | } |
---|
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; |
---|
103 | } |
---|
104 | |
---|
105 | bool ArgReader::errorHandling() |
---|
106 | { |
---|
107 | if(fail_) |
---|
108 | std::cout << errorStr_; |
---|
109 | return fail_; |
---|
110 | } |
---|