several bugs fixes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@302 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 1998-07-18 21:15:37 +00:00
parent cf5f9c9c3e
commit 9fbd8b8d61
2 changed files with 89 additions and 56 deletions

View File

@ -48,6 +48,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
#define CONST_CAST ((wxFileConfig *)this)->
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// global functions declarations // global functions declarations
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -143,8 +148,6 @@ void wxFileConfig::Init()
m_linesHead = m_linesHead =
m_linesTail = NULL; m_linesTail = NULL;
m_bExpandEnvVars = TRUE;
m_strPath.Empty(); m_strPath.Empty();
} }
@ -204,14 +207,18 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal)
{ {
const char *pStart; const char *pStart;
const char *pEnd; const char *pEnd;
wxString strLine;
uint nLineCount = file.GetLineCount();
for ( uint n = 0; n < nLineCount; n++ ) {
strLine = file[n];
for ( uint n = 0; n < file.GetLineCount(); n++ ) {
// add the line to linked list // add the line to linked list
if ( bLocal ) if ( bLocal )
LineListAppend(file[n]); LineListAppend(strLine);
// skip leading spaces // skip leading spaces
for ( pStart = file[n]; isspace(*pStart); pStart++ ) for ( pStart = strLine; isspace(*pStart); pStart++ )
; ;
// skip blank/comment lines // skip blank/comment lines
@ -317,12 +324,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal)
while ( isspace(*pEnd) ) while ( isspace(*pEnd) )
pEnd++; pEnd++;
wxString strValue; pEntry->SetValue(FilterIn(pEnd), FALSE /* read from file */);
if (m_bExpandEnvVars)
strValue = ExpandEnvVars(FilterIn(pEnd));
else
strValue = FilterIn(pEnd);
pEntry->SetValue(strValue, FALSE);
} }
} }
} }
@ -349,13 +351,13 @@ void wxFileConfig::SetPath(const wxString& strPath)
if ( strPath[0] == APPCONF_PATH_SEPARATOR ) { if ( strPath[0] == APPCONF_PATH_SEPARATOR ) {
// absolute path // absolute path
SplitPath(aParts, strPath); wxSplitPath(aParts, strPath);
} }
else { else {
// relative path, combine with current one // relative path, combine with current one
wxString strFullPath = m_strPath; wxString strFullPath = m_strPath;
strFullPath << APPCONF_PATH_SEPARATOR << strPath; strFullPath << APPCONF_PATH_SEPARATOR << strPath;
SplitPath(aParts, strFullPath); wxSplitPath(aParts, strFullPath);
} }
// change current group // change current group
@ -411,6 +413,38 @@ bool wxFileConfig::GetNextEntry (wxString& str, long& lIndex)
return FALSE; return FALSE;
} }
uint wxFileConfig::GetNumberOfEntries(bool bRecursive) const
{
uint n = m_pCurrentGroup->Entries().Count();
if ( bRecursive ) {
ConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
uint nSubgroups = m_pCurrentGroup->Groups().Count();
for ( uint nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
n += GetNumberOfEntries(TRUE);
CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
}
}
return n;
}
uint wxFileConfig::GetNumberOfGroups(bool bRecursive) const
{
uint n = m_pCurrentGroup->Groups().Count();
if ( bRecursive ) {
ConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
uint nSubgroups = m_pCurrentGroup->Groups().Count();
for ( uint nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
n += GetNumberOfGroups(TRUE);
CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
}
}
return n;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// tests for existence // tests for existence
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -435,15 +469,6 @@ bool wxFileConfig::HasEntry(const wxString& strName) const
// read/write values // read/write values
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const char *wxFileConfig::Read(const char *szKey,
const char *szDefault) const
{
PathChanger path(this, szKey);
ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
return pEntry == NULL ? szDefault : pEntry->Value().c_str();
}
bool wxFileConfig::Read(wxString *pstr, bool wxFileConfig::Read(wxString *pstr,
const char *szKey, const char *szKey,
const char *szDefault) const const char *szDefault) const
@ -452,15 +477,24 @@ bool wxFileConfig::Read(wxString *pstr,
ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name()); ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
if (pEntry == NULL) { if (pEntry == NULL) {
*pstr = szDefault; *pstr = ExpandEnvVars(szDefault);
return FALSE; return FALSE;
} }
else { else {
*pstr = pEntry->Value(); *pstr = ExpandEnvVars(pEntry->Value());
return TRUE; return TRUE;
} }
} }
const char *wxFileConfig::Read(const char *szKey,
const char *szDefault) const
{
static wxString s_str;
Read(&s_str, szKey, szDefault);
return s_str.c_str();
}
bool wxFileConfig::Read(long *pl, const char *szKey, long lDefault) const bool wxFileConfig::Read(long *pl, const char *szKey, long lDefault) const
{ {
wxString str; wxString str;
@ -773,9 +807,9 @@ wxFileConfig::ConfigGroup::FindEntry(const char *szName) const
res = Stricmp(pEntry->Name(), szName); res = Stricmp(pEntry->Name(), szName);
#endif #endif
if ( res < 0 ) if ( res > 0 )
hi = i; hi = i;
else if ( res > 0 ) else if ( res < 0 )
lo = i + 1; lo = i + 1;
else else
return pEntry; return pEntry;
@ -803,9 +837,9 @@ wxFileConfig::ConfigGroup::FindSubgroup(const char *szName) const
res = Stricmp(pGroup->Name(), szName); res = Stricmp(pGroup->Name(), szName);
#endif #endif
if ( res < 0 ) if ( res > 0 )
hi = i; hi = i;
else if ( res > 0 ) else if ( res < 0 )
lo = i + 1; lo = i + 1;
else else
return pGroup; return pGroup;
@ -848,54 +882,44 @@ wxFileConfig::ConfigGroup::AddSubgroup(const wxString& strName)
bool wxFileConfig::ConfigGroup::DeleteSubgroup(const char *szName) bool wxFileConfig::ConfigGroup::DeleteSubgroup(const char *szName)
{ {
uint n, nCount = m_aSubgroups.Count(); ConfigGroup *pGroup = FindSubgroup(szName);
for ( n = 0; n < nCount; n++ ) { wxCHECK( pGroup != NULL, FALSE ); // deleting non existing group?
if ( m_aSubgroups[n]->Name().IsSameAs(szName, APPCONF_CASE_SENSITIVE) )
break;
}
if ( n == nCount ) // delete all entries
return FALSE; uint nCount = pGroup->m_aEntries.Count();
for ( uint nEntry = 0; nEntry < nCount; nEntry++ ) {
nCount = m_aEntries.Count(); LineList *pLine = pGroup->m_aEntries[nEntry]->GetLine();
for ( n = 0; n < nCount; n++ ) {
LineList *pLine = m_aEntries[n]->GetLine();
if ( pLine != NULL ) if ( pLine != NULL )
m_pConfig->LineListRemove(pLine); m_pConfig->LineListRemove(pLine);
} }
ConfigGroup *pGroup = m_aSubgroups[n];
LineList *pLine = pGroup->m_pLine; LineList *pLine = pGroup->m_pLine;
if ( pLine != NULL ) if ( pLine != NULL )
m_pConfig->LineListRemove(pLine); m_pConfig->LineListRemove(pLine);
delete pGroup;
SetDirty(); SetDirty();
m_aSubgroups.Remove(n); m_aSubgroups.Remove(pGroup);
delete pGroup;
return TRUE; return TRUE;
} }
bool wxFileConfig::ConfigGroup::DeleteEntry(const char *szName) bool wxFileConfig::ConfigGroup::DeleteEntry(const char *szName)
{ {
uint n, nCount = m_aEntries.Count(); ConfigEntry *pEntry = FindEntry(szName);
for ( n = 0; n < nCount; n++ ) { if ( pEntry == NULL )
if ( m_aEntries[n]->Name().IsSameAs(szName, APPCONF_CASE_SENSITIVE) )
break;
}
if ( n == nCount )
return FALSE; return FALSE;
ConfigEntry *pEntry = m_aEntries[n];
LineList *pLine = pEntry->GetLine(); LineList *pLine = pEntry->GetLine();
if ( pLine != NULL ) if ( pLine != NULL )
m_pConfig->LineListRemove(pLine); m_pConfig->LineListRemove(pLine);
delete pEntry;
SetDirty(); SetDirty();
m_aEntries.Remove(n); m_aEntries.Remove(pEntry);
delete pEntry;
return TRUE; return TRUE;
} }

View File

@ -282,13 +282,22 @@ void wxString::Alloc(uint nLen)
{ {
wxStringData *pData = GetStringData(); wxStringData *pData = GetStringData();
if ( pData->nAllocLength <= nLen ) { if ( pData->nAllocLength <= nLen ) {
if ( pData->IsEmpty() ) if ( pData->IsEmpty() ) {
AllocBuffer(nLen); nLen += EXTRA_ALLOC;
wxStringData* pData = (wxStringData*)
malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char));
pData->nRefs = 1;
pData->nDataLength = 0;
pData->nAllocLength = nLen;
m_pchData = pData->data(); // data starts after wxStringData
m_pchData[0u] = '\0';
}
else if ( pData->IsShared() ) { else if ( pData->IsShared() ) {
pData->Unlock(); // memory not freed because shared pData->Unlock(); // memory not freed because shared
uint nLen = pData->nDataLength; uint nOldLen = pData->nDataLength;
AllocBuffer(nLen); AllocBuffer(nLen);
memcpy(m_pchData, pData->data(), nLen*sizeof(char)); memcpy(m_pchData, pData->data(), nOldLen*sizeof(char));
} }
else { else {
nLen += EXTRA_ALLOC; nLen += EXTRA_ALLOC;