1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxFileConfig derivation of wxConfig
4 // Author: Vadim Zeitlin
6 // Created: 07.04.98 (adapted from appconf.cpp)
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "fileconf.h"
17 // ============================================================================
19 // ============================================================================
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
24 #include "wx/wxprec.h"
31 #include <wx/string.h>
36 #include <wx/dynarray.h>
39 #include <wx/textfile.h>
40 #include <wx/config.h>
41 #include <wx/fileconf.h>
43 // _WINDOWS_ is defined when windows.h is included,
44 // __WXMSW__ is defined for MS Windows compilation
45 #if defined(__WXMSW__) && !defined(_WINDOWS_)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
55 #define CONST_CAST ((wxFileConfig *)this)->
57 // ----------------------------------------------------------------------------
58 // global functions declarations
59 // ----------------------------------------------------------------------------
61 // is 'c' a valid character in group name?
62 // NB: wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR must be valid chars,
63 // but _not_ ']' (group name delimiter)
64 inline bool IsValid(char c
) { return isalnum(c
) || strchr("@_/-!.*%", c
); }
66 // compare functions for sorting the arrays
67 static int CompareEntries(wxFileConfig::ConfigEntry
*p1
,
68 wxFileConfig::ConfigEntry
*p2
);
69 static int CompareGroups(wxFileConfig::ConfigGroup
*p1
,
70 wxFileConfig::ConfigGroup
*p2
);
73 static wxString
FilterIn(const wxString
& str
);
74 static wxString
FilterOut(const wxString
& str
);
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
83 wxString
wxFileConfig::GetGlobalDir()
89 #elif defined(__WXSTUBS__)
97 char szWinDir
[_MAX_PATH
];
98 ::GetWindowsDirectory(szWinDir
, _MAX_PATH
);
102 #endif // Unix/Windows
107 wxString
wxFileConfig::GetLocalDir()
112 const char *szHome
= getenv("HOME");
113 if ( szHome
== NULL
) {
115 wxLogWarning(_("can't find user's HOME, using current directory."));
120 strDir
<< '/'; // a double slash is no problem, a missin one yes
123 const char *szHome
= getenv("HOMEDRIVE");
124 if ( szHome
!= NULL
)
126 szHome
= getenv("HOMEPATH");
127 if ( szHome
!= NULL
)
130 // Win16 has no idea about home, so use the current directory instead
138 wxString
wxFileConfig::GetGlobalFileName(const char *szFile
)
140 wxString str
= GetGlobalDir();
143 if ( strchr(szFile
, '.') == NULL
)
153 wxString
wxFileConfig::GetLocalFileName(const char *szFile
)
155 wxString str
= GetLocalDir();
164 if ( strchr(szFile
, '.') == NULL
)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void wxFileConfig::Init()
178 m_pRootGroup
= new ConfigGroup(NULL
, "", this);
183 // it's not an error if (one of the) file(s) doesn't exist
185 // parse the global file
186 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) ) {
187 wxTextFile
fileGlobal(m_strGlobalFile
);
189 if ( fileGlobal
.Open() ) {
190 Parse(fileGlobal
, FALSE
/* global */);
194 wxLogWarning(_("can't open global configuration file '%s'."),
195 m_strGlobalFile
.c_str());
198 // parse the local file
199 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) ) {
200 wxTextFile
fileLocal(m_strLocalFile
);
201 if ( fileLocal
.Open() ) {
202 Parse(fileLocal
, TRUE
/* local */);
206 wxLogWarning(_("can't open user configuration file '%s'."),
207 m_strLocalFile
.c_str());
212 wxFileConfig::wxFileConfig(const char *szAppName
, bool bLocalOnly
)
214 wxASSERT( !IsEmpty(szAppName
) ); // invent a name for your application!
216 m_strLocalFile
= GetLocalFileName(szAppName
);
218 m_strGlobalFile
= GetGlobalFileName(szAppName
);
219 //else: it's going to be empty and we won't use the global file
224 wxFileConfig::wxFileConfig(const wxString
& strLocal
, const wxString
& strGlobal
)
225 : m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
227 // if the path is not absolute, prepend the standard directory to it
228 if ( !strLocal
.IsEmpty() && !wxIsAbsolutePath(strLocal
) )
230 m_strLocalFile
= GetLocalDir();
231 m_strLocalFile
<< strLocal
;
234 if ( !strGlobal
.IsEmpty() && !wxIsAbsolutePath(strGlobal
) )
236 m_strGlobalFile
= GetGlobalDir();
237 m_strGlobalFile
<< strGlobal
;
244 // New-style constructor
245 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
246 const wxString
& strLocal
, const wxString
& strGlobal
, long style
)
247 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
),
248 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
250 // Make up an application name if not supplied
251 if (appName
.IsEmpty() && wxTheApp
)
253 SetAppName(wxTheApp
->GetAppName());
256 // Make up names for files if empty
257 if (m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) && wxTheApp
)
259 m_strLocalFile
= wxTheApp
->GetAppName();
262 if (m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
))
264 // TODO: What should the default global filename be?
265 m_strGlobalFile
= "global";
268 // Check if styles are not supplied, but filenames are, in which case
269 // add the correct styles.
270 if (!m_strLocalFile
.IsEmpty() && ((style
& wxCONFIG_USE_LOCAL_FILE
) != wxCONFIG_USE_LOCAL_FILE
))
271 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
273 if (!m_strGlobalFile
.IsEmpty() && ((style
& wxCONFIG_USE_GLOBAL_FILE
) != wxCONFIG_USE_GLOBAL_FILE
))
274 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
276 // if the path is not absolute, prepend the standard directory to it
277 if ( !strLocal
.IsEmpty() && !wxIsAbsolutePath(strLocal
) )
279 m_strLocalFile
= GetLocalDir();
280 m_strLocalFile
<< strLocal
;
283 if ( !strGlobal
.IsEmpty() && !wxIsAbsolutePath(strGlobal
) )
285 m_strGlobalFile
= GetGlobalDir();
286 m_strGlobalFile
<< strGlobal
;
292 void wxFileConfig::CleanUp()
296 LineList
*pCur
= m_linesHead
;
297 while ( pCur
!= NULL
) {
298 LineList
*pNext
= pCur
->Next();
304 wxFileConfig::~wxFileConfig()
311 // ----------------------------------------------------------------------------
312 // parse a config file
313 // ----------------------------------------------------------------------------
315 void wxFileConfig::Parse(wxTextFile
& file
, bool bLocal
)
321 size_t nLineCount
= file
.GetLineCount();
322 for ( size_t n
= 0; n
< nLineCount
; n
++ ) {
325 // add the line to linked list
327 LineListAppend(strLine
);
329 // skip leading spaces
330 for ( pStart
= strLine
; isspace(*pStart
); pStart
++ )
333 // skip blank/comment lines
334 if ( *pStart
== '\0'|| *pStart
== ';' || *pStart
== '#' )
337 if ( *pStart
== '[' ) { // a new group
340 while ( *++pEnd
!= ']' ) {
341 if ( !IsValid(*pEnd
) && *pEnd
!= ' ' ) // allow spaces in group names
345 if ( *pEnd
!= ']' ) {
346 wxLogError(_("file '%s': unexpected character %c at line %d."),
347 file
.GetName(), *pEnd
, n
+ 1);
348 continue; // skip this line
351 // group name here is always considered as abs path
354 strGroup
<< wxCONFIG_PATH_SEPARATOR
<< wxString(pStart
, pEnd
- pStart
);
356 // will create it if doesn't yet exist
360 m_pCurrentGroup
->SetLine(m_linesTail
);
362 // check that there is nothing except comments left on this line
364 while ( *++pEnd
!= '\0' && bCont
) {
373 // ignore whitespace ('\n' impossible here)
377 wxLogWarning(_("file '%s', line %d: '%s' "
378 "ignored after group header."),
379 file
.GetName(), n
+ 1, pEnd
);
385 const char *pEnd
= pStart
;
386 while ( IsValid(*pEnd
) )
389 wxString
strKey(pStart
, pEnd
);
392 while ( isspace(*pEnd
) )
395 if ( *pEnd
++ != '=' ) {
396 wxLogError(_("file '%s', line %d: '=' expected."),
397 file
.GetName(), n
+ 1);
400 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
402 if ( pEntry
== NULL
) {
404 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
407 pEntry
->SetLine(m_linesTail
);
410 if ( bLocal
&& pEntry
->IsImmutable() ) {
411 // immutable keys can't be changed by user
412 wxLogWarning(_("file '%s', line %d: value for "
413 "immutable key '%s' ignored."),
414 file
.GetName(), n
+ 1, strKey
.c_str());
417 // the condition below catches the cases (a) and (b) but not (c):
418 // (a) global key found second time in global file
419 // (b) key found second (or more) time in local file
420 // (c) key from global file now found in local one
421 // which is exactly what we want.
422 else if ( !bLocal
|| pEntry
->IsLocal() ) {
423 wxLogWarning(_("file '%s', line %d: key '%s' was first "
424 "found at line %d."),
425 file
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
428 pEntry
->SetLine(m_linesTail
);
433 while ( isspace(*pEnd
) )
436 pEntry
->SetValue(FilterIn(pEnd
), FALSE
/* read from file */);
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 void wxFileConfig::SetRootPath()
449 m_pCurrentGroup
= m_pRootGroup
;
452 void wxFileConfig::SetPath(const wxString
& strPath
)
454 wxArrayString aParts
;
456 if ( strPath
.IsEmpty() ) {
461 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
463 wxSplitPath(aParts
, strPath
);
466 // relative path, combine with current one
467 wxString strFullPath
= m_strPath
;
468 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
469 wxSplitPath(aParts
, strFullPath
);
472 // change current group
474 m_pCurrentGroup
= m_pRootGroup
;
475 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
476 ConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
477 if ( pNextGroup
== NULL
)
478 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
479 m_pCurrentGroup
= pNextGroup
;
482 // recombine path parts in one variable
484 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
485 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
489 // ----------------------------------------------------------------------------
491 // ----------------------------------------------------------------------------
493 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
496 return GetNextGroup(str
, lIndex
);
499 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
501 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
502 str
= m_pCurrentGroup
->Groups()[lIndex
++]->Name();
509 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
512 return GetNextEntry(str
, lIndex
);
515 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
517 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
518 str
= m_pCurrentGroup
->Entries()[lIndex
++]->Name();
525 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
527 size_t n
= m_pCurrentGroup
->Entries().Count();
529 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
530 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
531 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
532 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
533 n
+= GetNumberOfEntries(TRUE
);
534 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
541 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
543 size_t n
= m_pCurrentGroup
->Groups().Count();
545 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
546 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
547 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
548 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
549 n
+= GetNumberOfGroups(TRUE
);
550 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
557 // ----------------------------------------------------------------------------
558 // tests for existence
559 // ----------------------------------------------------------------------------
561 bool wxFileConfig::HasGroup(const wxString
& strName
) const
563 wxConfigPathChanger
path(this, strName
);
565 ConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
566 return pGroup
!= NULL
;
569 bool wxFileConfig::HasEntry(const wxString
& strName
) const
571 wxConfigPathChanger
path(this, strName
);
573 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
574 return pEntry
!= NULL
;
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 bool wxFileConfig::Read(const wxString
& key
,
582 wxString
* pStr
) const
584 wxConfigPathChanger
path(this, key
);
586 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
587 if (pEntry
== NULL
) {
591 *pStr
= ExpandEnvVars(pEntry
->Value());
596 bool wxFileConfig::Read(const wxString
& key
,
597 wxString
* pStr
, const wxString
& defVal
) const
599 wxConfigPathChanger
path(this, key
);
601 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
602 if (pEntry
== NULL
) {
603 if( IsRecordingDefaults() )
604 ((wxFileConfig
*)this)->Write(key
,defVal
);
605 *pStr
= ExpandEnvVars(defVal
);
609 *pStr
= ExpandEnvVars(pEntry
->Value());
614 bool wxFileConfig::Read(const wxString
& key
, long *pl
) const
617 if ( Read(key
, & str
) ) {
626 bool wxFileConfig::Write(const wxString
& key
, const wxString
& szValue
)
628 wxConfigPathChanger
path(this, key
);
630 wxString strName
= path
.Name();
631 if ( strName
.IsEmpty() ) {
632 // setting the value of a group is an error
633 wxASSERT_MSG( IsEmpty(szValue
), _("can't set value of a group!") );
635 // ... except if it's empty in which case it's a way to force it's creation
636 m_pCurrentGroup
->SetDirty();
638 // this will add a line for this group if it didn't have it before
639 (void)m_pCurrentGroup
->GetGroupLine();
644 // check that the name is reasonable
645 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
) {
646 wxLogError(_("Entry name can't start with '%c'."),
647 wxCONFIG_IMMUTABLE_PREFIX
);
651 for ( const char *pc
= strName
; *pc
!= '\0'; pc
++ ) {
652 if ( !IsValid(*pc
) ) {
653 wxLogError(_("Character '%c' is invalid in a config entry name."),
659 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
660 if ( pEntry
== NULL
)
661 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
663 pEntry
->SetValue(szValue
);
669 bool wxFileConfig::Write(const wxString
& key
, long lValue
)
671 // ltoa() is not ANSI :-(
672 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
673 sprintf(szBuf
, "%ld", lValue
);
674 return Write(key
, szBuf
);
677 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
679 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() )
682 wxTempFile
file(m_strLocalFile
);
684 if ( !file
.IsOpened() ) {
685 wxLogError(_("can't open user configuration file."));
689 // write all strings to file
690 for ( LineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() ) {
691 if ( !file
.Write(p
->Text() + wxTextFile::GetEOL()) ) {
692 wxLogError(_("can't write user configuration file."));
697 return file
.Commit();
700 // ----------------------------------------------------------------------------
701 // delete groups/entries
702 // ----------------------------------------------------------------------------
704 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
706 wxConfigPathChanger
path(this, key
);
708 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
711 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
712 if ( m_pCurrentGroup
!= m_pRootGroup
) {
713 ConfigGroup
*pGroup
= m_pCurrentGroup
;
714 SetPath(".."); // changes m_pCurrentGroup!
715 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
717 //else: never delete the root group
723 bool wxFileConfig::DeleteGroup(const wxString
& key
)
725 wxConfigPathChanger
path(this, key
);
727 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
730 bool wxFileConfig::DeleteAll()
734 const char *szFile
= m_strLocalFile
;
736 if ( remove(szFile
) == -1 )
737 wxLogSysError(_("can't delete user configuration file '%s'"), szFile
);
739 m_strLocalFile
= m_strGlobalFile
= "";
745 // ----------------------------------------------------------------------------
746 // linked list functions
747 // ----------------------------------------------------------------------------
749 // append a new line to the end of the list
750 wxFileConfig::LineList
*wxFileConfig::LineListAppend(const wxString
& str
)
752 LineList
*pLine
= new LineList(str
);
754 if ( m_linesTail
== NULL
) {
760 m_linesTail
->SetNext(pLine
);
761 pLine
->SetPrev(m_linesTail
);
768 // insert a new line after the given one or in the very beginning if !pLine
769 wxFileConfig::LineList
*wxFileConfig::LineListInsert(const wxString
& str
,
772 if ( pLine
== m_linesTail
)
773 return LineListAppend(str
);
775 LineList
*pNewLine
= new LineList(str
);
776 if ( pLine
== NULL
) {
777 // prepend to the list
778 pNewLine
->SetNext(m_linesHead
);
779 m_linesHead
->SetPrev(pNewLine
);
780 m_linesHead
= pNewLine
;
783 // insert before pLine
784 LineList
*pNext
= pLine
->Next();
785 pNewLine
->SetNext(pNext
);
786 pNewLine
->SetPrev(pLine
);
787 pNext
->SetPrev(pNewLine
);
788 pLine
->SetNext(pNewLine
);
794 void wxFileConfig::LineListRemove(LineList
*pLine
)
796 LineList
*pPrev
= pLine
->Prev(),
797 *pNext
= pLine
->Next();
803 pPrev
->SetNext(pNext
);
809 pNext
->SetPrev(pPrev
);
814 bool wxFileConfig::LineListIsEmpty()
816 return m_linesHead
== NULL
;
819 // ============================================================================
820 // wxFileConfig::ConfigGroup
821 // ============================================================================
823 // ----------------------------------------------------------------------------
825 // ----------------------------------------------------------------------------
828 wxFileConfig::ConfigGroup::ConfigGroup(wxFileConfig::ConfigGroup
*pParent
,
829 const wxString
& strName
,
830 wxFileConfig
*pConfig
)
831 : m_aEntries(CompareEntries
),
832 m_aSubgroups(CompareGroups
),
844 // dtor deletes all children
845 wxFileConfig::ConfigGroup::~ConfigGroup()
848 size_t n
, nCount
= m_aEntries
.Count();
849 for ( n
= 0; n
< nCount
; n
++ )
850 delete m_aEntries
[n
];
853 nCount
= m_aSubgroups
.Count();
854 for ( n
= 0; n
< nCount
; n
++ )
855 delete m_aSubgroups
[n
];
858 // ----------------------------------------------------------------------------
860 // ----------------------------------------------------------------------------
862 void wxFileConfig::ConfigGroup::SetLine(LineList
*pLine
)
864 wxASSERT( m_pLine
== NULL
); // shouldn't be called twice
870 This is a bit complicated, so let me explain it in details. All lines that
871 were read from the local file (the only one we will ever modify) are stored
872 in a (doubly) linked list. Our problem is to know at which position in this
873 list should we insert the new entries/subgroups. To solve it we keep three
874 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
876 m_pLine points to the line containing "[group_name]"
877 m_pLastEntry points to the last entry of this group in the local file.
878 m_pLastGroup subgroup
880 Initially, they're NULL all three. When the group (an entry/subgroup) is read
881 from the local file, the corresponding variable is set. However, if the group
882 was read from the global file and then modified or created by the application
883 these variables are still NULL and we need to create the corresponding lines.
884 See the following functions (and comments preceding them) for the details of
887 Also, when our last entry/group are deleted we need to find the new last
888 element - the code in DeleteEntry/Subgroup does this by backtracking the list
889 of lines until it either founds an entry/subgroup (and this is the new last
890 element) or the m_pLine of the group, in which case there are no more entries
891 (or subgroups) left and m_pLast<element> becomes NULL.
893 NB: This last problem could be avoided for entries if we added new entries
894 immediately after m_pLine, but in this case the entries would appear
895 backwards in the config file (OTOH, it's not that important) and as we
896 would still need to do it for the subgroups the code wouldn't have been
897 significantly less complicated.
900 // Return the line which contains "[our name]". If we're still not in the list,
901 // add our line to it immediately after the last line of our parent group if we
902 // have it or in the very beginning if we're the root group.
903 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetGroupLine()
905 if ( m_pLine
== NULL
) {
906 ConfigGroup
*pParent
= Parent();
908 // this group wasn't present in local config file, add it now
909 if ( pParent
!= NULL
) {
910 wxString strFullName
;
911 strFullName
<< "[" << GetFullName().c_str() + 1 << "]"; // +1: no '/'
912 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
913 pParent
->GetLastGroupLine());
914 pParent
->SetLastGroup(this); // we're surely after all the others
917 // we return NULL, so that LineListInsert() will insert us in the
925 // Return the last line belonging to the subgroups of this group (after which
926 // we can add a new subgroup), if we don't have any subgroups or entries our
927 // last line is the group line (m_pLine) itself.
928 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetLastGroupLine()
930 // if we have any subgroups, our last line is the last line of the last
932 if ( m_pLastGroup
!= NULL
) {
933 wxFileConfig::LineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
935 wxASSERT( pLine
!= NULL
); // last group must have !NULL associated line
939 // no subgroups, so the last line is the line of thelast entry (if any)
940 return GetLastEntryLine();
943 // return the last line belonging to the entries of this group (after which
944 // we can add a new entry), if we don't have any entries we will add the new
945 // one immediately after the group line itself.
946 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetLastEntryLine()
948 if ( m_pLastEntry
!= NULL
) {
949 wxFileConfig::LineList
*pLine
= m_pLastEntry
->GetLine();
951 wxASSERT( pLine
!= NULL
); // last entry must have !NULL associated line
955 // no entries: insert after the group header
956 return GetGroupLine();
959 // ----------------------------------------------------------------------------
961 // ----------------------------------------------------------------------------
963 wxString
wxFileConfig::ConfigGroup::GetFullName() const
966 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
971 // ----------------------------------------------------------------------------
973 // ----------------------------------------------------------------------------
975 // use binary search because the array is sorted
976 wxFileConfig::ConfigEntry
*
977 wxFileConfig::ConfigGroup::FindEntry(const char *szName
) const
981 hi
= m_aEntries
.Count();
983 wxFileConfig::ConfigEntry
*pEntry
;
987 pEntry
= m_aEntries
[i
];
989 #if wxCONFIG_CASE_SENSITIVE
990 res
= strcmp(pEntry
->Name(), szName
);
992 res
= Stricmp(pEntry
->Name(), szName
);
1006 wxFileConfig::ConfigGroup
*
1007 wxFileConfig::ConfigGroup::FindSubgroup(const char *szName
) const
1011 hi
= m_aSubgroups
.Count();
1013 wxFileConfig::ConfigGroup
*pGroup
;
1017 pGroup
= m_aSubgroups
[i
];
1019 #if wxCONFIG_CASE_SENSITIVE
1020 res
= strcmp(pGroup
->Name(), szName
);
1022 res
= Stricmp(pGroup
->Name(), szName
);
1036 // ----------------------------------------------------------------------------
1037 // create a new item
1038 // ----------------------------------------------------------------------------
1040 // create a new entry and add it to the current group
1041 wxFileConfig::ConfigEntry
*
1042 wxFileConfig::ConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1044 wxASSERT( FindEntry(strName
) == NULL
);
1046 ConfigEntry
*pEntry
= new ConfigEntry(this, strName
, nLine
);
1047 m_aEntries
.Add(pEntry
);
1052 // create a new group and add it to the current group
1053 wxFileConfig::ConfigGroup
*
1054 wxFileConfig::ConfigGroup::AddSubgroup(const wxString
& strName
)
1056 wxASSERT( FindSubgroup(strName
) == NULL
);
1058 ConfigGroup
*pGroup
= new ConfigGroup(this, strName
, m_pConfig
);
1059 m_aSubgroups
.Add(pGroup
);
1064 // ----------------------------------------------------------------------------
1066 // ----------------------------------------------------------------------------
1069 The delete operations are _very_ slow if we delete the last item of this
1070 group (see comments before GetXXXLineXXX functions for more details),
1071 so it's much better to start with the first entry/group if we want to
1072 delete several of them.
1075 bool wxFileConfig::ConfigGroup::DeleteSubgroupByName(const char *szName
)
1077 return DeleteSubgroup(FindSubgroup(szName
));
1080 // doesn't delete the subgroup itself, but does remove references to it from
1081 // all other data structures (and normally the returned pointer should be
1082 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1083 bool wxFileConfig::ConfigGroup::DeleteSubgroup(ConfigGroup
*pGroup
)
1085 wxCHECK( pGroup
!= NULL
, FALSE
); // deleting non existing group?
1087 // delete all entries
1088 size_t nCount
= pGroup
->m_aEntries
.Count();
1089 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ ) {
1090 LineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1091 if ( pLine
!= NULL
)
1092 m_pConfig
->LineListRemove(pLine
);
1095 // and subgroups of this sungroup
1096 nCount
= pGroup
->m_aSubgroups
.Count();
1097 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ ) {
1098 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[nGroup
]);
1101 LineList
*pLine
= pGroup
->m_pLine
;
1102 if ( pLine
!= NULL
) {
1103 // notice that we may do this test inside the previous "if" because the
1104 // last entry's line is surely !NULL
1105 if ( pGroup
== m_pLastGroup
) {
1106 // our last entry is being deleted - find the last one which stays
1107 wxASSERT( m_pLine
!= NULL
); // we have a subgroup with !NULL pLine...
1109 // go back until we find a subgroup or reach the group's line
1110 ConfigGroup
*pNewLast
= NULL
;
1111 size_t n
, nSubgroups
= m_aSubgroups
.Count();
1113 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1114 // is it our subgroup?
1115 for ( n
= 0; (pNewLast
== NULL
) && (n
< nSubgroups
); n
++ ) {
1116 // do _not_ call GetGroupLine! we don't want to add it to the local
1117 // file if it's not already there
1118 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1119 pNewLast
= m_aSubgroups
[n
];
1122 if ( pNewLast
!= NULL
) // found?
1126 if ( pl
== m_pLine
) {
1127 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1129 // we've reached the group line without finding any subgroups
1130 m_pLastGroup
= NULL
;
1133 m_pLastGroup
= pNewLast
;
1136 m_pConfig
->LineListRemove(pLine
);
1141 m_aSubgroups
.Remove(pGroup
);
1147 bool wxFileConfig::ConfigGroup::DeleteEntry(const char *szName
)
1149 ConfigEntry
*pEntry
= FindEntry(szName
);
1150 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1152 LineList
*pLine
= pEntry
->GetLine();
1153 if ( pLine
!= NULL
) {
1154 // notice that we may do this test inside the previous "if" because the
1155 // last entry's line is surely !NULL
1156 if ( pEntry
== m_pLastEntry
) {
1157 // our last entry is being deleted - find the last one which stays
1158 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1160 // go back until we find another entry or reach the group's line
1161 ConfigEntry
*pNewLast
= NULL
;
1162 size_t n
, nEntries
= m_aEntries
.Count();
1164 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1165 // is it our subgroup?
1166 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1167 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1168 pNewLast
= m_aEntries
[n
];
1171 if ( pNewLast
!= NULL
) // found?
1175 if ( pl
== m_pLine
) {
1176 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1178 // we've reached the group line without finding any subgroups
1179 m_pLastEntry
= NULL
;
1182 m_pLastEntry
= pNewLast
;
1185 m_pConfig
->LineListRemove(pLine
);
1188 // we must be written back for the changes to be saved
1191 m_aEntries
.Remove(pEntry
);
1197 // ----------------------------------------------------------------------------
1199 // ----------------------------------------------------------------------------
1200 void wxFileConfig::ConfigGroup::SetDirty()
1203 if ( Parent() != NULL
) // propagate upwards
1204 Parent()->SetDirty();
1207 // ============================================================================
1208 // wxFileConfig::ConfigEntry
1209 // ============================================================================
1211 // ----------------------------------------------------------------------------
1213 // ----------------------------------------------------------------------------
1214 wxFileConfig::ConfigEntry::ConfigEntry(wxFileConfig::ConfigGroup
*pParent
,
1215 const wxString
& strName
,
1217 : m_strName(strName
)
1219 wxASSERT( !strName
.IsEmpty() );
1221 m_pParent
= pParent
;
1227 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1229 m_strName
.erase(0, 1); // remove first character
1232 // ----------------------------------------------------------------------------
1234 // ----------------------------------------------------------------------------
1236 void wxFileConfig::ConfigEntry::SetLine(LineList
*pLine
)
1238 if ( m_pLine
!= NULL
) {
1239 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1240 Name().c_str(), m_pParent
->GetFullName().c_str());
1244 Group()->SetLastEntry(this);
1247 // second parameter is FALSE if we read the value from file and prevents the
1248 // entry from being marked as 'dirty'
1249 void wxFileConfig::ConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1251 if ( bUser
&& IsImmutable() ) {
1252 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1257 // do nothing if it's the same value
1258 if ( strValue
== m_strValue
)
1261 m_strValue
= strValue
;
1264 wxString strVal
= FilterOut(strValue
);
1266 strLine
<< m_strName
<< " = " << strVal
;
1268 if ( m_pLine
!= NULL
) {
1269 // entry was read from the local config file, just modify the line
1270 m_pLine
->SetText(strLine
);
1273 // add a new line to the file
1274 wxASSERT( m_nLine
== NOT_FOUND
); // consistency check
1276 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1277 Group()->GetLastEntryLine());
1278 Group()->SetLastEntry(this);
1285 void wxFileConfig::ConfigEntry::SetDirty()
1288 Group()->SetDirty();
1291 // ============================================================================
1293 // ============================================================================
1295 // ----------------------------------------------------------------------------
1296 // compare functions for array sorting
1297 // ----------------------------------------------------------------------------
1299 int CompareEntries(wxFileConfig::ConfigEntry
*p1
,
1300 wxFileConfig::ConfigEntry
*p2
)
1302 #if wxCONFIG_CASE_SENSITIVE
1303 return strcmp(p1
->Name(), p2
->Name());
1305 return Stricmp(p1
->Name(), p2
->Name());
1309 int CompareGroups(wxFileConfig::ConfigGroup
*p1
,
1310 wxFileConfig::ConfigGroup
*p2
)
1312 #if wxCONFIG_CASE_SENSITIVE
1313 return strcmp(p1
->Name(), p2
->Name());
1315 return Stricmp(p1
->Name(), p2
->Name());
1319 // ----------------------------------------------------------------------------
1321 // ----------------------------------------------------------------------------
1324 wxString
FilterIn(const wxString
& str
)
1327 strResult
.Alloc(str
.Len());
1329 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1331 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1332 if ( str
[n
] == '\\' ) {
1333 switch ( str
[++n
] ) {
1356 if ( str
[n
] != '"' || !bQuoted
)
1357 strResult
+= str
[n
];
1358 else if ( n
!= str
.Len() - 1 ) {
1359 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1362 //else: it's the last quote of a quoted string, ok
1369 // quote the string before writing it to file
1370 wxString
FilterOut(const wxString
& str
)
1376 strResult
.Alloc(str
.Len());
1378 // quoting is necessary to preserve spaces in the beginning of the string
1379 bool bQuote
= isspace(str
[0]) || str
[0] == '"';
1385 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1408 //else: fall through
1411 strResult
+= str
[n
];
1412 continue; // nothing special to do
1415 // we get here only for special characters
1416 strResult
<< '\\' << c
;