| 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 | ArgReader::ArgReader(int argc, char** argv) | 
|---|
| 38 | { | 
|---|
| 39 |   counter_ = argc; | 
|---|
| 40 |   arguments_ = argv; | 
|---|
| 41 |   fail_ = false; | 
|---|
| 42 |   errorStr_ = ""; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void ArgReader::checkArgument(std::string option, std::string &string, bool must) | 
|---|
| 46 | { | 
|---|
| 47 |   int argpos = checkOption(option) + 1; | 
|---|
| 48 |   if(argpos != 0) | 
|---|
| 49 |   { | 
|---|
| 50 |     string = arguments_[argpos]; | 
|---|
| 51 |   } | 
|---|
| 52 |   else | 
|---|
| 53 |   { | 
|---|
| 54 |     if(must) { | 
|---|
| 55 |       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; | 
|---|
| 56 |       fail_ = true; | 
|---|
| 57 |     } | 
|---|
| 58 |   } | 
|---|
| 59 |  | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void ArgReader::checkArgument(std::string option, int &integer, bool must) | 
|---|
| 63 | { | 
|---|
| 64 |   int argpos = checkOption(option) + 1; | 
|---|
| 65 |   if(argpos != 0) | 
|---|
| 66 |   { | 
|---|
| 67 |     integer = atoi(arguments_[argpos]); | 
|---|
| 68 |   } | 
|---|
| 69 |   else | 
|---|
| 70 |   { | 
|---|
| 71 |     if(must) { | 
|---|
| 72 |       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; | 
|---|
| 73 |       fail_ = true; | 
|---|
| 74 |     } | 
|---|
| 75 |   } | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void ArgReader::checkArgument(std::string option, float &floating, bool must) | 
|---|
| 79 | { | 
|---|
| 80 |   int argpos = checkOption(option) + 1; | 
|---|
| 81 |   if(argpos != 0) | 
|---|
| 82 |   { | 
|---|
| 83 |     floating = (float)atof(arguments_[argpos]); | 
|---|
| 84 |   } | 
|---|
| 85 |   else | 
|---|
| 86 |   { | 
|---|
| 87 |     if(must) { | 
|---|
| 88 |       errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n"; | 
|---|
| 89 |       fail_ = true; | 
|---|
| 90 |     } | 
|---|
| 91 |   } | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | int ArgReader::checkOption(std::string option) | 
|---|
| 95 | { | 
|---|
| 96 |   for(int i = 1; i < counter_; i++) | 
|---|
| 97 |   { | 
|---|
| 98 |     if(arguments_[i] == "--" + option) | 
|---|
| 99 |         return i; | 
|---|
| 100 |   } | 
|---|
| 101 |   return -1; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | bool ArgReader::errorHandling() | 
|---|
| 105 | { | 
|---|
| 106 |   if(fail_) | 
|---|
| 107 |     std::cout << errorStr_; | 
|---|
| 108 |   return fail_; | 
|---|
| 109 | } | 
|---|