update dev300-m58
[ooovba.git] / sc / source / filter / ftools / ftools.cxx
blob98faeb9f03d4641ad6e700f41da1403ff1a698c1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ftools.cxx,v $
10 * $Revision: 1.20.4.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 #include "ftools.hxx"
34 #include <tools/color.hxx>
35 #include <unotools/charclass.hxx>
36 #include <svtools/itempool.hxx>
37 #include <svtools/itemset.hxx>
38 #include <svtools/poolitem.hxx>
39 #include <sot/storage.hxx>
41 #include <math.h>
42 #include "global.hxx"
43 #include "document.hxx"
44 #include "stlpool.hxx"
45 #include "stlsheet.hxx"
46 #include "compiler.hxx"
48 #include <stdio.h>
50 // ============================================================================
51 // ScFilterTools::ReadLongDouble()
53 #ifdef _MSC_VER
54 #if _MSC_VER <= 800
55 #undef __SIMPLE_FUNC
56 #define __SIMPLE_FUNC
57 #endif
58 #endif
60 double ScfTools::ReadLongDouble( SvStream& rStrm )
62 #ifdef __SIMPLE_FUNC // for <=VC 1.5
64 long double fRet;
65 rStrm.Read( &fRet, 10 );
66 return static_cast< double >( fRet );
68 #undef __SIMPLE_FUNC
70 #else // detailed for all others
74 " M a p p i n g - G u i d e " 10-Byte Intel
76 77777777 77666666 66665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
77 98765432 10987654 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit-# total
78 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 Byte-#
79 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 Bit-# in Byte
80 SEEEEEEE EEEEEEEE IMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM Group
81 01111110 00000000 06665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
82 14321098 76543210 02109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit in Group
85 register long double lfDouble = 0.0;
86 register long double lfFakt = 256.0;
87 sal_uInt8 pDouble10[ 10 ];
89 rStrm.Read( pDouble10, 10 ); // Intel-10 in pDouble10
91 lfDouble = static_cast< long double >( pDouble10[ 7 ] ); // Byte 7
92 lfDouble *= lfFakt;
93 lfDouble += static_cast< long double >( pDouble10[ 6 ] ); // Byte 6
94 lfDouble *= lfFakt;
95 lfDouble += static_cast< long double >( pDouble10[ 5 ] ); // Byte 5
96 lfDouble *= lfFakt;
97 lfDouble += static_cast< long double >( pDouble10[ 4 ] ); // Byte 4
98 lfDouble *= lfFakt;
99 lfDouble += static_cast< long double >( pDouble10[ 3 ] ); // Byte 3
100 lfDouble *= lfFakt;
101 lfDouble += static_cast< long double >( pDouble10[ 2 ] ); // Byte 2
102 lfDouble *= lfFakt;
103 lfDouble += static_cast< long double >( pDouble10[ 1 ] ); // Byte 1
104 lfDouble *= lfFakt;
105 lfDouble += static_cast< long double >( pDouble10[ 0 ] ); // Byte 0
107 // For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers
108 if( lfDouble != 0.0 )
110 // exponent
111 register sal_Int32 nExp;
112 nExp = pDouble10[ 9 ] & 0x7F;
113 nExp <<= 8;
114 nExp += pDouble10[ 8 ];
115 nExp -= 16446;
117 lfDouble *= pow( 2.0, static_cast< double >( nExp ) );
120 // sign
121 if( pDouble10[ 9 ] & 0x80 )
122 lfDouble *= static_cast< long double >( -1.0 );
124 return static_cast< double >( lfDouble );
126 #endif
128 // *** common methods *** -----------------------------------------------------
130 rtl_TextEncoding ScfTools::GetSystemTextEncoding()
132 return gsl_getSystemTextEncoding();
135 String ScfTools::GetHexStr( sal_uInt16 nValue )
137 const sal_Char pHex[] = "0123456789ABCDEF";
138 String aStr;
140 aStr += pHex[ nValue >> 12 ];
141 aStr += pHex[ (nValue >> 8) & 0x000F ];
142 aStr += pHex[ (nValue >> 4) & 0x000F ];
143 aStr += pHex[ nValue & 0x000F ];
144 return aStr;
147 sal_uInt8 ScfTools::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
149 sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
150 return static_cast< sal_uInt8 >( nTemp );
153 Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
155 return Color(
156 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
157 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
158 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) );
161 // *** conversion of names *** ------------------------------------------------
163 void ScfTools::ConvertToScDefinedName( String& rName )
165 xub_StrLen nLen = rName.Len();
166 if( nLen && !ScCompiler::IsCharWordChar( rName, 0 ) )
167 rName.SetChar( 0, '_' );
168 for( xub_StrLen nPos = 1; nPos < nLen; ++nPos )
169 if( !ScCompiler::IsWordChar( rName, nPos ) )
170 rName.SetChar( nPos, '_' );
173 // *** streams and storages *** -----------------------------------------------
175 SotStorageRef ScfTools::OpenStorageRead( SotStorageRef xStrg, const String& rStrgName )
177 SotStorageRef xSubStrg;
178 if( xStrg.Is() && xStrg->IsContained( rStrgName ) )
179 xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_READ );
180 return xSubStrg;
183 SotStorageRef ScfTools::OpenStorageWrite( SotStorageRef xStrg, const String& rStrgName )
185 SotStorageRef xSubStrg;
186 if( xStrg.Is() )
187 xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_WRITE );
188 return xSubStrg;
191 SotStorageStreamRef ScfTools::OpenStorageStreamRead( SotStorageRef xStrg, const String& rStrmName )
193 SotStorageStreamRef xStrm;
194 if( xStrg.Is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) )
195 xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_READ );
196 return xStrm;
199 SotStorageStreamRef ScfTools::OpenStorageStreamWrite( SotStorageRef xStrg, const String& rStrmName )
201 DBG_ASSERT( !xStrg || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
202 SotStorageStreamRef xStrm;
203 if( xStrg.Is() )
204 xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_WRITE | STREAM_TRUNC );
205 return xStrm;
208 // *** item handling *** ------------------------------------------------------
210 bool ScfTools::CheckItem( const SfxItemSet& rItemSet, USHORT nWhichId, bool bDeep )
212 return rItemSet.GetItemState( nWhichId, bDeep ) == SFX_ITEM_SET;
215 bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const USHORT* pnWhichIds, bool bDeep )
217 DBG_ASSERT( pnWhichIds, "ScfTools::CheckItems - no which id list" );
218 for( const USHORT* pnWhichId = pnWhichIds; *pnWhichId != 0; ++pnWhichId )
219 if( CheckItem( rItemSet, *pnWhichId, bDeep ) )
220 return true;
221 return false;
224 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef )
226 if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) )
227 rItemSet.Put( rItem, nWhichId );
230 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef )
232 PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef );
235 // *** style sheet handling *** -----------------------------------------------
237 namespace {
239 ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, SfxStyleFamily eFamily, bool bForceName )
241 // find an unused name
242 String aNewName( rStyleName );
243 sal_Int32 nIndex = 0;
244 SfxStyleSheetBase* pOldStyleSheet = 0;
245 while( SfxStyleSheetBase* pStyleSheet = rPool.Find( aNewName, eFamily ) )
247 if( !pOldStyleSheet )
248 pOldStyleSheet = pStyleSheet;
249 aNewName.Assign( rStyleName ).Append( ' ' ).Append( String::CreateFromInt32( ++nIndex ) );
252 // rename existing style
253 if( pOldStyleSheet && bForceName )
255 pOldStyleSheet->SetName( aNewName );
256 aNewName = rStyleName;
259 // create new style sheet
260 return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SFXSTYLEBIT_USERDEF ) );
263 } // namespace
265 ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, bool bForceName )
267 return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PARA, bForceName );
270 ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, bool bForceName )
272 return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PAGE, bForceName );
275 // *** byte string import operations *** --------------------------------------
277 ByteString ScfTools::ReadCString( SvStream& rStrm )
279 ByteString aRet;
280 sal_Char cChar;
282 rStrm >> cChar;
283 while( cChar )
285 aRet += cChar;
286 rStrm >> cChar;
288 return aRet;
291 ByteString ScfTools::ReadCString( SvStream& rStrm, sal_Int32& rnBytesLeft )
293 ByteString aRet;
294 sal_Char cChar;
296 rStrm >> cChar;
297 rnBytesLeft--;
298 while( cChar )
300 aRet += cChar;
301 rStrm >> cChar;
302 rnBytesLeft--;
305 return aRet;
308 void ScfTools::AppendCString( SvStream& rStrm, ByteString& rString )
310 sal_Char cChar;
312 rStrm >> cChar;
313 while( cChar )
315 rString += cChar;
316 rStrm >> cChar;
320 void ScfTools::AppendCString( SvStream& rStrm, String& rString, rtl_TextEncoding eTextEnc )
322 ByteString aByteString;
323 AppendCString( rStrm, aByteString );
324 rString += String( aByteString, eTextEnc );
327 // *** HTML table names <-> named range names *** -----------------------------
329 const String& ScfTools::GetHTMLDocName()
331 static const String saHTMLDoc( RTL_CONSTASCII_USTRINGPARAM( "HTML_all" ) );
332 return saHTMLDoc;
335 const String& ScfTools::GetHTMLTablesName()
337 static const String saHTMLTables( RTL_CONSTASCII_USTRINGPARAM( "HTML_tables" ) );
338 return saHTMLTables;
341 const String& ScfTools::GetHTMLIndexPrefix()
343 static const String saHTMLIndexPrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML_" ) );
344 return saHTMLIndexPrefix;
348 const String& ScfTools::GetHTMLNamePrefix()
350 static const String saHTMLNamePrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML__" ) );
351 return saHTMLNamePrefix;
354 String ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex )
356 String aName( GetHTMLIndexPrefix() );
357 aName += String::CreateFromInt32( static_cast< sal_Int32 >( nIndex ) );
358 return aName;
361 String ScfTools::GetNameFromHTMLName( const String& rTabName )
363 String aName( GetHTMLNamePrefix() );
364 aName += rTabName;
365 return aName;
368 bool ScfTools::IsHTMLDocName( const String& rSource )
370 return rSource.EqualsIgnoreCaseAscii( GetHTMLDocName() );
373 bool ScfTools::IsHTMLTablesName( const String& rSource )
375 return rSource.EqualsIgnoreCaseAscii( GetHTMLTablesName() );
378 bool ScfTools::GetHTMLNameFromName( const String& rSource, String& rName )
380 rName.Erase();
381 if( rSource.EqualsIgnoreCaseAscii( GetHTMLNamePrefix(), 0, GetHTMLNamePrefix().Len() ) )
383 rName = rSource.Copy( GetHTMLNamePrefix().Len() );
384 ScGlobal::AddQuotes( rName, '"', false );
386 else if( rSource.EqualsIgnoreCaseAscii( GetHTMLIndexPrefix(), 0, GetHTMLIndexPrefix().Len() ) )
388 String aIndex( rSource.Copy( GetHTMLIndexPrefix().Len() ) );
389 if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.ToInt32() > 0) )
390 rName = aIndex;
392 return rName.Len() > 0;
395 // ============================================================================
397 ScFormatFilterPluginImpl::ScFormatFilterPluginImpl()
401 ScFormatFilterPlugin * SAL_CALL ScFilterCreate(void)
403 return new ScFormatFilterPluginImpl();
406 // implementation class inside the filters