From 614108e211ecb6e11d32b646a91437061116b518 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 30 Sep 2012 22:28:08 +0000 Subject: [PATCH] Added wxFile::ReadAll() for consistency with wxFFile::ReadAll(). 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 | 1 + include/wx/file.h | 4 +++- interface/wx/file.h | 17 +++++++++++++++++ src/common/file.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index b732d87b69..f6c12193c9 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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): diff --git a/include/wx/file.h b/include/wx/file.h index 044e891fa8..75471c3b62 100644 --- a/include/wx/file.h +++ b/include/wx/file.h @@ -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 diff --git a/interface/wx/file.h b/interface/wx/file.h index 9c01ebdd7d..198fcc18cb 100644 --- a/interface/wx/file.h +++ b/interface/wx/file.h @@ -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. diff --git a/src/common/file.cpp b/src/common/file.cpp index bc6be8c1c4..02a15cb2ca 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -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) { -- 2.45.2