]> git.saurik.com Git - wxWidgets.git/blob - src/common/fileconf.cpp
fixed URL parsing problem
[wxWidgets.git] / src / common / fileconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: fileconf.cpp
3 // Purpose: implementation of wxFileConfig derivation of wxConfig
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.04.98 (adapted from appconf.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "fileconf.h"
15 #endif
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif //__BORLANDC__
26
27 #if wxUSE_CONFIG
28
29 #ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/intl.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/app.h"
35 #include "wx/dynarray.h"
36 #include "wx/file.h"
37 #include "wx/log.h"
38 #include "wx/textfile.h"
39 #include "wx/config.h"
40 #include "wx/fileconf.h"
41
42 #include "wx/utils.h" // for wxGetHomeDir
43
44 // _WINDOWS_ is defined when windows.h is included,
45 // __WXMSW__ is defined for MS Windows compilation
46 #if defined(__WXMSW__) && !defined(_WINDOWS_)
47 #include <windows.h>
48 #endif //windows.h
49 #if defined(__WXPM__)
50 #define INCL_DOS
51 #include <os2.h>
52 #define LINKAGEMODE _Optlink
53 #else
54 #define LINKAGEMODE
55 #endif
56
57 #include <stdlib.h>
58 #include <ctype.h>
59
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63 #define CONST_CAST ((wxFileConfig *)this)->
64
65 // ----------------------------------------------------------------------------
66 // constants
67 // ----------------------------------------------------------------------------
68 #ifndef MAX_PATH
69 #define MAX_PATH 512
70 #endif
71
72 // ----------------------------------------------------------------------------
73 // global functions declarations
74 // ----------------------------------------------------------------------------
75
76 // compare functions for sorting the arrays
77 static int LINKAGEMODE CompareEntries(ConfigEntry *p1, ConfigEntry *p2);
78 static int LINKAGEMODE CompareGroups(ConfigGroup *p1, ConfigGroup *p2);
79
80 // filter strings
81 static wxString FilterInValue(const wxString& str);
82 static wxString FilterOutValue(const wxString& str);
83
84 static wxString FilterInEntryName(const wxString& str);
85 static wxString FilterOutEntryName(const wxString& str);
86
87 // get the name to use in wxFileConfig ctor
88 static wxString GetAppName(const wxString& appname);
89
90 // ============================================================================
91 // implementation
92 // ============================================================================
93
94 // ----------------------------------------------------------------------------
95 // static functions
96 // ----------------------------------------------------------------------------
97 wxString wxFileConfig::GetGlobalDir()
98 {
99 wxString strDir;
100
101 #ifdef __UNIX__
102 strDir = wxT("/etc/");
103 #elif defined(__WXPM__)
104 ULONG aulSysInfo[QSV_MAX] = {0};
105 UINT drive;
106 APIRET rc;
107
108 rc = DosQuerySysInfo( 1L, QSV_MAX, (PVOID)aulSysInfo, sizeof(ULONG)*QSV_MAX);
109 if (rc == 0)
110 {
111 drive = aulSysInfo[QSV_BOOT_DRIVE - 1];
112 switch(drive)
113 {
114 case 1:
115 strDir = "A:\\OS2\\";
116 break;
117 case 2:
118 strDir = "B:\\OS2\\";
119 break;
120 case 3:
121 strDir = "C:\\OS2\\";
122 break;
123 case 4:
124 strDir = "D:\\OS2\\";
125 break;
126 case 5:
127 strDir = "E:\\OS2\\";
128 break;
129 case 6:
130 strDir = "F:\\OS2\\";
131 break;
132 case 7:
133 strDir = "G:\\OS2\\";
134 break;
135 case 8:
136 strDir = "H:\\OS2\\";
137 break;
138 case 9:
139 strDir = "I:\\OS2\\";
140 break;
141 case 10:
142 strDir = "J:\\OS2\\";
143 break;
144 case 11:
145 strDir = "K:\\OS2\\";
146 break;
147 case 12:
148 strDir = "L:\\OS2\\";
149 break;
150 case 13:
151 strDir = "M:\\OS2\\";
152 break;
153 case 14:
154 strDir = "N:\\OS2\\";
155 break;
156 case 15:
157 strDir = "O:\\OS2\\";
158 break;
159 case 16:
160 strDir = "P:\\OS2\\";
161 break;
162 case 17:
163 strDir = "Q:\\OS2\\";
164 break;
165 case 18:
166 strDir = "R:\\OS2\\";
167 break;
168 case 19:
169 strDir = "S:\\OS2\\";
170 break;
171 case 20:
172 strDir = "T:\\OS2\\";
173 break;
174 case 21:
175 strDir = "U:\\OS2\\";
176 break;
177 case 22:
178 strDir = "V:\\OS2\\";
179 break;
180 case 23:
181 strDir = "W:\\OS2\\";
182 break;
183 case 24:
184 strDir = "X:\\OS2\\";
185 break;
186 case 25:
187 strDir = "Y:\\OS2\\";
188 break;
189 case 26:
190 strDir = "Z:\\OS2\\";
191 break;
192 }
193 }
194 #elif defined(__WXSTUBS__)
195 wxASSERT_MSG( FALSE, wxT("TODO") ) ;
196 #elif defined(__WXMAC__)
197 wxASSERT_MSG( FALSE, wxT("TODO") ) ;
198 #else // Windows
199 wxChar szWinDir[MAX_PATH];
200 ::GetWindowsDirectory(szWinDir, MAX_PATH);
201
202 strDir = szWinDir;
203 strDir << wxT('\\');
204 #endif // Unix/Windows
205
206 return strDir;
207 }
208
209 wxString wxFileConfig::GetLocalDir()
210 {
211 wxString strDir;
212
213 wxGetHomeDir(&strDir);
214
215 #ifdef __UNIX__
216 if (strDir.Last() != wxT('/')) strDir << wxT('/');
217 #else
218 if (strDir.Last() != wxT('\\')) strDir << wxT('\\');
219 #endif
220
221 return strDir;
222 }
223
224 wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile)
225 {
226 wxString str = GetGlobalDir();
227 str << szFile;
228
229 if ( wxStrchr(szFile, wxT('.')) == NULL )
230 #ifdef __UNIX__
231 str << wxT(".conf");
232 #else // Windows
233 str << wxT(".ini");
234 #endif // UNIX/Win
235
236 return str;
237 }
238
239 wxString wxFileConfig::GetLocalFileName(const wxChar *szFile)
240 {
241 wxString str = GetLocalDir();
242
243 #ifdef __UNIX__
244 str << wxT('.');
245 #endif
246
247 str << szFile;
248
249 #ifdef __WXMSW__
250 if ( wxStrchr(szFile, wxT('.')) == NULL )
251 str << wxT(".ini");
252 #endif
253
254 return str;
255 }
256
257 // ----------------------------------------------------------------------------
258 // ctor
259 // ----------------------------------------------------------------------------
260
261 void wxFileConfig::Init()
262 {
263 m_pCurrentGroup =
264 m_pRootGroup = new ConfigGroup(NULL, "", this);
265
266 m_linesHead =
267 m_linesTail = NULL;
268
269 // it's not an error if (one of the) file(s) doesn't exist
270
271 // parse the global file
272 if ( !m_strGlobalFile.IsEmpty() && wxFile::Exists(m_strGlobalFile) ) {
273 wxTextFile fileGlobal(m_strGlobalFile);
274
275 if ( fileGlobal.Open() ) {
276 Parse(fileGlobal, FALSE /* global */);
277 SetRootPath();
278 }
279 else
280 wxLogWarning(_("can't open global configuration file '%s'."),
281 m_strGlobalFile.c_str());
282 }
283
284 // parse the local file
285 if ( !m_strLocalFile.IsEmpty() && wxFile::Exists(m_strLocalFile) ) {
286 wxTextFile fileLocal(m_strLocalFile);
287 if ( fileLocal.Open() ) {
288 Parse(fileLocal, TRUE /* local */);
289 SetRootPath();
290 }
291 else
292 wxLogWarning(_("can't open user configuration file '%s'."),
293 m_strLocalFile.c_str());
294 }
295 }
296
297 // constructor supports creation of wxFileConfig objects of any type
298 wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName,
299 const wxString& strLocal, const wxString& strGlobal,
300 long style)
301 : wxConfigBase(::GetAppName(appName), vendorName,
302 strLocal, strGlobal,
303 style),
304 m_strLocalFile(strLocal), m_strGlobalFile(strGlobal)
305 {
306 // Make up names for files if empty
307 if ( m_strLocalFile.IsEmpty() && (style & wxCONFIG_USE_LOCAL_FILE) )
308 {
309 m_strLocalFile = GetLocalFileName(GetAppName());
310 }
311
312 if ( m_strGlobalFile.IsEmpty() && (style & wxCONFIG_USE_GLOBAL_FILE) )
313 {
314 m_strGlobalFile = GetGlobalFileName(GetAppName());
315 }
316
317 // Check if styles are not supplied, but filenames are, in which case
318 // add the correct styles.
319 if ( !m_strLocalFile.IsEmpty() )
320 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE);
321
322 if ( !m_strGlobalFile.IsEmpty() )
323 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE);
324
325 // if the path is not absolute, prepend the standard directory to it
326 // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
327 if ( !(style & wxCONFIG_USE_RELATIVE_PATH) )
328 {
329 if ( !m_strLocalFile.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile) )
330 {
331 wxString strLocal = m_strLocalFile;
332 m_strLocalFile = GetLocalDir();
333 m_strLocalFile << strLocal;
334 }
335
336 if ( !m_strGlobalFile.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile) )
337 {
338 wxString strGlobal = m_strGlobalFile;
339 m_strGlobalFile = GetGlobalDir();
340 m_strGlobalFile << strGlobal;
341 }
342 }
343
344 Init();
345 }
346
347 void wxFileConfig::CleanUp()
348 {
349 delete m_pRootGroup;
350
351 LineList *pCur = m_linesHead;
352 while ( pCur != NULL ) {
353 LineList *pNext = pCur->Next();
354 delete pCur;
355 pCur = pNext;
356 }
357 }
358
359 wxFileConfig::~wxFileConfig()
360 {
361 Flush();
362
363 CleanUp();
364 }
365
366 // ----------------------------------------------------------------------------
367 // parse a config file
368 // ----------------------------------------------------------------------------
369
370 void wxFileConfig::Parse(wxTextFile& file, bool bLocal)
371 {
372 const wxChar *pStart;
373 const wxChar *pEnd;
374 wxString strLine;
375
376 size_t nLineCount = file.GetLineCount();
377 for ( size_t n = 0; n < nLineCount; n++ ) {
378 strLine = file[n];
379
380 // add the line to linked list
381 if ( bLocal )
382 LineListAppend(strLine);
383
384 // skip leading spaces
385 for ( pStart = strLine; wxIsspace(*pStart); pStart++ )
386 ;
387
388 // skip blank/comment lines
389 if ( *pStart == wxT('\0')|| *pStart == wxT(';') || *pStart == wxT('#') )
390 continue;
391
392 if ( *pStart == wxT('[') ) { // a new group
393 pEnd = pStart;
394
395 while ( *++pEnd != wxT(']') ) {
396 if ( *pEnd == wxT('\n') || *pEnd == wxT('\0') )
397 break;
398 }
399
400 if ( *pEnd != wxT(']') ) {
401 wxLogError(_("file '%s': unexpected character %c at line %d."),
402 file.GetName(), *pEnd, n + 1);
403 continue; // skip this line
404 }
405
406 // group name here is always considered as abs path
407 wxString strGroup;
408 pStart++;
409 strGroup << wxCONFIG_PATH_SEPARATOR
410 << FilterInEntryName(wxString(pStart, pEnd - pStart));
411
412 // will create it if doesn't yet exist
413 SetPath(strGroup);
414
415 if ( bLocal )
416 m_pCurrentGroup->SetLine(m_linesTail);
417
418 // check that there is nothing except comments left on this line
419 bool bCont = TRUE;
420 while ( *++pEnd != wxT('\0') && bCont ) {
421 switch ( *pEnd ) {
422 case wxT('#'):
423 case wxT(';'):
424 bCont = FALSE;
425 break;
426
427 case wxT(' '):
428 case wxT('\t'):
429 // ignore whitespace ('\n' impossible here)
430 break;
431
432 default:
433 wxLogWarning(_("file '%s', line %d: '%s' "
434 "ignored after group header."),
435 file.GetName(), n + 1, pEnd);
436 bCont = FALSE;
437 }
438 }
439 }
440 else { // a key
441 const wxChar *pEnd = pStart;
442 while ( *pEnd != wxT('=') && !wxIsspace(*pEnd) ) {
443 if ( *pEnd == wxT('\\') ) {
444 // next character may be space or not - still take it because it's
445 // quoted
446 pEnd++;
447 }
448
449 pEnd++;
450 }
451
452 wxString strKey(FilterInEntryName(wxString(pStart, pEnd)));
453
454 // skip whitespace
455 while ( isspace(*pEnd) )
456 pEnd++;
457
458 if ( *pEnd++ != wxT('=') ) {
459 wxLogError(_("file '%s', line %d: '=' expected."),
460 file.GetName(), n + 1);
461 }
462 else {
463 ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strKey);
464
465 if ( pEntry == NULL ) {
466 // new entry
467 pEntry = m_pCurrentGroup->AddEntry(strKey, n);
468
469 if ( bLocal )
470 pEntry->SetLine(m_linesTail);
471 }
472 else {
473 if ( bLocal && pEntry->IsImmutable() ) {
474 // immutable keys can't be changed by user
475 wxLogWarning(_("file '%s', line %d: value for "
476 "immutable key '%s' ignored."),
477 file.GetName(), n + 1, strKey.c_str());
478 continue;
479 }
480 // the condition below catches the cases (a) and (b) but not (c):
481 // (a) global key found second time in global file
482 // (b) key found second (or more) time in local file
483 // (c) key from global file now found in local one
484 // which is exactly what we want.
485 else if ( !bLocal || pEntry->IsLocal() ) {
486 wxLogWarning(_("file '%s', line %d: key '%s' was first "
487 "found at line %d."),
488 file.GetName(), n + 1, strKey.c_str(), pEntry->Line());
489
490 if ( bLocal )
491 pEntry->SetLine(m_linesTail);
492 }
493 }
494
495 // skip whitespace
496 while ( wxIsspace(*pEnd) )
497 pEnd++;
498
499 pEntry->SetValue(FilterInValue(pEnd), FALSE /* read from file */);
500 }
501 }
502 }
503 }
504
505 // ----------------------------------------------------------------------------
506 // set/retrieve path
507 // ----------------------------------------------------------------------------
508
509 void wxFileConfig::SetRootPath()
510 {
511 m_strPath.Empty();
512 m_pCurrentGroup = m_pRootGroup;
513 }
514
515 void wxFileConfig::SetPath(const wxString& strPath)
516 {
517 wxArrayString aParts;
518
519 if ( strPath.IsEmpty() ) {
520 SetRootPath();
521 return;
522 }
523
524 if ( strPath[0] == wxCONFIG_PATH_SEPARATOR ) {
525 // absolute path
526 wxSplitPath(aParts, strPath);
527 }
528 else {
529 // relative path, combine with current one
530 wxString strFullPath = m_strPath;
531 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
532 wxSplitPath(aParts, strFullPath);
533 }
534
535 // change current group
536 size_t n;
537 m_pCurrentGroup = m_pRootGroup;
538 for ( n = 0; n < aParts.Count(); n++ ) {
539 ConfigGroup *pNextGroup = m_pCurrentGroup->FindSubgroup(aParts[n]);
540 if ( pNextGroup == NULL )
541 pNextGroup = m_pCurrentGroup->AddSubgroup(aParts[n]);
542 m_pCurrentGroup = pNextGroup;
543 }
544
545 // recombine path parts in one variable
546 m_strPath.Empty();
547 for ( n = 0; n < aParts.Count(); n++ ) {
548 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
549 }
550 }
551
552 // ----------------------------------------------------------------------------
553 // enumeration
554 // ----------------------------------------------------------------------------
555
556 bool wxFileConfig::GetFirstGroup(wxString& str, long& lIndex) const
557 {
558 lIndex = 0;
559 return GetNextGroup(str, lIndex);
560 }
561
562 bool wxFileConfig::GetNextGroup (wxString& str, long& lIndex) const
563 {
564 if ( size_t(lIndex) < m_pCurrentGroup->Groups().Count() ) {
565 str = m_pCurrentGroup->Groups()[lIndex++]->Name();
566 return TRUE;
567 }
568 else
569 return FALSE;
570 }
571
572 bool wxFileConfig::GetFirstEntry(wxString& str, long& lIndex) const
573 {
574 lIndex = 0;
575 return GetNextEntry(str, lIndex);
576 }
577
578 bool wxFileConfig::GetNextEntry (wxString& str, long& lIndex) const
579 {
580 if ( size_t(lIndex) < m_pCurrentGroup->Entries().Count() ) {
581 str = m_pCurrentGroup->Entries()[lIndex++]->Name();
582 return TRUE;
583 }
584 else
585 return FALSE;
586 }
587
588 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive) const
589 {
590 size_t n = m_pCurrentGroup->Entries().Count();
591 if ( bRecursive ) {
592 ConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
593 size_t nSubgroups = m_pCurrentGroup->Groups().Count();
594 for ( size_t nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
595 CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
596 n += GetNumberOfEntries(TRUE);
597 CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
598 }
599 }
600
601 return n;
602 }
603
604 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive) const
605 {
606 size_t n = m_pCurrentGroup->Groups().Count();
607 if ( bRecursive ) {
608 ConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
609 size_t nSubgroups = m_pCurrentGroup->Groups().Count();
610 for ( size_t nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
611 CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
612 n += GetNumberOfGroups(TRUE);
613 CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
614 }
615 }
616
617 return n;
618 }
619
620 // ----------------------------------------------------------------------------
621 // tests for existence
622 // ----------------------------------------------------------------------------
623
624 bool wxFileConfig::HasGroup(const wxString& strName) const
625 {
626 wxConfigPathChanger path(this, strName);
627
628 ConfigGroup *pGroup = m_pCurrentGroup->FindSubgroup(path.Name());
629 return pGroup != NULL;
630 }
631
632 bool wxFileConfig::HasEntry(const wxString& strName) const
633 {
634 wxConfigPathChanger path(this, strName);
635
636 ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
637 return pEntry != NULL;
638 }
639
640 // ----------------------------------------------------------------------------
641 // read/write values
642 // ----------------------------------------------------------------------------
643
644 bool wxFileConfig::Read(const wxString& key,
645 wxString* pStr) const
646 {
647 wxConfigPathChanger path(this, key);
648
649 ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
650 if (pEntry == NULL) {
651 return FALSE;
652 }
653 else {
654 *pStr = ExpandEnvVars(pEntry->Value());
655 return TRUE;
656 }
657 }
658
659 bool wxFileConfig::Read(const wxString& key,
660 wxString* pStr, const wxString& defVal) const
661 {
662 wxConfigPathChanger path(this, key);
663
664 ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
665 if (pEntry == NULL) {
666 if( IsRecordingDefaults() )
667 ((wxFileConfig *)this)->Write(key,defVal);
668 *pStr = ExpandEnvVars(defVal);
669 return FALSE;
670 }
671 else {
672 *pStr = ExpandEnvVars(pEntry->Value());
673 return TRUE;
674 }
675 }
676
677 bool wxFileConfig::Read(const wxString& key, long *pl) const
678 {
679 wxString str;
680 if ( Read(key, & str) ) {
681 *pl = wxAtol(str);
682 return TRUE;
683 }
684 else {
685 return FALSE;
686 }
687 }
688
689 bool wxFileConfig::Write(const wxString& key, const wxString& szValue)
690 {
691 wxConfigPathChanger path(this, key);
692
693 wxString strName = path.Name();
694 if ( strName.IsEmpty() ) {
695 // setting the value of a group is an error
696 wxASSERT_MSG( wxIsEmpty(szValue), wxT("can't set value of a group!") );
697
698 // ... except if it's empty in which case it's a way to force it's creation
699 m_pCurrentGroup->SetDirty();
700
701 // this will add a line for this group if it didn't have it before
702 (void)m_pCurrentGroup->GetGroupLine();
703 }
704 else {
705 // writing an entry
706
707 // check that the name is reasonable
708 if ( strName[0u] == wxCONFIG_IMMUTABLE_PREFIX ) {
709 wxLogError(_("Config entry name cannot start with '%c'."),
710 wxCONFIG_IMMUTABLE_PREFIX);
711 return FALSE;
712 }
713
714 ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strName);
715 if ( pEntry == NULL )
716 pEntry = m_pCurrentGroup->AddEntry(strName);
717
718 pEntry->SetValue(szValue);
719 }
720
721 return TRUE;
722 }
723
724 bool wxFileConfig::Write(const wxString& key, long lValue)
725 {
726 // ltoa() is not ANSI :-(
727 wxString buf;
728 buf.Printf(wxT("%ld"), lValue);
729 return Write(key, buf);
730 }
731
732 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
733 {
734 if ( LineListIsEmpty() || !m_pRootGroup->IsDirty() )
735 return TRUE;
736
737 wxTempFile file(m_strLocalFile);
738
739 if ( !file.IsOpened() ) {
740 wxLogError(_("can't open user configuration file."));
741 return FALSE;
742 }
743
744 // write all strings to file
745 for ( LineList *p = m_linesHead; p != NULL; p = p->Next() ) {
746 if ( !file.Write(p->Text() + wxTextFile::GetEOL()) ) {
747 wxLogError(_("can't write user configuration file."));
748 return FALSE;
749 }
750 }
751
752 return file.Commit();
753 }
754
755 // ----------------------------------------------------------------------------
756 // renaming groups/entries
757 // ----------------------------------------------------------------------------
758
759 bool wxFileConfig::RenameEntry(const wxString& oldName,
760 const wxString& newName)
761 {
762 // check that the entry exists
763 ConfigEntry *oldEntry = m_pCurrentGroup->FindEntry(oldName);
764 if ( !oldEntry )
765 return FALSE;
766
767 // check that the new entry doesn't already exist
768 if ( m_pCurrentGroup->FindEntry(newName) )
769 return FALSE;
770
771 // delete the old entry, create the new one
772 wxString value = oldEntry->Value();
773 if ( !m_pCurrentGroup->DeleteEntry(oldName) )
774 return FALSE;
775
776 ConfigEntry *newEntry = m_pCurrentGroup->AddEntry(newName);
777 newEntry->SetValue(value);
778
779 return TRUE;
780 }
781
782 bool wxFileConfig::RenameGroup(const wxString& oldName,
783 const wxString& newName)
784 {
785 // check that the group exists
786 ConfigGroup *group = m_pCurrentGroup->FindSubgroup(oldName);
787 if ( !group )
788 return FALSE;
789
790 // check that the new group doesn't already exist
791 if ( m_pCurrentGroup->FindSubgroup(newName) )
792 return FALSE;
793
794 group->Rename(newName);
795
796 return TRUE;
797 }
798
799 // ----------------------------------------------------------------------------
800 // delete groups/entries
801 // ----------------------------------------------------------------------------
802
803 bool wxFileConfig::DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso)
804 {
805 wxConfigPathChanger path(this, key);
806
807 if ( !m_pCurrentGroup->DeleteEntry(path.Name()) )
808 return FALSE;
809
810 if ( bGroupIfEmptyAlso && m_pCurrentGroup->IsEmpty() ) {
811 if ( m_pCurrentGroup != m_pRootGroup ) {
812 ConfigGroup *pGroup = m_pCurrentGroup;
813 SetPath(wxT("..")); // changes m_pCurrentGroup!
814 m_pCurrentGroup->DeleteSubgroupByName(pGroup->Name());
815 }
816 //else: never delete the root group
817 }
818
819 return TRUE;
820 }
821
822 bool wxFileConfig::DeleteGroup(const wxString& key)
823 {
824 wxConfigPathChanger path(this, key);
825
826 return m_pCurrentGroup->DeleteSubgroupByName(path.Name());
827 }
828
829 bool wxFileConfig::DeleteAll()
830 {
831 CleanUp();
832
833 if ( remove(m_strLocalFile.fn_str()) == -1 )
834 wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile.c_str());
835
836 m_strLocalFile = m_strGlobalFile = wxT("");
837 Init();
838
839 return TRUE;
840 }
841
842 // ----------------------------------------------------------------------------
843 // linked list functions
844 // ----------------------------------------------------------------------------
845
846 // append a new line to the end of the list
847 LineList *wxFileConfig::LineListAppend(const wxString& str)
848 {
849 LineList *pLine = new LineList(str);
850
851 if ( m_linesTail == NULL ) {
852 // list is empty
853 m_linesHead = pLine;
854 }
855 else {
856 // adjust pointers
857 m_linesTail->SetNext(pLine);
858 pLine->SetPrev(m_linesTail);
859 }
860
861 m_linesTail = pLine;
862 return m_linesTail;
863 }
864
865 // insert a new line after the given one or in the very beginning if !pLine
866 LineList *wxFileConfig::LineListInsert(const wxString& str,
867 LineList *pLine)
868 {
869 if ( pLine == m_linesTail )
870 return LineListAppend(str);
871
872 LineList *pNewLine = new LineList(str);
873 if ( pLine == NULL ) {
874 // prepend to the list
875 pNewLine->SetNext(m_linesHead);
876 m_linesHead->SetPrev(pNewLine);
877 m_linesHead = pNewLine;
878 }
879 else {
880 // insert before pLine
881 LineList *pNext = pLine->Next();
882 pNewLine->SetNext(pNext);
883 pNewLine->SetPrev(pLine);
884 pNext->SetPrev(pNewLine);
885 pLine->SetNext(pNewLine);
886 }
887
888 return pNewLine;
889 }
890
891 void wxFileConfig::LineListRemove(LineList *pLine)
892 {
893 LineList *pPrev = pLine->Prev(),
894 *pNext = pLine->Next();
895
896 // first entry?
897 if ( pPrev == NULL )
898 m_linesHead = pNext;
899 else
900 pPrev->SetNext(pNext);
901
902 // last entry?
903 if ( pNext == NULL )
904 m_linesTail = pPrev;
905 else
906 pNext->SetPrev(pPrev);
907
908 delete pLine;
909 }
910
911 bool wxFileConfig::LineListIsEmpty()
912 {
913 return m_linesHead == NULL;
914 }
915
916 // ============================================================================
917 // wxFileConfig::ConfigGroup
918 // ============================================================================
919
920 // ----------------------------------------------------------------------------
921 // ctor/dtor
922 // ----------------------------------------------------------------------------
923
924 // ctor
925 ConfigGroup::ConfigGroup(ConfigGroup *pParent,
926 const wxString& strName,
927 wxFileConfig *pConfig)
928 : m_aEntries(CompareEntries),
929 m_aSubgroups(CompareGroups),
930 m_strName(strName)
931 {
932 m_pConfig = pConfig;
933 m_pParent = pParent;
934 m_bDirty = FALSE;
935 m_pLine = NULL;
936
937 m_pLastEntry = NULL;
938 m_pLastGroup = NULL;
939 }
940
941 // dtor deletes all children
942 ConfigGroup::~ConfigGroup()
943 {
944 // entries
945 size_t n, nCount = m_aEntries.Count();
946 for ( n = 0; n < nCount; n++ )
947 delete m_aEntries[n];
948
949 // subgroups
950 nCount = m_aSubgroups.Count();
951 for ( n = 0; n < nCount; n++ )
952 delete m_aSubgroups[n];
953 }
954
955 // ----------------------------------------------------------------------------
956 // line
957 // ----------------------------------------------------------------------------
958
959 void ConfigGroup::SetLine(LineList *pLine)
960 {
961 wxASSERT( m_pLine == NULL ); // shouldn't be called twice
962
963 m_pLine = pLine;
964 }
965
966 /*
967 This is a bit complicated, so let me explain it in details. All lines that
968 were read from the local file (the only one we will ever modify) are stored
969 in a (doubly) linked list. Our problem is to know at which position in this
970 list should we insert the new entries/subgroups. To solve it we keep three
971 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
972
973 m_pLine points to the line containing "[group_name]"
974 m_pLastEntry points to the last entry of this group in the local file.
975 m_pLastGroup subgroup
976
977 Initially, they're NULL all three. When the group (an entry/subgroup) is read
978 from the local file, the corresponding variable is set. However, if the group
979 was read from the global file and then modified or created by the application
980 these variables are still NULL and we need to create the corresponding lines.
981 See the following functions (and comments preceding them) for the details of
982 how we do it.
983
984 Also, when our last entry/group are deleted we need to find the new last
985 element - the code in DeleteEntry/Subgroup does this by backtracking the list
986 of lines until it either founds an entry/subgroup (and this is the new last
987 element) or the m_pLine of the group, in which case there are no more entries
988 (or subgroups) left and m_pLast<element> becomes NULL.
989
990 NB: This last problem could be avoided for entries if we added new entries
991 immediately after m_pLine, but in this case the entries would appear
992 backwards in the config file (OTOH, it's not that important) and as we
993 would still need to do it for the subgroups the code wouldn't have been
994 significantly less complicated.
995 */
996
997 // Return the line which contains "[our name]". If we're still not in the list,
998 // add our line to it immediately after the last line of our parent group if we
999 // have it or in the very beginning if we're the root group.
1000 LineList *ConfigGroup::GetGroupLine()
1001 {
1002 if ( m_pLine == NULL ) {
1003 ConfigGroup *pParent = Parent();
1004
1005 // this group wasn't present in local config file, add it now
1006 if ( pParent != NULL ) {
1007 wxString strFullName;
1008 strFullName << wxT("[")
1009 // +1: no '/'
1010 << FilterOutEntryName(GetFullName().c_str() + 1)
1011 << wxT("]");
1012 m_pLine = m_pConfig->LineListInsert(strFullName,
1013 pParent->GetLastGroupLine());
1014 pParent->SetLastGroup(this); // we're surely after all the others
1015 }
1016 else {
1017 // we return NULL, so that LineListInsert() will insert us in the
1018 // very beginning
1019 }
1020 }
1021
1022 return m_pLine;
1023 }
1024
1025 // Return the last line belonging to the subgroups of this group (after which
1026 // we can add a new subgroup), if we don't have any subgroups or entries our
1027 // last line is the group line (m_pLine) itself.
1028 LineList *ConfigGroup::GetLastGroupLine()
1029 {
1030 // if we have any subgroups, our last line is the last line of the last
1031 // subgroup
1032 if ( m_pLastGroup != NULL ) {
1033 LineList *pLine = m_pLastGroup->GetLastGroupLine();
1034
1035 wxASSERT( pLine != NULL ); // last group must have !NULL associated line
1036 return pLine;
1037 }
1038
1039 // no subgroups, so the last line is the line of thelast entry (if any)
1040 return GetLastEntryLine();
1041 }
1042
1043 // return the last line belonging to the entries of this group (after which
1044 // we can add a new entry), if we don't have any entries we will add the new
1045 // one immediately after the group line itself.
1046 LineList *ConfigGroup::GetLastEntryLine()
1047 {
1048 if ( m_pLastEntry != NULL ) {
1049 LineList *pLine = m_pLastEntry->GetLine();
1050
1051 wxASSERT( pLine != NULL ); // last entry must have !NULL associated line
1052 return pLine;
1053 }
1054
1055 // no entries: insert after the group header
1056 return GetGroupLine();
1057 }
1058
1059 // ----------------------------------------------------------------------------
1060 // group name
1061 // ----------------------------------------------------------------------------
1062
1063 void ConfigGroup::Rename(const wxString& newName)
1064 {
1065 m_strName = newName;
1066
1067 LineList *line = GetGroupLine();
1068 wxString strFullName;
1069 strFullName << wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); // +1: no '/'
1070 line->SetText(strFullName);
1071
1072 SetDirty();
1073 }
1074
1075 wxString ConfigGroup::GetFullName() const
1076 {
1077 if ( Parent() )
1078 return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR + Name();
1079 else
1080 return wxT("");
1081 }
1082
1083 // ----------------------------------------------------------------------------
1084 // find an item
1085 // ----------------------------------------------------------------------------
1086
1087 // use binary search because the array is sorted
1088 ConfigEntry *
1089 ConfigGroup::FindEntry(const wxChar *szName) const
1090 {
1091 size_t i,
1092 lo = 0,
1093 hi = m_aEntries.Count();
1094 int res;
1095 ConfigEntry *pEntry;
1096
1097 while ( lo < hi ) {
1098 i = (lo + hi)/2;
1099 pEntry = m_aEntries[i];
1100
1101 #if wxCONFIG_CASE_SENSITIVE
1102 res = wxStrcmp(pEntry->Name(), szName);
1103 #else
1104 res = wxStricmp(pEntry->Name(), szName);
1105 #endif
1106
1107 if ( res > 0 )
1108 hi = i;
1109 else if ( res < 0 )
1110 lo = i + 1;
1111 else
1112 return pEntry;
1113 }
1114
1115 return NULL;
1116 }
1117
1118 ConfigGroup *
1119 ConfigGroup::FindSubgroup(const wxChar *szName) const
1120 {
1121 size_t i,
1122 lo = 0,
1123 hi = m_aSubgroups.Count();
1124 int res;
1125 ConfigGroup *pGroup;
1126
1127 while ( lo < hi ) {
1128 i = (lo + hi)/2;
1129 pGroup = m_aSubgroups[i];
1130
1131 #if wxCONFIG_CASE_SENSITIVE
1132 res = wxStrcmp(pGroup->Name(), szName);
1133 #else
1134 res = wxStricmp(pGroup->Name(), szName);
1135 #endif
1136
1137 if ( res > 0 )
1138 hi = i;
1139 else if ( res < 0 )
1140 lo = i + 1;
1141 else
1142 return pGroup;
1143 }
1144
1145 return NULL;
1146 }
1147
1148 // ----------------------------------------------------------------------------
1149 // create a new item
1150 // ----------------------------------------------------------------------------
1151
1152 // create a new entry and add it to the current group
1153 ConfigEntry *
1154 ConfigGroup::AddEntry(const wxString& strName, int nLine)
1155 {
1156 wxASSERT( FindEntry(strName) == NULL );
1157
1158 ConfigEntry *pEntry = new ConfigEntry(this, strName, nLine);
1159 m_aEntries.Add(pEntry);
1160
1161 return pEntry;
1162 }
1163
1164 // create a new group and add it to the current group
1165 ConfigGroup *
1166 ConfigGroup::AddSubgroup(const wxString& strName)
1167 {
1168 wxASSERT( FindSubgroup(strName) == NULL );
1169
1170 ConfigGroup *pGroup = new ConfigGroup(this, strName, m_pConfig);
1171 m_aSubgroups.Add(pGroup);
1172
1173 return pGroup;
1174 }
1175
1176 // ----------------------------------------------------------------------------
1177 // delete an item
1178 // ----------------------------------------------------------------------------
1179
1180 /*
1181 The delete operations are _very_ slow if we delete the last item of this
1182 group (see comments before GetXXXLineXXX functions for more details),
1183 so it's much better to start with the first entry/group if we want to
1184 delete several of them.
1185 */
1186
1187 bool ConfigGroup::DeleteSubgroupByName(const wxChar *szName)
1188 {
1189 return DeleteSubgroup(FindSubgroup(szName));
1190 }
1191
1192 // doesn't delete the subgroup itself, but does remove references to it from
1193 // all other data structures (and normally the returned pointer should be
1194 // deleted a.s.a.p. because there is nothing much to be done with it anyhow)
1195 bool ConfigGroup::DeleteSubgroup(ConfigGroup *pGroup)
1196 {
1197 wxCHECK( pGroup != NULL, FALSE ); // deleting non existing group?
1198
1199 // delete all entries
1200 size_t nCount = pGroup->m_aEntries.Count();
1201 for ( size_t nEntry = 0; nEntry < nCount; nEntry++ ) {
1202 LineList *pLine = pGroup->m_aEntries[nEntry]->GetLine();
1203 if ( pLine != NULL )
1204 m_pConfig->LineListRemove(pLine);
1205 }
1206
1207 // and subgroups of this sungroup
1208 nCount = pGroup->m_aSubgroups.Count();
1209 for ( size_t nGroup = 0; nGroup < nCount; nGroup++ ) {
1210 pGroup->DeleteSubgroup(pGroup->m_aSubgroups[0]);
1211 }
1212
1213 LineList *pLine = pGroup->m_pLine;
1214 if ( pLine != NULL ) {
1215 // notice that we may do this test inside the previous "if" because the
1216 // last entry's line is surely !NULL
1217 if ( pGroup == m_pLastGroup ) {
1218 // our last entry is being deleted - find the last one which stays
1219 wxASSERT( m_pLine != NULL ); // we have a subgroup with !NULL pLine...
1220
1221 // go back until we find a subgroup or reach the group's line
1222 ConfigGroup *pNewLast = NULL;
1223 size_t n, nSubgroups = m_aSubgroups.Count();
1224 LineList *pl;
1225 for ( pl = pLine->Prev(); pl != m_pLine; pl = pl->Prev() ) {
1226 // is it our subgroup?
1227 for ( n = 0; (pNewLast == NULL) && (n < nSubgroups); n++ ) {
1228 // do _not_ call GetGroupLine! we don't want to add it to the local
1229 // file if it's not already there
1230 if ( m_aSubgroups[n]->m_pLine == m_pLine )
1231 pNewLast = m_aSubgroups[n];
1232 }
1233
1234 if ( pNewLast != NULL ) // found?
1235 break;
1236 }
1237
1238 if ( pl == m_pLine ) {
1239 wxASSERT( !pNewLast ); // how comes it has the same line as we?
1240
1241 // we've reached the group line without finding any subgroups
1242 m_pLastGroup = NULL;
1243 }
1244 else
1245 m_pLastGroup = pNewLast;
1246 }
1247
1248 m_pConfig->LineListRemove(pLine);
1249 }
1250
1251 SetDirty();
1252
1253 m_aSubgroups.Remove(pGroup);
1254 delete pGroup;
1255
1256 return TRUE;
1257 }
1258
1259 bool ConfigGroup::DeleteEntry(const wxChar *szName)
1260 {
1261 ConfigEntry *pEntry = FindEntry(szName);
1262 wxCHECK( pEntry != NULL, FALSE ); // deleting non existing item?
1263
1264 LineList *pLine = pEntry->GetLine();
1265 if ( pLine != NULL ) {
1266 // notice that we may do this test inside the previous "if" because the
1267 // last entry's line is surely !NULL
1268 if ( pEntry == m_pLastEntry ) {
1269 // our last entry is being deleted - find the last one which stays
1270 wxASSERT( m_pLine != NULL ); // if we have an entry with !NULL pLine...
1271
1272 // go back until we find another entry or reach the group's line
1273 ConfigEntry *pNewLast = NULL;
1274 size_t n, nEntries = m_aEntries.Count();
1275 LineList *pl;
1276 for ( pl = pLine->Prev(); pl != m_pLine; pl = pl->Prev() ) {
1277 // is it our subgroup?
1278 for ( n = 0; (pNewLast == NULL) && (n < nEntries); n++ ) {
1279 if ( m_aEntries[n]->GetLine() == m_pLine )
1280 pNewLast = m_aEntries[n];
1281 }
1282
1283 if ( pNewLast != NULL ) // found?
1284 break;
1285 }
1286
1287 if ( pl == m_pLine ) {
1288 wxASSERT( !pNewLast ); // how comes it has the same line as we?
1289
1290 // we've reached the group line without finding any subgroups
1291 m_pLastEntry = NULL;
1292 }
1293 else
1294 m_pLastEntry = pNewLast;
1295 }
1296
1297 m_pConfig->LineListRemove(pLine);
1298 }
1299
1300 // we must be written back for the changes to be saved
1301 SetDirty();
1302
1303 m_aEntries.Remove(pEntry);
1304 delete pEntry;
1305
1306 return TRUE;
1307 }
1308
1309 // ----------------------------------------------------------------------------
1310 //
1311 // ----------------------------------------------------------------------------
1312 void ConfigGroup::SetDirty()
1313 {
1314 m_bDirty = TRUE;
1315 if ( Parent() != NULL ) // propagate upwards
1316 Parent()->SetDirty();
1317 }
1318
1319 // ============================================================================
1320 // wxFileConfig::ConfigEntry
1321 // ============================================================================
1322
1323 // ----------------------------------------------------------------------------
1324 // ctor
1325 // ----------------------------------------------------------------------------
1326 ConfigEntry::ConfigEntry(ConfigGroup *pParent,
1327 const wxString& strName,
1328 int nLine)
1329 : m_strName(strName)
1330 {
1331 wxASSERT( !strName.IsEmpty() );
1332
1333 m_pParent = pParent;
1334 m_nLine = nLine;
1335 m_pLine = NULL;
1336
1337 m_bDirty = FALSE;
1338
1339 m_bImmutable = strName[0] == wxCONFIG_IMMUTABLE_PREFIX;
1340 if ( m_bImmutable )
1341 m_strName.erase(0, 1); // remove first character
1342 }
1343
1344 // ----------------------------------------------------------------------------
1345 // set value
1346 // ----------------------------------------------------------------------------
1347
1348 void ConfigEntry::SetLine(LineList *pLine)
1349 {
1350 if ( m_pLine != NULL ) {
1351 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1352 Name().c_str(), m_pParent->GetFullName().c_str());
1353 }
1354
1355 m_pLine = pLine;
1356 Group()->SetLastEntry(this);
1357 }
1358
1359 // second parameter is FALSE if we read the value from file and prevents the
1360 // entry from being marked as 'dirty'
1361 void ConfigEntry::SetValue(const wxString& strValue, bool bUser)
1362 {
1363 if ( bUser && IsImmutable() ) {
1364 wxLogWarning(_("attempt to change immutable key '%s' ignored."),
1365 Name().c_str());
1366 return;
1367 }
1368
1369 // do nothing if it's the same value
1370 if ( strValue == m_strValue )
1371 return;
1372
1373 m_strValue = strValue;
1374
1375 if ( bUser ) {
1376 wxString strVal = FilterOutValue(strValue);
1377 wxString strLine;
1378 strLine << FilterOutEntryName(m_strName) << wxT('=') << strVal;
1379
1380 if ( m_pLine != NULL ) {
1381 // entry was read from the local config file, just modify the line
1382 m_pLine->SetText(strLine);
1383 }
1384 else {
1385 // add a new line to the file
1386 wxASSERT( m_nLine == wxNOT_FOUND ); // consistency check
1387
1388 m_pLine = Group()->Config()->LineListInsert(strLine,
1389 Group()->GetLastEntryLine());
1390 Group()->SetLastEntry(this);
1391 }
1392
1393 SetDirty();
1394 }
1395 }
1396
1397 void ConfigEntry::SetDirty()
1398 {
1399 m_bDirty = TRUE;
1400 Group()->SetDirty();
1401 }
1402
1403 // ============================================================================
1404 // global functions
1405 // ============================================================================
1406
1407 // ----------------------------------------------------------------------------
1408 // compare functions for array sorting
1409 // ----------------------------------------------------------------------------
1410
1411 int CompareEntries(ConfigEntry *p1,
1412 ConfigEntry *p2)
1413 {
1414 #if wxCONFIG_CASE_SENSITIVE
1415 return wxStrcmp(p1->Name(), p2->Name());
1416 #else
1417 return wxStricmp(p1->Name(), p2->Name());
1418 #endif
1419 }
1420
1421 int CompareGroups(ConfigGroup *p1,
1422 ConfigGroup *p2)
1423 {
1424 #if wxCONFIG_CASE_SENSITIVE
1425 return wxStrcmp(p1->Name(), p2->Name());
1426 #else
1427 return wxStricmp(p1->Name(), p2->Name());
1428 #endif
1429 }
1430
1431 // ----------------------------------------------------------------------------
1432 // filter functions
1433 // ----------------------------------------------------------------------------
1434
1435 // undo FilterOutValue
1436 static wxString FilterInValue(const wxString& str)
1437 {
1438 wxString strResult;
1439 strResult.Alloc(str.Len());
1440
1441 bool bQuoted = !str.IsEmpty() && str[0] == '"';
1442
1443 for ( size_t n = bQuoted ? 1 : 0; n < str.Len(); n++ ) {
1444 if ( str[n] == wxT('\\') ) {
1445 switch ( str[++n] ) {
1446 case wxT('n'):
1447 strResult += wxT('\n');
1448 break;
1449
1450 case wxT('r'):
1451 strResult += wxT('\r');
1452 break;
1453
1454 case wxT('t'):
1455 strResult += wxT('\t');
1456 break;
1457
1458 case wxT('\\'):
1459 strResult += wxT('\\');
1460 break;
1461
1462 case wxT('"'):
1463 strResult += wxT('"');
1464 break;
1465 }
1466 }
1467 else {
1468 if ( str[n] != wxT('"') || !bQuoted )
1469 strResult += str[n];
1470 else if ( n != str.Len() - 1 ) {
1471 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1472 n, str.c_str());
1473 }
1474 //else: it's the last quote of a quoted string, ok
1475 }
1476 }
1477
1478 return strResult;
1479 }
1480
1481 // quote the string before writing it to file
1482 static wxString FilterOutValue(const wxString& str)
1483 {
1484 if ( !str )
1485 return str;
1486
1487 wxString strResult;
1488 strResult.Alloc(str.Len());
1489
1490 // quoting is necessary to preserve spaces in the beginning of the string
1491 bool bQuote = wxIsspace(str[0]) || str[0] == wxT('"');
1492
1493 if ( bQuote )
1494 strResult += wxT('"');
1495
1496 wxChar c;
1497 for ( size_t n = 0; n < str.Len(); n++ ) {
1498 switch ( str[n] ) {
1499 case wxT('\n'):
1500 c = wxT('n');
1501 break;
1502
1503 case wxT('\r'):
1504 c = wxT('r');
1505 break;
1506
1507 case wxT('\t'):
1508 c = wxT('t');
1509 break;
1510
1511 case wxT('\\'):
1512 c = wxT('\\');
1513 break;
1514
1515 case wxT('"'):
1516 if ( bQuote ) {
1517 c = wxT('"');
1518 break;
1519 }
1520 //else: fall through
1521
1522 default:
1523 strResult += str[n];
1524 continue; // nothing special to do
1525 }
1526
1527 // we get here only for special characters
1528 strResult << wxT('\\') << c;
1529 }
1530
1531 if ( bQuote )
1532 strResult += wxT('"');
1533
1534 return strResult;
1535 }
1536
1537 // undo FilterOutEntryName
1538 static wxString FilterInEntryName(const wxString& str)
1539 {
1540 wxString strResult;
1541 strResult.Alloc(str.Len());
1542
1543 for ( const wxChar *pc = str.c_str(); *pc != '\0'; pc++ ) {
1544 if ( *pc == wxT('\\') )
1545 pc++;
1546
1547 strResult += *pc;
1548 }
1549
1550 return strResult;
1551 }
1552
1553 // sanitize entry or group name: insert '\\' before any special characters
1554 static wxString FilterOutEntryName(const wxString& str)
1555 {
1556 wxString strResult;
1557 strResult.Alloc(str.Len());
1558
1559 for ( const wxChar *pc = str.c_str(); *pc != wxT('\0'); pc++ ) {
1560 wxChar c = *pc;
1561
1562 // we explicitly allow some of "safe" chars and 8bit ASCII characters
1563 // which will probably never have special meaning
1564 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
1565 // should *not* be quoted
1566 if ( !wxIsalnum(c) && !wxStrchr(wxT("@_/-!.*%"), c) && ((c & 0x80) == 0) )
1567 strResult += wxT('\\');
1568
1569 strResult += c;
1570 }
1571
1572 return strResult;
1573 }
1574
1575 // we can't put ?: in the ctor initializer list because it confuses some
1576 // broken compilers (Borland C++)
1577 static wxString GetAppName(const wxString& appName)
1578 {
1579 if ( !appName && wxTheApp )
1580 return wxTheApp->GetAppName();
1581 else
1582 return appName;
1583 }
1584
1585 #endif // wxUSE_CONFIG
1586