| 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 | *      Reto Grieder | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      Benjamin Knecht <beni_at_orxonox.net> | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file  ArgReader.cc | 
|---|
| 31 | @brief Argument Reader | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "ArgReader.h" | 
|---|
| 35 | #include "SubString.h" | 
|---|
| 36 | #include <ostream> | 
|---|
| 37 | #include <iostream> | 
|---|
| 38 |  | 
|---|
| 39 | class FooBar | 
|---|
| 40 | { | 
|---|
| 41 | //friend class ArgReader; | 
|---|
| 42 | public: | 
|---|
| 43 | operator long long() const | 
|---|
| 44 | { | 
|---|
| 45 | return 0; | 
|---|
| 46 | } | 
|---|
| 47 | float float_; | 
|---|
| 48 | static FooBar& createFooBar(); | 
|---|
| 49 | private: | 
|---|
| 50 | //FooBar(); | 
|---|
| 51 | //FooBar(const FooBar& instance); | 
|---|
| 52 | //~FooBar(); | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | inline std::ostream& operator<<(std::ostream& outstream, const FooBar& fb) | 
|---|
| 56 | { | 
|---|
| 57 | return outstream << fb.float_; | 
|---|
| 58 | } | 
|---|
| 59 | inline std::istream& operator>>(std::istream& instream,  const FooBar& fb); | 
|---|
| 60 | //inline bool explicitConversion(const char** output, const FooBar input) | 
|---|
| 61 | //{ | 
|---|
| 62 | //    return true; | 
|---|
| 63 | //} | 
|---|
| 64 |  | 
|---|
| 65 | #include "Convert.h" | 
|---|
| 66 |  | 
|---|
| 67 | //template<> | 
|---|
| 68 | //struct ConverterExplicit<const char*, FooBar> | 
|---|
| 69 | //{ | 
|---|
| 70 | //    static bool convert(const char** output, const FooBar input) | 
|---|
| 71 | //    { | 
|---|
| 72 | //        return true; | 
|---|
| 73 | //    } | 
|---|
| 74 | //}; | 
|---|
| 75 |  | 
|---|
| 76 | #include "MultiType.h" | 
|---|
| 77 |  | 
|---|
| 78 | ArgReader::ArgReader(int argc, char **argv) | 
|---|
| 79 | { | 
|---|
| 80 | failure_ = false; | 
|---|
| 81 | errorString_ = ""; | 
|---|
| 82 | CmdLineArg arg; | 
|---|
| 83 |  | 
|---|
| 84 | //std::cout << errorString_; | 
|---|
| 85 |  | 
|---|
| 86 | //const int a = conversionTests::ExplicitConversion<FooBar, const char*>::exists; | 
|---|
| 87 | //BOOST_STATIC_ASSERT(a == 0); | 
|---|
| 88 | //int val1; | 
|---|
| 89 | //long long val2 = 4LL;//conversion_cast<long long>(val1); | 
|---|
| 90 | //val2 = val1; | 
|---|
| 91 | //convertValue(&val2, val1); | 
|---|
| 92 | //convertValue(&FooBar(), val1); | 
|---|
| 93 |  | 
|---|
| 94 | //using namespace1::fooBar1; | 
|---|
| 95 | //fooBar1(); | 
|---|
| 96 | //int val1; | 
|---|
| 97 | //char val2; | 
|---|
| 98 | //convertValue(&val1, val2); | 
|---|
| 99 |  | 
|---|
| 100 | //std::istringstream asdf; | 
|---|
| 101 | //asdf >> val2; | 
|---|
| 102 |  | 
|---|
| 103 | int i = 1; | 
|---|
| 104 | while (i < argc) | 
|---|
| 105 | { | 
|---|
| 106 | if (argv[i][0] == '-' && argv[i][1] == '-') // name | 
|---|
| 107 | { | 
|---|
| 108 | if (argv[i][2] == '\0') | 
|---|
| 109 | { | 
|---|
| 110 | failure_ = true; | 
|---|
| 111 | errorString_ += "Expected word after \"--\".\n"; | 
|---|
| 112 | } | 
|---|
| 113 | arg.bChecked_ = false; | 
|---|
| 114 | arg.name_ = argv[i] + 2; | 
|---|
| 115 | arg.value_ = ""; | 
|---|
| 116 | arguments_.push_back(arg); | 
|---|
| 117 | } | 
|---|
| 118 | else // value | 
|---|
| 119 | { | 
|---|
| 120 | if (arguments_.size() == 0) | 
|---|
| 121 | { | 
|---|
| 122 | failure_ = true; | 
|---|
| 123 | errorString_ += "Expected \"--\" in command line arguments.\n"; | 
|---|
| 124 | arg.bChecked_ = false; | 
|---|
| 125 | arg.name_ = ""; | 
|---|
| 126 | arg.value_ = ""; | 
|---|
| 127 | arguments_.push_back(arg); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | if (arguments_.back().value_ != "") | 
|---|
| 131 | arguments_.back().value_ += " " + std::string(argv[i]); | 
|---|
| 132 | else | 
|---|
| 133 | arguments_.back().value_ = argv[i]; | 
|---|
| 134 | } | 
|---|
| 135 | ++i; | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | bool ArgReader::errorHandling() | 
|---|
| 140 | { | 
|---|
| 141 | bool argumentsChecked = true; | 
|---|
| 142 | for (unsigned int i = 1; i < arguments_.size(); ++i) | 
|---|
| 143 | argumentsChecked &= arguments_[i].bChecked_; | 
|---|
| 144 |  | 
|---|
| 145 | if (!argumentsChecked) | 
|---|
| 146 | errorString_ += "Not all arguments could be matched.\n"; | 
|---|
| 147 |  | 
|---|
| 148 | return !argumentsChecked || failure_; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | const std::string& ArgReader::getErrorString() | 
|---|
| 152 | { | 
|---|
| 153 | return errorString_; | 
|---|
| 154 | } | 
|---|