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 ///////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "fileconf.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
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 WX_DEFINE_SORTED_EXPORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
);
110 WX_DEFINE_SORTED_EXPORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
);
112 // ----------------------------------------------------------------------------
113 // wxFileConfigLineList
114 // ----------------------------------------------------------------------------
116 // we store all lines of the local config file as a linked list in memory
117 class wxFileConfigLineList
120 void SetNext(wxFileConfigLineList
*pNext
) { m_pNext
= pNext
; }
121 void SetPrev(wxFileConfigLineList
*pPrev
) { m_pPrev
= pPrev
; }
124 wxFileConfigLineList(const wxString
& str
,
125 wxFileConfigLineList
*pNext
= NULL
) : m_strLine(str
)
126 { SetNext(pNext
); SetPrev(NULL
); }
128 // next/prev nodes in the linked list
129 wxFileConfigLineList
*Next() const { return m_pNext
; }
130 wxFileConfigLineList
*Prev() const { return m_pPrev
; }
132 // get/change lines text
133 void SetText(const wxString
& str
) { m_strLine
= str
; }
134 const wxString
& Text() const { return m_strLine
; }
137 wxString m_strLine
; // line contents
138 wxFileConfigLineList
*m_pNext
, // next node
139 *m_pPrev
; // previous one
141 DECLARE_NO_COPY_CLASS(wxFileConfigLineList
)
144 // ----------------------------------------------------------------------------
145 // wxFileConfigEntry: a name/value pair
146 // ----------------------------------------------------------------------------
148 class wxFileConfigEntry
151 wxFileConfigGroup
*m_pParent
; // group that contains us
153 wxString m_strName
, // entry name
155 bool m_bDirty
:1, // changed since last read?
156 m_bImmutable
:1, // can be overriden locally?
157 m_bHasValue
:1; // set after first call to SetValue()
159 int m_nLine
; // used if m_pLine == NULL only
161 // pointer to our line in the linked list or NULL if it was found in global
162 // file (which we don't modify)
163 wxFileConfigLineList
*m_pLine
;
166 wxFileConfigEntry(wxFileConfigGroup
*pParent
,
167 const wxString
& strName
, int nLine
);
170 const wxString
& Name() const { return m_strName
; }
171 const wxString
& Value() const { return m_strValue
; }
172 wxFileConfigGroup
*Group() const { return m_pParent
; }
173 bool IsDirty() const { return m_bDirty
; }
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
);
183 void SetLine(wxFileConfigLineList
*pLine
);
185 DECLARE_NO_COPY_CLASS(wxFileConfigEntry
)
188 // ----------------------------------------------------------------------------
189 // wxFileConfigGroup: container of entries and other groups
190 // ----------------------------------------------------------------------------
192 class wxFileConfigGroup
195 wxFileConfig
*m_pConfig
; // config object we belong to
196 wxFileConfigGroup
*m_pParent
; // parent group (NULL for root group)
197 ArrayEntries m_aEntries
; // entries in this group
198 ArrayGroups m_aSubgroups
; // subgroups
199 wxString m_strName
; // group's name
200 bool m_bDirty
; // if FALSE => all subgroups are not dirty
201 wxFileConfigLineList
*m_pLine
; // pointer to our line in the linked list
202 wxFileConfigEntry
*m_pLastEntry
; // last entry/subgroup of this group in the
203 wxFileConfigGroup
*m_pLastGroup
; // local file (we insert new ones after it)
205 // DeleteSubgroupByName helper
206 bool DeleteSubgroup(wxFileConfigGroup
*pGroup
);
210 wxFileConfigGroup(wxFileConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
212 // dtor deletes all entries and subgroups also
213 ~wxFileConfigGroup();
216 const wxString
& Name() const { return m_strName
; }
217 wxFileConfigGroup
*Parent() const { return m_pParent
; }
218 wxFileConfig
*Config() const { return m_pConfig
; }
219 bool IsDirty() const { return m_bDirty
; }
221 const ArrayEntries
& Entries() const { return m_aEntries
; }
222 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
223 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
225 // find entry/subgroup (NULL if not found)
226 wxFileConfigGroup
*FindSubgroup(const wxChar
*szName
) const;
227 wxFileConfigEntry
*FindEntry (const wxChar
*szName
) const;
229 // delete entry/subgroup, return FALSE if doesn't exist
230 bool DeleteSubgroupByName(const wxChar
*szName
);
231 bool DeleteEntry(const wxChar
*szName
);
233 // create new entry/subgroup returning pointer to newly created element
234 wxFileConfigGroup
*AddSubgroup(const wxString
& strName
);
235 wxFileConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= wxNOT_FOUND
);
237 // will also recursively set parent's dirty flag
239 void SetLine(wxFileConfigLineList
*pLine
);
241 // rename: no checks are done to ensure that the name is unique!
242 void Rename(const wxString
& newName
);
245 wxString
GetFullName() const;
247 // get the last line belonging to an entry/subgroup of this group
248 wxFileConfigLineList
*GetGroupLine(); // line which contains [group]
249 wxFileConfigLineList
*GetLastEntryLine(); // after which our subgroups start
250 wxFileConfigLineList
*GetLastGroupLine(); // after which the next group starts
252 // called by entries/subgroups when they're created/deleted
253 void SetLastEntry(wxFileConfigEntry
*pEntry
) { m_pLastEntry
= pEntry
; }
254 void SetLastGroup(wxFileConfigGroup
*pGroup
) { m_pLastGroup
= pGroup
; }
256 DECLARE_NO_COPY_CLASS(wxFileConfigGroup
)
259 // ============================================================================
261 // ============================================================================
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
266 wxString
wxFileConfig::GetGlobalDir()
270 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
271 strDir
= wxT("sys$manager:");
272 #elif defined(__WXMAC__)
273 strDir
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
274 #elif defined( __UNIX__ )
275 strDir
= wxT("/etc/");
276 #elif defined(__WXPM__)
277 ULONG aulSysInfo
[QSV_MAX
] = {0};
281 rc
= DosQuerySysInfo( 1L, QSV_MAX
, (PVOID
)aulSysInfo
, sizeof(ULONG
)*QSV_MAX
);
284 drive
= aulSysInfo
[QSV_BOOT_DRIVE
- 1];
285 strDir
.Printf(wxT("%c:\\OS2\\"), 'A'+drive
-1);
287 #elif defined(__WXSTUBS__)
288 wxASSERT_MSG( FALSE
, wxT("TODO") ) ;
289 #elif defined(__DOS__)
290 // There's no such thing as global cfg dir in MS-DOS, let's return
291 // current directory (FIXME_MGL?)
294 wxChar szWinDir
[MAX_PATH
];
295 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
299 #endif // Unix/Windows
304 wxString
wxFileConfig::GetLocalDir()
308 #if defined(__WXMAC__) || defined(__DOS__)
309 // no local dir concept on Mac OS 9 or MS-DOS
310 return GetGlobalDir() ;
312 wxGetHomeDir(&strDir
);
316 if (strDir
.Last() != wxT(']'))
318 if (strDir
.Last() != wxT('/')) strDir
<< wxT('/');
320 if (strDir
.Last() != wxT('\\')) strDir
<< wxT('\\');
327 wxString
wxFileConfig::GetGlobalFileName(const wxChar
*szFile
)
329 wxString str
= GetGlobalDir();
332 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
333 #if defined( __WXMAC__ )
334 str
<< wxT(" Preferences") ;
335 #elif defined( __UNIX__ )
344 wxString
wxFileConfig::GetLocalFileName(const wxChar
*szFile
)
347 // On VMS I saw the problem that the home directory was appended
348 // twice for the configuration file. Does that also happen for
350 wxString str
= wxT( '.' );
352 wxString str
= GetLocalDir();
355 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
361 #if defined(__WINDOWS__) || defined(__DOS__)
362 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
367 str
<< wxT(" Preferences") ;
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 void wxFileConfig::Init()
380 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
385 // It's not an error if (one of the) file(s) doesn't exist.
387 // parse the global file
388 if ( !m_strGlobalFile
.IsEmpty() && wxFile::Exists(m_strGlobalFile
) )
390 wxTextFile
fileGlobal(m_strGlobalFile
);
392 if ( fileGlobal
.Open(m_conv
/*ignored in ANSI build*/) )
394 Parse(fileGlobal
, FALSE
/* global */);
399 wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile
.c_str());
403 // parse the local file
404 if ( !m_strLocalFile
.IsEmpty() && wxFile::Exists(m_strLocalFile
) )
406 wxTextFile
fileLocal(m_strLocalFile
);
407 if ( fileLocal
.Open(m_conv
/*ignored in ANSI build*/) )
409 Parse(fileLocal
, TRUE
/* local */);
414 wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile
.c_str() );
419 // constructor supports creation of wxFileConfig objects of any type
420 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
421 const wxString
& strLocal
, const wxString
& strGlobal
,
422 long style
, wxMBConv
& conv
)
423 : wxConfigBase(::GetAppName(appName
), vendorName
,
426 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
),
429 // Make up names for files if empty
430 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
431 m_strLocalFile
= GetLocalFileName(GetAppName());
433 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
434 m_strGlobalFile
= GetGlobalFileName(GetAppName());
436 // Check if styles are not supplied, but filenames are, in which case
437 // add the correct styles.
438 if ( !m_strLocalFile
.IsEmpty() )
439 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
441 if ( !m_strGlobalFile
.IsEmpty() )
442 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
444 // if the path is not absolute, prepend the standard directory to it
445 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
446 if ( !(style
& wxCONFIG_USE_RELATIVE_PATH
) )
448 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
450 wxString strLocal
= m_strLocalFile
;
451 m_strLocalFile
= GetLocalDir();
452 m_strLocalFile
<< strLocal
;
455 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
457 wxString strGlobal
= m_strGlobalFile
;
458 m_strGlobalFile
= GetGlobalDir();
459 m_strGlobalFile
<< strGlobal
;
470 wxFileConfig::wxFileConfig(wxInputStream
&inStream
, wxMBConv
& conv
)
473 // always local_file when this constructor is called (?)
474 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
477 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxT(""), this);
482 // translate everything to the current (platform-dependent) line
483 // termination character
489 while ( !inStream
.Read(buf
, WXSIZEOF(buf
)).Eof() )
490 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
492 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
494 strTrans
= wxTextBuffer::Translate(strTmp
);
497 wxMemoryText memText
;
499 // Now we can add the text to the memory text. To do this we extract line
500 // by line from the translated string, until we've reached the end.
502 // VZ: all this is horribly inefficient, we should do the translation on
503 // the fly in one pass saving both memory and time (TODO)
505 const wxChar
*pEOL
= wxTextBuffer::GetEOL(wxTextBuffer::typeDefault
);
506 const size_t EOLLen
= wxStrlen(pEOL
);
508 int posLineStart
= strTrans
.Find(pEOL
);
509 while ( posLineStart
!= -1 )
511 wxString
line(strTrans
.Left(posLineStart
));
513 memText
.AddLine(line
);
515 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
517 posLineStart
= strTrans
.Find(pEOL
);
520 // also add whatever we have left in the translated string.
521 memText
.AddLine(strTrans
);
523 // Finally we can parse it all.
524 Parse(memText
, TRUE
/* local */);
529 #endif // wxUSE_STREAMS
531 void wxFileConfig::CleanUp()
535 wxFileConfigLineList
*pCur
= m_linesHead
;
536 while ( pCur
!= NULL
) {
537 wxFileConfigLineList
*pNext
= pCur
->Next();
543 wxFileConfig::~wxFileConfig()
550 // ----------------------------------------------------------------------------
551 // parse a config file
552 // ----------------------------------------------------------------------------
554 void wxFileConfig::Parse(wxTextBuffer
& buffer
, bool bLocal
)
556 const wxChar
*pStart
;
560 size_t nLineCount
= buffer
.GetLineCount();
562 for ( size_t n
= 0; n
< nLineCount
; n
++ )
566 // add the line to linked list
569 LineListAppend(strLine
);
571 // let the root group have it start line as well
574 m_pCurrentGroup
->SetLine(m_linesTail
);
579 // skip leading spaces
580 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
583 // skip blank/comment lines
584 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
587 if ( *pStart
== wxT('[') ) { // a new group
590 while ( *++pEnd
!= wxT(']') ) {
591 if ( *pEnd
== wxT('\\') ) {
592 // the next char is escaped, so skip it even if it is ']'
596 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
597 // we reached the end of line, break out of the loop
602 if ( *pEnd
!= wxT(']') ) {
603 wxLogError(_("file '%s': unexpected character %c at line %d."),
604 buffer
.GetName(), *pEnd
, n
+ 1);
605 continue; // skip this line
608 // group name here is always considered as abs path
611 strGroup
<< wxCONFIG_PATH_SEPARATOR
612 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
614 // will create it if doesn't yet exist
618 m_pCurrentGroup
->SetLine(m_linesTail
);
620 // check that there is nothing except comments left on this line
622 while ( *++pEnd
!= wxT('\0') && bCont
) {
631 // ignore whitespace ('\n' impossible here)
635 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
636 buffer
.GetName(), n
+ 1, pEnd
);
642 const wxChar
*pEnd
= pStart
;
643 while ( *pEnd
&& *pEnd
!= wxT('=') && !wxIsspace(*pEnd
) ) {
644 if ( *pEnd
== wxT('\\') ) {
645 // next character may be space or not - still take it because it's
646 // quoted (unless there is nothing)
649 // the error message will be given below anyhow
657 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
)));
660 while ( wxIsspace(*pEnd
) )
663 if ( *pEnd
++ != wxT('=') ) {
664 wxLogError(_("file '%s', line %d: '=' expected."),
665 buffer
.GetName(), n
+ 1);
668 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
670 if ( pEntry
== NULL
) {
672 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
675 pEntry
->SetLine(m_linesTail
);
678 if ( bLocal
&& pEntry
->IsImmutable() ) {
679 // immutable keys can't be changed by user
680 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
681 buffer
.GetName(), n
+ 1, strKey
.c_str());
684 // the condition below catches the cases (a) and (b) but not (c):
685 // (a) global key found second time in global file
686 // (b) key found second (or more) time in local file
687 // (c) key from global file now found in local one
688 // which is exactly what we want.
689 else if ( !bLocal
|| pEntry
->IsLocal() ) {
690 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
691 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
694 pEntry
->SetLine(m_linesTail
);
699 while ( wxIsspace(*pEnd
) )
702 wxString value
= pEnd
;
703 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
) )
704 value
= FilterInValue(value
);
706 pEntry
->SetValue(value
, FALSE
);
712 // ----------------------------------------------------------------------------
714 // ----------------------------------------------------------------------------
716 void wxFileConfig::SetRootPath()
719 m_pCurrentGroup
= m_pRootGroup
;
722 void wxFileConfig::SetPath(const wxString
& strPath
)
724 wxArrayString aParts
;
726 if ( strPath
.IsEmpty() ) {
731 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
733 wxSplitPath(aParts
, strPath
);
736 // relative path, combine with current one
737 wxString strFullPath
= m_strPath
;
738 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
739 wxSplitPath(aParts
, strFullPath
);
742 // change current group
744 m_pCurrentGroup
= m_pRootGroup
;
745 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
746 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
747 if ( pNextGroup
== NULL
)
748 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
749 m_pCurrentGroup
= pNextGroup
;
752 // recombine path parts in one variable
754 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
755 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
759 // ----------------------------------------------------------------------------
761 // ----------------------------------------------------------------------------
763 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
766 return GetNextGroup(str
, lIndex
);
769 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
771 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
772 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
779 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
782 return GetNextEntry(str
, lIndex
);
785 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
787 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
788 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
795 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
797 size_t n
= m_pCurrentGroup
->Entries().Count();
799 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
800 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
801 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
802 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
803 n
+= GetNumberOfEntries(TRUE
);
804 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
811 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
813 size_t n
= m_pCurrentGroup
->Groups().Count();
815 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
816 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
817 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
818 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
819 n
+= GetNumberOfGroups(TRUE
);
820 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
827 // ----------------------------------------------------------------------------
828 // tests for existence
829 // ----------------------------------------------------------------------------
831 bool wxFileConfig::HasGroup(const wxString
& strName
) const
833 wxConfigPathChanger
path(this, strName
);
835 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
836 return pGroup
!= NULL
;
839 bool wxFileConfig::HasEntry(const wxString
& strName
) const
841 wxConfigPathChanger
path(this, strName
);
843 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
844 return pEntry
!= NULL
;
847 // ----------------------------------------------------------------------------
849 // ----------------------------------------------------------------------------
851 bool wxFileConfig::DoReadString(const wxString
& key
, wxString
* pStr
) const
853 wxConfigPathChanger
path(this, key
);
855 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
856 if (pEntry
== NULL
) {
860 *pStr
= pEntry
->Value();
865 bool wxFileConfig::DoReadLong(const wxString
& key
, long *pl
) const
868 if ( !Read(key
, & str
) )
872 return str
.ToLong(pl
) ;
875 bool wxFileConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
877 wxConfigPathChanger
path(this, key
);
878 wxString strName
= path
.Name();
880 wxLogTrace( _T("wxFileConfig"),
881 _T(" Writing String '%s' = '%s' to Group '%s'"),
886 if ( strName
.IsEmpty() )
888 // setting the value of a group is an error
890 wxASSERT_MSG( wxIsEmpty(szValue
), wxT("can't set value of a group!") );
892 // ... except if it's empty in which case it's a way to force it's creation
894 wxLogTrace( _T("wxFileConfig"),
895 _T(" Creating group %s"),
896 m_pCurrentGroup
->Name().c_str() );
898 m_pCurrentGroup
->SetDirty();
900 // this will add a line for this group if it didn't have it before
902 (void)m_pCurrentGroup
->GetGroupLine();
907 // check that the name is reasonable
909 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
)
911 wxLogError( _("Config entry name cannot start with '%c'."),
912 wxCONFIG_IMMUTABLE_PREFIX
);
916 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
920 wxLogTrace( _T("wxFileConfig"),
921 _T(" Adding Entry %s"),
923 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
926 wxLogTrace( _T("wxFileConfig"),
927 _T(" Setting value %s"),
929 pEntry
->SetValue(szValue
);
935 bool wxFileConfig::DoWriteLong(const wxString
& key
, long lValue
)
937 return Write(key
, wxString::Format(_T("%ld"), lValue
));
940 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
942 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() || !m_strLocalFile
)
946 // set the umask if needed
950 umaskOld
= umask((mode_t
)m_umask
);
954 wxTempFile
file(m_strLocalFile
);
956 if ( !file
.IsOpened() )
958 wxLogError(_("can't open user configuration file."));
962 // write all strings to file
963 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
965 wxString line
= p
->Text();
966 line
+= wxTextFile::GetEOL();
967 if ( !file
.Write(line
, m_conv
) )
969 wxLogError(_("can't write user configuration file."));
974 bool ret
= file
.Commit();
976 #if defined(__WXMAC__)
981 wxMacFilename2FSSpec( m_strLocalFile
, &spec
) ;
983 if ( FSpGetFInfo( &spec
, &finfo
) == noErr
)
985 finfo
.fdType
= 'TEXT' ;
986 finfo
.fdCreator
= 'ttxt' ;
987 FSpSetFInfo( &spec
, &finfo
) ;
993 // restore the old umask if we changed it
996 (void)umask(umaskOld
);
1003 // ----------------------------------------------------------------------------
1004 // renaming groups/entries
1005 // ----------------------------------------------------------------------------
1007 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
1008 const wxString
& newName
)
1010 // check that the entry exists
1011 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
1015 // check that the new entry doesn't already exist
1016 if ( m_pCurrentGroup
->FindEntry(newName
) )
1019 // delete the old entry, create the new one
1020 wxString value
= oldEntry
->Value();
1021 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
1024 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
1025 newEntry
->SetValue(value
);
1030 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
1031 const wxString
& newName
)
1033 // check that the group exists
1034 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
1038 // check that the new group doesn't already exist
1039 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
1042 group
->Rename(newName
);
1047 // ----------------------------------------------------------------------------
1048 // delete groups/entries
1049 // ----------------------------------------------------------------------------
1051 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1053 wxConfigPathChanger
path(this, key
);
1055 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1058 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1059 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1060 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1061 SetPath(wxT("..")); // changes m_pCurrentGroup!
1062 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
1064 //else: never delete the root group
1070 bool wxFileConfig::DeleteGroup(const wxString
& key
)
1072 wxConfigPathChanger
path(this, key
);
1074 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
1077 bool wxFileConfig::DeleteAll()
1081 if ( wxRemove(m_strLocalFile
) == -1 )
1082 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile
.c_str());
1084 m_strLocalFile
= m_strGlobalFile
= wxT("");
1090 // ----------------------------------------------------------------------------
1091 // linked list functions
1092 // ----------------------------------------------------------------------------
1094 // append a new line to the end of the list
1096 wxFileConfigLineList
*wxFileConfig::LineListAppend(const wxString
& str
)
1098 wxLogTrace( _T("wxFileConfig"),
1099 _T(" ** Adding Line '%s'"),
1101 wxLogTrace( _T("wxFileConfig"),
1103 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1104 wxLogTrace( _T("wxFileConfig"),
1106 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1108 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1110 if ( m_linesTail
== NULL
)
1113 m_linesHead
= pLine
;
1118 m_linesTail
->SetNext(pLine
);
1119 pLine
->SetPrev(m_linesTail
);
1122 m_linesTail
= pLine
;
1124 wxLogTrace( _T("wxFileConfig"),
1126 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1127 wxLogTrace( _T("wxFileConfig"),
1129 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1134 // insert a new line after the given one or in the very beginning if !pLine
1136 wxFileConfigLineList
*wxFileConfig::LineListInsert(const wxString
& str
,
1137 wxFileConfigLineList
*pLine
)
1139 wxLogTrace( _T("wxFileConfig"),
1140 _T(" ** Inserting Line '%s' after '%s'"),
1142 ((pLine
) ? pLine
->Text().c_str() : wxEmptyString
) );
1143 wxLogTrace( _T("wxFileConfig"),
1145 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1146 wxLogTrace( _T("wxFileConfig"),
1148 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1150 if ( pLine
== m_linesTail
)
1151 return LineListAppend(str
);
1153 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1154 if ( pLine
== NULL
)
1156 // prepend to the list
1157 pNewLine
->SetNext(m_linesHead
);
1158 m_linesHead
->SetPrev(pNewLine
);
1159 m_linesHead
= pNewLine
;
1163 // insert before pLine
1164 wxFileConfigLineList
*pNext
= pLine
->Next();
1165 pNewLine
->SetNext(pNext
);
1166 pNewLine
->SetPrev(pLine
);
1167 pNext
->SetPrev(pNewLine
);
1168 pLine
->SetNext(pNewLine
);
1171 wxLogTrace( _T("wxFileConfig"),
1173 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1174 wxLogTrace( _T("wxFileConfig"),
1176 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1181 void wxFileConfig::LineListRemove(wxFileConfigLineList
*pLine
)
1183 wxLogTrace( _T("wxFileConfig"),
1184 _T(" ** Removing Line '%s'"),
1185 pLine
->Text().c_str() );
1186 wxLogTrace( _T("wxFileConfig"),
1188 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1189 wxLogTrace( _T("wxFileConfig"),
1191 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1193 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1194 *pNext
= pLine
->Next();
1198 if ( pPrev
== NULL
)
1199 m_linesHead
= pNext
;
1201 pPrev
->SetNext(pNext
);
1205 if ( pNext
== NULL
)
1206 m_linesTail
= pPrev
;
1208 pNext
->SetPrev(pPrev
);
1210 wxLogTrace( _T("wxFileConfig"),
1212 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1213 wxLogTrace( _T("wxFileConfig"),
1215 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1220 bool wxFileConfig::LineListIsEmpty()
1222 return m_linesHead
== NULL
;
1225 // ============================================================================
1226 // wxFileConfig::wxFileConfigGroup
1227 // ============================================================================
1229 // ----------------------------------------------------------------------------
1231 // ----------------------------------------------------------------------------
1234 wxFileConfigGroup::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1235 const wxString
& strName
,
1236 wxFileConfig
*pConfig
)
1237 : m_aEntries(CompareEntries
),
1238 m_aSubgroups(CompareGroups
),
1241 m_pConfig
= pConfig
;
1242 m_pParent
= pParent
;
1246 m_pLastEntry
= NULL
;
1247 m_pLastGroup
= NULL
;
1250 // dtor deletes all children
1251 wxFileConfigGroup::~wxFileConfigGroup()
1254 size_t n
, nCount
= m_aEntries
.Count();
1255 for ( n
= 0; n
< nCount
; n
++ )
1256 delete m_aEntries
[n
];
1259 nCount
= m_aSubgroups
.Count();
1260 for ( n
= 0; n
< nCount
; n
++ )
1261 delete m_aSubgroups
[n
];
1264 // ----------------------------------------------------------------------------
1266 // ----------------------------------------------------------------------------
1268 void wxFileConfigGroup::SetLine(wxFileConfigLineList
*pLine
)
1270 wxASSERT( m_pLine
== 0 ); // shouldn't be called twice
1275 This is a bit complicated, so let me explain it in details. All lines that
1276 were read from the local file (the only one we will ever modify) are stored
1277 in a (doubly) linked list. Our problem is to know at which position in this
1278 list should we insert the new entries/subgroups. To solve it we keep three
1279 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1281 m_pLine points to the line containing "[group_name]"
1282 m_pLastEntry points to the last entry of this group in the local file.
1283 m_pLastGroup subgroup
1285 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1286 from the local file, the corresponding variable is set. However, if the group
1287 was read from the global file and then modified or created by the application
1288 these variables are still NULL and we need to create the corresponding lines.
1289 See the following functions (and comments preceding them) for the details of
1292 Also, when our last entry/group are deleted we need to find the new last
1293 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1294 of lines until it either founds an entry/subgroup (and this is the new last
1295 element) or the m_pLine of the group, in which case there are no more entries
1296 (or subgroups) left and m_pLast<element> becomes NULL.
1298 NB: This last problem could be avoided for entries if we added new entries
1299 immediately after m_pLine, but in this case the entries would appear
1300 backwards in the config file (OTOH, it's not that important) and as we
1301 would still need to do it for the subgroups the code wouldn't have been
1302 significantly less complicated.
1305 // Return the line which contains "[our name]". If we're still not in the list,
1306 // add our line to it immediately after the last line of our parent group if we
1307 // have it or in the very beginning if we're the root group.
1308 wxFileConfigLineList
*wxFileConfigGroup::GetGroupLine()
1310 wxLogTrace( _T("wxFileConfig"),
1311 _T(" GetGroupLine() for Group '%s'"),
1316 wxLogTrace( _T("wxFileConfig"),
1317 _T(" Getting Line item pointer") );
1319 wxFileConfigGroup
*pParent
= Parent();
1321 // this group wasn't present in local config file, add it now
1325 wxLogTrace( _T("wxFileConfig"),
1326 _T(" checking parent '%s'"),
1327 pParent
->Name().c_str() );
1329 wxString strFullName
;
1331 strFullName
<< wxT("[") // +1: no '/'
1332 << FilterOutEntryName(GetFullName().c_str() + 1)
1334 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1335 pParent
->GetLastGroupLine());
1336 pParent
->SetLastGroup(this); // we're surely after all the others
1340 // we return NULL, so that LineListInsert() will insert us in the
1348 // Return the last line belonging to the subgroups of this group (after which
1349 // we can add a new subgroup), if we don't have any subgroups or entries our
1350 // last line is the group line (m_pLine) itself.
1351 wxFileConfigLineList
*wxFileConfigGroup::GetLastGroupLine()
1353 // if we have any subgroups, our last line is
1354 // the last line of the last subgroup
1356 if ( m_pLastGroup
!= 0 )
1358 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1360 wxASSERT( pLine
!= 0 ); // last group must have !NULL associated line
1364 // no subgroups, so the last line is the line of thelast entry (if any)
1366 return GetLastEntryLine();
1369 // return the last line belonging to the entries of this group (after which
1370 // we can add a new entry), if we don't have any entries we will add the new
1371 // one immediately after the group line itself.
1372 wxFileConfigLineList
*wxFileConfigGroup::GetLastEntryLine()
1374 wxLogTrace( _T("wxFileConfig"),
1375 _T(" GetLastEntryLine() for Group '%s'"),
1378 if ( m_pLastEntry
!= 0 )
1380 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1382 wxASSERT( pLine
!= 0 ); // last entry must have !NULL associated line
1386 // no entries: insert after the group header
1388 return GetGroupLine();
1391 // ----------------------------------------------------------------------------
1393 // ----------------------------------------------------------------------------
1395 void wxFileConfigGroup::Rename(const wxString
& newName
)
1397 m_strName
= newName
;
1399 wxFileConfigLineList
*line
= GetGroupLine();
1400 wxString strFullName
;
1401 strFullName
<< wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); // +1: no '/'
1402 line
->SetText(strFullName
);
1407 wxString
wxFileConfigGroup::GetFullName() const
1410 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1415 // ----------------------------------------------------------------------------
1417 // ----------------------------------------------------------------------------
1419 // use binary search because the array is sorted
1421 wxFileConfigGroup::FindEntry(const wxChar
*szName
) const
1425 hi
= m_aEntries
.Count();
1427 wxFileConfigEntry
*pEntry
;
1431 pEntry
= m_aEntries
[i
];
1433 #if wxCONFIG_CASE_SENSITIVE
1434 res
= wxStrcmp(pEntry
->Name(), szName
);
1436 res
= wxStricmp(pEntry
->Name(), szName
);
1451 wxFileConfigGroup::FindSubgroup(const wxChar
*szName
) const
1455 hi
= m_aSubgroups
.Count();
1457 wxFileConfigGroup
*pGroup
;
1461 pGroup
= m_aSubgroups
[i
];
1463 #if wxCONFIG_CASE_SENSITIVE
1464 res
= wxStrcmp(pGroup
->Name(), szName
);
1466 res
= wxStricmp(pGroup
->Name(), szName
);
1480 // ----------------------------------------------------------------------------
1481 // create a new item
1482 // ----------------------------------------------------------------------------
1484 // create a new entry and add it to the current group
1485 wxFileConfigEntry
*wxFileConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1487 wxASSERT( FindEntry(strName
) == 0 );
1489 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1491 m_aEntries
.Add(pEntry
);
1495 // create a new group and add it to the current group
1496 wxFileConfigGroup
*wxFileConfigGroup::AddSubgroup(const wxString
& strName
)
1498 wxASSERT( FindSubgroup(strName
) == 0 );
1500 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1502 m_aSubgroups
.Add(pGroup
);
1506 // ----------------------------------------------------------------------------
1508 // ----------------------------------------------------------------------------
1511 The delete operations are _very_ slow if we delete the last item of this
1512 group (see comments before GetXXXLineXXX functions for more details),
1513 so it's much better to start with the first entry/group if we want to
1514 delete several of them.
1517 bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar
*szName
)
1519 wxFileConfigGroup
* const pGroup
= FindSubgroup(szName
);
1521 return pGroup
? DeleteSubgroup(pGroup
) : FALSE
;
1524 // Delete the subgroup and remove all references to it from
1525 // other data structures.
1526 bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1528 wxCHECK_MSG( pGroup
, FALSE
, _T("deleting non existing group?") );
1530 wxLogTrace( _T("wxFileConfig"),
1531 _T("Deleting group '%s' from '%s'"),
1532 pGroup
->Name().c_str(),
1535 wxLogTrace( _T("wxFileConfig"),
1536 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1537 ((m_pLine
) ? m_pLine
->Prev() : 0),
1539 ((m_pLine
) ? m_pLine
->Next() : 0) );
1540 wxLogTrace( _T("wxFileConfig"),
1542 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1544 // delete all entries
1545 size_t nCount
= pGroup
->m_aEntries
.Count();
1547 wxLogTrace(_T("wxFileConfig"),
1548 _T("Removing %lu Entries"),
1549 (unsigned long)nCount
);
1551 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ )
1553 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1557 wxLogTrace( _T("wxFileConfig"),
1559 pLine
->Text().c_str() );
1560 m_pConfig
->LineListRemove(pLine
);
1564 // and subgroups of this subgroup
1566 nCount
= pGroup
->m_aSubgroups
.Count();
1568 wxLogTrace( _T("wxFileConfig"),
1569 _T("Removing %lu SubGroups"),
1570 (unsigned long)nCount
);
1572 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ )
1574 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1577 // finally the group itself
1579 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1583 wxLogTrace( _T("wxFileConfig"),
1584 _T(" Removing line entry for Group '%s' : '%s'"),
1585 pGroup
->Name().c_str(),
1586 pLine
->Text().c_str() );
1587 wxLogTrace( _T("wxFileConfig"),
1588 _T(" Removing from Group '%s' : '%s'"),
1590 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1592 // notice that we may do this test inside the previous "if"
1593 // because the last entry's line is surely !NULL
1595 if ( pGroup
== m_pLastGroup
)
1597 wxLogTrace( _T("wxFileConfig"),
1598 _T(" ------- Removing last group -------") );
1600 // our last entry is being deleted, so find the last one which stays.
1601 // go back until we find a subgroup or reach the group's line, unless
1602 // we are the root group, which we'll notice shortly.
1604 wxFileConfigGroup
*pNewLast
= 0;
1605 size_t nSubgroups
= m_aSubgroups
.Count();
1606 wxFileConfigLineList
*pl
;
1608 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() )
1610 // is it our subgroup?
1612 for ( size_t n
= 0; (pNewLast
== 0) && (n
< nSubgroups
); n
++ )
1614 // do _not_ call GetGroupLine! we don't want to add it to the local
1615 // file if it's not already there
1617 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1618 pNewLast
= m_aSubgroups
[n
];
1621 if ( pNewLast
!= 0 ) // found?
1625 if ( pl
== m_pLine
|| m_pParent
== 0 )
1627 wxLogTrace( _T("wxFileConfig"),
1628 _T(" ------- No previous group found -------") );
1630 wxASSERT_MSG( !pNewLast
|| m_pLine
== 0,
1631 _T("how comes it has the same line as we?") );
1633 // we've reached the group line without finding any subgroups,
1634 // or realised we removed the last group from the root.
1640 wxLogTrace( _T("wxFileConfig"),
1641 _T(" ------- Last Group set to '%s' -------"),
1642 pNewLast
->Name().c_str() );
1644 m_pLastGroup
= pNewLast
;
1648 m_pConfig
->LineListRemove(pLine
);
1652 wxLogTrace( _T("wxFileConfig"),
1653 _T(" No line entry for Group '%s'?"),
1654 pGroup
->Name().c_str() );
1659 m_aSubgroups
.Remove(pGroup
);
1665 bool wxFileConfigGroup::DeleteEntry(const wxChar
*szName
)
1667 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1668 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1670 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1671 if ( pLine
!= NULL
) {
1672 // notice that we may do this test inside the previous "if" because the
1673 // last entry's line is surely !NULL
1674 if ( pEntry
== m_pLastEntry
) {
1675 // our last entry is being deleted - find the last one which stays
1676 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1678 // go back until we find another entry or reach the group's line
1679 wxFileConfigEntry
*pNewLast
= NULL
;
1680 size_t n
, nEntries
= m_aEntries
.Count();
1681 wxFileConfigLineList
*pl
;
1682 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1683 // is it our subgroup?
1684 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1685 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1686 pNewLast
= m_aEntries
[n
];
1689 if ( pNewLast
!= NULL
) // found?
1693 if ( pl
== m_pLine
) {
1694 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1696 // we've reached the group line without finding any subgroups
1697 m_pLastEntry
= NULL
;
1700 m_pLastEntry
= pNewLast
;
1703 m_pConfig
->LineListRemove(pLine
);
1706 // we must be written back for the changes to be saved
1709 m_aEntries
.Remove(pEntry
);
1715 // ----------------------------------------------------------------------------
1717 // ----------------------------------------------------------------------------
1718 void wxFileConfigGroup::SetDirty()
1721 if ( Parent() != NULL
) // propagate upwards
1722 Parent()->SetDirty();
1725 // ============================================================================
1726 // wxFileConfig::wxFileConfigEntry
1727 // ============================================================================
1729 // ----------------------------------------------------------------------------
1731 // ----------------------------------------------------------------------------
1732 wxFileConfigEntry::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1733 const wxString
& strName
,
1735 : m_strName(strName
)
1737 wxASSERT( !strName
.IsEmpty() );
1739 m_pParent
= pParent
;
1744 m_bHasValue
= FALSE
;
1746 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1748 m_strName
.erase(0, 1); // remove first character
1751 // ----------------------------------------------------------------------------
1753 // ----------------------------------------------------------------------------
1755 void wxFileConfigEntry::SetLine(wxFileConfigLineList
*pLine
)
1757 if ( m_pLine
!= NULL
) {
1758 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1759 Name().c_str(), m_pParent
->GetFullName().c_str());
1763 Group()->SetLastEntry(this);
1766 // second parameter is FALSE if we read the value from file and prevents the
1767 // entry from being marked as 'dirty'
1768 void wxFileConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1770 if ( bUser
&& IsImmutable() )
1772 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1777 // do nothing if it's the same value: but don't test for it
1778 // if m_bHasValue hadn't been set yet or we'd never write
1779 // empty values to the file
1781 if ( m_bHasValue
&& strValue
== m_strValue
)
1785 m_strValue
= strValue
;
1789 wxString strValFiltered
;
1791 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
)
1793 strValFiltered
= strValue
;
1796 strValFiltered
= FilterOutValue(strValue
);
1800 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strValFiltered
;
1804 // entry was read from the local config file, just modify the line
1805 m_pLine
->SetText(strLine
);
1808 // add a new line to the file
1809 wxASSERT( m_nLine
== wxNOT_FOUND
); // consistency check
1811 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1812 Group()->GetLastEntryLine());
1813 Group()->SetLastEntry(this);
1820 void wxFileConfigEntry::SetDirty()
1823 Group()->SetDirty();
1826 // ============================================================================
1828 // ============================================================================
1830 // ----------------------------------------------------------------------------
1831 // compare functions for array sorting
1832 // ----------------------------------------------------------------------------
1834 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1836 #if wxCONFIG_CASE_SENSITIVE
1837 return wxStrcmp(p1
->Name(), p2
->Name());
1839 return wxStricmp(p1
->Name(), p2
->Name());
1843 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1845 #if wxCONFIG_CASE_SENSITIVE
1846 return wxStrcmp(p1
->Name(), p2
->Name());
1848 return wxStricmp(p1
->Name(), p2
->Name());
1852 // ----------------------------------------------------------------------------
1854 // ----------------------------------------------------------------------------
1856 // undo FilterOutValue
1857 static wxString
FilterInValue(const wxString
& str
)
1860 strResult
.Alloc(str
.Len());
1862 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1864 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1865 if ( str
[n
] == wxT('\\') ) {
1866 switch ( str
[++n
] ) {
1868 strResult
+= wxT('\n');
1872 strResult
+= wxT('\r');
1876 strResult
+= wxT('\t');
1880 strResult
+= wxT('\\');
1884 strResult
+= wxT('"');
1889 if ( str
[n
] != wxT('"') || !bQuoted
)
1890 strResult
+= str
[n
];
1891 else if ( n
!= str
.Len() - 1 ) {
1892 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1895 //else: it's the last quote of a quoted string, ok
1902 // quote the string before writing it to file
1903 static wxString
FilterOutValue(const wxString
& str
)
1909 strResult
.Alloc(str
.Len());
1911 // quoting is necessary to preserve spaces in the beginning of the string
1912 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1915 strResult
+= wxT('"');
1918 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1941 //else: fall through
1944 strResult
+= str
[n
];
1945 continue; // nothing special to do
1948 // we get here only for special characters
1949 strResult
<< wxT('\\') << c
;
1953 strResult
+= wxT('"');
1958 // undo FilterOutEntryName
1959 static wxString
FilterInEntryName(const wxString
& str
)
1962 strResult
.Alloc(str
.Len());
1964 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
1965 if ( *pc
== wxT('\\') )
1974 // sanitize entry or group name: insert '\\' before any special characters
1975 static wxString
FilterOutEntryName(const wxString
& str
)
1978 strResult
.Alloc(str
.Len());
1980 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
1983 // we explicitly allow some of "safe" chars and 8bit ASCII characters
1984 // which will probably never have special meaning
1985 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
1986 // should *not* be quoted
1987 if ( !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) && ((c
& 0x80) == 0) )
1988 strResult
+= wxT('\\');
1996 // we can't put ?: in the ctor initializer list because it confuses some
1997 // broken compilers (Borland C++)
1998 static wxString
GetAppName(const wxString
& appName
)
2000 if ( !appName
&& wxTheApp
)
2001 return wxTheApp
->GetAppName();
2006 #endif // wxUSE_CONFIG