summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2011-09-04 21:17:16 +0100
committerCaolán McNamara <caolanm@redhat.com>2011-09-05 10:54:19 +0100
commite99a072c0e19d1bab68a49758acaac22dd981e0c (patch)
tree927772b072c8cf79026a2146478f9e1c267cac5a /tools
parentaf60795f76f04e5a0315eeaa4cb9c5a90f4ae90c (diff)
add a fairly efficient read_LEuInt16s_AsOUString, merge similar things
Diffstat (limited to 'tools')
-rw-r--r--tools/inc/tools/stream.hxx10
-rw-r--r--tools/qa/cppunit/test_stream.cxx8
-rw-r--r--tools/source/stream/stream.cxx45
3 files changed, 49 insertions, 14 deletions
diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx
index baaf758125c4..28e96aea0e03 100644
--- a/tools/inc/tools/stream.hxx
+++ b/tools/inc/tools/stream.hxx
@@ -564,9 +564,13 @@ 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);
+//Attempt to read nLen 8bit units to an OString, returned rtl::OString's length
+//is number of units successfully read
+TOOLS_DLLPUBLIC rtl::OString read_uInt8s_AsOString(SvStream& rStr, sal_Size nLen);
+
+//Attempt to read nLen little endian 16bit units to an OUString, returned
+//rtl::OUString's length is number of units successfully read
+TOOLS_DLLPUBLIC rtl::OUString read_LEuInt16s_AsOUString(SvStream& rStr, sal_Size nLen);
// --------------
// - FileStream -
diff --git a/tools/qa/cppunit/test_stream.cxx b/tools/qa/cppunit/test_stream.cxx
index ae098e5b836d..515952d93563 100644
--- a/tools/qa/cppunit/test_stream.cxx
+++ b/tools/qa/cppunit/test_stream.cxx
@@ -127,18 +127,18 @@ namespace
char foo[] = "foobar";
SvMemoryStream aMemStream(RTL_CONSTASCII_STRINGPARAM(foo), STREAM_READ);
- rtl::OString aOne = readBytesAsOString(aMemStream, 3);
+ rtl::OString aOne = read_uInt8s_AsOString(aMemStream, 3);
CPPUNIT_ASSERT(aOne.equalsL(RTL_CONSTASCII_STRINGPARAM("foo")));
- rtl::OString aTwo = readBytesAsOString(aMemStream, 3);
+ rtl::OString aTwo = read_uInt8s_AsOString(aMemStream, 3);
CPPUNIT_ASSERT(aTwo.equalsL(RTL_CONSTASCII_STRINGPARAM("bar")));
- rtl::OString aThree = readBytesAsOString(aMemStream, 3);
+ rtl::OString aThree = read_uInt8s_AsOString(aMemStream, 3);
CPPUNIT_ASSERT(!aThree.getLength());
aMemStream.Seek(0);
- rtl::OString aFour = readBytesAsOString(aMemStream, 100);
+ rtl::OString aFour = read_uInt8s_AsOString(aMemStream, 100);
CPPUNIT_ASSERT(aFour.equalsL(RTL_CONSTASCII_STRINGPARAM(foo)));
}
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index 312cd5f35b22..f5d8a9f77a9b 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -2474,20 +2474,20 @@ void SvDataCopyStream::Assign( const SvDataCopyStream& )
{
}
-//Create a OString of nSize bytes from rStream
-rtl::OString readBytesAsOString(SvStream& rStrm, sal_Size nSize)
+//Create a OString of nLen bytes from rStream
+rtl::OString read_uInt8s_AsOString(SvStream& rStrm, sal_Size nLen)
{
using comphelper::string::rtl_string_alloc;
rtl_String *pStr = NULL;
- if (nSize)
+ if (nLen)
{
- nSize = std::min(nSize, static_cast<sal_Size>(SAL_MAX_INT32));
+ nLen = std::min(nLen, 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)
+ pStr = rtl_string_alloc(sal::static_int_cast<sal_Int32>(nLen));
+ sal_Size nWasRead = rStrm.Read(pStr->buffer, nLen);
+ if (nWasRead != nLen)
{
//on (typically unlikely) short read set length to what we could
//read, and null terminate. Excess buffer capacity remains of
@@ -2501,4 +2501,35 @@ rtl::OString readBytesAsOString(SvStream& rStrm, sal_Size nSize)
return pStr ? rtl::OString(pStr, SAL_NO_ACQUIRE) : rtl::OString();
}
+//Create a OUString of nLen little endian sal_Unicodes from rStream
+rtl::OUString read_LEuInt16s_AsOUString(SvStream& rStrm, sal_Size nLen)
+{
+ using comphelper::string::rtl_uString_alloc;
+
+ rtl_uString *pStr = NULL;
+ if (nLen)
+ {
+ nLen = std::min(nLen, static_cast<sal_Size>(SAL_MAX_INT32));
+ //alloc a (ref-count 1) rtl_uString of the desired length.
+ //rtl_String's buffer is uninitialized, except for null termination
+ pStr = rtl_uString_alloc(sal::static_int_cast<sal_Int32>(nLen));
+ sal_Size nWasRead = rStrm.Read(pStr->buffer, nLen*2)/2;
+ if (nWasRead != nLen)
+ {
+ //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 OUString if it matters.
+ pStr->length = sal::static_int_cast<sal_Int32>(nWasRead);
+ pStr->buffer[pStr->length] = 0;
+ }
+#ifdef OSL_BIGENDIAN
+ for (sal_Int32 i = 0; i < pStr->length; ++i)
+ pStr->buffer[i] = SWAPSHORT(pStr->buffer[i]);
+#endif
+ }
+
+ //take ownership of buffer and return, otherwise return empty string
+ return pStr ? rtl::OUString(pStr, SAL_NO_ACQUIRE) : rtl::OUString();
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */