src/biu/OptionParser.hh
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #if !defined(IT_OPTIONPARSER_H__INCLUDED)
00013 #define IT_OPTIONPARSER_H__INCLUDED
00014
00015 #include <vector>
00016 #include <string>
00017 #include <sstream>
00018
00019 #include "biu/HashMap.hh"
00020
00021
00022 namespace biu {
00023
00029 class COption {
00030
00031 public:
00032
00033 static std::string DEF_INIT;
00034
00035 std::string option;
00036 bool optional;
00037 int retType;
00038 std::string description;
00039 std::string strValue;
00040 std::string defValue;
00041 bool exist;
00042
00044 enum TYPES { STRING, CHAR, INT, FLOAT, DOUBLE, BOOL , TYPES_SIZE};
00045
00047 static std::vector<std::string> initTypeNames() {
00048 std::vector<std::string> ret = std::vector<std::string>(TYPES_SIZE, "TYPE");
00049
00050 ret[STRING] = "str ";
00051 ret[CHAR] = "char";
00052 ret[INT] = "int ";
00053 ret[FLOAT] = "flt ";
00054 ret[DOUBLE] = "dbl ";
00055 ret[BOOL] = "bool";
00056
00057 return ret;
00058 }
00060 static std::vector<std::string> TYPE_NAME;
00061
00063 COption (std::string _option, bool _optional, int _retType, std::string _description, std::string _defaultValue=DEF_INIT) :
00064 option(_option),
00065 optional(_optional),
00066 retType(_retType),
00067 description(_description),
00068 strValue(_defaultValue),
00069 defValue(_defaultValue),
00070 exist(false)
00071 {}
00072
00073 ~COption() {}
00074 };
00075
00076
00078
00080
00082 typedef std::vector<COption> OptionMap;
00084 typedef OptionMap::const_iterator OptionMapIt;
00085
00092 class COptionParser {
00093
00094 private:
00095
00096 #ifdef HAVE_GNU_HASH_MAP
00097
00101 struct hash_string {
00102 size_t operator()(const std::string& str) const
00103 {
00104 size_t hash = 5381;
00105
00106 for (size_t i = 0; i < str.size(); i++) {
00107 hash = ((hash << 5) + hash) + (size_t)str[i];
00108 }
00109
00110 return hash;
00111 }
00112
00113 };
00114
00115 typedef __gnu_cxx::hash_map< std::string, int, hash_string > STR2INT_MAP;
00116 #else
00117 typedef std::map< std::string, int > STR2INT_MAP;
00118 #endif
00119
00120 OptionMap opt;
00121 std::string programName;
00122 std::string infoText;
00123
00124 bool errorOccured;
00125 unsigned int maxOName;
00126 STR2INT_MAP mopt;
00127
00129 enum ERRORS { ERR_NO_OPT, ERR_WR_USE, ERR_WR_VAL, ERR_NO_ARG };
00130
00135 void coutError(int error, std::string optionName, std::string errormsg);
00136
00141 bool isCastable(std::string val, int type) const;
00142
00144 void parseOpt(int argc, char** argv);
00145
00147 void coutLineBreaking(std::string text, std::ostream &os,const int emptyHeadSize,const int lineLength) const;
00148
00149 public:
00150
00152 static int OUTPUT_LINE_LENGTH;
00153
00155 COptionParser(OptionMap _options, int argc, char** argv, std::string infoText);
00156
00158 bool noErrors();
00159
00161 void coutUsage() const;
00162
00165 bool argExist(std::string option);
00166
00167
00168
00169 std::string getStrVal(std::string arg);
00170 char getCharVal(std::string arg);
00171 int getIntVal(std::string arg);
00172 float getFloatVal(std::string arg);
00173 double getDoubleVal(std::string arg);
00174 bool getBoolVal(std::string arg);
00175
00176 };
00177
00178 }
00179
00180
00181 #endif // !defined(IT_OPTIONPARSER_H__INCLUDED)
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209