00001
00002 #include <G3D/G3D.h>
00003 using namespace G3D;
00004
00005 #include <iostream>
00006 #include <sstream>
00007
00008 #include "G3DOperators.H"
00009 #include "CygPath.H"
00010
00011 #ifndef CONFIGVAL_H
00012 #define CONFIGVAL_H
00013
00014 namespace IS3D {
00015
00065
00066
00067
00068
00069
00070 class ConfigMap
00071 {
00072 public:
00073 static bool readFile(const std::string &filename, Log* log=NULL);
00074 static bool containsKey(const std::string &keyString);
00075 static std::string getValue(const std::string &keyString);
00076 static void set(const std::string &key, const std::string &value);
00077 static void debugPrint();
00078 private:
00079 static Table<std::string, std::string> _map;
00080 };
00081
00082
00083 static inline void notFoundWarning(const std::string &keyString) {
00084 std::cout << "No ConfigVal mapping found for \"" << keyString << "\"." << std::endl;
00085 }
00086
00088 template <class T>
00089 static inline bool retypeVal(const std::string &str, T &val) {
00090 std::istringstream is(str.c_str());
00091 is >> val;
00092 if (!is) return false;
00093 else return true;
00094 }
00095
00101 template <class KEYTYPE, class VALTYPE>
00102 VALTYPE ConfigVal(KEYTYPE keyString, const VALTYPE &defaultVal, bool warn=true) {
00103 if (!ConfigMap::containsKey(keyString)) {
00104 if (warn)
00105 notFoundWarning(keyString);
00106 return defaultVal;
00107 }
00108 else {
00109 std::string valString = ConfigMap::getValue(keyString);
00110
00111 VALTYPE val;
00112 bool ok = retypeVal(valString, val);
00113 if (!ok) {
00114
00115 std::string errString =
00116 std::string("ERROR: ConfigVal is unable to retype value.\n\tKey:\t'")
00117 + keyString + std::string("'\n\tValue:\t'") + valString + std::string("'\n");
00118 std::cerr << errString;
00119
00120 }
00121 return val;
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130 #if (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500) || \
00131 (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
00132 #define QUOTED_STRING const char *
00133 #else
00134 #define QUOTED_STRING char *
00135 #endif
00136
00137 inline std::string ConfigVal(QUOTED_STRING keyString, QUOTED_STRING defaultVal, bool warn=true) {
00138 if (ConfigMap::containsKey(keyString))
00139 return replaceEnvVars(ConfigMap::getValue(keyString));
00140 else {
00141 if (warn) notFoundWarning(keyString);
00142 return replaceEnvVars(std::string(defaultVal));
00143 }
00144 }
00145
00146 inline std::string ConfigVal(QUOTED_STRING keyString, std::string defaultVal, bool warn=true) {
00147 if (ConfigMap::containsKey(keyString))
00148 return replaceEnvVars(ConfigMap::getValue(keyString));
00149 else {
00150 if (warn) notFoundWarning(keyString);
00151 return replaceEnvVars(defaultVal);
00152 }
00153 }
00154
00155 inline std::string ConfigVal(std::string keyString, QUOTED_STRING defaultVal, bool warn=true) {
00156 if (ConfigMap::containsKey(keyString))
00157 return replaceEnvVars(ConfigMap::getValue(keyString));
00158 else {
00159 if (warn) notFoundWarning(keyString);
00160 return replaceEnvVars(std::string(defaultVal));
00161 }
00162 }
00163
00164 inline std::string ConfigVal(std::string keyString, std::string defaultVal, bool warn=true) {
00165 if (ConfigMap::containsKey(keyString))
00166 return replaceEnvVars(ConfigMap::getValue(keyString));
00167 else {
00168 if (warn) notFoundWarning(keyString);
00169 return replaceEnvVars(defaultVal);
00170 }
00171 }
00172
00173
00174 }
00175
00176
00177 #endif
00178