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 /* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */
165 void ScfTools::ConvertToScDefinedName( String
& rName
)
167 xub_StrLen nLen
= rName
.Len();
168 if( nLen
&& !ScCompiler::IsCharFlagAllConventions( rName
, 0, SC_COMPILER_C_CHAR_NAME
) )
169 rName
.SetChar( 0, '_' );
170 for( xub_StrLen nPos
= 1; nPos
< nLen
; ++nPos
)
171 if( !ScCompiler::IsCharFlagAllConventions( rName
, nPos
, SC_COMPILER_C_NAME
) )
172 rName
.SetChar( nPos
, '_' );
175 // *** streams and storages *** -----------------------------------------------
177 SotStorageRef
ScfTools::OpenStorageRead( SotStorageRef xStrg
, const String
& rStrgName
)
179 SotStorageRef xSubStrg
;
180 if( xStrg
.Is() && xStrg
->IsContained( rStrgName
) )
181 xSubStrg
= xStrg
->OpenSotStorage( rStrgName
, STREAM_STD_READ
);
185 SotStorageRef
ScfTools::OpenStorageWrite( SotStorageRef xStrg
, const String
& rStrgName
)
187 SotStorageRef xSubStrg
;
189 xSubStrg
= xStrg
->OpenSotStorage( rStrgName
, STREAM_STD_WRITE
);
193 SotStorageStreamRef
ScfTools::OpenStorageStreamRead( SotStorageRef xStrg
, const String
& rStrmName
)
195 SotStorageStreamRef xStrm
;
196 if( xStrg
.Is() && xStrg
->IsContained( rStrmName
) && xStrg
->IsStream( rStrmName
) )
197 xStrm
= xStrg
->OpenSotStream( rStrmName
, STREAM_STD_READ
);
201 SotStorageStreamRef
ScfTools::OpenStorageStreamWrite( SotStorageRef xStrg
, const String
& rStrmName
)
203 DBG_ASSERT( !xStrg
|| !xStrg
->IsContained( rStrmName
), "ScfTools::OpenStorageStreamWrite - stream exists already" );
204 SotStorageStreamRef xStrm
;
206 xStrm
= xStrg
->OpenSotStream( rStrmName
, STREAM_STD_WRITE
| STREAM_TRUNC
);
210 // *** item handling *** ------------------------------------------------------
212 bool ScfTools::CheckItem( const SfxItemSet
& rItemSet
, USHORT nWhichId
, bool bDeep
)
214 return rItemSet
.GetItemState( nWhichId
, bDeep
) == SFX_ITEM_SET
;
217 bool ScfTools::CheckItems( const SfxItemSet
& rItemSet
, const USHORT
* pnWhichIds
, bool bDeep
)
219 DBG_ASSERT( pnWhichIds
, "ScfTools::CheckItems - no which id list" );
220 for( const USHORT
* pnWhichId
= pnWhichIds
; *pnWhichId
!= 0; ++pnWhichId
)
221 if( CheckItem( rItemSet
, *pnWhichId
, bDeep
) )
226 void ScfTools::PutItem( SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
, sal_uInt16 nWhichId
, bool bSkipPoolDef
)
228 if( !bSkipPoolDef
|| (rItem
!= rItemSet
.GetPool()->GetDefaultItem( nWhichId
)) )
229 rItemSet
.Put( rItem
, nWhichId
);
232 void ScfTools::PutItem( SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
, bool bSkipPoolDef
)
234 PutItem( rItemSet
, rItem
, rItem
.Which(), bSkipPoolDef
);
237 // *** style sheet handling *** -----------------------------------------------
241 ScStyleSheet
& lclMakeStyleSheet( ScStyleSheetPool
& rPool
, const String
& rStyleName
, SfxStyleFamily eFamily
, bool bForceName
)
243 // find an unused name
244 String
aNewName( rStyleName
);
245 sal_Int32 nIndex
= 0;
246 SfxStyleSheetBase
* pOldStyleSheet
= 0;
247 while( SfxStyleSheetBase
* pStyleSheet
= rPool
.Find( aNewName
, eFamily
) )
249 if( !pOldStyleSheet
)
250 pOldStyleSheet
= pStyleSheet
;
251 aNewName
.Assign( rStyleName
).Append( ' ' ).Append( String::CreateFromInt32( ++nIndex
) );
254 // rename existing style
255 if( pOldStyleSheet
&& bForceName
)
257 pOldStyleSheet
->SetName( aNewName
);
258 aNewName
= rStyleName
;
261 // create new style sheet
262 return static_cast< ScStyleSheet
& >( rPool
.Make( aNewName
, eFamily
, SFXSTYLEBIT_USERDEF
) );
267 ScStyleSheet
& ScfTools::MakeCellStyleSheet( ScStyleSheetPool
& rPool
, const String
& rStyleName
, bool bForceName
)
269 return lclMakeStyleSheet( rPool
, rStyleName
, SFX_STYLE_FAMILY_PARA
, bForceName
);
272 ScStyleSheet
& ScfTools::MakePageStyleSheet( ScStyleSheetPool
& rPool
, const String
& rStyleName
, bool bForceName
)
274 return lclMakeStyleSheet( rPool
, rStyleName
, SFX_STYLE_FAMILY_PAGE
, bForceName
);
277 // *** byte string import operations *** --------------------------------------
279 ByteString
ScfTools::ReadCString( SvStream
& rStrm
)
293 ByteString
ScfTools::ReadCString( SvStream
& rStrm
, sal_Int32
& rnBytesLeft
)
310 void ScfTools::AppendCString( SvStream
& rStrm
, ByteString
& rString
)
322 void ScfTools::AppendCString( SvStream
& rStrm
, String
& rString
, rtl_TextEncoding eTextEnc
)
324 ByteString aByteString
;
325 AppendCString( rStrm
, aByteString
);
326 rString
+= String( aByteString
, eTextEnc
);
329 // *** HTML table names <-> named range names *** -----------------------------
331 const String
& ScfTools::GetHTMLDocName()
333 static const String
saHTMLDoc( RTL_CONSTASCII_USTRINGPARAM( "HTML_all" ) );
337 const String
& ScfTools::GetHTMLTablesName()
339 static const String
saHTMLTables( RTL_CONSTASCII_USTRINGPARAM( "HTML_tables" ) );
343 const String
& ScfTools::GetHTMLIndexPrefix()
345 static const String
saHTMLIndexPrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML_" ) );
346 return saHTMLIndexPrefix
;
350 const String
& ScfTools::GetHTMLNamePrefix()
352 static const String
saHTMLNamePrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML__" ) );
353 return saHTMLNamePrefix
;
356 String
ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex
)
358 String
aName( GetHTMLIndexPrefix() );
359 aName
+= String::CreateFromInt32( static_cast< sal_Int32
>( nIndex
) );
363 String
ScfTools::GetNameFromHTMLName( const String
& rTabName
)
365 String
aName( GetHTMLNamePrefix() );
370 bool ScfTools::IsHTMLDocName( const String
& rSource
)
372 return rSource
.EqualsIgnoreCaseAscii( GetHTMLDocName() );
375 bool ScfTools::IsHTMLTablesName( const String
& rSource
)
377 return rSource
.EqualsIgnoreCaseAscii( GetHTMLTablesName() );
380 bool ScfTools::GetHTMLNameFromName( const String
& rSource
, String
& rName
)
383 if( rSource
.EqualsIgnoreCaseAscii( GetHTMLNamePrefix(), 0, GetHTMLNamePrefix().Len() ) )
385 rName
= rSource
.Copy( GetHTMLNamePrefix().Len() );
386 ScGlobal::AddQuotes( rName
, '"', false );
388 else if( rSource
.EqualsIgnoreCaseAscii( GetHTMLIndexPrefix(), 0, GetHTMLIndexPrefix().Len() ) )
390 String
aIndex( rSource
.Copy( GetHTMLIndexPrefix().Len() ) );
391 if( CharClass::isAsciiNumeric( aIndex
) && (aIndex
.ToInt32() > 0) )
394 return rName
.Len() > 0;
397 // ============================================================================
399 ScFormatFilterPluginImpl::ScFormatFilterPluginImpl()
403 ScFormatFilterPlugin
* SAL_CALL
ScFilterCreate(void)
405 return new ScFormatFilterPluginImpl();
408 // implementation class inside the filters