]> git.saurik.com Git - wxWidgets.git/commitdiff
Write correct number of bytes in wxFile::Write(wxString).
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 12 Sep 2009 22:40:42 +0000 (22:40 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 12 Sep 2009 22:40:42 +0000 (22:40 +0000)
This function was broken for conversions using more than one byte per
character (e.g. UTF-16 or UTF-32) and also even for UTF-8 for strings
containing NUL bytes as it used strlen() to determine the number of bytes to
write out instead of using the really needed number.

Fix this by using the wxCharBuffer::length() method which always returns the
correct value.

Also add a wxFile unit test verifying that it can correctly read back a string
written using any of UTF-8, UTF-16 or UTF-32.

Closes #11192.

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

12 files changed:
src/common/file.cpp
tests/Makefile.in
tests/file/filetest.cpp [new file with mode: 0644]
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj
tests/test_vc9_test.vcproj

index 1063e452936cb0572a86d8785f7745d5785f27ec..8aaa9ac05e64ec4eddf1c2ab06831ad2bcf2c65f 100644 (file)
@@ -312,7 +312,7 @@ bool wxFile::Write(const wxString& s, const wxMBConv& conv)
   if ( !buf )
       return false;
 
-  const size_t size = strlen(buf); // FIXME: use buf.length() when available
+  const size_t size = buf.length();
   return Write(buf, size) == size;
 }
 
index ac17216617813d52699fa54c75cdd0f48abdc35f..fcc3680ff7b8176b9602da0b809b5b7fe198fbf4 100644 (file)
@@ -65,6 +65,7 @@ TEST_OBJECTS =  \
        test_evthandler.o \
        test_timertest.o \
        test_exec.o \
+       test_filetest.o \
        test_filekind.o \
        test_filenametest.o \
        test_filesystest.o \
@@ -378,6 +379,9 @@ test_timertest.o: $(srcdir)/events/timertest.cpp $(TEST_ODEP)
 test_exec.o: $(srcdir)/exec/exec.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/exec/exec.cpp
 
+test_filetest.o: $(srcdir)/file/filetest.cpp $(TEST_ODEP)
+       $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/file/filetest.cpp
+
 test_filekind.o: $(srcdir)/filekind/filekind.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/filekind/filekind.cpp
 
diff --git a/tests/file/filetest.cpp b/tests/file/filetest.cpp
new file mode 100644 (file)
index 0000000..dc3c286
--- /dev/null
@@ -0,0 +1,95 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/file/filetest.cpp
+// Purpose:     wxFile unit test
+// Author:      Vadim Zeitlin
+// Created:     2009-09-12
+// RCS-ID:      $Id$
+// Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+#if wxUSE_FILE
+
+#include "wx/file.h"
+
+#include "testfile.h"
+
+// ----------------------------------------------------------------------------
+// test class
+// ----------------------------------------------------------------------------
+
+class FileTestCase : public CppUnit::TestCase
+{
+public:
+    FileTestCase() { }
+
+private:
+    CPPUNIT_TEST_SUITE( FileTestCase );
+        CPPUNIT_TEST( RoundTripUTF8 );
+        CPPUNIT_TEST( RoundTripUTF16 );
+        CPPUNIT_TEST( RoundTripUTF32 );
+    CPPUNIT_TEST_SUITE_END();
+
+    void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
+    void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
+    void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
+
+    void DoRoundTripTest(const wxMBConv& conv);
+
+    wxDECLARE_NO_COPY_CLASS(FileTestCase);
+};
+
+// ----------------------------------------------------------------------------
+// CppUnit macros
+// ----------------------------------------------------------------------------
+
+CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
+
+// ----------------------------------------------------------------------------
+// tests implementation
+// ----------------------------------------------------------------------------
+
+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 wxFileOffset 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
+    }
+}
+
+#endif // wxUSE_FILE
index bba737c6c9ea08bef47af5d16c300e4eb8004489..77e128c2baf541c3e0e9fbdf2425d24931413cb4 100644 (file)
@@ -49,6 +49,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_evthandler.obj \
        $(OBJS)\test_timertest.obj \
        $(OBJS)\test_exec.obj \
+       $(OBJS)\test_filetest.obj \
        $(OBJS)\test_filekind.obj \
        $(OBJS)\test_filenametest.obj \
        $(OBJS)\test_filesystest.obj \
@@ -410,6 +411,9 @@ $(OBJS)\test_timertest.obj: .\events\timertest.cpp
 $(OBJS)\test_exec.obj: .\exec\exec.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\exec\exec.cpp
 
+$(OBJS)\test_filetest.obj: .\file\filetest.cpp
+       $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\file\filetest.cpp
+
 $(OBJS)\test_filekind.obj: .\filekind\filekind.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\filekind\filekind.cpp
 
