void FileTestCase::DoRoundTripTest(const wxMBConv& conv) { TestFile tf; const wxString data = "Hello\0UTF"; { wxFile fout(tf.GetName(), wxFile::write); CPPUNIT_ASSERT( fout.IsOpened() ); CPPUNIT_ASSERT( fout.Write(data, conv) ); } { wxFile fin(tf.GetName(), wxFile::read); CPPUNIT_ASSERT( fin.IsOpened() ); const ssize_t len = fin.Length(); wxCharBuffer buf(len); CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) ); wxWCharBuffer wbuf(conv.cMB2WC(buf)); #if wxUSE_UNICODE CPPUNIT_ASSERT_EQUAL( data, wbuf ); #else // !wxUSE_UNICODE CPPUNIT_ASSERT ( memcmp(wbuf, L"Hello\0UTF", data.length()*sizeof(wchar_t)) == 0 ); #endif // wxUSE_UNICODE/!wxUSE_UNICODE } }
// test that the conversion between str and wcs (subject to flags) succeeds // // the first argument is the index in the test array and is used solely for // diagnostics void Test(size_t n, wxMBConv& conv) const { if ( str ) { wxWCharBuffer wbuf = conv.cMB2WC(str); if ( wcs ) { CPPUNIT_ASSERT_MESSAGE ( Message(n, "MB2WC failed"), wbuf.data() ); CPPUNIT_ASSERT_MESSAGE ( Message(n, "MB2WC", wbuf, wcs), wxStrcmp(wbuf, wcs) == 0 ); } else // conversion is supposed to fail { CPPUNIT_ASSERT_MESSAGE ( Message(n, "MB2WC succeeded"), !wbuf.data() ); } } if ( wcs && !(flags & ONLY_MB2WC) ) { wxCharBuffer buf = conv.cWC2MB(wcs); if ( str ) { CPPUNIT_ASSERT_MESSAGE ( Message(n, "WC2MB failed"), buf.data() ); CPPUNIT_ASSERT_MESSAGE ( Message(n, "WC2MB", buf, str), strcmp(buf, str) == 0 ); } else { CPPUNIT_ASSERT_MESSAGE ( Message(n, "WC2MB succeeded"), !buf.data() ); } } }
wxString CTextFile::GetNextLine(const wxMBConv& conv) { wxCHECK_MSG(m_file.IsOpened(), wxEmptyString, wxT("Trying to read from closed file.")); wxCHECK_MSG(!m_file.Eof(), wxEmptyString, wxT("Trying to read past EOF")); wxCHECK_MSG((m_mode == read), wxEmptyString, wxT("Trying to read from non-readable file.")); wxString line; char buffer[TXTBUF_SIZE]; // Loop until EOF (fgets will then return NULL) or a newline is read. while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) { // NB: The majority of the time spent by this function is // spent converting the multibyte string to wide-char. line += conv.cMB2WC(buffer); // Remove any newlines, carriage returns, etc. if (line.Length() && (line.Last() == wxT('\n'))) { if ((line.Length() > 1)) { if (line[line.Length() - 2] == wxT('\r')) { // Carriage return + newline line.RemoveLast(2); } else { // Only a newline. line.RemoveLast(1); } } else { // Empty line line.Clear(); } // We've read an entire line. break; } } return line; }