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
497 while ( !inStream
.Read(buf
, WXSIZEOF(buf
)).Eof() )
498 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
500 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
502 strTrans
= wxTextBuffer::Translate(strTmp
);
505 wxMemoryText memText
;
507 // Now we can add the text to the memory text. To do this we extract line
508 // by line from the translated string, until we've reached the end.
510 // VZ: all this is horribly inefficient, we should do the translation on
511 // the fly in one pass saving both memory and time (TODO)
513 const wxChar
*pEOL
= wxTextBuffer::GetEOL(wxTextBuffer::typeDefault
);
514 const size_t EOLLen
= wxStrlen(pEOL
);
516 int posLineStart
= strTrans
.Find(pEOL
);
517 while ( posLineStart
!= -1 )
519 wxString
line(strTrans
.Left(posLineStart
));
521 memText
.AddLine(line
);
523 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
525 posLineStart
= strTrans
.Find(pEOL
);
528 // also add whatever we have left in the translated string.
529 memText
.AddLine(strTrans
);
531 // Finally we can parse it all.
532 Parse(memText
, TRUE
/* local */);
537 #endif // wxUSE_STREAMS
539 void wxFileConfig::CleanUp()
543 wxFileConfigLineList
*pCur
= m_linesHead
;
544 while ( pCur
!= NULL
) {
545 wxFileConfigLineList
*pNext
= pCur
->Next();
551 wxFileConfig::~wxFileConfig()
558 // ----------------------------------------------------------------------------
559 // parse a config file
560 // ----------------------------------------------------------------------------
562 void wxFileConfig::Parse(wxTextBuffer
& buffer
, bool bLocal
)
564 const wxChar
*pStart
;
568 size_t nLineCount
= buffer
.GetLineCount();
570 for ( size_t n
= 0; n
< nLineCount
; n
++ )
574 // add the line to linked list
577 LineListAppend(strLine
);
579 // let the root group have it start line as well
582 m_pCurrentGroup
->SetLine(m_linesTail
);
587 // skip leading spaces
588 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
591 // skip blank/comment lines
592 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
595 if ( *pStart
== wxT('[') ) { // a new group
598 while ( *++pEnd
!= wxT(']') ) {
599 if ( *pEnd
== wxT('\\') ) {
600 // the next char is escaped, so skip it even if it is ']'
604 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
605 // we reached the end of line, break out of the loop
610 if ( *pEnd
!= wxT(']') ) {
611 wxLogError(_("file '%s': unexpected character %c at line %d."),
612 buffer
.GetName(), *pEnd
, n
+ 1);
613 continue; // skip this line
616 // group name here is always considered as abs path
619 strGroup
<< wxCONFIG_PATH_SEPARATOR
620 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
622 // will create it if doesn't yet exist
627 if ( m_pCurrentGroup
->Parent() )
628 m_pCurrentGroup
->Parent()->SetLastGroup(m_pCurrentGroup
);
629 m_pCurrentGroup
->SetLine(m_linesTail
);
632 // check that there is nothing except comments left on this line
634 while ( *++pEnd
!= wxT('\0') && bCont
) {
643 // ignore whitespace ('\n' impossible here)
647 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
648 buffer
.GetName(), n
+ 1, pEnd
);
654 const wxChar
*pEnd
= pStart
;
655 while ( *pEnd
&& *pEnd
!= wxT('=') && !wxIsspace(*pEnd
) ) {
656 if ( *pEnd
== wxT('\\') ) {
657 // next character may be space or not - still take it because it's
658 // quoted (unless there is nothing)
661 // the error message will be given below anyhow
669 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
)));
672 while ( wxIsspace(*pEnd
) )
675 if ( *pEnd
++ != wxT('=') ) {
676 wxLogError(_("file '%s', line %d: '=' expected."),
677 buffer
.GetName(), n
+ 1);
680 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
682 if ( pEntry
== NULL
) {
684 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
687 if ( bLocal
&& pEntry
->IsImmutable() ) {
688 // immutable keys can't be changed by user
689 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
690 buffer
.GetName(), n
+ 1, strKey
.c_str());
693 // the condition below catches the cases (a) and (b) but not (c):
694 // (a) global key found second time in global file
695 // (b) key found second (or more) time in local file
696 // (c) key from global file now found in local one
697 // which is exactly what we want.
698 else if ( !bLocal
|| pEntry
->IsLocal() ) {
699 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
700 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
706 pEntry
->SetLine(m_linesTail
);
709 while ( wxIsspace(*pEnd
) )
712 wxString value
= pEnd
;
713 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
) )
714 value
= FilterInValue(value
);
716 pEntry
->SetValue(value
, FALSE
);
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
726 void wxFileConfig::SetRootPath()
729 m_pCurrentGroup
= m_pRootGroup
;
732 void wxFileConfig::SetPath(const wxString
& strPath
)
734 wxArrayString aParts
;
736 if ( strPath
.IsEmpty() ) {
741 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
743 wxSplitPath(aParts
, strPath
);
746 // relative path, combine with current one
747 wxString strFullPath
= m_strPath
;
748 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
749 wxSplitPath(aParts
, strFullPath
);
752 // change current group
754 m_pCurrentGroup
= m_pRootGroup
;
755 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
756 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
757 if ( pNextGroup
== NULL
)
758 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
759 m_pCurrentGroup
= pNextGroup
;
762 // recombine path parts in one variable
764 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
765 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
769 // ----------------------------------------------------------------------------
771 // ----------------------------------------------------------------------------
773 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
776 return GetNextGroup(str
, lIndex
);
779 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
781 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
782 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
789 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
792 return GetNextEntry(str
, lIndex
);
795 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
797 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
798 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
805 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
807 size_t n
= m_pCurrentGroup
->Entries().Count();
809 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
810 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
811 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
812 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
813 n
+= GetNumberOfEntries(TRUE
);
814 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
821 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
823 size_t n
= m_pCurrentGroup
->Groups().Count();
825 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
826 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
827 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
828 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
829 n
+= GetNumberOfGroups(TRUE
);
830 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
837 // ----------------------------------------------------------------------------
838 // tests for existence
839 // ----------------------------------------------------------------------------
841 bool wxFileConfig::HasGroup(const wxString
& strName
) const
843 wxConfigPathChanger
path(this, strName
);
845 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
846 return pGroup
!= NULL
;
849 bool wxFileConfig::HasEntry(const wxString
& strName
) const
851 wxConfigPathChanger
path(this, strName
);
853 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
854 return pEntry
!= NULL
;
857 // ----------------------------------------------------------------------------
859 // ----------------------------------------------------------------------------
861 bool wxFileConfig::DoReadString(const wxString
& key
, wxString
* pStr
) const
863 wxConfigPathChanger
path(this, key
);
865 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
866 if (pEntry
== NULL
) {
870 *pStr
= pEntry
->Value();
875 bool wxFileConfig::DoReadLong(const wxString
& key
, long *pl
) const
878 if ( !Read(key
, &str
) )
881 // extra spaces shouldn't prevent us from reading numeric values
884 return str
.ToLong(pl
);
887 bool wxFileConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
889 wxConfigPathChanger
path(this, key
);
890 wxString strName
= path
.Name();
892 wxLogTrace( _T("wxFileConfig"),
893 _T(" Writing String '%s' = '%s' to Group '%s'"),
898 if ( strName
.IsEmpty() )
900 // setting the value of a group is an error
902 wxASSERT_MSG( wxIsEmpty(szValue
), wxT("can't set value of a group!") );
904 // ... except if it's empty in which case it's a way to force it's creation
906 wxLogTrace( _T("wxFileConfig"),
907 _T(" Creating group %s"),
908 m_pCurrentGroup
->Name().c_str() );
910 m_pCurrentGroup
->SetDirty();
912 // this will add a line for this group if it didn't have it before
914 (void)m_pCurrentGroup
->GetGroupLine();
919 // check that the name is reasonable
921 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
)
923 wxLogError( _("Config entry name cannot start with '%c'."),
924 wxCONFIG_IMMUTABLE_PREFIX
);
928 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
932 wxLogTrace( _T("wxFileConfig"),
933 _T(" Adding Entry %s"),
935 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
938 wxLogTrace( _T("wxFileConfig"),
939 _T(" Setting value %s"),
941 pEntry
->SetValue(szValue
);
947 bool wxFileConfig::DoWriteLong(const wxString
& key
, long lValue
)
949 return Write(key
, wxString::Format(_T("%ld"), lValue
));
952 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
954 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() || !m_strLocalFile
)
958 // set the umask if needed
962 umaskOld
= umask((mode_t
)m_umask
);
966 wxTempFile
file(m_strLocalFile
);
968 if ( !file
.IsOpened() )
970 wxLogError(_("can't open user configuration file."));
974 // write all strings to file
975 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
977 wxString line
= p
->Text();
978 line
+= wxTextFile::GetEOL();
979 if ( !file
.Write(line
, m_conv
) )
981 wxLogError(_("can't write user configuration file."));
986 bool ret
= file
.Commit();
988 #if defined(__WXMAC__)
993 wxMacFilename2FSSpec( m_strLocalFile
, &spec
) ;
995 if ( FSpGetFInfo( &spec
, &finfo
) == noErr
)
997 finfo
.fdType
= 'TEXT' ;
998 finfo
.fdCreator
= 'ttxt' ;
999 FSpSetFInfo( &spec
, &finfo
) ;
1005 // restore the old umask if we changed it
1006 if ( m_umask
!= -1 )
1008 (void)umask(umaskOld
);
1015 // ----------------------------------------------------------------------------
1016 // renaming groups/entries
1017 // ----------------------------------------------------------------------------
1019 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
1020 const wxString
& newName
)
1022 // check that the entry exists
1023 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
1027 // check that the new entry doesn't already exist
1028 if ( m_pCurrentGroup
->FindEntry(newName
) )
1031 // delete the old entry, create the new one
1032 wxString value
= oldEntry
->Value();
1033 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
1036 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
1037 newEntry
->SetValue(value
);
1042 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
1043 const wxString
& newName
)
1045 // check that the group exists
1046 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
1050 // check that the new group doesn't already exist
1051 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
1054 group
->Rename(newName
);
1059 // ----------------------------------------------------------------------------
1060 // delete groups/entries
1061 // ----------------------------------------------------------------------------
1063 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1065 wxConfigPathChanger
path(this, key
);
1067 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1070 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1071 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1072 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1073 SetPath(wxT("..")); // changes m_pCurrentGroup!
1074 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
1076 //else: never delete the root group
1082 bool wxFileConfig::DeleteGroup(const wxString
& key
)
1084 wxConfigPathChanger
path(this, key
);
1086 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
1089 bool wxFileConfig::DeleteAll()
1093 if ( wxRemove(m_strLocalFile
) == -1 )
1094 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile
.c_str());
1096 m_strLocalFile
= m_strGlobalFile
= wxT("");
1102 // ----------------------------------------------------------------------------
1103 // linked list functions
1104 // ----------------------------------------------------------------------------
1106 // append a new line to the end of the list
1108 wxFileConfigLineList
*wxFileConfig::LineListAppend(const wxString
& str
)
1110 wxLogTrace( _T("wxFileConfig"),
1111 _T(" ** Adding Line '%s'"),
1113 wxLogTrace( _T("wxFileConfig"),
1115 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1116 wxLogTrace( _T("wxFileConfig"),
1118 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1120 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1122 if ( m_linesTail
== NULL
)
1125 m_linesHead
= pLine
;
1130 m_linesTail
->SetNext(pLine
);
1131 pLine
->SetPrev(m_linesTail
);
1134 m_linesTail
= pLine
;
1136 wxLogTrace( _T("wxFileConfig"),
1138 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1139 wxLogTrace( _T("wxFileConfig"),
1141 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1146 // insert a new line after the given one or in the very beginning if !pLine
1147 wxFileConfigLineList
*wxFileConfig::LineListInsert(const wxString
& str
,
1148 wxFileConfigLineList
*pLine
)
1150 wxLogTrace( _T("wxFileConfig"),
1151 _T(" ** Inserting Line '%s' after '%s'"),
1153 ((pLine
) ? pLine
->Text().c_str() : wxEmptyString
) );
1154 wxLogTrace( _T("wxFileConfig"),
1156 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1157 wxLogTrace( _T("wxFileConfig"),
1159 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1161 if ( pLine
== m_linesTail
)
1162 return LineListAppend(str
);
1164 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1165 if ( pLine
== NULL
)
1167 // prepend to the list
1168 pNewLine
->SetNext(m_linesHead
);
1169 m_linesHead
->SetPrev(pNewLine
);
1170 m_linesHead
= pNewLine
;
1174 // insert before pLine
1175 wxFileConfigLineList
*pNext
= pLine
->Next();
1176 pNewLine
->SetNext(pNext
);
1177 pNewLine
->SetPrev(pLine
);
1178 pNext
->SetPrev(pNewLine
);
1179 pLine
->SetNext(pNewLine
);
1182 wxLogTrace( _T("wxFileConfig"),
1184 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1185 wxLogTrace( _T("wxFileConfig"),
1187 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1192 void wxFileConfig::LineListRemove(wxFileConfigLineList
*pLine
)
1194 wxLogTrace( _T("wxFileConfig"),
1195 _T(" ** Removing Line '%s'"),
1196 pLine
->Text().c_str() );
1197 wxLogTrace( _T("wxFileConfig"),
1199 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1200 wxLogTrace( _T("wxFileConfig"),
1202 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1204 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1205 *pNext
= pLine
->Next();
1209 if ( pPrev
== NULL
)
1210 m_linesHead
= pNext
;
1212 pPrev
->SetNext(pNext
);
1216 if ( pNext
== NULL
)
1217 m_linesTail
= pPrev
;
1219 pNext
->SetPrev(pPrev
);
1221 wxLogTrace( _T("wxFileConfig"),
1223 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1224 wxLogTrace( _T("wxFileConfig"),
1226 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1231 bool wxFileConfig::LineListIsEmpty()
1233 return m_linesHead
== NULL
;
1236 // ============================================================================
1237 // wxFileConfig::wxFileConfigGroup
1238 // ============================================================================
1240 // ----------------------------------------------------------------------------
1242 // ----------------------------------------------------------------------------
1245 wxFileConfigGroup::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1246 const wxString
& strName
,
1247 wxFileConfig
*pConfig
)
1248 : m_aEntries(CompareEntries
),
1249 m_aSubgroups(CompareGroups
),
1252 m_pConfig
= pConfig
;
1253 m_pParent
= pParent
;
1257 m_pLastEntry
= NULL
;
1258 m_pLastGroup
= NULL
;
1261 // dtor deletes all children
1262 wxFileConfigGroup::~wxFileConfigGroup()
1265 size_t n
, nCount
= m_aEntries
.Count();
1266 for ( n
= 0; n
< nCount
; n
++ )
1267 delete m_aEntries
[n
];
1270 nCount
= m_aSubgroups
.Count();
1271 for ( n
= 0; n
< nCount
; n
++ )
1272 delete m_aSubgroups
[n
];
1275 // ----------------------------------------------------------------------------
1277 // ----------------------------------------------------------------------------
1279 void wxFileConfigGroup::SetLine(wxFileConfigLineList
*pLine
)
1281 wxASSERT( m_pLine
== 0 ); // shouldn't be called twice
1286 This is a bit complicated, so let me explain it in details. All lines that
1287 were read from the local file (the only one we will ever modify) are stored
1288 in a (doubly) linked list. Our problem is to know at which position in this
1289 list should we insert the new entries/subgroups. To solve it we keep three
1290 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1292 m_pLine points to the line containing "[group_name]"
1293 m_pLastEntry points to the last entry of this group in the local file.
1294 m_pLastGroup subgroup
1296 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1297 from the local file, the corresponding variable is set. However, if the group
1298 was read from the global file and then modified or created by the application
1299 these variables are still NULL and we need to create the corresponding lines.
1300 See the following functions (and comments preceding them) for the details of
1303 Also, when our last entry/group are deleted we need to find the new last
1304 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1305 of lines until it either founds an entry/subgroup (and this is the new last
1306 element) or the m_pLine of the group, in which case there are no more entries
1307 (or subgroups) left and m_pLast<element> becomes NULL.
1309 NB: This last problem could be avoided for entries if we added new entries
1310 immediately after m_pLine, but in this case the entries would appear
1311 backwards in the config file (OTOH, it's not that important) and as we
1312 would still need to do it for the subgroups the code wouldn't have been
1313 significantly less complicated.
1316 // Return the line which contains "[our name]". If we're still not in the list,
1317 // add our line to it immediately after the last line of our parent group if we
1318 // have it or in the very beginning if we're the root group.
1319 wxFileConfigLineList
*wxFileConfigGroup::GetGroupLine()
1321 wxLogTrace( _T("wxFileConfig"),
1322 _T(" GetGroupLine() for Group '%s'"),
1327 wxLogTrace( _T("wxFileConfig"),
1328 _T(" Getting Line item pointer") );
1330 wxFileConfigGroup
*pParent
= Parent();
1332 // this group wasn't present in local config file, add it now
1335 wxLogTrace( _T("wxFileConfig"),
1336 _T(" checking parent '%s'"),
1337 pParent
->Name().c_str() );
1339 wxString strFullName
;
1341 // add 1 to the name because we don't want to start with '/'
1342 strFullName
<< wxT("[")
1343 << FilterOutEntryName(GetFullName().c_str() + 1)
1345 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1346 pParent
->GetLastGroupLine());
1347 pParent
->SetLastGroup(this); // we're surely after all the others
1349 //else: this is the root group and so we return NULL because we don't
1350 // have any group line
1356 // Return the last line belonging to the subgroups of this group (after which
1357 // we can add a new subgroup), if we don't have any subgroups or entries our
1358 // last line is the group line (m_pLine) itself.
1359 wxFileConfigLineList
*wxFileConfigGroup::GetLastGroupLine()
1361 // if we have any subgroups, our last line is the last line of the last
1365 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1367 wxASSERT_MSG( pLine
, _T("last group must have !NULL associated line") );
1372 // no subgroups, so the last line is the line of thelast entry (if any)
1373 return GetLastEntryLine();
1376 // return the last line belonging to the entries of this group (after which
1377 // we can add a new entry), if we don't have any entries we will add the new
1378 // one immediately after the group line itself.
1379 wxFileConfigLineList
*wxFileConfigGroup::GetLastEntryLine()
1381 wxLogTrace( _T("wxFileConfig"),
1382 _T(" GetLastEntryLine() for Group '%s'"),
1387 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1389 wxASSERT_MSG( pLine
, _T("last entry must have !NULL associated line") );
1394 // no entries: insert after the group header, if any
1395 return GetGroupLine();
1398 void wxFileConfigGroup::SetLastEntry(wxFileConfigEntry
*pEntry
)
1400 m_pLastEntry
= pEntry
;
1404 // the only situation in which a group without its own line can have
1405 // an entry is when the first entry is added to the initially empty
1406 // root pseudo-group
1407 wxASSERT_MSG( !m_pParent
, _T("unexpected for non root group") );
1409 // let the group know that it does have a line in the file now
1410 m_pLine
= pEntry
->GetLine();
1414 // ----------------------------------------------------------------------------
1416 // ----------------------------------------------------------------------------
1418 void wxFileConfigGroup::Rename(const wxString
& newName
)
1420 wxCHECK_RET( m_pParent
, _T("the root group can't be renamed") );
1422 m_strName
= newName
;
1424 // +1: no leading '/'
1425 wxString strFullName
;
1426 strFullName
<< wxT("[") << (GetFullName().c_str() + 1) << wxT("]");
1428 wxFileConfigLineList
*line
= GetGroupLine();
1429 wxCHECK_RET( line
, _T("a non root group must have a corresponding line!") );
1431 line
->SetText(strFullName
);
1436 wxString
wxFileConfigGroup::GetFullName() const
1439 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1444 // ----------------------------------------------------------------------------
1446 // ----------------------------------------------------------------------------
1448 // use binary search because the array is sorted
1450 wxFileConfigGroup::FindEntry(const wxChar
*szName
) const
1454 hi
= m_aEntries
.Count();
1456 wxFileConfigEntry
*pEntry
;
1460 pEntry
= m_aEntries
[i
];
1462 #if wxCONFIG_CASE_SENSITIVE
1463 res
= wxStrcmp(pEntry
->Name(), szName
);
1465 res
= wxStricmp(pEntry
->Name(), szName
);
1480 wxFileConfigGroup::FindSubgroup(const wxChar
*szName
) const
1484 hi
= m_aSubgroups
.Count();
1486 wxFileConfigGroup
*pGroup
;
1490 pGroup
= m_aSubgroups
[i
];
1492 #if wxCONFIG_CASE_SENSITIVE
1493 res
= wxStrcmp(pGroup
->Name(), szName
);
1495 res
= wxStricmp(pGroup
->Name(), szName
);
1509 // ----------------------------------------------------------------------------
1510 // create a new item
1511 // ----------------------------------------------------------------------------
1513 // create a new entry and add it to the current group
1514 wxFileConfigEntry
*wxFileConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1516 wxASSERT( FindEntry(strName
) == 0 );
1518 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1520 m_aEntries
.Add(pEntry
);
1524 // create a new group and add it to the current group
1525 wxFileConfigGroup
*wxFileConfigGroup::AddSubgroup(const wxString
& strName
)
1527 wxASSERT( FindSubgroup(strName
) == 0 );
1529 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1531 m_aSubgroups
.Add(pGroup
);
1535 // ----------------------------------------------------------------------------
1537 // ----------------------------------------------------------------------------
1540 The delete operations are _very_ slow if we delete the last item of this
1541 group (see comments before GetXXXLineXXX functions for more details),
1542 so it's much better to start with the first entry/group if we want to
1543 delete several of them.
1546 bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar
*szName
)
1548 wxFileConfigGroup
* const pGroup
= FindSubgroup(szName
);
1550 return pGroup
? DeleteSubgroup(pGroup
) : FALSE
;
1553 // Delete the subgroup and remove all references to it from
1554 // other data structures.
1555 bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1557 wxCHECK_MSG( pGroup
, FALSE
, _T("deleting non existing group?") );
1559 wxLogTrace( _T("wxFileConfig"),
1560 _T("Deleting group '%s' from '%s'"),
1561 pGroup
->Name().c_str(),
1564 wxLogTrace( _T("wxFileConfig"),
1565 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1566 ((m_pLine
) ? m_pLine
->Prev() : 0),
1568 ((m_pLine
) ? m_pLine
->Next() : 0) );
1569 wxLogTrace( _T("wxFileConfig"),
1571 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1573 // delete all entries
1574 size_t nCount
= pGroup
->m_aEntries
.Count();
1576 wxLogTrace(_T("wxFileConfig"),
1577 _T("Removing %lu Entries"),
1578 (unsigned long)nCount
);
1580 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ )
1582 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1586 wxLogTrace( _T("wxFileConfig"),
1588 pLine
->Text().c_str() );
1589 m_pConfig
->LineListRemove(pLine
);
1593 // and subgroups of this subgroup
1595 nCount
= pGroup
->m_aSubgroups
.Count();
1597 wxLogTrace( _T("wxFileConfig"),
1598 _T("Removing %lu SubGroups"),
1599 (unsigned long)nCount
);
1601 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ )
1603 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1606 // finally the group itself
1608 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1612 wxLogTrace( _T("wxFileConfig"),
1613 _T(" Removing line entry for Group '%s' : '%s'"),
1614 pGroup
->Name().c_str(),
1615 pLine
->Text().c_str() );
1616 wxLogTrace( _T("wxFileConfig"),
1617 _T(" Removing from Group '%s' : '%s'"),
1619 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1621 // notice that we may do this test inside the previous "if"
1622 // because the last entry's line is surely !NULL
1624 if ( pGroup
== m_pLastGroup
)
1626 wxLogTrace( _T("wxFileConfig"),
1627 _T(" ------- Removing last group -------") );
1629 // our last entry is being deleted, so find the last one which stays.
1630 // go back until we find a subgroup or reach the group's line, unless
1631 // we are the root group, which we'll notice shortly.
1633 wxFileConfigGroup
*pNewLast
= 0;
1634 size_t nSubgroups
= m_aSubgroups
.Count();
1635 wxFileConfigLineList
*pl
;
1637 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() )
1639 // is it our subgroup?
1641 for ( size_t n
= 0; (pNewLast
== 0) && (n
< nSubgroups
); n
++ )
1643 // do _not_ call GetGroupLine! we don't want to add it to the local
1644 // file if it's not already there
1646 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1647 pNewLast
= m_aSubgroups
[n
];
1650 if ( pNewLast
!= 0 ) // found?
1654 if ( pl
== m_pLine
|| m_pParent
== 0 )
1656 wxLogTrace( _T("wxFileConfig"),
1657 _T(" ------- No previous group found -------") );
1659 wxASSERT_MSG( !pNewLast
|| m_pLine
== 0,
1660 _T("how comes it has the same line as we?") );
1662 // we've reached the group line without finding any subgroups,
1663 // or realised we removed the last group from the root.
1669 wxLogTrace( _T("wxFileConfig"),
1670 _T(" ------- Last Group set to '%s' -------"),
1671 pNewLast
->Name().c_str() );
1673 m_pLastGroup
= pNewLast
;
1677 m_pConfig
->LineListRemove(pLine
);
1681 wxLogTrace( _T("wxFileConfig"),
1682 _T(" No line entry for Group '%s'?"),
1683 pGroup
->Name().c_str() );
1688 m_aSubgroups
.Remove(pGroup
);
1694 bool wxFileConfigGroup::DeleteEntry(const wxChar
*szName
)
1696 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1697 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1699 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1700 if ( pLine
!= NULL
) {
1701 // notice that we may do this test inside the previous "if" because the
1702 // last entry's line is surely !NULL
1703 if ( pEntry
== m_pLastEntry
) {
1704 // our last entry is being deleted - find the last one which stays
1705 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1707 // go back until we find another entry or reach the group's line
1708 wxFileConfigEntry
*pNewLast
= NULL
;
1709 size_t n
, nEntries
= m_aEntries
.Count();
1710 wxFileConfigLineList
*pl
;
1711 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1712 // is it our subgroup?
1713 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1714 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1715 pNewLast
= m_aEntries
[n
];
1718 if ( pNewLast
!= NULL
) // found?
1722 if ( pl
== m_pLine
) {
1723 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1725 // we've reached the group line without finding any subgroups
1726 m_pLastEntry
= NULL
;
1729 m_pLastEntry
= pNewLast
;
1732 m_pConfig
->LineListRemove(pLine
);
1735 // we must be written back for the changes to be saved
1738 m_aEntries
.Remove(pEntry
);
1744 // ----------------------------------------------------------------------------
1746 // ----------------------------------------------------------------------------
1747 void wxFileConfigGroup::SetDirty()
1750 if ( Parent() != NULL
) // propagate upwards
1751 Parent()->SetDirty();
1754 // ============================================================================
1755 // wxFileConfig::wxFileConfigEntry
1756 // ============================================================================
1758 // ----------------------------------------------------------------------------
1760 // ----------------------------------------------------------------------------
1761 wxFileConfigEntry::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1762 const wxString
& strName
,
1764 : m_strName(strName
)
1766 wxASSERT( !strName
.IsEmpty() );
1768 m_pParent
= pParent
;
1773 m_bHasValue
= FALSE
;
1775 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1777 m_strName
.erase(0, 1); // remove first character
1780 // ----------------------------------------------------------------------------
1782 // ----------------------------------------------------------------------------
1784 void wxFileConfigEntry::SetLine(wxFileConfigLineList
*pLine
)
1786 if ( m_pLine
!= NULL
) {
1787 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1788 Name().c_str(), m_pParent
->GetFullName().c_str());
1792 Group()->SetLastEntry(this);
1795 // second parameter is FALSE if we read the value from file and prevents the
1796 // entry from being marked as 'dirty'
1797 void wxFileConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1799 if ( bUser
&& IsImmutable() )
1801 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1806 // do nothing if it's the same value: but don't test for it
1807 // if m_bHasValue hadn't been set yet or we'd never write
1808 // empty values to the file
1810 if ( m_bHasValue
&& strValue
== m_strValue
)
1814 m_strValue
= strValue
;
1818 wxString strValFiltered
;
1820 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
)
1822 strValFiltered
= strValue
;
1825 strValFiltered
= FilterOutValue(strValue
);
1829 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strValFiltered
;
1833 // entry was read from the local config file, just modify the line
1834 m_pLine
->SetText(strLine
);
1836 else // this entry didn't exist in the local file
1838 // add a new line to the file
1839 wxFileConfigLineList
*line
= Group()->GetLastEntryLine();
1840 m_pLine
= Group()->Config()->LineListInsert(strLine
, line
);
1842 Group()->SetLastEntry(this);
1849 void wxFileConfigEntry::SetDirty()
1852 Group()->SetDirty();
1855 // ============================================================================
1857 // ============================================================================
1859 // ----------------------------------------------------------------------------
1860 // compare functions for array sorting
1861 // ----------------------------------------------------------------------------
1863 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1865 #if wxCONFIG_CASE_SENSITIVE
1866 return wxStrcmp(p1
->Name(), p2
->Name());
1868 return wxStricmp(p1
->Name(), p2
->Name());
1872 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1874 #if wxCONFIG_CASE_SENSITIVE
1875 return wxStrcmp(p1
->Name(), p2
->Name());
1877 return wxStricmp(p1
->Name(), p2
->Name());
1881 // ----------------------------------------------------------------------------
1883 // ----------------------------------------------------------------------------
1885 // undo FilterOutValue
1886 static wxString
FilterInValue(const wxString
& str
)
1889 strResult
.Alloc(str
.Len());
1891 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1893 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1894 if ( str
[n
] == wxT('\\') ) {
1895 switch ( str
[++n
] ) {
1897 strResult
+= wxT('\n');
1901 strResult
+= wxT('\r');
1905 strResult
+= wxT('\t');
1909 strResult
+= wxT('\\');
1913 strResult
+= wxT('"');
1918 if ( str
[n
] != wxT('"') || !bQuoted
)
1919 strResult
+= str
[n
];
1920 else if ( n
!= str
.Len() - 1 ) {
1921 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1924 //else: it's the last quote of a quoted string, ok
1931 // quote the string before writing it to file
1932 static wxString
FilterOutValue(const wxString
& str
)
1938 strResult
.Alloc(str
.Len());
1940 // quoting is necessary to preserve spaces in the beginning of the string
1941 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1944 strResult
+= wxT('"');
1947 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1970 //else: fall through
1973 strResult
+= str
[n
];
1974 continue; // nothing special to do
1977 // we get here only for special characters
1978 strResult
<< wxT('\\') << c
;
1982 strResult
+= wxT('"');
1987 // undo FilterOutEntryName
1988 static wxString
FilterInEntryName(const wxString
& str
)
1991 strResult
.Alloc(str
.Len());
1993 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
1994 if ( *pc
== wxT('\\') )
2003 // sanitize entry or group name: insert '\\' before any special characters
2004 static wxString
FilterOutEntryName(const wxString
& str
)
2007 strResult
.Alloc(str
.Len());
2009 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
2012 // we explicitly allow some of "safe" chars and 8bit ASCII characters
2013 // which will probably never have special meaning
2014 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
2015 // should *not* be quoted
2016 if ( !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) && ((c
& 0x80) == 0) )
2017 strResult
+= wxT('\\');
2025 // we can't put ?: in the ctor initializer list because it confuses some
2026 // broken compilers (Borland C++)
2027 static wxString
GetAppName(const wxString
& appName
)
2029 if ( !appName
&& wxTheApp
)
2030 return wxTheApp
->GetAppName();
2035 #endif // wxUSE_CONFIG