00001
00002 #ifndef CONFIGMAP_H
00003 #define CONFIGMAP_H
00004
00005 #include <G3D/G3DAll.h>
00006 #include <sstream>
00007 #include "G3DOperators.H"
00008
00009 namespace VRG3D {
00010
00011
00012 #if (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500) || \
00013 (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
00014 #define QUOTED_STRING const char *
00015 #else
00016 #define QUOTED_STRING char *
00017 #endif
00018
00019
00083
00084
00085
00086
00087
00088 typedef ReferenceCountedPointer<class ConfigMap> ConfigMapRef;
00089 class ConfigMap : public ReferenceCountedObject
00090 {
00091 public:
00093 ConfigMap(Log *log) {
00094 _log = log;
00095 }
00096
00098 ConfigMap(const std::string &filename, Log *log) {
00099 _log = log;
00100 readFile(filename);
00101 }
00102
00105 ConfigMap(int argc, char **argv, Log *log, bool exitOnUnrecognizedArgument);
00106
00107 virtual ~ConfigMap() {}
00108
00109 void printArgumentHelpAndExit(const std::string &programName);
00110 bool readFile(const std::string &filename);
00111
00112 template <class T>
00113 bool retypeString(const std::string &str, T &val) {
00114 std::istringstream is(str.c_str());
00115 is >> val;
00116 if (!is) {
00117 return false;
00118 }
00119 else {
00120 return true;
00121 }
00122 }
00123
00124 template <class VALTYPE>
00125 VALTYPE get(std::string keyString, const VALTYPE &defaultVal) {
00126 if (containsKey(keyString)) {
00127 std::string valString = getValue(keyString);
00128 VALTYPE val;
00129 bool ok = retypeString(valString, val);
00130 if (ok) {
00131 return val;
00132 }
00133 else {
00134 if (_log) {
00135 _log->printf("ConfigMap Error: cannot remap '%s'", valString.c_str());
00136 }
00137 else {
00138 printf("ConfigMap Error: cannot remap '%s'", valString.c_str());
00139 }
00140 return defaultVal;
00141 }
00142 }
00143 else {
00144 if (_log) {
00145 _log->printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00146 }
00147 else {
00148 printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00149 }
00150 return defaultVal;
00151 }
00152 }
00153
00154 std::string get(QUOTED_STRING keyString, QUOTED_STRING defaultVal) {
00155 if (containsKey(keyString))
00156 return replaceEnvVars(getValue(keyString));
00157 else {
00158 if (_log) {
00159 _log->printf("ConfigMap Warning: no mapping for '%s'.\n", keyString);
00160 }
00161 else {
00162 printf("ConfigMap Warning: no mapping for '%s'.\n", keyString);
00163 }
00164 return replaceEnvVars(defaultVal);
00165 }
00166 }
00167
00168 std::string get(QUOTED_STRING keyString, std::string defaultVal) {
00169 if (containsKey(keyString))
00170 return replaceEnvVars(getValue(keyString));
00171 else {
00172 if (_log) {
00173 _log->printf("ConfigMap Warning: no mapping for '%s'.\n", keyString);
00174 }
00175 else {
00176 printf("ConfigMap Warning: no mapping for '%s'.\n", keyString);
00177 }
00178 return replaceEnvVars(defaultVal);
00179 }
00180 }
00181
00182 std::string get(std::string keyString, QUOTED_STRING defaultVal) {
00183 if (containsKey(keyString))
00184 return replaceEnvVars(getValue(keyString));
00185 else {
00186 if (_log) {
00187 _log->printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00188 }
00189 else {
00190 printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00191 }
00192 return replaceEnvVars(defaultVal);
00193 }
00194 }
00195
00196 std::string get(std::string keyString, std::string defaultVal) {
00197 if (containsKey(keyString))
00198 return replaceEnvVars(getValue(keyString));
00199 else {
00200 if (_log) {
00201 _log->printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00202 }
00203 else {
00204 printf("ConfigMap Warning: no mapping for '%s'.\n", keyString.c_str());
00205 }
00206 return replaceEnvVars(defaultVal);
00207 }
00208 }
00209
00210 bool containsKey(const std::string &keyString);
00211 std::string getValue(const std::string &keyString);
00212 void set(const std::string &key, const std::string &value);
00213 void debugPrint();
00214
00215 private:
00216 Table<std::string, std::string> _map;
00217 Log* _log;
00218 };
00219
00220
00221 }
00222
00223
00224 #endif
00225