]> Dogcows Code - chaz/openbox/blobdiff - src/Resource.cc
fixed memory leaks for strftime_format and rootcommand
[chaz/openbox] / src / Resource.cc
index 5ff05657c28a7822991e0556ea82106e97e1a63a..2a678f89dafde320e75491ca3741599e424a84ba 100644 (file)
@@ -20,6 +20,7 @@
 // DEALINGS IN THE SOFTWARE.
 
 #include "Resource.h"
+#include "Util.h"
 
 #ifdef    HAVE_CONFIG_H
 #  include "../config.h"
 #  include <stdio.h>
 #endif // HAVE_STDIO_H
 
-#include <assert.h>
+bool Resource::m_initialized = false;
 
-obResource::obResource(const std::string &file) {
+Resource::Resource(const std::string &file) {
   setFile(file);
   m_modified = false;
   m_database = NULL;
   m_autosave = true;
+  if (!m_initialized) {
+    XrmInitialize();
+    m_initialized = true;
+  }
 }
 
-obResource::obResource() {
+Resource::Resource() {
   m_modified = false;
   m_database = NULL;
   m_autosave = true;
+  if (!m_initialized) {
+    XrmInitialize();
+    m_initialized = true;
+  }
 }
 
-obResource::~obResource() {
+Resource::~Resource() {
   if (m_database != NULL)
     XrmDestroyDatabase(m_database);
 }
 
-void obResource::setFile(const std::string &file) {
+void Resource::setFile(const std::string &file) {
   m_file = file;
 }
 
-void obResource::setAutoSave(bool autosave) {
+void Resource::setAutoSave(bool autosave) {
   m_autosave = autosave;
 }
 
-void obResource::save() {
-  assert(m_file.c_str() != NULL);
-  assert(m_database != NULL);
+void Resource::save() {
+  ASSERT(m_database != NULL);
   XrmPutFileDatabase(m_database, m_file.c_str());
   m_modified = false;
 }
 
-bool obResource::load() {
-  assert(m_file.c_str() != NULL);
+bool Resource::load() {
   if (m_database != NULL)
     XrmDestroyDatabase(m_database);
   m_modified = false;
@@ -78,9 +85,15 @@ bool obResource::load() {
   return true;
 }
 
-void obResource::setValue(const std::string &rname, bool value) {
-  assert(rname.c_str() != NULL);
-  assert(m_database != NULL);
+void Resource::create() {
+  if (m_database != NULL)
+    XrmDestroyDatabase(m_database);
+  m_modified = false;
+  ASSERT(NULL != (m_database = XrmGetStringDatabase("")));
+}
+
+void Resource::setValue(const std::string &rname, bool value) {
+  ASSERT(m_database != NULL);
 
   const char *val = (value ? "True" : "False");
   std::string rc_string = rname + ": " + val;
@@ -91,9 +104,12 @@ void obResource::setValue(const std::string &rname, bool value) {
     save();
 }
 
-void obResource::setValue(const std::string &rname, long value) {
-  assert(rname.c_str() != NULL);
-  assert(m_database != NULL);
+void Resource::setValue(const std::string &rname, int value) {
+  setValue(rname, (long)value);
+}
+
+void Resource::setValue(const std::string &rname, long value) {
+  ASSERT(m_database != NULL);
   
   char val[11];
   sprintf(val, "%ld", value);
@@ -105,9 +121,9 @@ void obResource::setValue(const std::string &rname, long value) {
     save();
 }
 
-void obResource::setValue(const std::string &rname, const char *value) {
-  assert(rname.c_str() != NULL);
-  assert(m_database != NULL);
+void Resource::setValue(const std::string &rname, const char *value) {
+  ASSERT(m_database != NULL);
+  ASSERT(value != NULL);
   
   std::string rc_string = rname + ": " + value;
   XrmPutLineResource(&m_database, rc_string.c_str());
@@ -117,9 +133,8 @@ void obResource::setValue(const std::string &rname, const char *value) {
     save();
 }
 
-void obResource::setValue(const std::string &rname, const std::string &value) {
-  assert(rname.c_str() != NULL);
-  assert(m_database != NULL);
+void Resource::setValue(const std::string &rname, const std::string &value) {
+  ASSERT(m_database != NULL);
   
   std::string rc_string = rname + ": " + value;
   XrmPutLineResource(&m_database, rc_string.c_str());
@@ -129,11 +144,10 @@ void obResource::setValue(const std::string &rname, const std::string &value) {
     save();
 }
 
-bool obResource::getValue(const std::string &rname, const std::string &rclass,
+bool Resource::getValue(const std::string &rname, const std::string &rclass,
                           bool &value) const {
-  assert(rname.c_str() != NULL);
-  assert(rclass.c_str() != NULL);
-  assert(m_database != NULL);
+  ASSERT(rclass.c_str() != NULL);
+  ASSERT(m_database != NULL);
   
   char *rettype;
   XrmValue retvalue;
@@ -141,18 +155,16 @@ bool obResource::getValue(const std::string &rname, const std::string &rclass,
                           &rettype, &retvalue) || retvalue.addr == NULL)
     return false;
   std::string val = retvalue.addr;
-  if (val == "True")
+  if (0 == strncasecmp(val.c_str(), "true", val.length()))
     value = true;
   else
     value = false;
   return true;
 }
 
-bool obResource::getValue(const std::string &rname, const std::string &rclass,
+bool Resource::getValue(const std::string &rname, const std::string &rclass,
                           long &value) const {
-  assert(rname.c_str() != NULL);
-  assert(rclass.c_str() != NULL);
-  assert(m_database != NULL);
+  ASSERT(m_database != NULL);
   
   char *rettype;
   XrmValue retvalue;
@@ -166,11 +178,9 @@ bool obResource::getValue(const std::string &rname, const std::string &rclass,
   return true;
 }
 
-bool obResource::getValue(const std::string &rname, const std::string &rclass,
+bool Resource::getValue(const std::string &rname, const std::string &rclass,
                           std::string &value) const {
-  assert(rname.c_str() != NULL);
-  assert(rclass.c_str() != NULL);
-  assert(m_database != NULL);
+  ASSERT(m_database != NULL);
   
   char *rettype;
   XrmValue retvalue;
This page took 0.026231 seconds and 4 git commands to generate.