00001
00002 #include "config.H"
00003 #include "error.H"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 int ConfigInit::count = 0;
00014 Config* Config::_instance = NULL;
00015 bool Config::_replace = true;
00016 bool Config::_loaded = false;
00017 str_list* Config::_no_warn = NULL;
00018
00019
00020
00021
00022
00023
00024
00025
00026 Config::Config(Cstr_ptr& j) : _jot_root(j)
00027 {
00028 assert(!_instance);
00029 err_mesg(ERR_LEV_SPAM, "Config::Config() - Init...");
00030 _instance = this;
00031 }
00032
00033
00034
00035
00036 Config::~Config()
00037 {
00038 err_mesg(ERR_LEV_SPAM, "Config::~Config() - Closing down...");
00039 _instance = NULL;
00040 }
00041
00042
00043 bool
00044 Config::load_config(Cstr_ptr &f, bool rep)
00045 {
00046 bool old_rep = _replace;
00047 _replace = rep;
00048 bool ret = ((_instance)?(_instance->load(f)):(false));
00049
00050 _loaded |= ret;
00051 _replace = old_rep;
00052 return ret;
00053 }
00054
00055
00056
00057
00058 bool
00059 Config::get_var_is_set(Cstr_ptr& var)
00060 {
00061 return getenv(**var) ? true : false;
00062 }
00063
00064
00065
00066
00067 void
00068 Config::set_var_int(Cstr_ptr& var, int int_val)
00069 {
00070 if (_replace || !get_var_is_set(var))
00071 {
00072 putenv(**(var + "=" + str_ptr(int_val)));
00073 }
00074 else
00075 {
00076 err_mesg(ERR_LEV_WARN, "Config::set_var_int() - ***Variable '%s' is already set. Ignoring new value...***", **var);
00077 }
00078 }
00079
00080
00081
00082
00083 void
00084 Config::set_var_dbl(Cstr_ptr& var, double dbl_val)
00085 {
00086 if (_replace || !get_var_is_set(var))
00087 {
00088 putenv(**(var + "=" + str_ptr(dbl_val)));
00089 }
00090 else
00091 {
00092 err_mesg(ERR_LEV_WARN, "Config::set_var_dbl() - ***Variable '%s' is already set. Ignoring new value...***", **var);
00093 }
00094 }
00095
00096
00097
00098
00099 void
00100 Config::set_var_str(Cstr_ptr& var, Cstr_ptr& str_val)
00101 {
00102 if (_replace || !get_var_is_set(var))
00103 {
00104 putenv(**(var + "=" + str_val));
00105 }
00106 else
00107 {
00108 err_mesg(ERR_LEV_WARN, "Config::set_var_str() - ***Variable '%s' is already set. Ignoring new value...***", **var);
00109 }
00110 }
00111
00112
00113
00114
00115 void
00116 Config::set_var_bool(Cstr_ptr& var, bool bool_val)
00117 {
00118 if (_replace || !get_var_is_set(var))
00119 {
00120 putenv(**(var + "=" + ((bool_val)?("true"):("false"))));
00121 }
00122 else
00123 {
00124 err_mesg(ERR_LEV_WARN, "Config::set_var_bool() - ***Variable '%s' is already set. Ignoring new value...***", **var);
00125 }
00126
00127 }
00128
00129
00130
00131
00132 int
00133 Config::get_var_int(Cstr_ptr& var, int def_int_val, bool store)
00134 {
00135 if (!_instance)
00136 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_int() - ***WARNING*** Variable [%s] accessed without an existing _instance!!", **var);
00137 if (!_loaded)
00138 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_int() - ***WARNING*** Variable [%s] accessed before a Config::load() was completed.", **var);
00139
00140 int int_val;
00141
00142 if (!get_var_is_set(var))
00143 {
00144 int_val = def_int_val;
00145 if (store) set_var_int(var,def_int_val);
00146 }
00147 else
00148 {
00149 int_val = atoi(getenv(**var));
00150 }
00151
00152 return int_val;
00153 }
00154
00155
00156
00157
00158 double
00159 Config::get_var_dbl(Cstr_ptr& var, double def_dbl_val, bool store)
00160 {
00161
00162 if (!_instance)
00163 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_dbl() - ***WARNING*** Variable [%s] accessed without an existing _instance!!", **var);
00164 if (!_loaded)
00165 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_dbl() - ***WARNING*** Variable [%s] accessed before a Config::load() was completed.", **var);
00166
00167 double dbl_val;
00168
00169 if (!get_var_is_set(var))
00170 {
00171 dbl_val = def_dbl_val;
00172 if (store) set_var_dbl(var,def_dbl_val);
00173 }
00174 else
00175 {
00176 dbl_val = atof(getenv(**var));
00177 }
00178
00179 return dbl_val;
00180 }
00181
00182
00183
00184
00185
00186 str_ptr
00187 Config::get_var_str(Cstr_ptr& var, Cstr_ptr& def_str_val, bool store)
00188 {
00189 if (!_instance)
00190 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_str() - ***WARNING*** Variable [%s] accessed without an existing _instance!!", **var);
00191 if (!_loaded)
00192 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_str() - ***WARNING*** Variable [%s] accessed before a Config::load() was completed.", **var);
00193
00194 str_ptr str_val;
00195
00196 if (!get_var_is_set(var))
00197 {
00198 str_val = def_str_val;
00199 if (store) set_var_str(var,def_str_val);
00200 }
00201 else
00202 {
00203 str_val = str_ptr(getenv(**var));
00204 }
00205
00206 return str_val;
00207 }
00208
00209
00210
00211
00212 bool
00213 Config::get_var_bool(Cstr_ptr& var, bool def_bool_val, bool store)
00214 {
00215 if (!_instance)
00216 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_bool() - ***WARNING*** Variable [%s] accessed without an existing _instance!!", **var);
00217 if (!_loaded)
00218 err_mesg_cond(!_no_warn->contains(var), ERR_LEV_WARN, "Config::get_var_bool() - ***WARNING*** Variable [%s] accessed before a Config::load() was completed.", **var);
00219
00220 bool bool_val;
00221
00222 if (!get_var_is_set(var))
00223 {
00224 bool_val = def_bool_val;
00225 if (store) set_var_bool(var,def_bool_val);
00226 }
00227 else
00228 {
00229 str_ptr foo = str_ptr(getenv(**var));
00230
00231 if (foo == "true")
00232 {
00233 bool_val = true;
00234 }
00235 else if (foo == "false")
00236 {
00237 bool_val = false;
00238 }
00239 else
00240 {
00241 err_mesg(ERR_LEV_WARN, "Config::get_var_bool - ERROR! Boolean environment variable '%s' should be either 'true' or 'false'. Changing to 'false'.", **var);
00242 bool_val = false;
00243 set_var_bool(var,bool_val);
00244 }
00245 }
00246
00247 return bool_val;
00248 }
00249
00250