X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0516e0e8ecfb57e9241d84c7e78bdbc9d29ab771..ef3a5e0aae003c29941c7d41ed6becea4287752d:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 256655447a..aede4fa655 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -36,9 +36,14 @@ #include "wx/file.h" #include "wx/log.h" #include "wx/textfile.h" +#include "wx/memtext.h" #include "wx/config.h" #include "wx/fileconf.h" +#if wxUSE_STREAMS + #include "wx/stream.h" +#endif // wxUSE_STREAMS + #include "wx/utils.h" // for wxGetHomeDir // _WINDOWS_ is defined when windows.h is included, @@ -99,8 +104,8 @@ static wxString GetAppName(const wxString& appname); // "template" array types // ---------------------------------------------------------------------------- -WX_DEFINE_SORTED_ARRAY(wxFileConfigEntry *, ArrayEntries); -WX_DEFINE_SORTED_ARRAY(wxFileConfigGroup *, ArrayGroups); +WX_DEFINE_SORTED_EXPORTED_ARRAY(wxFileConfigEntry *, ArrayEntries); +WX_DEFINE_SORTED_EXPORTED_ARRAY(wxFileConfigGroup *, ArrayGroups); // ---------------------------------------------------------------------------- // wxFileConfigLineList @@ -256,6 +261,8 @@ wxString wxFileConfig::GetGlobalDir() #ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined strDir = wxT("sys$manager:"); + #elif defined(__WXMAC__) + strDir = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; #elif defined( __UNIX__ ) strDir = wxT("/etc/"); #elif defined(__WXPM__) @@ -351,8 +358,6 @@ wxString wxFileConfig::GetGlobalDir() } #elif defined(__WXSTUBS__) wxASSERT_MSG( FALSE, wxT("TODO") ) ; - #elif defined(__WXMAC__) - strDir = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; #else // Windows wxChar szWinDir[MAX_PATH]; ::GetWindowsDirectory(szWinDir, MAX_PATH); @@ -368,20 +373,20 @@ wxString wxFileConfig::GetLocalDir() { wxString strDir; -#ifndef __WXMAC__ +#if defined(__WXMAC__) + // no local dir concept on Mac OS 9 + return GetGlobalDir() ; +#else wxGetHomeDir(&strDir); -#ifdef __UNIX__ -#ifdef __VMS - if (strDir.Last() != wxT(']')) -#endif - if (strDir.Last() != wxT('/')) strDir << wxT('/'); -#else +# ifdef __UNIX__ +# ifdef __VMS + if (strDir.Last() != wxT(']')) +# endif + if (strDir.Last() != wxT('/')) strDir << wxT('/'); +# else if (strDir.Last() != wxT('\\')) strDir << wxT('\\'); -#endif -#else - // no local dir concept on mac - return GetGlobalDir() ; +# endif #endif return strDir; @@ -393,10 +398,10 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) str << szFile; if ( wxStrchr(szFile, wxT('.')) == NULL ) - #ifdef __UNIX__ - str << wxT(".conf"); - #elif defined( __WXMAC__ ) + #if defined( __WXMAC__ ) str << " Preferences"; + #elif defined( __UNIX__ ) + str << wxT(".conf"); #else // Windows str << wxT(".ini"); #endif // UNIX/Win @@ -414,7 +419,7 @@ wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) wxString str = GetLocalDir(); #endif - #if defined( __UNIX__ ) && !defined( __VMS ) + #if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ ) str << wxT('.'); #endif @@ -425,7 +430,6 @@ wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) str << wxT(".ini"); #endif - #ifdef __WXMAC__ str << " Preferences"; #endif @@ -524,6 +528,68 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, Init(); } +#if wxUSE_STREAMS + +wxFileConfig::wxFileConfig(wxInputStream &inStream) +{ + // always local_file when this constructor is called (?) + SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE); + + m_pCurrentGroup = + m_pRootGroup = new wxFileConfigGroup(NULL, "", this); + + m_linesHead = + m_linesTail = NULL; + + // translate everything to the current (platform-dependent) line + // termination character + wxString strTrans; + { + wxString strTmp; + + char buf[1024]; + while ( !inStream.Read(buf, WXSIZEOF(buf)).Eof() ) + strTmp.append(wxConvertMB2WX(buf), inStream.LastRead()); + + strTmp.append(wxConvertMB2WX(buf), inStream.LastRead()); + + strTrans = wxTextBuffer::Translate(strTmp); + } + + wxMemoryText memText; + + // Now we can add the text to the memory text. To do this we extract line + // by line from the translated string, until we've reached the end. + // + // VZ: all this is horribly inefficient, we should do the translation on + // the fly in one pass saving both memory and time (TODO) + + const wxChar *pEOL = wxTextBuffer::GetEOL(wxTextBuffer::typeDefault); + const size_t EOLLen = wxStrlen(pEOL); + + int posLineStart = strTrans.Find(pEOL); + while ( posLineStart != -1 ) + { + wxString line(strTrans.Left(posLineStart)); + + memText.AddLine(line); + + strTrans = strTrans.Mid(posLineStart + EOLLen); + + posLineStart = strTrans.Find(pEOL); + } + + // also add whatever we have left in the translated string. + memText.AddLine(strTrans); + + // Finally we can parse it all. + Parse(memText, TRUE /* local */); + + SetRootPath(); +} + +#endif // wxUSE_STREAMS + void wxFileConfig::CleanUp() { delete m_pRootGroup; @@ -547,15 +613,15 @@ wxFileConfig::~wxFileConfig() // parse a config file // ---------------------------------------------------------------------------- -void wxFileConfig::Parse(wxTextFile& file, bool bLocal) +void wxFileConfig::Parse(wxTextBuffer& buffer, bool bLocal) { const wxChar *pStart; const wxChar *pEnd; wxString strLine; - size_t nLineCount = file.GetLineCount(); + size_t nLineCount = buffer.GetLineCount(); for ( size_t n = 0; n < nLineCount; n++ ) { - strLine = file[n]; + strLine = buffer[n]; // add the line to linked list if ( bLocal ) @@ -586,7 +652,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) if ( *pEnd != wxT(']') ) { wxLogError(_("file '%s': unexpected character %c at line %d."), - file.GetName(), *pEnd, n + 1); + buffer.GetName(), *pEnd, n + 1); continue; // skip this line } @@ -618,7 +684,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) default: wxLogWarning(_("file '%s', line %d: '%s' ignored after group header."), - file.GetName(), n + 1, pEnd); + buffer.GetName(), n + 1, pEnd); bCont = FALSE; } } @@ -647,7 +713,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) if ( *pEnd++ != wxT('=') ) { wxLogError(_("file '%s', line %d: '=' expected."), - file.GetName(), n + 1); + buffer.GetName(), n + 1); } else { wxFileConfigEntry *pEntry = m_pCurrentGroup->FindEntry(strKey); @@ -663,7 +729,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) if ( bLocal && pEntry->IsImmutable() ) { // immutable keys can't be changed by user wxLogWarning(_("file '%s', line %d: value for immutable key '%s' ignored."), - file.GetName(), n + 1, strKey.c_str()); + buffer.GetName(), n + 1, strKey.c_str()); continue; } // the condition below catches the cases (a) and (b) but not (c): @@ -673,7 +739,7 @@ void wxFileConfig::Parse(wxTextFile& file, bool bLocal) // which is exactly what we want. else if ( !bLocal || pEntry->IsLocal() ) { wxLogWarning(_("file '%s', line %d: key '%s' was first found at line %d."), - file.GetName(), n + 1, strKey.c_str(), pEntry->Line()); + buffer.GetName(), n + 1, strKey.c_str(), pEntry->Line()); if ( bLocal ) pEntry->SetLine(m_linesTail); @@ -950,7 +1016,7 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) bool ret = file.Commit(); -#if defined(__WXMAC__) && !defined(__UNIX__) +#if defined(__WXMAC__) if ( ret ) { FSSpec spec ; @@ -964,7 +1030,7 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) FSpSetFInfo( &spec , &finfo ) ; } } -#endif // __WXMAC__ && !__UNIX__ +#endif // __WXMAC__ #ifdef __UNIX__ // restore the old umask if we changed it