summaryrefslogtreecommitdiff
path: root/tools
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 /tools
parent088e175776fe0fa37c859b68278f5d4304d7ddd6 (diff)
add a way to better construct an OString of len X from a SvStream
Diffstat (limited to 'tools')
-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
5 files changed, 56 insertions, 2 deletions
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: */