]> git.saurik.com Git - wxWidgets.git/blob - src/common/fileconf.cpp
moved wxapp_install_idle_handler and g_isIdle from many cpp files into gtk/private...
[wxWidgets.git] / src / common / fileconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif //__BORLANDC__
22
23 #if wxUSE_CONFIG && wxUSE_FILECONFIG
24
25 #ifndef WX_PRECOMP
26 #include "wx/string.h"
27 #include "wx/intl.h"
28 #endif //WX_PRECOMP
29
30 #include "wx/app.h"
31 #include "wx/dynarray.h"
32 #include "wx/file.h"
33 #include "wx/log.h"
34 #include "wx/textfile.h"
35 #include "wx/memtext.h"
36 #include "wx/config.h"
37 #include "wx/fileconf.h"
38 #include "wx/filefn.h"
39
40 #if wxUSE_STREAMS
41 #include "wx/stream.h"
42 #endif // wxUSE_STREAMS
43
44 #include "wx/utils.h" // for wxGetHomeDir
45
46 #if defined(__WXMAC__)
47 #include "wx/mac/private.h" // includes mac headers
48 #include "wx/filename.h" // for MacSetTypeAndCreator
49 #endif
50
51 #if defined(__WXMSW__)
52 #include "wx/msw/private.h"
53 #endif //windows.h
54 #if defined(__WXPM__)
55 #define INCL_DOS
56 #include <os2.h>
57 #endif
58
59 #include <stdlib.h>
60 #include <ctype.h>
61
62 // ----------------------------------------------------------------------------
63 // macros
64 // ----------------------------------------------------------------------------
65 #define CONST_CAST ((wxFileConfig *)this)->
66
67 // ----------------------------------------------------------------------------
68 // constants
69 // ----------------------------------------------------------------------------
70
71 #ifndef MAX_PATH
72 #define MAX_PATH 512
73 #endif
74
75 #define FILECONF_TRACE_MASK _T("fileconf")
76
77 // ----------------------------------------------------------------------------
78 // global functions declarations
79 // ----------------------------------------------------------------------------
80
81 // compare functions for sorting the arrays
82 static int LINKAGEMODE CompareEntries(wxFileConfigEntry *p1, wxFileConfigEntry *p2);
83 static int LINKAGEMODE CompareGroups(wxFileConfigGroup *p1, wxFileConfigGroup *p2);
84
85 // filter strings
86 static wxString FilterInValue(const wxString& str);
87 static wxString FilterOutValue(const wxString& str);
88
89 static wxString FilterInEntryName(const wxString& str);
90 static wxString FilterOutEntryName(const wxString& str);
91
92 // get the name to use in wxFileConfig ctor
93 static wxString GetAppName(const wxString& appname);
94
95 // ============================================================================
96 // private classes
97 // ============================================================================
98
99 // ----------------------------------------------------------------------------
100 // "template" array types
101 // ----------------------------------------------------------------------------
102
103 #ifdef WXMAKINGDLL_BASE
104 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigEntry *, ArrayEntries,
105 WXDLLIMPEXP_BASE);
106 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxFileConfigGroup *, ArrayGroups,
107 WXDLLIMPEXP_BASE);
108 #else
109 WX_DEFINE_SORTED_ARRAY(wxFileConfigEntry *, ArrayEntries);
110 WX_DEFINE_SORTED_ARRAY(wxFileConfigGroup *, ArrayGroups);
111 #endif
112
113 // ----------------------------------------------------------------------------
114 // wxFileConfigLineList
115 // ----------------------------------------------------------------------------
116
117 // we store all lines of the local config file as a linked list in memory
118 class wxFileConfigLineList
119 {
120 public:
121 void SetNext(wxFileConfigLineList *pNext) { m_pNext = pNext; }
122 void SetPrev(wxFileConfigLineList *pPrev) { m_pPrev = pPrev; }
123
124 // ctor
125 wxFileConfigLineList(const wxString& str,
126 wxFileConfigLineList *pNext = NULL) : m_strLine(str)
127 { SetNext(pNext); SetPrev(NULL); }
128
129 // next/prev nodes in the linked list
130 wxFileConfigLineList *Next() const { return m_pNext; }
131 wxFileConfigLineList *Prev() const { return m_pPrev; }
132
133 // get/change lines text
134 void SetText(const wxString& str) { m_strLine = str; }
135 const wxString& Text() const { return m_strLine; }
136
137 private:
138 wxString m_strLine; // line contents
139 wxFileConfigLineList *m_pNext, // next node
140 *m_pPrev; // previous one
141
142 DECLARE_NO_COPY_CLASS(wxFileConfigLineList)
143 };
144
145 // ----------------------------------------------------------------------------
146 // wxFileConfigEntry: a name/value pair
147 // ----------------------------------------------------------------------------
148
149 class wxFileConfigEntry
150 {
151 private:
152 wxFileConfigGroup *m_pParent; // group that contains us
153
154 wxString m_strName, // entry name
155 m_strValue; // value
156 bool m_bImmutable:1, // can be overriden locally?
157 m_bHasValue:1; // set after first call to SetValue()
158
159 int m_nLine; // used if m_pLine == NULL only
160
161 // pointer to our line in the linked list or NULL if it was found in global
162 // file (which we don't modify)
163 wxFileConfigLineList *m_pLine;
164
165 public:
166 wxFileConfigEntry(wxFileConfigGroup *pParent,
167 const wxString& strName, int nLine);
168
169 // simple accessors
170 const wxString& Name() const { return m_strName; }
171 const wxString& Value() const { return m_strValue; }
172 wxFileConfigGroup *Group() const { return m_pParent; }
173 bool IsImmutable() const { return m_bImmutable; }
174 bool IsLocal() const { return m_pLine != 0; }
175 int Line() const { return m_nLine; }
176 wxFileConfigLineList *
177 GetLine() const { return m_pLine; }
178
179 // modify entry attributes
180 void SetValue(const wxString& strValue, bool bUser = true);
181 void SetLine(wxFileConfigLineList *pLine);
182
183 DECLARE_NO_COPY_CLASS(wxFileConfigEntry)
184 };
185
186 // ----------------------------------------------------------------------------
187 // wxFileConfigGroup: container of entries and other groups
188 // ----------------------------------------------------------------------------
189
190 class wxFileConfigGroup
191 {
192 private:
193 wxFileConfig *m_pConfig; // config object we belong to
194 wxFileConfigGroup *m_pParent; // parent group (NULL for root group)
195 ArrayEntries m_aEntries; // entries in this group
196 ArrayGroups m_aSubgroups; // subgroups
197 wxString m_strName; // group's name
198 wxFileConfigLineList *m_pLine; // pointer to our line in the linked list
199 wxFileConfigEntry *m_pLastEntry; // last entry/subgroup of this group in the
200 wxFileConfigGroup *m_pLastGroup; // local file (we insert new ones after it)
201
202 // DeleteSubgroupByName helper
203 bool DeleteSubgroup(wxFileConfigGroup *pGroup);
204
205 // used by Rename()
206 void UpdateGroupAndSubgroupsLines();
207
208 public:
209 // ctor
210 wxFileConfigGroup(wxFileConfigGroup *pParent, const wxString& strName, wxFileConfig *);
211
212 // dtor deletes all entries and subgroups also
213 ~wxFileConfigGroup();
214
215 // simple accessors
216 const wxString& Name() const { return m_strName; }
217 wxFileConfigGroup *Parent() const { return m_pParent; }
218 wxFileConfig *Config() const { return m_pConfig; }
219
220 const ArrayEntries& Entries() const { return m_aEntries; }
221 const ArrayGroups& Groups() const { return m_aSubgroups; }
222 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
223
224 // find entry/subgroup (NULL if not found)
225 wxFileConfigGroup *FindSubgroup(const wxChar *szName) const;
226 wxFileConfigEntry *FindEntry (const wxChar *szName) const;
227
228 // delete entry/subgroup, return false if doesn't exist
229 bool DeleteSubgroupByName(const wxChar *szName);
230 bool DeleteEntry(const wxChar *szName);
231
232 // create new entry/subgroup returning pointer to newly created element
233 wxFileConfigGroup *AddSubgroup(const wxString& strName);
234 wxFileConfigEntry *AddEntry (const wxString& strName, int nLine = wxNOT_FOUND);
235
236 void SetLine(wxFileConfigLineList *pLine);
237
238 // rename: no checks are done to ensure that the name is unique!
239 void Rename(const wxString& newName);
240
241 //
242 wxString GetFullName() const;
243
244 // get the last line belonging to an entry/subgroup of this group
245 wxFileConfigLineList *GetGroupLine(); // line which contains [group]
246 wxFileConfigLineList *GetLastEntryLine(); // after which our subgroups start
247 wxFileConfigLineList *GetLastGroupLine(); // after which the next group starts
248
249 // called by entries/subgroups when they're created/deleted
250 void SetLastEntry(wxFileConfigEntry *pEntry);
251 void SetLastGroup(wxFileConfigGroup *pGroup)
252 { m_pLastGroup = pGroup; }
253
254 DECLARE_NO_COPY_CLASS(wxFileConfigGroup)
255 };
256
257 // ============================================================================
258 // implementation
259 // ============================================================================
260
261 // ----------------------------------------------------------------------------
262 // static functions
263 // ----------------------------------------------------------------------------
264 wxString wxFileConfig::GetGlobalDir()
265 {
266 wxString strDir;
267
268 #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
269 strDir = wxT("sys$manager:");
270 #elif defined(__WXMAC__)
271 strDir = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
272 #elif defined( __UNIX__ )
273 strDir = wxT("/etc/");
274 #elif defined(__OS2__)
275 ULONG aulSysInfo[QSV_MAX] = {0};
276 UINT drive;
277 APIRET rc;
278
279 rc = DosQuerySysInfo( 1L, QSV_MAX, (PVOID)aulSysInfo, sizeof(ULONG)*QSV_MAX);
280 if (rc == 0)
281 {
282 drive = aulSysInfo[QSV_BOOT_DRIVE - 1];
283 strDir.Printf(wxT("%c:\\OS2\\"), 'A'+drive-1);
284 }
285 #elif defined(__WXSTUBS__)
286 wxASSERT_MSG( false, wxT("TODO") ) ;
287 #elif defined(__DOS__)
288 // There's no such thing as global cfg dir in MS-DOS, let's return
289 // current directory (FIXME_MGL?)
290 strDir = wxT(".\\");
291 #elif defined(__WXWINCE__)
292 strDir = wxT("\\Windows\\");
293 #else // Windows
294
295 wxChar szWinDir[MAX_PATH];
296 ::GetWindowsDirectory(szWinDir, MAX_PATH);
297
298 strDir = szWinDir;
299 strDir << wxT('\\');
300 #endif // Unix/Windows
301
302 return strDir;
303 }
304
305 wxString wxFileConfig::GetLocalDir()
306 {
307 wxString strDir;
308
309 #if defined(__WXMAC__) || defined(__DOS__)
310 // no local dir concept on Mac OS 9 or MS-DOS
311 strDir << GetGlobalDir() ;
312 #else
313 wxGetHomeDir(&strDir);
314
315 #ifdef __UNIX__
316 if (
317 (strDir.Last() != wxT('/'))
318 #ifdef __VMS
319 && (strDir.Last() != wxT(']'))
320 #endif
321 )
322 strDir << wxT('/');
323 #else
324 if (strDir.Last() != wxT('\\'))
325 strDir << wxT('\\');
326 #endif
327 #endif
328
329 return strDir;
330 }
331
332 wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile)
333 {
334 wxString str = GetGlobalDir();
335 str << szFile;
336
337 if ( wxStrchr(szFile, wxT('.')) == NULL )
338 #if defined( __WXMAC__ )
339 str << wxT(" Preferences") ;
340 #elif defined( __UNIX__ )
341 str << wxT(".conf");
342 #else // Windows
343 str << wxT(".ini");
344 #endif // UNIX/Win
345
346 return str;
347 }
348
349 wxString wxFileConfig::GetLocalFileName(const wxChar *szFile)
350 {
351 #ifdef __VMS__
352 // On VMS I saw the problem that the home directory was appended
353 // twice for the configuration file. Does that also happen for
354 // other platforms?
355 wxString str = wxT( '.' );
356 #else
357 wxString str = GetLocalDir();
358 #endif
359
360 #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
361 str << wxT('.');
362 #endif
363
364 str << szFile;
365
366 #if defined(__WINDOWS__) || defined(__DOS__)
367 if ( wxStrchr(szFile, wxT('.')) == NULL )
368 str << wxT(".ini");
369 #endif
370
371 #ifdef __WXMAC__
372 str << wxT(" Preferences") ;
373 #endif
374
375 return str;
376 }
377
378 // ----------------------------------------------------------------------------
379 // ctor
380 // ----------------------------------------------------------------------------
381
382 void wxFileConfig::Init()
383 {
384 m_pCurrentGroup =
385 m_pRootGroup = new wxFileConfigGroup(NULL, wxEmptyString, this);
386
387 m_linesHead =
388 m_linesTail = NULL;
389
390 // It's not an error if (one of the) file(s) doesn't exist.
391
392 // parse the global file
393 if ( !m_strGlobalFile.empty() && wxFile::Exists(m_strGlobalFile) )
394 {
395 wxTextFile fileGlobal(m_strGlobalFile);
396
397 if ( fileGlobal.Open(m_conv/*ignored in ANSI build*/) )
398 {
399 Parse(fileGlobal, false /* global */);
400 SetRootPath();
401 }
402 else
403 {
404 wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile.c_str());
405 }
406 }
407
408 // parse the local file
409 if ( !m_strLocalFile.empty() && wxFile::Exists(m_strLocalFile) )
410 {
411 wxTextFile fileLocal(m_strLocalFile);
412 if ( fileLocal.Open(m_conv/*ignored in ANSI build*/) )
413 {
414 Parse(fileLocal, true /* local */);
415 SetRootPath();
416 }
417 else
418 {
419 wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile.c_str() );
420 }
421 }
422
423 m_isDirty = false;
424 }
425
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,
431 strLocal, strGlobal,
432 style),
433 m_strLocalFile(strLocal), m_strGlobalFile(strGlobal),
434 m_conv(conv)
435 {
436 // Make up names for files if empty
437 if ( m_strLocalFile.empty() && (style & wxCONFIG_USE_LOCAL_FILE) )
438 m_strLocalFile = GetLocalFileName(GetAppName());
439
440 if ( m_strGlobalFile.empty() && (style & wxCONFIG_USE_GLOBAL_FILE) )
441 m_strGlobalFile = GetGlobalFileName(GetAppName());
442
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);
447
448 if ( !m_strGlobalFile.empty() )
449 SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE);
450
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) )
454 {
455 if ( !m_strLocalFile.empty() && !wxIsAbsolutePath(m_strLocalFile) )
456 {
457 const wxString strLocalOrig = m_strLocalFile;
458 m_strLocalFile = GetLocalDir();
459 m_strLocalFile << strLocalOrig;
460 }
461
462 if ( !m_strGlobalFile.empty() && !wxIsAbsolutePath(m_strGlobalFile) )
463 {
464 const wxString strGlobalOrig = m_strGlobalFile;
465 m_strGlobalFile = GetGlobalDir();
466 m_strGlobalFile << strGlobalOrig;
467 }
468 }
469
470 SetUmask(-1);
471
472 Init();
473 }
474
475 #if wxUSE_STREAMS
476
477 wxFileConfig::wxFileConfig(wxInputStream &inStream, wxMBConv& conv)
478 : m_conv(conv)
479 {
480 // always local_file when this constructor is called (?)
481 SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE);
482
483 m_pCurrentGroup =
484 m_pRootGroup = new wxFileConfigGroup(NULL, wxEmptyString, this);
485
486 m_linesHead =
487 m_linesTail = NULL;
488
489 // translate everything to the current (platform-dependent) line
490 // termination character
491 wxString strTrans;
492 {
493 wxString strTmp;
494
495 char buf[1024];
496 do
497 {
498 inStream.Read(buf, WXSIZEOF(buf)-1); // leave room for the NULL
499
500 const wxStreamError err = inStream.GetLastError();
501
502 if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF )
503 {
504 wxLogError(_("Error reading config options."));
505 break;
506 }
507
508 // FIXME: this is broken because if we have part of multibyte
509 // character in the buffer (and another part hasn't been
510 // read yet) we're going to lose data because of conversion
511 // errors
512 buf[inStream.LastRead()] = '\0';
513 strTmp += conv.cMB2WX(buf);
514 }
515 while ( !inStream.Eof() );
516
517 strTrans = wxTextBuffer::Translate(strTmp);
518 }
519
520 wxMemoryText memText;
521
522 // Now we can add the text to the memory text. To do this we extract line
523 // by line from the translated string, until we've reached the end.
524 //
525 // VZ: all this is horribly inefficient, we should do the translation on
526 // the fly in one pass saving both memory and time (TODO)
527
528 const wxChar *pEOL = wxTextBuffer::GetEOL(wxTextBuffer::typeDefault);
529 const size_t EOLLen = wxStrlen(pEOL);
530
531 int posLineStart = strTrans.Find(pEOL);
532 while ( posLineStart != -1 )
533 {
534 wxString line(strTrans.Left(posLineStart));
535
536 memText.AddLine(line);
537
538 strTrans = strTrans.Mid(posLineStart + EOLLen);
539
540 posLineStart = strTrans.Find(pEOL);
541 }
542
543 // also add whatever we have left in the translated string.
544 if ( !strTrans.empty() )
545 memText.AddLine(strTrans);
546
547 // Finally we can parse it all.
548 Parse(memText, true /* local */);
549
550 SetRootPath();
551 ResetDirty();
552 }
553
554 #endif // wxUSE_STREAMS
555
556 void wxFileConfig::CleanUp()
557 {
558 delete m_pRootGroup;
559
560 wxFileConfigLineList *pCur = m_linesHead;
561 while ( pCur != NULL ) {
562 wxFileConfigLineList *pNext = pCur->Next();
563 delete pCur;
564 pCur = pNext;
565 }
566 }
567
568 wxFileConfig::~wxFileConfig()
569 {
570 Flush();
571
572 CleanUp();
573 }
574
575 // ----------------------------------------------------------------------------
576 // parse a config file
577 // ----------------------------------------------------------------------------
578
579 void wxFileConfig::Parse(const wxTextBuffer& buffer, bool bLocal)
580 {
581 const wxChar *pStart;
582 const wxChar *pEnd;
583 wxString strLine;
584
585 size_t nLineCount = buffer.GetLineCount();
586
587 for ( size_t n = 0; n < nLineCount; n++ )
588 {
589 strLine = buffer[n];
590
591 // add the line to linked list
592 if ( bLocal )
593 {
594 LineListAppend(strLine);
595
596 // let the root group have its start line as well
597 if ( !n )
598 {
599 m_pCurrentGroup->SetLine(m_linesTail);
600 }
601 }
602
603
604 // skip leading spaces
605 for ( pStart = strLine; wxIsspace(*pStart); pStart++ )
606 ;
607
608 // skip blank/comment lines
609 if ( *pStart == wxT('\0')|| *pStart == wxT(';') || *pStart == wxT('#') )
610 continue;
611
612 if ( *pStart == wxT('[') ) { // a new group
613 pEnd = pStart;
614
615 while ( *++pEnd != wxT(']') ) {
616 if ( *pEnd == wxT('\\') ) {
617 // the next char is escaped, so skip it even if it is ']'
618 pEnd++;
619 }
620
621 if ( *pEnd == wxT('\n') || *pEnd == wxT('\0') ) {
622 // we reached the end of line, break out of the loop
623 break;
624 }
625 }
626
627 if ( *pEnd != wxT(']') ) {
628 wxLogError(_("file '%s': unexpected character %c at line %d."),
629 buffer.GetName(), *pEnd, n + 1);
630 continue; // skip this line
631 }
632
633 // group name here is always considered as abs path
634 wxString strGroup;
635 pStart++;
636 strGroup << wxCONFIG_PATH_SEPARATOR
637 << FilterInEntryName(wxString(pStart, pEnd - pStart));
638
639 // will create it if doesn't yet exist
640 SetPath(strGroup);
641
642 if ( bLocal )
643 {
644 if ( m_pCurrentGroup->Parent() )
645 m_pCurrentGroup->Parent()->SetLastGroup(m_pCurrentGroup);
646 m_pCurrentGroup->SetLine(m_linesTail);
647 }
648
649 // check that there is nothing except comments left on this line
650 bool bCont = true;
651 while ( *++pEnd != wxT('\0') && bCont ) {
652 switch ( *pEnd ) {
653 case wxT('#'):
654 case wxT(';'):
655 bCont = false;
656 break;
657
658 case wxT(' '):
659 case wxT('\t'):
660 // ignore whitespace ('\n' impossible here)
661 break;
662
663 default:
664 wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."),
665 buffer.GetName(), n + 1, pEnd);
666 bCont = false;
667 }
668 }
669 }
670 else { // a key
671 pEnd = pStart;
672 while ( *pEnd && *pEnd != wxT('=') /* && !wxIsspace(*pEnd)*/ ) {
673 if ( *pEnd == wxT('\\') ) {
674 // next character may be space or not - still take it because it's
675 // quoted (unless there is nothing)
676 pEnd++;
677 if ( !*pEnd ) {
678 // the error message will be given below anyhow
679 break;
680 }
681 }
682
683 pEnd++;
684 }
685
686 wxString strKey(FilterInEntryName(wxString(pStart, pEnd).Trim()));
687
688 // skip whitespace
689 while ( wxIsspace(*pEnd) )
690 pEnd++;
691
692 if ( *pEnd++ != wxT('=') ) {
693 wxLogError(_("file '%s', line %d: '=' expected."),
694 buffer.GetName(), n + 1);
695 }
696 else {
697 wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strKey);
698
699 if ( pEntry == NULL ) {
700 // new entry
701 pEntry = m_pCurrentGroup->AddEntry(strKey, n);
702 }
703 else {
704 if ( bLocal && pEntry->IsImmutable() ) {
705 // immutable keys can't be changed by user
706 wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."),
707 buffer.GetName(), n + 1, strKey.c_str());
708 continue;
709 }
710 // the condition below catches the cases (a) and (b) but not (c):
711 // (a) global key found second time in global file
712 // (b) key found second (or more) time in local file
713 // (c) key from global file now found in local one
714 // which is exactly what we want.
715 else if ( !bLocal || pEntry->IsLocal() ) {
716 wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."),
717 buffer.GetName(), n + 1, strKey.c_str(), pEntry->Line());
718
719 }
720 }
721
722 if ( bLocal )
723 pEntry->SetLine(m_linesTail);
724
725 // skip whitespace
726 while ( wxIsspace(*pEnd) )
727 pEnd++;
728
729 wxString value = pEnd;
730 if ( !(GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS) )
731 value = FilterInValue(value);
732
733 pEntry->SetValue(value, false);
734 }
735 }
736 }
737 }
738
739 // ----------------------------------------------------------------------------
740 // set/retrieve path
741 // ----------------------------------------------------------------------------
742
743 void wxFileConfig::SetRootPath()
744 {
745 m_strPath.Empty();
746 m_pCurrentGroup = m_pRootGroup;
747 }
748
749 bool
750 wxFileConfig::DoSetPath(const wxString& strPath, bool createMissingComponents)
751 {
752 wxArrayString aParts;
753
754 if ( strPath.empty() ) {
755 SetRootPath();
756 return true;
757 }
758
759 if ( strPath[0] == wxCONFIG_PATH_SEPARATOR ) {
760 // absolute path
761 wxSplitPath(aParts, strPath);
762 }
763 else {
764 // relative path, combine with current one
765 wxString strFullPath = m_strPath;
766 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
767 wxSplitPath(aParts, strFullPath);
768 }
769
770 // change current group
771 size_t n;
772 m_pCurrentGroup = m_pRootGroup;
773 for ( n = 0; n < aParts.Count(); n++ ) {
774 wxFileConfigGroup *pNextGroup = m_pCurrentGroup->FindSubgroup(aParts[n]);
775 if ( pNextGroup == NULL )
776 {
777 if ( !createMissingComponents )
778 return false;
779
780 pNextGroup = m_pCurrentGroup->AddSubgroup(aParts[n]);
781 }
782
783 m_pCurrentGroup = pNextGroup;
784 }
785
786 // recombine path parts in one variable
787 m_strPath.Empty();
788 for ( n = 0; n < aParts.Count(); n++ ) {
789 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
790 }
791
792 return true;
793 }
794
795 void wxFileConfig::SetPath(const wxString& strPath)
796 {
797 DoSetPath(strPath, true /* create missing path components */);
798 }
799
800 // ----------------------------------------------------------------------------
801 // enumeration
802 // ----------------------------------------------------------------------------
803
804 bool wxFileConfig::GetFirstGroup(wxString& str, long& lIndex) const
805 {
806 lIndex = 0;
807 return GetNextGroup(str, lIndex);
808 }
809
810 bool wxFileConfig::GetNextGroup (wxString& str, long& lIndex) const
811 {
812 if ( size_t(lIndex) < m_pCurrentGroup->Groups().Count() ) {
813 str = m_pCurrentGroup->Groups()[(size_t)lIndex++]->Name();
814 return true;
815 }
816 else
817 return false;
818 }
819
820 bool wxFileConfig::GetFirstEntry(wxString& str, long& lIndex) const
821 {
822 lIndex = 0;
823 return GetNextEntry(str, lIndex);
824 }
825
826 bool wxFileConfig::GetNextEntry (wxString& str, long& lIndex) const
827 {
828 if ( size_t(lIndex) < m_pCurrentGroup->Entries().Count() ) {
829 str = m_pCurrentGroup->Entries()[(size_t)lIndex++]->Name();
830 return true;
831 }
832 else
833 return false;
834 }
835
836 size_t wxFileConfig::GetNumberOfEntries(bool bRecursive) const
837 {
838 size_t n = m_pCurrentGroup->Entries().Count();
839 if ( bRecursive ) {
840 wxFileConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
841 size_t nSubgroups = m_pCurrentGroup->Groups().Count();
842 for ( size_t nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
843 CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
844 n += GetNumberOfEntries(true);
845 CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
846 }
847 }
848
849 return n;
850 }
851
852 size_t wxFileConfig::GetNumberOfGroups(bool bRecursive) const
853 {
854 size_t n = m_pCurrentGroup->Groups().Count();
855 if ( bRecursive ) {
856 wxFileConfigGroup *pOldCurrentGroup = m_pCurrentGroup;
857 size_t nSubgroups = m_pCurrentGroup->Groups().Count();
858 for ( size_t nGroup = 0; nGroup < nSubgroups; nGroup++ ) {
859 CONST_CAST m_pCurrentGroup = m_pCurrentGroup->Groups()[nGroup];
860 n += GetNumberOfGroups(true);
861 CONST_CAST m_pCurrentGroup = pOldCurrentGroup;
862 }
863 }
864
865 return n;
866 }
867
868 // ----------------------------------------------------------------------------
869 // tests for existence
870 // ----------------------------------------------------------------------------
871
872 bool wxFileConfig::HasGroup(const wxString& strName) const
873 {
874 // special case: DoSetPath("") does work as it's equivalent to DoSetPath("/")
875 // but there is no group with empty name so treat this separately
876 if ( strName.empty() )
877 return false;
878
879 const wxString pathOld = GetPath();
880
881 wxFileConfig *self = wx_const_cast(wxFileConfig *, this);
882 const bool
883 rc = self->DoSetPath(strName, false /* don't create missing components */);
884
885 self->SetPath(pathOld);
886
887 return rc;
888 }
889
890 bool wxFileConfig::HasEntry(const wxString& strName) const
891 {
892 wxConfigPathChanger path(this, strName);
893
894 wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
895 return pEntry != NULL;
896 }
897
898 // ----------------------------------------------------------------------------
899 // read/write values
900 // ----------------------------------------------------------------------------
901
902 bool wxFileConfig::DoReadString(const wxString& key, wxString* pStr) const
903 {
904 wxConfigPathChanger path(this, key);
905
906 wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
907 if (pEntry == NULL) {
908 return false;
909 }
910
911 *pStr = pEntry->Value();
912
913 return true;
914 }
915
916 bool wxFileConfig::DoReadLong(const wxString& key, long *pl) const
917 {
918 wxString str;
919 if ( !Read(key, &str) )
920 return false;
921
922 // extra spaces shouldn't prevent us from reading numeric values
923 str.Trim();
924
925 return str.ToLong(pl);
926 }
927
928 bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue)
929 {
930 wxConfigPathChanger path(this, key);
931 wxString strName = path.Name();
932
933 wxLogTrace( FILECONF_TRACE_MASK,
934 _T(" Writing String '%s' = '%s' to Group '%s'"),
935 strName.c_str(),
936 szValue.c_str(),
937 GetPath().c_str() );
938
939 if ( strName.empty() )
940 {
941 // setting the value of a group is an error
942
943 wxASSERT_MSG( szValue.empty(), wxT("can't set value of a group!") );
944
945 // ... except if it's empty in which case it's a way to force it's creation
946
947 wxLogTrace( FILECONF_TRACE_MASK,
948 _T(" Creating group %s"),
949 m_pCurrentGroup->Name().c_str() );
950
951 SetDirty();
952
953 // this will add a line for this group if it didn't have it before
954
955 (void)m_pCurrentGroup->GetGroupLine();
956 }
957 else
958 {
959 // writing an entry check that the name is reasonable
960 if ( strName[0u] == wxCONFIG_IMMUTABLE_PREFIX )
961 {
962 wxLogError( _("Config entry name cannot start with '%c'."),
963 wxCONFIG_IMMUTABLE_PREFIX);
964 return false;
965 }
966
967 wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strName);
968
969 if ( pEntry == 0 )
970 {
971 wxLogTrace( FILECONF_TRACE_MASK,
972 _T(" Adding Entry %s"),
973 strName.c_str() );
974 pEntry = m_pCurrentGroup->AddEntry(strName);
975 }
976
977 wxLogTrace( FILECONF_TRACE_MASK,
978 _T(" Setting value %s"),
979 szValue.c_str() );
980 pEntry->SetValue(szValue);
981
982 SetDirty();
983 }
984
985 return true;
986 }
987
988 bool wxFileConfig::DoWriteLong(const wxString& key, long lValue)
989 {
990 return Write(key, wxString::Format(_T("%ld"), lValue));
991 }
992
993 bool wxFileConfig::Flush(bool /* bCurrentOnly */)
994 {
995 if ( !IsDirty() || !m_strLocalFile )
996 return true;
997
998 // set the umask if needed
999 wxCHANGE_UMASK(m_umask);
1000
1001 wxTempFile file(m_strLocalFile);
1002
1003 if ( !file.IsOpened() )
1004 {
1005 wxLogError(_("can't open user configuration file."));
1006 return false;
1007 }
1008
1009 // write all strings to file
1010 for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() )
1011 {
1012 wxString line = p->Text();
1013 line += wxTextFile::GetEOL();
1014 if ( !file.Write(line, m_conv) )
1015 {
1016 wxLogError(_("can't write user configuration file."));
1017 return false;
1018 }
1019 }
1020
1021 if ( !file.Commit() )
1022 {
1023 wxLogError(_("Failed to update user configuration file."));
1024
1025 return false;
1026 }
1027
1028 ResetDirty();
1029
1030 #if defined(__WXMAC__)
1031 wxFileName(m_strLocalFile).MacSetTypeAndCreator('TEXT', 'ttxt');
1032 #endif // __WXMAC__
1033
1034 return true;
1035 }
1036
1037 #if wxUSE_STREAMS
1038
1039 bool wxFileConfig::Save(wxOutputStream& os, wxMBConv& conv)
1040 {
1041 // save unconditionally, even if not dirty
1042 for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() )
1043 {
1044 wxString line = p->Text();
1045 line += wxTextFile::GetEOL();
1046
1047 wxCharBuffer buf(line.mb_str(conv));
1048 if ( !os.Write(buf, strlen(buf)) )
1049 {
1050 wxLogError(_("Error saving user configuration data."));
1051
1052 return false;
1053 }
1054 }
1055
1056 ResetDirty();
1057
1058 return true;
1059 }
1060
1061 #endif // wxUSE_STREAMS
1062
1063 // ----------------------------------------------------------------------------
1064 // renaming groups/entries
1065 // ----------------------------------------------------------------------------
1066
1067 bool wxFileConfig::RenameEntry(const wxString& oldName,
1068 const wxString& newName)
1069 {
1070 wxASSERT_MSG( !wxStrchr(oldName, wxCONFIG_PATH_SEPARATOR),
1071 _T("RenameEntry(): paths are not supported") );
1072
1073 // check that the entry exists
1074 wxFileConfigEntry *oldEntry = m_pCurrentGroup->FindEntry(oldName);
1075 if ( !oldEntry )
1076 return false;
1077
1078 // check that the new entry doesn't already exist
1079 if ( m_pCurrentGroup->FindEntry(newName) )
1080 return false;
1081
1082 // delete the old entry, create the new one
1083 wxString value = oldEntry->Value();
1084 if ( !m_pCurrentGroup->DeleteEntry(oldName) )
1085 return false;
1086
1087 SetDirty();
1088
1089 wxFileConfigEntry *newEntry = m_pCurrentGroup->AddEntry(newName);
1090 newEntry->SetValue(value);
1091
1092 return true;
1093 }
1094
1095 bool wxFileConfig::RenameGroup(const wxString& oldName,
1096 const wxString& newName)
1097 {
1098 // check that the group exists
1099 wxFileConfigGroup *group = m_pCurrentGroup->FindSubgroup(oldName);
1100 if ( !group )
1101 return false;
1102
1103 // check that the new group doesn't already exist
1104 if ( m_pCurrentGroup->FindSubgroup(newName) )
1105 return false;
1106
1107 group->Rename(newName);
1108
1109 SetDirty();
1110
1111 return true;
1112 }
1113
1114 // ----------------------------------------------------------------------------
1115 // delete groups/entries
1116 // ----------------------------------------------------------------------------
1117
1118 bool wxFileConfig::DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso)
1119 {
1120 wxConfigPathChanger path(this, key);
1121
1122 if ( !m_pCurrentGroup->DeleteEntry(path.Name()) )
1123 return false;
1124
1125 SetDirty();
1126
1127 if ( bGroupIfEmptyAlso && m_pCurrentGroup->IsEmpty() ) {
1128 if ( m_pCurrentGroup != m_pRootGroup ) {
1129 wxFileConfigGroup *pGroup = m_pCurrentGroup;
1130 SetPath(wxT("..")); // changes m_pCurrentGroup!
1131 m_pCurrentGroup->DeleteSubgroupByName(pGroup->Name());
1132 }
1133 //else: never delete the root group
1134 }
1135
1136 return true;
1137 }
1138
1139 bool wxFileConfig::DeleteGroup(const wxString& key)
1140 {
1141 wxConfigPathChanger path(this, key);
1142
1143 if ( !m_pCurrentGroup->DeleteSubgroupByName(path.Name()) )
1144 return false;
1145
1146 SetDirty();
1147
1148 return true;
1149 }
1150
1151 bool wxFileConfig::DeleteAll()
1152 {
1153 CleanUp();
1154
1155 if ( !m_strLocalFile.empty() )
1156 {
1157 if ( wxFile::Exists(m_strLocalFile) && wxRemove(m_strLocalFile) == -1 )
1158 {
1159 wxLogSysError(_("can't delete user configuration file '%s'"),
1160 m_strLocalFile.c_str());
1161 return false;
1162 }
1163 }
1164
1165 Init();
1166
1167 return true;
1168 }
1169
1170 // ----------------------------------------------------------------------------
1171 // linked list functions
1172 // ----------------------------------------------------------------------------
1173
1174 // append a new line to the end of the list
1175
1176 wxFileConfigLineList *wxFileConfig::LineListAppend(const wxString& str)
1177 {
1178 wxLogTrace( FILECONF_TRACE_MASK,
1179 _T(" ** Adding Line '%s'"),
1180 str.c_str() );
1181 wxLogTrace( FILECONF_TRACE_MASK,
1182 _T(" head: %s"),
1183 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1184 wxLogTrace( FILECONF_TRACE_MASK,
1185 _T(" tail: %s"),
1186 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1187
1188 wxFileConfigLineList *pLine = new wxFileConfigLineList(str);
1189
1190 if ( m_linesTail == NULL )
1191 {
1192 // list is empty
1193 m_linesHead = pLine;
1194 }
1195 else
1196 {
1197 // adjust pointers
1198 m_linesTail->SetNext(pLine);
1199 pLine->SetPrev(m_linesTail);
1200 }
1201
1202 m_linesTail = pLine;
1203
1204 wxLogTrace( FILECONF_TRACE_MASK,
1205 _T(" head: %s"),
1206 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1207 wxLogTrace( FILECONF_TRACE_MASK,
1208 _T(" tail: %s"),
1209 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1210
1211 return m_linesTail;
1212 }
1213
1214 // insert a new line after the given one or in the very beginning if !pLine
1215 wxFileConfigLineList *wxFileConfig::LineListInsert(const wxString& str,
1216 wxFileConfigLineList *pLine)
1217 {
1218 wxLogTrace( FILECONF_TRACE_MASK,
1219 _T(" ** Inserting Line '%s' after '%s'"),
1220 str.c_str(),
1221 ((pLine) ? pLine->Text().c_str() : wxEmptyString) );
1222 wxLogTrace( FILECONF_TRACE_MASK,
1223 _T(" head: %s"),
1224 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1225 wxLogTrace( FILECONF_TRACE_MASK,
1226 _T(" tail: %s"),
1227 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1228
1229 if ( pLine == m_linesTail )
1230 return LineListAppend(str);
1231
1232 wxFileConfigLineList *pNewLine = new wxFileConfigLineList(str);
1233 if ( pLine == NULL )
1234 {
1235 // prepend to the list
1236 pNewLine->SetNext(m_linesHead);
1237 m_linesHead->SetPrev(pNewLine);
1238 m_linesHead = pNewLine;
1239 }
1240 else
1241 {
1242 // insert before pLine
1243 wxFileConfigLineList *pNext = pLine->Next();
1244 pNewLine->SetNext(pNext);
1245 pNewLine->SetPrev(pLine);
1246 pNext->SetPrev(pNewLine);
1247 pLine->SetNext(pNewLine);
1248 }
1249
1250 wxLogTrace( FILECONF_TRACE_MASK,
1251 _T(" head: %s"),
1252 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1253 wxLogTrace( FILECONF_TRACE_MASK,
1254 _T(" tail: %s"),
1255 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1256
1257 return pNewLine;
1258 }
1259
1260 void wxFileConfig::LineListRemove(wxFileConfigLineList *pLine)
1261 {
1262 wxLogTrace( FILECONF_TRACE_MASK,
1263 _T(" ** Removing Line '%s'"),
1264 pLine->Text().c_str() );
1265 wxLogTrace( FILECONF_TRACE_MASK,
1266 _T(" head: %s"),
1267 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1268 wxLogTrace( FILECONF_TRACE_MASK,
1269 _T(" tail: %s"),
1270 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1271
1272 wxFileConfigLineList *pPrev = pLine->Prev(),
1273 *pNext = pLine->Next();
1274
1275 // first entry?
1276
1277 if ( pPrev == NULL )
1278 m_linesHead = pNext;
1279 else
1280 pPrev->SetNext(pNext);
1281
1282 // last entry?
1283
1284 if ( pNext == NULL )
1285 m_linesTail = pPrev;
1286 else
1287 pNext->SetPrev(pPrev);
1288
1289 if ( m_pRootGroup->GetGroupLine() == pLine )
1290 m_pRootGroup->SetLine(m_linesHead);
1291
1292 wxLogTrace( FILECONF_TRACE_MASK,
1293 _T(" head: %s"),
1294 ((m_linesHead) ? m_linesHead->Text().c_str() : wxEmptyString) );
1295 wxLogTrace( FILECONF_TRACE_MASK,
1296 _T(" tail: %s"),
1297 ((m_linesTail) ? m_linesTail->Text().c_str() : wxEmptyString) );
1298
1299 delete pLine;
1300 }
1301
1302 bool wxFileConfig::LineListIsEmpty()
1303 {
1304 return m_linesHead == NULL;
1305 }
1306
1307 // ============================================================================
1308 // wxFileConfig::wxFileConfigGroup
1309 // ============================================================================
1310
1311 // ----------------------------------------------------------------------------
1312 // ctor/dtor
1313 // ----------------------------------------------------------------------------
1314
1315 // ctor
1316 wxFileConfigGroup::wxFileConfigGroup(wxFileConfigGroup *pParent,
1317 const wxString& strName,
1318 wxFileConfig *pConfig)
1319 : m_aEntries(CompareEntries),
1320 m_aSubgroups(CompareGroups),
1321 m_strName(strName)
1322 {
1323 m_pConfig = pConfig;
1324 m_pParent = pParent;
1325 m_pLine = NULL;
1326
1327 m_pLastEntry = NULL;
1328 m_pLastGroup = NULL;
1329 }
1330
1331 // dtor deletes all children
1332 wxFileConfigGroup::~wxFileConfigGroup()
1333 {
1334 // entries
1335 size_t n, nCount = m_aEntries.Count();
1336 for ( n = 0; n < nCount; n++ )
1337 delete m_aEntries[n];
1338
1339 // subgroups
1340 nCount = m_aSubgroups.Count();
1341 for ( n = 0; n < nCount; n++ )
1342 delete m_aSubgroups[n];
1343 }
1344
1345 // ----------------------------------------------------------------------------
1346 // line
1347 // ----------------------------------------------------------------------------
1348
1349 void wxFileConfigGroup::SetLine(wxFileConfigLineList *pLine)
1350 {
1351 // for a normal (i.e. not root) group this method shouldn't be called twice
1352 // unless we are resetting the line
1353 wxASSERT_MSG( !m_pParent || !m_pLine || !pLine,
1354 _T("changing line for a non-root group?") );
1355
1356 m_pLine = pLine;
1357 }
1358
1359 /*
1360 This is a bit complicated, so let me explain it in details. All lines that
1361 were read from the local file (the only one we will ever modify) are stored
1362 in a (doubly) linked list. Our problem is to know at which position in this
1363 list should we insert the new entries/subgroups. To solve it we keep three
1364 variables for each group: m_pLine, m_pLastEntry and m_pLastGroup.
1365
1366 m_pLine points to the line containing "[group_name]"
1367 m_pLastEntry points to the last entry of this group in the local file.
1368 m_pLastGroup subgroup
1369
1370 Initially, they're NULL all three. When the group (an entry/subgroup) is read
1371 from the local file, the corresponding variable is set. However, if the group
1372 was read from the global file and then modified or created by the application
1373 these variables are still NULL and we need to create the corresponding lines.
1374 See the following functions (and comments preceding them) for the details of
1375 how we do it.
1376
1377 Also, when our last entry/group are deleted we need to find the new last
1378 element - the code in DeleteEntry/Subgroup does this by backtracking the list
1379 of lines until it either founds an entry/subgroup (and this is the new last
1380 element) or the m_pLine of the group, in which case there are no more entries
1381 (or subgroups) left and m_pLast<element> becomes NULL.
1382
1383 NB: This last problem could be avoided for entries if we added new entries
1384 immediately after m_pLine, but in this case the entries would appear
1385 backwards in the config file (OTOH, it's not that important) and as we
1386 would still need to do it for the subgroups the code wouldn't have been
1387 significantly less complicated.
1388 */
1389
1390 // Return the line which contains "[our name]". If we're still not in the list,
1391 // add our line to it immediately after the last line of our parent group if we
1392 // have it or in the very beginning if we're the root group.
1393 wxFileConfigLineList *wxFileConfigGroup::GetGroupLine()
1394 {
1395 wxLogTrace( FILECONF_TRACE_MASK,
1396 _T(" GetGroupLine() for Group '%s'"),
1397 Name().c_str() );
1398
1399 if ( !m_pLine )
1400 {
1401 wxLogTrace( FILECONF_TRACE_MASK,
1402 _T(" Getting Line item pointer") );
1403
1404 wxFileConfigGroup *pParent = Parent();
1405
1406 // this group wasn't present in local config file, add it now
1407 if ( pParent )
1408 {
1409 wxLogTrace( FILECONF_TRACE_MASK,
1410 _T(" checking parent '%s'"),
1411 pParent->Name().c_str() );
1412
1413 wxString strFullName;
1414
1415 // add 1 to the name because we don't want to start with '/'
1416 strFullName << wxT("[")
1417 << FilterOutEntryName(GetFullName().c_str() + 1)
1418 << wxT("]");
1419 m_pLine = m_pConfig->LineListInsert(strFullName,
1420 pParent->GetLastGroupLine());
1421 pParent->SetLastGroup(this); // we're surely after all the others
1422 }
1423 //else: this is the root group and so we return NULL because we don't
1424 // have any group line
1425 }
1426
1427 return m_pLine;
1428 }
1429
1430 // Return the last line belonging to the subgroups of this group (after which
1431 // we can add a new subgroup), if we don't have any subgroups or entries our
1432 // last line is the group line (m_pLine) itself.
1433 wxFileConfigLineList *wxFileConfigGroup::GetLastGroupLine()
1434 {
1435 // if we have any subgroups, our last line is the last line of the last
1436 // subgroup
1437 if ( m_pLastGroup )
1438 {
1439 wxFileConfigLineList *pLine = m_pLastGroup->GetLastGroupLine();
1440
1441 wxASSERT_MSG( pLine, _T("last group must have !NULL associated line") );
1442
1443 return pLine;
1444 }
1445
1446 // no subgroups, so the last line is the line of thelast entry (if any)
1447 return GetLastEntryLine();
1448 }
1449
1450 // return the last line belonging to the entries of this group (after which
1451 // we can add a new entry), if we don't have any entries we will add the new
1452 // one immediately after the group line itself.
1453 wxFileConfigLineList *wxFileConfigGroup::GetLastEntryLine()
1454 {
1455 wxLogTrace( FILECONF_TRACE_MASK,
1456 _T(" GetLastEntryLine() for Group '%s'"),
1457 Name().c_str() );
1458
1459 if ( m_pLastEntry )
1460 {
1461 wxFileConfigLineList *pLine = m_pLastEntry->GetLine();
1462
1463 wxASSERT_MSG( pLine, _T("last entry must have !NULL associated line") );
1464
1465 return pLine;
1466 }
1467
1468 // no entries: insert after the group header, if any
1469 return GetGroupLine();
1470 }
1471
1472 void wxFileConfigGroup::SetLastEntry(wxFileConfigEntry *pEntry)
1473 {
1474 m_pLastEntry = pEntry;
1475
1476 if ( !m_pLine )
1477 {
1478 // the only situation in which a group without its own line can have
1479 // an entry is when the first entry is added to the initially empty
1480 // root pseudo-group
1481 wxASSERT_MSG( !m_pParent, _T("unexpected for non root group") );
1482
1483 // let the group know that it does have a line in the file now
1484 m_pLine = pEntry->GetLine();
1485 }
1486 }
1487
1488 // ----------------------------------------------------------------------------
1489 // group name
1490 // ----------------------------------------------------------------------------
1491
1492 void wxFileConfigGroup::UpdateGroupAndSubgroupsLines()
1493 {
1494 // update the line of this group
1495 wxFileConfigLineList *line = GetGroupLine();
1496 wxCHECK_RET( line, _T("a non root group must have a corresponding line!") );
1497
1498 // +1: skip the leading '/'
1499 line->SetText(wxString::Format(_T("[%s]"), GetFullName().c_str() + 1));
1500
1501
1502 // also update all subgroups as they have this groups name in their lines
1503 const size_t nCount = m_aSubgroups.Count();
1504 for ( size_t n = 0; n < nCount; n++ )
1505 {
1506 m_aSubgroups[n]->UpdateGroupAndSubgroupsLines();
1507 }
1508 }
1509
1510 void wxFileConfigGroup::Rename(const wxString& newName)
1511 {
1512 wxCHECK_RET( m_pParent, _T("the root group can't be renamed") );
1513
1514 m_strName = newName;
1515
1516 // update the group lines recursively
1517 UpdateGroupAndSubgroupsLines();
1518 }
1519
1520 wxString wxFileConfigGroup::GetFullName() const
1521 {
1522 wxString fullname;
1523 if ( Parent() )
1524 fullname = Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR + Name();
1525
1526 return fullname;
1527 }
1528
1529 // ----------------------------------------------------------------------------
1530 // find an item
1531 // ----------------------------------------------------------------------------
1532
1533 // use binary search because the array is sorted
1534 wxFileConfigEntry *
1535 wxFileConfigGroup::FindEntry(const wxChar *szName) const
1536 {
1537 size_t i,
1538 lo = 0,
1539 hi = m_aEntries.Count();
1540 int res;
1541 wxFileConfigEntry *pEntry;
1542
1543 while ( lo < hi ) {
1544 i = (lo + hi)/2;
1545 pEntry = m_aEntries[i];
1546
1547 #if wxCONFIG_CASE_SENSITIVE
1548 res = wxStrcmp(pEntry->Name(), szName);
1549 #else
1550 res = wxStricmp(pEntry->Name(), szName);
1551 #endif
1552
1553 if ( res > 0 )
1554 hi = i;
1555 else if ( res < 0 )
1556 lo = i + 1;
1557 else
1558 return pEntry;
1559 }
1560
1561 return NULL;
1562 }
1563
1564 wxFileConfigGroup *
1565 wxFileConfigGroup::FindSubgroup(const wxChar *szName) const
1566 {
1567 size_t i,
1568 lo = 0,
1569 hi = m_aSubgroups.Count();
1570 int res;
1571 wxFileConfigGroup *pGroup;
1572
1573 while ( lo < hi ) {
1574 i = (lo + hi)/2;
1575 pGroup = m_aSubgroups[i];
1576
1577 #if wxCONFIG_CASE_SENSITIVE
1578 res = wxStrcmp(pGroup->Name(), szName);
1579 #else
1580 res = wxStricmp(pGroup->Name(), szName);
1581 #endif
1582
1583 if ( res > 0 )
1584 hi = i;
1585 else if ( res < 0 )
1586 lo = i + 1;
1587 else
1588 return pGroup;
1589 }
1590
1591 return NULL;
1592 }
1593
1594 // ----------------------------------------------------------------------------
1595 // create a new item
1596 // ----------------------------------------------------------------------------
1597
1598 // create a new entry and add it to the current group
1599 wxFileConfigEntry *wxFileConfigGroup::AddEntry(const wxString& strName, int nLine)
1600 {
1601 wxASSERT( FindEntry(strName) == 0 );
1602
1603 wxFileConfigEntry *pEntry = new wxFileConfigEntry(this, strName, nLine);
1604
1605 m_aEntries.Add(pEntry);
1606 return pEntry;
1607 }
1608
1609 // create a new group and add it to the current group
1610 wxFileConfigGroup *wxFileConfigGroup::AddSubgroup(const wxString& strName)
1611 {
1612 wxASSERT( FindSubgroup(strName) == 0 );
1613
1614 wxFileConfigGroup *pGroup = new wxFileConfigGroup(this, strName, m_pConfig);
1615
1616 m_aSubgroups.Add(pGroup);
1617 return pGroup;
1618 }
1619
1620 // ----------------------------------------------------------------------------
1621 // delete an item
1622 // ----------------------------------------------------------------------------
1623
1624 /*
1625 The delete operations are _very_ slow if we delete the last item of this
1626 group (see comments before GetXXXLineXXX functions for more details),
1627 so it's much better to start with the first entry/group if we want to
1628 delete several of them.
1629 */
1630
1631 bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar *szName)
1632 {
1633 wxFileConfigGroup * const pGroup = FindSubgroup(szName);
1634
1635 return pGroup ? DeleteSubgroup(pGroup) : false;
1636 }
1637
1638 // Delete the subgroup and remove all references to it from
1639 // other data structures.
1640 bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup *pGroup)
1641 {
1642 wxCHECK_MSG( pGroup, false, _T("deleting non existing group?") );
1643
1644 wxLogTrace( FILECONF_TRACE_MASK,
1645 _T("Deleting group '%s' from '%s'"),
1646 pGroup->Name().c_str(),
1647 Name().c_str() );
1648
1649 wxLogTrace( FILECONF_TRACE_MASK,
1650 _T(" (m_pLine) = prev: %p, this %p, next %p"),
1651 ((m_pLine) ? m_pLine->Prev() : 0),
1652 m_pLine,
1653 ((m_pLine) ? m_pLine->Next() : 0) );
1654 wxLogTrace( FILECONF_TRACE_MASK,
1655 _T(" text: '%s'"),
1656 ((m_pLine) ? m_pLine->Text().c_str() : wxEmptyString) );
1657
1658 // delete all entries...
1659 size_t nCount = pGroup->m_aEntries.Count();
1660
1661 wxLogTrace(FILECONF_TRACE_MASK,
1662 _T("Removing %lu entries"), (unsigned long)nCount );
1663
1664 for ( size_t nEntry = 0; nEntry < nCount; nEntry++ )
1665 {
1666 wxFileConfigLineList *pLine = pGroup->m_aEntries[nEntry]->GetLine();
1667
1668 if ( pLine )
1669 {
1670 wxLogTrace( FILECONF_TRACE_MASK,
1671 _T(" '%s'"),
1672 pLine->Text().c_str() );
1673 m_pConfig->LineListRemove(pLine);
1674 }
1675 }
1676
1677 // ...and subgroups of this subgroup
1678 nCount = pGroup->m_aSubgroups.Count();
1679
1680 wxLogTrace( FILECONF_TRACE_MASK,
1681 _T("Removing %lu subgroups"), (unsigned long)nCount );
1682
1683 for ( size_t nGroup = 0; nGroup < nCount; nGroup++ )
1684 {
1685 pGroup->DeleteSubgroup(pGroup->m_aSubgroups[0]);
1686 }
1687
1688 // and then finally the group itself
1689 wxFileConfigLineList *pLine = pGroup->m_pLine;
1690 if ( pLine )
1691 {
1692 wxLogTrace( FILECONF_TRACE_MASK,
1693 _T(" Removing line for group '%s' : '%s'"),
1694 pGroup->Name().c_str(),
1695 pLine->Text().c_str() );
1696 wxLogTrace( FILECONF_TRACE_MASK,
1697 _T(" Removing from group '%s' : '%s'"),
1698 Name().c_str(),
1699 ((m_pLine) ? m_pLine->Text().c_str() : wxEmptyString) );
1700
1701 // notice that we may do this test inside the previous "if"
1702 // because the last entry's line is surely !NULL
1703 if ( pGroup == m_pLastGroup )
1704 {
1705 wxLogTrace( FILECONF_TRACE_MASK,
1706 _T(" Removing last group") );
1707
1708 // our last entry is being deleted, so find the last one which
1709 // stays by going back until we find a subgroup or reach the
1710 // group line
1711 const size_t nSubgroups = m_aSubgroups.Count();
1712
1713 m_pLastGroup = NULL;
1714 for ( wxFileConfigLineList *pl = pLine->Prev();
1715 pl && pl != m_pLine && !m_pLastGroup;
1716 pl = pl->Prev() )
1717 {
1718 // does this line belong to our subgroup?
1719 for ( size_t n = 0; n < nSubgroups; n++ )
1720 {
1721 // do _not_ call GetGroupLine! we don't want to add it to
1722 // the local file if it's not already there
1723 if ( m_aSubgroups[n]->m_pLine == pl )
1724 {
1725 m_pLastGroup = m_aSubgroups[n];
1726 break;
1727 }
1728 }
1729 }
1730 }
1731
1732 m_pConfig->LineListRemove(pLine);
1733 }
1734 else
1735 {
1736 wxLogTrace( FILECONF_TRACE_MASK,
1737 _T(" No line entry for Group '%s'?"),
1738 pGroup->Name().c_str() );
1739 }
1740
1741 m_aSubgroups.Remove(pGroup);
1742 delete pGroup;
1743
1744 return true;
1745 }
1746
1747 bool wxFileConfigGroup::DeleteEntry(const wxChar *szName)
1748 {
1749 wxFileConfigEntry *pEntry = FindEntry(szName);
1750 if ( !pEntry )
1751 {
1752 // entry doesn't exist, nothing to do
1753 return false;
1754 }
1755
1756 wxFileConfigLineList *pLine = pEntry->GetLine();
1757 if ( pLine != NULL ) {
1758 // notice that we may do this test inside the previous "if" because the
1759 // last entry's line is surely !NULL
1760 if ( pEntry == m_pLastEntry ) {
1761 // our last entry is being deleted - find the last one which stays
1762 wxASSERT( m_pLine != NULL ); // if we have an entry with !NULL pLine...
1763
1764 // go back until we find another entry or reach the group's line
1765 wxFileConfigEntry *pNewLast = NULL;
1766 size_t n, nEntries = m_aEntries.Count();
1767 wxFileConfigLineList *pl;
1768 for ( pl = pLine->Prev(); pl != m_pLine; pl = pl->Prev() ) {
1769 // is it our subgroup?
1770 for ( n = 0; (pNewLast == NULL) && (n < nEntries); n++ ) {
1771 if ( m_aEntries[n]->GetLine() == m_pLine )
1772 pNewLast = m_aEntries[n];
1773 }
1774
1775 if ( pNewLast != NULL ) // found?
1776 break;
1777 }
1778
1779 if ( pl == m_pLine ) {
1780 wxASSERT( !pNewLast ); // how comes it has the same line as we?
1781
1782 // we've reached the group line without finding any subgroups
1783 m_pLastEntry = NULL;
1784 }
1785 else
1786 m_pLastEntry = pNewLast;
1787 }
1788
1789 m_pConfig->LineListRemove(pLine);
1790 }
1791
1792 m_aEntries.Remove(pEntry);
1793 delete pEntry;
1794
1795 return true;
1796 }
1797
1798 // ============================================================================
1799 // wxFileConfig::wxFileConfigEntry
1800 // ============================================================================
1801
1802 // ----------------------------------------------------------------------------
1803 // ctor
1804 // ----------------------------------------------------------------------------
1805 wxFileConfigEntry::wxFileConfigEntry(wxFileConfigGroup *pParent,
1806 const wxString& strName,
1807 int nLine)
1808 : m_strName(strName)
1809 {
1810 wxASSERT( !strName.empty() );
1811
1812 m_pParent = pParent;
1813 m_nLine = nLine;
1814 m_pLine = NULL;
1815
1816 m_bHasValue = false;
1817
1818 m_bImmutable = strName[0] == wxCONFIG_IMMUTABLE_PREFIX;
1819 if ( m_bImmutable )
1820 m_strName.erase(0, 1); // remove first character
1821 }
1822
1823 // ----------------------------------------------------------------------------
1824 // set value
1825 // ----------------------------------------------------------------------------
1826
1827 void wxFileConfigEntry::SetLine(wxFileConfigLineList *pLine)
1828 {
1829 if ( m_pLine != NULL ) {
1830 wxLogWarning(_("entry '%s' appears more than once in group '%s'"),
1831 Name().c_str(), m_pParent->GetFullName().c_str());
1832 }
1833
1834 m_pLine = pLine;
1835 Group()->SetLastEntry(this);
1836 }
1837
1838 // second parameter is false if we read the value from file and prevents the
1839 // entry from being marked as 'dirty'
1840 void wxFileConfigEntry::SetValue(const wxString& strValue, bool bUser)
1841 {
1842 if ( bUser && IsImmutable() )
1843 {
1844 wxLogWarning( _("attempt to change immutable key '%s' ignored."),
1845 Name().c_str());
1846 return;
1847 }
1848
1849 // do nothing if it's the same value: but don't test for it if m_bHasValue
1850 // hadn't been set yet or we'd never write empty values to the file
1851 if ( m_bHasValue && strValue == m_strValue )
1852 return;
1853
1854 m_bHasValue = true;
1855 m_strValue = strValue;
1856
1857 if ( bUser )
1858 {
1859 wxString strValFiltered;
1860
1861 if ( Group()->Config()->GetStyle() & wxCONFIG_USE_NO_ESCAPE_CHARACTERS )
1862 {
1863 strValFiltered = strValue;
1864 }
1865 else {
1866 strValFiltered = FilterOutValue(strValue);
1867 }
1868
1869 wxString strLine;
1870 strLine << FilterOutEntryName(m_strName) << wxT('=') << strValFiltered;
1871
1872 if ( m_pLine )
1873 {
1874 // entry was read from the local config file, just modify the line
1875 m_pLine->SetText(strLine);
1876 }
1877 else // this entry didn't exist in the local file
1878 {
1879 // add a new line to the file: note that line returned by
1880 // GetLastEntryLine() may be NULL if we're in the root group and it
1881 // doesn't have any entries yet, but this is ok as passing NULL
1882 // line to LineListInsert() means to prepend new line to the list
1883 wxFileConfigLineList *line = Group()->GetLastEntryLine();
1884 m_pLine = Group()->Config()->LineListInsert(strLine, line);
1885
1886 Group()->SetLastEntry(this);
1887 }
1888 }
1889 }
1890
1891 // ============================================================================
1892 // global functions
1893 // ============================================================================
1894
1895 // ----------------------------------------------------------------------------
1896 // compare functions for array sorting
1897 // ----------------------------------------------------------------------------
1898
1899 int CompareEntries(wxFileConfigEntry *p1, wxFileConfigEntry *p2)
1900 {
1901 #if wxCONFIG_CASE_SENSITIVE
1902 return wxStrcmp(p1->Name(), p2->Name());
1903 #else
1904 return wxStricmp(p1->Name(), p2->Name());
1905 #endif
1906 }
1907
1908 int CompareGroups(wxFileConfigGroup *p1, wxFileConfigGroup *p2)
1909 {
1910 #if wxCONFIG_CASE_SENSITIVE
1911 return wxStrcmp(p1->Name(), p2->Name());
1912 #else
1913 return wxStricmp(p1->Name(), p2->Name());
1914 #endif
1915 }
1916
1917 // ----------------------------------------------------------------------------
1918 // filter functions
1919 // ----------------------------------------------------------------------------
1920
1921 // undo FilterOutValue
1922 static wxString FilterInValue(const wxString& str)
1923 {
1924 wxString strResult;
1925 strResult.Alloc(str.Len());
1926
1927 bool bQuoted = !str.empty() && str[0] == '"';
1928
1929 for ( size_t n = bQuoted ? 1 : 0; n < str.Len(); n++ ) {
1930 if ( str[n] == wxT('\\') ) {
1931 switch ( str[++n] ) {
1932 case wxT('n'):
1933 strResult += wxT('\n');
1934 break;
1935
1936 case wxT('r'):
1937 strResult += wxT('\r');
1938 break;
1939
1940 case wxT('t'):
1941 strResult += wxT('\t');
1942 break;
1943
1944 case wxT('\\'):
1945 strResult += wxT('\\');
1946 break;
1947
1948 case wxT('"'):
1949 strResult += wxT('"');
1950 break;
1951 }
1952 }
1953 else {
1954 if ( str[n] != wxT('"') || !bQuoted )
1955 strResult += str[n];
1956 else if ( n != str.Len() - 1 ) {
1957 wxLogWarning(_("unexpected \" at position %d in '%s'."),
1958 n, str.c_str());
1959 }
1960 //else: it's the last quote of a quoted string, ok
1961 }
1962 }
1963
1964 return strResult;
1965 }
1966
1967 // quote the string before writing it to file
1968 static wxString FilterOutValue(const wxString& str)
1969 {
1970 if ( !str )
1971 return str;
1972
1973 wxString strResult;
1974 strResult.Alloc(str.Len());
1975
1976 // quoting is necessary to preserve spaces in the beginning of the string
1977 bool bQuote = wxIsspace(str[0]) || str[0] == wxT('"');
1978
1979 if ( bQuote )
1980 strResult += wxT('"');
1981
1982 wxChar c;
1983 for ( size_t n = 0; n < str.Len(); n++ ) {
1984 switch ( str[n] ) {
1985 case wxT('\n'):
1986 c = wxT('n');
1987 break;
1988
1989 case wxT('\r'):
1990 c = wxT('r');
1991 break;
1992
1993 case wxT('\t'):
1994 c = wxT('t');
1995 break;
1996
1997 case wxT('\\'):
1998 c = wxT('\\');
1999 break;
2000
2001 case wxT('"'):
2002 if ( bQuote ) {
2003 c = wxT('"');
2004 break;
2005 }
2006 //else: fall through
2007
2008 default:
2009 strResult += str[n];
2010 continue; // nothing special to do
2011 }
2012
2013 // we get here only for special characters
2014 strResult << wxT('\\') << c;
2015 }
2016
2017 if ( bQuote )
2018 strResult += wxT('"');
2019
2020 return strResult;
2021 }
2022
2023 // undo FilterOutEntryName
2024 static wxString FilterInEntryName(const wxString& str)
2025 {
2026 wxString strResult;
2027 strResult.Alloc(str.Len());
2028
2029 for ( const wxChar *pc = str.c_str(); *pc != '\0'; pc++ ) {
2030 if ( *pc == wxT('\\') ) {
2031 // we need to test it here or we'd skip past the NUL in the loop line
2032 if ( *++pc == _T('\0') )
2033 break;
2034 }
2035
2036 strResult += *pc;
2037 }
2038
2039 return strResult;
2040 }
2041
2042 // sanitize entry or group name: insert '\\' before any special characters
2043 static wxString FilterOutEntryName(const wxString& str)
2044 {
2045 wxString strResult;
2046 strResult.Alloc(str.Len());
2047
2048 for ( const wxChar *pc = str.c_str(); *pc != wxT('\0'); pc++ ) {
2049 const wxChar c = *pc;
2050
2051 // we explicitly allow some of "safe" chars and 8bit ASCII characters
2052 // which will probably never have special meaning and with which we can't
2053 // use isalnum() anyhow (in ASCII built, in Unicode it's just fine)
2054 //
2055 // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR
2056 // should *not* be quoted
2057 if (
2058 #if !wxUSE_UNICODE
2059 ((unsigned char)c < 127) &&
2060 #endif // ANSI
2061 !wxIsalnum(c) && !wxStrchr(wxT("@_/-!.*%"), c) )
2062 {
2063 strResult += wxT('\\');
2064 }
2065
2066 strResult += c;
2067 }
2068
2069 return strResult;
2070 }
2071
2072 // we can't put ?: in the ctor initializer list because it confuses some
2073 // broken compilers (Borland C++)
2074 static wxString GetAppName(const wxString& appName)
2075 {
2076 if ( !appName && wxTheApp )
2077 return wxTheApp->GetAppName();
2078 else
2079 return appName;
2080 }
2081
2082 #endif // wxUSE_CONFIG