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" ) ;
98 #elif defined(__WXMAC__)
99 wxASSERT_MSG( FALSE
, "TODO" ) ;
101 char szWinDir
[MAX_PATH
];
102 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
106 #endif // Unix/Windows
111 wxString
wxFileConfig::GetLocalDir()
115 wxGetHomeDir(&strDir
);
118 if (strDir
.Last() != '/') strDir
<< '/';
120 if (strDir
.Last() != '\\') strDir
<< '\\';
126 wxString
wxFileConfig::GetGlobalFileName(const char *szFile
)
128 wxString str
= GetGlobalDir();
131 if ( strchr(szFile
, '.') == NULL
)
141 wxString
wxFileConfig::GetLocalFileName(const char *szFile
)
143 wxString str
= GetLocalDir();
152 if ( strchr(szFile
, '.') == NULL
)
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxFileConfig::Init()
166 m_pRootGroup
= new ConfigGroup(NULL
, "", this);
171 // it's not an error if (one of the) file(s) doesn't exist
173 // parse the global file
174 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) ) {
175 wxTextFile
fileGlobal(m_strGlobalFile
);
177 if ( fileGlobal
.Open() ) {
178 Parse(fileGlobal
, FALSE
/* global */);
182 wxLogWarning(_("can't open global configuration file '%s'."),
183 m_strGlobalFile
.c_str());
186 // parse the local file
187 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) ) {
188 wxTextFile
fileLocal(m_strLocalFile
);
189 if ( fileLocal
.Open() ) {
190 Parse(fileLocal
, TRUE
/* local */);
194 wxLogWarning(_("can't open user configuration file '%s'."),
195 m_strLocalFile
.c_str());
199 // constructor supports creation of wxFileConfig objects of any type
200 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
201 const wxString
& strLocal
, const wxString
& strGlobal
,
203 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
),
204 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
206 // Make up an application name if not supplied
207 if (appName
.IsEmpty() && wxTheApp
)
209 SetAppName(wxTheApp
->GetAppName());
212 // Make up names for files if empty
213 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
215 m_strLocalFile
= GetLocalFileName(GetAppName());
218 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
220 m_strGlobalFile
= GetGlobalFileName(GetAppName());
223 // Check if styles are not supplied, but filenames are, in which case
224 // add the correct styles.
225 if ( !m_strLocalFile
.IsEmpty() )
226 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
228 if ( !m_strGlobalFile
.IsEmpty() )
229 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
231 // if the path is not absolute, prepend the standard directory to it
232 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
234 wxString strLocal
= m_strLocalFile
;
235 m_strLocalFile
= GetLocalDir();
236 m_strLocalFile
<< strLocal
;
239 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
241 wxString strGlobal
= m_strGlobalFile
;
242 m_strGlobalFile
= GetGlobalDir();
243 m_strGlobalFile
<< strGlobal
;
249 void wxFileConfig::CleanUp()
253 LineList
*pCur
= m_linesHead
;
254 while ( pCur
!= NULL
) {
255 LineList
*pNext
= pCur
->Next();
261 wxFileConfig::~wxFileConfig()
268 // ----------------------------------------------------------------------------
269 // parse a config file
270 // ----------------------------------------------------------------------------
272 void wxFileConfig::Parse(wxTextFile
& file
, bool bLocal
)
278 size_t nLineCount
= file
.GetLineCount();
279 for ( size_t n
= 0; n
< nLineCount
; n
++ ) {
282 // add the line to linked list
284 LineListAppend(strLine
);
286 // skip leading spaces
287 for ( pStart
= strLine
; isspace(*pStart
); pStart
++ )
290 // skip blank/comment lines
291 if ( *pStart
== '\0'|| *pStart
== ';' || *pStart
== '#' )
294 if ( *pStart
== '[' ) { // a new group
297 while ( *++pEnd
!= ']' ) {
298 if ( !IsValid(*pEnd
) && *pEnd
!= ' ' ) // allow spaces in group names
302 if ( *pEnd
!= ']' ) {
303 wxLogError(_("file '%s': unexpected character %c at line %d."),
304 file
.GetName(), *pEnd
, n
+ 1);
305 continue; // skip this line
308 // group name here is always considered as abs path
311 strGroup
<< wxCONFIG_PATH_SEPARATOR
<< wxString(pStart
, pEnd
- pStart
);
313 // will create it if doesn't yet exist
317 m_pCurrentGroup
->SetLine(m_linesTail
);
319 // check that there is nothing except comments left on this line
321 while ( *++pEnd
!= '\0' && bCont
) {
330 // ignore whitespace ('\n' impossible here)
334 wxLogWarning(_("file '%s', line %d: '%s' "
335 "ignored after group header."),
336 file
.GetName(), n
+ 1, pEnd
);
342 const char *pEnd
= pStart
;
343 while ( IsValid(*pEnd
) )
346 wxString
strKey(pStart
, pEnd
);
349 while ( isspace(*pEnd
) )
352 if ( *pEnd
++ != '=' ) {
353 wxLogError(_("file '%s', line %d: '=' expected."),
354 file
.GetName(), n
+ 1);
357 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
359 if ( pEntry
== NULL
) {
361 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
364 pEntry
->SetLine(m_linesTail
);
367 if ( bLocal
&& pEntry
->IsImmutable() ) {
368 // immutable keys can't be changed by user
369 wxLogWarning(_("file '%s', line %d: value for "
370 "immutable key '%s' ignored."),
371 file
.GetName(), n
+ 1, strKey
.c_str());
374 // the condition below catches the cases (a) and (b) but not (c):
375 // (a) global key found second time in global file
376 // (b) key found second (or more) time in local file
377 // (c) key from global file now found in local one
378 // which is exactly what we want.
379 else if ( !bLocal
|| pEntry
->IsLocal() ) {
380 wxLogWarning(_("file '%s', line %d: key '%s' was first "
381 "found at line %d."),
382 file
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
385 pEntry
->SetLine(m_linesTail
);
390 while ( isspace(*pEnd
) )
393 pEntry
->SetValue(FilterIn(pEnd
), FALSE
/* read from file */);
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 void wxFileConfig::SetRootPath()
406 m_pCurrentGroup
= m_pRootGroup
;
409 void wxFileConfig::SetPath(const wxString
& strPath
)
411 wxArrayString aParts
;
413 if ( strPath
.IsEmpty() ) {
418 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
420 wxSplitPath(aParts
, strPath
);
423 // relative path, combine with current one
424 wxString strFullPath
= m_strPath
;
425 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
426 wxSplitPath(aParts
, strFullPath
);
429 // change current group
431 m_pCurrentGroup
= m_pRootGroup
;
432 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
433 ConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
434 if ( pNextGroup
== NULL
)
435 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
436 m_pCurrentGroup
= pNextGroup
;
439 // recombine path parts in one variable
441 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
442 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
453 return GetNextGroup(str
, lIndex
);
456 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
458 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
459 str
= m_pCurrentGroup
->Groups()[lIndex
++]->Name();
466 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
469 return GetNextEntry(str
, lIndex
);
472 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
474 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
475 str
= m_pCurrentGroup
->Entries()[lIndex
++]->Name();
482 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
484 size_t n
= m_pCurrentGroup
->Entries().Count();
486 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
487 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
488 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
489 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
490 n
+= GetNumberOfEntries(TRUE
);
491 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
498 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
500 size_t n
= m_pCurrentGroup
->Groups().Count();
502 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
503 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
504 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
505 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
506 n
+= GetNumberOfGroups(TRUE
);
507 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
514 // ----------------------------------------------------------------------------
515 // tests for existence
516 // ----------------------------------------------------------------------------
518 bool wxFileConfig::HasGroup(const wxString
& strName
) const
520 wxConfigPathChanger
path(this, strName
);
522 ConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
523 return pGroup
!= NULL
;
526 bool wxFileConfig::HasEntry(const wxString
& strName
) const
528 wxConfigPathChanger
path(this, strName
);
530 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
531 return pEntry
!= NULL
;
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 bool wxFileConfig::Read(const wxString
& key
,
539 wxString
* pStr
) const
541 wxConfigPathChanger
path(this, key
);
543 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
544 if (pEntry
== NULL
) {
548 *pStr
= ExpandEnvVars(pEntry
->Value());
553 bool wxFileConfig::Read(const wxString
& key
,
554 wxString
* pStr
, const wxString
& defVal
) const
556 wxConfigPathChanger
path(this, key
);
558 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
559 if (pEntry
== NULL
) {
560 if( IsRecordingDefaults() )
561 ((wxFileConfig
*)this)->Write(key
,defVal
);
562 *pStr
= ExpandEnvVars(defVal
);
566 *pStr
= ExpandEnvVars(pEntry
->Value());
571 bool wxFileConfig::Read(const wxString
& key
, long *pl
) const
574 if ( Read(key
, & str
) ) {
583 bool wxFileConfig::Write(const wxString
& key
, const wxString
& szValue
)
585 wxConfigPathChanger
path(this, key
);
587 wxString strName
= path
.Name();
588 if ( strName
.IsEmpty() ) {
589 // setting the value of a group is an error
590 wxASSERT_MSG( IsEmpty(szValue
), _("can't set value of a group!") );
592 // ... except if it's empty in which case it's a way to force it's creation
593 m_pCurrentGroup
->SetDirty();
595 // this will add a line for this group if it didn't have it before
596 (void)m_pCurrentGroup
->GetGroupLine();
601 // check that the name is reasonable
602 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
) {
603 wxLogError(_("Entry name can't start with '%c'."),
604 wxCONFIG_IMMUTABLE_PREFIX
);
608 for ( const char *pc
= strName
; *pc
!= '\0'; pc
++ ) {
609 if ( !IsValid(*pc
) ) {
610 wxLogError(_("Character '%c' is invalid in a config entry name."),
616 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
617 if ( pEntry
== NULL
)
618 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
620 pEntry
->SetValue(szValue
);
626 bool wxFileConfig::Write(const wxString
& key
, long lValue
)
628 // ltoa() is not ANSI :-(
630 buf
.Printf("%ld", lValue
);
631 return Write(key
, buf
);
634 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
636 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() )
639 wxTempFile
file(m_strLocalFile
);
641 if ( !file
.IsOpened() ) {
642 wxLogError(_("can't open user configuration file."));
646 // write all strings to file
647 for ( LineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() ) {
648 if ( !file
.Write(p
->Text() + wxTextFile::GetEOL()) ) {
649 wxLogError(_("can't write user configuration file."));
654 return file
.Commit();
657 // ----------------------------------------------------------------------------
658 // renaming groups/entries
659 // ----------------------------------------------------------------------------
661 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
662 const wxString
& newName
)
664 // check that the entry exists
665 ConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
669 // check that the new entry doesn't already exist
670 if ( m_pCurrentGroup
->FindEntry(newName
) )
673 // delete the old entry, create the new one
674 wxString value
= oldEntry
->Value();
675 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
678 ConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
679 newEntry
->SetValue(value
);
684 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
685 const wxString
& newName
)
687 // check that the group exists
688 ConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
692 // check that the new group doesn't already exist
693 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
696 group
->Rename(newName
);
701 // ----------------------------------------------------------------------------
702 // delete groups/entries
703 // ----------------------------------------------------------------------------
705 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
707 wxConfigPathChanger
path(this, key
);
709 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
712 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
713 if ( m_pCurrentGroup
!= m_pRootGroup
) {
714 ConfigGroup
*pGroup
= m_pCurrentGroup
;
715 SetPath(".."); // changes m_pCurrentGroup!
716 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
718 //else: never delete the root group
724 bool wxFileConfig::DeleteGroup(const wxString
& key
)
726 wxConfigPathChanger
path(this, key
);
728 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
731 bool wxFileConfig::DeleteAll()
735 const char *szFile
= m_strLocalFile
;
737 if ( remove(szFile
) == -1 )
738 wxLogSysError(_("can't delete user configuration file '%s'"), szFile
);
740 m_strLocalFile
= m_strGlobalFile
= "";
746 // ----------------------------------------------------------------------------
747 // linked list functions
748 // ----------------------------------------------------------------------------
750 // append a new line to the end of the list
751 LineList
*wxFileConfig::LineListAppend(const wxString
& str
)
753 LineList
*pLine
= new LineList(str
);
755 if ( m_linesTail
== NULL
) {
761 m_linesTail
->SetNext(pLine
);
762 pLine
->SetPrev(m_linesTail
);
769 // insert a new line after the given one or in the very beginning if !pLine
770 LineList
*wxFileConfig::LineListInsert(const wxString
& str
,
773 if ( pLine
== m_linesTail
)
774 return LineListAppend(str
);
776 LineList
*pNewLine
= new LineList(str
);
777 if ( pLine
== NULL
) {
778 // prepend to the list
779 pNewLine
->SetNext(m_linesHead
);
780 m_linesHead
->SetPrev(pNewLine
);
781 m_linesHead
= pNewLine
;
784 // insert before pLine
785 LineList
*pNext
= pLine
->Next();
786 pNewLine
->SetNext(pNext
);
787 pNewLine
->SetPrev(pLine
);
788 pNext
->SetPrev(pNewLine
);
789 pLine
->SetNext(pNewLine
);
795 void wxFileConfig::LineListRemove(LineList
*pLine
)
797 LineList
*pPrev
= pLine
->Prev(),
798 *pNext
= pLine
->Next();
804 pPrev
->SetNext(pNext
);
810 pNext
->SetPrev(pPrev
);
815 bool wxFileConfig::LineListIsEmpty()
817 return m_linesHead
== NULL
;
820 // ============================================================================
821 // wxFileConfig::ConfigGroup
822 // ============================================================================
824 // ----------------------------------------------------------------------------
826 // ----------------------------------------------------------------------------
829 ConfigGroup::ConfigGroup(ConfigGroup
*pParent
,
830 const wxString
& strName
,
831 wxFileConfig
*pConfig
)
832 : m_aEntries(CompareEntries
),
833 m_aSubgroups(CompareGroups
),
845 // dtor deletes all children
846 ConfigGroup::~ConfigGroup()
849 size_t n
, nCount
= m_aEntries
.Count();
850 for ( n
= 0; n
< nCount
; n
++ )
851 delete m_aEntries
[n
];
854 nCount
= m_aSubgroups
.Count();
855 for ( n
= 0; n
< nCount
; n
++ )
856 delete m_aSubgroups
[n
];
859 // ----------------------------------------------------------------------------
861 // ----------------------------------------------------------------------------
863 void ConfigGroup::SetLine(LineList
*pLine
)
865 wxASSERT( m_pLine
== NULL
); // shouldn't be called twice
871 This is a bit complicated, so let me explain it in details. All lines that
872 were read from the local file (the only one we will ever modify) are stored
873 in a (doubly) linked list. Our problem is to know at which position in this
874 list should we insert the new entries/subgroups. To solve it we keep three
875 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
877 m_pLine points to the line containing "[group_name]"
878 m_pLastEntry points to the last entry of this group in the local file.
879 m_pLastGroup subgroup
881 Initially, they're NULL all three. When the group (an entry/subgroup) is read
882 from the local file, the corresponding variable is set. However, if the group
883 was read from the global file and then modified or created by the application
884 these variables are still NULL and we need to create the corresponding lines.
885 See the following functions (and comments preceding them) for the details of
888 Also, when our last entry/group are deleted we need to find the new last
889 element - the code in DeleteEntry/Subgroup does this by backtracking the list
890 of lines until it either founds an entry/subgroup (and this is the new last
891 element) or the m_pLine of the group, in which case there are no more entries
892 (or subgroups) left and m_pLast<element> becomes NULL.
894 NB: This last problem could be avoided for entries if we added new entries
895 immediately after m_pLine, but in this case the entries would appear
896 backwards in the config file (OTOH, it's not that important) and as we
897 would still need to do it for the subgroups the code wouldn't have been
898 significantly less complicated.
901 // Return the line which contains "[our name]". If we're still not in the list,
902 // add our line to it immediately after the last line of our parent group if we
903 // have it or in the very beginning if we're the root group.
904 LineList
*ConfigGroup::GetGroupLine()
906 if ( m_pLine
== NULL
) {
907 ConfigGroup
*pParent
= Parent();
909 // this group wasn't present in local config file, add it now
910 if ( pParent
!= NULL
) {
911 wxString strFullName
;
912 strFullName
<< "[" << (GetFullName().c_str() + 1) << "]"; // +1: no '/'
913 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
914 pParent
->GetLastGroupLine());
915 pParent
->SetLastGroup(this); // we're surely after all the others
918 // we return NULL, so that LineListInsert() will insert us in the
926 // Return the last line belonging to the subgroups of this group (after which
927 // we can add a new subgroup), if we don't have any subgroups or entries our
928 // last line is the group line (m_pLine) itself.
929 LineList
*ConfigGroup::GetLastGroupLine()
931 // if we have any subgroups, our last line is the last line of the last
933 if ( m_pLastGroup
!= NULL
) {
934 LineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
936 wxASSERT( pLine
!= NULL
); // last group must have !NULL associated line
940 // no subgroups, so the last line is the line of thelast entry (if any)
941 return GetLastEntryLine();
944 // return the last line belonging to the entries of this group (after which
945 // we can add a new entry), if we don't have any entries we will add the new
946 // one immediately after the group line itself.
947 LineList
*ConfigGroup::GetLastEntryLine()
949 if ( m_pLastEntry
!= NULL
) {
950 LineList
*pLine
= m_pLastEntry
->GetLine();
952 wxASSERT( pLine
!= NULL
); // last entry must have !NULL associated line
956 // no entries: insert after the group header
957 return GetGroupLine();
960 // ----------------------------------------------------------------------------
962 // ----------------------------------------------------------------------------
964 void ConfigGroup::Rename(const wxString
& newName
)
968 LineList
*line
= GetGroupLine();
969 wxString strFullName
;
970 strFullName
<< "[" << (GetFullName().c_str() + 1) << "]"; // +1: no '/'
971 line
->SetText(strFullName
);
976 wxString
ConfigGroup::GetFullName() const
979 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
984 // ----------------------------------------------------------------------------
986 // ----------------------------------------------------------------------------
988 // use binary search because the array is sorted
990 ConfigGroup::FindEntry(const char *szName
) const
994 hi
= m_aEntries
.Count();
1000 pEntry
= m_aEntries
[i
];
1002 #if wxCONFIG_CASE_SENSITIVE
1003 res
= strcmp(pEntry
->Name(), szName
);
1005 res
= Stricmp(pEntry
->Name(), szName
);
1020 ConfigGroup::FindSubgroup(const char *szName
) const
1024 hi
= m_aSubgroups
.Count();
1026 ConfigGroup
*pGroup
;
1030 pGroup
= m_aSubgroups
[i
];
1032 #if wxCONFIG_CASE_SENSITIVE
1033 res
= strcmp(pGroup
->Name(), szName
);
1035 res
= Stricmp(pGroup
->Name(), szName
);
1049 // ----------------------------------------------------------------------------
1050 // create a new item
1051 // ----------------------------------------------------------------------------
1053 // create a new entry and add it to the current group
1055 ConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1057 wxASSERT( FindEntry(strName
) == NULL
);
1059 ConfigEntry
*pEntry
= new ConfigEntry(this, strName
, nLine
);
1060 m_aEntries
.Add(pEntry
);
1065 // create a new group and add it to the current group
1067 ConfigGroup::AddSubgroup(const wxString
& strName
)
1069 wxASSERT( FindSubgroup(strName
) == NULL
);
1071 ConfigGroup
*pGroup
= new ConfigGroup(this, strName
, m_pConfig
);
1072 m_aSubgroups
.Add(pGroup
);
1077 // ----------------------------------------------------------------------------
1079 // ----------------------------------------------------------------------------
1082 The delete operations are _very_ slow if we delete the last item of this
1083 group (see comments before GetXXXLineXXX functions for more details),
1084 so it's much better to start with the first entry/group if we want to
1085 delete several of them.
1088 bool ConfigGroup::DeleteSubgroupByName(const char *szName
)
1090 return DeleteSubgroup(FindSubgroup(szName
));
1093 // doesn't delete the subgroup itself, but does remove references to it from
1094 // all other data structures (and normally the returned pointer should be
1095 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1096 bool ConfigGroup::DeleteSubgroup(ConfigGroup
*pGroup
)
1098 wxCHECK( pGroup
!= NULL
, FALSE
); // deleting non existing group?
1100 // delete all entries
1101 size_t nCount
= pGroup
->m_aEntries
.Count();
1102 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ ) {
1103 LineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1104 if ( pLine
!= NULL
)
1105 m_pConfig
->LineListRemove(pLine
);
1108 // and subgroups of this sungroup
1109 nCount
= pGroup
->m_aSubgroups
.Count();
1110 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ ) {
1111 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[nGroup
]);
1114 LineList
*pLine
= pGroup
->m_pLine
;
1115 if ( pLine
!= NULL
) {
1116 // notice that we may do this test inside the previous "if" because the
1117 // last entry's line is surely !NULL
1118 if ( pGroup
== m_pLastGroup
) {
1119 // our last entry is being deleted - find the last one which stays
1120 wxASSERT( m_pLine
!= NULL
); // we have a subgroup with !NULL pLine...
1122 // go back until we find a subgroup or reach the group's line
1123 ConfigGroup
*pNewLast
= NULL
;
1124 size_t n
, nSubgroups
= m_aSubgroups
.Count();
1126 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1127 // is it our subgroup?
1128 for ( n
= 0; (pNewLast
== NULL
) && (n
< nSubgroups
); n
++ ) {
1129 // do _not_ call GetGroupLine! we don't want to add it to the local
1130 // file if it's not already there
1131 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1132 pNewLast
= m_aSubgroups
[n
];
1135 if ( pNewLast
!= NULL
) // found?
1139 if ( pl
== m_pLine
) {
1140 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1142 // we've reached the group line without finding any subgroups
1143 m_pLastGroup
= NULL
;
1146 m_pLastGroup
= pNewLast
;
1149 m_pConfig
->LineListRemove(pLine
);
1154 m_aSubgroups
.Remove(pGroup
);
1160 bool ConfigGroup::DeleteEntry(const char *szName
)
1162 ConfigEntry
*pEntry
= FindEntry(szName
);
1163 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1165 LineList
*pLine
= pEntry
->GetLine();
1166 if ( pLine
!= NULL
) {
1167 // notice that we may do this test inside the previous "if" because the
1168 // last entry's line is surely !NULL
1169 if ( pEntry
== m_pLastEntry
) {
1170 // our last entry is being deleted - find the last one which stays
1171 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1173 // go back until we find another entry or reach the group's line
1174 ConfigEntry
*pNewLast
= NULL
;
1175 size_t n
, nEntries
= m_aEntries
.Count();
1177 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1178 // is it our subgroup?
1179 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1180 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1181 pNewLast
= m_aEntries
[n
];
1184 if ( pNewLast
!= NULL
) // found?
1188 if ( pl
== m_pLine
) {
1189 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1191 // we've reached the group line without finding any subgroups
1192 m_pLastEntry
= NULL
;
1195 m_pLastEntry
= pNewLast
;
1198 m_pConfig
->LineListRemove(pLine
);
1201 // we must be written back for the changes to be saved
1204 m_aEntries
.Remove(pEntry
);
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1213 void ConfigGroup::SetDirty()
1216 if ( Parent() != NULL
) // propagate upwards
1217 Parent()->SetDirty();
1220 // ============================================================================
1221 // wxFileConfig::ConfigEntry
1222 // ============================================================================
1224 // ----------------------------------------------------------------------------
1226 // ----------------------------------------------------------------------------
1227 ConfigEntry::ConfigEntry(ConfigGroup
*pParent
,
1228 const wxString
& strName
,
1230 : m_strName(strName
)
1232 wxASSERT( !strName
.IsEmpty() );
1234 m_pParent
= pParent
;
1240 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1242 m_strName
.erase(0, 1); // remove first character
1245 // ----------------------------------------------------------------------------
1247 // ----------------------------------------------------------------------------
1249 void ConfigEntry::SetLine(LineList
*pLine
)
1251 if ( m_pLine
!= NULL
) {
1252 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1253 Name().c_str(), m_pParent
->GetFullName().c_str());
1257 Group()->SetLastEntry(this);
1260 // second parameter is FALSE if we read the value from file and prevents the
1261 // entry from being marked as 'dirty'
1262 void ConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1264 if ( bUser
&& IsImmutable() ) {
1265 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1270 // do nothing if it's the same value
1271 if ( strValue
== m_strValue
)
1274 m_strValue
= strValue
;
1277 wxString strVal
= FilterOut(strValue
);
1279 strLine
<< m_strName
<< " = " << strVal
;
1281 if ( m_pLine
!= NULL
) {
1282 // entry was read from the local config file, just modify the line
1283 m_pLine
->SetText(strLine
);
1286 // add a new line to the file
1287 wxASSERT( m_nLine
== wxNOT_FOUND
); // consistency check
1289 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1290 Group()->GetLastEntryLine());
1291 Group()->SetLastEntry(this);
1298 void ConfigEntry::SetDirty()
1301 Group()->SetDirty();
1304 // ============================================================================
1306 // ============================================================================
1308 // ----------------------------------------------------------------------------
1309 // compare functions for array sorting
1310 // ----------------------------------------------------------------------------
1312 int CompareEntries(ConfigEntry
*p1
,
1315 #if wxCONFIG_CASE_SENSITIVE
1316 return strcmp(p1
->Name(), p2
->Name());
1318 return Stricmp(p1
->Name(), p2
->Name());
1322 int CompareGroups(ConfigGroup
*p1
,
1325 #if wxCONFIG_CASE_SENSITIVE
1326 return strcmp(p1
->Name(), p2
->Name());
1328 return Stricmp(p1
->Name(), p2
->Name());
1332 // ----------------------------------------------------------------------------
1334 // ----------------------------------------------------------------------------
1337 wxString
FilterIn(const wxString
& str
)
1340 strResult
.Alloc(str
.Len());
1342 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1344 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1345 if ( str
[n
] == '\\' ) {
1346 switch ( str
[++n
] ) {
1369 if ( str
[n
] != '"' || !bQuoted
)
1370 strResult
+= str
[n
];
1371 else if ( n
!= str
.Len() - 1 ) {
1372 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1375 //else: it's the last quote of a quoted string, ok
1382 // quote the string before writing it to file
1383 wxString
FilterOut(const wxString
& str
)
1389 strResult
.Alloc(str
.Len());
1391 // quoting is necessary to preserve spaces in the beginning of the string
1392 bool bQuote
= isspace(str
[0]) || str
[0] == '"';
1398 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1421 //else: fall through
1424 strResult
+= str
[n
];
1425 continue; // nothing special to do
1428 // we get here only for special characters
1429 strResult
<< '\\' << c
;