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