merge the formfield patch from ooo-build
[ooovba.git] / sc / source / filter / ftools / ftools.cxx
blob2271a8f35d4d471aa0c1b0f7b79d4359f472cb6b
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 /* 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 );
182 return xSubStrg;
185 SotStorageRef ScfTools::OpenStorageWrite( SotStorageRef xStrg, const String& rStrgName )
187 SotStorageRef xSubStrg;
188 if( xStrg.Is() )
189 xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_WRITE );
190 return xSubStrg;
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 );
198 return xStrm;
201 SotStorageStreamRef ScfTools::OpenStorageStreamWrite( SotStorageRef xStrg, const String& rStrmName )
203 DBG_ASSERT( !xStrg || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
204 SotStorageStreamRef xStrm;
205 if( xStrg.Is() )
206 xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_WRITE | STREAM_TRUNC );
207 return xStrm;
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 ) )
222 return true;
223 return false;
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 *** -----------------------------------------------
239 namespace {
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 ) );
265 } // namespace
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 )
281 ByteString aRet;
282 sal_Char cChar;
284 rStrm >> cChar;
285 while( cChar )
287 aRet += cChar;
288 rStrm >> cChar;
290 return aRet;
293 ByteString ScfTools::ReadCString( SvStream& rStrm, sal_Int32& rnBytesLeft )
295 ByteString aRet;
296 sal_Char cChar;
298 rStrm >> cChar;
299 rnBytesLeft--;
300 while( cChar )
302 aRet += cChar;
303 rStrm >> cChar;
304 rnBytesLeft--;
307 return aRet;
310 void ScfTools::AppendCString( SvStream& rStrm, ByteString& rString )
312 sal_Char cChar;
314 rStrm >> cChar;
315 while( cChar )
317 rString += cChar;
318 rStrm >> cChar;
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" ) );
334 return saHTMLDoc;
337 const String& ScfTools::GetHTMLTablesName()
339 static const String saHTMLTables( RTL_CONSTASCII_USTRINGPARAM( "HTML_tables" ) );
340 return saHTMLTables;
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 ) );
360 return aName;
363 String ScfTools::GetNameFromHTMLName( const String& rTabName )
365 String aName( GetHTMLNamePrefix() );
366 aName += rTabName;
367 return aName;
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 )
382 rName.Erase();
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) )
392 rName = aIndex;
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