X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1d910ac1fcebc69cda4c0b85278af09a55c9a611..4fdae9972d39fbe62adaa1c1d721ba1762cd8950:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 8c96c4d6bd..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 // ---------------------------------------------------------------------------- @@ -227,13 +230,14 @@ wxString wxFileConfig::GetLocalDir() #ifndef __WXMAC__ wxGetHomeDir(&strDir); -#ifndef __VMS__ -# ifdef __UNIX__ - if (strDir.Last() != wxT('/')) strDir << wxT('/'); +#ifdef __UNIX__ +#ifdef __VMS + if (strDir.Last() != wxT(']')) +#endif + if (strDir.Last() != wxT('/')) strDir << wxT('/'); #else if (strDir.Last() != wxT('\\')) strDir << wxT('\\'); #endif -#endif #else // no local dir concept on mac return GetGlobalDir() ; @@ -264,12 +268,12 @@ wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) #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( ' ' ); + wxString str = wxT( '.' ); #else wxString str = GetLocalDir(); #endif - #ifdef __UNIX__ + #if defined( __UNIX__ ) && !defined( __VMS ) str << wxT('.'); #endif @@ -374,6 +378,8 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, } } + SetUmask(-1); + Init(); } @@ -426,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(']') ) { @@ -463,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; } @@ -472,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++; @@ -485,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('=') ) { @@ -505,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; } @@ -516,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 ) @@ -683,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, @@ -695,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) @@ -767,6 +783,15 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) 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() ) { @@ -782,10 +807,9 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) } } -#ifndef __WXMAC__ - return file.Commit(); -#else bool ret = file.Commit(); + +#ifdef __WXMAC__ if ( ret ) { FSSpec spec ; @@ -799,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; } // ---------------------------------------------------------------------------- @@ -881,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("");