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