#include "wx/log.h"
#include "wx/app.h"
#include "wx/utils.h" // for wxGetHomeDir
+ #if wxUSE_STREAMS
+ #include "wx/stream.h"
+ #endif // wxUSE_STREAMS
#endif //WX_PRECOMP
#include "wx/file.h"
#include "wx/fileconf.h"
#include "wx/filefn.h"
-#if wxUSE_STREAMS
- #include "wx/stream.h"
-#endif // wxUSE_STREAMS
-
-
#if defined(__WXMAC__)
#include "wx/mac/private.h" // includes mac headers
#include "wx/filename.h" // for MacSetTypeAndCreator
return strDir;
}
-wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile)
+wxString wxFileConfig::GetGlobalFileName(const wxString& file)
{
wxString str = GetGlobalDir();
- str << szFile;
+ str << file;
- if ( wxStrchr(szFile, wxT('.')) == NULL )
+ if ( wxStrchr(file, wxT('.')) == NULL )
#if defined( __WXMAC__ )
str << wxT(" Preferences") ;
#elif defined( __UNIX__ )
return str;
}
-wxString wxFileConfig::GetLocalFileName(const wxChar *szFile)
+wxString wxFileConfig::GetLocalFileName(const wxString& file)
{
#ifdef __VMS__
// On VMS I saw the problem that the home directory was appended
str << wxT('.');
#endif
- str << szFile;
+ str << file;
#if defined(__WINDOWS__) || defined(__DOS__)
- if ( wxStrchr(szFile, wxT('.')) == NULL )
+ if ( wxStrchr(file, wxT('.')) == NULL )
str << wxT(".ini");
#endif
// ----------------------------------------------------------------------------
// ctor
// ----------------------------------------------------------------------------
+IMPLEMENT_ABSTRACT_CLASS(wxFileConfig, wxConfigBase)
void wxFileConfig::Init()
{
{
// Make up names for files if empty
if ( m_strLocalFile.empty() && (style & wxCONFIG_USE_LOCAL_FILE) )
+ {
m_strLocalFile = GetLocalFileName(GetAppName());
+#if defined(__UNIX__) && !defined(__VMS)
+ if ( style & wxCONFIG_USE_SUBDIR )
+ m_strLocalFile << wxFILE_SEP_PATH << GetAppName() << _T(".conf");
+#endif
+ }
if ( m_strGlobalFile.empty() && (style & wxCONFIG_USE_GLOBAL_FILE) )
m_strGlobalFile = GetGlobalFileName(GetAppName());
m_linesHead =
m_linesTail = NULL;
- // translate everything to the current (platform-dependent) line
- // termination character
- wxString strTrans;
+ // read the entire stream contents in memory
+ wxString str;
{
- wxString strTmp;
+ static const size_t chunkLen = 1024;
- char buf[1024];
+ wxMemoryBuffer buf(chunkLen);
do
{
- inStream.Read(buf, WXSIZEOF(buf)-1); // leave room for the NULL
+ inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen);
+ buf.UngetAppendBuf(inStream.LastRead());
const wxStreamError err = inStream.GetLastError();
wxLogError(_("Error reading config options."));
break;
}
-
- // FIXME: this is broken because if we have part of multibyte
- // character in the buffer (and another part hasn't been
- // read yet) we're going to lose data because of conversion
- // errors
- buf[inStream.LastRead()] = '\0';
- strTmp += conv.cMB2WX(buf);
}
while ( !inStream.Eof() );
- strTrans = wxTextBuffer::Translate(strTmp);
+#if wxUSE_UNICODE
+ size_t len;
+ str = conv.cMB2WC((char *)buf.GetData(), buf.GetDataLen(), &len);
+ if ( !len && buf.GetDataLen() )
+ {
+ wxLogError(_("Failed to read config options."));
+ }
+#else // !wxUSE_UNICODE
+ // no need for conversion
+ str.assign((char *)buf.GetData(), buf.GetDataLen());
+#endif // wxUSE_UNICODE/!wxUSE_UNICODE
}
+
+ // translate everything to the current (platform-dependent) line
+ // termination character
+ str = wxTextBuffer::Translate(str);
+
wxMemoryText memText;
// Now we can add the text to the memory text. To do this we extract line
const wxChar *pEOL = wxTextBuffer::GetEOL(wxTextBuffer::typeDefault);
const size_t EOLLen = wxStrlen(pEOL);
- int posLineStart = strTrans.Find(pEOL);
+ int posLineStart = str.Find(pEOL);
while ( posLineStart != -1 )
{
- wxString line(strTrans.Left(posLineStart));
+ wxString line(str.Left(posLineStart));
memText.AddLine(line);
- strTrans = strTrans.Mid(posLineStart + EOLLen);
+ str = str.Mid(posLineStart + EOLLen);
- posLineStart = strTrans.Find(pEOL);
+ posLineStart = str.Find(pEOL);
}
// also add whatever we have left in the translated string.
- if ( !strTrans.empty() )
- memText.AddLine(strTrans);
+ if ( !str.empty() )
+ memText.AddLine(str);
// Finally we can parse it all.
Parse(memText, true /* local */);
return rc;
}
-bool wxFileConfig::HasEntry(const wxString& strName) const
+bool wxFileConfig::HasEntry(const wxString& entry) const
{
- wxConfigPathChanger path(this, strName);
+ // path is the part before the last "/"
+ wxString path = entry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
- wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
- return pEntry != NULL;
+ // except in the special case of "/keyname" when there is nothing before "/"
+ if ( path.empty() && *entry.c_str() == wxCONFIG_PATH_SEPARATOR )
+ {
+ path = wxCONFIG_PATH_SEPARATOR;
+ }
+
+ // change to the path of the entry if necessary and remember the old path
+ // to restore it later
+ wxString pathOld;
+ wxFileConfig * const self = wx_const_cast(wxFileConfig *, this);
+ if ( !path.empty() )
+ {
+ pathOld = GetPath();
+ if ( pathOld.empty() )
+ pathOld = wxCONFIG_PATH_SEPARATOR;
+
+ if ( !self->DoSetPath(path, false /* don't create if doesn't exist */) )
+ {
+ return false;
+ }
+ }
+
+ // check if the entry exists in this group
+ const bool exists = m_pCurrentGroup->FindEntry(
+ entry.AfterLast(wxCONFIG_PATH_SEPARATOR)) != NULL;
+
+ // restore the old path if we changed it above
+ if ( !pathOld.empty() )
+ {
+ self->SetPath(pathOld);
+ }
+
+ return exists;
}
// ----------------------------------------------------------------------------
}
// write all strings to file
+ wxString filetext;
+ filetext.reserve(4096);
for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() )
{
- wxString line = p->Text();
- line += wxTextFile::GetEOL();
- if ( !file.Write(line, *m_conv) )
- {
- wxLogError(_("can't write user configuration file."));
- return false;
- }
+ filetext << p->Text() << wxTextFile::GetEOL();
+ }
+
+ if ( !file.Write(filetext, *m_conv) )
+ {
+ wxLogError(_("can't write user configuration file."));
+ return false;
}
if ( !file.Commit() )
bool wxFileConfig::DeleteGroup(const wxString& key)
{
- wxConfigPathChanger path(this, key);
+ wxConfigPathChanger path(this, RemoveTrailingSeparator(key));
if ( !m_pCurrentGroup->DeleteSubgroupByName(path.Name()) )
return false;
+ path.UpdateIfDeleted();
+
SetDirty();
return true;
str.c_str() );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
wxFileConfigLineList *pLine = new wxFileConfigLineList(str);
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
return m_linesTail;
}
wxLogTrace( FILECONF_TRACE_MASK,
_T(" ** Inserting Line '%s' after '%s'"),
str.c_str(),
- ((pLine) ? pLine->Text().c_str() : wxEmptyString) );
+ ((pLine) ? (const wxChar*)pLine->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
if ( pLine == m_linesTail )
return LineListAppend(str);
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
return pNewLine;
}
pLine->Text().c_str() );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
wxFileConfigLineList *pPrev = pLine->Prev(),
*pNext = pLine->Next();
wxLogTrace( FILECONF_TRACE_MASK,
_T(" head: %s"),
- ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
+ ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str()
+ : wxEmptyString) );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" tail: %s"),
- ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
+ ((m_linesTail) ? (const wxChar*)m_linesTail->Text().c_str()
+ : wxEmptyString) );
delete pLine;
}
{
wxCHECK_RET( m_pParent, _T("the root group can't be renamed") );
+ if ( newName == m_strName )
+ return;
+
+ // we need to remove the group from the parent and it back under the new
+ // name to keep the parents array of subgroups alphabetically sorted
+ m_pParent->m_aSubgroups.Remove(this);
+
m_strName = newName;
+ m_pParent->m_aSubgroups.Add(this);
+
// update the group lines recursively
UpdateGroupAndSubgroupsLines();
}
wxLogTrace( FILECONF_TRACE_MASK,
_T(" (m_pLine) = prev: %p, this %p, next %p"),
- ((m_pLine) ? m_pLine->Prev() : 0),
- m_pLine,
- ((m_pLine) ? m_pLine->Next() : 0) );
+ m_pLine ? wx_static_cast(void*, m_pLine->Prev()) : 0,
+ wx_static_cast(void*, m_pLine),
+ m_pLine ? wx_static_cast(void*, m_pLine->Next()) : 0 );
wxLogTrace( FILECONF_TRACE_MASK,
_T(" text: '%s'"),
- ((m_pLine) ? m_pLine->Text().c_str() : wxEmptyString) );
+ m_pLine ? (const wxChar*)m_pLine->Text().c_str()
+ : wxEmptyString );
// delete all entries...
size_t nCount = pGroup->m_aEntries.Count();
wxLogTrace( FILECONF_TRACE_MASK,
_T(" Removing from group '%s' : '%s'"),
Name().c_str(),
- ((m_pLine) ? m_pLine->Text().c_str() : wxEmptyString) );
+ ((m_pLine) ? (const wxChar*)m_pLine->Text().c_str()
+ : wxEmptyString) );
// notice that we may do this test inside the previous "if"
// because the last entry's line is surely !NULL
for ( size_t n = bQuoted ? 1 : 0; n < str.Len(); n++ ) {
if ( str[n] == wxT('\\') ) {
- switch ( str[++n] ) {
+ switch ( str[++n].GetValue() ) {
case wxT('n'):
strResult += wxT('\n');
break;
wxChar c;
for ( size_t n = 0; n < str.Len(); n++ ) {
- switch ( str[n] ) {
+ switch ( str[n].GetValue() ) {
case wxT('\n'):
c = wxT('n');
break;