From 3ee84cb27e49d26acc50e235abafd7aa5e6a8c72 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Tue, 24 Jan 2012 16:44:15 +0000 Subject: use write_uInt16s_FromOUString pattern --- sc/source/filter/dif/difexp.cxx | 2 +- sc/source/ui/docshell/docsh.cxx | 2 +- tools/inc/tools/stream.hxx | 71 +++++++++++++++++++++++++++++++++-------- tools/source/stream/stream.cxx | 47 +++++++++++++++++---------- vcl/source/gdi/metaact.cxx | 32 +++---------------- 5 files changed, 94 insertions(+), 60 deletions(-) diff --git a/sc/source/filter/dif/difexp.cxx b/sc/source/filter/dif/difexp.cxx index 38c168a5b8a6..b3da5a3e6295 100644 --- a/sc/source/filter/dif/difexp.cxx +++ b/sc/source/filter/dif/difexp.cxx @@ -255,7 +255,7 @@ FltError ScFormatFilterPluginImpl::ScExportDif( SvStream& rOut, ScDocument* pDoc nPos = aTmpStr.Search( cStrDelim, nPos+2 ); } rOut.WriteUniOrByteChar( cStrDelim, eCharSet ); - rOut.WriteUnicodeText(aTmpStr); + write_uInt16s_FromOUString(rOut, aTmpStr); rOut.WriteUniOrByteChar( cStrDelim, eCharSet ); } else if ( bContextOrNotAsciiEncoding ) diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx index 1041126e7026..74548824f1db 100644 --- a/sc/source/ui/docshell/docsh.cxx +++ b/sc/source/ui/docshell/docsh.cxx @@ -1947,7 +1947,7 @@ void ScDocShell::AsciiSave( SvStream& rStream, const ScImportOptions& rAsciiOpt } if ( bNeedQuotes ) rStream.WriteUniOrByteChar( cStrDelim, eCharSet ); - rStream.WriteUnicodeText( aUniString ); + write_uInt16s_FromOUString(rStream, aUniString); if ( bNeedQuotes ) rStream.WriteUniOrByteChar( cStrDelim, eCharSet ); } 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 + sal_Size write_lenPrefixed_seq_From_str(SvStream& rStrm, const S &rStr) + { + SAL_WARN_IF(rStr.getLength() > std::numeric_limits::max(), + "tools.stream", + "string too long for prefix count to fit in output type"); + + sal_Size nWritten = 0; + prefix nUnits = std::min(rStr.getLength(), std::numeric_limits::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 sal_Size write_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm, + const rtl::OUString &rStr) +{ + return streamdetail::write_lenPrefixed_seq_From_str(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(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 sal_Size write_lenPrefixed_uInt8s_FromOString(SvStream& rStrm, const rtl::OString &rStr) { - SAL_WARN_IF(rStr.getLength() > std::numeric_limits::max(), - "tools.stream", - "string too long for prefix count to fit in output type"); - - sal_Size nWritten = 0; - prefix nUnits = std::min(rStr.getLength(), std::numeric_limits::max()); - rStrm << nUnits; - if (rStrm.good()) - { - nWritten += sizeof(prefix); - nWritten += rStrm.Write(rStr.getStr(), nUnits); - } - return nWritten; + return streamdetail::write_lenPrefixed_seq_From_str(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; } } diff --git a/vcl/source/gdi/metaact.cxx b/vcl/source/gdi/metaact.cxx index 2d0c94c479ec..f67c1cc572dd 100644 --- a/vcl/source/gdi/metaact.cxx +++ b/vcl/source/gdi/metaact.cxx @@ -1263,13 +1263,7 @@ void MetaTextAction::Write( SvStream& rOStm, ImplMetaWriteData* pData ) rOStm << mnIndex; rOStm << mnLen; - sal_uInt16 nLen = sal::static_int_cast(maStr.getLength()); // version 2 - rOStm << nLen; - for (sal_uInt16 i = 0; i < nLen; ++i ) - { - sal_Unicode nUni = maStr[i]; - rOStm << nUni; - } + write_lenPrefixed_uInt16s_FromOUString(rOStm, maStr); // version 2 } // ------------------------------------------------------------------------ @@ -1410,13 +1404,7 @@ void MetaTextArrayAction::Write( SvStream& rOStm, ImplMetaWriteData* pData ) for( sal_uLong i = 0UL; i < nAryLen; i++ ) rOStm << mpDXAry[ i ]; - sal_uInt16 nLen = sal::static_int_cast(maStr.getLength()); // version 2 - rOStm << nLen; - for (sal_uInt16 j = 0; j < nLen; ++j ) - { - sal_Unicode nUni = maStr[j]; - rOStm << nUni; - } + write_lenPrefixed_uInt16s_FromOUString(rOStm, maStr); // version 2 } // ------------------------------------------------------------------------ @@ -1550,13 +1538,7 @@ void MetaStretchTextAction::Write( SvStream& rOStm, ImplMetaWriteData* pData ) rOStm << mnIndex; rOStm << mnLen; - sal_uInt16 nLen = sal::static_int_cast(maStr.getLength()); // version 2 - rOStm << nLen; - for ( sal_uInt16 i = 0; i < nLen; ++i ) - { - sal_Unicode nUni = maStr[i]; - rOStm << nUni; - } + write_lenPrefixed_uInt16s_FromOUString(rOStm, maStr); // version 2 } // ------------------------------------------------------------------------ @@ -1637,13 +1619,7 @@ void MetaTextRectAction::Write( SvStream& rOStm, ImplMetaWriteData* pData ) rOStm.WriteUniOrByteString( maStr, pData->meActualCharSet ); rOStm << mnStyle; - sal_uInt16 nLen = sal::static_int_cast(maStr.getLength()); // version 2 - rOStm << nLen; - for (sal_uInt16 i = 0; i < nLen; ++i) - { - sal_Unicode nUni = maStr[i]; - rOStm << nUni; - } + write_lenPrefixed_uInt16s_FromOUString(rOStm, maStr); // version 2 } // ------------------------------------------------------------------------ -- cgit