diff options
author | Ivo Hinkelmann <ihi@openoffice.org> | 2011-02-16 13:08:06 +0100 |
---|---|---|
committer | Ivo Hinkelmann <ihi@openoffice.org> | 2011-02-16 13:08:06 +0100 |
commit | 8b332a7ed61b6be14be15b37ad5d417947d81791 (patch) | |
tree | 7de2ef928526012879697c04099d3376e6cfe95c | |
parent | f9626c6a8fa6609948b6482742ab069851c24423 (diff) | |
parent | 612abca175d0563c2f33f07748927ef4915a1081 (diff) |
CWS-TOOLING: integrate CWS ab80
-rw-r--r-- | tools/inc/tools/stream.hxx | 17 | ||||
-rwxr-xr-x | tools/source/stream/stream.cxx | 46 |
2 files changed, 56 insertions, 7 deletions
diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx index 66034d1dc10e..841b364fef6e 100644 --- a/tools/inc/tools/stream.hxx +++ b/tools/inc/tools/stream.hxx @@ -460,9 +460,20 @@ public: /// Switch to no endian swapping and write 0xfeff sal_Bool StartWritingUnicodeText(); - /// Read 16bit, if 0xfeff do nothing, if 0xfffe switch - /// endian swapping, if none of them put back - sal_Bool StartReadingUnicodeText(); + + /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, + if 0xfeff do nothing (UTF-16), if 0xfffe switch endian + swapping (UTF-16), if 0xefbb or 0xbbef read another byte + and check for UTF-8. If no UTF-* BOM was detected put all + read bytes back. This means that if 2 bytes were read it + was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 + BOM. There is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection! + + If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a + BOM of that encoding and switch endian swapping if UTF-16 + and 0xfffe. + */ + sal_Bool StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet ); /// Read a line of Unicode sal_Bool ReadUniStringLine( String& rStr ); diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index 6e1a6401b7a0..5d0da0f3e29d 100755 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -1046,21 +1046,59 @@ sal_Bool SvStream::StartWritingUnicodeText() |* *************************************************************************/ -sal_Bool SvStream::StartReadingUnicodeText() +sal_Bool SvStream::StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet ) { + if (!( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW || + eReadBomCharSet == RTL_TEXTENCODING_UNICODE || + eReadBomCharSet == RTL_TEXTENCODING_UTF8)) + return sal_True; // nothing to read + + bool bTryUtf8 = false; sal_uInt16 nFlag; + sal_sSize nBack = sizeof(nFlag); *this >> nFlag; switch ( nFlag ) { case 0xfeff : - // native + // native UTF-16 + if ( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW || + eReadBomCharSet == RTL_TEXTENCODING_UNICODE) + nBack = 0; break; case 0xfffe : - SetEndianSwap( !bSwap ); + // swapped UTF-16 + if ( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW || + eReadBomCharSet == RTL_TEXTENCODING_UNICODE) + { + SetEndianSwap( !bSwap ); + nBack = 0; + } + break; + case 0xefbb : + if (nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN && + (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW || + eReadBomCharSet == RTL_TEXTENCODING_UTF8)) + bTryUtf8 = true; + break; + case 0xbbef : + if (nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN && + (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW || + eReadBomCharSet == RTL_TEXTENCODING_UTF8)) + bTryUtf8 = true; break; default: - SeekRel( -((sal_sSize)sizeof(nFlag)) ); // no BOM, pure data + ; // nothing + } + if (bTryUtf8) + { + sal_uChar nChar; + nBack += sizeof(nChar); + *this >> nChar; + if (nChar == 0xbf) + nBack = 0; // it is UTF-8 } + if (nBack) + SeekRel( -nBack ); // no BOM, pure data return nError == SVSTREAM_OK; } |