]> git.saurik.com Git - wxWidgets.git/commitdiff
speed up reading wxFileConfig from wxInputStream by factors of 3.5, 20 and 7 for...
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 18 Jul 2007 23:37:12 +0000 (23:37 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 18 Jul 2007 23:37:12 +0000 (23:37 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47555 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/fileconf.cpp

index 7a82bf45aebc553ce04dfa65a3fa61053904d81c..0334138412d767b05070f9862f7761928ec1ce79 100644 (file)
@@ -437,70 +437,59 @@ wxFileConfig::wxFileConfig(wxInputStream &inStream, const wxMBConv& conv)
     m_linesTail = NULL;
 
     // read the entire stream contents in memory
     m_linesTail = NULL;
 
     // read the entire stream contents in memory
-    wxString str;
-    {
-        static const size_t chunkLen = 1024;
+    wxWxCharBuffer cbuf;
+    static const size_t chunkLen = 1024;
 
 
-        wxMemoryBuffer buf(chunkLen);
-        do
-        {
-            inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen);
-            buf.UngetAppendBuf(inStream.LastRead());
+    wxMemoryBuffer buf(chunkLen);
+    do
+    {
+        inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen);
+        buf.UngetAppendBuf(inStream.LastRead());
 
 
-            const wxStreamError err = inStream.GetLastError();
+        const wxStreamError err = inStream.GetLastError();
 
 
-            if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF )
-            {
-                wxLogError(_("Error reading config options."));
-                break;
-            }
+        if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF )
+        {
+            wxLogError(_("Error reading config options."));
+            break;
         }
         }
-        while ( !inStream.Eof() );
+    }
+    while ( !inStream.Eof() );
 
 #if wxUSE_UNICODE
 
 #if wxUSE_UNICODE
-        size_t len;
-        str = conv.cMB2WC((char *)buf.GetData(), buf.GetDataLen(), &len);
-        if ( !len && buf.GetDataLen() )
-        {
-            wxLogError(_("Failed to read config options."));
-        }
+    size_t len;
+    cbuf = conv.cMB2WC((char *)buf.GetData(), buf.GetDataLen(), &len);
+    if ( !len && buf.GetDataLen() )
+    {
+        wxLogError(_("Failed to read config options."));
+    }
 #else // !wxUSE_UNICODE
 #else // !wxUSE_UNICODE
-        // no need for conversion
-        str.assign((char *)buf.GetData(), buf.GetDataLen());
+    // no need for conversion
+    cbuf = wxCharBuffer::CreateNonOwned((char *)buf.GetData());
 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
-    }
 
 
 
 
-    // translate everything to the current (platform-dependent) line
-    // termination character
-    str = wxTextBuffer::Translate(str);
-
+    // now break it into lines
     wxMemoryText memText;
     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 = str.Find(pEOL);
-    while ( posLineStart != -1 )
+    for ( const wxChar *s = cbuf; ; ++s )
     {
     {
-        wxString line(str.Left(posLineStart));
+        const wxChar *e = s;
+        while ( *e != '\0' && *e != '\n' && *e != '\r' )
+            ++e;
 
 
-        memText.AddLine(line);
+        // notice that we throw away the original EOL kind here, maybe we
+        // should preserve it?
+        memText.AddLine(wxString(s, e));
 
 
-        str = str.Mid(posLineStart + EOLLen);
+        if ( *e == '\0' )
+            break;
 
 
-        posLineStart = str.Find(pEOL);
-    }
+        // skip the second EOL byte if it's a DOS one
+        if ( *e == '\r' && e[1] == '\n' )
+            ++e;
 
 
-    // also add whatever we have left in the translated string.
-    if ( !str.empty() )
-        memText.AddLine(str);
+        s = e;
+    }
 
     // Finally we can parse it all.
     Parse(memText, true /* local */);
 
     // Finally we can parse it all.
     Parse(memText, true /* local */);