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