diff options
author | Caolán McNamara <caolanm@redhat.com> | 2012-01-24 16:44:15 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2012-01-24 16:53:56 +0000 |
commit | 3ee84cb27e49d26acc50e235abafd7aa5e6a8c72 (patch) | |
tree | b56bd4e9e21313746629461752196f624b012c46 /tools | |
parent | 17ecf0036d44657b954de6c8f7efd536ab5c4809 (diff) |
use write_uInt16s_FromOUString pattern
Diffstat (limited to 'tools')
-rw-r--r-- | tools/inc/tools/stream.hxx | 71 | ||||
-rw-r--r-- | tools/source/stream/stream.cxx | 47 |
2 files changed, 88 insertions, 30 deletions
diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx index 0e1c6f77e21c..9fcb72904732 100644 --- a/tools/inc/tools/stream.hxx +++ b/tools/inc/tools/stream.hxx @@ -515,6 +515,50 @@ rtl::OUString read_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm) return read_uInt16s_ToOUString(rStrm, nUnits); } +//Attempt to write a prefixed sequence of nUnits 16bit units from an OUString, +//returned value is number of bytes written +TOOLS_DLLPUBLIC sal_Size write_uInt16s_FromOUString(SvStream& rStrm, + const rtl::OUString& rStr, sal_Size nUnits); + +TOOLS_DLLPUBLIC inline sal_Size write_uInt16s_FromOUString(SvStream& rStrm, + const rtl::OUString& rStr) +{ + return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength()); +} + +namespace streamdetail +{ + //Attempt to write a pascal-style length (of type prefix) prefixed sequence of + //units from a string-type, returned value is number of bytes written (including + //byte-count of prefix) + template<typename prefix, typename S, sal_Size (*writeOper)(SvStream&, const S&, sal_Size)> + sal_Size write_lenPrefixed_seq_From_str(SvStream& rStrm, const S &rStr) + { + SAL_WARN_IF(rStr.getLength() > std::numeric_limits<prefix>::max(), + "tools.stream", + "string too long for prefix count to fit in output type"); + + sal_Size nWritten = 0; + prefix nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<prefix>::max()); + rStrm << nUnits; + if (rStrm.good()) + { + nWritten += sizeof(prefix); + nWritten += writeOper(rStrm, rStr, nUnits); + } + return nWritten; + } +} + +//Attempt to write a pascal-style length (of type prefix) prefixed sequence of +//16bit units from an OUString, returned value is number of bytes written (including +//byte-count of prefix) +template<typename prefix> sal_Size write_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm, + const rtl::OUString &rStr) +{ + return streamdetail::write_lenPrefixed_seq_From_str<prefix, rtl::OUString, write_uInt16s_FromOUString>(rStrm, rStr); +} + //Attempt to read 8bit units to an OString until a zero terminator is //encountered, returned rtl::OString's length is number of units *definitely* //successfully read, check SvStream::good() to see if null terminator was @@ -546,25 +590,26 @@ rtl::OUString read_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm, return rtl::OStringToOUString(read_lenPrefixed_uInt8s_ToOString<prefix>(rStrm), eEnc); } +//Attempt to write a prefixed sequence of nUnits 8bit units from an OString, +//returned value is number of bytes written +TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const rtl::OString& rStr, + sal_Size nUnits) +{ + return rStrm.Write(rStr.getStr(), nUnits); +} + +TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const rtl::OString& rStr) +{ + return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength()); +} + //Attempt to write a pascal-style length (of type prefix) prefixed sequence of //8bit units from an OString, returned value is number of bytes written (including //byte-count of prefix) template<typename prefix> sal_Size write_lenPrefixed_uInt8s_FromOString(SvStream& rStrm, const rtl::OString &rStr) { - SAL_WARN_IF(rStr.getLength() > std::numeric_limits<prefix>::max(), - "tools.stream", - "string too long for prefix count to fit in output type"); - - sal_Size nWritten = 0; - prefix nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<prefix>::max()); - rStrm << nUnits; - if (rStrm.good()) - { - nWritten += sizeof(prefix); - nWritten += rStrm.Write(rStr.getStr(), nUnits); - } - return nWritten; + return streamdetail::write_lenPrefixed_seq_From_str<prefix, rtl::OString, write_uInt8s_FromOString>(rStrm, rStr); } //Attempt to write a pascal-style length (of type prefix) prefixed sequence of diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index 11cfe33e766c..0e899050fced 100644 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -856,21 +856,21 @@ rtl::OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStream, rtl_TextE read_zeroTerminated_uInt8s_ToOString(rStream), eEnc); } -/************************************************************************* -|* -|* Stream::WriteUnicodeText() -|* -*************************************************************************/ - -sal_Bool SvStream::WriteUnicodeText( const String& rStr ) -{ - DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "WriteUnicodeText: swapping sizeof(sal_Unicode) not implemented" ); - if ( bSwap ) +//Attempt to write a prefixed sequence of nUnits 16bit units from an OUString, +//returned value is number of bytes written +sal_Size write_uInt16s_FromOUString(SvStream& rStrm, const rtl::OUString& rStr, + sal_Size nUnits) +{ + DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "write_uInt16s_FromOUString: swapping sizeof(sal_Unicode) not implemented" ); + sal_Size nWritten; + if (!rStrm.IsEndianSwap()) + nWritten = rStrm.Write( (char*)rStr.getStr(), nUnits * sizeof(sal_Unicode) ); + else { - xub_StrLen nLen = rStr.Len(); + sal_Size nLen = nUnits; sal_Unicode aBuf[384]; sal_Unicode* const pTmp = ( nLen > 384 ? new sal_Unicode[nLen] : aBuf); - memcpy( pTmp, rStr.GetBuffer(), nLen * sizeof(sal_Unicode) ); + memcpy( pTmp, rStr.getStr(), nLen * sizeof(sal_Unicode) ); sal_Unicode* p = pTmp; const sal_Unicode* const pStop = pTmp + nLen; while ( p < pStop ) @@ -878,23 +878,36 @@ sal_Bool SvStream::WriteUnicodeText( const String& rStr ) SwapUShort( *p ); p++; } - Write( (char*)pTmp, nLen * sizeof(sal_Unicode) ); + nWritten = rStrm.Write( (char*)pTmp, nLen * sizeof(sal_Unicode) ); if ( pTmp != aBuf ) delete [] pTmp; } - else - Write( (char*)rStr.GetBuffer(), rStr.Len() * sizeof(sal_Unicode) ); + return nWritten; +} + +/************************************************************************* +|* +|* Stream::WriteUnicodeText() +|* +*************************************************************************/ + +sal_Bool SvStream::WriteUnicodeText( const String& rStr ) +{ + write_uInt16s_FromOUString(*this, rStr, rStr.Len()); return nError == SVSTREAM_OK; } sal_Bool SvStream::WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet ) { if ( eDestCharSet == RTL_TEXTENCODING_UNICODE ) - return WriteUnicodeText( rStr ); + { + write_uInt16s_FromOUString(*this, rStr, rStr.Len()); + return nError == SVSTREAM_OK; + } else { rtl::OString aStr(rtl::OUStringToOString(rStr, eDestCharSet)); - Write(aStr.getStr(), aStr.getLength()); + write_uInt8s_FromOString(*this, aStr, aStr.getLength()); return nError == SVSTREAM_OK; } } |