diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-12-14 22:52:07 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-12-15 12:22:49 +0000 |
commit | 66c3655a07e109d88183002192410bcc9866c0f2 (patch) | |
tree | 019188e9165edc40641dd5a6836e94a64529c5d7 | |
parent | drop unused ReadCString with hidden StreamCharSet monstrosity (diff) | |
download | core-66c3655a07e109d88183002192410bcc9866c0f2.tar.gz core-66c3655a07e109d88183002192410bcc9866c0f2.zip |
convert ReadCString from ByteString to OString
Nobody ever checked the return value anyway, so just return the string
and use the stream state bits if necessary to find failures.
Doesn't need to be a member, make a standalone function
Rename it to read_zeroTerminated_uInt8s_AsO[U]String, stupid perhaps,
but *shrug*, unambiguous.
Drop misleading overloaded String variants use:
read_zeroTerminated_uInt8s_AsOString or
read_zeroTerminated_uInt8s_AsOUString
added a unit test, valgrinded it, found and fixed invalid read
in original implementation.
-rw-r--r-- | sc/source/filter/ftools/ftools.cxx | 4 | ||||
-rw-r--r-- | svtools/source/urlobj/inetimg.cxx | 9 | ||||
-rwxr-xr-x | sw/source/ui/dochdl/swdtflvr.cxx | 6 | ||||
-rw-r--r-- | tools/inc/tools/stream.hxx | 13 | ||||
-rw-r--r-- | tools/qa/cppunit/test_stream.cxx | 21 | ||||
-rw-r--r-- | tools/source/stream/stream.cxx | 43 |
6 files changed, 52 insertions, 44 deletions
diff --git a/sc/source/filter/ftools/ftools.cxx b/sc/source/filter/ftools/ftools.cxx index 52f848e457c4..b346295455e0 100644 --- a/sc/source/filter/ftools/ftools.cxx +++ b/sc/source/filter/ftools/ftools.cxx @@ -311,9 +311,7 @@ ByteString ScfTools::ReadCString( SvStream& rStrm, sal_Int32& rnBytesLeft ) void ScfTools::AppendCString( SvStream& rStrm, String& rString, rtl_TextEncoding eTextEnc ) { - ByteString aByteString; - rStrm.ReadCString(aByteString); - rString += String( aByteString, eTextEnc ); + rString += read_zeroTerminated_uInt8s_AsOUString(rStrm, eTextEnc); } // *** HTML table names <-> named range names *** ----------------------------- diff --git a/svtools/source/urlobj/inetimg.cxx b/svtools/source/urlobj/inetimg.cxx index f203fbdee4c6..fff9d170399a 100644 --- a/svtools/source/urlobj/inetimg.cxx +++ b/svtools/source/urlobj/inetimg.cxx @@ -73,8 +73,7 @@ sal_Bool INetImage::Read( SvStream& rIStm, sal_uLong nFormat ) { case SOT_FORMATSTR_ID_INET_IMAGE: { - String sINetImg; - rIStm.ReadCString( sINetImg, RTL_TEXTENCODING_UTF8 ); + String sINetImg = read_zeroTerminated_uInt8s_AsOUString(rIStm, RTL_TEXTENCODING_UTF8); xub_StrLen nStart = 0; aImageURL = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart ); aTargetURL = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart ); @@ -120,11 +119,11 @@ sal_Bool INetImage::Read( SvStream& rIStm, sal_uLong nFormat ) // skip over iExtraHTML_Offset rIStm.SeekRel( sizeof( int ) ); - rIStm.ReadCString( aImageURL, eSysCSet ); + aImageURL = read_zeroTerminated_uInt8s_AsOUString(rIStm, eSysCSet); if( nAltOffset ) { rIStm.Seek( nFilePos + nAltOffset ); - rIStm.ReadCString( aAlternateText, eSysCSet ); + aAlternateText = read_zeroTerminated_uInt8s_AsOUString(rIStm, eSysCSet); } else if( aAlternateText.Len() ) aAlternateText.Erase(); @@ -132,7 +131,7 @@ sal_Bool INetImage::Read( SvStream& rIStm, sal_uLong nFormat ) if( nAnchorOffset ) { rIStm.Seek( nFilePos + nAnchorOffset ); - rIStm.ReadCString( aTargetURL, eSysCSet ); + aTargetURL = read_zeroTerminated_uInt8s_AsOUString(rIStm, eSysCSet); } else if( aTargetURL.Len() ) aTargetURL.Erase(); diff --git a/sw/source/ui/dochdl/swdtflvr.cxx b/sw/source/ui/dochdl/swdtflvr.cxx index e5fdd55a98fc..f9bb431670f0 100755 --- a/sw/source/ui/dochdl/swdtflvr.cxx +++ b/sw/source/ui/dochdl/swdtflvr.cxx @@ -1993,9 +1993,9 @@ int SwTransferable::_PasteDDE( TransferableDataHelper& rData, } // report useful error!! rtl_TextEncoding eEncoding = DDE_TXT_ENCODING; - xStrm->ReadCString( aApp, eEncoding ); - xStrm->ReadCString( aTopic, eEncoding ); - xStrm->ReadCString( aItem, eEncoding ); + aApp = read_zeroTerminated_uInt8s_AsOUString(*xStrm, eEncoding); + aTopic = read_zeroTerminated_uInt8s_AsOUString(*xStrm, eEncoding); + aItem = read_zeroTerminated_uInt8s_AsOUString(*xStrm, eEncoding); } String aCmd; diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx index bf884a13ddbd..5dca30fdb264 100644 --- a/tools/inc/tools/stream.hxx +++ b/tools/inc/tools/stream.hxx @@ -385,12 +385,6 @@ public: // next Tell() <= nSize sal_Bool SetStreamSize( sal_Size nSize ); - /// Read in the stream to a zero character and put all - /// read chracters in the Bytestring. The String interface - /// convert the BytString with the given encoding to a String - sal_Bool ReadCString( ByteString& rStr ); - sal_Bool ReadCString( String& rStr, rtl_TextEncoding eToEncode ); - sal_Bool ReadLine( ByteString& rStr ); sal_Bool ReadLine( rtl::OString& rStr ); sal_Bool WriteLine( const ByteString& rStr ); @@ -559,6 +553,13 @@ TOOLS_DLLPUBLIC rtl::OString read_uInt8s_AsOString(SvStream& rStr, sal_Size nLen //rtl::OUString's length is number of units successfully read TOOLS_DLLPUBLIC rtl::OUString read_LEuInt16s_AsOUString(SvStream& rStr, sal_Size nLen); +//Attempt to read 8bit units to an OString until a zero terminator is encountered +TOOLS_DLLPUBLIC rtl::OString read_zeroTerminated_uInt8s_AsOString(SvStream& rStr); + +//Attempt to read 8bit units assuming source encoding eEnc to an OUString until +//a zero terminator is encountered +TOOLS_DLLPUBLIC rtl::OUString read_zeroTerminated_uInt8s_AsOUString(SvStream& rStr, rtl_TextEncoding eEnc); + // -------------- // - FileStream - // -------------- diff --git a/tools/qa/cppunit/test_stream.cxx b/tools/qa/cppunit/test_stream.cxx index dc80c7d5c02a..77c2b9ed75ae 100644 --- a/tools/qa/cppunit/test_stream.cxx +++ b/tools/qa/cppunit/test_stream.cxx @@ -46,10 +46,12 @@ namespace public: void test_stdstream(); void test_fastostring(); + void test_read_cstring(); CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(test_stdstream); CPPUNIT_TEST(test_fastostring); + CPPUNIT_TEST(test_read_cstring); CPPUNIT_TEST_SUITE_END(); }; @@ -146,6 +148,25 @@ namespace CPPUNIT_ASSERT(aFour.equalsL(RTL_CONSTASCII_STRINGPARAM(foo))); } + void Test::test_read_cstring() + { + char foo[] = "foobar"; + SvMemoryStream aMemStream(RTL_CONSTASCII_STRINGPARAM(foo), STREAM_READ); + + rtl::OString aOne = read_zeroTerminated_uInt8s_AsOString(aMemStream); + CPPUNIT_ASSERT(aOne.equalsL(RTL_CONSTASCII_STRINGPARAM("foobar"))); + CPPUNIT_ASSERT(!aMemStream.good()); + CPPUNIT_ASSERT(!aMemStream.bad()); + CPPUNIT_ASSERT(aMemStream.eof()); + + aMemStream.Seek(0); + foo[3] = 0; + rtl::OString aTwo = read_zeroTerminated_uInt8s_AsOString(aMemStream); + CPPUNIT_ASSERT(aTwo.equalsL(RTL_CONSTASCII_STRINGPARAM("foo"))); + CPPUNIT_ASSERT(aMemStream.good()); + } + + CPPUNIT_TEST_SUITE_REGISTRATION(Test); } diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index 33eef858ab27..19009f9ec07a 100644 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -806,30 +806,23 @@ sal_Bool SvStream::ReadUniOrByteStringLine( String& rStr, rtl_TextEncoding eSrcC return ReadByteStringLine( rStr, eSrcCharSet ); } -/************************************************************************* -|* -|* Stream::ReadCString -|* -*************************************************************************/ - -sal_Bool SvStream::ReadCString( ByteString& rStr ) +rtl::OString read_zeroTerminated_uInt8s_AsOString(SvStream& rStream) { - if( rStr.Len() ) - rStr.Erase(); + rtl::OStringBuffer aOutput; sal_Char buf[ 256 + 1 ]; sal_Bool bEnd = sal_False; - sal_Size nFilePos = Tell(); + sal_Size nFilePos = rStream.Tell(); - while( !bEnd && !GetError() ) + while( !bEnd && !rStream.GetError() ) { - sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 ); - sal_uInt16 nReallyRead = nLen; - if( !nLen ) + sal_Size nLen = rStream.Read(buf, sizeof(buf)-1); + if (!nLen) break; + sal_Size nReallyRead = nLen; const sal_Char* pPtr = buf; - while( *pPtr && nLen ) + while (nLen && *pPtr) ++pPtr, --nLen; bEnd = ( nReallyRead < sizeof(buf)-1 ) // read less than attempted to read @@ -837,25 +830,21 @@ sal_Bool SvStream::ReadCString( ByteString& rStr ) && ( 0 == *pPtr ) // AND found a string terminator ); - rStr.Append( buf, ::sal::static_int_cast< xub_StrLen >( pPtr - buf ) ); + aOutput.append(buf, pPtr - buf); } - nFilePos += rStr.Len(); - if( Tell() > nFilePos ) - nFilePos++; - Seek( nFilePos ); // seeken wg. obigem BlockRead! - return bEnd; + nFilePos += aOutput.getLength(); + if (rStream.Tell() > nFilePos) + rStream.Seek(nFilePos+1); // seeken wg. obigem BlockRead! + return aOutput.makeStringAndClear(); } -sal_Bool SvStream::ReadCString( String& rStr, rtl_TextEncoding eToEncode ) +rtl::OUString read_zeroTerminated_uInt8s_AsOUString(SvStream& rStream, rtl_TextEncoding eEnc) { - ByteString sStr; - sal_Bool bRet = ReadCString( sStr ); - rStr = String( sStr, eToEncode ); - return bRet; + return rtl::OStringToOUString( + read_zeroTerminated_uInt8s_AsOString(rStream), eEnc); } - /************************************************************************* |* |* Stream::WriteUnicodeText() |