[945] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 3 | * > www.orxonox.net < |
---|
[945] | 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 |
---|
[945] | 24 | * Co-authors: |
---|
[1535] | 25 | * Benjamin Knecht <beni_at_orxonox.net> |
---|
[945] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file ArgReader.cc |
---|
| 31 | @brief Argument Reader |
---|
| 32 | */ |
---|
| 33 | |
---|
[1062] | 34 | #include "ArgReader.h" |
---|
[1535] | 35 | #include "SubString.h" |
---|
[1062] | 36 | |
---|
[1535] | 37 | ArgReader::ArgReader(int argc, char **argv) |
---|
[945] | 38 | { |
---|
[1535] | 39 | failure_ = false; |
---|
| 40 | errorString_ = ""; |
---|
| 41 | CmdLineArg arg; |
---|
[945] | 42 | |
---|
[1535] | 43 | int i = 1; |
---|
| 44 | while (i < argc) |
---|
[945] | 45 | { |
---|
[1535] | 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); |
---|
[945] | 57 | } |
---|
[1535] | 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 | } |
---|
[945] | 69 | |
---|
[1535] | 70 | if (arguments_.back().value_ != "") |
---|
| 71 | arguments_.back().value_ += " " + std::string(argv[i]); |
---|
| 72 | else |
---|
| 73 | arguments_.back().value_ = argv[i]; |
---|
[945] | 74 | } |
---|
[1535] | 75 | ++i; |
---|
[945] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
[1535] | 79 | bool ArgReader::errorHandling() |
---|
[945] | 80 | { |
---|
[1535] | 81 | bool argumentsChecked = true; |
---|
| 82 | for (unsigned int i = 1; i < arguments_.size(); ++i) |
---|
| 83 | argumentsChecked &= arguments_[i].bChecked_; |
---|
[945] | 84 | |
---|
[1535] | 85 | if (!argumentsChecked) |
---|
| 86 | errorString_ += "Not all arguments could be matched.\n"; |
---|
| 87 | |
---|
| 88 | return !argumentsChecked || failure_; |
---|
[945] | 89 | } |
---|
| 90 | |
---|
[1535] | 91 | const std::string& ArgReader::getErrorString() |
---|
[945] | 92 | { |
---|
[1535] | 93 | return errorString_; |
---|
[945] | 94 | } |
---|