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