X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/004c69ab7b4d4de3620bb3076e42742375b2cb19..ecffe99226f1f13659cdc965b5b5d18692fd25f6:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index af6e490dca..a4cefd642a 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -14,19 +14,18 @@ #pragma implementation "fileconf.h" #endif -// ============================================================================ -// declarations -// ============================================================================ - // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- + #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif //__BORLANDC__ +#if wxUSE_CONFIG + #ifndef WX_PRECOMP #include "wx/string.h" #include "wx/intl.h" @@ -47,10 +46,20 @@ #if defined(__WXMSW__) && !defined(_WINDOWS_) #include #endif //windows.h +#if defined(__WXPM__) + #define INCL_DOS + #include +#endif #include #include +// headers needed for umask() +#ifdef __UNIX__ + #include + #include +#endif // __UNIX__ + // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -68,8 +77,8 @@ // ---------------------------------------------------------------------------- // compare functions for sorting the arrays -static int CompareEntries(ConfigEntry *p1, ConfigEntry *p2); -static int CompareGroups(ConfigGroup *p1, ConfigGroup *p2); +static int LINKAGEMODE CompareEntries(ConfigEntry *p1, ConfigEntry *p2); +static int LINKAGEMODE CompareGroups(ConfigGroup *p1, ConfigGroup *p2); // filter strings static wxString FilterInValue(const wxString& str); @@ -92,18 +101,111 @@ wxString wxFileConfig::GetGlobalDir() { wxString strDir; - #ifdef __UNIX__ - strDir = _T("/etc/"); + #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined + strDir = wxT("sys$manager:"); + #elif defined( __UNIX__ ) + strDir = wxT("/etc/"); + #elif defined(__WXPM__) + ULONG aulSysInfo[QSV_MAX] = {0}; + UINT drive; + APIRET rc; + + rc = DosQuerySysInfo( 1L, QSV_MAX, (PVOID)aulSysInfo, sizeof(ULONG)*QSV_MAX); + if (rc == 0) + { + drive = aulSysInfo[QSV_BOOT_DRIVE - 1]; + switch(drive) + { + case 1: + strDir = "A:\\OS2\\"; + break; + case 2: + strDir = "B:\\OS2\\"; + break; + case 3: + strDir = "C:\\OS2\\"; + break; + case 4: + strDir = "D:\\OS2\\"; + break; + case 5: + strDir = "E:\\OS2\\"; + break; + case 6: + strDir = "F:\\OS2\\"; + break; + case 7: + strDir = "G:\\OS2\\"; + break; + case 8: + strDir = "H:\\OS2\\"; + break; + case 9: + strDir = "I:\\OS2\\"; + break; + case 10: + strDir = "J:\\OS2\\"; + break; + case 11: + strDir = "K:\\OS2\\"; + break; + case 12: + strDir = "L:\\OS2\\"; + break; + case 13: + strDir = "M:\\OS2\\"; + break; + case 14: + strDir = "N:\\OS2\\"; + break; + case 15: + strDir = "O:\\OS2\\"; + break; + case 16: + strDir = "P:\\OS2\\"; + break; + case 17: + strDir = "Q:\\OS2\\"; + break; + case 18: + strDir = "R:\\OS2\\"; + break; + case 19: + strDir = "S:\\OS2\\"; + break; + case 20: + strDir = "T:\\OS2\\"; + break; + case 21: + strDir = "U:\\OS2\\"; + break; + case 22: + strDir = "V:\\OS2\\"; + break; + case 23: + strDir = "W:\\OS2\\"; + break; + case 24: + strDir = "X:\\OS2\\"; + break; + case 25: + strDir = "Y:\\OS2\\"; + break; + case 26: + strDir = "Z:\\OS2\\"; + break; + } + } #elif defined(__WXSTUBS__) - wxASSERT_MSG( FALSE, _T("TODO") ) ; + wxASSERT_MSG( FALSE, wxT("TODO") ) ; #elif defined(__WXMAC__) - wxASSERT_MSG( FALSE, _T("TODO") ) ; + strDir = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; #else // Windows wxChar szWinDir[MAX_PATH]; ::GetWindowsDirectory(szWinDir, MAX_PATH); strDir = szWinDir; - strDir << _T('\\'); + strDir << wxT('\\'); #endif // Unix/Windows return strDir; @@ -113,12 +215,20 @@ wxString wxFileConfig::GetLocalDir() { wxString strDir; +#ifndef __WXMAC__ wxGetHomeDir(&strDir); #ifdef __UNIX__ - if (strDir.Last() != _T('/')) strDir << _T('/'); +#ifdef __VMS + if (strDir.Last() != wxT(']')) +#endif + if (strDir.Last() != wxT('/')) strDir << wxT('/'); #else - if (strDir.Last() != _T('\\')) strDir << _T('\\'); + if (strDir.Last() != wxT('\\')) strDir << wxT('\\'); +#endif +#else + // no local dir concept on mac + return GetGlobalDir() ; #endif return strDir; @@ -129,11 +239,13 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) wxString str = GetGlobalDir(); str << szFile; - if ( wxStrchr(szFile, _T('.')) == NULL ) + if ( wxStrchr(szFile, wxT('.')) == NULL ) #ifdef __UNIX__ - str << _T(".conf"); + str << wxT(".conf"); + #elif defined( __WXMAC__ ) + str << " Preferences"; #else // Windows - str << _T(".ini"); + str << wxT(".ini"); #endif // UNIX/Win return str; @@ -141,19 +253,29 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) { - wxString str = GetLocalDir(); - - #ifdef __UNIX__ - str << _T('.'); +#ifdef __VMS__ // On VMS I saw the problem that the home directory was appended + // twice for the configuration file. Does that also happen for other + // platforms? + wxString str = wxT( '.' ); +#else + wxString str = GetLocalDir(); +#endif + + #if defined( __UNIX__ ) && !defined( __VMS ) + str << wxT('.'); #endif str << szFile; #ifdef __WXMSW__ - if ( wxStrchr(szFile, _T('.')) == NULL ) - str << _T(".ini"); + if ( wxStrchr(szFile, wxT('.')) == NULL ) + str << wxT(".ini"); #endif + + #ifdef __WXMAC__ + str << " Preferences"; + #endif return str; } @@ -226,20 +348,26 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE); // if the path is not absolute, prepend the standard directory to it - if ( !m_strLocalFile.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile) ) + // UNLESS wxCONFIG_USE_RELATIVE_PATH style is set + if ( !(style & wxCONFIG_USE_RELATIVE_PATH) ) { - wxString strLocal = m_strLocalFile; - m_strLocalFile = GetLocalDir(); - m_strLocalFile << strLocal; - } + if ( !m_strLocalFile.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile) ) + { + wxString strLocal = m_strLocalFile; + m_strLocalFile = GetLocalDir(); + m_strLocalFile << strLocal; + } - if ( !m_strGlobalFile.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile) ) - { - wxString strGlobal = m_strGlobalFile; - m_strGlobalFile = GetGlobalDir(); - m_strGlobalFile << strGlobal; + if ( !m_strGlobalFile.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile) ) + { + wxString strGlobal = m_strGlobalFile; + m_strGlobalFile = GetGlobalDir(); + m_strGlobalFile << strGlobal; + } } + SetUmask(-1); + Init(); } @@ -285,18 +413,25 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) ; // skip blank/comment lines - if ( *pStart == _T('\0')|| *pStart == _T(';') || *pStart == _T('#') ) + if ( *pStart == wxT('\0')|| *pStart == wxT(';') || *pStart == wxT('#') ) continue; - if ( *pStart == _T('[') ) { // a new group + if ( *pStart == wxT('[') ) { // a new group pEnd = pStart; - while ( *++pEnd != _T(']') ) { - if ( *pEnd == _T('\n') || *pEnd == _T('\0') ) + while ( *++pEnd != wxT(']') ) { + if ( *pEnd == wxT('\\') ) { + // the next char is escaped, so skip it even if it is ']' + pEnd++; + } + + if ( *pEnd == wxT('\n') || *pEnd == wxT('\0') ) { + // we reached the end of line, break out of the loop break; + } } - if ( *pEnd != _T(']') ) { + if ( *pEnd != wxT(']') ) { wxLogError(_("file '%s': unexpected character %c at line %d."), file.GetName(), *pEnd, n + 1); continue; // skip this line @@ -316,21 +451,20 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) // check that there is nothing except comments left on this line bool bCont = TRUE; - while ( *++pEnd != _T('\0') && bCont ) { + while ( *++pEnd != wxT('\0') && bCont ) { switch ( *pEnd ) { - case _T('#'): - case _T(';'): + case wxT('#'): + case wxT(';'): bCont = FALSE; break; - case _T(' '): - case _T('\t'): + case wxT(' '): + case wxT('\t'): // ignore whitespace ('\n' impossible here) break; default: - wxLogWarning(_("file '%s', line %d: '%s' " - "ignored after group header."), + wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."), file.GetName(), n + 1, pEnd); bCont = FALSE; } @@ -338,11 +472,15 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) } else { // a key const wxChar *pEnd = pStart; - while ( !wxIsspace(*pEnd) ) { - if ( *pEnd == _T('\\') ) { + while ( *pEnd && *pEnd != wxT('=') && !wxIsspace(*pEnd) ) { + if ( *pEnd == wxT('\\') ) { // next character may be space or not - still take it because it's - // quoted + // quoted (unless there is nothing) pEnd++; + if ( !*pEnd ) { + // the error message will be given below anyhow + break; + } } pEnd++; @@ -351,10 +489,10 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) wxString strKey(FilterInEntryName(wxString(pStart, pEnd))); // skip whitespace - while ( isspace(*pEnd) ) + while ( wxIsspace(*pEnd) ) pEnd++; - if ( *pEnd++ != _T('=') ) { + if ( *pEnd++ != wxT('=') ) { wxLogError(_("file '%s', line %d: '=' expected."), file.GetName(), n + 1); } @@ -371,8 +509,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) else { if ( bLocal && pEntry->IsImmutable() ) { // immutable keys can't be changed by user - wxLogWarning(_("file '%s', line %d: value for " - "immutable key '%s' ignored."), + wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."), file.GetName(), n + 1, strKey.c_str()); continue; } @@ -382,8 +519,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) // (c) key from global file now found in local one // which is exactly what we want. else if ( !bLocal || pEntry->IsLocal() ) { - wxLogWarning(_("file '%s', line %d: key '%s' was first " - "found at line %d."), + wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."), file.GetName(), n + 1, strKey.c_str(), pEntry->Line()); if ( bLocal ) @@ -461,7 +597,7 @@ bool wxFileConfig::GetFirstGroup(wxString& str, long& lIndex) const bool wxFileConfig::GetNextGroup (wxString& str, long& lIndex) const { if ( size_t(lIndex) < m_pCurrentGroup->Groups().Count() ) { - str = m_pCurrentGroup->Groups()[lIndex++]->Name(); + str = m_pCurrentGroup->Groups()[(size_t)lIndex++]->Name(); return TRUE; } else @@ -477,7 +613,7 @@ bool wxFileConfig::GetFirstEntry(wxString& str, long& lIndex) const bool wxFileConfig::GetNextEntry (wxString& str, long& lIndex) const { if ( size_t(lIndex) < m_pCurrentGroup->Entries().Count() ) { - str = m_pCurrentGroup->Entries()[lIndex++]->Name(); + str = m_pCurrentGroup->Entries()[(size_t)lIndex++]->Name(); return TRUE; } else @@ -549,10 +685,9 @@ bool wxFileConfig::Read(const wxString& key, if (pEntry == NULL) { return FALSE; } - else { - *pStr = ExpandEnvVars(pEntry->Value()); - return TRUE; - } + + *pStr = ExpandEnvVars(pEntry->Value()); + return TRUE; } bool wxFileConfig::Read(const wxString& key, @@ -561,28 +696,31 @@ bool wxFileConfig::Read(const wxString& key, wxConfigPathChanger path(this, key); ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name()); + bool ok; if (pEntry == NULL) { if( IsRecordingDefaults() ) ((wxFileConfig *)this)->Write(key,defVal); *pStr = ExpandEnvVars(defVal); - return FALSE; + ok = FALSE; } else { *pStr = ExpandEnvVars(pEntry->Value()); - return TRUE; + ok = TRUE; } + + return ok; } bool wxFileConfig::Read(const wxString& key, long *pl) const { wxString str; - if ( Read(key, & str) ) { - *pl = wxAtol(str); - return TRUE; - } - else { + if ( !Read(key, & str) ) + { return FALSE; } + + *pl = wxAtol(str); + return TRUE; } bool wxFileConfig::Write(const wxString& key, const wxString& szValue) @@ -592,7 +730,7 @@ bool wxFileConfig::Write(const wxString& key, const wxString& szValue) wxString strName = path.Name(); if ( strName.IsEmpty() ) { // setting the value of a group is an error - wxASSERT_MSG( wxIsEmpty(szValue), _T("can't set value of a group!") ); + wxASSERT_MSG( wxIsEmpty(szValue), wxT("can't set value of a group!") ); // ... except if it's empty in which case it's a way to force it's creation m_pCurrentGroup->SetDirty(); @@ -610,8 +748,6 @@ bool wxFileConfig::Write(const wxString& key, const wxString& szValue) return FALSE; } - strName = FilterOutEntryName(strName); - ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strName); if ( pEntry == NULL ) pEntry = m_pCurrentGroup->AddEntry(strName); @@ -626,15 +762,24 @@ bool wxFileConfig::Write(const wxString& key, long lValue) { // ltoa() is not ANSI :-( wxString buf; - buf.Printf(_T("%ld"), lValue); + buf.Printf(wxT("%ld"), lValue); return Write(key, buf); } bool wxFileConfig::Flush(bool /* bCurrentOnly */) { - if ( LineListIsEmpty() || !m_pRootGroup->IsDirty() ) + if ( LineListIsEmpty() || !m_pRootGroup->IsDirty() || !m_strLocalFile ) return TRUE; +#ifdef __UNIX__ + // set the umask if needed + mode_t umaskOld = 0; + if ( m_umask != -1 ) + { + umaskOld = umask((mode_t)m_umask); + } +#endif // __UNIX__ + wxTempFile file(m_strLocalFile); if ( !file.IsOpened() ) { @@ -650,7 +795,33 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) } } - return file.Commit(); + bool ret = file.Commit(); + +#if defined(__WXMAC__) && !defined(__UNIX__) + if ( ret ) + { + FSSpec spec ; + + wxMacFilename2FSSpec( m_strLocalFile , &spec ) ; + FInfo finfo ; + if ( FSpGetFInfo( &spec , &finfo ) == noErr ) + { + finfo.fdType = 'TEXT' ; + finfo.fdCreator = 'ttxt' ; + FSpSetFInfo( &spec , &finfo ) ; + } + } +#endif // __WXMAC__ && !__UNIX__ + +#ifdef __UNIX__ + // restore the old umask if we changed it + if ( m_umask != -1 ) + { + (void)umask(umaskOld); + } +#endif // __UNIX__ + + return ret; } // ---------------------------------------------------------------------------- @@ -711,7 +882,7 @@ bool wxFileConfig::DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso) if ( bGroupIfEmptyAlso && m_pCurrentGroup->IsEmpty() ) { if ( m_pCurrentGroup != m_pRootGroup ) { ConfigGroup *pGroup = m_pCurrentGroup; - SetPath(_T("..")); // changes m_pCurrentGroup! + SetPath(wxT("..")); // changes m_pCurrentGroup! m_pCurrentGroup->DeleteSubgroupByName(pGroup->Name()); } //else: never delete the root group @@ -731,10 +902,10 @@ bool wxFileConfig::DeleteAll() { CleanUp(); - if ( remove(m_strLocalFile.fn_str()) == -1 ) + if ( wxRemove(m_strLocalFile) == -1 ) wxLogSysError(_("can't delete user configuration file '%s'"), m_strLocalFile.c_str()); - m_strLocalFile = m_strGlobalFile = _T(""); + m_strLocalFile = m_strGlobalFile = wxT(""); Init(); return TRUE; @@ -906,7 +1077,10 @@ LineList *ConfigGroup::GetGroupLine() // this group wasn't present in local config file, add it now if ( pParent != NULL ) { wxString strFullName; - strFullName << _T("[") << (GetFullName().c_str() + 1) << _T("]"); // +1: no '/' + strFullName << wxT("[") + // +1: no '/' + << FilterOutEntryName(GetFullName().c_str() + 1) + << wxT("]"); m_pLine = m_pConfig->LineListInsert(strFullName, pParent->GetLastGroupLine()); pParent->SetLastGroup(this); // we're surely after all the others @@ -964,7 +1138,7 @@ void ConfigGroup::Rename(const wxString& newName) LineList *line = GetGroupLine(); wxString strFullName; - strFullName << _T("[") << (GetFullName().c_str() + 1) << _T("]"); // +1: no '/' + strFullName << wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); // +1: no '/' line->SetText(strFullName); SetDirty(); @@ -975,7 +1149,7 @@ wxString ConfigGroup::GetFullName() const if ( Parent() ) return Parent()->GetFullName() + wxCONFIG_PATH_SEPARATOR + Name(); else - return _T(""); + return wxT(""); } // ---------------------------------------------------------------------------- @@ -1273,7 +1447,7 @@ void ConfigEntry::SetValue(const wxString& strValue, bool bUser) if ( bUser ) { wxString strVal = FilterOutValue(strValue); wxString strLine; - strLine << m_strName << _T(" = ") << strVal; + strLine << FilterOutEntryName(m_strName) << wxT('=') << strVal; if ( m_pLine != NULL ) { // entry was read from the local config file, just modify the line @@ -1339,31 +1513,31 @@ static wxString FilterInValue(const wxString& str) bool bQuoted = !str.IsEmpty() && str[0] == '"'; for ( size_t n = bQuoted ? 1 : 0; n < str.Len(); n++ ) { - if ( str[n] == _T('\\') ) { + if ( str[n] == wxT('\\') ) { switch ( str[++n] ) { - case _T('n'): - strResult += _T('\n'); + case wxT('n'): + strResult += wxT('\n'); break; - case _T('r'): - strResult += _T('\r'); + case wxT('r'): + strResult += wxT('\r'); break; - case _T('t'): - strResult += _T('\t'); + case wxT('t'): + strResult += wxT('\t'); break; - case _T('\\'): - strResult += _T('\\'); + case wxT('\\'): + strResult += wxT('\\'); break; - case _T('"'): - strResult += _T('"'); + case wxT('"'): + strResult += wxT('"'); break; } } else { - if ( str[n] != _T('"') || !bQuoted ) + if ( str[n] != wxT('"') || !bQuoted ) strResult += str[n]; else if ( n != str.Len() - 1 ) { wxLogWarning(_("unexpected \" at position %d in '%s'."), @@ -1386,33 +1560,33 @@ static wxString FilterOutValue(const wxString& str) strResult.Alloc(str.Len()); // quoting is necessary to preserve spaces in the beginning of the string - bool bQuote = wxIsspace(str[0]) || str[0] == _T('"'); + bool bQuote = wxIsspace(str[0]) || str[0] == wxT('"'); if ( bQuote ) - strResult += _T('"'); + strResult += wxT('"'); wxChar c; for ( size_t n = 0; n < str.Len(); n++ ) { switch ( str[n] ) { - case _T('\n'): - c = _T('n'); + case wxT('\n'): + c = wxT('n'); break; - case _T('\r'): - c = _T('r'); + case wxT('\r'): + c = wxT('r'); break; - case _T('\t'): - c = _T('t'); + case wxT('\t'): + c = wxT('t'); break; - case _T('\\'): - c = _T('\\'); + case wxT('\\'): + c = wxT('\\'); break; - case _T('"'): + case wxT('"'): if ( bQuote ) { - c = _T('"'); + c = wxT('"'); break; } //else: fall through @@ -1423,11 +1597,11 @@ static wxString FilterOutValue(const wxString& str) } // we get here only for special characters - strResult << _T('\\') << c; + strResult << wxT('\\') << c; } if ( bQuote ) - strResult += _T('"'); + strResult += wxT('"'); return strResult; } @@ -1439,7 +1613,7 @@ static wxString FilterInEntryName(const wxString& str) strResult.Alloc(str.Len()); for ( const wxChar *pc = str.c_str(); *pc != '\0'; pc++ ) { - if ( *pc == _T('\\') ) + if ( *pc == wxT('\\') ) pc++; strResult += *pc; @@ -1454,15 +1628,15 @@ static wxString FilterOutEntryName(const wxString& str) wxString strResult; strResult.Alloc(str.Len()); - for ( const wxChar *pc = str.c_str(); *pc != _T('\0'); pc++ ) { + for ( const wxChar *pc = str.c_str(); *pc != wxT('\0'); pc++ ) { wxChar c = *pc; // we explicitly allow some of "safe" chars and 8bit ASCII characters // which will probably never have special meaning // NB: note that wxCONFIG_IMMUTABLE_PREFIX and wxCONFIG_PATH_SEPARATOR // should *not* be quoted - if ( !wxIsalnum(c) && !wxStrchr(_T("@_/-!.*%"), c) && ((c & 0x80) == 0) ) - strResult += _T('\\'); + if ( !wxIsalnum(c) && !wxStrchr(wxT("@_/-!.*%"), c) && ((c & 0x80) == 0) ) + strResult += wxT('\\'); strResult += c; } @@ -1479,3 +1653,6 @@ static wxString GetAppName(const wxString& appName) else return appName; } + +#endif // wxUSE_CONFIG +