LCOV - code coverage report
Current view: top level - sc/source/filter/inc - xestring.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 16 16 100.0 %
Date: 2015-06-13 12:38:46 Functions: 11 13 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XESTRING_HXX
      21             : #define INCLUDED_SC_SOURCE_FILTER_INC_XESTRING_HXX
      22             : 
      23             : #include "xlstring.hxx"
      24             : 
      25             : class XclExpStream;
      26             : class XclExpXmlStream;
      27             : 
      28             : /** This class stores an unformatted or formatted string for Excel export.
      29             : 
      30             :     The class supports two completely different types of Excel strings:
      31             :     1)  BIFF2-BIFF7 byte strings: The text is encoded as a 8-bit character
      32             :         array. The strings cannot contain any character formatting.
      33             :     2)  BIFF8 Unicode strings: The text may be stored as UCS-2 character array,
      34             :         or compressed to an 8-bit array, if all characters are less than
      35             :         U+0100. Unicode strings may contain a formatting array, that specifies
      36             :         the used FONT record for different ranges of characters.
      37             : 
      38             :     The class provides full support for NUL characters in strings. On
      39             :     construction or assignment the passed flags specify the behaviour of the
      40             :     string while it is written to a stream (the 'Write' functions and
      41             :     'operator<<').
      42             :  */
      43         840 : class XclExpString
      44             : {
      45             : public:
      46             :     // constructors -----------------------------------------------------------
      47             : 
      48             :     /** Constructs an empty BIFF8 Unicode string.
      49             :         @param nFlags  Modifiers for string export.
      50             :         @param nMaxLen  The maximum number of characters to store in this string. */
      51             :     explicit            XclExpString(
      52             :                             XclStrFlags nFlags = EXC_STR_DEFAULT,
      53             :                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
      54             : 
      55             :     /** Constructs an unformatted BIFF8 Unicode string.
      56             :         @param nFlags  Modifiers for string export.
      57             :         @param nMaxLen  The maximum number of characters to store in this string. */
      58             :     explicit            XclExpString(
      59             :                             const OUString& rString,
      60             :                             XclStrFlags nFlags = EXC_STR_DEFAULT,
      61             :                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
      62             : 
      63             :     // assign -----------------------------------------------------------------
      64             : 
      65             :     /** Assigns an unformatted string, converts this object to a BIFF8 Unicode string.
      66             :         @param nFlags  Modifiers for string export.
      67             :         @param nMaxLen  The maximum number of characters to store in this string. */
      68             :     void                Assign(
      69             :                             const OUString& rString,
      70             :                             XclStrFlags nFlags = EXC_STR_DEFAULT,
      71             :                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
      72             : 
      73             :     /** Assigns a Unicode character, converts this object to a BIFF8 Unicode string.
      74             :         @param nFlags  Modifiers for string export.
      75             :         @param nMaxLen  The maximum number of characters to store in this string (for appending). */
      76             :     void                Assign(
      77             :                             sal_Unicode cChar,
      78             :                             XclStrFlags nFlags = EXC_STR_DEFAULT,
      79             :                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
      80             : 
      81             :     /** Assigns an unformatted string, converts this object to a BIFF2-BIFF7 byte string.
      82             :         @param nFlags  Modifiers for string export.
      83             :         @param nMaxLen  The maximum number of characters to store in this string. */
      84             :     void                AssignByte(
      85             :                             const OUString& rString,
      86             :                             rtl_TextEncoding eTextEnc,
      87             :                             XclStrFlags nFlags = EXC_STR_DEFAULT,
      88             :                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
      89             : 
      90             :     // append -----------------------------------------------------------------
      91             : 
      92             :     /** Appends a string. Uses the string flags used in constructor or last Assign().
      93             :         @descr  This object must be a BIFF8 Unicode string. */
      94             :     void                Append( const OUString& rString );
      95             : 
      96             :     /** Appends a string. Uses the string flags used in constructor or last Assign().
      97             :         @descr  This object must be a BIFF2-BIFF7 byte string. */
      98             :     void                AppendByte( const OUString& rString, rtl_TextEncoding eTextEnc );
      99             :     /** Appends a character. Uses the string flags used in constructor or last Assign().
     100             :         @descr  This object must be a BIFF2-BIFF7 byte string. */
     101             :     void                AppendByte( sal_Unicode cChar, rtl_TextEncoding eTextEnc );
     102             : 
     103             :     // formatting runs --------------------------------------------------------
     104             : 
     105             :     /** Appends a formatting run. nChar must be greater than last contained character index. */
     106             :     void                AppendFormat( sal_uInt16 nChar, sal_uInt16 nFontIdx, bool bDropDuplicate = true );
     107             :     /** Appends a trailing formatting run with the passed font index. */
     108             :     void                AppendTrailingFormat( sal_uInt16 nFontIdx );
     109             :     /** Removes formatting runs at the end, if the string contains too much. */
     110             :     void                LimitFormatCount( sal_uInt16 nMaxCount );
     111             :     /** Removes and returns the font index for the first char from the formatting runs, otherwise EXC_FONT_NOTFOUND. */
     112             :     sal_uInt16          RemoveLeadingFont();
     113             : 
     114             :     // get data ---------------------------------------------------------------
     115             : 
     116             :     /** Returns the character count of the string. */
     117         980 :     inline sal_uInt16   Len() const { return mnLen; }
     118             :     /** Returns true, if the string is empty. */
     119         756 :     inline bool         IsEmpty() const { return mnLen == 0; }
     120             :     /** Returns true, if the string contains line breaks. */
     121         455 :     inline bool         IsWrapped() const { return mbWrapped; }
     122             :     /** Returns true, if this string is equal to the passed string. */
     123             :     bool                IsEqual( const XclExpString& rCmp ) const;
     124             :     /** Returns true, if this string is less than the passed string. */
     125             :     bool                IsLessThan( const XclExpString& rCmp ) const;
     126             : 
     127             :     /** Returns true, if the string contains formatting information. */
     128        2085 :     inline bool         IsRich() const { return !maFormats.empty(); }
     129             :     /** Returns the current count of formatting runs for rich strings. */
     130             :     sal_uInt16          GetFormatsCount() const;
     131             :     /** Returns the vector with all formatting runs. */
     132          18 :     inline const XclFormatRunVec& GetFormats() const { return maFormats; }
     133             : 
     134             :     /** Returns the current string flags field to export. */
     135             :     sal_uInt8           GetFlagField() const;
     136             :     /** Returns the byte count the header will take on export. */
     137             :     sal_uInt16          GetHeaderSize() const;
     138             :     /** Returns the byte count the character buffer will take on export. */
     139             :     sal_Size            GetBufferSize() const;
     140             :     /** Returns the byte count the whole string will take on export. */
     141             :     sal_Size            GetSize() const;
     142             : 
     143             :     /** Returns the specified character from the (already encoded) string. */
     144             :     sal_uInt16          GetChar( sal_uInt16 nCharIdx ) const;
     145             :     /** Returns a hash value for the string. */
     146             :     sal_uInt16          GetHash() const;
     147             : 
     148         246 :     const ScfUInt16Vec& GetUnicodeBuffer() const { return maUniBuffer; }
     149             : 
     150             :     // streaming --------------------------------------------------------------
     151             : 
     152             :     /** Writes the string length field (1 byte or 2 bytes). */
     153             :     void                WriteLenField( XclExpStream& rStrm ) const;
     154             :     /** Writes the string flags field (1 byte). */
     155             :     void                WriteFlagField( XclExpStream& rStrm ) const;
     156             :     /** Writes 8-bit or 16-bit length field and string flags field. */
     157             :     void                WriteHeader( XclExpStream& rStrm ) const;
     158             :     /** Writes the raw character buffer. */
     159             :     void                WriteBuffer( XclExpStream& rStrm ) const;
     160             :     /** Writes the raw formatting run buffer. */
     161             :     void                WriteFormats( XclExpStream& rStrm, bool bWriteSize = false ) const;
     162             :     /** Writes the complete Unicode string. */
     163             :     void                Write( XclExpStream& rStrm ) const;
     164             : 
     165             :     /** Writes the string header to memory. */
     166             :     void                WriteHeaderToMem( sal_uInt8* pnMem ) const;
     167             :     /** Writes the raw character buffer to memory (8-bit or 16-bit little-endian). */
     168             :     void                WriteBufferToMem( sal_uInt8* pnMem ) const;
     169             :     /** Writes the entire string to memory. */
     170             :     void                WriteToMem( sal_uInt8* pnMem ) const;
     171             : 
     172             :     void                WriteXml( XclExpXmlStream& rStrm ) const;
     173             : 
     174             : private:
     175             :     /** Returns true, if the flag field should be written. */
     176             :     bool                IsWriteFlags() const;
     177             :     /** Returns true, if the formatting run vector should be written. */
     178             :     bool                IsWriteFormats() const;
     179             : 
     180             :     /** Sets the string length but regards the limit given in mnMaxLen. */
     181             :     void                SetStrLen( sal_Int32 nNewLen );
     182             :     /** Inserts the passed character array into the internal character buffer.
     183             :         @param nBegin  First index in internal buffer to fill.
     184             :         @param nLen  Number of characters to insert. */
     185             :     void                CharsToBuffer( const sal_Unicode* pcSource, sal_Int32 nBegin, sal_Int32 nLen );
     186             :     /** Inserts the passed character array into the internal character buffer.
     187             :         @param nBegin  First index in internal buffer to fill.
     188             :         @param nLen  Number of characters to insert. */
     189             :     void                CharsToBuffer( const sal_Char* pcSource, sal_Int32 nBegin, sal_Int32 nLen );
     190             : 
     191             :     /** Initializes flags, string length, and resizes character buffer.
     192             :         @param nFlags  Modifiers for string export.
     193             :         @param nCurrLen  The requested number of characters for the string.
     194             :         @param nMaxLen  The maximum length allowed of the resulting string.
     195             :         @param bBiff8  true = BIFF8 Unicode string; false = BIFF2-BIFF7 byte string. */
     196             :     void                Init( sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen, bool bBiff8 );
     197             :     /** Creates the character buffer from the given Unicode array.
     198             :         @param pcSource  The source character buffer. Trailing NUL character is not necessary.
     199             :         @param nFlags  Modifiers for string export.
     200             :         @param nCurrLen  The real count of characters contained in the passed buffer.
     201             :         @param nMaxLen  The maximum length allowed of the resulting string. */
     202             :     void                Build(
     203             :                             const sal_Unicode* pcSource, sal_Int32 nCurrLen,
     204             :                             XclStrFlags nFlags, sal_uInt16 nMaxLen );
     205             :     /** Creates the character buffer from the given character array.
     206             :         @param pcSource  The source character buffer. Trailing NUL character is not necessary.
     207             :         @param nFlags  Modifiers for string export.
     208             :         @param nCurrLen  The real count of characters contained in the passed buffer.
     209             :         @param nMaxLen  The maximum length allowed of the resulting string. */
     210             :     void                Build(
     211             :                             const sal_Char* pcSource, sal_Int32 nCurrLen,
     212             :                             XclStrFlags nFlags, sal_uInt16 nMaxLen );
     213             : 
     214             :     /** Initializes string length and resizes character buffers for appending operation.
     215             :         @param nAddLen  The number of characters to be appended. */
     216             :     void                InitAppend( sal_Int32 nAddLen );
     217             :     /** Appends the given Unicode array to the character buffer.
     218             :         @param pcSource  The source character buffer. Trailing NUL character is not necessary.
     219             :         @param nAddLen  The real count of characters contained in the passed buffer. */
     220             :     void                BuildAppend( const sal_Unicode* pcSource, sal_Int32 nAddLen );
     221             :     /** Appends the given character array to the character buffer.
     222             :         @param pcSource  The source character buffer. Trailing NUL character is not necessary.
     223             :         @param nAddLen  The real count of characters contained in the passed buffer. */
     224             :     void                BuildAppend( const sal_Char* pcSource, sal_Int32 nAddLen );
     225             : 
     226             :     /** Initializes write process on stream. */
     227             :     void                PrepareWrite( XclExpStream& rStrm, sal_uInt16 nBytes ) const;
     228             : 
     229             : private:
     230             :     ScfUInt16Vec        maUniBuffer;    /// The Unicode character buffer.
     231             :     ScfUInt8Vec         maCharBuffer;   /// The byte character buffer.
     232             :     XclFormatRunVec     maFormats;      /// All formatting runs.
     233             :     sal_uInt16          mnLen;          /// Character count to export.
     234             :     sal_uInt16          mnMaxLen;       /// Maximum allowed number of characters.
     235             :     bool                mbIsBiff8;      /// true = BIFF8 Unicode string, false = BIFF2-7 bytestring.
     236             :     bool                mbIsUnicode;    /// true, if at least one character is >0xFF.
     237             :     bool                mb8BitLen;      /// true = write 8-bit string length; false = 16-bit.
     238             :     bool                mbSmartFlags;   /// true = omit flags on empty string; false = always write flags.
     239             :     bool                mbSkipFormats;  /// true = skip formats on export; false = write complete formatted string.
     240             :     bool                mbWrapped;      /// true = text contains several paragraphs.
     241             :     bool                mbSkipHeader;   /// true = skip length and flags when writing string bytes.
     242             : };
     243             : 
     244          79 : inline bool operator==( const XclExpString& rLeft, const XclExpString& rRight )
     245             : {
     246          79 :     return rLeft.IsEqual( rRight );
     247             : }
     248             : 
     249          77 : inline bool operator!=( const XclExpString& rLeft, const XclExpString& rRight )
     250             : {
     251          77 :     return !(rLeft == rRight);
     252             : }
     253             : 
     254          83 : inline bool operator<( const XclExpString& rLeft, const XclExpString& rRight )
     255             : {
     256          83 :     return rLeft.IsLessThan( rRight );
     257             : }
     258             : 
     259         302 : inline XclExpStream& operator<<( XclExpStream& rStrm, const XclExpString& rString )
     260             : {
     261         302 :     rString.Write( rStrm );
     262         302 :     return rStrm;
     263             : }
     264             : 
     265             : #endif
     266             : 
     267             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11