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 #include "wx/utils.h" // for wxGetHomeDir
45 // _WINDOWS_ is defined when windows.h is included,
46 // __WXMSW__ is defined for MS Windows compilation
47 #if defined(__WXMSW__) && !defined(_WINDOWS_)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
57 #define CONST_CAST ((wxFileConfig *)this)->
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
67 // global functions declarations
68 // ----------------------------------------------------------------------------
70 // is 'c' a valid character in group name?
71 // NB: wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR must be valid chars,
72 // but _not_ ']' (group name delimiter)
73 inline bool IsValid(char c
) { return isalnum(c
) || strchr("@_/-!.*%", c
); }
75 // compare functions for sorting the arrays
76 static int CompareEntries(ConfigEntry
*p1
, ConfigEntry
*p2
);
77 static int CompareGroups(ConfigGroup
*p1
, ConfigGroup
*p2
);
80 static wxString
FilterIn(const wxString
& str
);
81 static wxString
FilterOut(const wxString
& str
);
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
90 wxString
wxFileConfig::GetGlobalDir()
96 #elif defined(__WXSTUBS__)
97 wxASSERT_MSG( FALSE
, "TODO" ) ;
99 char szWinDir
[MAX_PATH
];
100 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
104 #endif // Unix/Windows
109 wxString
wxFileConfig::GetLocalDir()
113 wxGetHomeDir(&strDir
);
116 if (strDir
.Last() != '/') strDir
<< '/';
118 if (strDir
.Last() != '\\') strDir
<< '\\';
124 wxString
wxFileConfig::GetGlobalFileName(const char *szFile
)
126 wxString str
= GetGlobalDir();
129 if ( strchr(szFile
, '.') == NULL
)
139 wxString
wxFileConfig::GetLocalFileName(const char *szFile
)
141 wxString str
= GetLocalDir();
150 if ( strchr(szFile
, '.') == NULL
)
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void wxFileConfig::Init()
164 m_pRootGroup
= new ConfigGroup(NULL
, "", this);
169 // it's not an error if (one of the) file(s) doesn't exist
171 // parse the global file
172 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) ) {
173 wxTextFile
fileGlobal(m_strGlobalFile
);
175 if ( fileGlobal
.Open() ) {
176 Parse(fileGlobal
, FALSE
/* global */);
180 wxLogWarning(_("can't open global configuration file '%s'."),
181 m_strGlobalFile
.c_str());
184 // parse the local file
185 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) ) {
186 wxTextFile
fileLocal(m_strLocalFile
);
187 if ( fileLocal
.Open() ) {
188 Parse(fileLocal
, TRUE
/* local */);
192 wxLogWarning(_("can't open user configuration file '%s'."),
193 m_strLocalFile
.c_str());
197 // constructor supports creation of wxFileConfig objects of any type
198 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
199 const wxString
& strLocal
, const wxString
& strGlobal
,
201 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
),
202 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
204 // Make up an application name if not supplied
205 if (appName
.IsEmpty() && wxTheApp
)
207 SetAppName(wxTheApp
->GetAppName());
210 // Make up names for files if empty
211 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
213 m_strLocalFile
= GetLocalFileName(GetAppName());
216 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
218 m_strGlobalFile
= GetGlobalFileName(GetAppName());
221 // Check if styles are not supplied, but filenames are, in which case
222 // add the correct styles.
223 if ( !m_strLocalFile
.IsEmpty() )
224 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
226 if ( !m_strGlobalFile
.IsEmpty() )
227 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
229 // if the path is not absolute, prepend the standard directory to it
230 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
232 wxString strLocal
= m_strLocalFile
;
233 m_strLocalFile
= GetLocalDir();
234 m_strLocalFile
<< strLocal
;
237 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
239 wxString strGlobal
= m_strGlobalFile
;
240 m_strGlobalFile
= GetGlobalDir();
241 m_strGlobalFile
<< strGlobal
;
247 void wxFileConfig::CleanUp()
251 LineList
*pCur
= m_linesHead
;
252 while ( pCur
!= NULL
) {
253 LineList
*pNext
= pCur
->Next();
259 wxFileConfig::~wxFileConfig()
266 // ----------------------------------------------------------------------------
267 // parse a config file
268 // ----------------------------------------------------------------------------
270 void wxFileConfig::Parse(wxTextFile
& file
, bool bLocal
)
276 size_t nLineCount
= file
.GetLineCount();
277 for ( size_t n
= 0; n
< nLineCount
; n
++ ) {
280 // add the line to linked list
282 LineListAppend(strLine
);
284 // skip leading spaces
285 for ( pStart
= strLine
; isspace(*pStart
); pStart
++ )
288 // skip blank/comment lines
289 if ( *pStart
== '\0'|| *pStart
== ';' || *pStart
== '#' )
292 if ( *pStart
== '[' ) { // a new group
295 while ( *++pEnd
!= ']' ) {
296 if ( !IsValid(*pEnd
) && *pEnd
!= ' ' ) // allow spaces in group names
300 if ( *pEnd
!= ']' ) {
301 wxLogError(_("file '%s': unexpected character %c at line %d."),
302 file
.GetName(), *pEnd
, n
+ 1);
303 continue; // skip this line
306 // group name here is always considered as abs path
309 strGroup
<< wxCONFIG_PATH_SEPARATOR
<< wxString(pStart
, pEnd
- pStart
);
311 // will create it if doesn't yet exist
315 m_pCurrentGroup
->SetLine(m_linesTail
);
317 // check that there is nothing except comments left on this line
319 while ( *++pEnd
!= '\0' && bCont
) {
328 // ignore whitespace ('\n' impossible here)
332 wxLogWarning(_("file '%s', line %d: '%s' "
333 "ignored after group header."),
334 file
.GetName(), n
+ 1, pEnd
);
340 const char *pEnd
= pStart
;
341 while ( IsValid(*pEnd
) )
344 wxString
strKey(pStart
, pEnd
);
347 while ( isspace(*pEnd
) )
350 if ( *pEnd
++ != '=' ) {
351 wxLogError(_("file '%s', line %d: '=' expected."),
352 file
.GetName(), n
+ 1);
355 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
357 if ( pEntry
== NULL
) {
359 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
362 pEntry
->SetLine(m_linesTail
);
365 if ( bLocal
&& pEntry
->IsImmutable() ) {
366 // immutable keys can't be changed by user
367 wxLogWarning(_("file '%s', line %d: value for "
368 "immutable key '%s' ignored."),
369 file
.GetName(), n
+ 1, strKey
.c_str());
372 // the condition below catches the cases (a) and (b) but not (c):
373 // (a) global key found second time in global file
374 // (b) key found second (or more) time in local file
375 // (c) key from global file now found in local one
376 // which is exactly what we want.
377 else if ( !bLocal
|| pEntry
->IsLocal() ) {
378 wxLogWarning(_("file '%s', line %d: key '%s' was first "
379 "found at line %d."),
380 file
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
383 pEntry
->SetLine(m_linesTail
);
388 while ( isspace(*pEnd
) )
391 pEntry
->SetValue(FilterIn(pEnd
), FALSE
/* read from file */);
397 // ----------------------------------------------------------------------------
399 // ----------------------------------------------------------------------------
401 void wxFileConfig::SetRootPath()
404 m_pCurrentGroup
= m_pRootGroup
;
407 void wxFileConfig::SetPath(const wxString
& strPath
)
409 wxArrayString aParts
;
411 if ( strPath
.IsEmpty() ) {
416 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
418 wxSplitPath(aParts
, strPath
);
421 // relative path, combine with current one
422 wxString strFullPath
= m_strPath
;
423 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
424 wxSplitPath(aParts
, strFullPath
);
427 // change current group
429 m_pCurrentGroup
= m_pRootGroup
;
430 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
431 ConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
432 if ( pNextGroup
== NULL
)
433 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
434 m_pCurrentGroup
= pNextGroup
;
437 // recombine path parts in one variable
439 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
440 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
451 return GetNextGroup(str
, lIndex
);
454 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
456 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
457 str
= m_pCurrentGroup
->Groups()[lIndex
++]->Name();
464 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
467 return GetNextEntry(str
, lIndex
);
470 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
472 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
473 str
= m_pCurrentGroup
->Entries()[lIndex
++]->Name();
480 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
482 size_t n
= m_pCurrentGroup
->Entries().Count();
484 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
485 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
486 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
487 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
488 n
+= GetNumberOfEntries(TRUE
);
489 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
496 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
498 size_t n
= m_pCurrentGroup
->Groups().Count();
500 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
501 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
502 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
503 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
504 n
+= GetNumberOfGroups(TRUE
);
505 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
512 // ----------------------------------------------------------------------------
513 // tests for existence
514 // ----------------------------------------------------------------------------
516 bool wxFileConfig::HasGroup(const wxString
& strName
) const
518 wxConfigPathChanger
path(this, strName
);
520 ConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
521 return pGroup
!= NULL
;
524 bool wxFileConfig::HasEntry(const wxString
& strName
) const
526 wxConfigPathChanger
path(this, strName
);
528 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
529 return pEntry
!= NULL
;
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 bool wxFileConfig::Read(const wxString
& key
,
537 wxString
* pStr
) const
539 wxConfigPathChanger
path(this, key
);
541 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
542 if (pEntry
== NULL
) {
546 *pStr
= ExpandEnvVars(pEntry
->Value());
551 bool wxFileConfig::Read(const wxString
& key
,
552 wxString
* pStr
, const wxString
& defVal
) const
554 wxConfigPathChanger
path(this, key
);
556 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
557 if (pEntry
== NULL
) {
558 if( IsRecordingDefaults() )
559 ((wxFileConfig
*)this)->Write(key
,defVal
);
560 *pStr
= ExpandEnvVars(defVal
);
564 *pStr
= ExpandEnvVars(pEntry
->Value());
569 bool wxFileConfig::Read(const wxString
& key
, long *pl
) const
572 if ( Read(key
, & str
) ) {
581 bool wxFileConfig::Write(const wxString
& key
, const wxString
& szValue
)
583 wxConfigPathChanger
path(this, key
);
585 wxString strName
= path
.Name();
586 if ( strName
.IsEmpty() ) {
587 // setting the value of a group is an error
588 wxASSERT_MSG( IsEmpty(szValue
), _("can't set value of a group!") );
590 // ... except if it's empty in which case it's a way to force it's creation
591 m_pCurrentGroup
->SetDirty();
593 // this will add a line for this group if it didn't have it before
594 (void)m_pCurrentGroup
->GetGroupLine();
599 // check that the name is reasonable
600 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
) {
601 wxLogError(_("Entry name can't start with '%c'."),
602 wxCONFIG_IMMUTABLE_PREFIX
);
606 for ( const char *pc
= strName
; *pc
!= '\0'; pc
++ ) {
607 if ( !IsValid(*pc
) ) {
608 wxLogError(_("Character '%c' is invalid in a config entry name."),
614 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
615 if ( pEntry
== NULL
)
616 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
618 pEntry
->SetValue(szValue
);
624 bool wxFileConfig::Write(const wxString
& key
, long lValue
)
626 // ltoa() is not ANSI :-(
628 buf
.Printf("%ld", lValue
);
629 return Write(key
, buf
);
632 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
634 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() )
637 wxTempFile
file(m_strLocalFile
);
639 if ( !file
.IsOpened() ) {
640 wxLogError(_("can't open user configuration file."));
644 // write all strings to file
645 for ( LineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() ) {
646 if ( !file
.Write(p
->Text() + wxTextFile::GetEOL()) ) {
647 wxLogError(_("can't write user configuration file."));
652 return file
.Commit();
655 // ----------------------------------------------------------------------------
656 // renaming groups/entries
657 // ----------------------------------------------------------------------------
659 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
660 const wxString
& newName
)
662 // check that the entry exists
663 ConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
667 // check that the new entry doesn't already exist
668 if ( m_pCurrentGroup
->FindEntry(newName
) )
671 // delete the old entry, create the new one
672 wxString value
= oldEntry
->Value();
673 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
676 ConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
677 newEntry
->SetValue(value
);
682 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
683 const wxString
& newName
)
685 // check that the group exists
686 ConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
690 // check that the new group doesn't already exist
691 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
694 group
->Rename(newName
);
699 // ----------------------------------------------------------------------------
700 // delete groups/entries
701 // ----------------------------------------------------------------------------
703 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
705 wxConfigPathChanger
path(this, key
);
707 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
710 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
711 if ( m_pCurrentGroup
!= m_pRootGroup
) {
712 ConfigGroup
*pGroup
= m_pCurrentGroup
;
713 SetPath(".."); // changes m_pCurrentGroup!
714 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
716 //else: never delete the root group
722 bool wxFileConfig::DeleteGroup(const wxString
& key
)
724 wxConfigPathChanger
path(this, key
);
726 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
729 bool wxFileConfig::DeleteAll()
733 const char *szFile
= m_strLocalFile
;
735 if ( remove(szFile
) == -1 )
736 wxLogSysError(_("can't delete user configuration file '%s'"), szFile
);
738 m_strLocalFile
= m_strGlobalFile
= "";
744 // ----------------------------------------------------------------------------
745 // linked list functions
746 // ----------------------------------------------------------------------------
748 // append a new line to the end of the list
749 LineList
*wxFileConfig::LineListAppend(const wxString
& str
)
751 LineList
*pLine
= new LineList(str
);
753 if ( m_linesTail
== NULL
) {
759 m_linesTail
->SetNext(pLine
);
760 pLine
->SetPrev(m_linesTail
);
767 // insert a new line after the given one or in the very beginning if !pLine
768 LineList
*wxFileConfig::LineListInsert(const wxString
& str
,
771 if ( pLine
== m_linesTail
)
772 return LineListAppend(str
);
774 LineList
*pNewLine
= new LineList(str
);
775 if ( pLine
== NULL
) {
776 // prepend to the list
777 pNewLine
->SetNext(m_linesHead
);
778 m_linesHead
->SetPrev(pNewLine
);
779 m_linesHead
= pNewLine
;
782 // insert before pLine
783 LineList
*pNext
= pLine
->Next();
784 pNewLine
->SetNext(pNext
);
785 pNewLine
->SetPrev(pLine
);
786 pNext
->SetPrev(pNewLine
);
787 pLine
->SetNext(pNewLine
);
793 void wxFileConfig::LineListRemove(LineList
*pLine
)
795 LineList
*pPrev
= pLine
->Prev(),
796 *pNext
= pLine
->Next();
802 pPrev
->SetNext(pNext
);
808 pNext
->SetPrev(pPrev
);
813 bool wxFileConfig::LineListIsEmpty()
815 return m_linesHead
== NULL
;
818 // ============================================================================
819 // wxFileConfig::ConfigGroup
820 // ============================================================================
822 // ----------------------------------------------------------------------------
824 // ----------------------------------------------------------------------------
827 ConfigGroup::ConfigGroup(ConfigGroup
*pParent
,
828 const wxString
& strName
,
829 wxFileConfig
*pConfig
)
830 : m_aEntries(CompareEntries
),
831 m_aSubgroups(CompareGroups
),
843 // dtor deletes all children
844 ConfigGroup::~ConfigGroup()
847 size_t n
, nCount
= m_aEntries
.Count();
848 for ( n
= 0; n
< nCount
; n
++ )
849 delete m_aEntries
[n
];
852 nCount
= m_aSubgroups
.Count();
853 for ( n
= 0; n
< nCount
; n
++ )
854 delete m_aSubgroups
[n
];
857 // ----------------------------------------------------------------------------
859 // ----------------------------------------------------------------------------
861 void ConfigGroup::SetLine(LineList
*pLine
)
863 wxASSERT( m_pLine
== NULL
); // shouldn't be called twice
869 This is a bit complicated, so let me explain it in details. All lines that
870 were read from the local file (the only one we will ever modify) are stored
871 in a (doubly) linked list. Our problem is to know at which position in this
872 list should we insert the new entries/subgroups. To solve it we keep three
873 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
875 m_pLine points to the line containing "[group_name]"
876 m_pLastEntry points to the last entry of this group in the local file.
877 m_pLastGroup subgroup
879 Initially, they're NULL all three. When the group (an entry/subgroup) is read
880 from the local file, the corresponding variable is set. However, if the group
881 was read from the global file and then modified or created by the application
882 these variables are still NULL and we need to create the corresponding lines.
883 See the following functions (and comments preceding them) for the details of
886 Also, when our last entry/group are deleted we need to find the new last
887 element - the code in DeleteEntry/Subgroup does this by backtracking the list
888 of lines until it either founds an entry/subgroup (and this is the new last
889 element) or the m_pLine of the group, in which case there are no more entries
890 (or subgroups) left and m_pLast<element> becomes NULL.
892 NB: This last problem could be avoided for entries if we added new entries
893 immediately after m_pLine, but in this case the entries would appear
894 backwards in the config file (OTOH, it's not that important) and as we
895 would still need to do it for the subgroups the code wouldn't have been
896 significantly less complicated.
899 // Return the line which contains "[our name]". If we're still not in the list,
900 // add our line to it immediately after the last line of our parent group if we
901 // have it or in the very beginning if we're the root group.
902 LineList
*ConfigGroup::GetGroupLine()
904 if ( m_pLine
== NULL
) {
905 ConfigGroup
*pParent
= Parent();
907 // this group wasn't present in local config file, add it now
908 if ( pParent
!= NULL
) {
909 wxString strFullName
;
910 strFullName
<< "[" << (GetFullName().c_str() + 1) << "]"; // +1: no '/'
911 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
912 pParent
->GetLastGroupLine());
913 pParent
->SetLastGroup(this); // we're surely after all the others
916 // we return NULL, so that LineListInsert() will insert us in the
924 // Return the last line belonging to the subgroups of this group (after which
925 // we can add a new subgroup), if we don't have any subgroups or entries our
926 // last line is the group line (m_pLine) itself.
927 LineList
*ConfigGroup::GetLastGroupLine()
929 // if we have any subgroups, our last line is the last line of the last
931 if ( m_pLastGroup
!= NULL
) {
932 LineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
934 wxASSERT( pLine
!= NULL
); // last group must have !NULL associated line
938 // no subgroups, so the last line is the line of thelast entry (if any)
939 return GetLastEntryLine();
942 // return the last line belonging to the entries of this group (after which
943 // we can add a new entry), if we don't have any entries we will add the new
944 // one immediately after the group line itself.
945 LineList
*ConfigGroup::GetLastEntryLine()
947 if ( m_pLastEntry
!= NULL
) {
948 LineList
*pLine
= m_pLastEntry
->GetLine();
950 wxASSERT( pLine
!= NULL
); // last entry must have !NULL associated line
954 // no entries: insert after the group header
955 return GetGroupLine();
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 void ConfigGroup::Rename(const wxString
& newName
)
966 LineList
*line
= GetGroupLine();
967 wxString strFullName
;
968 strFullName
<< "[" << (GetFullName().c_str() + 1) << "]"; // +1: no '/'
969 line
->SetText(strFullName
);
974 wxString
ConfigGroup::GetFullName() const
977 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 // use binary search because the array is sorted
988 ConfigGroup::FindEntry(const char *szName
) const
992 hi
= m_aEntries
.Count();
998 pEntry
= m_aEntries
[i
];
1000 #if wxCONFIG_CASE_SENSITIVE
1001 res
= strcmp(pEntry
->Name(), szName
);
1003 res
= Stricmp(pEntry
->Name(), szName
);
1018 ConfigGroup::FindSubgroup(const char *szName
) const
1022 hi
= m_aSubgroups
.Count();
1024 ConfigGroup
*pGroup
;
1028 pGroup
= m_aSubgroups
[i
];
1030 #if wxCONFIG_CASE_SENSITIVE
1031 res
= strcmp(pGroup
->Name(), szName
);
1033 res
= Stricmp(pGroup
->Name(), szName
);
1047 // ----------------------------------------------------------------------------
1048 // create a new item
1049 // ----------------------------------------------------------------------------
1051 // create a new entry and add it to the current group
1053 ConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1055 wxASSERT( FindEntry(strName
) == NULL
);
1057 ConfigEntry
*pEntry
= new ConfigEntry(this, strName
, nLine
);
1058 m_aEntries
.Add(pEntry
);
1063 // create a new group and add it to the current group
1065 ConfigGroup::AddSubgroup(const wxString
& strName
)
1067 wxASSERT( FindSubgroup(strName
) == NULL
);
1069 ConfigGroup
*pGroup
= new ConfigGroup(this, strName
, m_pConfig
);
1070 m_aSubgroups
.Add(pGroup
);
1075 // ----------------------------------------------------------------------------
1077 // ----------------------------------------------------------------------------
1080 The delete operations are _very_ slow if we delete the last item of this
1081 group (see comments before GetXXXLineXXX functions for more details),
1082 so it's much better to start with the first entry/group if we want to
1083 delete several of them.
1086 bool ConfigGroup::DeleteSubgroupByName(const char *szName
)
1088 return DeleteSubgroup(FindSubgroup(szName
));
1091 // doesn't delete the subgroup itself, but does remove references to it from
1092 // all other data structures (and normally the returned pointer should be
1093 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1094 bool ConfigGroup::DeleteSubgroup(ConfigGroup
*pGroup
)
1096 wxCHECK( pGroup
!= NULL
, FALSE
); // deleting non existing group?
1098 // delete all entries
1099 size_t nCount
= pGroup
->m_aEntries
.Count();
1100 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ ) {
1101 LineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1102 if ( pLine
!= NULL
)
1103 m_pConfig
->LineListRemove(pLine
);
1106 // and subgroups of this sungroup
1107 nCount
= pGroup
->m_aSubgroups
.Count();
1108 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ ) {
1109 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[nGroup
]);
1112 LineList
*pLine
= pGroup
->m_pLine
;
1113 if ( pLine
!= NULL
) {
1114 // notice that we may do this test inside the previous "if" because the
1115 // last entry's line is surely !NULL
1116 if ( pGroup
== m_pLastGroup
) {
1117 // our last entry is being deleted - find the last one which stays
1118 wxASSERT( m_pLine
!= NULL
); // we have a subgroup with !NULL pLine...
1120 // go back until we find a subgroup or reach the group's line
1121 ConfigGroup
*pNewLast
= NULL
;
1122 size_t n
, nSubgroups
= m_aSubgroups
.Count();
1124 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1125 // is it our subgroup?
1126 for ( n
= 0; (pNewLast
== NULL
) && (n
< nSubgroups
); n
++ ) {
1127 // do _not_ call GetGroupLine! we don't want to add it to the local
1128 // file if it's not already there
1129 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1130 pNewLast
= m_aSubgroups
[n
];
1133 if ( pNewLast
!= NULL
) // found?
1137 if ( pl
== m_pLine
) {
1138 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1140 // we've reached the group line without finding any subgroups
1141 m_pLastGroup
= NULL
;
1144 m_pLastGroup
= pNewLast
;
1147 m_pConfig
->LineListRemove(pLine
);
1152 m_aSubgroups
.Remove(pGroup
);
1158 bool ConfigGroup::DeleteEntry(const char *szName
)
1160 ConfigEntry
*pEntry
= FindEntry(szName
);
1161 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1163 LineList
*pLine
= pEntry
->GetLine();
1164 if ( pLine
!= NULL
) {
1165 // notice that we may do this test inside the previous "if" because the
1166 // last entry's line is surely !NULL
1167 if ( pEntry
== m_pLastEntry
) {
1168 // our last entry is being deleted - find the last one which stays
1169 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1171 // go back until we find another entry or reach the group's line
1172 ConfigEntry
*pNewLast
= NULL
;
1173 size_t n
, nEntries
= m_aEntries
.Count();
1175 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1176 // is it our subgroup?
1177 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1178 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1179 pNewLast
= m_aEntries
[n
];
1182 if ( pNewLast
!= NULL
) // found?
1186 if ( pl
== m_pLine
) {
1187 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1189 // we've reached the group line without finding any subgroups
1190 m_pLastEntry
= NULL
;
1193 m_pLastEntry
= pNewLast
;
1196 m_pConfig
->LineListRemove(pLine
);
1199 // we must be written back for the changes to be saved
1202 m_aEntries
.Remove(pEntry
);
1208 // ----------------------------------------------------------------------------
1210 // ----------------------------------------------------------------------------
1211 void ConfigGroup::SetDirty()
1214 if ( Parent() != NULL
) // propagate upwards
1215 Parent()->SetDirty();
1218 // ============================================================================
1219 // wxFileConfig::ConfigEntry
1220 // ============================================================================
1222 // ----------------------------------------------------------------------------
1224 // ----------------------------------------------------------------------------
1225 ConfigEntry::ConfigEntry(ConfigGroup
*pParent
,
1226 const wxString
& strName
,
1228 : m_strName(strName
)
1230 wxASSERT( !strName
.IsEmpty() );
1232 m_pParent
= pParent
;
1238 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1240 m_strName
.erase(0, 1); // remove first character
1243 // ----------------------------------------------------------------------------
1245 // ----------------------------------------------------------------------------
1247 void ConfigEntry::SetLine(LineList
*pLine
)
1249 if ( m_pLine
!= NULL
) {
1250 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1251 Name().c_str(), m_pParent
->GetFullName().c_str());
1255 Group()->SetLastEntry(this);
1258 // second parameter is FALSE if we read the value from file and prevents the
1259 // entry from being marked as 'dirty'
1260 void ConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1262 if ( bUser
&& IsImmutable() ) {
1263 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1268 // do nothing if it's the same value
1269 if ( strValue
== m_strValue
)
1272 m_strValue
= strValue
;
1275 wxString strVal
= FilterOut(strValue
);
1277 strLine
<< m_strName
<< " = " << strVal
;
1279 if ( m_pLine
!= NULL
) {
1280 // entry was read from the local config file, just modify the line
1281 m_pLine
->SetText(strLine
);
1284 // add a new line to the file
1285 wxASSERT( m_nLine
== wxNOT_FOUND
); // consistency check
1287 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1288 Group()->GetLastEntryLine());
1289 Group()->SetLastEntry(this);
1296 void ConfigEntry::SetDirty()
1299 Group()->SetDirty();
1302 // ============================================================================
1304 // ============================================================================
1306 // ----------------------------------------------------------------------------
1307 // compare functions for array sorting
1308 // ----------------------------------------------------------------------------
1310 int CompareEntries(ConfigEntry
*p1
,
1313 #if wxCONFIG_CASE_SENSITIVE
1314 return strcmp(p1
->Name(), p2
->Name());
1316 return Stricmp(p1
->Name(), p2
->Name());
1320 int CompareGroups(ConfigGroup
*p1
,
1323 #if wxCONFIG_CASE_SENSITIVE
1324 return strcmp(p1
->Name(), p2
->Name());
1326 return Stricmp(p1
->Name(), p2
->Name());
1330 // ----------------------------------------------------------------------------
1332 // ----------------------------------------------------------------------------
1335 wxString
FilterIn(const wxString
& str
)
1338 strResult
.Alloc(str
.Len());
1340 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1342 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1343 if ( str
[n
] == '\\' ) {
1344 switch ( str
[++n
] ) {
1367 if ( str
[n
] != '"' || !bQuoted
)
1368 strResult
+= str
[n
];
1369 else if ( n
!= str
.Len() - 1 ) {
1370 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1373 //else: it's the last quote of a quoted string, ok
1380 // quote the string before writing it to file
1381 wxString
FilterOut(const wxString
& str
)
1387 strResult
.Alloc(str
.Len());
1389 // quoting is necessary to preserve spaces in the beginning of the string
1390 bool bQuote
= isspace(str
[0]) || str
[0] == '"';
1396 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1419 //else: fall through
1422 strResult
+= str
[n
];
1423 continue; // nothing special to do
1426 // we get here only for special characters
1427 strResult
<< '\\' << c
;