add a way to better construct an OString of len X from a SvStream

This commit is contained in:
Caolán McNamara 2011-09-01 23:55:18 +01:00
parent 088e175776
commit 0ee8ec18c4
8 changed files with 68 additions and 31 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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, \

View file

@ -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, \

View file

@ -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 -
// --------------

View file

@ -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);
}

View file

@ -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: */