nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / filter / ftools / ftools.cxx
blobe1813a10d82a24ddfeb9210dfb08bae76e5c4b1d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
31 #include <math.h>
32 #include <global.hxx>
33 #include <stlpool.hxx>
34 #include <stlsheet.hxx>
35 #include <compiler.hxx>
37 #include <orcusfiltersimpl.hxx>
40 // ScFilterTools::ReadLongDouble()
42 double ScfTools::ReadLongDouble( SvStream& rStrm )
44 #ifdef __SIMPLE_FUNC // for <=VC 1.5
46 long double fRet;
47 rStrm.Read( &fRet, 10 );
48 return static_cast< double >( fRet );
50 #undef __SIMPLE_FUNC
52 #else // detailed for all others
56 "Mapping - Guide" 10-Byte Intel
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
67 long double lfDouble;
68 long double lfFactor = 256.0;
69 sal_uInt8 pDouble10[ 10 ];
71 rStrm.ReadBytes(pDouble10, 10); // Intel-10 in pDouble10
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
89 // For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers
90 if( lfDouble != 0.0 )
92 // exponent
93 sal_Int32 nExp;
94 nExp = pDouble10[ 9 ] & 0x7F;
95 nExp <<= 8;
96 nExp += pDouble10[ 8 ];
97 nExp -= 16446;
99 lfDouble *= pow( 2.0, static_cast< double >( nExp ) );
102 // sign
103 if( pDouble10[ 9 ] & 0x80 )
104 lfDouble *= static_cast< long double >( -1.0 );
106 return static_cast< double >( lfDouble );
108 #endif
110 // *** common methods *** -----------------------------------------------------
112 rtl_TextEncoding ScfTools::GetSystemTextEncoding()
114 return osl_getThreadTextEncoding();
117 OUString ScfTools::GetHexStr( sal_uInt16 nValue )
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;
127 sal_uInt8 ScfTools::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
129 sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
130 return static_cast< sal_uInt8 >( nTemp );
133 Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
135 return Color(
136 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
137 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
138 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) );
141 // *** conversion of names *** ------------------------------------------------
143 /* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */
145 OUString ScfTools::ConvertToScDefinedName(const OUString& rName )
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();
151 if( nLen && !ScCompiler::IsCharFlagAllConventions( sName, 0, ScCharFlags::CharName ) )
152 sName = sName.replaceAt( 0, 1, "_" );
153 for( sal_Int32 nPos = 1; nPos < nLen; ++nPos )
154 if( !ScCompiler::IsCharFlagAllConventions( sName, nPos, ScCharFlags::Name ) )
155 sName = sName.replaceAt( nPos, 1, "_" );
156 return sName;
159 // *** streams and storages *** -----------------------------------------------
161 tools::SvRef<SotStorage> ScfTools::OpenStorageRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName )
163 tools::SvRef<SotStorage> xSubStrg;
164 if( xStrg.is() && xStrg->IsContained( rStrgName ) )
165 xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_READ );
166 return xSubStrg;
169 tools::SvRef<SotStorage> ScfTools::OpenStorageWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName )
171 tools::SvRef<SotStorage> xSubStrg;
172 if( xStrg.is() )
173 xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_WRITE );
174 return xSubStrg;
177 tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName )
179 tools::SvRef<SotStorageStream> xStrm;
180 if( xStrg.is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) )
181 xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_READ );
182 return xStrm;
185 tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName )
187 OSL_ENSURE( !xStrg.is() || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
188 tools::SvRef<SotStorageStream> xStrm;
189 if( xStrg.is() )
190 xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_WRITE | StreamMode::TRUNC );
191 return xStrm;
194 // *** item handling *** ------------------------------------------------------
196 bool ScfTools::CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep )
198 return rItemSet.GetItemState( nWhichId, bDeep ) == SfxItemState::SET;
201 bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep )
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;
210 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef )
212 if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) )
214 rItemSet.Put( rItem.CloneSetWhich(nWhichId) );
218 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef )
220 PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef );
223 // *** style sheet handling *** -----------------------------------------------
225 namespace {
227 ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, SfxStyleFamily eFamily, bool bForceName )
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 ) )
235 if( !pOldStyleSheet )
236 pOldStyleSheet = pStyleSheet;
237 aNewName = rStyleName + " " + OUString::number( ++nIndex );
240 // rename existing style
241 if( pOldStyleSheet && bForceName )
243 pOldStyleSheet->SetName( aNewName );
244 aNewName = rStyleName;
247 // create new style sheet
248 return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SfxStyleSearchBits::UserDefined ) );
251 } // namespace
253 ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
255 return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Para, bForceName );
258 ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
260 return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Page, bForceName );
263 // *** byte string import operations *** --------------------------------------
265 OString ScfTools::read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft)
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;
274 void ScfTools::AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc )
276 rString += ::read_zeroTerminated_uInt8s_ToOUString(rStrm, eTextEnc);
279 // *** HTML table names <-> named range names *** -----------------------------
281 const OUString& ScfTools::GetHTMLDocName()
283 static const OUString saHTMLDoc( "HTML_all" );
284 return saHTMLDoc;
287 const OUString& ScfTools::GetHTMLTablesName()
289 static const OUString saHTMLTables( "HTML_tables" );
290 return saHTMLTables;
293 const OUString& ScfTools::GetHTMLIndexPrefix()
295 static const OUString saHTMLIndexPrefix( "HTML_" );
296 return saHTMLIndexPrefix;
300 const OUString& ScfTools::GetHTMLNamePrefix()
302 static const OUString saHTMLNamePrefix( "HTML__" );
303 return saHTMLNamePrefix;
306 OUString ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex )
308 OUString aName = GetHTMLIndexPrefix() +
309 OUString::number( static_cast< sal_Int32 >( nIndex ) );
310 return aName;
313 OUString ScfTools::GetNameFromHTMLName( const OUString& rTabName )
315 return GetHTMLNamePrefix() + rTabName;
318 bool ScfTools::IsHTMLDocName( const OUString& rSource )
320 return rSource.equalsIgnoreAsciiCase( GetHTMLDocName() );
323 bool ScfTools::IsHTMLTablesName( const OUString& rSource )
325 return rSource.equalsIgnoreAsciiCase( GetHTMLTablesName() );
328 bool ScfTools::GetHTMLNameFromName( const OUString& rSource, OUString& rName )
330 rName.clear();
331 if( rSource.startsWithIgnoreAsciiCase( GetHTMLNamePrefix() ) )
333 rName = rSource.copy( GetHTMLNamePrefix().getLength() );
334 ScGlobal::AddQuotes( rName, '"', false );
336 else if( rSource.startsWithIgnoreAsciiCase( GetHTMLIndexPrefix() ) )
338 OUString aIndex( rSource.copy( GetHTMLIndexPrefix().getLength() ) );
339 if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.toInt32() > 0) )
340 rName = aIndex;
342 return !rName.isEmpty();
345 ScFormatFilterPluginImpl::ScFormatFilterPluginImpl() {}
346 ScFormatFilterPluginImpl::~ScFormatFilterPluginImpl() {}
348 ScOrcusFilters* ScFormatFilterPluginImpl::GetOrcusFilters()
350 static ScOrcusFiltersImpl aImpl;
351 return &aImpl;
354 ScFormatFilterPlugin * ScFilterCreate()
356 return new ScFormatFilterPluginImpl();
359 // implementation class inside the filters
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */