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