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"
42 #include "wx/filefn.h"
45 #include "wx/stream.h"
46 #endif // wxUSE_STREAMS
48 #include "wx/utils.h" // for wxGetHomeDir
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
54 #if defined(__WXMSW__)
55 #include "wx/msw/private.h"
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
68 #define CONST_CAST ((wxFileConfig *)this)->
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
79 // global functions declarations
80 // ----------------------------------------------------------------------------
82 // compare functions for sorting the arrays
83 static int LINKAGEMODE
CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
);
84 static int LINKAGEMODE
CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
);
87 static wxString
FilterInValue(const wxString
& str
);
88 static wxString
FilterOutValue(const wxString
& str
);
90 static wxString
FilterInEntryName(const wxString
& str
);
91 static wxString
FilterOutEntryName(const wxString
& str
);
93 // get the name to use in wxFileConfig ctor
94 static wxString
GetAppName(const wxString
& appname
);
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // "template" array types
102 // ----------------------------------------------------------------------------
104 #ifdef WXMAKINGDLL_BASE
105 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
,
107 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
,
110 WX_DEFINE_SORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
);
111 WX_DEFINE_SORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
);
114 // ----------------------------------------------------------------------------
115 // wxFileConfigLineList
116 // ----------------------------------------------------------------------------
118 // we store all lines of the local config file as a linked list in memory
119 class wxFileConfigLineList
122 void SetNext(wxFileConfigLineList
*pNext
) { m_pNext
= pNext
; }
123 void SetPrev(wxFileConfigLineList
*pPrev
) { m_pPrev
= pPrev
; }
126 wxFileConfigLineList(const wxString
& str
,
127 wxFileConfigLineList
*pNext
= NULL
) : m_strLine(str
)
128 { SetNext(pNext
); SetPrev(NULL
); }
130 // next/prev nodes in the linked list
131 wxFileConfigLineList
*Next() const { return m_pNext
; }
132 wxFileConfigLineList
*Prev() const { return m_pPrev
; }
134 // get/change lines text
135 void SetText(const wxString
& str
) { m_strLine
= str
; }
136 const wxString
& Text() const { return m_strLine
; }
139 wxString m_strLine
; // line contents
140 wxFileConfigLineList
*m_pNext
, // next node
141 *m_pPrev
; // previous one
143 DECLARE_NO_COPY_CLASS(wxFileConfigLineList
)
146 // ----------------------------------------------------------------------------
147 // wxFileConfigEntry: a name/value pair
148 // ----------------------------------------------------------------------------
150 class wxFileConfigEntry
153 wxFileConfigGroup
*m_pParent
; // group that contains us
155 wxString m_strName
, // entry name
157 bool m_bImmutable
:1, // can be overriden locally?
158 m_bHasValue
:1; // set after first call to SetValue()
160 int m_nLine
; // used if m_pLine == NULL only
162 // pointer to our line in the linked list or NULL if it was found in global
163 // file (which we don't modify)
164 wxFileConfigLineList
*m_pLine
;
167 wxFileConfigEntry(wxFileConfigGroup
*pParent
,
168 const wxString
& strName
, int nLine
);
171 const wxString
& Name() const { return m_strName
; }
172 const wxString
& Value() const { return m_strValue
; }
173 wxFileConfigGroup
*Group() const { return m_pParent
; }
174 bool IsImmutable() const { return m_bImmutable
; }
175 bool IsLocal() const { return m_pLine
!= 0; }
176 int Line() const { return m_nLine
; }
177 wxFileConfigLineList
*
178 GetLine() const { return m_pLine
; }
180 // modify entry attributes
181 void SetValue(const wxString
& strValue
, bool bUser
= true);
182 void SetLine(wxFileConfigLineList
*pLine
);
184 DECLARE_NO_COPY_CLASS(wxFileConfigEntry
)
187 // ----------------------------------------------------------------------------
188 // wxFileConfigGroup: container of entries and other groups
189 // ----------------------------------------------------------------------------
191 class wxFileConfigGroup
194 wxFileConfig
*m_pConfig
; // config object we belong to
195 wxFileConfigGroup
*m_pParent
; // parent group (NULL for root group)
196 ArrayEntries m_aEntries
; // entries in this group
197 ArrayGroups m_aSubgroups
; // subgroups
198 wxString m_strName
; // group's name
199 wxFileConfigLineList
*m_pLine
; // pointer to our line in the linked list
200 wxFileConfigEntry
*m_pLastEntry
; // last entry/subgroup of this group in the
201 wxFileConfigGroup
*m_pLastGroup
; // local file (we insert new ones after it)
203 // DeleteSubgroupByName helper
204 bool DeleteSubgroup(wxFileConfigGroup
*pGroup
);
208 wxFileConfigGroup(wxFileConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
210 // dtor deletes all entries and subgroups also
211 ~wxFileConfigGroup();
214 const wxString
& Name() const { return m_strName
; }
215 wxFileConfigGroup
*Parent() const { return m_pParent
; }
216 wxFileConfig
*Config() const { return m_pConfig
; }
218 const ArrayEntries
& Entries() const { return m_aEntries
; }
219 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
220 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
222 // find entry/subgroup (NULL if not found)
223 wxFileConfigGroup
*FindSubgroup(const wxChar
*szName
) const;
224 wxFileConfigEntry
*FindEntry (const wxChar
*szName
) const;
226 // delete entry/subgroup, return false if doesn't exist
227 bool DeleteSubgroupByName(const wxChar
*szName
);
228 bool DeleteEntry(const wxChar
*szName
);
230 // create new entry/subgroup returning pointer to newly created element
231 wxFileConfigGroup
*AddSubgroup(const wxString
& strName
);
232 wxFileConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= wxNOT_FOUND
);
234 void SetLine(wxFileConfigLineList
*pLine
);
236 // rename: no checks are done to ensure that the name is unique!
237 void Rename(const wxString
& newName
);
240 wxString
GetFullName() const;
242 // get the last line belonging to an entry/subgroup of this group
243 wxFileConfigLineList
*GetGroupLine(); // line which contains [group]
244 wxFileConfigLineList
*GetLastEntryLine(); // after which our subgroups start
245 wxFileConfigLineList
*GetLastGroupLine(); // after which the next group starts
247 // called by entries/subgroups when they're created/deleted
248 void SetLastEntry(wxFileConfigEntry
*pEntry
);
249 void SetLastGroup(wxFileConfigGroup
*pGroup
)
250 { m_pLastGroup
= pGroup
; }
252 DECLARE_NO_COPY_CLASS(wxFileConfigGroup
)
255 // ============================================================================
257 // ============================================================================
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
262 wxString wxFileConfig
::GetGlobalDir()
266 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
267 strDir
= wxT("sys$manager:");
268 #elif defined(__WXMAC__)
269 strDir
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
270 #elif defined( __UNIX__ )
271 strDir
= wxT("/etc/");
272 #elif defined(__WXPM__)
273 ULONG aulSysInfo
[QSV_MAX
] = {0};
277 rc
= DosQuerySysInfo( 1L, QSV_MAX
, (PVOID
)aulSysInfo
, sizeof(ULONG
)*QSV_MAX
);
280 drive
= aulSysInfo
[QSV_BOOT_DRIVE
- 1];
281 strDir
.Printf(wxT("%c:\\OS2\\"), 'A'+drive
-1);
283 #elif defined(__WXSTUBS__)
284 wxASSERT_MSG( false, wxT("TODO") ) ;
285 #elif defined(__DOS__)
286 // There's no such thing as global cfg dir in MS-DOS, let's return
287 // current directory (FIXME_MGL?)
290 wxChar szWinDir
[MAX_PATH
];
291 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
295 #endif // Unix/Windows
300 wxString wxFileConfig
::GetLocalDir()
304 #if defined(__WXMAC__) || defined(__DOS__)
305 // no local dir concept on Mac OS 9 or MS-DOS
306 return GetGlobalDir() ;
308 wxGetHomeDir(&strDir
);
312 if (strDir
.Last() != wxT(']'))
314 if (strDir
.Last() != wxT('/')) strDir
<< wxT('/');
316 if (strDir
.Last() != wxT('\\')) strDir
<< wxT('\\');
323 wxString wxFileConfig
::GetGlobalFileName(const wxChar
*szFile
)
325 wxString str
= GetGlobalDir();
328 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
329 #if defined( __WXMAC__ )
330 str
<< wxT(" Preferences") ;
331 #elif defined( __UNIX__ )
340 wxString wxFileConfig
::GetLocalFileName(const wxChar
*szFile
)
343 // On VMS I saw the problem that the home directory was appended
344 // twice for the configuration file. Does that also happen for
346 wxString str
= wxT( '.' );
348 wxString str
= GetLocalDir();
351 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
357 #if defined(__WINDOWS__) || defined(__DOS__)
358 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
363 str
<< wxT(" Preferences") ;
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 void wxFileConfig
::Init()
376 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
381 // It's not an error if (one of the) file(s) doesn't exist.
383 // parse the global file
384 if ( !m_strGlobalFile
.IsEmpty() && wxFile
::Exists(m_strGlobalFile
) )
386 wxTextFile
fileGlobal(m_strGlobalFile
);
388 if ( fileGlobal
.Open(m_conv
/*ignored in ANSI build*/) )
390 Parse(fileGlobal
, false /* global */);
395 wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile
.c_str());
399 // parse the local file
400 if ( !m_strLocalFile
.IsEmpty() && wxFile
::Exists(m_strLocalFile
) )
402 wxTextFile
fileLocal(m_strLocalFile
);
403 if ( fileLocal
.Open(m_conv
/*ignored in ANSI build*/) )
405 Parse(fileLocal
, true /* local */);
410 wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile
.c_str() );
417 // constructor supports creation of wxFileConfig objects of any type
418 wxFileConfig
::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
419 const wxString
& strLocal
, const wxString
& strGlobal
,
420 long style
, wxMBConv
& conv
)
421 : wxConfigBase(::GetAppName(appName
), vendorName
,
424 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
),
427 // Make up names for files if empty
428 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
429 m_strLocalFile
= GetLocalFileName(GetAppName());
431 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
432 m_strGlobalFile
= GetGlobalFileName(GetAppName());
434 // Check if styles are not supplied, but filenames are, in which case
435 // add the correct styles.
436 if ( !m_strLocalFile
.IsEmpty() )
437 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
439 if ( !m_strGlobalFile
.IsEmpty() )
440 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
442 // if the path is not absolute, prepend the standard directory to it
443 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
444 if ( !(style
& wxCONFIG_USE_RELATIVE_PATH
) )
446 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
448 wxString strLocal
= m_strLocalFile
;
449 m_strLocalFile
= GetLocalDir();
450 m_strLocalFile
<< strLocal
;
453 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
455 wxString strGlobal
= m_strGlobalFile
;
456 m_strGlobalFile
= GetGlobalDir();
457 m_strGlobalFile
<< strGlobal
;
468 wxFileConfig
::wxFileConfig(wxInputStream
&inStream
, wxMBConv
& conv
)
471 // always local_file when this constructor is called (?)
472 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
475 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
480 // translate everything to the current (platform-dependent) line
481 // termination character
489 inStream
.Read(buf
, WXSIZEOF(buf
));
491 const wxStreamError err
= inStream
.GetLastError();
493 if ( err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
)
495 wxLogError(_("Error reading config options."));
499 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
501 while ( !inStream
.Eof() );
503 strTrans
= wxTextBuffer
::Translate(strTmp
);
506 wxMemoryText memText
;
508 // Now we can add the text to the memory text. To do this we extract line
509 // by line from the translated string, until we've reached the end.
511 // VZ: all this is horribly inefficient, we should do the translation on
512 // the fly in one pass saving both memory and time (TODO)
514 const wxChar
*pEOL
= wxTextBuffer
::GetEOL(wxTextBuffer
::typeDefault
);
515 const size_t EOLLen
= wxStrlen(pEOL
);
517 int posLineStart
= strTrans
.Find(pEOL
);
518 while ( posLineStart
!= -1 )
520 wxString
line(strTrans
.Left(posLineStart
));
522 memText
.AddLine(line
);
524 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
526 posLineStart
= strTrans
.Find(pEOL
);
529 // also add whatever we have left in the translated string.
530 memText
.AddLine(strTrans
);
532 // Finally we can parse it all.
533 Parse(memText
, true /* local */);
538 #endif // wxUSE_STREAMS
540 void wxFileConfig
::CleanUp()
544 wxFileConfigLineList
*pCur
= m_linesHead
;
545 while ( pCur
!= NULL
) {
546 wxFileConfigLineList
*pNext
= pCur
->Next();
552 wxFileConfig
::~wxFileConfig()
559 // ----------------------------------------------------------------------------
560 // parse a config file
561 // ----------------------------------------------------------------------------
563 void wxFileConfig
::Parse(wxTextBuffer
& buffer
, bool bLocal
)
565 const wxChar
*pStart
;
569 size_t nLineCount
= buffer
.GetLineCount();
571 for ( size_t n
= 0; n
< nLineCount
; n
++ )
575 // add the line to linked list
578 LineListAppend(strLine
);
580 // let the root group have it start line as well
583 m_pCurrentGroup
->SetLine(m_linesTail
);
588 // skip leading spaces
589 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
592 // skip blank/comment lines
593 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
596 if ( *pStart
== wxT('[') ) { // a new group
599 while ( *++pEnd
!= wxT(']') ) {
600 if ( *pEnd
== wxT('\\') ) {
601 // the next char is escaped, so skip it even if it is ']'
605 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
606 // we reached the end of line, break out of the loop
611 if ( *pEnd
!= wxT(']') ) {
612 wxLogError(_("file '%s': unexpected character %c at line %d."),
613 buffer
.GetName(), *pEnd
, n
+ 1);
614 continue; // skip this line
617 // group name here is always considered as abs path
620 strGroup
<< wxCONFIG_PATH_SEPARATOR
621 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
623 // will create it if doesn't yet exist
628 if ( m_pCurrentGroup
->Parent() )
629 m_pCurrentGroup
->Parent()->SetLastGroup(m_pCurrentGroup
);
630 m_pCurrentGroup
->SetLine(m_linesTail
);
633 // check that there is nothing except comments left on this line
635 while ( *++pEnd
!= wxT('\0') && bCont
) {
644 // ignore whitespace ('\n' impossible here)
648 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
649 buffer
.GetName(), n
+ 1, pEnd
);
655 const wxChar
*pEnd
= pStart
;
656 while ( *pEnd
&& *pEnd
!= wxT('=') /* && !wxIsspace(*pEnd)*/ ) {
657 if ( *pEnd
== wxT('\\') ) {
658 // next character may be space or not - still take it because it's
659 // quoted (unless there is nothing)
662 // the error message will be given below anyhow
670 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
).Trim()));
673 while ( wxIsspace(*pEnd
) )
676 if ( *pEnd
++ != wxT('=') ) {
677 wxLogError(_("file '%s', line %d: '=' expected."),
678 buffer
.GetName(), n
+ 1);
681 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
683 if ( pEntry
== NULL
) {
685 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
688 if ( bLocal
&& pEntry
->IsImmutable() ) {
689 // immutable keys can't be changed by user
690 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
691 buffer
.GetName(), n
+ 1, strKey
.c_str());
694 // the condition below catches the cases (a) and (b) but not (c):
695 // (a) global key found second time in global file
696 // (b) key found second (or more) time in local file
697 // (c) key from global file now found in local one
698 // which is exactly what we want.
699 else if ( !bLocal
|| pEntry
->IsLocal() ) {
700 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
701 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
707 pEntry
->SetLine(m_linesTail
);
710 while ( wxIsspace(*pEnd
) )
713 wxString value
= pEnd
;
714 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
) )
715 value
= FilterInValue(value
);
717 pEntry
->SetValue(value
, false);
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
727 void wxFileConfig
::SetRootPath()
730 m_pCurrentGroup
= m_pRootGroup
;
733 void wxFileConfig
::SetPath(const wxString
& strPath
)
735 wxArrayString aParts
;
737 if ( strPath
.IsEmpty() ) {
742 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
744 wxSplitPath(aParts
, strPath
);
747 // relative path, combine with current one
748 wxString strFullPath
= m_strPath
;
749 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
750 wxSplitPath(aParts
, strFullPath
);
753 // change current group
755 m_pCurrentGroup
= m_pRootGroup
;
756 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
757 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
758 if ( pNextGroup
== NULL
)
759 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
760 m_pCurrentGroup
= pNextGroup
;
763 // recombine path parts in one variable
765 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
766 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
774 bool wxFileConfig
::GetFirstGroup(wxString
& str
, long& lIndex
) const
777 return GetNextGroup(str
, lIndex
);
780 bool wxFileConfig
::GetNextGroup (wxString
& str
, long& lIndex
) const
782 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
783 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
790 bool wxFileConfig
::GetFirstEntry(wxString
& str
, long& lIndex
) const
793 return GetNextEntry(str
, lIndex
);
796 bool wxFileConfig
::GetNextEntry (wxString
& str
, long& lIndex
) const
798 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
799 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
806 size_t wxFileConfig
::GetNumberOfEntries(bool bRecursive
) const
808 size_t n
= m_pCurrentGroup
->Entries().Count();
810 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
811 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
812 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
813 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
814 n
+= GetNumberOfEntries(true);
815 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
822 size_t wxFileConfig
::GetNumberOfGroups(bool bRecursive
) const
824 size_t n
= m_pCurrentGroup
->Groups().Count();
826 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
827 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
828 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
829 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
830 n
+= GetNumberOfGroups(true);
831 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
838 // ----------------------------------------------------------------------------
839 // tests for existence
840 // ----------------------------------------------------------------------------
842 bool wxFileConfig
::HasGroup(const wxString
& strName
) const
844 wxConfigPathChanger
path(this, strName
);
846 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
847 return pGroup
!= NULL
;
850 bool wxFileConfig
::HasEntry(const wxString
& strName
) const
852 wxConfigPathChanger
path(this, strName
);
854 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
855 return pEntry
!= NULL
;
858 // ----------------------------------------------------------------------------
860 // ----------------------------------------------------------------------------
862 bool wxFileConfig
::DoReadString(const wxString
& key
, wxString
* pStr
) const
864 wxConfigPathChanger
path(this, key
);
866 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
867 if (pEntry
== NULL
) {
871 *pStr
= pEntry
->Value();
876 bool wxFileConfig
::DoReadLong(const wxString
& key
, long *pl
) const
879 if ( !Read(key
, &str
) )
882 // extra spaces shouldn't prevent us from reading numeric values
885 return str
.ToLong(pl
);
888 bool wxFileConfig
::DoWriteString(const wxString
& key
, const wxString
& szValue
)
890 wxConfigPathChanger
path(this, key
);
891 wxString strName
= path
.Name();
893 wxLogTrace( _T("wxFileConfig"),
894 _T(" Writing String '%s' = '%s' to Group '%s'"),
899 if ( strName
.IsEmpty() )
901 // setting the value of a group is an error
903 wxASSERT_MSG( wxIsEmpty(szValue
), wxT("can't set value of a group!") );
905 // ... except if it's empty in which case it's a way to force it's creation
907 wxLogTrace( _T("wxFileConfig"),
908 _T(" Creating group %s"),
909 m_pCurrentGroup
->Name().c_str() );
913 // this will add a line for this group if it didn't have it before
915 (void)m_pCurrentGroup
->GetGroupLine();
919 // writing an entry check that the name is reasonable
920 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
)
922 wxLogError( _("Config entry name cannot start with '%c'."),
923 wxCONFIG_IMMUTABLE_PREFIX
);
927 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
931 wxLogTrace( _T("wxFileConfig"),
932 _T(" Adding Entry %s"),
934 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
937 wxLogTrace( _T("wxFileConfig"),
938 _T(" Setting value %s"),
940 pEntry
->SetValue(szValue
);
948 bool wxFileConfig
::DoWriteLong(const wxString
& key
, long lValue
)
950 return Write(key
, wxString
::Format(_T("%ld"), lValue
));
953 bool wxFileConfig
::Flush(bool /* bCurrentOnly */)
955 if ( !IsDirty() || !m_strLocalFile
)
958 // set the umask if needed
959 wxCHANGE_UMASK(m_umask
);
961 wxTempFile
file(m_strLocalFile
);
963 if ( !file
.IsOpened() )
965 wxLogError(_("can't open user configuration file."));
969 // write all strings to file
970 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
972 wxString line
= p
->Text();
973 line
+= wxTextFile
::GetEOL();
974 if ( !file
.Write(line
, m_conv
) )
976 wxLogError(_("can't write user configuration file."));
981 if ( !file
.Commit() )
983 wxLogError(_("Failed to update user configuration file."));
990 #if defined(__WXMAC__)
991 wxFileName(m_strLocalFile
).MacSetTypeAndCreator('TEXT', 'ttxt');
997 // ----------------------------------------------------------------------------
998 // renaming groups/entries
999 // ----------------------------------------------------------------------------
1001 bool wxFileConfig
::RenameEntry(const wxString
& oldName
,
1002 const wxString
& newName
)
1004 // check that the entry exists
1005 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
1009 // check that the new entry doesn't already exist
1010 if ( m_pCurrentGroup
->FindEntry(newName
) )
1013 // delete the old entry, create the new one
1014 wxString value
= oldEntry
->Value();
1015 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
1020 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
1021 newEntry
->SetValue(value
);
1026 bool wxFileConfig
::RenameGroup(const wxString
& oldName
,
1027 const wxString
& newName
)
1029 // check that the group exists
1030 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
1034 // check that the new group doesn't already exist
1035 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
1038 group
->Rename(newName
);
1045 // ----------------------------------------------------------------------------
1046 // delete groups/entries
1047 // ----------------------------------------------------------------------------
1049 bool wxFileConfig
::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1051 wxConfigPathChanger
path(this, key
);
1053 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1056 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1057 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1058 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1059 SetPath(wxT("..")); // changes m_pCurrentGroup!
1060 if ( m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name()) )
1063 //else: never delete the root group
1069 bool wxFileConfig
::DeleteGroup(const wxString
& key
)
1071 wxConfigPathChanger
path(this, key
);
1073 if ( !m_pCurrentGroup
->DeleteSubgroupByName(path
.Name()) )
1081 bool wxFileConfig
::DeleteAll()
1085 if ( wxFile
::Exists(m_strLocalFile
) && wxRemove(m_strLocalFile
) == -1 )
1087 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile
.c_str());
1091 m_strLocalFile
= m_strGlobalFile
= wxT("");
1097 // ----------------------------------------------------------------------------
1098 // linked list functions
1099 // ----------------------------------------------------------------------------
1101 // append a new line to the end of the list
1103 wxFileConfigLineList
*wxFileConfig
::LineListAppend(const wxString
& str
)
1105 wxLogTrace( _T("wxFileConfig"),
1106 _T(" ** Adding Line '%s'"),
1108 wxLogTrace( _T("wxFileConfig"),
1110 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1111 wxLogTrace( _T("wxFileConfig"),
1113 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1115 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1117 if ( m_linesTail
== NULL
)
1120 m_linesHead
= pLine
;
1125 m_linesTail
->SetNext(pLine
);
1126 pLine
->SetPrev(m_linesTail
);
1129 m_linesTail
= pLine
;
1131 wxLogTrace( _T("wxFileConfig"),
1133 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1134 wxLogTrace( _T("wxFileConfig"),
1136 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1141 // insert a new line after the given one or in the very beginning if !pLine
1142 wxFileConfigLineList
*wxFileConfig
::LineListInsert(const wxString
& str
,
1143 wxFileConfigLineList
*pLine
)
1145 wxLogTrace( _T("wxFileConfig"),
1146 _T(" ** Inserting Line '%s' after '%s'"),
1148 ((pLine
) ? pLine
->Text().c_str() : wxEmptyString
) );
1149 wxLogTrace( _T("wxFileConfig"),
1151 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1152 wxLogTrace( _T("wxFileConfig"),
1154 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1156 if ( pLine
== m_linesTail
)
1157 return LineListAppend(str
);
1159 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1160 if ( pLine
== NULL
)
1162 // prepend to the list
1163 pNewLine
->SetNext(m_linesHead
);
1164 m_linesHead
->SetPrev(pNewLine
);
1165 m_linesHead
= pNewLine
;
1169 // insert before pLine
1170 wxFileConfigLineList
*pNext
= pLine
->Next();
1171 pNewLine
->SetNext(pNext
);
1172 pNewLine
->SetPrev(pLine
);
1173 pNext
->SetPrev(pNewLine
);
1174 pLine
->SetNext(pNewLine
);
1177 wxLogTrace( _T("wxFileConfig"),
1179 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1180 wxLogTrace( _T("wxFileConfig"),
1182 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1187 void wxFileConfig
::LineListRemove(wxFileConfigLineList
*pLine
)
1189 wxLogTrace( _T("wxFileConfig"),
1190 _T(" ** Removing Line '%s'"),
1191 pLine
->Text().c_str() );
1192 wxLogTrace( _T("wxFileConfig"),
1194 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1195 wxLogTrace( _T("wxFileConfig"),
1197 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1199 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1200 *pNext
= pLine
->Next();
1204 if ( pPrev
== NULL
)
1205 m_linesHead
= pNext
;
1207 pPrev
->SetNext(pNext
);
1211 if ( pNext
== NULL
)
1212 m_linesTail
= pPrev
;
1214 pNext
->SetPrev(pPrev
);
1216 wxLogTrace( _T("wxFileConfig"),
1218 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1219 wxLogTrace( _T("wxFileConfig"),
1221 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1226 bool wxFileConfig
::LineListIsEmpty()
1228 return m_linesHead
== NULL
;
1231 // ============================================================================
1232 // wxFileConfig::wxFileConfigGroup
1233 // ============================================================================
1235 // ----------------------------------------------------------------------------
1237 // ----------------------------------------------------------------------------
1240 wxFileConfigGroup
::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1241 const wxString
& strName
,
1242 wxFileConfig
*pConfig
)
1243 : m_aEntries(CompareEntries
),
1244 m_aSubgroups(CompareGroups
),
1247 m_pConfig
= pConfig
;
1248 m_pParent
= pParent
;
1251 m_pLastEntry
= NULL
;
1252 m_pLastGroup
= NULL
;
1255 // dtor deletes all children
1256 wxFileConfigGroup
::~wxFileConfigGroup()
1259 size_t n
, nCount
= m_aEntries
.Count();
1260 for ( n
= 0; n
< nCount
; n
++ )
1261 delete m_aEntries
[n
];
1264 nCount
= m_aSubgroups
.Count();
1265 for ( n
= 0; n
< nCount
; n
++ )
1266 delete m_aSubgroups
[n
];
1269 // ----------------------------------------------------------------------------
1271 // ----------------------------------------------------------------------------
1273 void wxFileConfigGroup
::SetLine(wxFileConfigLineList
*pLine
)
1275 wxASSERT( m_pLine
== 0 ); // shouldn't be called twice
1280 This is a bit complicated, so let me explain it in details. All lines that
1281 were read from the local file (the only one we will ever modify) are stored
1282 in a (doubly) linked list. Our problem is to know at which position in this
1283 list should we insert the new entries/subgroups. To solve it we keep three
1284 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1286 m_pLine points to the line containing "[group_name]"
1287 m_pLastEntry points to the last entry of this group in the local file.
1288 m_pLastGroup subgroup
1290 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1291 from the local file, the corresponding variable is set. However, if the group
1292 was read from the global file and then modified or created by the application
1293 these variables are still NULL and we need to create the corresponding lines.
1294 See the following functions (and comments preceding them) for the details of
1297 Also, when our last entry/group are deleted we need to find the new last
1298 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1299 of lines until it either founds an entry/subgroup (and this is the new last
1300 element) or the m_pLine of the group, in which case there are no more entries
1301 (or subgroups) left and m_pLast<element> becomes NULL.
1303 NB: This last problem could be avoided for entries if we added new entries
1304 immediately after m_pLine, but in this case the entries would appear
1305 backwards in the config file (OTOH, it's not that important) and as we
1306 would still need to do it for the subgroups the code wouldn't have been
1307 significantly less complicated.
1310 // Return the line which contains "[our name]". If we're still not in the list,
1311 // add our line to it immediately after the last line of our parent group if we
1312 // have it or in the very beginning if we're the root group.
1313 wxFileConfigLineList
*wxFileConfigGroup
::GetGroupLine()
1315 wxLogTrace( _T("wxFileConfig"),
1316 _T(" GetGroupLine() for Group '%s'"),
1321 wxLogTrace( _T("wxFileConfig"),
1322 _T(" Getting Line item pointer") );
1324 wxFileConfigGroup
*pParent
= Parent();
1326 // this group wasn't present in local config file, add it now
1329 wxLogTrace( _T("wxFileConfig"),
1330 _T(" checking parent '%s'"),
1331 pParent
->Name().c_str() );
1333 wxString strFullName
;
1335 // add 1 to the name because we don't want to start with '/'
1336 strFullName
<< wxT("[")
1337 << FilterOutEntryName(GetFullName().c_str() + 1)
1339 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1340 pParent
->GetLastGroupLine());
1341 pParent
->SetLastGroup(this); // we're surely after all the others
1343 //else: this is the root group and so we return NULL because we don't
1344 // have any group line
1350 // Return the last line belonging to the subgroups of this group (after which
1351 // we can add a new subgroup), if we don't have any subgroups or entries our
1352 // last line is the group line (m_pLine) itself.
1353 wxFileConfigLineList
*wxFileConfigGroup
::GetLastGroupLine()
1355 // if we have any subgroups, our last line is the last line of the last
1359 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1361 wxASSERT_MSG( pLine
, _T("last group must have !NULL associated line") );
1366 // no subgroups, so the last line is the line of thelast entry (if any)
1367 return GetLastEntryLine();
1370 // return the last line belonging to the entries of this group (after which
1371 // we can add a new entry), if we don't have any entries we will add the new
1372 // one immediately after the group line itself.
1373 wxFileConfigLineList
*wxFileConfigGroup
::GetLastEntryLine()
1375 wxLogTrace( _T("wxFileConfig"),
1376 _T(" GetLastEntryLine() for Group '%s'"),
1381 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1383 wxASSERT_MSG( pLine
, _T("last entry must have !NULL associated line") );
1388 // no entries: insert after the group header, if any
1389 return GetGroupLine();
1392 void wxFileConfigGroup
::SetLastEntry(wxFileConfigEntry
*pEntry
)
1394 m_pLastEntry
= pEntry
;
1398 // the only situation in which a group without its own line can have
1399 // an entry is when the first entry is added to the initially empty
1400 // root pseudo-group
1401 wxASSERT_MSG( !m_pParent
, _T("unexpected for non root group") );
1403 // let the group know that it does have a line in the file now
1404 m_pLine
= pEntry
->GetLine();
1408 // ----------------------------------------------------------------------------
1410 // ----------------------------------------------------------------------------
1412 void wxFileConfigGroup
::Rename(const wxString
& newName
)
1414 wxCHECK_RET( m_pParent
, _T("the root group can't be renamed") );
1416 m_strName
= newName
;
1418 // +1: no leading '/'
1419 wxString strFullName
;
1420 strFullName
<< wxT("[") << (GetFullName().c_str() + 1) << wxT("]");
1422 wxFileConfigLineList
*line
= GetGroupLine();
1423 wxCHECK_RET( line
, _T("a non root group must have a corresponding line!") );
1425 line
->SetText(strFullName
);
1428 wxString wxFileConfigGroup
::GetFullName() const
1431 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1436 // ----------------------------------------------------------------------------
1438 // ----------------------------------------------------------------------------
1440 // use binary search because the array is sorted
1442 wxFileConfigGroup
::FindEntry(const wxChar
*szName
) const
1446 hi
= m_aEntries
.Count();
1448 wxFileConfigEntry
*pEntry
;
1452 pEntry
= m_aEntries
[i
];
1454 #if wxCONFIG_CASE_SENSITIVE
1455 res
= wxStrcmp(pEntry
->Name(), szName
);
1457 res
= wxStricmp(pEntry
->Name(), szName
);
1472 wxFileConfigGroup
::FindSubgroup(const wxChar
*szName
) const
1476 hi
= m_aSubgroups
.Count();
1478 wxFileConfigGroup
*pGroup
;
1482 pGroup
= m_aSubgroups
[i
];
1484 #if wxCONFIG_CASE_SENSITIVE
1485 res
= wxStrcmp(pGroup
->Name(), szName
);
1487 res
= wxStricmp(pGroup
->Name(), szName
);
1501 // ----------------------------------------------------------------------------
1502 // create a new item
1503 // ----------------------------------------------------------------------------
1505 // create a new entry and add it to the current group
1506 wxFileConfigEntry
*wxFileConfigGroup
::AddEntry(const wxString
& strName
, int nLine
)
1508 wxASSERT( FindEntry(strName
) == 0 );
1510 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1512 m_aEntries
.Add(pEntry
);
1516 // create a new group and add it to the current group
1517 wxFileConfigGroup
*wxFileConfigGroup
::AddSubgroup(const wxString
& strName
)
1519 wxASSERT( FindSubgroup(strName
) == 0 );
1521 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1523 m_aSubgroups
.Add(pGroup
);
1527 // ----------------------------------------------------------------------------
1529 // ----------------------------------------------------------------------------
1532 The delete operations are _very_ slow if we delete the last item of this
1533 group (see comments before GetXXXLineXXX functions for more details),
1534 so it's much better to start with the first entry/group if we want to
1535 delete several of them.
1538 bool wxFileConfigGroup
::DeleteSubgroupByName(const wxChar
*szName
)
1540 wxFileConfigGroup
* const pGroup
= FindSubgroup(szName
);
1542 return pGroup ?
DeleteSubgroup(pGroup
) : false;
1545 // Delete the subgroup and remove all references to it from
1546 // other data structures.
1547 bool wxFileConfigGroup
::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1549 wxCHECK_MSG( pGroup
, false, _T("deleting non existing group?") );
1551 wxLogTrace( _T("wxFileConfig"),
1552 _T("Deleting group '%s' from '%s'"),
1553 pGroup
->Name().c_str(),
1556 wxLogTrace( _T("wxFileConfig"),
1557 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1558 ((m_pLine
) ? m_pLine
->Prev() : 0),
1560 ((m_pLine
) ? m_pLine
->Next() : 0) );
1561 wxLogTrace( _T("wxFileConfig"),
1563 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1565 // delete all entries
1566 size_t nCount
= pGroup
->m_aEntries
.Count();
1568 wxLogTrace(_T("wxFileConfig"),
1569 _T("Removing %lu Entries"),
1570 (unsigned long)nCount
);
1572 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ )
1574 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1578 wxLogTrace( _T("wxFileConfig"),
1580 pLine
->Text().c_str() );
1581 m_pConfig
->LineListRemove(pLine
);
1585 // and subgroups of this subgroup
1587 nCount
= pGroup
->m_aSubgroups
.Count();
1589 wxLogTrace( _T("wxFileConfig"),
1590 _T("Removing %lu SubGroups"),
1591 (unsigned long)nCount
);
1593 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ )
1595 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1598 // finally the group itself
1600 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1604 wxLogTrace( _T("wxFileConfig"),
1605 _T(" Removing line entry for Group '%s' : '%s'"),
1606 pGroup
->Name().c_str(),
1607 pLine
->Text().c_str() );
1608 wxLogTrace( _T("wxFileConfig"),
1609 _T(" Removing from Group '%s' : '%s'"),
1611 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1613 // notice that we may do this test inside the previous "if"
1614 // because the last entry's line is surely !NULL
1616 if ( pGroup
== m_pLastGroup
)
1618 wxLogTrace( _T("wxFileConfig"),
1619 _T(" ------- Removing last group -------") );
1621 // our last entry is being deleted, so find the last one which stays.
1622 // go back until we find a subgroup or reach the group's line, unless
1623 // we are the root group, which we'll notice shortly.
1625 wxFileConfigGroup
*pNewLast
= 0;
1626 size_t nSubgroups
= m_aSubgroups
.Count();
1627 wxFileConfigLineList
*pl
;
1629 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() )
1631 // is it our subgroup?
1633 for ( size_t n
= 0; (pNewLast
== 0) && (n
< nSubgroups
); n
++ )
1635 // do _not_ call GetGroupLine! we don't want to add it to the local
1636 // file if it's not already there
1638 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1639 pNewLast
= m_aSubgroups
[n
];
1642 if ( pNewLast
!= 0 ) // found?
1646 if ( pl
== m_pLine
|| m_pParent
== 0 )
1648 wxLogTrace( _T("wxFileConfig"),
1649 _T(" ------- No previous group found -------") );
1651 wxASSERT_MSG( !pNewLast
|| m_pLine
== 0,
1652 _T("how comes it has the same line as we?") );
1654 // we've reached the group line without finding any subgroups,
1655 // or realised we removed the last group from the root.
1661 wxLogTrace( _T("wxFileConfig"),
1662 _T(" ------- Last Group set to '%s' -------"),
1663 pNewLast
->Name().c_str() );
1665 m_pLastGroup
= pNewLast
;
1669 m_pConfig
->LineListRemove(pLine
);
1673 wxLogTrace( _T("wxFileConfig"),
1674 _T(" No line entry for Group '%s'?"),
1675 pGroup
->Name().c_str() );
1678 m_aSubgroups
.Remove(pGroup
);
1684 bool wxFileConfigGroup
::DeleteEntry(const wxChar
*szName
)
1686 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1687 wxCHECK( pEntry
!= NULL
, false ); // deleting non existing item?
1689 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1690 if ( pLine
!= NULL
) {
1691 // notice that we may do this test inside the previous "if" because the
1692 // last entry's line is surely !NULL
1693 if ( pEntry
== m_pLastEntry
) {
1694 // our last entry is being deleted - find the last one which stays
1695 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1697 // go back until we find another entry or reach the group's line
1698 wxFileConfigEntry
*pNewLast
= NULL
;
1699 size_t n
, nEntries
= m_aEntries
.Count();
1700 wxFileConfigLineList
*pl
;
1701 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1702 // is it our subgroup?
1703 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1704 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1705 pNewLast
= m_aEntries
[n
];
1708 if ( pNewLast
!= NULL
) // found?
1712 if ( pl
== m_pLine
) {
1713 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1715 // we've reached the group line without finding any subgroups
1716 m_pLastEntry
= NULL
;
1719 m_pLastEntry
= pNewLast
;
1722 m_pConfig
->LineListRemove(pLine
);
1725 m_aEntries
.Remove(pEntry
);
1731 // ============================================================================
1732 // wxFileConfig::wxFileConfigEntry
1733 // ============================================================================
1735 // ----------------------------------------------------------------------------
1737 // ----------------------------------------------------------------------------
1738 wxFileConfigEntry
::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1739 const wxString
& strName
,
1741 : m_strName(strName
)
1743 wxASSERT( !strName
.IsEmpty() );
1745 m_pParent
= pParent
;
1749 m_bHasValue
= false;
1751 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1753 m_strName
.erase(0, 1); // remove first character
1756 // ----------------------------------------------------------------------------
1758 // ----------------------------------------------------------------------------
1760 void wxFileConfigEntry
::SetLine(wxFileConfigLineList
*pLine
)
1762 if ( m_pLine
!= NULL
) {
1763 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1764 Name().c_str(), m_pParent
->GetFullName().c_str());
1768 Group()->SetLastEntry(this);
1771 // second parameter is false if we read the value from file and prevents the
1772 // entry from being marked as 'dirty'
1773 void wxFileConfigEntry
::SetValue(const wxString
& strValue
, bool bUser
)
1775 if ( bUser
&& IsImmutable() )
1777 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1782 // do nothing if it's the same value: but don't test for it if m_bHasValue
1783 // hadn't been set yet or we'd never write empty values to the file
1784 if ( m_bHasValue
&& strValue
== m_strValue
)
1788 m_strValue
= strValue
;
1792 wxString strValFiltered
;
1794 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
)
1796 strValFiltered
= strValue
;
1799 strValFiltered
= FilterOutValue(strValue
);
1803 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strValFiltered
;
1807 // entry was read from the local config file, just modify the line
1808 m_pLine
->SetText(strLine
);
1810 else // this entry didn't exist in the local file
1812 // add a new line to the file
1813 wxFileConfigLineList
*line
= Group()->GetLastEntryLine();
1814 m_pLine
= Group()->Config()->LineListInsert(strLine
, line
);
1816 Group()->SetLastEntry(this);
1821 // ============================================================================
1823 // ============================================================================
1825 // ----------------------------------------------------------------------------
1826 // compare functions for array sorting
1827 // ----------------------------------------------------------------------------
1829 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1831 #if wxCONFIG_CASE_SENSITIVE
1832 return wxStrcmp(p1
->Name(), p2
->Name());
1834 return wxStricmp(p1
->Name(), p2
->Name());
1838 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1840 #if wxCONFIG_CASE_SENSITIVE
1841 return wxStrcmp(p1
->Name(), p2
->Name());
1843 return wxStricmp(p1
->Name(), p2
->Name());
1847 // ----------------------------------------------------------------------------
1849 // ----------------------------------------------------------------------------
1851 // undo FilterOutValue
1852 static wxString
FilterInValue(const wxString
& str
)
1855 strResult
.Alloc(str
.Len());
1857 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1859 for ( size_t n
= bQuoted ?
1 : 0; n
< str
.Len(); n
++ ) {
1860 if ( str
[n
] == wxT('\\') ) {
1861 switch ( str
[++n
] ) {
1863 strResult
+= wxT('\n');
1867 strResult
+= wxT('\r');
1871 strResult
+= wxT('\t');
1875 strResult
+= wxT('\\');
1879 strResult
+= wxT('"');
1884 if ( str
[n
] != wxT('"') || !bQuoted
)
1885 strResult
+= str
[n
];
1886 else if ( n
!= str
.Len() - 1 ) {
1887 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1890 //else: it's the last quote of a quoted string, ok
1897 // quote the string before writing it to file
1898 static wxString
FilterOutValue(const wxString
& str
)
1904 strResult
.Alloc(str
.Len());
1906 // quoting is necessary to preserve spaces in the beginning of the string
1907 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1910 strResult
+= wxT('"');
1913 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1936 //else: fall through
1939 strResult
+= str
[n
];
1940 continue; // nothing special to do
1943 // we get here only for special characters
1944 strResult
<< wxT('\\') << c
;
1948 strResult
+= wxT('"');
1953 // undo FilterOutEntryName
1954 static wxString
FilterInEntryName(const wxString
& str
)
1957 strResult
.Alloc(str
.Len());
1959 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
1960 if ( *pc
== wxT('\\') )
1969 // sanitize entry or group name: insert '\\' before any special characters
1970 static wxString
FilterOutEntryName(const wxString
& str
)
1973 strResult
.Alloc(str
.Len());
1975 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
1976 const wxChar c
= *pc
;
1978 // we explicitly allow some of "safe" chars and 8bit ASCII characters
1979 // which will probably never have special meaning and with which we can't
1980 // use isalnum() anyhow (in ASCII built, in Unicode it's just fine)
1982 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
1983 // should *not* be quoted
1986 ((unsigned char)c
< 127) &&
1988 !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) )
1990 strResult
+= wxT('\\');
1999 // we can't put ?: in the ctor initializer list because it confuses some
2000 // broken compilers (Borland C++)
2001 static wxString
GetAppName(const wxString
& appName
)
2003 if ( !appName
&& wxTheApp
)
2004 return wxTheApp
->GetAppName();
2009 #endif // wxUSE_CONFIG