]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/textfile.cpp
fixed bug in Set(double jdn): don't test whether the date is valid before setting...
[wxWidgets.git] / src / common / textfile.cpp
index 26053922cd0ada639c9c47e9758cdc7b4a9de14e..25503be862e36806246222c6d8246dc56e64ad19 100644 (file)
@@ -6,14 +6,14 @@
 // Created:     03.04.98
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
 // headers
 // ============================================================================
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "textfile.h"
 #endif
 
@@ -91,97 +91,110 @@ bool wxTextFile::OnClose()
 
 bool wxTextFile::OnRead(wxMBConv& conv)
 {
-  // file should be opened and we must be in it's beginning
-  wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
-
-#if wxUSE_UNICODE
-  char conv_mbBuf[2];
-  wchar_t conv_wcBuf[2];
-  conv_mbBuf[1] = 0;
-#else
-  (void)conv;
-#endif
+    // file should be opened and we must be in it's beginning
+    wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
 
-  wxString str;
-  char ch, chLast = '\0';
-  char buf[1024];
-  int n, nRead;
-  do {
-    nRead = m_file.Read(buf, WXSIZEOF(buf));
-    if ( nRead == wxInvalidOffset ) {
-      // read error (error message already given in wxFile::Read)
-      return FALSE;
-    }
+    char *strBuf, *strPtr, *strEnd;
+    char ch, chLast = '\0';
+    char buf[1024];
+    size_t nRead;
 
-    for ( n = 0; n < nRead; n++ ) {
-      ch = buf[n];
-      switch ( ch ) {
-        case '\n':
-          // Dos/Unix line termination
-                AddLine(str, chLast == '\r' ? wxTextFileType_Dos
-                                      : wxTextFileType_Unix);
-          str.Empty();
-          chLast = '\n';
-          break;
-
-        case '\r':
-          if ( chLast == '\r' ) {
-            // Mac empty line
-                    AddLine(wxEmptyString, wxTextFileType_Mac);
-          }
-          else
-            chLast = '\r';
-          break;
+    strPtr = strBuf = new char[1024];
+    strEnd = strBuf + 1024;
 
-        default:
-          if ( chLast == '\r' ) {
-            // Mac line termination
-                    AddLine(str, wxTextFileType_Mac);
-            chLast = ch;
-#if wxUSE_UNICODE
-            conv_mbBuf[0] = ch;
-            if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
-                conv_wcBuf[0] = ch;
-            str = conv_wcBuf[0];
-#else
-            str = ch;
-#endif
-          }
-          else {
-            // add to the current line
-#if wxUSE_UNICODE
-            conv_mbBuf[0] = ch;
-            if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
-                conv_wcBuf[0] = ch;
-            str += conv_wcBuf[0];
-#else
-            str += ch;
-#endif
-          }
-      }
+    do
+    {
+        nRead = m_file.Read(buf, WXSIZEOF(buf));
+        if ( nRead == (size_t)wxInvalidOffset )
+        {
+            // read error (error message already given in wxFile::Read)
+            delete[] strBuf;
+            return false;
+        }
+
+        for (size_t n = 0; n < nRead; n++)
+        {
+            ch = buf[n];
+            switch ( ch )
+            {
+                case '\n':
+                    // Dos/Unix line termination
+                    *strPtr = '\0';
+                    AddLine(wxString(strBuf, conv),
+                            chLast == '\r' ? wxTextFileType_Dos
+                                           : wxTextFileType_Unix);
+                    strPtr = strBuf;
+                    chLast = '\n';
+                    break;
+
+                case '\r':
+                    if ( chLast == '\r' )
+                    {
+                        // Mac empty line
+                        AddLine(wxEmptyString, wxTextFileType_Mac);
+                    }
+                    else
+                        chLast = '\r';
+                    break;
+
+                default:
+                    if ( chLast == '\r' )
+                    {
+                        // Mac line termination
+                        *strPtr = '\0';
+                        AddLine(wxString(strBuf, conv), wxTextFileType_Mac);
+                        chLast = ch;
+                        strPtr = strBuf;
+                        *(strPtr++) = ch;
+                    }
+                    else
+                    {
+                        // add to the current line
+                        *(strPtr++) = ch;
+                        if ( strPtr == strEnd )
+                        {
+                            // we must allocate more memory
+                            size_t size = strEnd - strBuf;
+                            char *newBuf = new char[size + 1024];
+                            memcpy(newBuf, strBuf, size);
+                            delete[] strBuf;
+                            strBuf = newBuf;
+                            strEnd = strBuf + size + 1024;
+                            strPtr = strBuf + size;
+                        }
+                    }
+            }
+        }
+    } while ( nRead == WXSIZEOF(buf) );
+
+    // anything in the last line?
+    if ( strPtr != strBuf )
+    {
+        *strPtr = '\0';
+        AddLine(wxString(strBuf, conv),
+                wxTextFileType_None); // no line terminator
     }
-  } while ( nRead == WXSIZEOF(buf) );
 
-  // anything in the last line?
-  if ( !str.IsEmpty() ) {
-        AddLine(str, wxTextFileType_None);  // no line terminator
-  }
-
-  return TRUE;
+    delete[] strBuf;
+    return true;
 }
 
 
 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
 {
     wxFileName fn = m_strBufferName;
+
+    // We do NOT want wxPATH_NORM_CASE here, or the case will not
+    // be preserved.
     if ( !fn.IsAbsolute() )
-        fn.Normalize();
+        fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
+                     wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
 
-    wxTempFile fileTmp(fn.GetFullName());
+    wxTempFile fileTmp(fn.GetFullPath());
 
     if ( !fileTmp.IsOpened() ) {
         wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
-        return FALSE;
+        return false;
     }
 
     size_t nCount = GetLineCount();