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