X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7c74e7fe1de14f32dcd6f3c9cdd727de540bbd0e..8e19f54e55255ddef1df49acde530102f5db2492:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 80b528c70e..0d5b0a4ae3 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -49,14 +49,17 @@ #if defined(__WXPM__) #define INCL_DOS #include - #define LINKAGEMODE _Optlink -#else - #define LINKAGEMODE #endif #include #include +// headers needed for umask() +#ifdef __UNIX__ + #include + #include +#endif // __UNIX__ + // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -98,7 +101,9 @@ wxString wxFileConfig::GetGlobalDir() { wxString strDir; - #ifdef __UNIX__ + #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}; @@ -226,7 +231,10 @@ wxString wxFileConfig::GetLocalDir() wxGetHomeDir(&strDir); #ifdef __UNIX__ - if (strDir.Last() != wxT('/')) strDir << wxT('/'); +#ifdef __VMS + if (strDir.Last() != wxT(']')) +#endif + if (strDir.Last() != wxT('/')) strDir << wxT('/'); #else if (strDir.Last() != wxT('\\')) strDir << wxT('\\'); #endif @@ -257,9 +265,15 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) { - wxString str = GetLocalDir(); - - #ifdef __UNIX__ +#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 @@ -364,6 +378,8 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, } } + SetUmask(-1); + Init(); } @@ -416,8 +432,15 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) pEnd = pStart; while ( *++pEnd != wxT(']') ) { - if ( *pEnd == wxT('\n') || *pEnd == wxT('\0') ) + 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 != wxT(']') ) { @@ -453,8 +476,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) 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; } @@ -462,11 +484,15 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) } else { // a key const wxChar *pEnd = pStart; - while ( *pEnd != wxT('=') && !wxIsspace(*pEnd) ) { + 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++; @@ -475,7 +501,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) wxString strKey(FilterInEntryName(wxString(pStart, pEnd))); // skip whitespace - while ( isspace(*pEnd) ) + while ( wxIsspace(*pEnd) ) pEnd++; if ( *pEnd++ != wxT('=') ) { @@ -495,8 +521,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; } @@ -506,8 +531,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 ) @@ -585,7 +609,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 @@ -601,7 +625,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 @@ -673,10 +697,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, @@ -685,28 +708,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) @@ -754,9 +780,18 @@ bool wxFileConfig::Write(const wxString& key, long lValue) 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() ) { @@ -772,10 +807,9 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) } } -#ifndef __WXMAC__ - return file.Commit(); -#else bool ret = file.Commit(); + +#ifdef __WXMAC__ if ( ret ) { FSSpec spec ; @@ -789,8 +823,17 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) FSpSetFInfo( &spec , &finfo ) ; } } - return ret ; -#endif +#endif // __WXMAC__ + +#ifdef __UNIX__ + // restore the old umask if we changed it + if ( m_umask != -1 ) + { + (void)umask(umaskOld); + } +#endif // __UNIX__ + + return ret; } // ---------------------------------------------------------------------------- @@ -871,7 +914,7 @@ 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 = wxT("");