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