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