added support for binary data to wxConfig (slightly modified patch 1736788)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47353 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-07-12 00:11:03 +00:00
parent 84ebfcbd18
commit 5814e8ba4e
11 changed files with 136 additions and 0 deletions

View File

@@ -109,6 +109,7 @@ All:
- Added wxStreamBuffer::Truncate() (Stas Sergeev) - Added wxStreamBuffer::Truncate() (Stas Sergeev)
- Allow using wxEventLoop in console applications (Lukasz Michalski) - Allow using wxEventLoop in console applications (Lukasz Michalski)
- Added functions for Base64 en/decoding (Charles Reimers) - 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) - Added functions for atomically inc/decrementing integers (Armel Asselin)
- wxLogInterposer has been added to replace wxLogPassThrough and new - wxLogInterposer has been added to replace wxLogPassThrough and new
wxLogInterposerTemp was added wxLogInterposerTemp was added

View 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 Reads a bool value, returning \true if the value was found. If the value was
not found, {\it defaultVal} is used instead. 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 \pythonnote{In place of a single overloaded method name, wxPython
implements the following methods:\par implements the following methods:\par
\indented{2cm}{\begin{twocollist} \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{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. 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 \pythonnote{In place of a single overloaded method name, wxPython

View File

@@ -17,6 +17,7 @@
#include "wx/defs.h" #include "wx/defs.h"
#include "wx/string.h" #include "wx/string.h"
#include "wx/object.h" #include "wx/object.h"
#include "wx/base64.h"
class WXDLLIMPEXP_FWD_BASE wxArrayString; 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) const;
bool Read(const wxString& key, bool* val, bool defVal) 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 // 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) // int/double/bool as there would be ambiguities with the long one then)
wxString Read(const wxString& key, wxString Read(const wxString& key,
@@ -202,6 +208,9 @@ public:
bool Write(const wxString& key, bool value) bool Write(const wxString& key, bool value)
{ return DoWriteBool(key, 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 // we have to provide a separate version for C strings as otherwise they
// would be converted to bool and not to wxString as expected! // would be converted to bool and not to wxString as expected!
bool Write(const wxString& key, const char *value) bool Write(const wxString& key, const char *value)
@@ -273,12 +282,14 @@ protected:
virtual bool DoReadInt(const wxString& key, int *pi) const; virtual bool DoReadInt(const wxString& key, int *pi) const;
virtual bool DoReadDouble(const wxString& key, double* val) const; virtual bool DoReadDouble(const wxString& key, double* val) const;
virtual bool DoReadBool(const wxString& key, bool* 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 DoWriteString(const wxString& key, const wxString& value) = 0;
virtual bool DoWriteLong(const wxString& key, long value) = 0; virtual bool DoWriteLong(const wxString& key, long value) = 0;
virtual bool DoWriteInt(const wxString& key, int value); virtual bool DoWriteInt(const wxString& key, int value);
virtual bool DoWriteDouble(const wxString& key, double value); virtual bool DoWriteDouble(const wxString& key, double value);
virtual bool DoWriteBool(const wxString& key, bool value); virtual bool DoWriteBool(const wxString& key, bool value);
virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) = 0;
private: private:
// are we doing automatic environment variable expansion? // are we doing automatic environment variable expansion?

View File

@@ -194,9 +194,11 @@ public:
protected: protected:
virtual bool DoReadString(const wxString& key, wxString *pStr) const; virtual bool DoReadString(const wxString& key, wxString *pStr) const;
virtual bool DoReadLong(const wxString& key, long *pl) 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 DoWriteString(const wxString& key, const wxString& szValue);
virtual bool DoWriteLong(const wxString& key, long lValue); virtual bool DoWriteLong(const wxString& key, long lValue);
virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
private: private:
// GetXXXFileName helpers: return ('/' terminated) directory names // GetXXXFileName helpers: return ('/' terminated) directory names

View File

@@ -78,9 +78,11 @@ protected:
// read/write // read/write
bool DoReadString(const wxString& key, wxString *pStr) const; bool DoReadString(const wxString& key, wxString *pStr) const;
bool DoReadLong(const wxString& key, long *plResult) 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 DoWriteString(const wxString& key, const wxString& szValue);
bool DoWriteLong(const wxString& key, long lValue); bool DoWriteLong(const wxString& key, long lValue);
bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
private: private:
// helpers // helpers

View File

@@ -18,6 +18,7 @@
#include "wx/object.h" #include "wx/object.h"
#include "wx/confbase.h" #include "wx/confbase.h"
#include "buffer.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxRegConfig // wxRegConfig
@@ -90,9 +91,11 @@ protected:
// implement read/write methods // implement read/write methods
virtual bool DoReadString(const wxString& key, wxString *pStr) const; virtual bool DoReadString(const wxString& key, wxString *pStr) const;
virtual bool DoReadLong(const wxString& key, long *plResult) 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 DoWriteString(const wxString& key, const wxString& szValue);
virtual bool DoWriteLong(const wxString& key, long lValue); virtual bool DoWriteLong(const wxString& key, long lValue);
virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
private: private:
// these keys are opened during all lifetime of wxRegConfig object // these keys are opened during all lifetime of wxRegConfig object

View File

@@ -67,9 +67,11 @@ protected:
// implement read/write methods // implement read/write methods
virtual bool DoReadString(const wxString& key, wxString *pStr) const; virtual bool DoReadString(const wxString& key, wxString *pStr) const;
virtual bool DoReadLong(const wxString& key, long *plResult) 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 DoWriteString(const wxString& key, const wxString& szValue);
virtual bool DoWriteLong(const wxString& key, long lValue); virtual bool DoWriteLong(const wxString& key, long lValue);
virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
private: private:
// current path (not '/' terminated) // current path (not '/' terminated)

View File

@@ -42,6 +42,8 @@
#include "wx/fileconf.h" #include "wx/fileconf.h"
#include "wx/filefn.h" #include "wx/filefn.h"
#include "wx/base64.h"
#include "wx/stdpaths.h" #include "wx/stdpaths.h"
#if defined(__WXMAC__) #if defined(__WXMAC__)
@@ -916,6 +918,18 @@ bool wxFileConfig::DoReadLong(const wxString& key, long *pl) const
return str.ToLong(pl); 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) bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue)
{ {
wxConfigPathChanger path(this, key); wxConfigPathChanger path(this, key);
@@ -981,6 +995,11 @@ bool wxFileConfig::DoWriteLong(const wxString& key, long lValue)
return Write(key, wxString::Format(_T("%ld"), 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 */) bool wxFileConfig::Flush(bool /* bCurrentOnly */)
{ {
if ( !IsDirty() || !m_fnLocalFile.GetFullPath() ) if ( !IsDirty() || !m_fnLocalFile.GetFullPath() )

View 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); 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 // implementation
// ============================================================================ // ============================================================================
@@ -620,6 +625,40 @@ bool wxRegConfig::DoReadLong(const wxString& key, long *plResult) const
return false; 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) bool wxRegConfig::DoWriteString(const wxString& key, const wxString& szValue)
{ {
wxConfigPathChanger path(this, key); wxConfigPathChanger path(this, key);
@@ -644,6 +683,18 @@ bool wxRegConfig::DoWriteLong(const wxString& key, long lValue)
return LocalKey().SetValue(path.Name(), 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 // renaming
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -178,6 +178,12 @@ bool wxPrefConfig::DoReadLong(const wxString& key, long *plResult) const
return false; return false;
} }
bool DoReadBinary(const wxString& key, wxMemoryBuffer *buf) const
{
/* TODO */
return false;
}
bool wxPrefConfig::DoWriteString(const wxString& key, const wxString& szValue) bool wxPrefConfig::DoWriteString(const wxString& key, const wxString& szValue)
{ {
/* TODO */ /* TODO */
@@ -190,6 +196,12 @@ bool wxPrefConfig::DoWriteLong(const wxString& key, long lValue)
return false; return false;
} }
bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
{
/* TODO */
return false;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// renaming // renaming
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -67,6 +67,7 @@ private:
CPPUNIT_TEST( GetGroups ); CPPUNIT_TEST( GetGroups );
CPPUNIT_TEST( HasEntry ); CPPUNIT_TEST( HasEntry );
CPPUNIT_TEST( HasGroup ); CPPUNIT_TEST( HasGroup );
CPPUNIT_TEST( Binary );
CPPUNIT_TEST( Save ); CPPUNIT_TEST( Save );
CPPUNIT_TEST( DeleteEntry ); CPPUNIT_TEST( DeleteEntry );
CPPUNIT_TEST( DeleteGroup ); CPPUNIT_TEST( DeleteGroup );
@@ -85,6 +86,7 @@ private:
void GetGroups(); void GetGroups();
void HasEntry(); void HasEntry();
void HasGroup(); void HasGroup();
void Binary();
void Save(); void Save();
void DeleteEntry(); void DeleteEntry();
void DeleteGroup(); void DeleteGroup();
@@ -265,6 +267,30 @@ void FileConfigTestCase::HasGroup()
CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) ); 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() void FileConfigTestCase::Save()
{ {
wxStringInputStream sis(testconfig); wxStringInputStream sis(testconfig);