1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxFileConfig derivation of wxConfig
4 // Author: Vadim Zeitlin
6 // Created: 07.04.98 (adapted from appconf.cpp)
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "fileconf.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
27 #if wxUSE_CONFIG && wxUSE_FILECONFIG
30 #include "wx/string.h"
35 #include "wx/dynarray.h"
38 #include "wx/textfile.h"
39 #include "wx/memtext.h"
40 #include "wx/config.h"
41 #include "wx/fileconf.h"
42 #include "wx/filefn.h"
45 #include "wx/stream.h"
46 #endif // wxUSE_STREAMS
48 #include "wx/utils.h" // for wxGetHomeDir
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
52 #include "wx/filename.h" // for MacSetTypeAndCreator
55 #if defined(__WXMSW__)
56 #include "wx/msw/private.h"
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
69 #define CONST_CAST ((wxFileConfig *)this)->
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
79 #define FILECONF_TRACE_MASK _T("fileconf")
81 // ----------------------------------------------------------------------------
82 // global functions declarations
83 // ----------------------------------------------------------------------------
85 // compare functions for sorting the arrays
86 static int LINKAGEMODE
CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
);
87 static int LINKAGEMODE
CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
);
90 static wxString
FilterInValue(const wxString
& str
);
91 static wxString
FilterOutValue(const wxString
& str
);
93 static wxString
FilterInEntryName(const wxString
& str
);
94 static wxString
FilterOutEntryName(const wxString
& str
);
96 // get the name to use in wxFileConfig ctor
97 static wxString
GetAppName(const wxString
& appname
);
99 // ============================================================================
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // "template" array types
105 // ----------------------------------------------------------------------------
107 #ifdef WXMAKINGDLL_BASE
108 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
,
110 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
,
113 WX_DEFINE_SORTED_ARRAY(wxFileConfigEntry
*, ArrayEntries
);
114 WX_DEFINE_SORTED_ARRAY(wxFileConfigGroup
*, ArrayGroups
);
117 // ----------------------------------------------------------------------------
118 // wxFileConfigLineList
119 // ----------------------------------------------------------------------------
121 // we store all lines of the local config file as a linked list in memory
122 class wxFileConfigLineList
125 void SetNext(wxFileConfigLineList
*pNext
) { m_pNext
= pNext
; }
126 void SetPrev(wxFileConfigLineList
*pPrev
) { m_pPrev
= pPrev
; }
129 wxFileConfigLineList(const wxString
& str
,
130 wxFileConfigLineList
*pNext
= NULL
) : m_strLine(str
)
131 { SetNext(pNext
); SetPrev(NULL
); }
133 // next/prev nodes in the linked list
134 wxFileConfigLineList
*Next() const { return m_pNext
; }
135 wxFileConfigLineList
*Prev() const { return m_pPrev
; }
137 // get/change lines text
138 void SetText(const wxString
& str
) { m_strLine
= str
; }
139 const wxString
& Text() const { return m_strLine
; }
142 wxString m_strLine
; // line contents
143 wxFileConfigLineList
*m_pNext
, // next node
144 *m_pPrev
; // previous one
146 DECLARE_NO_COPY_CLASS(wxFileConfigLineList
)
149 // ----------------------------------------------------------------------------
150 // wxFileConfigEntry: a name/value pair
151 // ----------------------------------------------------------------------------
153 class wxFileConfigEntry
156 wxFileConfigGroup
*m_pParent
; // group that contains us
158 wxString m_strName
, // entry name
160 bool m_bImmutable
:1, // can be overriden locally?
161 m_bHasValue
:1; // set after first call to SetValue()
163 int m_nLine
; // used if m_pLine == NULL only
165 // pointer to our line in the linked list or NULL if it was found in global
166 // file (which we don't modify)
167 wxFileConfigLineList
*m_pLine
;
170 wxFileConfigEntry(wxFileConfigGroup
*pParent
,
171 const wxString
& strName
, int nLine
);
174 const wxString
& Name() const { return m_strName
; }
175 const wxString
& Value() const { return m_strValue
; }
176 wxFileConfigGroup
*Group() const { return m_pParent
; }
177 bool IsImmutable() const { return m_bImmutable
; }
178 bool IsLocal() const { return m_pLine
!= 0; }
179 int Line() const { return m_nLine
; }
180 wxFileConfigLineList
*
181 GetLine() const { return m_pLine
; }
183 // modify entry attributes
184 void SetValue(const wxString
& strValue
, bool bUser
= true);
185 void SetLine(wxFileConfigLineList
*pLine
);
187 DECLARE_NO_COPY_CLASS(wxFileConfigEntry
)
190 // ----------------------------------------------------------------------------
191 // wxFileConfigGroup: container of entries and other groups
192 // ----------------------------------------------------------------------------
194 class wxFileConfigGroup
197 wxFileConfig
*m_pConfig
; // config object we belong to
198 wxFileConfigGroup
*m_pParent
; // parent group (NULL for root group)
199 ArrayEntries m_aEntries
; // entries in this group
200 ArrayGroups m_aSubgroups
; // subgroups
201 wxString m_strName
; // group's name
202 wxFileConfigLineList
*m_pLine
; // pointer to our line in the linked list
203 wxFileConfigEntry
*m_pLastEntry
; // last entry/subgroup of this group in the
204 wxFileConfigGroup
*m_pLastGroup
; // local file (we insert new ones after it)
206 // DeleteSubgroupByName helper
207 bool DeleteSubgroup(wxFileConfigGroup
*pGroup
);
210 void UpdateGroupAndSubgroupsLines();
214 wxFileConfigGroup(wxFileConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
216 // dtor deletes all entries and subgroups also
217 ~wxFileConfigGroup();
220 const wxString
& Name() const { return m_strName
; }
221 wxFileConfigGroup
*Parent() const { return m_pParent
; }
222 wxFileConfig
*Config() const { return m_pConfig
; }
224 const ArrayEntries
& Entries() const { return m_aEntries
; }
225 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
226 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
228 // find entry/subgroup (NULL if not found)
229 wxFileConfigGroup
*FindSubgroup(const wxChar
*szName
) const;
230 wxFileConfigEntry
*FindEntry (const wxChar
*szName
) const;
232 // delete entry/subgroup, return false if doesn't exist
233 bool DeleteSubgroupByName(const wxChar
*szName
);
234 bool DeleteEntry(const wxChar
*szName
);
236 // create new entry/subgroup returning pointer to newly created element
237 wxFileConfigGroup
*AddSubgroup(const wxString
& strName
);
238 wxFileConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= wxNOT_FOUND
);
240 void SetLine(wxFileConfigLineList
*pLine
);
242 // rename: no checks are done to ensure that the name is unique!
243 void Rename(const wxString
& newName
);
246 wxString
GetFullName() const;
248 // get the last line belonging to an entry/subgroup of this group
249 wxFileConfigLineList
*GetGroupLine(); // line which contains [group]
250 wxFileConfigLineList
*GetLastEntryLine(); // after which our subgroups start
251 wxFileConfigLineList
*GetLastGroupLine(); // after which the next group starts
253 // called by entries/subgroups when they're created/deleted
254 void SetLastEntry(wxFileConfigEntry
*pEntry
);
255 void SetLastGroup(wxFileConfigGroup
*pGroup
)
256 { m_pLastGroup
= pGroup
; }
258 DECLARE_NO_COPY_CLASS(wxFileConfigGroup
)
261 // ============================================================================
263 // ============================================================================
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
268 wxString
wxFileConfig::GetGlobalDir()
272 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
273 strDir
= wxT("sys$manager:");
274 #elif defined(__WXMAC__)
275 strDir
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
276 #elif defined( __UNIX__ )
277 strDir
= wxT("/etc/");
278 #elif defined(__WXPM__)
279 ULONG aulSysInfo
[QSV_MAX
] = {0};
283 rc
= DosQuerySysInfo( 1L, QSV_MAX
, (PVOID
)aulSysInfo
, sizeof(ULONG
)*QSV_MAX
);
286 drive
= aulSysInfo
[QSV_BOOT_DRIVE
- 1];
287 strDir
.Printf(wxT("%c:\\OS2\\"), 'A'+drive
-1);
289 #elif defined(__WXSTUBS__)
290 wxASSERT_MSG( false, wxT("TODO") ) ;
291 #elif defined(__DOS__)
292 // There's no such thing as global cfg dir in MS-DOS, let's return
293 // current directory (FIXME_MGL?)
295 #elif defined(__WXWINCE__)
296 strDir
= wxT("\\Windows\\");
299 wxChar szWinDir
[MAX_PATH
];
300 ::GetWindowsDirectory(szWinDir
, MAX_PATH
);
304 #endif // Unix/Windows
309 wxString
wxFileConfig::GetLocalDir()
313 #if defined(__WXMAC__) || defined(__DOS__)
314 // no local dir concept on Mac OS 9 or MS-DOS
315 return GetGlobalDir() ;
317 wxGetHomeDir(&strDir
);
321 if (strDir
.Last() != wxT(']'))
323 if (strDir
.Last() != wxT('/')) strDir
<< wxT('/');
325 if (strDir
.Last() != wxT('\\')) strDir
<< wxT('\\');
332 wxString
wxFileConfig::GetGlobalFileName(const wxChar
*szFile
)
334 wxString str
= GetGlobalDir();
337 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
338 #if defined( __WXMAC__ )
339 str
<< wxT(" Preferences") ;
340 #elif defined( __UNIX__ )
349 wxString
wxFileConfig::GetLocalFileName(const wxChar
*szFile
)
352 // On VMS I saw the problem that the home directory was appended
353 // twice for the configuration file. Does that also happen for
355 wxString str
= wxT( '.' );
357 wxString str
= GetLocalDir();
360 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
366 #if defined(__WINDOWS__) || defined(__DOS__)
367 if ( wxStrchr(szFile
, wxT('.')) == NULL
)
372 str
<< wxT(" Preferences") ;
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void wxFileConfig::Init()
385 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxEmptyString
, this);
390 // It's not an error if (one of the) file(s) doesn't exist.
392 // parse the global file
393 if ( !m_strGlobalFile
.empty() && wxFile::Exists(m_strGlobalFile
) )
395 wxTextFile
fileGlobal(m_strGlobalFile
);
397 if ( fileGlobal
.Open(m_conv
/*ignored in ANSI build*/) )
399 Parse(fileGlobal
, false /* global */);
404 wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile
.c_str());
408 // parse the local file
409 if ( !m_strLocalFile
.empty() && wxFile::Exists(m_strLocalFile
) )
411 wxTextFile
fileLocal(m_strLocalFile
);
412 if ( fileLocal
.Open(m_conv
/*ignored in ANSI build*/) )
414 Parse(fileLocal
, true /* local */);
419 wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile
.c_str() );
426 // constructor supports creation of wxFileConfig objects of any type
427 wxFileConfig::wxFileConfig(const wxString
& appName
, const wxString
& vendorName
,
428 const wxString
& strLocal
, const wxString
& strGlobal
,
429 long style
, wxMBConv
& conv
)
430 : wxConfigBase(::GetAppName(appName
), vendorName
,
433 m_strLocalFile(strLocal
), m_strGlobalFile(strGlobal
),
436 // Make up names for files if empty
437 if ( m_strLocalFile
.empty() && (style
& wxCONFIG_USE_LOCAL_FILE
) )
438 m_strLocalFile
= GetLocalFileName(GetAppName());
440 if ( m_strGlobalFile
.empty() && (style
& wxCONFIG_USE_GLOBAL_FILE
) )
441 m_strGlobalFile
= GetGlobalFileName(GetAppName());
443 // Check if styles are not supplied, but filenames are, in which case
444 // add the correct styles.
445 if ( !m_strLocalFile
.empty() )
446 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
448 if ( !m_strGlobalFile
.empty() )
449 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE
);
451 // if the path is not absolute, prepend the standard directory to it
452 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
453 if ( !(style
& wxCONFIG_USE_RELATIVE_PATH
) )
455 if ( !m_strLocalFile
.empty() && !wxIsAbsolutePath(m_strLocalFile
) )
457 wxString strLocal
= m_strLocalFile
;
458 m_strLocalFile
= GetLocalDir();
459 m_strLocalFile
<< strLocal
;
462 if ( !m_strGlobalFile
.empty() && !wxIsAbsolutePath(m_strGlobalFile
) )
464 wxString strGlobal
= m_strGlobalFile
;
465 m_strGlobalFile
= GetGlobalDir();
466 m_strGlobalFile
<< strGlobal
;
477 wxFileConfig::wxFileConfig(wxInputStream
&inStream
, wxMBConv
& conv
)
480 // always local_file when this constructor is called (?)
481 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE
);
484 m_pRootGroup
= new wxFileConfigGroup(NULL
, wxEmptyString
, this);
489 // translate everything to the current (platform-dependent) line
490 // termination character
498 inStream
.Read(buf
, WXSIZEOF(buf
));
500 const wxStreamError err
= inStream
.GetLastError();
502 if ( err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
)
504 wxLogError(_("Error reading config options."));
508 strTmp
.append(wxConvertMB2WX(buf
), inStream
.LastRead());
510 while ( !inStream
.Eof() );
512 strTrans
= wxTextBuffer::Translate(strTmp
);
515 wxMemoryText memText
;
517 // Now we can add the text to the memory text. To do this we extract line
518 // by line from the translated string, until we've reached the end.
520 // VZ: all this is horribly inefficient, we should do the translation on
521 // the fly in one pass saving both memory and time (TODO)
523 const wxChar
*pEOL
= wxTextBuffer::GetEOL(wxTextBuffer::typeDefault
);
524 const size_t EOLLen
= wxStrlen(pEOL
);
526 int posLineStart
= strTrans
.Find(pEOL
);
527 while ( posLineStart
!= -1 )
529 wxString
line(strTrans
.Left(posLineStart
));
531 memText
.AddLine(line
);
533 strTrans
= strTrans
.Mid(posLineStart
+ EOLLen
);
535 posLineStart
= strTrans
.Find(pEOL
);
538 // also add whatever we have left in the translated string.
539 if ( !strTrans
.empty() )
540 memText
.AddLine(strTrans
);
542 // Finally we can parse it all.
543 Parse(memText
, true /* local */);
548 #endif // wxUSE_STREAMS
550 void wxFileConfig::CleanUp()
554 wxFileConfigLineList
*pCur
= m_linesHead
;
555 while ( pCur
!= NULL
) {
556 wxFileConfigLineList
*pNext
= pCur
->Next();
562 wxFileConfig::~wxFileConfig()
569 // ----------------------------------------------------------------------------
570 // parse a config file
571 // ----------------------------------------------------------------------------
573 void wxFileConfig::Parse(wxTextBuffer
& buffer
, bool bLocal
)
575 const wxChar
*pStart
;
579 size_t nLineCount
= buffer
.GetLineCount();
581 for ( size_t n
= 0; n
< nLineCount
; n
++ )
585 // add the line to linked list
588 LineListAppend(strLine
);
590 // let the root group have it start line as well
593 m_pCurrentGroup
->SetLine(m_linesTail
);
598 // skip leading spaces
599 for ( pStart
= strLine
; wxIsspace(*pStart
); pStart
++ )
602 // skip blank/comment lines
603 if ( *pStart
== wxT('\0')|| *pStart
== wxT(';') || *pStart
== wxT('#') )
606 if ( *pStart
== wxT('[') ) { // a new group
609 while ( *++pEnd
!= wxT(']') ) {
610 if ( *pEnd
== wxT('\\') ) {
611 // the next char is escaped, so skip it even if it is ']'
615 if ( *pEnd
== wxT('\n') || *pEnd
== wxT('\0') ) {
616 // we reached the end of line, break out of the loop
621 if ( *pEnd
!= wxT(']') ) {
622 wxLogError(_("file '%s': unexpected character %c at line %d."),
623 buffer
.GetName(), *pEnd
, n
+ 1);
624 continue; // skip this line
627 // group name here is always considered as abs path
630 strGroup
<< wxCONFIG_PATH_SEPARATOR
631 << FilterInEntryName(wxString(pStart
, pEnd
- pStart
));
633 // will create it if doesn't yet exist
638 if ( m_pCurrentGroup
->Parent() )
639 m_pCurrentGroup
->Parent()->SetLastGroup(m_pCurrentGroup
);
640 m_pCurrentGroup
->SetLine(m_linesTail
);
643 // check that there is nothing except comments left on this line
645 while ( *++pEnd
!= wxT('\0') && bCont
) {
654 // ignore whitespace ('\n' impossible here)
658 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
659 buffer
.GetName(), n
+ 1, pEnd
);
665 const wxChar
*pEnd
= pStart
;
666 while ( *pEnd
&& *pEnd
!= wxT('=') /* && !wxIsspace(*pEnd)*/ ) {
667 if ( *pEnd
== wxT('\\') ) {
668 // next character may be space or not - still take it because it's
669 // quoted (unless there is nothing)
672 // the error message will be given below anyhow
680 wxString
strKey(FilterInEntryName(wxString(pStart
, pEnd
).Trim()));
683 while ( wxIsspace(*pEnd
) )
686 if ( *pEnd
++ != wxT('=') ) {
687 wxLogError(_("file '%s', line %d: '=' expected."),
688 buffer
.GetName(), n
+ 1);
691 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strKey
);
693 if ( pEntry
== NULL
) {
695 pEntry
= m_pCurrentGroup
->AddEntry(strKey
, n
);
698 if ( bLocal
&& pEntry
->IsImmutable() ) {
699 // immutable keys can't be changed by user
700 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
701 buffer
.GetName(), n
+ 1, strKey
.c_str());
704 // the condition below catches the cases (a) and (b) but not (c):
705 // (a) global key found second time in global file
706 // (b) key found second (or more) time in local file
707 // (c) key from global file now found in local one
708 // which is exactly what we want.
709 else if ( !bLocal
|| pEntry
->IsLocal() ) {
710 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
711 buffer
.GetName(), n
+ 1, strKey
.c_str(), pEntry
->Line());
717 pEntry
->SetLine(m_linesTail
);
720 while ( wxIsspace(*pEnd
) )
723 wxString value
= pEnd
;
724 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
) )
725 value
= FilterInValue(value
);
727 pEntry
->SetValue(value
, false);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 void wxFileConfig::SetRootPath()
740 m_pCurrentGroup
= m_pRootGroup
;
743 void wxFileConfig::SetPath(const wxString
& strPath
)
745 wxArrayString aParts
;
747 if ( strPath
.empty() ) {
752 if ( strPath
[0] == wxCONFIG_PATH_SEPARATOR
) {
754 wxSplitPath(aParts
, strPath
);
757 // relative path, combine with current one
758 wxString strFullPath
= m_strPath
;
759 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
760 wxSplitPath(aParts
, strFullPath
);
763 // change current group
765 m_pCurrentGroup
= m_pRootGroup
;
766 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
767 wxFileConfigGroup
*pNextGroup
= m_pCurrentGroup
->FindSubgroup(aParts
[n
]);
768 if ( pNextGroup
== NULL
)
769 pNextGroup
= m_pCurrentGroup
->AddSubgroup(aParts
[n
]);
770 m_pCurrentGroup
= pNextGroup
;
773 // recombine path parts in one variable
775 for ( n
= 0; n
< aParts
.Count(); n
++ ) {
776 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 bool wxFileConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
787 return GetNextGroup(str
, lIndex
);
790 bool wxFileConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
792 if ( size_t(lIndex
) < m_pCurrentGroup
->Groups().Count() ) {
793 str
= m_pCurrentGroup
->Groups()[(size_t)lIndex
++]->Name();
800 bool wxFileConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
803 return GetNextEntry(str
, lIndex
);
806 bool wxFileConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
808 if ( size_t(lIndex
) < m_pCurrentGroup
->Entries().Count() ) {
809 str
= m_pCurrentGroup
->Entries()[(size_t)lIndex
++]->Name();
816 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive
) const
818 size_t n
= m_pCurrentGroup
->Entries().Count();
820 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
821 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
822 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
823 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
824 n
+= GetNumberOfEntries(true);
825 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
832 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive
) const
834 size_t n
= m_pCurrentGroup
->Groups().Count();
836 wxFileConfigGroup
*pOldCurrentGroup
= m_pCurrentGroup
;
837 size_t nSubgroups
= m_pCurrentGroup
->Groups().Count();
838 for ( size_t nGroup
= 0; nGroup
< nSubgroups
; nGroup
++ ) {
839 CONST_CAST m_pCurrentGroup
= m_pCurrentGroup
->Groups()[nGroup
];
840 n
+= GetNumberOfGroups(true);
841 CONST_CAST m_pCurrentGroup
= pOldCurrentGroup
;
848 // ----------------------------------------------------------------------------
849 // tests for existence
850 // ----------------------------------------------------------------------------
852 bool wxFileConfig::HasGroup(const wxString
& strName
) const
854 wxConfigPathChanger
path(this, strName
);
856 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
->FindSubgroup(path
.Name());
857 return pGroup
!= NULL
;
860 bool wxFileConfig::HasEntry(const wxString
& strName
) const
862 wxConfigPathChanger
path(this, strName
);
864 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
865 return pEntry
!= NULL
;
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 bool wxFileConfig::DoReadString(const wxString
& key
, wxString
* pStr
) const
874 wxConfigPathChanger
path(this, key
);
876 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(path
.Name());
877 if (pEntry
== NULL
) {
881 *pStr
= pEntry
->Value();
886 bool wxFileConfig::DoReadLong(const wxString
& key
, long *pl
) const
889 if ( !Read(key
, &str
) )
892 // extra spaces shouldn't prevent us from reading numeric values
895 return str
.ToLong(pl
);
898 bool wxFileConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
900 wxConfigPathChanger
path(this, key
);
901 wxString strName
= path
.Name();
903 wxLogTrace( FILECONF_TRACE_MASK
,
904 _T(" Writing String '%s' = '%s' to Group '%s'"),
909 if ( strName
.empty() )
911 // setting the value of a group is an error
913 wxASSERT_MSG( szValue
.empty(), wxT("can't set value of a group!") );
915 // ... except if it's empty in which case it's a way to force it's creation
917 wxLogTrace( FILECONF_TRACE_MASK
,
918 _T(" Creating group %s"),
919 m_pCurrentGroup
->Name().c_str() );
923 // this will add a line for this group if it didn't have it before
925 (void)m_pCurrentGroup
->GetGroupLine();
929 // writing an entry check that the name is reasonable
930 if ( strName
[0u] == wxCONFIG_IMMUTABLE_PREFIX
)
932 wxLogError( _("Config entry name cannot start with '%c'."),
933 wxCONFIG_IMMUTABLE_PREFIX
);
937 wxFileConfigEntry
*pEntry
= m_pCurrentGroup
->FindEntry(strName
);
941 wxLogTrace( FILECONF_TRACE_MASK
,
942 _T(" Adding Entry %s"),
944 pEntry
= m_pCurrentGroup
->AddEntry(strName
);
947 wxLogTrace( FILECONF_TRACE_MASK
,
948 _T(" Setting value %s"),
950 pEntry
->SetValue(szValue
);
958 bool wxFileConfig::DoWriteLong(const wxString
& key
, long lValue
)
960 return Write(key
, wxString::Format(_T("%ld"), lValue
));
963 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
965 if ( !IsDirty() || !m_strLocalFile
)
968 // set the umask if needed
969 wxCHANGE_UMASK(m_umask
);
971 wxTempFile
file(m_strLocalFile
);
973 if ( !file
.IsOpened() )
975 wxLogError(_("can't open user configuration file."));
979 // write all strings to file
980 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
982 wxString line
= p
->Text();
983 line
+= wxTextFile::GetEOL();
984 if ( !file
.Write(line
, m_conv
) )
986 wxLogError(_("can't write user configuration file."));
991 if ( !file
.Commit() )
993 wxLogError(_("Failed to update user configuration file."));
1000 #if defined(__WXMAC__)
1001 wxFileName(m_strLocalFile
).MacSetTypeAndCreator('TEXT', 'ttxt');
1009 bool wxFileConfig::Save(wxOutputStream
& os
, wxMBConv
& conv
)
1011 // save unconditionally, even if not dirty
1012 for ( wxFileConfigLineList
*p
= m_linesHead
; p
!= NULL
; p
= p
->Next() )
1014 wxString line
= p
->Text();
1015 line
+= wxTextFile::GetEOL();
1016 if ( !os
.Write(line
.mb_str(conv
), line
.length()) )
1018 wxLogError(_("Error saving user configuration data."));
1029 #endif // wxUSE_STREAMS
1031 // ----------------------------------------------------------------------------
1032 // renaming groups/entries
1033 // ----------------------------------------------------------------------------
1035 bool wxFileConfig::RenameEntry(const wxString
& oldName
,
1036 const wxString
& newName
)
1038 wxASSERT_MSG( !wxStrchr(oldName
, wxCONFIG_PATH_SEPARATOR
),
1039 _T("RenameEntry(): paths are not supported") );
1041 // check that the entry exists
1042 wxFileConfigEntry
*oldEntry
= m_pCurrentGroup
->FindEntry(oldName
);
1046 // check that the new entry doesn't already exist
1047 if ( m_pCurrentGroup
->FindEntry(newName
) )
1050 // delete the old entry, create the new one
1051 wxString value
= oldEntry
->Value();
1052 if ( !m_pCurrentGroup
->DeleteEntry(oldName
) )
1057 wxFileConfigEntry
*newEntry
= m_pCurrentGroup
->AddEntry(newName
);
1058 newEntry
->SetValue(value
);
1063 bool wxFileConfig::RenameGroup(const wxString
& oldName
,
1064 const wxString
& newName
)
1066 // check that the group exists
1067 wxFileConfigGroup
*group
= m_pCurrentGroup
->FindSubgroup(oldName
);
1071 // check that the new group doesn't already exist
1072 if ( m_pCurrentGroup
->FindSubgroup(newName
) )
1075 group
->Rename(newName
);
1082 // ----------------------------------------------------------------------------
1083 // delete groups/entries
1084 // ----------------------------------------------------------------------------
1086 bool wxFileConfig::DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
)
1088 wxConfigPathChanger
path(this, key
);
1090 if ( !m_pCurrentGroup
->DeleteEntry(path
.Name()) )
1093 if ( bGroupIfEmptyAlso
&& m_pCurrentGroup
->IsEmpty() ) {
1094 if ( m_pCurrentGroup
!= m_pRootGroup
) {
1095 wxFileConfigGroup
*pGroup
= m_pCurrentGroup
;
1096 SetPath(wxT("..")); // changes m_pCurrentGroup!
1097 if ( m_pCurrentGroup
->DeleteSubgroupByName(pGroup
->Name()) )
1100 //else: never delete the root group
1106 bool wxFileConfig::DeleteGroup(const wxString
& key
)
1108 wxConfigPathChanger
path(this, key
);
1110 if ( !m_pCurrentGroup
->DeleteSubgroupByName(path
.Name()) )
1118 bool wxFileConfig::DeleteAll()
1122 if ( !m_strLocalFile
.empty() )
1124 if ( wxFile::Exists(m_strLocalFile
) && wxRemove(m_strLocalFile
) == -1 )
1126 wxLogSysError(_("can't delete user configuration file '%s'"),
1127 m_strLocalFile
.c_str());
1132 m_strGlobalFile
= wxEmptyString
;
1140 // ----------------------------------------------------------------------------
1141 // linked list functions
1142 // ----------------------------------------------------------------------------
1144 // append a new line to the end of the list
1146 wxFileConfigLineList
*wxFileConfig::LineListAppend(const wxString
& str
)
1148 wxLogTrace( FILECONF_TRACE_MASK
,
1149 _T(" ** Adding Line '%s'"),
1151 wxLogTrace( FILECONF_TRACE_MASK
,
1153 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1154 wxLogTrace( FILECONF_TRACE_MASK
,
1156 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1158 wxFileConfigLineList
*pLine
= new wxFileConfigLineList(str
);
1160 if ( m_linesTail
== NULL
)
1163 m_linesHead
= pLine
;
1168 m_linesTail
->SetNext(pLine
);
1169 pLine
->SetPrev(m_linesTail
);
1172 m_linesTail
= pLine
;
1174 wxLogTrace( FILECONF_TRACE_MASK
,
1176 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1177 wxLogTrace( FILECONF_TRACE_MASK
,
1179 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1184 // insert a new line after the given one or in the very beginning if !pLine
1185 wxFileConfigLineList
*wxFileConfig::LineListInsert(const wxString
& str
,
1186 wxFileConfigLineList
*pLine
)
1188 wxLogTrace( FILECONF_TRACE_MASK
,
1189 _T(" ** Inserting Line '%s' after '%s'"),
1191 ((pLine
) ? pLine
->Text().c_str() : wxEmptyString
) );
1192 wxLogTrace( FILECONF_TRACE_MASK
,
1194 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1195 wxLogTrace( FILECONF_TRACE_MASK
,
1197 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1199 if ( pLine
== m_linesTail
)
1200 return LineListAppend(str
);
1202 wxFileConfigLineList
*pNewLine
= new wxFileConfigLineList(str
);
1203 if ( pLine
== NULL
)
1205 // prepend to the list
1206 pNewLine
->SetNext(m_linesHead
);
1207 m_linesHead
->SetPrev(pNewLine
);
1208 m_linesHead
= pNewLine
;
1212 // insert before pLine
1213 wxFileConfigLineList
*pNext
= pLine
->Next();
1214 pNewLine
->SetNext(pNext
);
1215 pNewLine
->SetPrev(pLine
);
1216 pNext
->SetPrev(pNewLine
);
1217 pLine
->SetNext(pNewLine
);
1220 wxLogTrace( FILECONF_TRACE_MASK
,
1222 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1223 wxLogTrace( FILECONF_TRACE_MASK
,
1225 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1230 void wxFileConfig::LineListRemove(wxFileConfigLineList
*pLine
)
1232 wxLogTrace( FILECONF_TRACE_MASK
,
1233 _T(" ** Removing Line '%s'"),
1234 pLine
->Text().c_str() );
1235 wxLogTrace( FILECONF_TRACE_MASK
,
1237 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1238 wxLogTrace( FILECONF_TRACE_MASK
,
1240 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1242 wxFileConfigLineList
*pPrev
= pLine
->Prev(),
1243 *pNext
= pLine
->Next();
1247 if ( pPrev
== NULL
)
1248 m_linesHead
= pNext
;
1250 pPrev
->SetNext(pNext
);
1254 if ( pNext
== NULL
)
1255 m_linesTail
= pPrev
;
1257 pNext
->SetPrev(pPrev
);
1259 wxLogTrace( FILECONF_TRACE_MASK
,
1261 ((m_linesHead
) ? m_linesHead
->Text().c_str() : wxEmptyString
) );
1262 wxLogTrace( FILECONF_TRACE_MASK
,
1264 ((m_linesTail
) ? m_linesTail
->Text().c_str() : wxEmptyString
) );
1269 bool wxFileConfig::LineListIsEmpty()
1271 return m_linesHead
== NULL
;
1274 // ============================================================================
1275 // wxFileConfig::wxFileConfigGroup
1276 // ============================================================================
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1283 wxFileConfigGroup::wxFileConfigGroup(wxFileConfigGroup
*pParent
,
1284 const wxString
& strName
,
1285 wxFileConfig
*pConfig
)
1286 : m_aEntries(CompareEntries
),
1287 m_aSubgroups(CompareGroups
),
1290 m_pConfig
= pConfig
;
1291 m_pParent
= pParent
;
1294 m_pLastEntry
= NULL
;
1295 m_pLastGroup
= NULL
;
1298 // dtor deletes all children
1299 wxFileConfigGroup::~wxFileConfigGroup()
1302 size_t n
, nCount
= m_aEntries
.Count();
1303 for ( n
= 0; n
< nCount
; n
++ )
1304 delete m_aEntries
[n
];
1307 nCount
= m_aSubgroups
.Count();
1308 for ( n
= 0; n
< nCount
; n
++ )
1309 delete m_aSubgroups
[n
];
1312 // ----------------------------------------------------------------------------
1314 // ----------------------------------------------------------------------------
1316 void wxFileConfigGroup::SetLine(wxFileConfigLineList
*pLine
)
1318 wxASSERT( m_pLine
== 0 ); // shouldn't be called twice
1323 This is a bit complicated, so let me explain it in details. All lines that
1324 were read from the local file (the only one we will ever modify) are stored
1325 in a (doubly) linked list. Our problem is to know at which position in this
1326 list should we insert the new entries/subgroups. To solve it we keep three
1327 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1329 m_pLine points to the line containing "[group_name]"
1330 m_pLastEntry points to the last entry of this group in the local file.
1331 m_pLastGroup subgroup
1333 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1334 from the local file, the corresponding variable is set. However, if the group
1335 was read from the global file and then modified or created by the application
1336 these variables are still NULL and we need to create the corresponding lines.
1337 See the following functions (and comments preceding them) for the details of
1340 Also, when our last entry/group are deleted we need to find the new last
1341 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1342 of lines until it either founds an entry/subgroup (and this is the new last
1343 element) or the m_pLine of the group, in which case there are no more entries
1344 (or subgroups) left and m_pLast<element> becomes NULL.
1346 NB: This last problem could be avoided for entries if we added new entries
1347 immediately after m_pLine, but in this case the entries would appear
1348 backwards in the config file (OTOH, it's not that important) and as we
1349 would still need to do it for the subgroups the code wouldn't have been
1350 significantly less complicated.
1353 // Return the line which contains "[our name]". If we're still not in the list,
1354 // add our line to it immediately after the last line of our parent group if we
1355 // have it or in the very beginning if we're the root group.
1356 wxFileConfigLineList
*wxFileConfigGroup::GetGroupLine()
1358 wxLogTrace( FILECONF_TRACE_MASK
,
1359 _T(" GetGroupLine() for Group '%s'"),
1364 wxLogTrace( FILECONF_TRACE_MASK
,
1365 _T(" Getting Line item pointer") );
1367 wxFileConfigGroup
*pParent
= Parent();
1369 // this group wasn't present in local config file, add it now
1372 wxLogTrace( FILECONF_TRACE_MASK
,
1373 _T(" checking parent '%s'"),
1374 pParent
->Name().c_str() );
1376 wxString strFullName
;
1378 // add 1 to the name because we don't want to start with '/'
1379 strFullName
<< wxT("[")
1380 << FilterOutEntryName(GetFullName().c_str() + 1)
1382 m_pLine
= m_pConfig
->LineListInsert(strFullName
,
1383 pParent
->GetLastGroupLine());
1384 pParent
->SetLastGroup(this); // we're surely after all the others
1386 //else: this is the root group and so we return NULL because we don't
1387 // have any group line
1393 // Return the last line belonging to the subgroups of this group (after which
1394 // we can add a new subgroup), if we don't have any subgroups or entries our
1395 // last line is the group line (m_pLine) itself.
1396 wxFileConfigLineList
*wxFileConfigGroup::GetLastGroupLine()
1398 // if we have any subgroups, our last line is the last line of the last
1402 wxFileConfigLineList
*pLine
= m_pLastGroup
->GetLastGroupLine();
1404 wxASSERT_MSG( pLine
, _T("last group must have !NULL associated line") );
1409 // no subgroups, so the last line is the line of thelast entry (if any)
1410 return GetLastEntryLine();
1413 // return the last line belonging to the entries of this group (after which
1414 // we can add a new entry), if we don't have any entries we will add the new
1415 // one immediately after the group line itself.
1416 wxFileConfigLineList
*wxFileConfigGroup::GetLastEntryLine()
1418 wxLogTrace( FILECONF_TRACE_MASK
,
1419 _T(" GetLastEntryLine() for Group '%s'"),
1424 wxFileConfigLineList
*pLine
= m_pLastEntry
->GetLine();
1426 wxASSERT_MSG( pLine
, _T("last entry must have !NULL associated line") );
1431 // no entries: insert after the group header, if any
1432 return GetGroupLine();
1435 void wxFileConfigGroup::SetLastEntry(wxFileConfigEntry
*pEntry
)
1437 m_pLastEntry
= pEntry
;
1441 // the only situation in which a group without its own line can have
1442 // an entry is when the first entry is added to the initially empty
1443 // root pseudo-group
1444 wxASSERT_MSG( !m_pParent
, _T("unexpected for non root group") );
1446 // let the group know that it does have a line in the file now
1447 m_pLine
= pEntry
->GetLine();
1451 // ----------------------------------------------------------------------------
1453 // ----------------------------------------------------------------------------
1455 void wxFileConfigGroup::UpdateGroupAndSubgroupsLines()
1457 // update the line of this group
1458 wxFileConfigLineList
*line
= GetGroupLine();
1459 wxCHECK_RET( line
, _T("a non root group must have a corresponding line!") );
1461 // +1: skip the leading '/'
1462 line
->SetText(wxString::Format(_T("[%s]"), GetFullName().c_str() + 1));
1465 // also update all subgroups as they have this groups name in their lines
1466 const size_t nCount
= m_aSubgroups
.Count();
1467 for ( size_t n
= 0; n
< nCount
; n
++ )
1469 m_aSubgroups
[n
]->UpdateGroupAndSubgroupsLines();
1473 void wxFileConfigGroup::Rename(const wxString
& newName
)
1475 wxCHECK_RET( m_pParent
, _T("the root group can't be renamed") );
1477 m_strName
= newName
;
1479 // update the group lines recursively
1480 UpdateGroupAndSubgroupsLines();
1483 wxString
wxFileConfigGroup::GetFullName() const
1487 fullname
= Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR
+ Name();
1492 // ----------------------------------------------------------------------------
1494 // ----------------------------------------------------------------------------
1496 // use binary search because the array is sorted
1498 wxFileConfigGroup::FindEntry(const wxChar
*szName
) const
1502 hi
= m_aEntries
.Count();
1504 wxFileConfigEntry
*pEntry
;
1508 pEntry
= m_aEntries
[i
];
1510 #if wxCONFIG_CASE_SENSITIVE
1511 res
= wxStrcmp(pEntry
->Name(), szName
);
1513 res
= wxStricmp(pEntry
->Name(), szName
);
1528 wxFileConfigGroup::FindSubgroup(const wxChar
*szName
) const
1532 hi
= m_aSubgroups
.Count();
1534 wxFileConfigGroup
*pGroup
;
1538 pGroup
= m_aSubgroups
[i
];
1540 #if wxCONFIG_CASE_SENSITIVE
1541 res
= wxStrcmp(pGroup
->Name(), szName
);
1543 res
= wxStricmp(pGroup
->Name(), szName
);
1557 // ----------------------------------------------------------------------------
1558 // create a new item
1559 // ----------------------------------------------------------------------------
1561 // create a new entry and add it to the current group
1562 wxFileConfigEntry
*wxFileConfigGroup::AddEntry(const wxString
& strName
, int nLine
)
1564 wxASSERT( FindEntry(strName
) == 0 );
1566 wxFileConfigEntry
*pEntry
= new wxFileConfigEntry(this, strName
, nLine
);
1568 m_aEntries
.Add(pEntry
);
1572 // create a new group and add it to the current group
1573 wxFileConfigGroup
*wxFileConfigGroup::AddSubgroup(const wxString
& strName
)
1575 wxASSERT( FindSubgroup(strName
) == 0 );
1577 wxFileConfigGroup
*pGroup
= new wxFileConfigGroup(this, strName
, m_pConfig
);
1579 m_aSubgroups
.Add(pGroup
);
1583 // ----------------------------------------------------------------------------
1585 // ----------------------------------------------------------------------------
1588 The delete operations are _very_ slow if we delete the last item of this
1589 group (see comments before GetXXXLineXXX functions for more details),
1590 so it's much better to start with the first entry/group if we want to
1591 delete several of them.
1594 bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar
*szName
)
1596 wxFileConfigGroup
* const pGroup
= FindSubgroup(szName
);
1598 return pGroup
? DeleteSubgroup(pGroup
) : false;
1601 // Delete the subgroup and remove all references to it from
1602 // other data structures.
1603 bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup
*pGroup
)
1605 wxCHECK_MSG( pGroup
, false, _T("deleting non existing group?") );
1607 wxLogTrace( FILECONF_TRACE_MASK
,
1608 _T("Deleting group '%s' from '%s'"),
1609 pGroup
->Name().c_str(),
1612 wxLogTrace( FILECONF_TRACE_MASK
,
1613 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1614 ((m_pLine
) ? m_pLine
->Prev() : 0),
1616 ((m_pLine
) ? m_pLine
->Next() : 0) );
1617 wxLogTrace( FILECONF_TRACE_MASK
,
1619 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1621 // delete all entries...
1622 size_t nCount
= pGroup
->m_aEntries
.Count();
1624 wxLogTrace(FILECONF_TRACE_MASK
,
1625 _T("Removing %lu entries"), (unsigned long)nCount
);
1627 for ( size_t nEntry
= 0; nEntry
< nCount
; nEntry
++ )
1629 wxFileConfigLineList
*pLine
= pGroup
->m_aEntries
[nEntry
]->GetLine();
1633 wxLogTrace( FILECONF_TRACE_MASK
,
1635 pLine
->Text().c_str() );
1636 m_pConfig
->LineListRemove(pLine
);
1640 // ...and subgroups of this subgroup
1641 nCount
= pGroup
->m_aSubgroups
.Count();
1643 wxLogTrace( FILECONF_TRACE_MASK
,
1644 _T("Removing %lu subgroups"), (unsigned long)nCount
);
1646 for ( size_t nGroup
= 0; nGroup
< nCount
; nGroup
++ )
1648 pGroup
->DeleteSubgroup(pGroup
->m_aSubgroups
[0]);
1651 // and then finally the group itself
1652 wxFileConfigLineList
*pLine
= pGroup
->m_pLine
;
1655 wxLogTrace( FILECONF_TRACE_MASK
,
1656 _T(" Removing line for group '%s' : '%s'"),
1657 pGroup
->Name().c_str(),
1658 pLine
->Text().c_str() );
1659 wxLogTrace( FILECONF_TRACE_MASK
,
1660 _T(" Removing from group '%s' : '%s'"),
1662 ((m_pLine
) ? m_pLine
->Text().c_str() : wxEmptyString
) );
1664 // notice that we may do this test inside the previous "if"
1665 // because the last entry's line is surely !NULL
1666 if ( pGroup
== m_pLastGroup
)
1668 wxLogTrace( FILECONF_TRACE_MASK
,
1669 _T(" Removing last group") );
1671 // our last entry is being deleted, so find the last one which
1672 // stays by going back until we find a subgroup or reach the
1674 const size_t nSubgroups
= m_aSubgroups
.Count();
1676 m_pLastGroup
= NULL
;
1677 for ( wxFileConfigLineList
*pl
= pLine
->Prev();
1678 pl
&& pl
!= m_pLine
&& !m_pLastGroup
;
1681 // does this line belong to our subgroup?
1682 for ( size_t n
= 0; n
< nSubgroups
; n
++ )
1684 // do _not_ call GetGroupLine! we don't want to add it to
1685 // the local file if it's not already there
1686 if ( m_aSubgroups
[n
]->m_pLine
== pl
)
1688 m_pLastGroup
= m_aSubgroups
[n
];
1695 m_pConfig
->LineListRemove(pLine
);
1699 wxLogTrace( FILECONF_TRACE_MASK
,
1700 _T(" No line entry for Group '%s'?"),
1701 pGroup
->Name().c_str() );
1704 m_aSubgroups
.Remove(pGroup
);
1710 bool wxFileConfigGroup::DeleteEntry(const wxChar
*szName
)
1712 wxFileConfigEntry
*pEntry
= FindEntry(szName
);
1715 // entry doesn't exist, nothing to do
1719 wxFileConfigLineList
*pLine
= pEntry
->GetLine();
1720 if ( pLine
!= NULL
) {
1721 // notice that we may do this test inside the previous "if" because the
1722 // last entry's line is surely !NULL
1723 if ( pEntry
== m_pLastEntry
) {
1724 // our last entry is being deleted - find the last one which stays
1725 wxASSERT( m_pLine
!= NULL
); // if we have an entry with !NULL pLine...
1727 // go back until we find another entry or reach the group's line
1728 wxFileConfigEntry
*pNewLast
= NULL
;
1729 size_t n
, nEntries
= m_aEntries
.Count();
1730 wxFileConfigLineList
*pl
;
1731 for ( pl
= pLine
->Prev(); pl
!= m_pLine
; pl
= pl
->Prev() ) {
1732 // is it our subgroup?
1733 for ( n
= 0; (pNewLast
== NULL
) && (n
< nEntries
); n
++ ) {
1734 if ( m_aEntries
[n
]->GetLine() == m_pLine
)
1735 pNewLast
= m_aEntries
[n
];
1738 if ( pNewLast
!= NULL
) // found?
1742 if ( pl
== m_pLine
) {
1743 wxASSERT( !pNewLast
); // how comes it has the same line as we?
1745 // we've reached the group line without finding any subgroups
1746 m_pLastEntry
= NULL
;
1749 m_pLastEntry
= pNewLast
;
1752 m_pConfig
->LineListRemove(pLine
);
1755 m_aEntries
.Remove(pEntry
);
1761 // ============================================================================
1762 // wxFileConfig::wxFileConfigEntry
1763 // ============================================================================
1765 // ----------------------------------------------------------------------------
1767 // ----------------------------------------------------------------------------
1768 wxFileConfigEntry::wxFileConfigEntry(wxFileConfigGroup
*pParent
,
1769 const wxString
& strName
,
1771 : m_strName(strName
)
1773 wxASSERT( !strName
.empty() );
1775 m_pParent
= pParent
;
1779 m_bHasValue
= false;
1781 m_bImmutable
= strName
[0] == wxCONFIG_IMMUTABLE_PREFIX
;
1783 m_strName
.erase(0, 1); // remove first character
1786 // ----------------------------------------------------------------------------
1788 // ----------------------------------------------------------------------------
1790 void wxFileConfigEntry::SetLine(wxFileConfigLineList
*pLine
)
1792 if ( m_pLine
!= NULL
) {
1793 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1794 Name().c_str(), m_pParent
->GetFullName().c_str());
1798 Group()->SetLastEntry(this);
1801 // second parameter is false if we read the value from file and prevents the
1802 // entry from being marked as 'dirty'
1803 void wxFileConfigEntry::SetValue(const wxString
& strValue
, bool bUser
)
1805 if ( bUser
&& IsImmutable() )
1807 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1812 // do nothing if it's the same value: but don't test for it if m_bHasValue
1813 // hadn't been set yet or we'd never write empty values to the file
1814 if ( m_bHasValue
&& strValue
== m_strValue
)
1818 m_strValue
= strValue
;
1822 wxString strValFiltered
;
1824 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS
)
1826 strValFiltered
= strValue
;
1829 strValFiltered
= FilterOutValue(strValue
);
1833 strLine
<< FilterOutEntryName(m_strName
) << wxT('=') << strValFiltered
;
1837 // entry was read from the local config file, just modify the line
1838 m_pLine
->SetText(strLine
);
1840 else // this entry didn't exist in the local file
1842 // add a new line to the file: note the hack for the root group
1843 // which is special in that it doesn't have its own group line
1844 // (something like "[/]") and so the line we get for it may be not
1845 // its line at all if it doesn't have any entries
1847 // this is definitely not the right place to fix it but changing
1848 // the root group to have NULL m_pLine will probably break too
1849 // much stuff elsewhere so I don't dare to do it...
1850 wxFileConfigLineList
*line
= Group()->GetLastEntryLine();
1851 if ( !Group()->Parent() && line
== Group()->GetGroupLine() )
1853 // prepend the first root group entry to the head of the list
1856 m_pLine
= Group()->Config()->LineListInsert(strLine
, line
);
1858 Group()->SetLastEntry(this);
1863 // ============================================================================
1865 // ============================================================================
1867 // ----------------------------------------------------------------------------
1868 // compare functions for array sorting
1869 // ----------------------------------------------------------------------------
1871 int CompareEntries(wxFileConfigEntry
*p1
, wxFileConfigEntry
*p2
)
1873 #if wxCONFIG_CASE_SENSITIVE
1874 return wxStrcmp(p1
->Name(), p2
->Name());
1876 return wxStricmp(p1
->Name(), p2
->Name());
1880 int CompareGroups(wxFileConfigGroup
*p1
, wxFileConfigGroup
*p2
)
1882 #if wxCONFIG_CASE_SENSITIVE
1883 return wxStrcmp(p1
->Name(), p2
->Name());
1885 return wxStricmp(p1
->Name(), p2
->Name());
1889 // ----------------------------------------------------------------------------
1891 // ----------------------------------------------------------------------------
1893 // undo FilterOutValue
1894 static wxString
FilterInValue(const wxString
& str
)
1897 strResult
.Alloc(str
.Len());
1899 bool bQuoted
= !str
.empty() && str
[0] == '"';
1901 for ( size_t n
= bQuoted
? 1 : 0; n
< str
.Len(); n
++ ) {
1902 if ( str
[n
] == wxT('\\') ) {
1903 switch ( str
[++n
] ) {
1905 strResult
+= wxT('\n');
1909 strResult
+= wxT('\r');
1913 strResult
+= wxT('\t');
1917 strResult
+= wxT('\\');
1921 strResult
+= wxT('"');
1926 if ( str
[n
] != wxT('"') || !bQuoted
)
1927 strResult
+= str
[n
];
1928 else if ( n
!= str
.Len() - 1 ) {
1929 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1932 //else: it's the last quote of a quoted string, ok
1939 // quote the string before writing it to file
1940 static wxString
FilterOutValue(const wxString
& str
)
1946 strResult
.Alloc(str
.Len());
1948 // quoting is necessary to preserve spaces in the beginning of the string
1949 bool bQuote
= wxIsspace(str
[0]) || str
[0] == wxT('"');
1952 strResult
+= wxT('"');
1955 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
1978 //else: fall through
1981 strResult
+= str
[n
];
1982 continue; // nothing special to do
1985 // we get here only for special characters
1986 strResult
<< wxT('\\') << c
;
1990 strResult
+= wxT('"');
1995 // undo FilterOutEntryName
1996 static wxString
FilterInEntryName(const wxString
& str
)
1999 strResult
.Alloc(str
.Len());
2001 for ( const wxChar
*pc
= str
.c_str(); *pc
!= '\0'; pc
++ ) {
2002 if ( *pc
== wxT('\\') )
2011 // sanitize entry or group name: insert '\\' before any special characters
2012 static wxString
FilterOutEntryName(const wxString
& str
)
2015 strResult
.Alloc(str
.Len());
2017 for ( const wxChar
*pc
= str
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
2018 const wxChar c
= *pc
;
2020 // we explicitly allow some of "safe" chars and 8bit ASCII characters
2021 // which will probably never have special meaning and with which we can't
2022 // use isalnum() anyhow (in ASCII built, in Unicode it's just fine)
2024 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
2025 // should *not* be quoted
2028 ((unsigned char)c
< 127) &&
2030 !wxIsalnum(c
) && !wxStrchr(wxT("@_/-!.*%"), c
) )
2032 strResult
+= wxT('\\');
2041 // we can't put ?: in the ctor initializer list because it confuses some
2042 // broken compilers (Borland C++)
2043 static wxString
GetAppName(const wxString
& appName
)
2045 if ( !appName
&& wxTheApp
)
2046 return wxTheApp
->GetAppName();
2051 #endif // wxUSE_CONFIG