]> git.saurik.com Git - wxWidgets.git/commitdiff
added support for binary data to wxConfig (slightly modified patch 1736788)
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 12 Jul 2007 00:11:03 +0000 (00:11 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 12 Jul 2007 00:11:03 +0000 (00:11 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47353 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/config.tex
include/wx/confbase.h
include/wx/fileconf.h
include/wx/msw/iniconf.h
include/wx/msw/regconf.h
include/wx/palmos/prefconf.h
src/common/fileconf.cpp
src/msw/regconf.cpp
src/palmos/prefconf.cpp
tests/fileconf/fileconftest.cpp

index 173eb59ac164df8c7727fe19e0ae6502c0a8eb7c..1ea9b35234d360e0a83f6dca8e7316eb800c4d08 100644 (file)
@@ -109,6 +109,7 @@ All:
 - Added wxStreamBuffer::Truncate() (Stas Sergeev)
 - Allow using  wxEventLoop in console applications (Lukasz Michalski)
 - Added functions for Base64 en/decoding (Charles Reimers)
+- Added support for binary data to wxConfig (Charles Reimers)
 - Added functions for atomically inc/decrementing integers (Armel Asselin)
 - wxLogInterposer has been added to replace wxLogPassThrough and new
   wxLogInterposerTemp was added
index 4653313e391a8420ec2447ebb2fc7808ae826b09..5955677d543985e48c322f85a826e51ce852b85e 100644 (file)
@@ -716,6 +716,11 @@ not found, {\it b} is not changed.
 Reads a bool value, returning \true if the value was found. If the value was
 not found, {\it defaultVal} is used instead.
 
+\constfunc{bool}{Read}{\param{const wxString\& }{ key}, \param{wxMemoryBuffer*}{ buf}}
+
+Reads a binary block, returning \true if the value was found. If the value was
+not found, {\it buf} is not changed.
+
 \pythonnote{In place of a single overloaded method name, wxPython
 implements the following methods:\par
 \indented{2cm}{\begin{twocollist}
@@ -806,6 +811,8 @@ value}}
 
 \func{bool}{Write}{\param{const wxString\& }{ key}, \param{bool}{ value}}
 
+\func{bool}{Write}{\param{const wxString\& }{ key}, \param{const wxMemoryBuffer\&}{ buf}}
+
 These functions write the specified value to the config file and return \true on success.
 
 \pythonnote{In place of a single overloaded method name, wxPython
index cc796e97244502754fd71e257b3e4a6bd87d632e..58f3b646fbc024aa5a37368cafc66e69237965aa 100644 (file)
@@ -17,6 +17,7 @@
 #include "wx/defs.h"
 #include "wx/string.h"
 #include "wx/object.h"
+#include "wx/base64.h"
 
 class WXDLLIMPEXP_FWD_BASE wxArrayString;
 
@@ -177,6 +178,11 @@ public:
   bool Read(const wxString& key, bool* val) const;
   bool Read(const wxString& key, bool* val, bool defVal) const;
 
+    // read a binary data block
+  bool Read(const wxString& key, wxMemoryBuffer* data) const
+   { return DoReadBinary(key, data); }
+   // no default version since it does not make sense for binary data
+
   // convenience functions returning directly the value (we don't have them for
   // int/double/bool as there would be ambiguities with the long one then)
   wxString Read(const wxString& key,
@@ -202,6 +208,9 @@ public:
   bool Write(const wxString& key, bool value)
     { return DoWriteBool(key, value); }
 
+  bool Write(const wxString& key, const wxMemoryBuffer& buf)
+    { return DoWriteBinary(key, buf); }
+
   // we have to provide a separate version for C strings as otherwise they
   // would be converted to bool and not to wxString as expected!
   bool Write(const wxString& key, const char *value)
@@ -273,12 +282,14 @@ protected:
   virtual bool DoReadInt(const wxString& key, int *pi) const;
   virtual bool DoReadDouble(const wxString& key, double* val) const;
   virtual bool DoReadBool(const wxString& key, bool* val) const;
+  virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const = 0;
 
   virtual bool DoWriteString(const wxString& key, const wxString& value) = 0;
   virtual bool DoWriteLong(const wxString& key, long value) = 0;
   virtual bool DoWriteInt(const wxString& key, int value);
   virtual bool DoWriteDouble(const wxString& key, double value);
   virtual bool DoWriteBool(const wxString& key, bool value);
+  virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) = 0;
 
 private:
   // are we doing automatic environment variable expansion?
index 68e230964b3f4dd793cde2f8564ad9ba4640be78..276ad96f8a726d42907b37d49bc8729dec6d8540 100644 (file)
@@ -194,9 +194,11 @@ public:
 protected:
   virtual bool DoReadString(const wxString& key, wxString *pStr) const;
   virtual bool DoReadLong(const wxString& key, long *pl) const;
+  virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const;
 
   virtual bool DoWriteString(const wxString& key, const wxString& szValue);
   virtual bool DoWriteLong(const wxString& key, long lValue);
+  virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
 
 private:
   // GetXXXFileName helpers: return ('/' terminated) directory names
index 948825de3468522905ba5a38895a736c6834031c..e0ee004ca99ea7fb69f8c192dcbaf30e8b058364 100644 (file)
@@ -78,9 +78,11 @@ protected:
   // read/write
   bool DoReadString(const wxString& key, wxString *pStr) const;
   bool DoReadLong(const wxString& key, long *plResult) const;
+  bool DoReadBinary(const wxString& key, wxMemoryBuffer *buf) const;
 
   bool DoWriteString(const wxString& key, const wxString& szValue);
   bool DoWriteLong(const wxString& key, long lValue);
+  bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
 
 private:
   // helpers
index 82ab6d14a3a9f9c8aacf39477048dbd7d09a3bff..e9e8dbde43ba1010b70925cafdc3f8e4514f2616 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "wx/object.h"
 #include "wx/confbase.h"
+#include "buffer.h"
 
 // ----------------------------------------------------------------------------
 // wxRegConfig
@@ -90,9 +91,11 @@ protected:
   // implement read/write methods
   virtual bool DoReadString(const wxString& key, wxString *pStr) const;
   virtual bool DoReadLong(const wxString& key, long *plResult) const;
+  virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const;
 
   virtual bool DoWriteString(const wxString& key, const wxString& szValue);
   virtual bool DoWriteLong(const wxString& key, long lValue);
+  virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
 
 private:
   // these keys are opened during all lifetime of wxRegConfig object
index ba756045c2f208a4b5a0d2c6e79d3bf3cc341b35..1ca9a013ee70f4b87ab3d2d24d92fd26b11ff71a 100644 (file)
@@ -67,9 +67,11 @@ protected:
   // implement read/write methods
   virtual bool DoReadString(const wxString& key, wxString *pStr) const;
   virtual bool DoReadLong(const wxString& key, long *plResult) const;
+  virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer *buf) const;
 
   virtual bool DoWriteString(const wxString& key, const wxString& szValue);
   virtual bool DoWriteLong(const wxString& key, long lValue);
+  virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
 
 private:
   // current path (not '/' terminated)
index bc153a0a9f9994af8c07c1ab4c2ee52ecd361391..65fc44e0a7ae9c90517052fa0a7fb7f7ad9f48ce 100644 (file)
@@ -42,6 +42,8 @@
 #include  "wx/fileconf.h"
 #include  "wx/filefn.h"
 
+#include "wx/base64.h"
+
 #include  "wx/stdpaths.h"
 
 #if defined(__WXMAC__)
@@ -916,6 +918,18 @@ bool wxFileConfig::DoReadLong(const wxString& key, long *pl) const
     return str.ToLong(pl);
 }
 
+bool wxFileConfig::DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const
+{
+    wxCHECK_MSG( buf, false, _T("NULL buffer") );
+
+    wxString str;
+    if ( !Read(key, &str) )
+        return false;
+
+    *buf = wxBase64Decode(str);
+    return true;
+}
+
 bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue)
 {
     wxConfigPathChanger     path(this, key);
@@ -981,6 +995,11 @@ bool wxFileConfig::DoWriteLong(const wxString& key, long lValue)
   return Write(key, wxString::Format(_T("%ld"), lValue));
 }
 
