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>
35 #include <wx/dynarray.h>
38 #include <wx/textfile.h>
39 #include <wx/config.h>
40 #include <wx/fileconf.h>
42 // _WINDOWS_ is defined when windows.h is included,
43 // __WXMSW__ is defined for MS Windows compilation
44 #if defined(__WXMSW__) && !defined(_WINDOWS_)
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
54 #define CONST_CAST ((wxFileConfig *)this)->
56 // ----------------------------------------------------------------------------
57 // global functions declarations
58 // ----------------------------------------------------------------------------
60 // is 'c' a valid character in group name?
61 // NB: wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR must be valid chars,
62 // but _not_ ']' (group name delimiter)
63 inline bool IsValid(char c
) { return isalnum(c
) || strchr("@_/-!.*%", c
); }
65 // compare functions for sorting the arrays
66 static int CompareEntries(wxFileConfig::ConfigEntry
*p1
,
67 wxFileConfig::ConfigEntry
*p2
);
68 static int CompareGroups(wxFileConfig::ConfigGroup
*p1
,
69 wxFileConfig::ConfigGroup
*p2
);
72 static wxString
FilterIn(const wxString
& str
);
73 static wxString
FilterOut(const wxString
& str
);
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
82 wxString
wxFileConfig::GetGlobalDir()
93 char szWinDir
[_MAX_PATH
];
94 ::GetWindowsDirectory(szWinDir
, _MAX_PATH
);
98 #endif // Unix/Windows
103 wxString
wxFileConfig::GetLocalDir()
108 const char *szHome
= getenv("HOME");
109 if ( szHome
== NULL
) {
111 wxLogWarning(_("can't find user's HOME, using current directory."));
116 strDir
<< '/'; // a double slash is no problem, a missin one yes
119 const char *szHome
= getenv("HOMEDRIVE");
120 if ( szHome
!= NULL
)
122 szHome
= getenv("HOMEPATH");
123 if ( szHome
!= NULL
)
126 // Win16 has no idea about home, so use the current directory instead
134 wxString
wxFileConfig::GetGlobalFileName(const char *szFile
)
136 wxString str
= GetGlobalDir();
139 if ( strchr(szFile
, '.') == NULL
)
149 wxString
wxFileConfig::GetLocalFileName(const char *szFile
)
151 wxString str
= GetLocalDir();
160 if ( strchr(szFile
, '.') == NULL
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxFileConfig::Init()
174 m_pRootGroup
= new ConfigGroup(NULL
, "", this);
179 // it's not an error if (one of the) file(s) doesn't exist
181 // parse the global file
182 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) ) {
183 wxTextFile
fileGlobal(m_strGlobalFile
);
185 if ( fileGlobal
.Open() ) {
186 Parse(fileGlobal
, FALSE
/* global */);
190 wxLogWarning(_("can't open global configuration file '%s'."),
191 m_strGlobalFile
.c_str());
194 // parse the local file
195 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) ) {
196 wxTextFile
fileLocal(m_strLocalFile
);
197 if ( fileLocal
.Open() ) {
198 Parse(fileLocal
, TRUE
/* local */);
202 wxLogWarning(_("can't open user configuration file '%s'."),
203 m_strLocalFile
.c_str());
207 wxFileConfig::wxFileConfig(const char *szAppName
, bool bLocalOnly
)
209 wxASSERT( !IsEmpty(szAppName
) ); // invent a name for your application!
211 m_strLocalFile
= GetLocalFileName(szAppName
);
213 m_strGlobalFile
= GetGlobalFileName(szAppName
);
214 //else: it's going to be empty and we won't use the global file
219 wxFileConfig::wxFileConfig(const wxString
& strLocal
, const wxString
& strGlobal
)
220 : m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
222 // if the path is not absolute, prepend the standard directory to it
223 if ( !strLocal
.IsEmpty() && !wxIsAbsolutePath(strLocal
) )
225 m_strLocalFile
= GetLocalDir();
226 m_strLocalFile
<< strLocal
;
229 if ( !strGlobal
.IsEmpty() && !wxIsAbsolutePath(strGlobal
) )
231 m_strGlobalFile
= GetGlobalDir();
232 m_strGlobalFile
<< strGlobal
;
238 void wxFileConfig::CleanUp()
242 LineList
*pCur
= m_linesHead
;
243 while ( pCur
!= NULL
) {
244 LineList
*pNext
= pCur
->Next();
250 wxFileConfig::~wxFileConfig()
257 // ----------------------------------------------------------------------------
258 // parse a config file
259 // ----------------------------------------------------------------------------
261 void wxFileConfig::Parse(wxTextFile
& file
, bool bLocal
)
267 uint nLineCount
= file
.GetLineCount();
268 for ( uint n
= 0; n
< nLineCount
; n
++ ) {
271 // add the line to linked list
273 LineListAppend(strLine
);
275 // skip leading spaces
276 for ( pStart
= strLine
; isspace(*pStart
); pStart
++ )
279 // skip blank/comment lines
280 if ( *pStart
== '\0'|| *pStart
== ';' || *pStart
== '#' )
283 if ( *pStart
== '[' ) { // a new group
286 while ( *++pEnd
!= ']' ) {
287 if ( !IsValid(*pEnd
) && *pEnd
!= ' ' ) // allow spaces in group names
291 if ( *pEnd
!= ']' ) {
292 wxLogError(_("file '%s': unexpected character %c at line %d."),
293 file
.GetName(), *pEnd
, n
+ 1);
294 continue; // skip this line
297 // group name here is always considered as abs path
300 strGroup
<< wxCONFIG_PATH_SEPARATOR
<< wxString(pStart
, pEnd
- pStart
);
302 // will create it if doesn't yet exist
306 m_pCurrentGroup
->SetLine(m_linesTail
);
308 // check that there is nothing except comments left on this line
310 while ( *++pEnd
!= '\0' && bCont
) {
319 // ignore whitespace ('\n' impossible here)
323 wxLogWarning(_("file '%s', line %d: '%s' "
324 "ignored after group header."),
325 file
.GetName(), n
+ 1, pEnd
);
331 const char *pEnd
= pStart
;
332 while ( IsValid(*pEnd
) )
335 wxString
strKey(pStart
, pEnd
);
338 while ( isspace(*pEnd
) )
341 if ( *pEnd
++ != '=' ) {
342 wxLogError(_("file '%s', line %d: '=' expected."),
343 file
.GetName(), n
+ 1);
346 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
348 if ( pEntry
== NULL
) {
350 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
353 pEntry
->SetLine(m_linesTail
);
356 if ( bLocal
&& pEntry
->IsImmutable() ) {
357 // immutable keys can't be changed by user
358 wxLogWarning(_("file '%s', line %d: value for "
359 "immutable key '%s' ignored."),
360 file
.GetName(), n
+ 1, strKey
.c_str());
363 // the condition below catches the cases (a) and (b) but not (c):
364 // (a) global key found second time in global file
365 // (b) key found second (or more) time in local file
366 // (c) key from global file now found in local one
367 // which is exactly what we want.
368 else if ( !bLocal
|| pEntry
->IsLocal() ) {
369 wxLogWarning(_("file '%s', line %d: key '%s' was first "
370 "found at line %d."),
371 file
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
374 pEntry
->SetLine(m_linesTail
);
379 while ( isspace(*pEnd
) )
382 pEntry
->SetValue(FilterIn(pEnd
), FALSE
/* read from file */);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 void wxFileConfig::SetRootPath()
395 m_pCurrentGroup
= m_pRootGroup
;
398 void wxFileConfig::SetPath(const wxString
& strPath
)
400 wxArrayString aParts
;
402 if ( strPath
.IsEmpty() ) {
407 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
409 wxSplitPath(aParts
, strPath
);
412 // relative path, combine with current one
413 wxString strFullPath
= m_strPath
;
414 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
415 wxSplitPath(aParts
, strFullPath
);
418 // change current group
420 m_pCurrentGroup
= m_pRootGroup
;
421 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
422 ConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
423 if ( pNextGroup
== NULL
)
424 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
425 m_pCurrentGroup
= pNextGroup
;
428 // recombine path parts in one variable
430 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
431 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
442 return GetNextGroup(str
, lIndex
);
445 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
447 if ( uint(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
448 str
= m_pCurrentGroup
->Groups()[lIndex
++]->Name();
455 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
458 return GetNextEntry(str
, lIndex
);
461 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
463 if ( uint(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
464 str
= m_pCurrentGroup
->Entries()[lIndex
++]->Name();
471 uint
wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
473 uint n
= m_pCurrentGroup
->Entries().Count();
475 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
476 uint nSubgroups
= m_pCurrentGroup
->Groups().Count();
477 for ( uint nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
478 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
479 n
+= GetNumberOfEntries(TRUE
);
480 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
487 uint
wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
489 uint n
= m_pCurrentGroup
->Groups().Count();
491 ConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
492 uint nSubgroups
= m_pCurrentGroup
->Groups().Count();
493 for ( uint nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
494 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
495 n
+= GetNumberOfGroups(TRUE
);
496 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
503 // ----------------------------------------------------------------------------
504 // tests for existence
505 // ----------------------------------------------------------------------------
507 bool wxFileConfig::HasGroup(const wxString
& strName
) const
509 PathChanger
path(this, strName
);
511 ConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
512 return pGroup
!= NULL
;
515 bool wxFileConfig::HasEntry(const wxString
& strName
) const
517 PathChanger
path(this, strName
);
519 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
520 return pEntry
!= NULL
;
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 bool wxFileConfig::Read(wxString
*pstr
,
529 const char *szDefault
) const
531 PathChanger
path(this, szKey
);
533 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
534 if (pEntry
== NULL
) {
535 if( IsRecordingDefaults() )
536 ((wxFileConfig
*)this)->Write(szKey
,szDefault
);
537 *pstr
= ExpandEnvVars(szDefault
);
541 *pstr
= ExpandEnvVars(pEntry
->Value());
546 const char *wxFileConfig::Read(const char *szKey
,
547 const char *szDefault
) const
549 static wxString s_str
;
550 Read(&s_str
, szKey
, szDefault
);
552 return s_str
.c_str();
555 bool wxFileConfig::Read(long *pl
, const char *szKey
, long lDefault
) const
558 if ( Read(&str
, szKey
) ) {
568 bool wxFileConfig::Write(const char *szKey
, const char *szValue
)
570 PathChanger
path(this, szKey
);
572 wxString strName
= path
.Name();
573 if ( strName
.IsEmpty() ) {
574 // setting the value of a group is an error
575 wxASSERT_MSG( IsEmpty(szValue
), "can't set value of a group!" );
577 // ... except if it's empty in which case it's a way to force it's creation
578 m_pCurrentGroup
->SetDirty();
580 // this will add a line for this group if it didn't have it before
581 (void)m_pCurrentGroup
->GetGroupLine();
586 // check that the name is reasonable
587 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
) {
588 wxLogError(_("Entry name can't start with '%c'."),
589 wxCONFIG_IMMUTABLE_PREFIX
);
593 for ( const char *pc
= strName
; *pc
!= '\0'; pc
++ ) {
594 if ( !IsValid(*pc
) ) {
595 wxLogError(_("Character '%c' is invalid in a config entry name."),
601 ConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
602 if ( pEntry
== NULL
)
603 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
605 pEntry
->SetValue(szValue
);
611 bool wxFileConfig::Write(const char *szKey
, long lValue
)
613 // ltoa() is not ANSI :-(
614 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
615 sprintf(szBuf
, "%ld", lValue
);
616 return Write(szKey
, szBuf
);
619 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
621 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() )
624 wxTempFile
file(m_strLocalFile
);
626 if ( !file
.IsOpened() ) {
627 wxLogError(_("can't open user configuration file."));
631 // write all strings to file
632 for ( LineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() ) {
633 if ( !file
.Write(p
->Text() + wxTextFile::GetEOL()) ) {
634 wxLogError(_("can't write user configuration file."));
639 return file
.Commit();
642 // ----------------------------------------------------------------------------
643 // delete groups/entries
644 // ----------------------------------------------------------------------------
646 bool wxFileConfig::DeleteEntry(const char *szKey
, bool bGroupIfEmptyAlso
)
648 PathChanger
path(this, szKey
);
650 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
653 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
654 if ( m_pCurrentGroup
!= m_pRootGroup
) {
655 ConfigGroup
*pGroup
= m_pCurrentGroup
;
656 SetPath(".."); // changes m_pCurrentGroup!
657 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
659 //else: never delete the root group
665 bool wxFileConfig::DeleteGroup(const char *szKey
)
667 PathChanger
path(this, szKey
);
669 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
672 bool wxFileConfig::DeleteAll()
676 const char *szFile
= m_strLocalFile
;
678 if ( remove(szFile
) == -1 )
679 wxLogSysError(_("can't delete user configuration file '%s'"), szFile
);
681 m_strLocalFile
= m_strGlobalFile
= "";
687 // ----------------------------------------------------------------------------
688 // linked list functions
689 // ----------------------------------------------------------------------------
691 // append a new line to the end of the list
692 wxFileConfig::LineList
*wxFileConfig::LineListAppend(const wxString
& str
)
694 LineList
*pLine
= new LineList(str
);
696 if ( m_linesTail
== NULL
) {
702 m_linesTail
->SetNext(pLine
);
703 pLine
->SetPrev(m_linesTail
);
710 // insert a new line after the given one or in the very beginning if !pLine
711 wxFileConfig::LineList
*wxFileConfig::LineListInsert(const wxString
& str
,
714 if ( pLine
== m_linesTail
)
715 return LineListAppend(str
);
717 LineList
*pNewLine
= new LineList(str
);
718 if ( pLine
== NULL
) {
719 // prepend to the list
720 pNewLine
->SetNext(m_linesHead
);
721 m_linesHead
->SetPrev(pNewLine
);
722 m_linesHead
= pNewLine
;
725 // insert before pLine
726 LineList
*pNext
= pLine
->Next();
727 pNewLine
->SetNext(pNext
);
728 pNewLine
->SetPrev(pLine
);
729 pNext
->SetPrev(pNewLine
);
730 pLine
->SetNext(pNewLine
);
736 void wxFileConfig::LineListRemove(LineList
*pLine
)
738 LineList
*pPrev
= pLine
->Prev(),
739 *pNext
= pLine
->Next();
745 pPrev
->SetNext(pNext
);
751 pNext
->SetPrev(pPrev
);
756 bool wxFileConfig::LineListIsEmpty()
758 return m_linesHead
== NULL
;
761 // ============================================================================
762 // wxFileConfig::ConfigGroup
763 // ============================================================================
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
770 wxFileConfig::ConfigGroup::ConfigGroup(wxFileConfig::ConfigGroup
*pParent
,
771 const wxString
& strName
,
772 wxFileConfig
*pConfig
)
773 : m_aEntries(CompareEntries
),
774 m_aSubgroups(CompareGroups
),
786 // dtor deletes all children
787 wxFileConfig::ConfigGroup::~ConfigGroup()
790 uint n
, nCount
= m_aEntries
.Count();
791 for ( n
= 0; n
< nCount
; n
++ )
792 delete m_aEntries
[n
];
795 nCount
= m_aSubgroups
.Count();
796 for ( n
= 0; n
< nCount
; n
++ )
797 delete m_aSubgroups
[n
];
800 // ----------------------------------------------------------------------------
802 // ----------------------------------------------------------------------------
804 void wxFileConfig::ConfigGroup::SetLine(LineList
*pLine
)
806 wxASSERT( m_pLine
== NULL
); // shouldn't be called twice
812 This is a bit complicated, so let me explain it in details. All lines that
813 were read from the local file (the only one we will ever modify) are stored
814 in a (doubly) linked list. Our problem is to know at which position in this
815 list should we insert the new entries/subgroups. To solve it we keep three
816 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
818 m_pLine points to the line containing "[group_name]"
819 m_pLastEntry points to the last entry of this group in the local file.
820 m_pLastGroup subgroup
822 Initially, they're NULL all three. When the group (an entry/subgroup) is read
823 from the local file, the corresponding variable is set. However, if the group
824 was read from the global file and then modified or created by the application
825 these variables are still NULL and we need to create the corresponding lines.
826 See the following functions (and comments preceding them) for the details of
829 Also, when our last entry/group are deleted we need to find the new last
830 element - the code in DeleteEntry/Subgroup does this by backtracking the list
831 of lines until it either founds an entry/subgroup (and this is the new last
832 element) or the m_pLine of the group, in which case there are no more entries
833 (or subgroups) left and m_pLast<element> becomes NULL.
835 NB: This last problem could be avoided for entries if we added new entries
836 immediately after m_pLine, but in this case the entries would appear
837 backwards in the config file (OTOH, it's not that important) and as we
838 would still need to do it for the subgroups the code wouldn't have been
839 significantly less complicated.
842 // Return the line which contains "[our name]". If we're still not in the list,
843 // add our line to it immediately after the last line of our parent group if we
844 // have it or in the very beginning if we're the root group.
845 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetGroupLine()
847 if ( m_pLine
== NULL
) {
848 ConfigGroup
*pParent
= Parent();
850 // this group wasn't present in local config file, add it now
851 if ( pParent
!= NULL
) {
852 wxString strFullName
;
853 strFullName
<< "[" << GetFullName().c_str() + 1 << "]"; // +1: no '/'
854 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
855 pParent
->GetLastGroupLine());
856 pParent
->SetLastGroup(this); // we're surely after all the others
859 // we return NULL, so that LineListInsert() will insert us in the
867 // Return the last line belonging to the subgroups of this group (after which
868 // we can add a new subgroup), if we don't have any subgroups or entries our
869 // last line is the group line (m_pLine) itself.
870 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetLastGroupLine()
872 // if we have any subgroups, our last line is the last line of the last
874 if ( m_pLastGroup
!= NULL
) {
875 wxFileConfig::LineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
877 wxASSERT( pLine
!= NULL
); // last group must have !NULL associated line
881 // no subgroups, so the last line is the line of thelast entry (if any)
882 return GetLastEntryLine();
885 // return the last line belonging to the entries of this group (after which
886 // we can add a new entry), if we don't have any entries we will add the new
887 // one immediately after the group line itself.
888 wxFileConfig::LineList
*wxFileConfig::ConfigGroup::GetLastEntryLine()
890 if ( m_pLastEntry
!= NULL
) {
891 wxFileConfig::LineList
*pLine
= m_pLastEntry
->GetLine();
893 wxASSERT( pLine
!= NULL
); // last entry must have !NULL associated line
897 // no entries: insert after the group header
898 return GetGroupLine();
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
905 wxString
wxFileConfig::ConfigGroup::GetFullName() const
908 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 // use binary search because the array is sorted
918 wxFileConfig::ConfigEntry
*
919 wxFileConfig::ConfigGroup::FindEntry(const char *szName
) const
923 hi
= m_aEntries
.Count();
925 wxFileConfig::ConfigEntry
*pEntry
;
929 pEntry
= m_aEntries
[i
];
931 #if wxCONFIG_CASE_SENSITIVE
932 res
= strcmp(pEntry
->Name(), szName
);
934 res
= Stricmp(pEntry
->Name(), szName
);
948 wxFileConfig::ConfigGroup
*
949 wxFileConfig::ConfigGroup::FindSubgroup(const char *szName
) const
953 hi
= m_aSubgroups
.Count();
955 wxFileConfig::ConfigGroup
*pGroup
;
959 pGroup
= m_aSubgroups
[i
];
961 #if wxCONFIG_CASE_SENSITIVE
962 res
= strcmp(pGroup
->Name(), szName
);
964 res
= Stricmp(pGroup
->Name(), szName
);
978 // ----------------------------------------------------------------------------
980 // ----------------------------------------------------------------------------
982 // create a new entry and add it to the current group
983 wxFileConfig::ConfigEntry
*
984 wxFileConfig::ConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
986 wxASSERT( FindEntry(strName
) == NULL
);
988 ConfigEntry
*pEntry
= new ConfigEntry(this, strName
, nLine
);
989 m_aEntries
.Add(pEntry
);
994 // create a new group and add it to the current group
995 wxFileConfig::ConfigGroup
*
996 wxFileConfig::ConfigGroup::AddSubgroup(const wxString
& strName
)
998 wxASSERT( FindSubgroup(strName
) == NULL
);
1000 ConfigGroup
*pGroup
= new ConfigGroup(this, strName
, m_pConfig
);
1001 m_aSubgroups
.Add(pGroup
);
1006 // ----------------------------------------------------------------------------
1008 // ----------------------------------------------------------------------------
1011 The delete operations are _very_ slow if we delete the last item of this
1012 group (see comments before GetXXXLineXXX functions for more details),
1013 so it's much better to start with the first entry/group if we want to
1014 delete several of them.
1017 bool wxFileConfig::ConfigGroup::DeleteSubgroupByName(const char *szName
)
1019 return DeleteSubgroup(FindSubgroup(szName
));
1022 // doesn't delete the subgroup itself, but does remove references to it from
1023 // all other data structures (and normally the returned pointer should be
1024 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1025 bool wxFileConfig::ConfigGroup::DeleteSubgroup(ConfigGroup
*pGroup
)
1027 wxCHECK( pGroup
!= NULL
, FALSE
); // deleting non existing group?
1029 // delete all entries
1030 uint nCount
= pGroup
->m_aEntries
.Count();
1031 for ( uint nEntry
= 0; nEntry
< nCount
; nEntry
++ ) {
1032 LineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1033 if ( pLine
!= NULL
)
1034 m_pConfig
->LineListRemove(pLine
);
1037 // and subgroups of this sungroup
1038 nCount
= pGroup
->m_aSubgroups
.Count();
1039 for ( uint nGroup
= 0; nGroup
< nCount
; nGroup
++ ) {
1040 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[nGroup
]);
1043 LineList
*pLine
= pGroup
->m_pLine
;
1044 if ( pLine
!= NULL
) {
1045 // notice that we may do this test inside the previous "if" because the
1046 // last entry's line is surely !NULL
1047 if ( pGroup
== m_pLastGroup
) {
1048 // our last entry is being deleted - find the last one which stays
1049 wxASSERT( m_pLine
!= NULL
); // we have a subgroup with !NULL pLine...
1051 // go back until we find a subgroup or reach the group's line
1052 ConfigGroup
*pNewLast
= NULL
;
1053 uint n
, nSubgroups
= m_aSubgroups
.Count();
1055 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1056 // is it our subgroup?
1057 for ( n
= 0; (pNewLast
== NULL
) && (n
< nSubgroups
); n
++ ) {
1058 // do _not_ call GetGroupLine! we don't want to add it to the local
1059 // file if it's not already there
1060 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1061 pNewLast
= m_aSubgroups
[n
];
1064 if ( pNewLast
!= NULL
) // found?
1068 if ( pl
== m_pLine
) {
1069 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1071 // we've reached the group line without finding any subgroups
1072 m_pLastGroup
= NULL
;
1075 m_pLastGroup
= pNewLast
;
1078 m_pConfig
->LineListRemove(pLine
);
1083 m_aSubgroups
.Remove(pGroup
);
1089 bool wxFileConfig::ConfigGroup::DeleteEntry(const char *szName
)
1091 ConfigEntry
*pEntry
= FindEntry(szName
);
1092 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1094 LineList
*pLine
= pEntry
->GetLine();
1095 if ( pLine
!= NULL
) {
1096 // notice that we may do this test inside the previous "if" because the
1097 // last entry's line is surely !NULL
1098 if ( pEntry
== m_pLastEntry
) {
1099 // our last entry is being deleted - find the last one which stays
1100 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1102 // go back until we find another entry or reach the group's line
1103 ConfigEntry
*pNewLast
= NULL
;
1104 uint n
, nEntries
= m_aEntries
.Count();
1106 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1107 // is it our subgroup?
1108 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1109 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1110 pNewLast
= m_aEntries
[n
];
1113 if ( pNewLast
!= NULL
) // found?
1117 if ( pl
== m_pLine
) {
1118 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1120 // we've reached the group line without finding any subgroups
1121 m_pLastEntry
= NULL
;
1124 m_pLastEntry
= pNewLast
;
1127 m_pConfig
->LineListRemove(pLine
);
1130 // we must be written back for the changes to be saved
1133 m_aEntries
.Remove(pEntry
);
1139 // ----------------------------------------------------------------------------
1141 // ----------------------------------------------------------------------------
1142 void wxFileConfig::ConfigGroup::SetDirty()
1145 if ( Parent() != NULL
) // propagate upwards
1146 Parent()->SetDirty();
1149 // ============================================================================
1150 // wxFileConfig::ConfigEntry
1151 // ============================================================================
1153 // ----------------------------------------------------------------------------
1155 // ----------------------------------------------------------------------------
1156 wxFileConfig::ConfigEntry::ConfigEntry(wxFileConfig::ConfigGroup
*pParent
,
1157 const wxString
& strName
,
1159 : m_strName(strName
)
1161 wxASSERT( !strName
.IsEmpty() );
1163 m_pParent
= pParent
;
1169 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1171 m_strName
.erase(0, 1); // remove first character
1174 // ----------------------------------------------------------------------------
1176 // ----------------------------------------------------------------------------
1178 void wxFileConfig::ConfigEntry::SetLine(LineList
*pLine
)
1180 if ( m_pLine
!= NULL
) {
1181 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1182 Name().c_str(), m_pParent
->GetFullName().c_str());
1186 Group()->SetLastEntry(this);
1189 // second parameter is FALSE if we read the value from file and prevents the
1190 // entry from being marked as 'dirty'
1191 void wxFileConfig::ConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1193 if ( bUser
&& IsImmutable() ) {
1194 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1199 // do nothing if it's the same value
1200 if ( strValue
== m_strValue
)
1203 m_strValue
= strValue
;
1206 wxString strVal
= FilterOut(strValue
);
1208 strLine
<< m_strName
<< " = " << strVal
;
1210 if ( m_pLine
!= NULL
) {
1211 // entry was read from the local config file, just modify the line
1212 m_pLine
->SetText(strLine
);
1215 // add a new line to the file
1216 wxASSERT( m_nLine
== NOT_FOUND
); // consistency check
1218 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1219 Group()->GetLastEntryLine());
1220 Group()->SetLastEntry(this);
1227 void wxFileConfig::ConfigEntry::SetDirty()
1230 Group()->SetDirty();
1233 // ============================================================================
1235 // ============================================================================
1237 // ----------------------------------------------------------------------------
1238 // compare functions for array sorting
1239 // ----------------------------------------------------------------------------
1241 int CompareEntries(wxFileConfig::ConfigEntry
*p1
,
1242 wxFileConfig::ConfigEntry
*p2
)
1244 #if wxCONFIG_CASE_SENSITIVE
1245 return strcmp(p1
->Name(), p2
->Name());
1247 return Stricmp(p1
->Name(), p2
->Name());
1251 int CompareGroups(wxFileConfig::ConfigGroup
*p1
,
1252 wxFileConfig::ConfigGroup
*p2
)
1254 #if wxCONFIG_CASE_SENSITIVE
1255 return strcmp(p1
->Name(), p2
->Name());
1257 return Stricmp(p1
->Name(), p2
->Name());
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1266 wxString
FilterIn(const wxString
& str
)
1269 strResult
.Alloc(str
.Len());
1271 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1273 for ( uint n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1274 if ( str
[n
] == '\\' ) {
1275 switch ( str
[++n
] ) {
1298 if ( str
[n
] != '"' || !bQuoted
)
1299 strResult
+= str
[n
];
1300 else if ( n
!= str
.Len() - 1 ) {
1301 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1304 //else: it's the last quote of a quoted string, ok
1311 // quote the string before writing it to file
1312 wxString
FilterOut(const wxString
& str
)
1315 strResult
.Alloc(str
.Len());
1317 // quoting is necessary to preserve spaces in the beginning of the string
1318 bool bQuote
= isspace(str
[0]) || str
[0] == '"';
1324 for ( uint n
= 0; n
< str
.Len(); n
++ ) {
1347 //else: fall through
1350 strResult
+= str
[n
];
1351 continue; // nothing special to do
1354 // we get here only for special characters
1355 strResult
<< '\\' << c
;