1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxFileConfig derivation of wxConfig
4 // Author: Vadim Zeitlin
6 // Created: 07.04.98 (adapted from appconf.cpp)
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "fileconf.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #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
142 // ----------------------------------------------------------------------------
143 // wxFileConfigEntry: a name/value pair
144 // ----------------------------------------------------------------------------
146 class wxFileConfigEntry
149 wxFileConfigGroup
*m_pParent
; // group that contains us
151 wxString m_strName
, // entry name
153 bool m_bDirty
:1, // changed since last read?
154 m_bImmutable
:1, // can be overriden locally?
155 m_bHasValue
:1; // set after first call to SetValue()
157 int m_nLine
; // used if m_pLine == NULL only
159 // pointer to our line in the linked list or NULL if it was found in global
160 // file (which we don't modify)
161 wxFileConfigLineList
*m_pLine
;
164 wxFileConfigEntry(wxFileConfigGroup
*pParent
,
165 const wxString
& strName
, int nLine
);
168 const wxString
& Name() const { return m_strName
; }
169 const wxString
& Value() const { return m_strValue
; }
170 wxFileConfigGroup
*Group() const { return m_pParent
; }
171 bool IsDirty() const { return m_bDirty
; }
172 bool IsImmutable() const { return m_bImmutable
; }
173 bool IsLocal() const { return m_pLine
!= 0; }
174 int Line() const { return m_nLine
; }
175 wxFileConfigLineList
*
176 GetLine() const { return m_pLine
; }
178 // modify entry attributes
179 void SetValue(const wxString
& strValue
, bool bUser
= TRUE
);
181 void SetLine(wxFileConfigLineList
*pLine
);
184 // ----------------------------------------------------------------------------
185 // wxFileConfigGroup: container of entries and other groups
186 // ----------------------------------------------------------------------------
188 class wxFileConfigGroup
191 wxFileConfig
*m_pConfig
; // config object we belong to
192 wxFileConfigGroup
*m_pParent
; // parent group (NULL for root group)
193 ArrayEntries m_aEntries
; // entries in this group
194 ArrayGroups m_aSubgroups
; // subgroups
195 wxString m_strName
; // group's name
196 bool m_bDirty
; // if FALSE => all subgroups are not dirty
197 wxFileConfigLineList
*m_pLine
; // pointer to our line in the linked list
198 wxFileConfigEntry
*m_pLastEntry
; // last entry/subgroup of this group in the
199 wxFileConfigGroup
*m_pLastGroup
; // local file (we insert new ones after it)
201 // DeleteSubgroupByName helper
202 bool DeleteSubgroup(wxFileConfigGroup
*pGroup
);
206 wxFileConfigGroup(wxFileConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
208 // dtor deletes all entries and subgroups also
209 ~wxFileConfigGroup();
212 const wxString
& Name() const { return m_strName
; }
213 wxFileConfigGroup
*Parent() const { return m_pParent
; }
214 wxFileConfig
*Config() const { return m_pConfig
; }
215 bool IsDirty() const { return m_bDirty
; }
217 const ArrayEntries
& Entries() const { return m_aEntries
; }
218 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
219 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
221 // find entry/subgroup (NULL if not found)
222 wxFileConfigGroup
*FindSubgroup(const wxChar
*szName
) const;
223 wxFileConfigEntry
*FindEntry (const wxChar
*szName
) const;
225 // delete entry/subgroup, return FALSE if doesn't exist
226 bool DeleteSubgroupByName(const wxChar
*szName
);
227 bool DeleteEntry(const wxChar
*szName
);
229 // create new entry/subgroup returning pointer to newly created element
230 wxFileConfigGroup
*AddSubgroup(const wxString
& strName
);
231 wxFileConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= wxNOT_FOUND
);
233 // will also recursively set parent's dirty flag
235 void SetLine(wxFileConfigLineList
*pLine
);
237 // rename: no checks are done to ensure that the name is unique!
238 void Rename(const wxString
& newName
);
241 wxString
GetFullName() const;
243 // get the last line belonging to an entry/subgroup of this group
244 wxFileConfigLineList
*GetGroupLine(); // line which contains [group]
245 wxFileConfigLineList
*GetLastEntryLine(); // after which our subgroups start
246 wxFileConfigLineList
*GetLastGroupLine(); // after which the next group starts
248 // called by entries/subgroups when they're created/deleted
249 void SetLastEntry(wxFileConfigEntry
*pEntry
) { m_pLastEntry
= pEntry
; }
250 void SetLastGroup(wxFileConfigGroup
*pGroup
) { m_pLastGroup
= pGroup
; }
253 // ============================================================================
255 // ============================================================================
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
260 wxString wxFileConfig
::GetGlobalDir()
264 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
265 strDir
= wxT("sys$manager:");
266 #elif defined(__WXMAC__)
267 strDir
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
268 #elif defined( __UNIX__ )
269 strDir
= wxT("/etc/");
270 #elif defined(__WXPM__)
271 ULONG aulSysInfo
[QSV_MAX
] = {0};
275 rc
= DosQuerySysInfo( 1L, QSV_MAX
, (PVOID
)aulSysInfo
, sizeof(ULONG
)*QSV_MAX
);
278 drive
= aulSysInfo
[QSV_BOOT_DRIVE
- 1];
279 strDir
.Printf(wxT("%c:\\OS2\\"), 'A'+drive
-1);
281 #elif defined(__WXSTUBS__)
282 wxASSERT_MSG( FALSE
, wxT("TODO") ) ;
283 #elif defined(__DOS__)
284 // There's no such thing as global cfg dir in MS-DOS, let's return
285 // current directory (FIXME_MGL?)
288 wxChar szWinDir
[MAX_PATH
];
289 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
293 #endif // Unix/Windows
298 wxString wxFileConfig
::GetLocalDir()
302 #if defined(__WXMAC__) || defined(__DOS__)
303 // no local dir concept on Mac OS 9 or MS-DOS
304 return GetGlobalDir() ;
306 wxGetHomeDir(&strDir
);
310 if (strDir
.Last() != wxT(']'))
312 if (strDir
.Last() != wxT('/')) strDir
<< wxT('/');
314 if (strDir
.Last() != wxT('\\')) strDir
<< wxT('\\');
321 wxString wxFileConfig
::GetGlobalFileName(const wxChar
*szFile
)
323 wxString str
= GetGlobalDir();
326 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
327 #if defined( __WXMAC__ )
328 str
<< " Preferences";
329 #elif defined( __UNIX__ )
338 wxString wxFileConfig
::GetLocalFileName(const wxChar
*szFile
)
340 #ifdef __VMS__ // On VMS I saw the problem that the home directory was appended
341 // twice for the configuration file. Does that also happen for other
343 wxString str
= wxT( '.' );
345 wxString str
= GetLocalDir();
348 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
354 #if defined(__WINDOWS__) || defined(__DOS__)
355 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
360 str
<< " Preferences";
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 void wxFileConfig
::Init()
372 m_pRootGroup
= new wxFileConfigGroup(NULL
, "", this);
377 // it's not an error if (one of the) file(s) doesn't exist
379 // parse the global file
380 if ( !m_strGlobalFile
.IsEmpty() && wxFile
::Exists(m_strGlobalFile
) ) {
381 wxTextFile
fileGlobal(m_strGlobalFile
);
383 if ( fileGlobal
.Open() ) {
384 Parse(fileGlobal
, FALSE
/* global */);
388 wxLogWarning(_("can't open global configuration file '%s'."),
389 m_strGlobalFile
.c_str());
392 // parse the local file
393 if ( !m_strLocalFile
.IsEmpty() && wxFile
::Exists(m_strLocalFile
) ) {
394 wxTextFile
fileLocal(m_strLocalFile
);
395 if ( fileLocal
.Open() ) {
396 Parse(fileLocal
, TRUE
/* local */);
400 wxLogWarning(_("can't open user configuration file '%s'."),
401 m_strLocalFile
.c_str());
405 // constructor supports creation of wxFileConfig objects of any type
406 wxFileConfig
::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
407 const wxString
& strLocal
, const wxString
& strGlobal
,
409 : wxConfigBase(::GetAppName(appName
), vendorName
,
412 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
)
414 // Make up names for files if empty
415 if ( m_strLocalFile
.IsEmpty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
417 m_strLocalFile
= GetLocalFileName(GetAppName());
420 if ( m_strGlobalFile
.IsEmpty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
422 m_strGlobalFile
= GetGlobalFileName(GetAppName());
425 // Check if styles are not supplied, but filenames are, in which case
426 // add the correct styles.
427 if ( !m_strLocalFile
.IsEmpty() )
428 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
430 if ( !m_strGlobalFile
.IsEmpty() )
431 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
433 // if the path is not absolute, prepend the standard directory to it
434 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
435 if ( !(style
& wxCONFIG_USE_RELATIVE_PATH
) )
437 if ( !m_strLocalFile
.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile
) )
439 wxString strLocal
= m_strLocalFile
;
440 m_strLocalFile
= GetLocalDir();
441 m_strLocalFile
<< strLocal
;
444 if ( !m_strGlobalFile
.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile
) )
446 wxString strGlobal
= m_strGlobalFile
;
447 m_strGlobalFile
= GetGlobalDir();
448 m_strGlobalFile
<< strGlobal
;
459 wxFileConfig
::wxFileConfig(wxInputStream
&inStream
)
461 // always local_file when this constructor is called (?)
462 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
465 m_pRootGroup
= new wxFileConfigGroup(NULL
, "", this);
470 // translate everything to the current (platform-dependent) line
471 // termination character
477 while ( !inStream
.Read(buf
, WXSIZEOF(buf
)).Eof() )
478 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
480 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
482 strTrans
= wxTextBuffer
::Translate(strTmp
);
485 wxMemoryText memText
;
487 // Now we can add the text to the memory text. To do this we extract line
488 // by line from the translated string, until we've reached the end.
490 // VZ: all this is horribly inefficient, we should do the translation on
491 // the fly in one pass saving both memory and time (TODO)
493 const wxChar
*pEOL
= wxTextBuffer
::GetEOL(wxTextBuffer
::typeDefault
);
494 const size_t EOLLen
= wxStrlen(pEOL
);
496 int posLineStart
= strTrans
.Find(pEOL
);
497 while ( posLineStart
!= -1 )
499 wxString
line(strTrans
.Left(posLineStart
));
501 memText
.AddLine(line
);
503 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
505 posLineStart
= strTrans
.Find(pEOL
);
508 // also add whatever we have left in the translated string.
509 memText
.AddLine(strTrans
);
511 // Finally we can parse it all.
512 Parse(memText
, TRUE
/* local */);
517 #endif // wxUSE_STREAMS
519 void wxFileConfig
::CleanUp()
523 wxFileConfigLineList
*pCur
= m_linesHead
;
524 while ( pCur
!= NULL
) {
525 wxFileConfigLineList
*pNext
= pCur
->Next();
531 wxFileConfig
::~wxFileConfig()
538 // ----------------------------------------------------------------------------
539 // parse a config file
540 // ----------------------------------------------------------------------------
542 void wxFileConfig
::Parse(wxTextBuffer
& buffer
, bool bLocal
)
544 const wxChar
*pStart
;
548 size_t nLineCount
= buffer
.GetLineCount();
549 for ( size_t n
= 0; n
< nLineCount
; n
++ ) {
552 // add the line to linked list
554 LineListAppend(strLine
);
556 // skip leading spaces
557 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
560 // skip blank/comment lines
561 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
564 if ( *pStart
== wxT('[') ) { // a new group
567 while ( *++pEnd
!= wxT(']') ) {
568 if ( *pEnd
== wxT('\\') ) {
569 // the next char is escaped, so skip it even if it is ']'
573 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
574 // we reached the end of line, break out of the loop
579 if ( *pEnd
!= wxT(']') ) {
580 wxLogError(_("file '%s': unexpected character %c at line %d."),
581 buffer
.GetName(), *pEnd
, n
+ 1);
582 continue; // skip this line
585 // group name here is always considered as abs path
588 strGroup
<< wxCONFIG_PATH_SEPARATOR
589 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
591 // will create it if doesn't yet exist
595 m_pCurrentGroup
->SetLine(m_linesTail
);
597 // check that there is nothing except comments left on this line
599 while ( *++pEnd
!= wxT('\0') && bCont
) {
608 // ignore whitespace ('\n' impossible here)
612 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
613 buffer
.GetName(), n
+ 1, pEnd
);
619 const wxChar
*pEnd
= pStart
;
620 while ( *pEnd
&& *pEnd
!= wxT('=') && !wxIsspace(*pEnd
) ) {
621 if ( *pEnd
== wxT('\\') ) {
622 // next character may be space or not - still take it because it's
623 // quoted (unless there is nothing)
626 // the error message will be given below anyhow
634 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
)));
637 while ( wxIsspace(*pEnd
) )
640 if ( *pEnd
++ != wxT('=') ) {
641 wxLogError(_("file '%s', line %d: '=' expected."),
642 buffer
.GetName(), n
+ 1);
645 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
647 if ( pEntry
== NULL
) {
649 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
652 pEntry
->SetLine(m_linesTail
);
655 if ( bLocal
&& pEntry
->IsImmutable() ) {
656 // immutable keys can't be changed by user
657 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
658 buffer
.GetName(), n
+ 1, strKey
.c_str());
661 // the condition below catches the cases (a) and (b) but not (c):
662 // (a) global key found second time in global file
663 // (b) key found second (or more) time in local file
664 // (c) key from global file now found in local one
665 // which is exactly what we want.
666 else if ( !bLocal
|| pEntry
->IsLocal() ) {
667 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
668 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
671 pEntry
->SetLine(m_linesTail
);
676 while ( wxIsspace(*pEnd
) )
679 pEntry
->SetValue(FilterInValue(pEnd
), FALSE
/* read from file */);
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
689 void wxFileConfig
::SetRootPath()
692 m_pCurrentGroup
= m_pRootGroup
;
695 void wxFileConfig
::SetPath(const wxString
& strPath
)
697 wxArrayString aParts
;
699 if ( strPath
.IsEmpty() ) {
704 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
706 wxSplitPath(aParts
, strPath
);
709 // relative path, combine with current one
710 wxString strFullPath
= m_strPath
;
711 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
712 wxSplitPath(aParts
, strFullPath
);
715 // change current group
717 m_pCurrentGroup
= m_pRootGroup
;
718 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
719 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
720 if ( pNextGroup
== NULL
)
721 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
722 m_pCurrentGroup
= pNextGroup
;
725 // recombine path parts in one variable
727 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
728 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
732 // ----------------------------------------------------------------------------
734 // ----------------------------------------------------------------------------
736 bool wxFileConfig
::GetFirstGroup(wxString
& str
, long& lIndex
) const
739 return GetNextGroup(str
, lIndex
);
742 bool wxFileConfig
::GetNextGroup (wxString
& str
, long& lIndex
) const
744 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
745 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
752 bool wxFileConfig
::GetFirstEntry(wxString
& str
, long& lIndex
) const
755 return GetNextEntry(str
, lIndex
);
758 bool wxFileConfig
::GetNextEntry (wxString
& str
, long& lIndex
) const
760 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
761 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
768 size_t wxFileConfig
::GetNumberOfEntries(bool bRecursive
) const
770 size_t n
= m_pCurrentGroup
->Entries().Count();
772 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
773 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
774 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
775 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
776 n
+= GetNumberOfEntries(TRUE
);
777 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
784 size_t wxFileConfig
::GetNumberOfGroups(bool bRecursive
) const
786 size_t n
= m_pCurrentGroup
->Groups().Count();
788 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
789 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
790 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
791 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
792 n
+= GetNumberOfGroups(TRUE
);
793 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
800 // ----------------------------------------------------------------------------
801 // tests for existence
802 // ----------------------------------------------------------------------------
804 bool wxFileConfig
::HasGroup(const wxString
& strName
) const
806 wxConfigPathChanger
path(this, strName
);
808 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
809 return pGroup
!= NULL
;
812 bool wxFileConfig
::HasEntry(const wxString
& strName
) const
814 wxConfigPathChanger
path(this, strName
);
816 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
817 return pEntry
!= NULL
;
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 bool wxFileConfig
::DoReadString(const wxString
& key
, wxString
* pStr
) const
826 wxConfigPathChanger
path(this, key
);
828 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
829 if (pEntry
== NULL
) {
833 *pStr
= pEntry
->Value();
838 bool wxFileConfig
::DoReadLong(const wxString
& key
, long *pl
) const
841 if ( !Read(key
, & str
) )
845 return str
.ToLong(pl
) ;
848 bool wxFileConfig
::DoWriteString(const wxString
& key
, const wxString
& szValue
)
850 wxConfigPathChanger
path(this, key
);
852 wxString strName
= path
.Name();
853 if ( strName
.IsEmpty() ) {
854 // setting the value of a group is an error
855 wxASSERT_MSG( wxIsEmpty(szValue
), wxT("can't set value of a group!") );
857 // ... except if it's empty in which case it's a way to force it's creation
858 m_pCurrentGroup
->SetDirty();
860 // this will add a line for this group if it didn't have it before
861 (void)m_pCurrentGroup
->GetGroupLine();
866 // check that the name is reasonable
867 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
) {
868 wxLogError(_("Config entry name cannot start with '%c'."),
869 wxCONFIG_IMMUTABLE_PREFIX
);
873 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
874 if ( pEntry
== NULL
)
875 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
877 pEntry
->SetValue(szValue
);
883 bool wxFileConfig
::DoWriteLong(const wxString
& key
, long lValue
)
885 return Write(key
, wxString
::Format(_T("%ld"), lValue
));
888 bool wxFileConfig
::Flush(bool /* bCurrentOnly */)
890 if ( LineListIsEmpty() || !m_pRootGroup
->IsDirty() || !m_strLocalFile
)
894 // set the umask if needed
898 umaskOld
= umask((mode_t
)m_umask
);
902 wxTempFile
file(m_strLocalFile
);
904 if ( !file
.IsOpened() )
906 wxLogError(_("can't open user configuration file."));
910 // write all strings to file
911 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
913 wxString line
= p
->Text();
914 line
+= wxTextFile
::GetEOL();
916 wxCharBuffer buf
= wxConvLocal
.cWX2MB( line
);
917 if ( !file
.Write( (const char*)buf
, strlen( (const char*) buf
) ) )
919 if ( !file
.Write( line
.c_str(), line
.Len() ) )
922 wxLogError(_("can't write user configuration file."));
927 bool ret
= file
.Commit();
929 #if defined(__WXMAC__)
934 wxMacFilename2FSSpec( m_strLocalFile
, &spec
) ;
936 if ( FSpGetFInfo( &spec
, &finfo
) == noErr
)
938 finfo
.fdType
= 'TEXT' ;
939 finfo
.fdCreator
= 'ttxt' ;
940 FSpSetFInfo( &spec
, &finfo
) ;
946 // restore the old umask if we changed it
949 (void)umask(umaskOld
);
956 // ----------------------------------------------------------------------------
957 // renaming groups/entries
958 // ----------------------------------------------------------------------------
960 bool wxFileConfig
::RenameEntry(const wxString
& oldName
,
961 const wxString
& newName
)
963 // check that the entry exists
964 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
968 // check that the new entry doesn't already exist
969 if ( m_pCurrentGroup
->FindEntry(newName
) )
972 // delete the old entry, create the new one
973 wxString value
= oldEntry
->Value();
974 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
977 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
978 newEntry
->SetValue(value
);
983 bool wxFileConfig
::RenameGroup(const wxString
& oldName
,
984 const wxString
& newName
)
986 // check that the group exists
987 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
991 // check that the new group doesn't already exist
992 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
995 group
->Rename(newName
);
1000 // ----------------------------------------------------------------------------
1001 // delete groups/entries
1002 // ----------------------------------------------------------------------------
1004 bool wxFileConfig
::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1006 wxConfigPathChanger
path(this, key
);
1008 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1011 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1012 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1013 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1014 SetPath(wxT("..")); // changes m_pCurrentGroup!
1015 m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name());
1017 //else: never delete the root group
1023 bool wxFileConfig
::DeleteGroup(const wxString
& key
)
1025 wxConfigPathChanger
path(this, key
);
1027 return m_pCurrentGroup
->DeleteSubgroupByName(path
.Name());
1030 bool wxFileConfig
::DeleteAll()
1034 if ( wxRemove(m_strLocalFile
) == -1 )
1035 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile
.c_str());
1037 m_strLocalFile
= m_strGlobalFile
= wxT("");
1043 // ----------------------------------------------------------------------------
1044 // linked list functions
1045 // ----------------------------------------------------------------------------
1047 // append a new line to the end of the list
1048 wxFileConfigLineList
*wxFileConfig
::LineListAppend(const wxString
& str
)
1050 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1052 if ( m_linesTail
== NULL
) {
1054 m_linesHead
= pLine
;
1058 m_linesTail
->SetNext(pLine
);
1059 pLine
->SetPrev(m_linesTail
);
1062 m_linesTail
= pLine
;
1066 // insert a new line after the given one or in the very beginning if !pLine
1067 wxFileConfigLineList
*wxFileConfig
::LineListInsert(const wxString
& str
,
1068 wxFileConfigLineList
*pLine
)
1070 if ( pLine
== m_linesTail
)
1071 return LineListAppend(str
);
1073 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1074 if ( pLine
== NULL
) {
1075 // prepend to the list
1076 pNewLine
->SetNext(m_linesHead
);
1077 m_linesHead
->SetPrev(pNewLine
);
1078 m_linesHead
= pNewLine
;
1081 // insert before pLine
1082 wxFileConfigLineList
*pNext
= pLine
->Next();
1083 pNewLine
->SetNext(pNext
);
1084 pNewLine
->SetPrev(pLine
);
1085 pNext
->SetPrev(pNewLine
);
1086 pLine
->SetNext(pNewLine
);
1092 void wxFileConfig
::LineListRemove(wxFileConfigLineList
*pLine
)
1094 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1095 *pNext
= pLine
->Next();
1098 if ( pPrev
== NULL
)
1099 m_linesHead
= pNext
;
1101 pPrev
->SetNext(pNext
);
1104 if ( pNext
== NULL
)
1105 m_linesTail
= pPrev
;
1107 pNext
->SetPrev(pPrev
);
1112 bool wxFileConfig
::LineListIsEmpty()
1114 return m_linesHead
== NULL
;
1117 // ============================================================================
1118 // wxFileConfig::wxFileConfigGroup
1119 // ============================================================================
1121 // ----------------------------------------------------------------------------
1123 // ----------------------------------------------------------------------------
1126 wxFileConfigGroup
::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1127 const wxString
& strName
,
1128 wxFileConfig
*pConfig
)
1129 : m_aEntries(CompareEntries
),
1130 m_aSubgroups(CompareGroups
),
1133 m_pConfig
= pConfig
;
1134 m_pParent
= pParent
;
1138 m_pLastEntry
= NULL
;
1139 m_pLastGroup
= NULL
;
1142 // dtor deletes all children
1143 wxFileConfigGroup
::~wxFileConfigGroup()
1146 size_t n
, nCount
= m_aEntries
.Count();
1147 for ( n
= 0; n
< nCount
; n
++ )
1148 delete m_aEntries
[n
];
1151 nCount
= m_aSubgroups
.Count();
1152 for ( n
= 0; n
< nCount
; n
++ )
1153 delete m_aSubgroups
[n
];
1156 // ----------------------------------------------------------------------------
1158 // ----------------------------------------------------------------------------
1160 void wxFileConfigGroup
::SetLine(wxFileConfigLineList
*pLine
)
1162 wxASSERT( m_pLine
== NULL
); // shouldn't be called twice
1168 This is a bit complicated, so let me explain it in details. All lines that
1169 were read from the local file (the only one we will ever modify) are stored
1170 in a (doubly) linked list. Our problem is to know at which position in this
1171 list should we insert the new entries/subgroups. To solve it we keep three
1172 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1174 m_pLine points to the line containing "[group_name]"
1175 m_pLastEntry points to the last entry of this group in the local file.
1176 m_pLastGroup subgroup
1178 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1179 from the local file, the corresponding variable is set. However, if the group
1180 was read from the global file and then modified or created by the application
1181 these variables are still NULL and we need to create the corresponding lines.
1182 See the following functions (and comments preceding them) for the details of
1185 Also, when our last entry/group are deleted we need to find the new last
1186 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1187 of lines until it either founds an entry/subgroup (and this is the new last
1188 element) or the m_pLine of the group, in which case there are no more entries
1189 (or subgroups) left and m_pLast<element> becomes NULL.
1191 NB: This last problem could be avoided for entries if we added new entries
1192 immediately after m_pLine, but in this case the entries would appear
1193 backwards in the config file (OTOH, it's not that important) and as we
1194 would still need to do it for the subgroups the code wouldn't have been
1195 significantly less complicated.
1198 // Return the line which contains "[our name]". If we're still not in the list,
1199 // add our line to it immediately after the last line of our parent group if we
1200 // have it or in the very beginning if we're the root group.
1201 wxFileConfigLineList
*wxFileConfigGroup
::GetGroupLine()
1203 if ( m_pLine
== NULL
) {
1204 wxFileConfigGroup
*pParent
= Parent();
1206 // this group wasn't present in local config file, add it now
1207 if ( pParent
!= NULL
) {
1208 wxString strFullName
;
1209 strFullName
<< wxT("[")
1211 << FilterOutEntryName(GetFullName().c_str() + 1)
1213 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1214 pParent
->GetLastGroupLine());
1215 pParent
->SetLastGroup(this); // we're surely after all the others
1218 // we return NULL, so that LineListInsert() will insert us in the
1226 // Return the last line belonging to the subgroups of this group (after which
1227 // we can add a new subgroup), if we don't have any subgroups or entries our
1228 // last line is the group line (m_pLine) itself.
1229 wxFileConfigLineList
*wxFileConfigGroup
::GetLastGroupLine()
1231 // if we have any subgroups, our last line is the last line of the last
1233 if ( m_pLastGroup
!= NULL
) {
1234 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1236 wxASSERT( pLine
!= NULL
); // last group must have !NULL associated line
1240 // no subgroups, so the last line is the line of thelast entry (if any)
1241 return GetLastEntryLine();
1244 // return the last line belonging to the entries of this group (after which
1245 // we can add a new entry), if we don't have any entries we will add the new
1246 // one immediately after the group line itself.
1247 wxFileConfigLineList
*wxFileConfigGroup
::GetLastEntryLine()
1249 if ( m_pLastEntry
!= NULL
) {
1250 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1252 wxASSERT( pLine
!= NULL
); // last entry must have !NULL associated line
1256 // no entries: insert after the group header
1257 return GetGroupLine();
1260 // ----------------------------------------------------------------------------
1262 // ----------------------------------------------------------------------------
1264 void wxFileConfigGroup
::Rename(const wxString
& newName
)
1266 m_strName
= newName
;
1268 wxFileConfigLineList
*line
= GetGroupLine();
1269 wxString strFullName
;
1270 strFullName
<< wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); // +1: no '/'
1271 line
->SetText(strFullName
);
1276 wxString wxFileConfigGroup
::GetFullName() const
1279 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1284 // ----------------------------------------------------------------------------
1286 // ----------------------------------------------------------------------------
1288 // use binary search because the array is sorted
1290 wxFileConfigGroup
::FindEntry(const wxChar
*szName
) const
1294 hi
= m_aEntries
.Count();
1296 wxFileConfigEntry
*pEntry
;
1300 pEntry
= m_aEntries
[i
];
1302 #if wxCONFIG_CASE_SENSITIVE
1303 res
= wxStrcmp(pEntry
->Name(), szName
);
1305 res
= wxStricmp(pEntry
->Name(), szName
);
1320 wxFileConfigGroup
::FindSubgroup(const wxChar
*szName
) const
1324 hi
= m_aSubgroups
.Count();
1326 wxFileConfigGroup
*pGroup
;
1330 pGroup
= m_aSubgroups
[i
];
1332 #if wxCONFIG_CASE_SENSITIVE
1333 res
= wxStrcmp(pGroup
->Name(), szName
);
1335 res
= wxStricmp(pGroup
->Name(), szName
);
1349 // ----------------------------------------------------------------------------
1350 // create a new item
1351 // ----------------------------------------------------------------------------
1353 // create a new entry and add it to the current group
1355 wxFileConfigGroup
::AddEntry(const wxString
& strName
, int nLine
)
1357 wxASSERT( FindEntry(strName
) == NULL
);
1359 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1360 m_aEntries
.Add(pEntry
);
1365 // create a new group and add it to the current group
1367 wxFileConfigGroup
::AddSubgroup(const wxString
& strName
)
1369 wxASSERT( FindSubgroup(strName
) == NULL
);
1371 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1372 m_aSubgroups
.Add(pGroup
);
1377 // ----------------------------------------------------------------------------
1379 // ----------------------------------------------------------------------------
1382 The delete operations are _very_ slow if we delete the last item of this
1383 group (see comments before GetXXXLineXXX functions for more details),
1384 so it's much better to start with the first entry/group if we want to
1385 delete several of them.
1388 bool wxFileConfigGroup
::DeleteSubgroupByName(const wxChar
*szName
)
1390 return DeleteSubgroup(FindSubgroup(szName
));
1393 // doesn't delete the subgroup itself, but does remove references to it from
1394 // all other data structures (and normally the returned pointer should be
1395 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1396 bool wxFileConfigGroup
::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1398 wxCHECK( pGroup
!= NULL
, FALSE
); // deleting non existing group?
1400 // delete all entries
1401 size_t nCount
= pGroup
->m_aEntries
.Count();
1402 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ ) {
1403 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1404 if ( pLine
!= NULL
)
1405 m_pConfig
->LineListRemove(pLine
);
1408 // and subgroups of this sungroup
1409 nCount
= pGroup
->m_aSubgroups
.Count();
1410 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ ) {
1411 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1414 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1415 if ( pLine
!= NULL
) {
1416 // notice that we may do this test inside the previous "if" because the
1417 // last entry's line is surely !NULL
1418 if ( pGroup
== m_pLastGroup
) {
1419 // our last entry is being deleted - find the last one which stays
1420 wxASSERT( m_pLine
!= NULL
); // we have a subgroup with !NULL pLine...
1422 // go back until we find a subgroup or reach the group's line
1423 wxFileConfigGroup
*pNewLast
= NULL
;
1424 size_t n
, nSubgroups
= m_aSubgroups
.Count();
1425 wxFileConfigLineList
*pl
;
1426 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1427 // is it our subgroup?
1428 for ( n
= 0; (pNewLast
== NULL
) && (n
< nSubgroups
); n
++ ) {
1429 // do _not_ call GetGroupLine! we don't want to add it to the local
1430 // file if it's not already there
1431 if ( m_aSubgroups
[n
]->m_pLine
== m_pLine
)
1432 pNewLast
= m_aSubgroups
[n
];
1435 if ( pNewLast
!= NULL
) // found?
1439 if ( pl
== m_pLine
) {
1440 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1442 // we've reached the group line without finding any subgroups
1443 m_pLastGroup
= NULL
;
1446 m_pLastGroup
= pNewLast
;
1449 m_pConfig
->LineListRemove(pLine
);
1454 m_aSubgroups
.Remove(pGroup
);
1460 bool wxFileConfigGroup
::DeleteEntry(const wxChar
*szName
)
1462 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1463 wxCHECK( pEntry
!= NULL
, FALSE
); // deleting non existing item?
1465 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1466 if ( pLine
!= NULL
) {
1467 // notice that we may do this test inside the previous "if" because the
1468 // last entry's line is surely !NULL
1469 if ( pEntry
== m_pLastEntry
) {
1470 // our last entry is being deleted - find the last one which stays
1471 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1473 // go back until we find another entry or reach the group's line
1474 wxFileConfigEntry
*pNewLast
= NULL
;
1475 size_t n
, nEntries
= m_aEntries
.Count();
1476 wxFileConfigLineList
*pl
;
1477 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1478 // is it our subgroup?
1479 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1480 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1481 pNewLast
= m_aEntries
[n
];
1484 if ( pNewLast
!= NULL
) // found?
1488 if ( pl
== m_pLine
) {
1489 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1491 // we've reached the group line without finding any subgroups
1492 m_pLastEntry
= NULL
;
1495 m_pLastEntry
= pNewLast
;
1498 m_pConfig
->LineListRemove(pLine
);
1501 // we must be written back for the changes to be saved
1504 m_aEntries
.Remove(pEntry
);
1510 // ----------------------------------------------------------------------------
1512 // ----------------------------------------------------------------------------
1513 void wxFileConfigGroup
::SetDirty()
1516 if ( Parent() != NULL
) // propagate upwards
1517 Parent()->SetDirty();
1520 // ============================================================================
1521 // wxFileConfig::wxFileConfigEntry
1522 // ============================================================================
1524 // ----------------------------------------------------------------------------
1526 // ----------------------------------------------------------------------------
1527 wxFileConfigEntry
::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1528 const wxString
& strName
,
1530 : m_strName(strName
)
1532 wxASSERT( !strName
.IsEmpty() );
1534 m_pParent
= pParent
;
1539 m_bHasValue
= FALSE
;
1541 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1543 m_strName
.erase(0, 1); // remove first character
1546 // ----------------------------------------------------------------------------
1548 // ----------------------------------------------------------------------------
1550 void wxFileConfigEntry
::SetLine(wxFileConfigLineList
*pLine
)
1552 if ( m_pLine
!= NULL
) {
1553 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1554 Name().c_str(), m_pParent
->GetFullName().c_str());
1558 Group()->SetLastEntry(this);
1561 // second parameter is FALSE if we read the value from file and prevents the
1562 // entry from being marked as 'dirty'
1563 void wxFileConfigEntry
::SetValue(const wxString
& strValue
, bool bUser
)
1565 if ( bUser
&& IsImmutable() ) {
1566 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1571 // do nothing if it's the same value: but don't test for it if m_bHasValue
1572 // hadn't been set yet or we'd never write empty values to the file
1573 if ( m_bHasValue
&& strValue
== m_strValue
)
1577 m_strValue
= strValue
;
1580 wxString strVal
= FilterOutValue(strValue
);
1582 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strVal
;
1584 if ( m_pLine
!= NULL
) {
1585 // entry was read from the local config file, just modify the line
1586 m_pLine
->SetText(strLine
);
1589 // add a new line to the file
1590 wxASSERT( m_nLine
== wxNOT_FOUND
); // consistency check
1592 m_pLine
= Group()->Config()->LineListInsert(strLine
,
1593 Group()->GetLastEntryLine());
1594 Group()->SetLastEntry(this);
1601 void wxFileConfigEntry
::SetDirty()
1604 Group()->SetDirty();
1607 // ============================================================================
1609 // ============================================================================
1611 // ----------------------------------------------------------------------------
1612 // compare functions for array sorting
1613 // ----------------------------------------------------------------------------
1615 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1617 #if wxCONFIG_CASE_SENSITIVE
1618 return wxStrcmp(p1
->Name(), p2
->Name());
1620 return wxStricmp(p1
->Name(), p2
->Name());
1624 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1626 #if wxCONFIG_CASE_SENSITIVE
1627 return wxStrcmp(p1
->Name(), p2
->Name());
1629 return wxStricmp(p1
->Name(), p2
->Name());
1633 // ----------------------------------------------------------------------------
1635 // ----------------------------------------------------------------------------
1637 // undo FilterOutValue
1638 static wxString
FilterInValue(const wxString
& str
)
1641 strResult
.Alloc(str
.Len());
1643 bool bQuoted
= !str
.IsEmpty() && str
[0] == '"';
1645 for ( size_t n
= bQuoted ?
1 : 0; n
< str
.Len(); n
++ ) {
1646 if ( str
[n
] == wxT('\\') ) {
1647 switch ( str
[++n
] ) {
1649 strResult
+= wxT('\n');
1653 strResult
+= wxT('\r');
1657 strResult
+= wxT('\t');
1661 strResult
+= wxT('\\');
1665 strResult
+= wxT('"');
1670 if ( str
[n
] != wxT('"') || !bQuoted
)
1671 strResult
+= str
[n
];
1672 else if ( n
!= str
.Len() - 1 ) {
1673 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1676 //else: it's the last quote of a quoted string, ok
1683 // quote the string before writing it to file
1684 static wxString
FilterOutValue(const wxString
& str
)
1690 strResult
.Alloc(str
.Len());
1692 // quoting is necessary to preserve spaces in the beginning of the string
1693 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1696 strResult
+= wxT('"');
1699 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1722 //else: fall through
1725 strResult
+= str
[n
];
1726 continue; // nothing special to do
1729 // we get here only for special characters
1730 strResult
<< wxT('\\') << c
;
1734 strResult
+= wxT('"');
1739 // undo FilterOutEntryName
1740 static wxString
FilterInEntryName(const wxString
& str
)
1743 strResult
.Alloc(str
.Len());
1745 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
1746 if ( *pc
== wxT('\\') )
1755 // sanitize entry or group name: insert '\\' before any special characters
1756 static wxString
FilterOutEntryName(const wxString
& str
)
1759 strResult
.Alloc(str
.Len());
1761 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
1764 // we explicitly allow some of "safe" chars and 8bit ASCII characters
1765 // which will probably never have special meaning
1766 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
1767 // should *not* be quoted
1768 if ( !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) && ((c
& 0x80) == 0) )
1769 strResult
+= wxT('\\');
1777 // we can't put ?: in the ctor initializer list because it confuses some
1778 // broken compilers (Borland C++)
1779 static wxString
GetAppName(const wxString
& appName
)
1781 if ( !appName
&& wxTheApp
)
1782 return wxTheApp
->GetAppName();
1787 #endif // wxUSE_CONFIG