]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxFile::ReadAll() for consistency with wxFFile::ReadAll().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 30 Sep 2012 22:28:08 +0000 (22:28 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 30 Sep 2012 22:28:08 +0000 (22:28 +0000)
Make it possible to use wxFFile and wxFile interchangeably for simply reading
the entire contents of the file as a string.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72596 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/file.h
interface/wx/file.h
src/common/file.cpp

index b732d87b69ae3e9648f139a504cd914fc869c545..f6c12193c951b3f852f4704773a380369fd6667b 100644 (file)
@@ -534,6 +534,7 @@ All:
 - Add wxDir::Close() method (Silverstorm82).
 - Fix compilation of wxHash{Map,Set} with g++ 4.7 (Nathan Ridge).
 - Fix posting large amounts of data in wxHTTP (Platonides).
+- Add wxFile::ReadAll() for consistency with wxFFile.
 - Added Nepali translation (Him Prasad Gautam).
 
 All (GUI):
index 044e891fa8a7cbbdecfe22c1a917e2d97065fe0f..75471c3b62a126487e3ee8ee6f9c9fae971d4c38 100644 (file)
@@ -19,7 +19,7 @@
 
 #include  "wx/string.h"
 #include  "wx/filefn.h"
-#include  "wx/strconv.h"
+#include  "wx/convauto.h"
 
 // ----------------------------------------------------------------------------
 // class wxFile: raw file IO
@@ -70,6 +70,8 @@ public:
   int  fd() const { return m_fd; }
 
   // read/write (unbuffered)
+    // read all data from the file into a string (useful for text files)
+  bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
     // returns number of bytes read or wxInvalidOffset on error
   ssize_t Read(void *pBuf, size_t nCount);
     // returns the number of bytes written
index 9c01ebdd7d6f9e2e9228d0140ff8f67cbb59a54f..198fcc18cbf4b16d2ed27eee36aa036df15b6a83 100644 (file)
@@ -372,6 +372,23 @@ public:
     */
     ssize_t Read(void* buffer, size_t count);
 
+    /**
+        Reads the entire contents of the file into a string.
+
+        @param str
+            Non-@NULL pointer to a string to read data into.
+        @param conv
+            Conversion object to use in Unicode build; by default supposes
+            that file contents is encoded in UTF-8 but falls back to the
+            current locale encoding (or Latin-1 if it is UTF-8 too) if it is
+            not.
+
+        @return @true if file was read successfully, @false otherwise.
+
+        @since 2.9.5
+    */
+    bool ReadAll(wxString* str, const wxMBConv& conv = wxConvAuto());
+
     /**
         Seeks to the specified position.
 
index bc6be8c1c47bb0b0dfdf1959ca5b2d6375fdfc0c..02a15cb2ca492cdc1eea6882b070c05db644d315 100644 (file)
@@ -291,6 +291,34 @@ bool wxFile::Close()
 // read/write
 // ----------------------------------------------------------------------------
 
+bool wxFile::ReadAll(wxString *str, const wxMBConv& conv)
+{
+    wxCHECK_MSG( str, false, wxS("Output string must be non-NULL") );
+
+    size_t length = wx_truncate_cast(size_t, Length());
+    wxCHECK_MSG( (wxFileOffset)length == Length(), false, wxT("huge file not supported") );
+
+    wxCharBuffer buf(length);
+    char* p = buf.data();
+    for ( ;; )
+    {
+        static const unsigned READSIZE = 4096;
+
+        ssize_t read = Read(p, length > READSIZE ? READSIZE : length);
+        if ( read == wxInvalidOffset )
+            return false;
+
+        p += read;
+    }
+
+    *p = 0;
+
+    wxString strTmp(buf, conv);
+    str->swap(strTmp);
+
+    return true;
+}
+
 // read
 ssize_t wxFile::Read(void *pBuf, size_t nCount)
 {