+bool wxFileConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
+{
+  return Write(key, wxBase64Encode(buf));
+}
+
 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
 {
   if ( !IsDirty() || !m_fnLocalFile.GetFullPath() )
index 19481f6b37dae8010270b257f1bd7f6aa350a41a..4d5388cf4cfe49f0e85313051f6a5c0a707258a4 100644 (file)
@@ -53,6 +53,11 @@ bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
   return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
 }
 
+bool TryGetValue(const wxRegKey& key, const wxString& str, wxMemoryBuffer &plVal)
+{
+  return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
+}
+
 // ============================================================================
 // implementation
 // ============================================================================
@@ -620,6 +625,40 @@ bool wxRegConfig::DoReadLong(const wxString& key, long *plResult) const
   return false;
 }
 
+bool wxRegConfig::DoReadBinary(const wxString& key, wxMemoryBuffer *buf) const
+{
+    wxCHECK_MSG( buf, false, _T("wxRegConfig::Read(): NULL param") );
+
+  wxConfigPathChanger path(this, key);
+
+  bool bQueryGlobal = true;
+
+  // if immutable key exists in global key we must check that it's not
+  // overriden by the local key with the same name
+  if ( IsImmutable(path.Name()) ) {
+    if ( TryGetValue(m_keyGlobal, path.Name(), *buf) ) {
+      if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) {
+        wxLogWarning(wxT("User value for immutable key '%s' ignored."),
+                   path.Name().c_str());
+      }
+
+      return true;
+    }
+    else {
+      // don't waste time - it's not there anyhow
+      bQueryGlobal = false;
+    }
+  }
+
+  // first try local key
+  if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *buf)) ||
+       (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *buf)) ) {
+    return true;
+  }
+
+  return false;
+}
+
 bool wxRegConfig::DoWriteString(const wxString& key, const wxString& szValue)
 {
   wxConfigPathChanger path(this, key);
@@ -644,6 +683,18 @@ bool wxRegConfig::DoWriteLong(const wxString& key, long lValue)
   return LocalKey().SetValue(path.Name(), lValue);
 }
 
