summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2011-09-01 23:55:18 +0100
committerCaolán McNamara <caolanm@redhat.com>2011-09-02 09:54:18 +0100
commit0ee8ec18c4218da4f82c4ff2bf601edca42100be (patch)
tree7bde351118562e0060dcb50e51ed4f9ada3e1c95
parent088e175776fe0fa37c859b68278f5d4304d7ddd6 (diff)
add a way to better construct an OString of len X from a SvStream
-rw-r--r--sw/source/filter/ww8/ww8par2.cxx6
-rw-r--r--sw/source/filter/ww8/ww8scan.cxx33
-rw-r--r--sw/source/filter/ww8/ww8scan.hxx2
-rw-r--r--tools/CppunitTest_tools_test.mk2
-rw-r--r--tools/Executable_tools_cppunittester_all.mk2
-rw-r--r--tools/inc/tools/stream.hxx4
-rw-r--r--tools/qa/cppunit/test_stream.cxx (renamed from tools/qa/cppunit/test_streamstate.cxx)22
-rw-r--r--tools/source/stream/stream.cxx28
8 files changed, 68 insertions, 31 deletions
diff --git a/sw/source/filter/ww8/ww8par2.cxx b/sw/source/filter/ww8/ww8par2.cxx
index e5948d962a20..613a6e1ffe60 100644
--- a/sw/source/filter/ww8/ww8par2.cxx
+++ b/sw/source/filter/ww8/ww8par2.cxx
@@ -4337,9 +4337,9 @@ void WW8RStyle::ImportOldFormatStyles()
}
else // user style
{
- ByteString aTmp;
- nByteCount = static_cast< sal_uInt16 >(nByteCount + SafeReadString(aTmp, nCount, rSt));
- sName = String(aTmp, eStructChrSet);
+ rtl::OString aTmp = readBytesAsOString(rSt, nCount);
+ nByteCount += aTmp.getLength();
+ sName = rtl::OStringToOUString(aTmp, eStructChrSet);
}
rSI.SetOrgWWIdent(sName, stc);
rSI.bImported = true;
diff --git a/sw/source/filter/ww8/ww8scan.cxx b/sw/source/filter/ww8/ww8scan.cxx
index 12aab49f1578..0419c44852d2 100644
--- a/sw/source/filter/ww8/ww8scan.cxx
+++ b/sw/source/filter/ww8/ww8scan.cxx
@@ -1972,18 +1972,6 @@ String WW8Read_xstz(SvStream& rStrm, sal_uInt16 nChars, bool bAtEndSeekRel1)
return aStr;
}
-sal_uLong SafeReadString(ByteString &rStr,sal_uInt16 nLen,SvStream &rStrm)
-{
- sal_uLong nWasRead=0;
- if (nLen)
- {
- nWasRead = rStrm.Read( rStr.AllocBuffer( nLen ), nLen);
- if( nWasRead != nLen )
- rStr.ReleaseBufferAccess(static_cast<xub_StrLen>(nWasRead));
- }
- return nWasRead;
-}
-
xub_StrLen WW8ScannerBase::WW8ReadString( SvStream& rStrm, String& rStr,
WW8_CP nAktStartCp, long nTotalLen, rtl_TextEncoding eEnc ) const
{
@@ -2018,9 +2006,8 @@ xub_StrLen WW8ScannerBase::WW8ReadString( SvStream& rStrm, String& rStr,
else
{
// Alloc method automatically sets Zero at the end
- ByteString aByteStr;
- SafeReadString(aByteStr,(sal_uInt16)nLen,rStrm);
- rStr += String( aByteStr, eEnc );
+ rtl::OString aByteStr = readBytesAsOString(rStrm, nLen);
+ rStr.Append(String(rtl::OStringToOUString(aByteStr, eEnc)));
}
nTotalRead += nLen;
nAktStartCp += nLen;
@@ -3923,9 +3910,8 @@ void WW8ReadSTTBF(bool bVer8, SvStream& rStrm, sal_uInt32 nStart, sal_Int32 nLen
{
sal_uInt8 nBChar(0);
rStrm >> nBChar;
- ByteString aTmp;
- SafeReadString(aTmp,nBChar,rStrm);
- rArray.push_back(String(aTmp, eCS));
+ rtl::OString aTmp = readBytesAsOString(rStrm, nBChar);
+ rArray.push_back(rtl::OStringToOUString(aTmp, eCS));
}
// Skip the extra data
@@ -3957,9 +3943,8 @@ void WW8ReadSTTBF(bool bVer8, SvStream& rStrm, sal_uInt32 nStart, sal_Int32 nLen
{
sal_uInt8 nBChar(0);
rStrm >> nBChar;
- ByteString aTmp;
- SafeReadString(aTmp,nBChar,rStrm);
- pValueArray->push_back(String(aTmp, eCS));
+ rtl::OString aTmp = readBytesAsOString(rStrm, nBChar);
+ pValueArray->push_back(rtl::OStringToOUString(aTmp, eCS));
}
}
}
@@ -3984,9 +3969,9 @@ void WW8ReadSTTBF(bool bVer8, SvStream& rStrm, sal_uInt32 nStart, sal_Int32 nLen
++nRead;
if (nBChar)
{
- ByteString aTmp;
- nRead += SafeReadString(aTmp,nBChar,rStrm);
- rArray.push_back(String(aTmp, eCS));
+ rtl::OString aTmp = readBytesAsOString(rStrm, nBChar);
+ nRead += aTmp.getLength();
+ rArray.push_back(rtl::OStringToOUString(aTmp, eCS));
}
else
rArray.push_back(aEmptyStr);
diff --git a/sw/source/filter/ww8/ww8scan.hxx b/sw/source/filter/ww8/ww8scan.hxx
index 0a67f0592a47..be90ffff2006 100644
--- a/sw/source/filter/ww8/ww8scan.hxx
+++ b/sw/source/filter/ww8/ww8scan.hxx
@@ -1779,8 +1779,6 @@ void SwapQuotesInField(String &rFmt);
Word2CHPX ReadWord2Chpx(SvStream &rSt, sal_Size nOffset, sal_uInt8 nSize);
std::vector<sal_uInt8> ChpxToSprms(const Word2CHPX &rChpx);
-sal_uLong SafeReadString(ByteString &rStr,sal_uInt16 nLen,SvStream &rStrm);
-
bool checkSeek(SvStream &rSt, sal_uInt32 nOffset)
SAL_WARN_UNUSED_RESULT;
diff --git a/tools/CppunitTest_tools_test.mk b/tools/CppunitTest_tools_test.mk
index 5808e3a5f18e..c39f0a2461f3 100644
--- a/tools/CppunitTest_tools_test.mk
+++ b/tools/CppunitTest_tools_test.mk
@@ -33,7 +33,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,tools_test))
$(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \
tools/qa/cppunit/test_reversemap \
tools/qa/cppunit/test_pathutils \
- tools/qa/cppunit/test_streamstate \
+ tools/qa/cppunit/test_stream \
))
$(eval $(call gb_CppunitTest_add_api,tools_test, \
diff --git a/tools/Executable_tools_cppunittester_all.mk b/tools/Executable_tools_cppunittester_all.mk
index a5325f9d741b..d2fd13b3f0ba 100644
--- a/tools/Executable_tools_cppunittester_all.mk
+++ b/tools/Executable_tools_cppunittester_all.mk
@@ -59,7 +59,7 @@ $(eval $(call gb_Executable_add_exception_objects,tools_cppunittester_all, \
tools/qa/cppunit/tools_cppunittester_all \
tools/qa/cppunit/test_reversemap \
tools/qa/cppunit/test_pathutils \
- tools/qa/cppunit/test_streamstate \
+ tools/qa/cppunit/test_stream \
))
$(eval $(call gb_Executable_add_api,tools_cppunittester_all, \
diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx
index 5323e3cca40d..baaf758125c4 100644
--- a/tools/inc/tools/stream.hxx
+++ b/tools/inc/tools/stream.hxx
@@ -564,6 +564,10 @@ TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
/// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
+//Attempt to read nSize bytes to an OString, returned rtl::OString's length is
+//number of bytes successfully read
+TOOLS_DLLPUBLIC rtl::OString readBytesAsOString(SvStream& rStr, sal_Size nSize);
+
// --------------
// - FileStream -
// --------------
diff --git a/tools/qa/cppunit/test_streamstate.cxx b/tools/qa/cppunit/test_stream.cxx
index 182ad6c45561..ae098e5b836d 100644
--- a/tools/qa/cppunit/test_streamstate.cxx
+++ b/tools/qa/cppunit/test_stream.cxx
@@ -41,9 +41,11 @@ namespace
{
public:
void test_stdstream();
+ void test_fastostring();
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(test_stdstream);
+ CPPUNIT_TEST(test_fastostring);
CPPUNIT_TEST_SUITE_END();
};
@@ -120,6 +122,26 @@ namespace
//failbit is rather subtle wrt e.g seeks
}
+ void Test::test_fastostring()
+ {
+ char foo[] = "foobar";
+ SvMemoryStream aMemStream(RTL_CONSTASCII_STRINGPARAM(foo), STREAM_READ);
+
+ rtl::OString aOne = readBytesAsOString(aMemStream, 3);
+ CPPUNIT_ASSERT(aOne.equalsL(RTL_CONSTASCII_STRINGPARAM("foo")));
+
+ rtl::OString aTwo = readBytesAsOString(aMemStream, 3);
+ CPPUNIT_ASSERT(aTwo.equalsL(RTL_CONSTASCII_STRINGPARAM("bar")));
+
+ rtl::OString aThree = readBytesAsOString(aMemStream, 3);
+ CPPUNIT_ASSERT(!aThree.getLength());
+
+ aMemStream.Seek(0);
+
+ rtl::OString aFour = readBytesAsOString(aMemStream, 100);
+ CPPUNIT_ASSERT(aFour.equalsL(RTL_CONSTASCII_STRINGPARAM(foo)));
+ }
+
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index a1f55392749a..312cd5f35b22 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -43,6 +43,7 @@
#include <tools/solar.h>
+#include <comphelper/string.hxx>
#define SWAPNIBBLES(c) \
unsigned char nSwapTmp=c; \
@@ -2473,4 +2474,31 @@ void SvDataCopyStream::Assign( const SvDataCopyStream& )
{
}
+//Create a OString of nSize bytes from rStream
+rtl::OString readBytesAsOString(SvStream& rStrm, sal_Size nSize)
+{
+ using comphelper::string::rtl_string_alloc;
+
+ rtl_String *pStr = NULL;
+ if (nSize)
+ {
+ nSize = std::min(nSize, static_cast<sal_Size>(SAL_MAX_INT32));
+ //alloc a (ref-count 1) rtl_String of the desired length.
+ //rtl_String's buffer is uninitialized, except for null termination
+ pStr = rtl_string_alloc(sal::static_int_cast<sal_Int32>(nSize));
+ sal_Size nWasRead = rStrm.Read(pStr->buffer, nSize);
+ if (nWasRead != nSize)
+ {
+ //on (typically unlikely) short read set length to what we could
+ //read, and null terminate. Excess buffer capacity remains of
+ //course, could create a (true) replacement OString if it matters.
+ pStr->length = sal::static_int_cast<sal_Int32>(nWasRead);
+ pStr->buffer[pStr->length] = 0;
+ }
+ }
+
+ //take ownership of buffer and return, otherwise return empty string
+ return pStr ? rtl::OString(pStr, SAL_NO_ACQUIRE) : rtl::OString();
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */