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 SC_XESTRING_HXX
21 : #define SC_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 0 : 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 0 : inline sal_uInt16 Len() const { return mnLen; }
118 : /** Returns true, if the string is empty. */
119 0 : inline bool IsEmpty() const { return mnLen == 0; }
120 : /** Returns true, if the string contains line breaks. */
121 0 : 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 0 : 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 0 : 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 0 : 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 :
175 : private:
176 : /** Returns true, if the flag field should be written. */
177 : bool IsWriteFlags() const;
178 : /** Returns true, if the formatting run vector should be written. */
179 : bool IsWriteFormats() const;
180 :
181 : /** Sets the string length but regards the limit given in mnMaxLen. */
182 : void SetStrLen( sal_Int32 nNewLen );
183 : /** Inserts the passed character array into the internal character buffer.
184 : @param nBegin First index in internal buffer to fill.
185 : @param nLen Number of characters to insert. */
186 : void CharsToBuffer( const sal_Unicode* pcSource, sal_Int32 nBegin, sal_Int32 nLen );
187 : /** Inserts the passed character array into the internal character buffer.
188 : @param nBegin First index in internal buffer to fill.
189 : @param nLen Number of characters to insert. */
190 : void CharsToBuffer( const sal_Char* pcSource, sal_Int32 nBegin, sal_Int32 nLen );
191 :
192 : /** Initializes flags, string length, and resizes character buffer.
193 : @param nFlags Modifiers for string export.
194 : @param nCurrLen The requested number of characters for the string.
195 : @param nMaxLen The maximum length allowed of the resulting string.
196 : @param bBiff8 true = BIFF8 Unicode string; false = BIFF2-BIFF7 byte string. */
197 : void Init( sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen, bool bBiff8 );
198 : /** Creates the character buffer from the given Unicode array.
199 : @param pcSource The source character buffer. Trailing NUL character is not necessary.
200 : @param nFlags Modifiers for string export.
201 : @param nCurrLen The real count of characters contained in the passed buffer.
202 : @param nMaxLen The maximum length allowed of the resulting string. */
203 : void Build(
204 : const sal_Unicode* pcSource, sal_Int32 nCurrLen,
205 : XclStrFlags nFlags, sal_uInt16 nMaxLen );
206 : /** Creates the character buffer from the given character array.
207 : @param pcSource The source character buffer. Trailing NUL character is not necessary.
208 : @param nFlags Modifiers for string export.
209 : @param nCurrLen The real count of characters contained in the passed buffer.
210 : @param nMaxLen The maximum length allowed of the resulting string. */
211 : void Build(
212 : const sal_Char* pcSource, sal_Int32 nCurrLen,
213 : XclStrFlags nFlags, sal_uInt16 nMaxLen );
214 :
215 : /** Initializes string length and resizes character buffers for appending operation.
216 : @param nAddLen The number of characters to be appended. */
217 : void InitAppend( sal_Int32 nAddLen );
218 : /** Appends the given Unicode array to the character buffer.
219 : @param pcSource The source character buffer. Trailing NUL character is not necessary.
220 : @param nAddLen The real count of characters contained in the passed buffer. */
221 : void BuildAppend( const sal_Unicode* pcSource, sal_Int32 nAddLen );
222 : /** Appends the given character array to the character buffer.
223 : @param pcSource The source character buffer. Trailing NUL character is not necessary.
224 : @param nAddLen The real count of characters contained in the passed buffer. */
225 : void BuildAppend( const sal_Char* pcSource, sal_Int32 nAddLen );
226 :
227 : /** Initializes write process on stream. */
228 : void PrepareWrite( XclExpStream& rStrm, sal_uInt16 nBytes ) const;
229 :
230 : private:
231 : ScfUInt16Vec maUniBuffer; /// The Unicode character buffer.
232 : ScfUInt8Vec maCharBuffer; /// The byte character buffer.
233 : XclFormatRunVec maFormats; /// All formatting runs.
234 : sal_uInt16 mnLen; /// Character count to export.
235 : sal_uInt16 mnMaxLen; /// Maximum allowed number of characters.
236 : bool mbIsBiff8; /// true = BIFF8 Unicode string, false = BIFF2-7 bytestring.
237 : bool mbIsUnicode; /// true, if at least one character is >0xFF.
238 : bool mb8BitLen; /// true = write 8-bit string length; false = 16-bit.
239 : bool mbSmartFlags; /// true = omit flags on empty string; false = always write flags.
240 : bool mbSkipFormats; /// true = skip formats on export; false = write complete formatted string.
241 : bool mbWrapped; /// true = text contains several paragraphs.
242 : bool mbSkipHeader; /// ture = skip length and flags when writing string bytes.
243 : };
244 :
245 0 : inline bool operator==( const XclExpString& rLeft, const XclExpString& rRight )
246 : {
247 0 : return rLeft.IsEqual( rRight );
248 : }
249 :
250 0 : inline bool operator!=( const XclExpString& rLeft, const XclExpString& rRight )
251 : {
252 0 : return !(rLeft == rRight);
253 : }
254 :
255 0 : inline bool operator<( const XclExpString& rLeft, const XclExpString& rRight )
256 : {
257 0 : return rLeft.IsLessThan( rRight );
258 : }
259 :
260 0 : inline XclExpStream& operator<<( XclExpStream& rStrm, const XclExpString& rString )
261 : {
262 0 : rString.Write( rStrm );
263 0 : return rStrm;
264 : }
265 :
266 : #endif
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|