- // 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;
- while ( !m_file.Eof() ) {
- nRead = m_file.Read(buf, WXSIZEOF(buf));
- if ( nRead == ofsInvalid ) {
- // read error (error message already given in wxFile::Read)
- m_file.Close();
- return FALSE;
+ // file should be opened and we must be in it's beginning
+ wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
+
+ // read the entire file in memory: this is not the most efficient thing to
+ // do but there is no good way to avoid it in Unicode build because if we
+ // read the file block by block we can't convert each block to Unicode
+ // separately (the last multibyte char in the block might be only partially
+ // read and so the conversion would fail) and, as the file contents is kept
+ // in memory by wxTextFile anyhow, it shouldn't be a big problem to read
+ // the file entirely
+ const size_t bufSize = (size_t)(m_file.Length() + 4 /* for trailing NULs */ );
+ size_t bufPos = 0;
+ wxCharBuffer buf(bufSize - 1 /* it adds 1 internally */);
+
+ char block[1024];
+ for ( bool eof = false; !eof; )
+ {
+ // try to read up to the size of the entire block
+ ssize_t nRead = m_file.Read(block, WXSIZEOF(block));
+
+ if ( nRead == wxInvalidOffset )
+ {
+ // read error (error message already given in wxFile::Read)
+ return false;
+ }
+
+ if ( nRead == 0 )
+ break;
+
+ // this shouldn't happen but don't overwrite the buffer if it does
+ wxCHECK_MSG( bufPos + nRead <= bufSize, false,
+ _T("read more than file length?") );
+
+ // append to the buffer
+ memcpy(buf.data() + bufPos, block, nRead);
+ bufPos += nRead;