index 9519ea74ae772bd44f7495cef12a638075550005..08f2802a74abb71ccb5189ff3698134990ed19eb 100644 (file)
@@ -41,6 +41,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_evthandler.o \
        $(OBJS)\test_timertest.o \
        $(OBJS)\test_exec.o \
+       $(OBJS)\test_filetest.o \
        $(OBJS)\test_filekind.o \
        $(OBJS)\test_filenametest.o \
        $(OBJS)\test_filesystest.o \
@@ -391,6 +392,9 @@ $(OBJS)\test_timertest.o: ./events/timertest.cpp
 $(OBJS)\test_exec.o: ./exec/exec.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
+$(OBJS)\test_filetest.o: ./file/filetest.cpp
+       $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+
 $(OBJS)\test_filekind.o: ./filekind/filekind.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
index 151f5f18e83ef3112bfdc08f0c5ee7462cf5a77e..92c2b269a25b5db0822d17910aee54f474d12f93 100644 (file)
@@ -42,6 +42,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_evthandler.obj \
        $(OBJS)\test_timertest.obj \
        $(OBJS)\test_exec.obj \
+       $(OBJS)\test_filetest.obj \
        $(OBJS)\test_filekind.obj \
        $(OBJS)\test_filenametest.obj \
        $(OBJS)\test_filesystest.obj \
@@ -493,6 +494,9 @@ $(OBJS)\test_timertest.obj: .\events\timertest.cpp
 $(OBJS)\test_exec.obj: .\exec\exec.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\exec\exec.cpp
 
+$(OBJS)\test_filetest.obj: .\file\filetest.cpp
+       $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\file\filetest.cpp
+
 $(OBJS)\test_filekind.obj: .\filekind\filekind.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\filekind\filekind.cpp
 
index c5bddaca9aab3c3135fcc6ffdac1a8f66b6ef812..bdaba4b6d0a4b12330b0cfc6fe8d815a833c56b0 100644 (file)
@@ -270,6 +270,7 @@ TEST_OBJECTS =  &
        $(OBJS)\test_evthandler.obj &
        $(OBJS)\test_timertest.obj &
        $(OBJS)\test_exec.obj &
+       $(OBJS)\test_filetest.obj &
        $(OBJS)\test_filekind.obj &
        $(OBJS)\test_filenametest.obj &
        $(OBJS)\test_filesystest.obj &
@@ -448,6 +449,9 @@ $(OBJS)\test_timertest.obj :  .AUTODEPEND .\events\timertest.cpp
 $(OBJS)\test_exec.obj :  .AUTODEPEND .\exec\exec.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
+$(OBJS)\test_filetest.obj :  .AUTODEPEND .\file\filetest.cpp
+       $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+
 $(OBJS)\test_filekind.obj :  .AUTODEPEND .\filekind\filekind.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
index 37a7cdb7f499350b611e0a0c9abe0f7bfed8ffce..80c71cce9b1add494e6f7e3019dfe0829ab5f04a 100644 (file)
@@ -40,6 +40,7 @@
             events/evthandler.cpp
             events/timertest.cpp
             exec/exec.cpp
+            file/filetest.cpp
             filekind/filekind.cpp
             filename/filenametest.cpp
             filesys/filesystest.cpp
index b131c952744d9364b502f32a4fdf148eda0456a3..031f5258896b29ba9a02c5c421aef36f3d3d5b13 100644 (file)
@@ -321,6 +321,10 @@ SOURCE=.\filesys\filesystest.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\file\filetest.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\fontmap\fontmaptest.cpp\r
 # End Source File\r
 # Begin Source File\r
index d3d49db7577d317ff6749ddedb7aa396b9ba9f7d..cc848ea3bf14d4941761ca068a461e6b3e39361c 100644 (file)
                        <File\r
                                RelativePath=".\filesys\filesystest.cpp">\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\file\filetest.cpp">\r
+                       </File>\r
                        <File\r
                                RelativePath=".\fontmap\fontmaptest.cpp">\r
                        </File>\r
index a554a86dc399a004f8aef657badf0b78c1f781c8..e8fccf8fbf611bd6f8ccfa6e0651f10c9b19291d 100644 (file)
                                RelativePath=".\filesys\filesystest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\file\filetest.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\fontmap\fontmaptest.cpp"\r
                                >\r
index 9753505ef08bfaf1055247f5163b904c1da69b9d..55021dba017ea4a8e145353cd85c26ebfd11e99c 100644 (file)
                                RelativePath=".\filesys\filesystest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\file\filetest.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\fontmap\fontmaptest.cpp"\r
                                >\r