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"
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>
43 #include "document.hxx"
44 #include "stlpool.hxx"
45 #include "stlsheet.hxx"
46 #include "compiler.hxx"
50 // ============================================================================
51 // ScFilterTools::ReadLongDouble()
60 double ScfTools::ReadLongDouble( SvStream
& rStrm
)
62 #ifdef __SIMPLE_FUNC // for <=VC 1.5
65 rStrm
.Read( &fRet
, 10 );
66 return static_cast< double >( fRet
);
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
93 lfDouble
+= static_cast< long double >( pDouble10
[ 6 ] ); // Byte 6
95 lfDouble
+= static_cast< long double >( pDouble10
[ 5 ] ); // Byte 5
97 lfDouble
+= static_cast< long double >( pDouble10
[ 4 ] ); // Byte 4
99 lfDouble
+= static_cast< long double >( pDouble10
[ 3 ] ); // Byte 3
101 lfDouble
+= static_cast< long double >( pDouble10
[ 2 ] ); // Byte 2
103 lfDouble
+= static_cast< long double >( pDouble10
[ 1 ] ); // Byte 1
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 )
111 register sal_Int32 nExp
;
112 nExp
= pDouble10
[ 9 ] & 0x7F;
114 nExp
+= pDouble10
[ 8 ];
117 lfDouble
*= pow( 2.0, static_cast< double >( nExp
) );
121 if( pDouble10
[ 9 ] & 0x80 )
122 lfDouble
*= static_cast< long double >( -1.0 );
124 return static_cast< double >( lfDouble
);
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";
140 aStr
+= pHex
[ nValue
>> 12 ];
141 aStr
+= pHex
[ (nValue
>> 8) & 0x000F ];
142 aStr
+= pHex
[ (nValue
>> 4) & 0x000F ];
143 aStr
+= pHex
[ nValue
& 0x000F ];
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
)
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
);
183 SotStorageRef
ScfTools::OpenStorageWrite( SotStorageRef xStrg
, const String
& rStrgName
)
185 SotStorageRef xSubStrg
;
187 xSubStrg
= xStrg
->OpenSotStorage( rStrgName
, STREAM_STD_WRITE
);
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
);
199 SotStorageStreamRef
ScfTools::OpenStorageStreamWrite( SotStorageRef xStrg
, const String
& rStrmName
)
201 DBG_ASSERT( !xStrg
|| !xStrg
->IsContained( rStrmName
), "ScfTools::OpenStorageStreamWrite - stream exists already" );
202 SotStorageStreamRef xStrm
;
204 xStrm
= xStrg
->OpenSotStream( rStrmName
, STREAM_STD_WRITE
| STREAM_TRUNC
);
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
) )
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 *** -----------------------------------------------
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
) );
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
)
291 ByteString
ScfTools::ReadCString( SvStream
& rStrm
, sal_Int32
& rnBytesLeft
)
308 void ScfTools::AppendCString( SvStream
& rStrm
, ByteString
& rString
)
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" ) );
335 const String
& ScfTools::GetHTMLTablesName()
337 static const String
saHTMLTables( RTL_CONSTASCII_USTRINGPARAM( "HTML_tables" ) );
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
) );
361 String
ScfTools::GetNameFromHTMLName( const String
& rTabName
)
363 String
aName( GetHTMLNamePrefix() );
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
)
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) )
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