xref: /trunk/main/sc/source/filter/excel/xestring.cxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_scfilt.hxx"
26 
27 #include <algorithm>
28 #include <stdio.h>
29 #include "xestream.hxx"
30 #include "xlstyle.hxx"
31 #include "xestyle.hxx"
32 #include "xestring.hxx"
33 
34 using namespace ::oox;
35 
36 using ::rtl::OString;
37 using ::rtl::OUString;
38 
39 // ============================================================================
40 
41 namespace {
42 
43 // compare vectors
44 
45 /** Compares two vectors.
46     @return  A negative value, if rLeft<rRight; or a positive value, if rLeft>rRight;
47     or 0, if rLeft==rRight. */
48 template< typename Type >
lclCompareVectors(const::std::vector<Type> & rLeft,const::std::vector<Type> & rRight)49 int lclCompareVectors( const ::std::vector< Type >& rLeft, const ::std::vector< Type >& rRight )
50 {
51     int nResult = 0;
52 
53     // 1st: compare all elements of the vectors
54     typedef typename ::std::vector< Type >::const_iterator CIT;
55     CIT aEndL = rLeft.end(), aEndR = rRight.end();
56     for( CIT aItL = rLeft.begin(), aItR = rRight.begin(); !nResult && (aItL != aEndL) && (aItR != aEndR); ++aItL, ++aItR )
57         nResult = static_cast< int >( *aItL ) - static_cast< int >( *aItR );
58 
59     // 2nd: no differences found so far -> compare the vector sizes. Shorter vector is less
60     if( !nResult )
61         nResult = static_cast< int >( rLeft.size() ) - static_cast< int >( rRight.size() );
62 
63     return nResult;
64 }
65 
66 // hashing helpers
67 
68 /** Base class for value hashers.
69     @descr  These function objects are used to hash any value to a sal_uInt32 value. */
70 template< typename Type >
71 struct XclHasher
72 {
73     typedef Type argument_type;
74     typedef sal_uInt32 result_type;
75 };
76 
77 template< typename Type >
78 struct XclDirectHasher : public XclHasher< Type >
79 {
operator ()__anon080073250111::XclDirectHasher80     inline sal_uInt32   operator()( Type nVal ) const { return nVal; }
81 };
82 
83 struct XclFormatRunHasher : public XclHasher< const XclFormatRun& >
84 {
operator ()__anon080073250111::XclFormatRunHasher85     inline sal_uInt32   operator()( const XclFormatRun& rRun ) const
86                             { return (rRun.mnChar << 8) ^ rRun.mnFontIdx; }
87 };
88 
89 /** Calculates a hash value from a vector.
90     @descr Uses the passed hasher function object to calculate hash values from
91     all vector elements. */
92 template< typename Type, typename ValueHasher >
lclHashVector(const::std::vector<Type> & rVec,const ValueHasher & rHasher)93 sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec, const ValueHasher& rHasher )
94 {
95     sal_uInt32 nHash = rVec.size();
96     typedef typename ::std::vector< Type >::const_iterator CIT;
97     for( CIT aIt = rVec.begin(), aEnd = rVec.end(); aIt != aEnd; ++aIt )
98         (nHash *= 31) += rHasher( *aIt );
99     return static_cast< sal_uInt16 >( nHash ^ (nHash >> 16) );
100 }
101 
102 /** Calculates a hash value from a vector. Uses XclDirectHasher to hash the vector elements. */
103 template< typename Type >
lclHashVector(const::std::vector<Type> & rVec)104 inline sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec )
105 {
106     return lclHashVector( rVec, XclDirectHasher< Type >() );
107 }
108 
109 } // namespace
110 
111 // constructors ---------------------------------------------------------------
112 
XclExpString(XclStrFlags nFlags,sal_uInt16 nMaxLen)113 XclExpString::XclExpString( XclStrFlags nFlags, sal_uInt16 nMaxLen )
114 {
115     Init( 0, nFlags, nMaxLen, true );
116 }
117 
XclExpString(const String & rString,XclStrFlags nFlags,sal_uInt16 nMaxLen)118 XclExpString::XclExpString( const String& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
119 {
120     Assign( rString, nFlags, nMaxLen );
121 }
122 
XclExpString(const OUString & rString,XclStrFlags nFlags,sal_uInt16 nMaxLen)123 XclExpString::XclExpString( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
124 {
125     Assign( rString, nFlags, nMaxLen );
126 }
127 
128 //UNUSED2008-05  XclExpString::XclExpString(
129 //UNUSED2008-05      const String& rString, const XclFormatRunVec& rFormats,
130 //UNUSED2008-05      XclStrFlags nFlags, sal_uInt16 nMaxLen )
131 //UNUSED2008-05  {
132 //UNUSED2008-05      Assign( rString, rFormats, nFlags, nMaxLen );
133 //UNUSED2008-05  }
134 //UNUSED2008-05
135 //UNUSED2008-05  XclExpString::XclExpString(
136 //UNUSED2008-05          const OUString& rString, const XclFormatRunVec& rFormats,
137 //UNUSED2008-05          XclStrFlags nFlags, sal_uInt16 nMaxLen )
138 //UNUSED2008-05  {
139 //UNUSED2008-05      Assign( rString, rFormats, nFlags, nMaxLen );
140 //UNUSED2008-05  }
141 
142 // assign ---------------------------------------------------------------------
143 
Assign(const String & rString,XclStrFlags nFlags,sal_uInt16 nMaxLen)144 void XclExpString::Assign( const String& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
145 {
146     Build( rString.GetBuffer(), rString.Len(), nFlags, nMaxLen );
147 }
148 
Assign(const String & rString,const XclFormatRunVec & rFormats,XclStrFlags nFlags,sal_uInt16 nMaxLen)149 void XclExpString::Assign(
150         const String& rString, const XclFormatRunVec& rFormats,
151         XclStrFlags nFlags, sal_uInt16 nMaxLen )
152 {
153     Assign( rString, nFlags, nMaxLen );
154     SetFormats( rFormats );
155 }
156 
Assign(const OUString & rString,XclStrFlags nFlags,sal_uInt16 nMaxLen)157 void XclExpString::Assign( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
158 {
159     Build( rString.getStr(), rString.getLength(), nFlags, nMaxLen );
160 }
161 
Assign(const OUString & rString,const XclFormatRunVec & rFormats,XclStrFlags nFlags,sal_uInt16 nMaxLen)162 void XclExpString::Assign(
163         const OUString& rString, const XclFormatRunVec& rFormats,
164         XclStrFlags nFlags, sal_uInt16 nMaxLen )
165 {
166     Assign( rString, nFlags, nMaxLen );
167     SetFormats( rFormats );
168 }
169 
Assign(sal_Unicode cChar,XclStrFlags nFlags,sal_uInt16 nMaxLen)170 void XclExpString::Assign( sal_Unicode cChar, XclStrFlags nFlags, sal_uInt16 nMaxLen )
171 {
172     Build( &cChar, 1, nFlags, nMaxLen );
173 }
174 
AssignByte(const String & rString,rtl_TextEncoding eTextEnc,XclStrFlags nFlags,sal_uInt16 nMaxLen)175 void XclExpString::AssignByte(
176         const String& rString, rtl_TextEncoding eTextEnc, XclStrFlags nFlags, sal_uInt16 nMaxLen )
177 {
178     ByteString aByteStr( rString, eTextEnc );   // length may differ from length of rString
179     Build( aByteStr.GetBuffer(), aByteStr.Len(), nFlags, nMaxLen );
180 }
181 
182 //UNUSED2008-05  void XclExpString::AssignByte( sal_Unicode cChar, rtl_TextEncoding eTextEnc, XclStrFlags nFlags, sal_uInt16 nMaxLen )
183 //UNUSED2008-05  {
184 //UNUSED2008-05      if( !cChar )
185 //UNUSED2008-05      {
186 //UNUSED2008-05          sal_Char cByteChar = 0;
187 //UNUSED2008-05          Build( &cByteChar, 1, nFlags, nMaxLen );
188 //UNUSED2008-05      }
189 //UNUSED2008-05      else
190 //UNUSED2008-05      {
191 //UNUSED2008-05          ByteString aByteStr( &cChar, 1, eTextEnc );     // length may be >1
192 //UNUSED2008-05          Build( aByteStr.GetBuffer(), aByteStr.Len(), nFlags, nMaxLen );
193 //UNUSED2008-05      }
194 //UNUSED2008-05  }
195 
196 // append ---------------------------------------------------------------------
197 
Append(const String & rString)198 void XclExpString::Append( const String& rString )
199 {
200     BuildAppend( rString.GetBuffer(), rString.Len() );
201 }
202 
203 //UNUSED2008-05  void XclExpString::Append( const ::rtl::OUString& rString )
204 //UNUSED2008-05  {
205 //UNUSED2008-05      BuildAppend( rString.getStr(), rString.getLength() );
206 //UNUSED2008-05  }
207 //UNUSED2008-05
208 //UNUSED2008-05  void XclExpString::Append( sal_Unicode cChar )
209 //UNUSED2008-05  {
210 //UNUSED2008-05      BuildAppend( &cChar, 1 );
211 //UNUSED2008-05  }
212 
AppendByte(const String & rString,rtl_TextEncoding eTextEnc)213 void XclExpString::AppendByte( const String& rString, rtl_TextEncoding eTextEnc )
214 {
215     if( rString.Len() > 0 )
216     {
217         ByteString aByteStr( rString, eTextEnc );   // length may differ from length of rString
218         BuildAppend( aByteStr.GetBuffer(), aByteStr.Len() );
219     }
220 }
221 
AppendByte(sal_Unicode cChar,rtl_TextEncoding eTextEnc)222 void XclExpString::AppendByte( sal_Unicode cChar, rtl_TextEncoding eTextEnc )
223 {
224     if( !cChar )
225     {
226         sal_Char cByteChar = 0;
227         BuildAppend( &cByteChar, 1 );
228     }
229     else
230     {
231         ByteString aByteStr( &cChar, 1, eTextEnc );     // length may be >1
232         BuildAppend( aByteStr.GetBuffer(), aByteStr.Len() );
233     }
234 }
235 
236 // formatting runs ------------------------------------------------------------
237 
SetFormats(const XclFormatRunVec & rFormats)238 void XclExpString::SetFormats( const XclFormatRunVec& rFormats )
239 {
240     maFormats = rFormats;
241 #ifdef DBG_UTIL
242     if( IsRich() )
243     {
244         XclFormatRunVec::const_iterator aCurr = maFormats.begin();
245         XclFormatRunVec::const_iterator aPrev = aCurr;
246         XclFormatRunVec::const_iterator aEnd = maFormats.end();
247         for( ++aCurr; aCurr != aEnd; ++aCurr, ++aPrev )
248             DBG_ASSERT( aPrev->mnChar < aCurr->mnChar, "XclExpString::SetFormats - invalid char order" );
249         DBG_ASSERT( aPrev->mnChar <= mnLen, "XclExpString::SetFormats - invalid char index" );
250     }
251 #endif
252     LimitFormatCount( mbIsBiff8 ? EXC_STR_MAXLEN : EXC_STR_MAXLEN_8BIT );
253 }
254 
AppendFormat(sal_uInt16 nChar,sal_uInt16 nFontIdx,bool bDropDuplicate)255 void XclExpString::AppendFormat( sal_uInt16 nChar, sal_uInt16 nFontIdx, bool bDropDuplicate )
256 {
257     DBG_ASSERT( maFormats.empty() || (maFormats.back().mnChar < nChar), "XclExpString::AppendFormat - invalid char index" );
258     size_t nMaxSize = static_cast< size_t >( mbIsBiff8 ? EXC_STR_MAXLEN : EXC_STR_MAXLEN_8BIT );
259     if( maFormats.empty() || ((maFormats.size() < nMaxSize) && (!bDropDuplicate || (maFormats.back().mnFontIdx != nFontIdx))) )
260         maFormats.push_back( XclFormatRun( nChar, nFontIdx ) );
261 }
262 
AppendTrailingFormat(sal_uInt16 nFontIdx)263 void XclExpString::AppendTrailingFormat( sal_uInt16 nFontIdx )
264 {
265     AppendFormat( mnLen, nFontIdx, false );
266 }
267 
LimitFormatCount(sal_uInt16 nMaxCount)268 void XclExpString::LimitFormatCount( sal_uInt16 nMaxCount )
269 {
270     if( maFormats.size() > nMaxCount )
271         maFormats.erase( maFormats.begin() + nMaxCount, maFormats.end() );
272 }
273 
RemoveLeadingFont()274 sal_uInt16 XclExpString::RemoveLeadingFont()
275 {
276     sal_uInt16 nFontIdx = EXC_FONT_NOTFOUND;
277     if( !maFormats.empty() && (maFormats.front().mnChar == 0) )
278     {
279         nFontIdx = maFormats.front().mnFontIdx;
280         maFormats.erase( maFormats.begin() );
281     }
282     return nFontIdx;
283 }
284 
IsEqual(const XclExpString & rCmp) const285 bool XclExpString::IsEqual( const XclExpString& rCmp ) const
286 {
287     return
288         (mnLen          == rCmp.mnLen)          &&
289         (mbIsBiff8      == rCmp.mbIsBiff8)      &&
290         (mbIsUnicode    == rCmp.mbIsUnicode)    &&
291         (mbWrapped      == rCmp.mbWrapped)      &&
292         (
293             ( mbIsBiff8 && (maUniBuffer  == rCmp.maUniBuffer)) ||
294             (!mbIsBiff8 && (maCharBuffer == rCmp.maCharBuffer))
295         ) &&
296         (maFormats      == rCmp.maFormats);
297 }
298 
IsLessThan(const XclExpString & rCmp) const299 bool XclExpString::IsLessThan( const XclExpString& rCmp ) const
300 {
301     int nResult = mbIsBiff8 ?
302         lclCompareVectors( maUniBuffer, rCmp.maUniBuffer ) :
303         lclCompareVectors( maCharBuffer, rCmp.maCharBuffer );
304     return (nResult != 0) ? (nResult < 0) : (maFormats < rCmp.maFormats);
305 }
306 
307 // get data -------------------------------------------------------------------
308 
GetFormatsCount() const309 sal_uInt16 XclExpString::GetFormatsCount() const
310 {
311     return static_cast< sal_uInt16 >( maFormats.size() );
312 }
313 
GetFlagField() const314 sal_uInt8 XclExpString::GetFlagField() const
315 {
316     return (mbIsUnicode ? EXC_STRF_16BIT : 0) | (IsWriteFormats() ? EXC_STRF_RICH : 0);
317 }
318 
GetHeaderSize() const319 sal_uInt16 XclExpString::GetHeaderSize() const
320 {
321     return
322         (mb8BitLen ? 1 : 2) +           // length field
323         (IsWriteFlags() ? 1 : 0) +      // flag field
324         (IsWriteFormats() ? 2 : 0);     // richtext formattting count
325 }
326 
GetBufferSize() const327 sal_Size XclExpString::GetBufferSize() const
328 {
329     return mnLen * (mbIsUnicode ? 2 : 1);
330 }
331 
GetSize() const332 sal_Size XclExpString::GetSize() const
333 {
334     return
335         GetHeaderSize() +                                   // header
336         GetBufferSize() +                                   // character buffer
337         (IsWriteFormats() ? (4 * GetFormatsCount()) : 0);   // richtext formattting
338 }
339 
GetChar(sal_uInt16 nCharIdx) const340 sal_uInt16 XclExpString::GetChar( sal_uInt16 nCharIdx ) const
341 {
342     DBG_ASSERT( nCharIdx < Len(), "XclExpString::GetChar - invalid character index" );
343     return static_cast< sal_uInt16 >( mbIsBiff8 ? maUniBuffer[ nCharIdx ] : maCharBuffer[ nCharIdx ] );
344 }
345 
GetHash() const346 sal_uInt16 XclExpString::GetHash() const
347 {
348     return
349         (mbIsBiff8 ? lclHashVector( maUniBuffer ) : lclHashVector( maCharBuffer )) ^
350         lclHashVector( maFormats, XclFormatRunHasher() );
351 }
352 
353 // streaming ------------------------------------------------------------------
354 
WriteLenField(XclExpStream & rStrm) const355 void XclExpString::WriteLenField( XclExpStream& rStrm ) const
356 {
357     if( mb8BitLen )
358         rStrm << static_cast< sal_uInt8 >( mnLen );
359     else
360         rStrm << mnLen;
361 }
362 
WriteFlagField(XclExpStream & rStrm) const363 void XclExpString::WriteFlagField( XclExpStream& rStrm ) const
364 {
365     if( mbIsBiff8 )
366     {
367         PrepareWrite( rStrm, 1 );
368         rStrm << GetFlagField();
369         rStrm.SetSliceSize( 0 );
370     }
371 }
372 
WriteHeader(XclExpStream & rStrm) const373 void XclExpString::WriteHeader( XclExpStream& rStrm ) const
374 {
375     DBG_ASSERT( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeader - string too long" );
376     PrepareWrite( rStrm, GetHeaderSize() );
377     // length
378     WriteLenField( rStrm );
379     // flag field
380     if( IsWriteFlags() )
381         rStrm << GetFlagField();
382     // format run count
383     if( IsWriteFormats() )
384         rStrm << GetFormatsCount();
385     rStrm.SetSliceSize( 0 );
386 }
387 
WriteBuffer(XclExpStream & rStrm) const388 void XclExpString::WriteBuffer( XclExpStream& rStrm ) const
389 {
390     if( mbIsBiff8 )
391         rStrm.WriteUnicodeBuffer( maUniBuffer, GetFlagField() );
392     else
393         rStrm.WriteCharBuffer( maCharBuffer );
394 }
395 
WriteFormats(XclExpStream & rStrm,bool bWriteSize) const396 void XclExpString::WriteFormats( XclExpStream& rStrm, bool bWriteSize ) const
397 {
398     if( IsRich() )
399     {
400         XclFormatRunVec::const_iterator aIt = maFormats.begin(), aEnd = maFormats.end();
401         if( mbIsBiff8 )
402         {
403             if( bWriteSize )
404                 rStrm << GetFormatsCount();
405             rStrm.SetSliceSize( 4 );
406             for( ; aIt != aEnd; ++aIt )
407                 rStrm << aIt->mnChar << aIt->mnFontIdx;
408         }
409         else
410         {
411             if( bWriteSize )
412                 rStrm << static_cast< sal_uInt8 >( GetFormatsCount() );
413             rStrm.SetSliceSize( 2 );
414             for( ; aIt != aEnd; ++aIt )
415                 rStrm << static_cast< sal_uInt8 >( aIt->mnChar ) << static_cast< sal_uInt8 >( aIt->mnFontIdx );
416         }
417         rStrm.SetSliceSize( 0 );
418     }
419 }
420 
Write(XclExpStream & rStrm) const421 void XclExpString::Write( XclExpStream& rStrm ) const
422 {
423     if (!mbSkipHeader)
424         WriteHeader( rStrm );
425     WriteBuffer( rStrm );
426     if( IsWriteFormats() )      // only in BIFF8 included in string
427         WriteFormats( rStrm );
428 }
429 
WriteHeaderToMem(sal_uInt8 * pnMem) const430 void XclExpString::WriteHeaderToMem( sal_uInt8* pnMem ) const
431 {
432     DBG_ASSERT( pnMem, "XclExpString::WriteHeaderToMem - no memory to write to" );
433     DBG_ASSERT( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeaderToMem - string too long" );
434     DBG_ASSERT( !IsWriteFormats(), "XclExpString::WriteHeaderToMem - formatted strings not supported" );
435     // length
436     if( mb8BitLen )
437     {
438         *pnMem = static_cast< sal_uInt8 >( mnLen );
439         ++pnMem;
440     }
441     else
442     {
443         ShortToSVBT16( mnLen, pnMem );
444         pnMem += 2;
445     }
446     // flag field
447     if( IsWriteFlags() )
448         *pnMem = GetFlagField();
449 }
450 
WriteBufferToMem(sal_uInt8 * pnMem) const451 void XclExpString::WriteBufferToMem( sal_uInt8* pnMem ) const
452 {
453     DBG_ASSERT( pnMem, "XclExpString::WriteBufferToMem - no memory to write to" );
454     if( !IsEmpty() )
455     {
456         if( mbIsBiff8 )
457         {
458             for( ScfUInt16Vec::const_iterator aIt = maUniBuffer.begin(), aEnd = maUniBuffer.end(); aIt != aEnd; ++aIt )
459             {
460                 sal_uInt16 nChar = *aIt;
461                 *pnMem = static_cast< sal_uInt8 >( nChar );
462                 ++pnMem;
463                 if( mbIsUnicode )
464                 {
465                     *pnMem = static_cast< sal_uInt8 >( nChar >> 8 );
466                     ++pnMem;
467                 }
468             }
469         }
470         else
471             memcpy( pnMem, &maCharBuffer[ 0 ], mnLen );
472     }
473 }
474 
WriteToMem(sal_uInt8 * pnMem) const475 void XclExpString::WriteToMem( sal_uInt8* pnMem ) const
476 {
477     WriteHeaderToMem( pnMem );
478     WriteBufferToMem( pnMem + GetHeaderSize() );
479 }
480 
lcl_WriteRun(XclExpXmlStream & rStrm,const ScfUInt16Vec & rBuffer,sal_uInt16 nStart,sal_Int32 nLength,const XclExpFont * pFont)481 static sal_uInt16 lcl_WriteRun( XclExpXmlStream& rStrm, const ScfUInt16Vec& rBuffer, sal_uInt16 nStart, sal_Int32 nLength, const XclExpFont* pFont )
482 {
483     if( nLength == 0 )
484         return nStart;
485 
486     sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
487 
488     rWorksheet->startElement( XML_r, FSEND );
489     if( pFont )
490     {
491         const XclFontData& rFontData = pFont->GetFontData();
492         rWorksheet->startElement( XML_rPr, FSEND );
493         rStrm.WriteFontData( rFontData, XML_rFont );
494         rWorksheet->endElement( XML_rPr );
495     }
496     rWorksheet->startElement( XML_t,
497             FSNS( XML_xml, XML_space ), "preserve",
498             FSEND );
499     rWorksheet->writeEscaped( XclXmlUtils::ToOUString( rBuffer, nStart, nLength ) );
500     rWorksheet->endElement( XML_t );
501     rWorksheet->endElement( XML_r );
502     return static_cast<sal_uInt16>(nStart + nLength);
503 }
504 
WriteXml(XclExpXmlStream & rStrm) const505 void XclExpString::WriteXml( XclExpXmlStream& rStrm ) const
506 {
507     sax_fastparser::FSHelperPtr rWorksheet = rStrm.GetCurrentStream();
508 
509     if( !IsWriteFormats() )
510     {
511         rWorksheet->startElement( XML_t, FSEND );
512         rWorksheet->writeEscaped( XclXmlUtils::ToOUString( *this ) );
513         rWorksheet->endElement( XML_t );
514     }
515     else
516     {
517         XclExpFontBuffer& rFonts = rStrm.GetRoot().GetFontBuffer();
518         XclFormatRunVec::const_iterator aIt = maFormats.begin(), aEnd = maFormats.end();
519 
520         sal_uInt16  nStart = 0;
521         const XclExpFont* pFont = NULL;
522         for ( ; aIt != aEnd; ++aIt )
523         {
524             nStart = lcl_WriteRun( rStrm, GetUnicodeBuffer(),
525                     nStart, aIt->mnChar-nStart, pFont );
526             pFont = rFonts.GetFont( aIt->mnFontIdx );
527         }
528         lcl_WriteRun( rStrm, GetUnicodeBuffer(),
529                 nStart, GetUnicodeBuffer().size() - nStart, pFont );
530     }
531 }
532 
533 // ----------------------------------------------------------------------------
534 
IsWriteFlags() const535 bool XclExpString::IsWriteFlags() const
536 {
537     return mbIsBiff8 && (!IsEmpty() || !mbSmartFlags);
538 }
539 
IsWriteFormats() const540 bool XclExpString::IsWriteFormats() const
541 {
542     return mbIsBiff8 && !mbSkipFormats && IsRich();
543 }
544 
SetStrLen(sal_Int32 nNewLen)545 void XclExpString::SetStrLen( sal_Int32 nNewLen )
546 {
547     sal_uInt16 nAllowedLen = (mb8BitLen && (mnMaxLen > 255)) ? 255 : mnMaxLen;
548     mnLen = limit_cast< sal_uInt16 >( nNewLen, 0, nAllowedLen );
549 }
550 
CharsToBuffer(const sal_Unicode * pcSource,sal_Int32 nBegin,sal_Int32 nLen)551 void XclExpString::CharsToBuffer( const sal_Unicode* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
552 {
553     DBG_ASSERT( maUniBuffer.size() >= static_cast< size_t >( nBegin + nLen ),
554         "XclExpString::CharsToBuffer - char buffer invalid" );
555     ScfUInt16Vec::iterator aBeg = maUniBuffer.begin() + nBegin;
556     ScfUInt16Vec::iterator aEnd = aBeg + nLen;
557     const sal_Unicode* pcSrcChar = pcSource;
558     for( ScfUInt16Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
559     {
560         *aIt = static_cast< sal_uInt16 >( *pcSrcChar );
561         if( *aIt & 0xFF00 )
562             mbIsUnicode = true;
563     }
564     if( !mbWrapped )
565         mbWrapped = ::std::find( aBeg, aEnd, EXC_LF ) != aEnd;
566 }
567 
CharsToBuffer(const sal_Char * pcSource,sal_Int32 nBegin,sal_Int32 nLen)568 void XclExpString::CharsToBuffer( const sal_Char* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
569 {
570     DBG_ASSERT( maCharBuffer.size() >= static_cast< size_t >( nBegin + nLen ),
571         "XclExpString::CharsToBuffer - char buffer invalid" );
572     ScfUInt8Vec::iterator aBeg = maCharBuffer.begin() + nBegin;
573     ScfUInt8Vec::iterator aEnd = aBeg + nLen;
574     const sal_Char* pcSrcChar = pcSource;
575     for( ScfUInt8Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
576         *aIt = static_cast< sal_uInt8 >( *pcSrcChar );
577     mbIsUnicode = false;
578     if( !mbWrapped )
579         mbWrapped = ::std::find( aBeg, aEnd, EXC_LF_C ) != aEnd;
580 }
581 
Init(sal_Int32 nCurrLen,XclStrFlags nFlags,sal_uInt16 nMaxLen,bool bBiff8)582 void XclExpString::Init( sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen, bool bBiff8 )
583 {
584     mbIsBiff8 = bBiff8;
585     mbIsUnicode = bBiff8 && ::get_flag( nFlags, EXC_STR_FORCEUNICODE );
586     mb8BitLen = ::get_flag( nFlags, EXC_STR_8BITLENGTH );
587     mbSmartFlags = bBiff8 && ::get_flag( nFlags, EXC_STR_SMARTFLAGS );
588     mbSkipFormats = ::get_flag( nFlags, EXC_STR_SEPARATEFORMATS );
589     mbWrapped = false;
590     mbSkipHeader = ::get_flag( nFlags, EXC_STR_NOHEADER );
591     mnMaxLen = nMaxLen;
592     SetStrLen( nCurrLen );
593 
594     maFormats.clear();
595     if( mbIsBiff8 )
596     {
597         maCharBuffer.clear();
598         maUniBuffer.resize( mnLen );
599     }
600     else
601     {
602         maUniBuffer.clear();
603         maCharBuffer.resize( mnLen );
604     }
605 }
606 
Build(const sal_Unicode * pcSource,sal_Int32 nCurrLen,XclStrFlags nFlags,sal_uInt16 nMaxLen)607 void XclExpString::Build( const sal_Unicode* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
608 {
609     Init( nCurrLen, nFlags, nMaxLen, true );
610     CharsToBuffer( pcSource, 0, mnLen );
611 }
612 
Build(const sal_Char * pcSource,sal_Int32 nCurrLen,XclStrFlags nFlags,sal_uInt16 nMaxLen)613 void XclExpString::Build( const sal_Char* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
614 {
615     Init( nCurrLen, nFlags, nMaxLen, false );
616     CharsToBuffer( pcSource, 0, mnLen );
617 }
618 
InitAppend(sal_Int32 nAddLen)619 void XclExpString::InitAppend( sal_Int32 nAddLen )
620 {
621     SetStrLen( static_cast< sal_Int32 >( mnLen ) + nAddLen );
622     if( mbIsBiff8 )
623         maUniBuffer.resize( mnLen );
624     else
625         maCharBuffer.resize( mnLen );
626 }
627 
BuildAppend(const sal_Unicode * pcSource,sal_Int32 nAddLen)628 void XclExpString::BuildAppend( const sal_Unicode* pcSource, sal_Int32 nAddLen )
629 {
630     DBG_ASSERT( mbIsBiff8, "XclExpString::BuildAppend - must not be called at byte strings" );
631     if( mbIsBiff8 )
632     {
633         sal_uInt16 nOldLen = mnLen;
634         InitAppend( nAddLen );
635         CharsToBuffer( pcSource, nOldLen, mnLen - nOldLen );
636     }
637 }
638 
BuildAppend(const sal_Char * pcSource,sal_Int32 nAddLen)639 void XclExpString::BuildAppend( const sal_Char* pcSource, sal_Int32 nAddLen )
640 {
641     DBG_ASSERT( !mbIsBiff8, "XclExpString::BuildAppend - must not be called at unicode strings" );
642     if( !mbIsBiff8 )
643     {
644         sal_uInt16 nOldLen = mnLen;
645         InitAppend( nAddLen );
646         CharsToBuffer( pcSource, nOldLen, mnLen - nOldLen );
647     }
648 }
649 
PrepareWrite(XclExpStream & rStrm,sal_uInt16 nBytes) const650 void XclExpString::PrepareWrite( XclExpStream& rStrm, sal_uInt16 nBytes ) const
651 {
652     rStrm.SetSliceSize( nBytes + (mbIsUnicode ? 2 : 1) );
653 }
654 
655 // ============================================================================
656