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 licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "fileconf.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
27 #if wxUSE_CONFIG && wxUSE_FILECONFIG
30 #include "wx/string.h"
35 #include "wx/dynarray.h"
38 #include "wx/textfile.h"
39 #include "wx/memtext.h"
40 #include "wx/config.h"
41 #include "wx/fileconf.h"
44 #include "wx/stream.h"
45 #endif // wxUSE_STREAMS
47 #include "wx/utils.h" // for wxGetHomeDir
49 #if defined(__WXMAC__)
50 #include "wx/mac/private.h" // includes mac headers
53 #if defined(__WXMSW__)
54 #include "wx/msw/private.h"
64 // headers needed for umask()
66 #include <sys/types.h>
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
73 #define CONST_CAST ((wxFileConfig *)this)->
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
84 // global functions declarations
85 // ----------------------------------------------------------------------------
87 // compare functions for sorting the arrays
88 static int LINKAGEMODE
CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
);
89 static int LINKAGEMODE
CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
);
92 static wxString
FilterInValue(const wxString
& str
);
93 static wxString
FilterOutValue(const wxString
& str
);
95 static wxString
FilterInEntryName(const wxString
& str
);
96 static wxString
FilterOutEntryName(const wxString
& str
);
98 // get the name to use in wxFileConfig ctor
99 static wxString
GetAppName(const wxString
& appname
);
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // "template" array types
107 // ----------------------------------------------------------------------------
109 #ifdef WXMAKINGDLL_BASE
110 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
,
112 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
,
115 WX_DEFINE_SORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
);
116 WX_DEFINE_SORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
);
119 // ----------------------------------------------------------------------------
120 // wxFileConfigLineList
121 // ----------------------------------------------------------------------------
123 // we store all lines of the local config file as a linked list in memory
124 class wxFileConfigLineList
127 void SetNext(wxFileConfigLineList
*pNext
) { m_pNext
= pNext
; }
128 void SetPrev(wxFileConfigLineList
*pPrev
) { m_pPrev
= pPrev
; }
131 wxFileConfigLineList(const wxString
& str
,
132 wxFileConfigLineList
*pNext
= NULL
) : m_strLine(str
)
133 { SetNext(pNext
); SetPrev(NULL
); }
135 // next/prev nodes in the linked list
136 wxFileConfigLineList
*Next() const { return m_pNext
; }
137 wxFileConfigLineList
*Prev() const { return m_pPrev
; }
139 // get/change lines text
140 void SetText(const wxString
& str
) { m_strLine
= str
; }
141 const wxString
& Text() const { return m_strLine
; }
144 wxString m_strLine
; // line contents
145 wxFileConfigLineList
*m_pNext
, // next node
146 *m_pPrev
; // previous one
148 DECLARE_NO_COPY_CLASS(wxFileConfigLineList
)
151 // ----------------------------------------------------------------------------
152 // wxFileConfigEntry: a name/value pair
153 // ----------------------------------------------------------------------------
155 class wxFileConfigEntry
158 wxFileConfigGroup
*m_pParent
; // group that contains us
160 wxString m_strName
, // entry name
162 bool m_bDirty
:1, // changed since last read?
163 m_bImmutable
:1, // can be overriden locally?
164 m_bHasValue
:1; // set after first call to SetValue()
166 int m_nLine
; // used if m_pLine == NULL only
168 // pointer to our line in the linked list or NULL if it was found in global
169 // file (which we don't modify)
170 wxFileConfigLineList
*m_pLine
;
173 wxFileConfigEntry(wxFileConfigGroup
*pParent
,
174 const wxString
& strName
, int nLine
);
177 const wxString
& Name() const { return m_strName
; }
178 const wxString
& Value() const { return m_strValue
; }
179 wxFileConfigGroup
*Group() const { return m_pParent
; }
180 bool IsDirty() const { return m_bDirty
; }
181 bool IsImmutable() const { return m_bImmutable
; }
182 bool IsLocal() const { return m_pLine
!= 0; }
183 int Line() const { return m_nLine
; }
184 wxFileConfigLineList
*
185 GetLine() const { return m_pLine
; }
187 // modify entry attributes
188 void SetValue(const wxString
& strValue
, bool bUser
= TRUE
);
190 void SetLine(wxFileConfigLineList
*pLine
);
192 DECLARE_NO_COPY_CLASS(wxFileConfigEntry
)
195 // ----------------------------------------------------------------------------
196 // wxFileConfigGroup: container of entries and other groups
197 // ----------------------------------------------------------------------------
199 class wxFileConfigGroup
202 wxFileConfig
*m_pConfig
; // config object we belong to
203 wxFileConfigGroup
*m_pParent
; // parent group (NULL for root group)
204 ArrayEntries m_aEntries
; // entries in this group
205 ArrayGroups m_aSubgroups
; // subgroups
206 wxString m_strName
; // group's name
207 bool m_bDirty
; // if FALSE => all subgroups are not dirty
208 wxFileConfigLineList
*m_pLine
; // pointer to our line in the linked list
209 wxFileConfigEntry
*m_pLastEntry
; // last entry/subgroup of this group in the
210 wxFileConfigGroup
*m_pLastGroup
; // local file (we insert new ones after it)
212 // DeleteSubgroupByName helper
213 bool DeleteSubgroup(wxFileConfigGroup
*pGroup
);
217 wxFileConfigGroup(wxFileConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
219 // dtor deletes all entries and subgroups also
220 ~wxFileConfigGroup();
223 const wxString
& Name() const { return m_strName
; }
224 wxFileConfigGroup
*Parent() const { return m_pParent
; }
225 wxFileConfig
*Config() const { return m_pConfig
; }
226 bool IsDirty() const { return m_bDirty
; }
228 const ArrayEntries
& Entries() const { return m_aEntries
; }
229 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
230 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
232 // find entry/subgroup (NULL if not found)
233 wxFileConfigGroup
*FindSubgroup(const wxChar
*szName
) const;
234 wxFileConfigEntry
*FindEntry (const wxChar
*szName
) const;
236 // delete entry/subgroup, return FALSE if doesn't exist
237 bool DeleteSubgroupByName(const wxChar
*szName
);
238 bool DeleteEntry(const wxChar
*szName
);
240 // create new entry/subgroup returning pointer to newly created element
241 wxFileConfigGroup
*AddSubgroup(const wxString
& strName
);
242 wxFileConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= wxNOT_FOUND
);
244 // will also recursively set parent's dirty flag
246 void SetLine(wxFileConfigLineList
*pLine
);
248 // rename: no checks are done to ensure that the name is unique!
249 void Rename(const wxString
& newName
);
252 wxString
GetFullName() const;
254 // get the last line belonging to an entry/subgroup of this group
255 wxFileConfigLineList
*GetGroupLine(); // line which contains [group]
256 wxFileConfigLineList
*GetLastEntryLine(); // after which our subgroups start
257 wxFileConfigLineList
*GetLastGroupLine(); // after which the next group starts
259 // called by entries/subgroups when they're created/deleted
260 void SetLastEntry(wxFileConfigEntry
*pEntry
);
261 void SetLastGroup(wxFileConfigGroup
*pGroup
)
262 { m_pLastGroup
= pGroup
; }
264 DECLARE_NO_COPY_CLASS(wxFileConfigGroup
)
267 // ============================================================================
269 // ============================================================================
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
274 wxString
wxFileConfig::GetGlobalDir()
278 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
279 strDir
= wxT("sys$manager:");
280 #elif defined(__WXMAC__)
281 strDir
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
282 #elif defined( __UNIX__ )
283 strDir
= wxT("/etc/");
284 #elif defined(__WXPM__)
285 ULONG aulSysInfo
[QSV_MAX
] = {0};
289 rc
= DosQuerySysInfo( 1L, QSV_MAX
, (PVOID
)aulSysInfo
, sizeof(ULONG
)*QSV_MAX
);
292 drive
= aulSysInfo
[QSV_BOOT_DRIVE
- 1];
293 strDir
.Printf(wxT("%c:\\OS2\\"), 'A'+drive
-1);
295 #elif defined(__WXSTUBS__)
296 wxASSERT_MSG( FALSE
, wxT("TODO") ) ;
297 #elif defined(__DOS__)
298 // There's no such thing as global cfg dir in MS-DOS, let's return
299 // current directory (FIXME_MGL?)
302 wxChar szWinDir
[MAX_PATH
];
303 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
307 #endif // Unix/Windows
312 wxString
wxFileConfig::GetLocalDir()
316 #if defined(__WXMAC__) || defined(__DOS__)
317 // no local dir concept on Mac OS 9 or MS-DOS
318 return GetGlobalDir() ;
320 wxGetHomeDir(&strDir
);
324 if (strDir
.Last() != wxT(']'))
326 if (strDir
.Last() != wxT('/')) strDir
<< wxT('/');
328 if (strDir
.Last() != wxT('\\')) strDir
<< wxT('\\');
335 wxString
wxFileConfig::GetGlobalFileName(const wxChar
*szFile
)
337 wxString str
= GetGlobalDir();
340 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
341 #if defined( __WXMAC__ )
342 str
<< wxT(" Preferences") ;
343 #elif defined( __UNIX__ )
352 wxString
wxFileConfig::GetLocalFileName(const wxChar
*szFile
)
355 // On VMS I saw the problem that the home directory was appended
356 // twice for the configuration file. Does that also happen for
358 wxString str
= wxT( '.' );
360 wxString str
= GetLocalDir();
363 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
369 #if defined(__WINDOWS__) || defined(__DOS__)
370 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
375 str
<< wxT(" Preferences") ;
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 void wxFileConfig::Init()
388 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
393 // It's not an error if (one of the) file(s) doesn't exist.
395 // parse the global file
396 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) )
398 wxTextFile
fileGlobal(m_strGlobalFile
);
400 if ( fileGlobal
.Open(m_conv
/*ignored in ANSI build*/) )
402 Parse(fileGlobal
, FALSE
/* global */);
407 wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile
.c_str());
411 // parse the local file
412 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) )
414 wxTextFile
fileLocal(m_strLocalFile
);
415 if ( fileLocal
.Open(m_conv
/*ignored in ANSI build*/) )
417 Parse(fileLocal
, TRUE
/* local */);
422 wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile
.c_str() );
427 // constructor supports creation of wxFileConfig objects of any type
428 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
429 const wxString
& strLocal
, const wxString
& strGlobal
,
430 long style
, wxMBConv
& conv
)
431 : wxConfigBase(::GetAppName(appName
), vendorName
,
434 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
),
437 // Make up names for files if empty
438 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
439 m_strLocalFile
= GetLocalFileName(GetAppName());
441 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
442 m_strGlobalFile
= GetGlobalFileName(GetAppName());
444 // Check if styles are not supplied, but filenames are, in which case
445 // add the correct styles.
446 if ( !m_strLocalFile
.IsEmpty() )
447 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
449 if ( !m_strGlobalFile
.IsEmpty() )
450 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
452 // if the path is not absolute, prepend the standard directory to it
453 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
454 if ( !(style
& wxCONFIG_USE_RELATIVE_PATH
) )
456 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
458 wxString strLocal
= m_strLocalFile
;
459 m_strLocalFile
= GetLocalDir();
460 m_strLocalFile
<< strLocal
;
463 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
465 wxString strGlobal
= m_strGlobalFile
;
466 m_strGlobalFile
= GetGlobalDir();
467 m_strGlobalFile
<< strGlobal
;
478 wxFileConfig::wxFileConfig(wxInputStream
&inStream
, wxMBConv
& conv
)
481 // always local_file when this constructor is called (?)
482 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
485 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
490 // translate everything to the current (platform-dependent) line
491 // termination character
499 inStream
.Read(buf
, WXSIZEOF(buf
));
501 const wxStreamError err
= inStream
.GetLastError();
503 if ( err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
)
505 wxLogError(_("Error reading config options."));
509 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
511 while ( !inStream
.Eof() );
513 strTrans
= wxTextBuffer::Translate(strTmp
);
516 wxMemoryText memText
;
518 // Now we can add the text to the memory text. To do this we extract line
519 // by line from the translated string, until we've reached the end.
521 // VZ: all this is horribly inefficient, we should do the translation on
522 // the fly in one pass saving both memory and time (TODO)
524 const wxChar
*pEOL
= wxTextBuffer::GetEOL(wxTextBuffer::typeDefault
);
525 const size_t EOLLen
= wxStrlen(pEOL
);
527 int posLineStart
= strTrans
.Find(pEOL
);
528 while ( posLineStart
!= -1 )
530 wxString
line(strTrans
.Left(posLineStart
));
532 memText
.AddLine(line
);
534 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
536 posLineStart
= strTrans
.Find(pEOL
);
539 // also add whatever we have left in the translated string.
540 memText
.AddLine(strTrans
);
542 // Finally we can parse it all.
543 Parse(memText
, TRUE
/* local */);
548 #endif // wxUSE_STREAMS
550 void wxFileConfig::CleanUp()
554 wxFileConfigLineList
*pCur
= m_linesHead
;
555 while ( pCur
!= NULL
) {
556 wxFileConfigLineList
*pNext
= pCur
->Next();
562 wxFileConfig::~wxFileConfig()
569 // ----------------------------------------------------------------------------
570 // parse a config file
571 // ----------------------------------------------------------------------------
573 void wxFileConfig::Parse(wxTextBuffer
& buffer
, bool bLocal
)
575 const wxChar
*pStart
;
579 size_t nLineCount
= buffer
.GetLineCount();
581 for ( size_t n
= 0; n
< nLineCount
; n
++ )
585 // add the line to linked list
588 LineListAppend(strLine
);
590 // let the root group have it start line as well
593 m_pCurrentGroup
->SetLine(m_linesTail
);
598 // skip leading spaces
599 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
602 // skip blank/comment lines
603 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
606 if ( *pStart
== wxT('[') ) { // a new group
609 while ( *++pEnd
!= wxT(']') ) {
610 if ( *pEnd
== wxT('\\') ) {
611 // the next char is escaped, so skip it even if it is ']'
615 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
616 // we reached the end of line, break out of the loop
621 if ( *pEnd
!= wxT(']') ) {
622 wxLogError(_("file '%s': unexpected character %c at line %d."),
623 buffer
.GetName(), *pEnd
, n
+ 1);
624 continue; // skip this line
627 // group name here is always considered as abs path
630 strGroup
<< wxCONFIG_PATH_SEPARATOR
631 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
633 // will create it if doesn't yet exist
638 if ( m_pCurrentGroup
->Parent() )
639 m_pCurrentGroup
->Parent()->SetLastGroup(m_pCurrentGroup
);
640 m_pCurrentGroup
->SetLine(m_linesTail
);
643 // check that there is nothing except comments left on this line
645 while ( *++pEnd
!= wxT('\0') && bCont
) {
654 // ignore whitespace ('\n' impossible here)
658 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
659 buffer
.GetName(), n
+ 1, pEnd
);
665 const wxChar
*pEnd
= pStart
;
666 while ( *pEnd
&& *pEnd
!= wxT('=') && !wxIsspace(*pEnd
) ) {
667 if ( *pEnd
== wxT('\\') ) {
668 // next character may be space or not - still take it because it's
669 // quoted (unless there is nothing)
672 // the error message will be given below anyhow
680 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
)));
683 while ( wxIsspace(*pEnd
) )
686 if ( *pEnd
++ != wxT('=') ) {
687 wxLogError(_("file '%s', line %d: '=' expected."),
688 buffer
.GetName(), n
+ 1);
691 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
693 if ( pEntry
== NULL
) {
695 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
698 if ( bLocal
&& pEntry
->IsImmutable() ) {
699 // immutable keys can't be changed by user
700 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
701 buffer
.GetName(), n
+ 1, strKey
.c_str());
704 // the condition below catches the cases (a) and (b) but not (c):
705 // (a) global key found second time in global file
706 // (b) key found second (or more) time in local file
707 // (c) key from global file now found in local one
708 // which is exactly what we want.
709 else if ( !bLocal
|| pEntry
->IsLocal() ) {
710 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
711 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
717 pEntry
->SetLine(m_linesTail
);
720 while ( wxIsspace(*pEnd
) )
723 wxString value
= pEnd
;
724 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
) )
725 value
= FilterInValue(value
);
727 pEntry
->SetValue(value
, FALSE
);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 void wxFileConfig::SetRootPath()
740 m_pCurrentGroup
= m_pRootGroup
;
743 void wxFileConfig::SetPath(const wxString
& strPath
)
745 wxArrayString aParts
;
747 if ( strPath
.IsEmpty() ) {
752 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
754 wxSplitPath(aParts
, strPath
);
757 // relative path, combine with current one
758 wxString strFullPath
= m_strPath
;
759 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
760 wxSplitPath(aParts
, strFullPath
);
763 // change current group
765 m_pCurrentGroup
= m_pRootGroup
;
766 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
767 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
768 if ( pNextGroup
== NULL
)
769 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
770 m_pCurrentGroup
= pNextGroup
;
773 // recombine path parts in one variable
775 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
776 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
787 return GetNextGroup(str
, lIndex
);
790 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
792 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
793 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
800 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
803 return GetNextEntry(str
, lIndex
);
806 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
808 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
809 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
816 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
818 size_t n
= m_pCurrentGroup
->Entries().Count();
820 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
821 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
822 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
823 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
824 n
+= GetNumberOfEntries(TRUE
);
825 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
832 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
834 size_t n
= m_pCurrentGroup
->Groups().Count();
836 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
837 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
838 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
839 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
840 n
+= GetNumberOfGroups(TRUE
);
841 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
848 // ----------------------------------------------------------------------------
849 // tests for existence
850 // ----------------------------------------------------------------------------
852 bool wxFileConfig::HasGroup(const wxString
& strName
) const
854 wxConfigPathChanger
path(this, strName
);
856 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
857 return pGroup
!= NULL
;
860 bool wxFileConfig::HasEntry(const wxString
& strName
) const
862 wxConfigPathChanger
path(this, strName
);
864 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
865 return pEntry
!= NULL
;
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 bool wxFileConfig::DoReadString(const wxString
& key
, wxString
* pStr
) const
874 wxConfigPathChanger
path(this, key
);
876 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
877 if (pEntry
== NULL
) {
881 *pStr
= pEntry
->Value();
886 bool wxFileConfig::DoReadLong(const wxString
& key
, long *pl
) const
889 if ( !Read(key
, &str
) )
892 // extra spaces shouldn't prevent us from reading numeric values
895 return str
.ToLong(pl
);
898 bool wxFileConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
900 wxConfigPathChanger
path(this, key
);
901 wxString strName
= path
.Name();
903 wxLogTrace( _T("wxFileConfig"),
904 _T(" Writing String '%s' = '%s' to Group '%s'"),
909 if ( strName
.IsEmpty() )
911 // setting the value of a group is an error
913 wxASSERT_MSG( wxIsEmpty(szValue
), wxT("can't set value of a group!") );
915 // ... except if it's empty in which case it's a way to force it's creation
917 wxLogTrace( _T("wxFileConfig"),
918 _T(" Creating group %s"),
919 m_pCurrentGroup
->Name().c_str() );
921 m_pCurrentGroup
->SetDirty();
923 // this will add a line for this group if it didn't have it before
925 (void)m_pCurrentGroup
->GetGroupLine();
930 // check that the name is reasonable
932 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
)
934 wxLogError( _("Config entry name cannot start with '%c'."),
935 wxCONFIG_IMMUTABLE_PREFIX
);
939 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
943 wxLogTrace( _T("wxFileConfig"),
944 _T(" Adding Entry %s"),
946 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
949 wxLogTrace( _T("wxFileConfig"),
950 _T(" Setting value %s"),
952 pEntry
->SetValue(szValue
);
958 bool wxFileConfig::DoWriteLong(const wxString
& key
, long lValue
)
960 return Write(key
, wxString::Format(_T("%ld"), lValue
));
963 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
965 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() || !m_strLocalFile
)
969 // set the umask if needed
973 umaskOld
= umask((mode_t
)m_umask
);
977 wxTempFile
file(m_strLocalFile
);
979 if ( !file
.IsOpened() )
981 wxLogError(_("can't open user configuration file."));
985 // write all strings to file
986 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
988 wxString line
= p
->Text();
989 line
+= wxTextFile::GetEOL();
990 if ( !file
.Write(line
, m_conv
) )
992 wxLogError(_("can't write user configuration file."));
997 bool ret
= file
.Commit();
999 #if defined(__WXMAC__)
1004 wxMacFilename2FSSpec( m_strLocalFile
, &spec
) ;
1006 if ( FSpGetFInfo( &spec
, &finfo
) == noErr
)
1008 finfo
.fdType
= 'TEXT' ;
1009 finfo
.fdCreator
= 'ttxt' ;
1010 FSpSetFInfo( &spec
, &finfo
) ;
1016 // restore the old umask if we changed it
1017 if ( m_umask
!= -1 )
1019 (void)umask(umaskOld
);
1026 // ----------------------------------------------------------------------------
1027 // renaming groups/entries
1028 // ----------------------------------------------------------------------------
1030 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
1031 const wxString
& newName
)
1033 // check that the entry exists
1034 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
1038 // check that the new entry doesn't already exist
1039 if ( m_pCurrentGroup
->FindEntry(newName
) )
1042 // delete the old entry, create the new one
1043 wxString value
= oldEntry
->Value();
1044 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
1047 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
1048 newEntry
->SetValue(value
);
1053 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
1054 const wxString
& newName
)
1056 // check that the group exists
1057 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
1061 // check that the new group doesn't already exist
1062 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
1065 group
->Rename(newName
);
1070 // ----------------------------------------------------------------------------
1071 // delete groups/entries
1072 // ----------------------------------------------------------------------------
1074 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1076 wxConfigPathChanger
path(this, key
);
1078 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1081 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1082 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1083 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1084 SetPath(wxT("..")); // changes m_pCurrentGroup!
1085 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
1087 //else: never delete the root group
1093 bool wxFileConfig::DeleteGroup(const wxString
& key
)
1095 wxConfigPathChanger
path(this, key
);
1097 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
1100 bool wxFileConfig::DeleteAll()
1104 if ( wxFile::Exists(m_strLocalFile
) && wxRemove(m_strLocalFile
) == -1 )
1106 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile
.c_str());
1110 m_strLocalFile
= m_strGlobalFile
= wxT("");
1116 // ----------------------------------------------------------------------------
1117 // linked list functions
1118 // ----------------------------------------------------------------------------
1120 // append a new line to the end of the list
1122 wxFileConfigLineList
*wxFileConfig::LineListAppend(const wxString
& str
)
1124 wxLogTrace( _T("wxFileConfig"),
1125 _T(" ** Adding Line '%s'"),
1127 wxLogTrace( _T("wxFileConfig"),
1129 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1130 wxLogTrace( _T("wxFileConfig"),
1132 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1134 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1136 if ( m_linesTail
== NULL
)
1139 m_linesHead
= pLine
;
1144 m_linesTail
->SetNext(pLine
);
1145 pLine
->SetPrev(m_linesTail
);
1148 m_linesTail
= pLine
;
1150 wxLogTrace( _T("wxFileConfig"),
1152 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1153 wxLogTrace( _T("wxFileConfig"),
1155 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1160 // insert a new line after the given one or in the very beginning if !pLine
1161 wxFileConfigLineList
*wxFileConfig::LineListInsert(const wxString
& str
,
1162 wxFileConfigLineList
*pLine
)
1164 wxLogTrace( _T("wxFileConfig"),
1165 _T(" ** Inserting Line '%s' after '%s'"),
1167 ((pLine
) ? pLine
->Text().c_str() : wxEmptyString
) );
1168 wxLogTrace( _T("wxFileConfig"),
1170 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1171 wxLogTrace( _T("wxFileConfig"),
1173 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1175 if ( pLine
== m_linesTail
)
1176 return LineListAppend(str
);
1178 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1179 if ( pLine
== NULL
)
1181 // prepend to the list
1182 pNewLine
->SetNext(m_linesHead
);
1183 m_linesHead
->SetPrev(pNewLine
);
1184 m_linesHead
= pNewLine
;
1188 // insert before pLine
1189 wxFileConfigLineList
*pNext
= pLine
->Next();
1190 pNewLine
->SetNext(pNext
);
1191 pNewLine
->SetPrev(pLine
);
1192 pNext
->SetPrev(pNewLine
);
1193 pLine
->SetNext(pNewLine
);
1196 wxLogTrace( _T("wxFileConfig"),
1198 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1199 wxLogTrace( _T("wxFileConfig"),
1201 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1206 void wxFileConfig::LineListRemove(wxFileConfigLineList
*pLine
)
1208 wxLogTrace( _T("wxFileConfig"),
1209 _T(" ** Removing Line '%s'"),
1210 pLine
->Text().c_str() );
1211 wxLogTrace( _T("wxFileConfig"),
1213 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1214 wxLogTrace( _T("wxFileConfig"),
1216 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1218 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1219 *pNext
= pLine
->Next();
1223 if ( pPrev
== NULL
)
1224 m_linesHead
= pNext
;
1226 pPrev
->SetNext(pNext
);
1230 if ( pNext
== NULL
)
1231 m_linesTail
= pPrev
;
1233 pNext
->SetPrev(pPrev
);
1235 wxLogTrace( _T("wxFileConfig"),
1237 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1238 wxLogTrace( _T("wxFileConfig"),
1240 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1245 bool wxFileConfig::LineListIsEmpty()
1247 return m_linesHead
== NULL
;
1250 // ============================================================================
1251 // wxFileConfig::wxFileConfigGroup
1252 // ============================================================================
1254 // ----------------------------------------------------------------------------
1256 // ----------------------------------------------------------------------------
1259 wxFileConfigGroup::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1260 const wxString
& strName
,
1261 wxFileConfig
*pConfig
)
1262 : m_aEntries(CompareEntries
),
1263 m_aSubgroups(CompareGroups
),
1266 m_pConfig
= pConfig
;
1267 m_pParent
= pParent
;
1271 m_pLastEntry
= NULL
;
1272 m_pLastGroup
= NULL
;
1275 // dtor deletes all children
1276 wxFileConfigGroup::~wxFileConfigGroup()
1279 size_t n
, nCount
= m_aEntries
.Count();
1280 for ( n
= 0; n
< nCount
; n
++ )
1281 delete m_aEntries
[n
];
1284 nCount
= m_aSubgroups
.Count();
1285 for ( n
= 0; n
< nCount
; n
++ )
1286 delete m_aSubgroups
[n
];
1289 // ----------------------------------------------------------------------------
1291 // ----------------------------------------------------------------------------
1293 void wxFileConfigGroup::SetLine(wxFileConfigLineList
*pLine
)
1295 wxASSERT( m_pLine
== 0 ); // shouldn't be called twice
1300 This is a bit complicated, so let me explain it in details. All lines that
1301 were read from the local file (the only one we will ever modify) are stored
1302 in a (doubly) linked list. Our problem is to know at which position in this
1303 list should we insert the new entries/subgroups. To solve it we keep three
1304 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1306 m_pLine points to the line containing "[group_name]"
1307 m_pLastEntry points to the last entry of this group in the local file.
1308 m_pLastGroup subgroup
1310 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1311 from the local file, the corresponding variable is set. However, if the group
1312 was read from the global file and then modified or created by the application
1313 these variables are still NULL and we need to create the corresponding lines.
1314 See the following functions (and comments preceding them) for the details of
1317 Also, when our last entry/group are deleted we need to find the new last
1318 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1319 of lines until it either founds an entry/subgroup (and this is the new last
1320 element) or the m_pLine of the group, in which case there are no more entries
1321 (or subgroups) left and m_pLast<element> becomes NULL.
1323 NB: This last problem could be avoided for entries if we added new entries
1324 immediately after m_pLine, but in this case the entries would appear
1325 backwards in the config file (OTOH, it's not that important) and as we
1326 would still need to do it for the subgroups the code wouldn't have been
1327 significantly less complicated.
1330 // Return the line which contains "[our name]". If we're still not in the list,
1331 // add our line to it immediately after the last line of our parent group if we
1332 // have it or in the very beginning if we're the root group.
1333 wxFileConfigLineList
*wxFileConfigGroup::GetGroupLine()
1335 wxLogTrace( _T("wxFileConfig"),
1336 _T(" GetGroupLine() for Group '%s'"),
1341 wxLogTrace( _T("wxFileConfig"),
1342 _T(" Getting Line item pointer") );
1344 wxFileConfigGroup
*pParent
= Parent();
1346 // this group wasn't present in local config file, add it now
1349 wxLogTrace( _T("wxFileConfig"),
1350 _T(" checking parent '%s'"),
1351 pParent
->Name().c_str() );
1353 wxString strFullName
;
1355 // add 1 to the name because we don't want to start with '/'
1356 strFullName
<< wxT("[")
1357 << FilterOutEntryName(GetFullName().c_str() + 1)
1359 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1360 pParent
->GetLastGroupLine());
1361 pParent
->SetLastGroup(this); // we're surely after all the others
1363 //else: this is the root group and so we return NULL because we don't
1364 // have any group line
1370 // Return the last line belonging to the subgroups of this group (after which
1371 // we can add a new subgroup), if we don't have any subgroups or entries our
1372 // last line is the group line (m_pLine) itself.
1373 wxFileConfigLineList
*wxFileConfigGroup::GetLastGroupLine()
1375 // if we have any subgroups, our last line is the last line of the last
1379 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1381 wxASSERT_MSG( pLine
, _T("last group must have !NULL associated line") );
1386 // no subgroups, so the last line is the line of thelast entry (if any)
1387 return GetLastEntryLine();
1390 // return the last line belonging to the entries of this group (after which
1391 // we can add a new entry), if we don't have any entries we will add the new
1392 // one immediately after the group line itself.
1393 wxFileConfigLineList
*wxFileConfigGroup::GetLastEntryLine()
1395 wxLogTrace( _T("wxFileConfig"),
1396 _T(" GetLastEntryLine() for Group '%s'"),
1401 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1403 wxASSERT_MSG( pLine
, _T("last entry must have !NULL associated line") );
1408 // no entries: insert after the group header, if any
1409 return GetGroupLine();
1412 void wxFileConfigGroup::SetLastEntry(wxFileConfigEntry
*pEntry
)
1414 m_pLastEntry
= pEntry
;
1418 // the only situation in which a group without its own line can have
1419 // an entry is when the first entry is added to the initially empty
1420 // root pseudo-group
1421 wxASSERT_MSG( !m_pParent
, _T("unexpected for non root group") );
1423 // let the group know that it does have a line in the file now
1424 m_pLine
= pEntry
->GetLine();
1428 // ----------------------------------------------------------------------------
1430 // ----------------------------------------------------------------------------
1432 void wxFileConfigGroup::Rename(const wxString
& newName
)
1434 wxCHECK_RET( m_pParent
, _T("the root group can't be renamed") );
1436 m_strName
= newName
;
1438 // +1: no leading '/'
1439 wxString strFullName
;
1440 strFullName
<< wxT("[") << (GetFullName().c_str() + 1) << wxT("]");
1442 wxFileConfigLineList
*line
= GetGroupLine();
1443 wxCHECK_RET( line
, _T("a non root group must have a corresponding line!") );
1445 line
->SetText(strFullName
);
1450 wxString
wxFileConfigGroup::GetFullName() const
1453 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1458 // ----------------------------------------------------------------------------
1460 // ----------------------------------------------------------------------------
1462 // use binary search because the array is sorted
1464 wxFileConfigGroup::FindEntry(const wxChar
*szName
) const
1468 hi
= m_aEntries
.Count();
1470 wxFileConfigEntry
*pEntry
;
1474 pEntry
= m_aEntries
[i
];
1476 #if wxCONFIG_CASE_SENSITIVE
1477 res
= wxStrcmp(pEntry
->Name(), szName
);
1479 res
= wxStricmp(pEntry
->Name(), szName
);
1494 wxFileConfigGroup::FindSubgroup(const wxChar
*szName
) const
1498 hi
= m_aSubgroups
.Count();
1500 wxFileConfigGroup
*pGroup
;
1504 pGroup
= m_aSubgroups
[i
];
1506 #if wxCONFIG_CASE_SENSITIVE
1507 res
= wxStrcmp(pGroup
->Name(), szName
);
1509 res
= wxStricmp(pGroup
->Name(), szName
);
1523 // ----------------------------------------------------------------------------
1524 // create a new item
1525 // ----------------------------------------------------------------------------
1527 // create a new entry and add it to the current group
1528 wxFileConfigEntry
*wxFileConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1530 wxASSERT( FindEntry(strName
) == 0 );
1532 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1534 m_aEntries
.Add(pEntry
);
1538 // create a new group and add it to the current group
1539 wxFileConfigGroup
*wxFileConfigGroup::AddSubgroup(const wxString
& strName
)
1541 wxASSERT( FindSubgroup(strName
) == 0 );
1543 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1545 m_aSubgroups
.Add(pGroup
);
1549 // ----------------------------------------------------------------------------
1551 // ----------------------------------------------------------------------------
1554 The delete operations are _very_ slow if we delete the last item of this
1555 group (see comments before GetXXXLineXXX functions for more details),
1556 so it's much better to start with the first entry/group if we want to
1557 delete several of them.
1560 bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar
*szName
)
1562 wxFileConfigGroup
* const pGroup
= FindSubgroup(szName
);
1564 return pGroup
? DeleteSubgroup(pGroup
) : FALSE
;
1567 // Delete the subgroup and remove all references to it from
1568 // other data structures.
1569 bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1571 wxCHECK_MSG( pGroup
, FALSE
, _T("deleting non existing group?") );
1573 wxLogTrace( _T("wxFileConfig"),
1574 _T("Deleting group '%s' from '%s'"),
1575 pGroup
->Name().c_str(),
1578 wxLogTrace( _T("wxFileConfig"),
1579 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1580 ((m_pLine
) ? m_pLine
->Prev() : 0),
1582 ((m_pLine
) ? m_pLine
->Next() : 0) );
1583 wxLogTrace( _T("wxFileConfig"),
1585 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1587 // delete all entries
1588 size_t nCount
= pGroup
->m_aEntries
.Count();
1590 wxLogTrace(_T("wxFileConfig"),
1591 _T("Removing %lu Entries"),
1592 (unsigned long)nCount
);
1594 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ )
1596 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1600 wxLogTrace( _T("wxFileConfig"),
1602 pLine
->Text().c_str() );
1603 m_pConfig
->LineListRemove(pLine
);
1607 // and subgroups of this subgroup
1609 nCount
= pGroup
->m_aSubgroups
.Count();
1611 wxLogTrace( _T("wxFileConfig"),
1612 _T("Removing %lu SubGroups"),
1613 (unsigned long)nCount
);
1615 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ )
1617 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1620 // finally the group itself
1622 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1626 wxLogTrace( _T("wxFileConfig"),
1627 _T(" Removing line entry for Group '%s' : '%s'"),
1628 pGroup
->Name().c_str(),
1629 pLine
->Text().c_str() );
1630 wxLogTrace( _T("wxFileConfig"),
1631 _T(" Removing from Group '%s' : '%s'"),
1633 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1635 // notice that we may do this test inside the previous "if"
1636 // because the last entry's line is surely !NULL
1638 if ( pGroup
== m_pLastGroup
)
1640 wxLogTrace( _T("wxFileConfig"),
1641 _T(" ------- Removing last group -------") );
1643 // our last entry is being deleted, so find the last one which stays.
1644 // go back until we find a subgroup or reach the group's line, unless
1645 // we are the root group, which we'll notice shortly.
1647 wxFileConfigGroup
*pNewLast
= 0;
1648 size_t nSubgroups
= m_aSubgroups
.Count();
1649 wxFileConfigLineList
*pl
;
1651 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() )
1653 // is it our subgroup?
1655 for ( size_t n
= 0; (pNewLast
== 0) && (n
< nSubgroups
); n
++ )
1657 // do _not_ call GetGroupLine! we don't want to add it to the local
1658 // file if it's not already there
1660 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1661 pNewLast
= m_aSubgroups
[n
];
1664 if ( pNewLast
!= 0 ) // found?
1668 if ( pl
== m_pLine
|| m_pParent
== 0 )
1670 wxLogTrace( _T("wxFileConfig"),
1671 _T(" ------- No previous group found -------") );
1673 wxASSERT_MSG( !pNewLast
|| m_pLine
== 0,
1674 _T("how comes it has the same line as we?") );
1676 // we've reached the group line without finding any subgroups,
1677 // or realised we removed the last group from the root.
1683 wxLogTrace( _T("wxFileConfig"),
1684 _T(" ------- Last Group set to '%s' -------"),
1685 pNewLast
->Name().c_str() );
1687 m_pLastGroup
= pNewLast
;
1691 m_pConfig
->LineListRemove(pLine
);
1695 wxLogTrace( _T("wxFileConfig"),
1696 _T(" No line entry for Group '%s'?"),
1697 pGroup
->Name().c_str() );
1702 m_aSubgroups
.Remove(pGroup
);
1708 bool wxFileConfigGroup::DeleteEntry(const wxChar
*szName
)
1710 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1711 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1713 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1714 if ( pLine
!= NULL
) {
1715 // notice that we may do this test inside the previous "if" because the
1716 // last entry's line is surely !NULL
1717 if ( pEntry
== m_pLastEntry
) {
1718 // our last entry is being deleted - find the last one which stays
1719 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1721 // go back until we find another entry or reach the group's line
1722 wxFileConfigEntry
*pNewLast
= NULL
;
1723 size_t n
, nEntries
= m_aEntries
.Count();
1724 wxFileConfigLineList
*pl
;
1725 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1726 // is it our subgroup?
1727 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1728 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1729 pNewLast
= m_aEntries
[n
];
1732 if ( pNewLast
!= NULL
) // found?
1736 if ( pl
== m_pLine
) {
1737 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1739 // we've reached the group line without finding any subgroups
1740 m_pLastEntry
= NULL
;
1743 m_pLastEntry
= pNewLast
;
1746 m_pConfig
->LineListRemove(pLine
);
1749 // we must be written back for the changes to be saved
1752 m_aEntries
.Remove(pEntry
);
1758 // ----------------------------------------------------------------------------
1760 // ----------------------------------------------------------------------------
1761 void wxFileConfigGroup::SetDirty()
1764 if ( Parent() != NULL
) // propagate upwards
1765 Parent()->SetDirty();
1768 // ============================================================================
1769 // wxFileConfig::wxFileConfigEntry
1770 // ============================================================================
1772 // ----------------------------------------------------------------------------
1774 // ----------------------------------------------------------------------------
1775 wxFileConfigEntry::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1776 const wxString
& strName
,
1778 : m_strName(strName
)
1780 wxASSERT( !strName
.IsEmpty() );
1782 m_pParent
= pParent
;
1787 m_bHasValue
= FALSE
;
1789 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1791 m_strName
.erase(0, 1); // remove first character
1794 // ----------------------------------------------------------------------------
1796 // ----------------------------------------------------------------------------
1798 void wxFileConfigEntry::SetLine(wxFileConfigLineList
*pLine
)
1800 if ( m_pLine
!= NULL
) {
1801 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1802 Name().c_str(), m_pParent
->GetFullName().c_str());
1806 Group()->SetLastEntry(this);
1809 // second parameter is FALSE if we read the value from file and prevents the
1810 // entry from being marked as 'dirty'
1811 void wxFileConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1813 if ( bUser
&& IsImmutable() )
1815 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1820 // do nothing if it's the same value: but don't test for it
1821 // if m_bHasValue hadn't been set yet or we'd never write
1822 // empty values to the file
1824 if ( m_bHasValue
&& strValue
== m_strValue
)
1828 m_strValue
= strValue
;
1832 wxString strValFiltered
;
1834 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
)
1836 strValFiltered
= strValue
;
1839 strValFiltered
= FilterOutValue(strValue
);
1843 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strValFiltered
;
1847 // entry was read from the local config file, just modify the line
1848 m_pLine
->SetText(strLine
);
1850 else // this entry didn't exist in the local file
1852 // add a new line to the file
1853 wxFileConfigLineList
*line
= Group()->GetLastEntryLine();
1854 m_pLine
= Group()->Config()->LineListInsert(strLine
, line
);
1856 Group()->SetLastEntry(this);
1863 void wxFileConfigEntry::SetDirty()
1866 Group()->SetDirty();
1869 // ============================================================================
1871 // ============================================================================
1873 // ----------------------------------------------------------------------------
1874 // compare functions for array sorting
1875 // ----------------------------------------------------------------------------
1877 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1879 #if wxCONFIG_CASE_SENSITIVE
1880 return wxStrcmp(p1
->Name(), p2
->Name());
1882 return wxStricmp(p1
->Name(), p2
->Name());
1886 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1888 #if wxCONFIG_CASE_SENSITIVE
1889 return wxStrcmp(p1
->Name(), p2
->Name());
1891 return wxStricmp(p1
->Name(), p2
->Name());
1895 // ----------------------------------------------------------------------------
1897 // ----------------------------------------------------------------------------
1899 // undo FilterOutValue
1900 static wxString
FilterInValue(const wxString
& str
)
1903 strResult
.Alloc(str
.Len());
1905 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1907 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1908 if ( str
[n
] == wxT('\\') ) {
1909 switch ( str
[++n
] ) {
1911 strResult
+= wxT('\n');
1915 strResult
+= wxT('\r');
1919 strResult
+= wxT('\t');
1923 strResult
+= wxT('\\');
1927 strResult
+= wxT('"');
1932 if ( str
[n
] != wxT('"') || !bQuoted
)
1933 strResult
+= str
[n
];
1934 else if ( n
!= str
.Len() - 1 ) {
1935 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1938 //else: it's the last quote of a quoted string, ok
1945 // quote the string before writing it to file
1946 static wxString
FilterOutValue(const wxString
& str
)
1952 strResult
.Alloc(str
.Len());
1954 // quoting is necessary to preserve spaces in the beginning of the string
1955 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1958 strResult
+= wxT('"');
1961 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1984 //else: fall through
1987 strResult
+= str
[n
];
1988 continue; // nothing special to do
1991 // we get here only for special characters
1992 strResult
<< wxT('\\') << c
;
1996 strResult
+= wxT('"');
2001 // undo FilterOutEntryName
2002 static wxString
FilterInEntryName(const wxString
& str
)
2005 strResult
.Alloc(str
.Len());
2007 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
2008 if ( *pc
== wxT('\\') )
2017 // sanitize entry or group name: insert '\\' before any special characters
2018 static wxString
FilterOutEntryName(const wxString
& str
)
2021 strResult
.Alloc(str
.Len());
2023 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
2026 // we explicitly allow some of "safe" chars and 8bit ASCII characters
2027 // which will probably never have special meaning
2028 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
2029 // should *not* be quoted
2030 if ( !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) && ((c
& 0x80) == 0) )
2031 strResult
+= wxT('\\');
2039 // we can't put ?: in the ctor initializer list because it confuses some
2040 // broken compilers (Borland C++)
2041 static wxString
GetAppName(const wxString
& appName
)
2043 if ( !appName
&& wxTheApp
)
2044 return wxTheApp
->GetAppName();
2049 #endif // wxUSE_CONFIG