LibreOffice Module sc (master)  1
ftools.cxx
Go to the documentation of this file.
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 #include <memory>
21 #include <ftools.hxx>
22 #include <osl/diagnose.h>
23 #include <osl/thread.h>
24 #include <tools/color.hxx>
25 #include <unotools/charclass.hxx>
26 #include <svl/itempool.hxx>
27 #include <svl/itemset.hxx>
28 #include <svl/poolitem.hxx>
29 #include <sot/storage.hxx>
30 
31 #include <math.h>
32 #include <global.hxx>
33 #include <stlpool.hxx>
34 #include <stlsheet.hxx>
35 #include <compiler.hxx>
36 
37 #include <orcusfiltersimpl.hxx>
38 
39 
40 // ScFilterTools::ReadLongDouble()
41 
43 
44 #ifdef __SIMPLE_FUNC // for <=VC 1.5
45 {
46  long double fRet;
47  rStrm.Read( &fRet, 10 );
48  return static_cast< double >( fRet );
49 }
50 #undef __SIMPLE_FUNC
51 
52 #else // detailed for all others
53 {
54 
55 /*
56 "Mapping - Guide" 10-Byte Intel
57 
58 77777777 77666666 66665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
59 98765432 10987654 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit-# total
60 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 Byte-#
61 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 Bit-# in Byte
62 SEEEEEEE EEEEEEEE IMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM Group
63 01111110 00000000 06665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
64 14321098 76543210 02109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit in Group
65 */
66 
67  long double lfDouble;
68  long double lfFactor = 256.0;
69  sal_uInt8 pDouble10[ 10 ];
70 
71  rStrm.ReadBytes(pDouble10, 10); // Intel-10 in pDouble10
72 
73  lfDouble = static_cast< long double >( pDouble10[ 7 ] ); // Byte 7
74  lfDouble *= lfFactor;
75  lfDouble += static_cast< long double >( pDouble10[ 6 ] ); // Byte 6
76  lfDouble *= lfFactor;
77  lfDouble += static_cast< long double >( pDouble10[ 5 ] ); // Byte 5
78  lfDouble *= lfFactor;
79  lfDouble += static_cast< long double >( pDouble10[ 4 ] ); // Byte 4
80  lfDouble *= lfFactor;
81  lfDouble += static_cast< long double >( pDouble10[ 3 ] ); // Byte 3
82  lfDouble *= lfFactor;
83  lfDouble += static_cast< long double >( pDouble10[ 2 ] ); // Byte 2
84  lfDouble *= lfFactor;
85  lfDouble += static_cast< long double >( pDouble10[ 1 ] ); // Byte 1
86  lfDouble *= lfFactor;
87  lfDouble += static_cast< long double >( pDouble10[ 0 ] ); // Byte 0
88 
89  // For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers
90  if( lfDouble != 0.0 )
91  {
92  // exponent
93  sal_Int32 nExp;
94  nExp = pDouble10[ 9 ] & 0x7F;
95  nExp <<= 8;
96  nExp += pDouble10[ 8 ];
97  nExp -= 16446;
98 
99  lfDouble *= pow( 2.0, static_cast< double >( nExp ) );
100  }
101 
102  // sign
103  if( pDouble10[ 9 ] & 0x80 )
104  lfDouble *= static_cast< long double >( -1.0 );
105 
106  return static_cast< double >( lfDouble );
107 }
108 #endif
109 
110 // *** common methods *** -----------------------------------------------------
111 
113 {
114  return osl_getThreadTextEncoding();
115 }
116 
117 OUString ScfTools::GetHexStr( sal_uInt16 nValue )
118 {
119  const char pHex[] = "0123456789ABCDEF";
120  OUString aStr = OUStringChar( pHex[ nValue >> 12 ] )
121  + OUStringChar( pHex[ (nValue >> 8) & 0x000F ] )
122  + OUStringChar( pHex[ (nValue >> 4) & 0x000F ] )
123  + OUStringChar( pHex[ nValue & 0x000F ] );
124  return aStr;
125 }
126 
128 {
129  sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
130  return static_cast< sal_uInt8 >( nTemp );
131 }
132 
133 Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
134 {
135  return Color(
136  GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
137  GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
138  GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) );
139 }
140 
141 // *** conversion of names *** ------------------------------------------------
142 
143 /* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */
144 
145 OUString ScfTools::ConvertToScDefinedName(const OUString& rName )
146 {
147  //fdo#37872: we don't allow points in range names any more
148  OUString sName = rName.replace(u'.',
149  u'_');
150  sal_Int32 nLen = sName.getLength();
152  sName = sName.replaceAt( 0, 1, "_" );
153  for( sal_Int32 nPos = 1; nPos < nLen; ++nPos )
155  sName = sName.replaceAt( nPos, 1, "_" );
156  return sName;
157 }
158 
159 // *** streams and storages *** -----------------------------------------------
160 
162 {
163  tools::SvRef<SotStorage> xSubStrg;
164  if( xStrg.is() && xStrg->IsContained( rStrgName ) )
165  xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_READ );
166  return xSubStrg;
167 }
168 
170 {
171  tools::SvRef<SotStorage> xSubStrg;
172  if( xStrg.is() )
173  xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_WRITE );
174  return xSubStrg;
175 }
176 
178 {
180  if( xStrg.is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) )
181  xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_READ );
182  return xStrm;
183 }
184 
186 {
187  OSL_ENSURE( !xStrg.is() || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
189  if( xStrg.is() )
190  xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_WRITE | StreamMode::TRUNC );
191  return xStrm;
192 }
193 
194 // *** item handling *** ------------------------------------------------------
195 
196 bool ScfTools::CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep )
197 {
198  return rItemSet.GetItemState( nWhichId, bDeep ) == SfxItemState::SET;
199 }
200 
201 bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep )
202 {
203  OSL_ENSURE( pnWhichIds, "ScfTools::CheckItems - no which id list" );
204  for( const sal_uInt16* pnWhichId = pnWhichIds; *pnWhichId != 0; ++pnWhichId )
205  if( CheckItem( rItemSet, *pnWhichId, bDeep ) )
206  return true;
207  return false;
208 }
209 
210 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef )
211 {
212  if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) )
213  {
214  rItemSet.Put( rItem.CloneSetWhich(nWhichId) );
215  }
216 }
217 
218 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef )
219 {
220  PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef );
221 }
222 
223 // *** style sheet handling *** -----------------------------------------------
224 
225 namespace {
226 
227 ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, SfxStyleFamily eFamily, bool bForceName )
228 {
229  // find an unused name
230  OUString aNewName( rStyleName );
231  sal_Int32 nIndex = 0;
232  SfxStyleSheetBase* pOldStyleSheet = nullptr;
233  while( SfxStyleSheetBase* pStyleSheet = rPool.Find( aNewName, eFamily ) )
234  {
235  if( !pOldStyleSheet )
236  pOldStyleSheet = pStyleSheet;
237  aNewName = rStyleName + " " + OUString::number( ++nIndex );
238  }
239 
240  // rename existing style
241  if( pOldStyleSheet && bForceName )
242  {
243  pOldStyleSheet->SetName( aNewName );
244  aNewName = rStyleName;
245  }
246 
247  // create new style sheet
248  return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SfxStyleSearchBits::UserDefined ) );
249 }
250 
251 } // namespace
252 
253 ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
254 {
255  return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Para, bForceName );
256 }
257 
258 ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
259 {
260  return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Page, bForceName );
261 }
262 
263 // *** byte string import operations *** --------------------------------------
264 
265 OString ScfTools::read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft)
266 {
267  OString aRet(::read_zeroTerminated_uInt8s_ToOString(rStrm));
268  rnBytesLeft -= aRet.getLength(); //we read this number of bytes anyway
269  if (rStrm.good()) //if the stream is happy we read the null terminator as well
270  --rnBytesLeft;
271  return aRet;
272 }
273 
274 void ScfTools::AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc )
275 {
276  rString += ::read_zeroTerminated_uInt8s_ToOUString(rStrm, eTextEnc);
277 }
278 
279 // *** HTML table names <-> named range names *** -----------------------------
280 
281 const OUString& ScfTools::GetHTMLDocName()
282 {
283  static const OUString saHTMLDoc( "HTML_all" );
284  return saHTMLDoc;
285 }
286 
288 {
289  static const OUString saHTMLTables( "HTML_tables" );
290  return saHTMLTables;
291 }
292 
294 {
295  static const OUString saHTMLIndexPrefix( "HTML_" );
296  return saHTMLIndexPrefix;
297 
298 }
299 
301 {
302  static const OUString saHTMLNamePrefix( "HTML__" );
303  return saHTMLNamePrefix;
304 }
305 
306 OUString ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex )
307 {
308  OUString aName = GetHTMLIndexPrefix() +
309  OUString::number( static_cast< sal_Int32 >( nIndex ) );
310  return aName;
311 }
312 
313 OUString ScfTools::GetNameFromHTMLName( const OUString& rTabName )
314 {
315  return GetHTMLNamePrefix() + rTabName;
316 }
317 
318 bool ScfTools::IsHTMLDocName( const OUString& rSource )
319 {
320  return rSource.equalsIgnoreAsciiCase( GetHTMLDocName() );
321 }
322 
323 bool ScfTools::IsHTMLTablesName( const OUString& rSource )
324 {
325  return rSource.equalsIgnoreAsciiCase( GetHTMLTablesName() );
326 }
327 
328 bool ScfTools::GetHTMLNameFromName( const OUString& rSource, OUString& rName )
329 {
330  rName.clear();
331  if( rSource.startsWithIgnoreAsciiCase( GetHTMLNamePrefix() ) )
332  {
333  rName = rSource.copy( GetHTMLNamePrefix().getLength() );
334  ScGlobal::AddQuotes( rName, '"', false );
335  }
336  else if( rSource.startsWithIgnoreAsciiCase( GetHTMLIndexPrefix() ) )
337  {
338  OUString aIndex( rSource.copy( GetHTMLIndexPrefix().getLength() ) );
339  if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.toInt32() > 0) )
340  rName = aIndex;
341  }
342  return !rName.isEmpty();
343 }
344 
347 
349 {
350  static ScOrcusFiltersImpl aImpl;
351  return &aImpl;
352 }
353 
355 {
356  return new ScFormatFilterPluginImpl();
357 }
358 
359 // implementation class inside the filters
360 
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static double ReadLongDouble(SvStream &rStrm)
Reads a 10-byte-long-double and converts it to double.
Definition: ftools.cxx:42
bool is() const
static void AppendCString(SvStream &rStrm, OUString &rString, rtl_TextEncoding eTextEnc)
Appends a zero terminated byte string.
Definition: ftools.cxx:274
sal_Int32 nIndex
static bool GetHTMLNameFromName(const OUString &rSource, OUString &rName)
Converts a built-in range name to an HTML table name.
Definition: ftools.cxx:328
sal_uInt8 GetRed() const
static bool CheckItem(const SfxItemSet &rItemSet, sal_uInt16 nWhichId, bool bDeep)
Returns true, if the passed item set contains the item.
Definition: ftools.cxx:196
Collection of orcus filter wrappers.
static bool IsHTMLTablesName(const OUString &rSource)
Returns true, if rSource is the built-in range name for all HTML tables.
Definition: ftools.cxx:323
static tools::SvRef< SotStorageStream > OpenStorageStreamWrite(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrmName)
Creates and opens a stream with the specified name in the passed storage (read/write).
Definition: ftools.cxx:185
static OUString ConvertToScDefinedName(const OUString &rName)
Converts a string to a valid Calc defined name or database range name.
Definition: ftools.cxx:145
static bool IsHTMLDocName(const OUString &rSource)
Returns true, if rSource is the built-in range name for an HTML document.
Definition: ftools.cxx:318
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true)
bool IsStream(const OUString &rEleName) const
static OUString GetHexStr(sal_uInt16 nValue)
Returns a string representing the hexadecimal value of nValue.
Definition: ftools.cxx:117
bool IsContained(const OUString &rEleName) const
static tools::SvRef< SotStorage > OpenStorageWrite(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrgName)
Creates and opens a storage with the specified name in the passed storage (read/write).
Definition: ftools.cxx:169
static OUString read_zeroTerminated_uInt8s_ToOUString(SvStream &rStrm, sal_Int32 &rnBytesLeft, rtl_TextEncoding eTextEnc)
Reads and returns a zero terminated byte string and decreases a stream counter.
Definition: ftools.hxx:213
static ScStyleSheet & MakePageStyleSheet(ScStyleSheetPool &rPool, const OUString &rStyleName, bool bForceName)
Creates and returns a page style sheet and inserts it into the pool.
Definition: ftools.cxx:258
SotStorage * OpenSotStorage(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE, bool transacted=true)
static const OUString & GetHTMLTablesName()
Returns the built-in range name for all HTML tables.
Definition: ftools.cxx:287
SfxStyleFamily
sal_uInt8 GetBlue() const
static OUString GetNameFromHTMLIndex(sal_uInt32 nIndex)
Returns the built-in range name for an HTML table, specified by table index.
Definition: ftools.cxx:306
const char * sName
static tools::SvRef< SotStorage > OpenStorageRead(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrgName)
Tries to open an existing storage with the specified name in the passed storage (read-only).
Definition: ftools.cxx:161
static Color GetMixedColor(const Color &rFore, const Color &rBack, sal_uInt8 nTrans)
Mixes colors with given transparence.
Definition: ftools.cxx:133
virtual ScOrcusFilters * GetOrcusFilters() override
Definition: ftools.cxx:348
static void PutItem(SfxItemSet &rItemSet, const SfxPoolItem &rItem, sal_uInt16 nWhichId, bool bSkipPoolDef)
Puts the item into the passed item set.
Definition: ftools.cxx:210
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
SotStorageStream * OpenSotStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE)
static sal_uInt8 GetMixedColorComp(sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans)
Mixes RGB components with given transparence.
Definition: ftools.cxx:127
static bool CheckItems(const SfxItemSet &rItemSet, const sal_uInt16 *pnWhichIds, bool bDeep)
Returns true, if the passed item set contains at least one of the items.
Definition: ftools.cxx:201
static bool IsCharFlagAllConventions(OUString const &rStr, sal_Int32 nPos, ScCharFlags nFlags)
If the character is allowed as tested by nFlags (SC_COMPILER_C_...
Definition: compiler.cxx:5001
static const OUString & GetHTMLDocName()
Returns the built-in range name for an HTML document.
Definition: ftools.cxx:281
float u
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
std::unique_ptr< SfxPoolItem > CloneSetWhich(sal_uInt16 nNewWhich) const
SfxItemPool * GetPool() const
ScFormatFilterPlugin * ScFilterCreate()
Definition: ftools.cxx:354
static tools::SvRef< SotStorageStream > OpenStorageStreamRead(tools::SvRef< SotStorage > const &xStrg, const OUString &rStrmName)
Tries to open an existing stream with the specified name in the passed storage (read-only).
Definition: ftools.cxx:177
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt8 GetGreen() const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
virtual ~ScFormatFilterPluginImpl()
Definition: ftools.cxx:346
static OUString GetNameFromHTMLName(const OUString &rTabName)
Returns the built-in range name for an HTML table, specified by table name.
Definition: ftools.cxx:313
std::deque< AttacherIndex_Impl > aIndex
unsigned char sal_uInt8
OUString aName
virtual SfxStyleSheetBase & Make(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits nMask=SfxStyleSearchBits::All) override
Definition: stlpool.cxx:71
static const OUString & GetHTMLNamePrefix()
Returns the prefix for table names.
Definition: ftools.cxx:300
double getLength(const B2DPolygon &rCandidate)
static bool isAsciiNumeric(const OUString &rStr)
bool good() const
static ScStyleSheet & MakeCellStyleSheet(ScStyleSheetPool &rPool, const OUString &rStyleName, bool bForceName)
Creates and returns a cell style sheet and inserts it into the pool.
Definition: ftools.cxx:253
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
static rtl_TextEncoding GetSystemTextEncoding()
Returns system text encoding for byte string conversion.
Definition: ftools.cxx:112
static SC_DLLPUBLIC void AddQuotes(OUString &rString, sal_Unicode cQuote, bool bEscapeEmbedded=true)
Inserts the character cQuote at beginning and end of rString.
Definition: global.cxx:677
aStr
static OString read_zeroTerminated_uInt8s_ToOString(SvStream &rStrm, sal_Int32 &rnBytesLeft)
Reads and returns a zero terminated byte string and decreases a stream counter.
Definition: ftools.cxx:265
sal_uInt16 Which() const
sal_uInt16 nPos
static const OUString & GetHTMLIndexPrefix()
Returns the prefix for table index names.
Definition: ftools.cxx:293