+bool wxRegConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
+{
+  wxConfigPathChanger path(this, key);
+
+  if ( IsImmutable(path.Name()) ) {
+    wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
+    return false;
+  }
+
+  return LocalKey().SetValue(path.Name(), buf);
+}
+
 // ----------------------------------------------------------------------------
 // renaming
 // ----------------------------------------------------------------------------
index 288e91f178150119e7aaa32903bde376e15ceae9..5347007980bf8a1bb49c39c3a47d4d02cbc09d4c 100644 (file)
@@ -178,6 +178,12 @@ bool wxPrefConfig::DoReadLong(const wxString& key, long *plResult) const
     return false;
 }
 
+bool DoReadBinary(const wxString& key, wxMemoryBuffer *buf) const
+{
+    /* TODO */
+    return false;
+}
+
 bool wxPrefConfig::DoWriteString(const wxString& key, const wxString& szValue)
 {
     /* TODO */
@@ -190,6 +196,12 @@ bool wxPrefConfig::DoWriteLong(const wxString& key, long lValue)
     return false;
 }
 
+bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
+{
+    /* TODO */
+    return false;
+}
+
 // ----------------------------------------------------------------------------
 // renaming
 // ----------------------------------------------------------------------------
index 6f4aa5ff5357d1d215a02f01a6f8ef62a4730e44..d3694190478543c84f83da72e89a9f74d1f357da 100644 (file)
@@ -67,6 +67,7 @@ private:
         CPPUNIT_TEST( GetGroups );
         CPPUNIT_TEST( HasEntry );
         CPPUNIT_TEST( HasGroup );
+        CPPUNIT_TEST( Binary );
         CPPUNIT_TEST( Save );
         CPPUNIT_TEST( DeleteEntry );
         CPPUNIT_TEST( DeleteGroup );
@@ -85,6 +86,7 @@ private:
     void GetGroups();
     void HasEntry();
     void HasGroup();
+    void Binary();
     void Save();
     void DeleteEntry();
     void DeleteGroup();
@@ -265,6 +267,30 @@ void FileConfigTestCase::HasGroup()
     CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
 }
 
+void FileConfigTestCase::Binary()
+{
+    wxStringInputStream sis(
+        "[root]\n"
+        "binary=Zm9vCg==\n"
+    );
+    wxFileConfig fc(sis);
+
+    wxMemoryBuffer buf;
+    fc.Read("/root/binary", &buf);
+
+    CPPUNIT_ASSERT( memcmp("foo\n", buf.GetData(), buf.GetDataLen()) == 0 );
+
+    buf.SetDataLen(0);
+    buf.AppendData("\0\1\2", 3);
+    fc.Write("/root/012", buf);
+    wxVERIFY_FILECONFIG(
+        "[root]\n"
+        "binary=Zm9vCg==\n"
+        "012=AAEC\n",
+        fc
+    );
+}
+
 void FileConfigTestCase::Save()
 {
     wxStringInputStream sis(testconfig);