X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2FConfiguration.cc;h=52e1b272061d164e5defeb4261380ccc360fb0a0;hb=eb1f1b359f821061e1fe6a2953c8d984474c9ef4;hp=be0cd096e7aa7de15ffb04c0fc6e0e9cc88d906e;hpb=b8ee3dbc4565efb19338c5cf1948f0f4e2f24fd6;p=chaz%2Fopenbox diff --git a/src/Configuration.cc b/src/Configuration.cc index be0cd096..52e1b272 100644 --- a/src/Configuration.cc +++ b/src/Configuration.cc @@ -20,199 +20,212 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#ifdef HAVE_CONFIG_H #include "../config.h" +#endif // HAVE_CONFIG_H + +extern "C" { +#ifdef HAVE_STDLIB_H +# include +#endif // HAVE_STDLIB_H +} #include "Configuration.hh" #include "Util.hh" #include -#ifdef HAVE_STDLIB_H -# include -#endif // HAVE_STDLIB_H - using std::string; -bool Configuration::m_initialized = false; +bool Configuration::_initialized = False; -Configuration::Configuration(const string &file) { +Configuration::Configuration(const string &file, bool autosave) { setFile(file); - m_modified = false; - m_database = NULL; - m_autosave = true; - if (! m_initialized) { + _modified = False; + _database = NULL; + _autosave = autosave; + if (! _initialized) { XrmInitialize(); - m_initialized = true; + _initialized = True; } } -Configuration::Configuration() { - m_modified = false; - m_database = NULL; - m_autosave = true; - if (! m_initialized) { +Configuration::Configuration(bool autosave) { + _modified = False; + _database = NULL; + _autosave = autosave; + if (! _initialized) { XrmInitialize(); - m_initialized = true; + _initialized = True; } } Configuration::~Configuration() { - if (m_database != NULL) - XrmDestroyDatabase(m_database); + if (_database != NULL) + XrmDestroyDatabase(_database); } void Configuration::setFile(const string &file) { - m_file = file; + _file = file; } void Configuration::setAutoSave(bool autosave) { - m_autosave = autosave; + _autosave = autosave; } void Configuration::save() { - assert(m_database != NULL); - XrmPutFileDatabase(m_database, m_file.c_str()); - m_modified = false; + assert(_database != NULL); + XrmPutFileDatabase(_database, _file.c_str()); + _modified = False; } bool Configuration::load() { - if (m_database != NULL) - XrmDestroyDatabase(m_database); - m_modified = false; - if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str()))) - return false; - return true; + if (_database != NULL) + XrmDestroyDatabase(_database); + _modified = False; + if (NULL == (_database = XrmGetFileDatabase(_file.c_str()))) + return False; + return True; +} + +bool Configuration::merge(const string &file, bool overwrite) { + if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0) + return False; + _modified = True; + if (_autosave) + save(); + return True; } void Configuration::create() { - if (m_database != NULL) - XrmDestroyDatabase(m_database); - m_modified = false; - assert(NULL != (m_database = XrmGetStringDatabase(""))); + if (_database != NULL) + XrmDestroyDatabase(_database); + _modified = False; + assert(NULL != (_database = XrmGetStringDatabase(""))); } void Configuration::setValue(const string &rname, bool value) { - assert(m_database != NULL); + assert(_database != NULL); const char *val = (value ? "True" : "False"); string rc_string = rname + ": " + val; - XrmPutLineResource(&m_database, rc_string.c_str()); + XrmPutLineResource(&_database, rc_string.c_str()); - m_modified = true; - if (m_autosave) + _modified = True; + if (_autosave) save(); } void Configuration::setValue(const string &rname, unsigned long value) { - assert(m_database != NULL); + assert(_database != NULL); string rc_string = rname + ": " + itostring(value); - XrmPutLineResource(&m_database, rc_string.c_str()); + XrmPutLineResource(&_database, rc_string.c_str()); - m_modified = true; - if (m_autosave) + _modified = True; + if (_autosave) save(); } void Configuration::setValue(const string &rname, long value) { - assert(m_database != NULL); + assert(_database != NULL); string rc_string = rname + ": " + itostring(value); - XrmPutLineResource(&m_database, rc_string.c_str()); + XrmPutLineResource(&_database, rc_string.c_str()); - m_modified = true; - if (m_autosave) + _modified = True; + if (_autosave) save(); } void Configuration::setValue(const string &rname, const char *value) { - assert(m_database != NULL); + assert(_database != NULL); assert(value != NULL); string rc_string = rname + ": " + value; - XrmPutLineResource(&m_database, rc_string.c_str()); + XrmPutLineResource(&_database, rc_string.c_str()); - m_modified = true; - if (m_autosave) + _modified = True; + if (_autosave) save(); } void Configuration::setValue(const string &rname, const string &value) { - assert(m_database != NULL); + assert(_database != NULL); string rc_string = rname + ": " + value; - XrmPutLineResource(&m_database, rc_string.c_str()); + XrmPutLineResource(&_database, rc_string.c_str()); - m_modified = true; - if (m_autosave) + _modified = True; + if (_autosave) save(); } bool Configuration::getValue(const string &rname, bool &value) const { - assert(m_database != NULL); + assert(_database != NULL); string rclass = createClassName(rname); char *rettype; XrmValue retvalue; - if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), + if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(), &rettype, &retvalue) || retvalue.addr == NULL) - return false; + return False; string val = retvalue.addr; - if (val == "true" || val == "True") - value = true; + if (val == "True" || val == "True") + value = True; else - value = false; - return true; + value = False; + return True; } bool Configuration::getValue(const string &rname, long &value) const { - assert(m_database != NULL); + assert(_database != NULL); string rclass = createClassName(rname); char *rettype; XrmValue retvalue; - if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), + if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(), &rettype, &retvalue) || retvalue.addr == NULL) - return false; + return False; char *end; value = strtol(retvalue.addr, &end, 10); if (end == retvalue.addr) - return false; - return true; + return False; + return True; } bool Configuration::getValue(const string &rname, unsigned long &value) const { - assert(m_database != NULL); + assert(_database != NULL); string rclass = createClassName(rname); char *rettype; XrmValue retvalue; - if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), + if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(), &rettype, &retvalue) || retvalue.addr == NULL) - return false; + return False; char *end; value = strtoul(retvalue.addr, &end, 10); if (end == retvalue.addr) - return false; - return true; + return False; + return True; } bool Configuration::getValue(const string &rname, string &value) const { - assert(m_database != NULL); + assert(_database != NULL); string rclass = createClassName(rname); char *rettype; XrmValue retvalue; - if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), + if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(), &rettype, &retvalue) || retvalue.addr == NULL) - return false; + return False; value = retvalue.addr; - return true; + return True; } @@ -220,7 +233,7 @@ string Configuration::createClassName(const string &rname) const { string rclass(rname); string::iterator it = rclass.begin(), end = rclass.end(); - while (true) { + while (True) { *it = toUpper(*it); ++it; if (it == end) break;