1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file ArgReader.cc |
---|
30 | @brief Argument Reader |
---|
31 | */ |
---|
32 | |
---|
33 | #include <iostream> |
---|
34 | |
---|
35 | #include "ArgReader.h" |
---|
36 | |
---|
37 | namespace orxonox { |
---|
38 | |
---|
39 | ArgReader::ArgReader(int argc, char** argv) |
---|
40 | { |
---|
41 | counter_ = argc; |
---|
42 | arguments_ = argv; |
---|
43 | fail_ = false; |
---|
44 | errorStr_ = ""; |
---|
45 | } |
---|
46 | |
---|
47 | void ArgReader::checkArgument(std::string option, std::string &string, bool must) |
---|
48 | { |
---|
49 | int argpos = checkOption(option) + 1; |
---|
50 | if(argpos != 0) |
---|
51 | { |
---|
52 | string = arguments_[argpos]; |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | if(must) { |
---|
57 | errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; |
---|
58 | fail_ = true; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | void ArgReader::checkArgument(std::string option, int &integer, bool must) |
---|
65 | { |
---|
66 | int argpos = checkOption(option) + 1; |
---|
67 | if(argpos != 0) |
---|
68 | { |
---|
69 | integer = atoi(arguments_[argpos]); |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | if(must) { |
---|
74 | errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; |
---|
75 | fail_ = true; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void ArgReader::checkArgument(std::string option, float &floating, bool must) |
---|
81 | { |
---|
82 | int argpos = checkOption(option) + 1; |
---|
83 | if(argpos != 0) |
---|
84 | { |
---|
85 | floating = atof(arguments_[argpos]); |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | if(must) { |
---|
90 | errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; |
---|
91 | fail_ = true; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | int ArgReader::checkOption(std::string option) |
---|
97 | { |
---|
98 | for(int i = 1; i < counter_; i++) |
---|
99 | { |
---|
100 | if(arguments_[i] == "--" + option) |
---|
101 | return i; |
---|
102 | } |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | bool ArgReader::errorHandling() |
---|
107 | { |
---|
108 | if(fail_) |
---|
109 | std::cout << errorStr_; |
---|
110 | return fail_; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|