Changeset 1531 for code/branches/input/src/util
- Timestamp:
- Jun 4, 2008, 8:06:38 PM (17 years ago)
- Location:
- code/branches/input/src/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/util/ArgReader.cc
r1505 r1531 21 21 * 22 22 * Author: 23 * Benjamin Knecht <beni_at_orxonox.net>, (C) 200723 * Reto Grieder 24 24 * Co-authors: 25 * ...25 * Benjamin Knecht <beni_at_orxonox.net> 26 26 * 27 27 */ … … 33 33 34 34 #include "ArgReader.h" 35 #include "SubString.h" 35 36 36 #include <iostream> 37 ArgReader::ArgReader(int argc, char **argv) 38 { 39 failure_ = false; 40 errorString_ = ""; 41 CmdLineArg arg; 37 42 38 ArgReader::ArgReader(int argc, char** argv) 39 { 40 counter_ = argc; 41 arguments_ = argv; 42 fail_ = false; 43 errorStr_ = ""; 44 } 43 int i = 1; 44 while (i < argc) 45 { 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); 57 } 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 } 45 69 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];70 if (arguments_.back().value_ != "") 71 arguments_.back().value_ += " " + std::string(argv[i]); 72 else 73 arguments_.back().value_ = argv[i]; 74 } 75 ++i; 52 76 } 53 else54 {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 else71 {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 else87 {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 77 } 104 78 105 79 bool ArgReader::errorHandling() 106 80 { 107 if(fail_) 108 std::cout << errorStr_; 109 return fail_; 81 bool argumentsChecked = true; 82 for (unsigned int i = 1; i < arguments_.size(); ++i) 83 argumentsChecked &= arguments_[i].bChecked_; 84 85 if (!argumentsChecked) 86 errorString_ += "Not all arguments could be matched.\n"; 87 88 return !argumentsChecked || failure_; 110 89 } 90 91 const std::string& ArgReader::getErrorString() 92 { 93 return errorString_; 94 } -
code/branches/input/src/util/ArgReader.h
r1505 r1531 21 21 * 22 22 * Author: 23 * Reto Grieder 24 * Co-authors: 23 25 * Benjamin Knecht <beni_at_orxonox.net> 24 * Co-authors:25 * ...26 26 * 27 27 */ … … 39 39 40 40 #include <string> 41 #include <vector> 42 #include "Convert.h" 43 44 struct _UtilExport CmdLineArg 45 { 46 std::string name_; 47 std::string value_; 48 bool bChecked_; 49 }; 41 50 42 51 class _UtilExport ArgReader … … 44 53 public: 45 54 ArgReader(int argc, char **argv); 46 void checkArgument(std::string option, std::string& string, bool must=false); 47 void checkArgument(std::string option, int& integer, bool must=false); 48 void checkArgument(std::string option, float& floating, bool must=false); 55 template <class T> 56 void checkArgument(std::string option, T* value, bool must = false); 49 57 bool errorHandling(); 50 private: 51 int checkOption(std::string option); 58 const std::string& getErrorString(); 52 59 53 60 private: 54 int counter_; 55 char **arguments_; 56 bool fail_; 57 std::string errorStr_; 61 std::vector<CmdLineArg> arguments_; 62 bool failure_; 63 std::string errorString_; 58 64 }; 59 65 66 template <class T> 67 void ArgReader::checkArgument(std::string option, T* value, bool must) 68 { 69 unsigned int iArg = 0; 70 while (iArg < arguments_.size()) 71 { 72 if (arguments_[iArg].name_ == option) 73 break; 74 ++iArg; 75 } 76 if (iArg == arguments_.size()) 77 { 78 if (must) 79 { 80 failure_ = true; 81 errorString_ += "Cannot find mandatory argument \"" + option + "\"\n"; 82 return; 83 } 84 else 85 return; 86 } 87 88 arguments_[iArg].bChecked_ = true; 89 90 if (!convertValue(value, arguments_[iArg].value_)) 91 { 92 failure_ = true; 93 errorString_ += "Cannot convert argument value for option \"" + option + "\"\n"; 94 } 95 } 96 97 template <> 98 void ArgReader::checkArgument(std::string option, bool* value, bool must) 99 { 100 // for type bool, only check whether the option was set or not 101 unsigned int iArg = 0; 102 while (iArg < arguments_.size()) 103 { 104 if (arguments_[iArg].name_ == option) 105 { 106 arguments_[iArg].bChecked_ = true; 107 *value = true; 108 break; 109 } 110 ++iArg; 111 } 112 if (iArg == arguments_.size()) 113 *value = false; 114 } 115 60 116 #endif /* _ArgReader_H__ */
Note: See TracChangeset
for help on using the changeset viewer.