xref: /trunk/main/sc/source/filter/ftools/ftools.cxx (revision b77af630)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_scfilt.hxx"
26 #include "ftools.hxx"
27 #include <tools/color.hxx>
28 #include <unotools/charclass.hxx>
29 #include <svl/itempool.hxx>
30 #include <svl/itemset.hxx>
31 #include <svl/poolitem.hxx>
32 #include <sot/storage.hxx>
33 
34 #include <math.h>
35 #include "global.hxx"
36 #include "document.hxx"
37 #include "stlpool.hxx"
38 #include "stlsheet.hxx"
39 #include "compiler.hxx"
40 
41 #include <stdio.h>
42 
43 // ============================================================================
44 // ScFilterTools::ReadLongDouble()
45 
46 #ifdef _MSC_VER
47 #if _MSC_VER <= 800
48 #undef __SIMPLE_FUNC
49 #define __SIMPLE_FUNC
50 #endif
51 #endif
52 
ReadLongDouble(SvStream & rStrm)53 double ScfTools::ReadLongDouble( SvStream& rStrm )
54 
55 #ifdef __SIMPLE_FUNC                // for <=VC 1.5
56 {
57     long double fRet;
58     rStrm.Read( &fRet, 10 );
59     return static_cast< double >( fRet );
60 }
61 #undef __SIMPLE_FUNC
62 
63 #else                               // detailed for all others
64 {
65 
66 /*
67 " M a p p i n g - G u i d e " 10-Byte Intel
68 
69 77777777 77666666 66665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000   x10
70 98765432 10987654 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210   Bit-# total
71 9      9 8      8 7      7 6      6 5      5 4      4 3      3 2      2 1      1 0      0   Byte-#
72 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210   Bit-# in Byte
73 SEEEEEEE EEEEEEEE IMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM   Group
74 01111110 00000000 06665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000       x10
75 14321098 76543210 02109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210   Bit in Group
76 */
77 
78     register long double lfDouble = 0.0;
79     register long double lfFakt = 256.0;
80     sal_uInt8 pDouble10[ 10 ];
81 
82     rStrm.Read( pDouble10, 10 );            // Intel-10 in pDouble10
83 
84     lfDouble  = static_cast< long double >( pDouble10[ 7 ] );   // Byte 7
85     lfDouble *= lfFakt;
86     lfDouble += static_cast< long double >( pDouble10[ 6 ] );   // Byte 6
87     lfDouble *= lfFakt;
88     lfDouble += static_cast< long double >( pDouble10[ 5 ] );   // Byte 5
89     lfDouble *= lfFakt;
90     lfDouble += static_cast< long double >( pDouble10[ 4 ] );   // Byte 4
91     lfDouble *= lfFakt;
92     lfDouble += static_cast< long double >( pDouble10[ 3 ] );   // Byte 3
93     lfDouble *= lfFakt;
94     lfDouble += static_cast< long double >( pDouble10[ 2 ] );   // Byte 2
95     lfDouble *= lfFakt;
96     lfDouble += static_cast< long double >( pDouble10[ 1 ] );   // Byte 1
97     lfDouble *= lfFakt;
98     lfDouble += static_cast< long double >( pDouble10[ 0 ] );   // Byte 0
99 
100     //  For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers
101     if( lfDouble != 0.0 )
102     {
103         // exponent
104         register sal_Int32 nExp;
105         nExp = pDouble10[ 9 ] & 0x7F;
106         nExp <<= 8;
107         nExp += pDouble10[ 8 ];
108         nExp -= 16446;
109 
110         lfDouble *= pow( 2.0, static_cast< double >( nExp ) );
111     }
112 
113     // sign
114     if( pDouble10[ 9 ] & 0x80 )
115         lfDouble *= static_cast< long double >( -1.0 );
116 
117     return static_cast< double >( lfDouble );
118 }
119 #endif
120 
121 // *** common methods *** -----------------------------------------------------
122 
GetSystemTextEncoding()123 rtl_TextEncoding ScfTools::GetSystemTextEncoding()
124 {
125     return gsl_getSystemTextEncoding();
126 }
127 
GetHexStr(sal_uInt16 nValue)128 String ScfTools::GetHexStr( sal_uInt16 nValue )
129 {
130     const sal_Char pHex[] = "0123456789ABCDEF";
131     String aStr;
132 
133     aStr += pHex[ nValue >> 12 ];
134     aStr += pHex[ (nValue >> 8) & 0x000F ];
135     aStr += pHex[ (nValue >> 4) & 0x000F ];
136     aStr += pHex[ nValue & 0x000F ];
137     return aStr;
138 }
139 
GetMixedColorComp(sal_uInt8 nFore,sal_uInt8 nBack,sal_uInt8 nTrans)140 sal_uInt8 ScfTools::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
141 {
142     sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
143     return static_cast< sal_uInt8 >( nTemp );
144 }
145 
GetMixedColor(const Color & rFore,const Color & rBack,sal_uInt8 nTrans)146 Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
147 {
148     return Color(
149         GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
150         GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
151         GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) );
152 }
153 
154 // *** conversion of names *** ------------------------------------------------
155 
156 /* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */
157 
ConvertToScDefinedName(String & rName)158 void ScfTools::ConvertToScDefinedName( String& rName )
159 {
160     xub_StrLen nLen = rName.Len();
161     if( nLen && !ScCompiler::IsCharFlagAllConventions( rName, 0, SC_COMPILER_C_CHAR_NAME ) )
162         rName.SetChar( 0, '_' );
163     for( xub_StrLen nPos = 1; nPos < nLen; ++nPos )
164         if( !ScCompiler::IsCharFlagAllConventions( rName, nPos, SC_COMPILER_C_NAME ) )
165             rName.SetChar( nPos, '_' );
166 }
167 
168 // *** streams and storages *** -----------------------------------------------
169 
OpenStorageRead(SotStorageRef xStrg,const String & rStrgName)170 SotStorageRef ScfTools::OpenStorageRead( SotStorageRef xStrg, const String& rStrgName )
171 {
172     SotStorageRef xSubStrg;
173     if( xStrg.Is() && xStrg->IsContained( rStrgName ) )
174         xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_READ );
175     return xSubStrg;
176 }
177 
OpenStorageWrite(SotStorageRef xStrg,const String & rStrgName)178 SotStorageRef ScfTools::OpenStorageWrite( SotStorageRef xStrg, const String& rStrgName )
179 {
180     SotStorageRef xSubStrg;
181     if( xStrg.Is() )
182         xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_WRITE );
183     return xSubStrg;
184 }
185 
OpenStorageStreamRead(SotStorageRef xStrg,const String & rStrmName)186 SotStorageStreamRef ScfTools::OpenStorageStreamRead( SotStorageRef xStrg, const String& rStrmName )
187 {
188     SotStorageStreamRef xStrm;
189     if( xStrg.Is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) )
190         xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_READ );
191     return xStrm;
192 }
193 
OpenStorageStreamWrite(SotStorageRef xStrg,const String & rStrmName)194 SotStorageStreamRef ScfTools::OpenStorageStreamWrite( SotStorageRef xStrg, const String& rStrmName )
195 {
196     DBG_ASSERT( !xStrg || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
197     SotStorageStreamRef xStrm;
198     if( xStrg.Is() )
199         xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_WRITE | STREAM_TRUNC );
200     return xStrm;
201 }
202 
203 // *** item handling *** ------------------------------------------------------
204 
CheckItem(const SfxItemSet & rItemSet,sal_uInt16 nWhichId,bool bDeep)205 bool ScfTools::CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep )
206 {
207     return rItemSet.GetItemState( nWhichId, bDeep ) == SFX_ITEM_SET;
208 }
209 
CheckItems(const SfxItemSet & rItemSet,const sal_uInt16 * pnWhichIds,bool bDeep)210 bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep )
211 {
212     DBG_ASSERT( pnWhichIds, "ScfTools::CheckItems - no which id list" );
213     for( const sal_uInt16* pnWhichId = pnWhichIds; *pnWhichId != 0; ++pnWhichId )
214         if( CheckItem( rItemSet, *pnWhichId, bDeep ) )
215             return true;
216     return false;
217 }
218 
PutItem(SfxItemSet & rItemSet,const SfxPoolItem & rItem,sal_uInt16 nWhichId,bool bSkipPoolDef)219 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef )
220 {
221     if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) )
222         rItemSet.Put( rItem, nWhichId );
223 }
224 
PutItem(SfxItemSet & rItemSet,const SfxPoolItem & rItem,bool bSkipPoolDef)225 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef )
226 {
227     PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef );
228 }
229 
230 // *** style sheet handling *** -----------------------------------------------
231 
232 namespace {
233 
lclMakeStyleSheet(ScStyleSheetPool & rPool,const String & rStyleName,SfxStyleFamily eFamily,bool bForceName)234 ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, SfxStyleFamily eFamily, bool bForceName )
235 {
236     // find an unused name
237     String aNewName( rStyleName );
238     sal_Int32 nIndex = 0;
239     SfxStyleSheetBase* pOldStyleSheet = 0;
240     while( SfxStyleSheetBase* pStyleSheet = rPool.Find( aNewName, eFamily ) )
241     {
242         if( !pOldStyleSheet )
243             pOldStyleSheet = pStyleSheet;
244         aNewName.Assign( rStyleName ).Append( ' ' ).Append( String::CreateFromInt32( ++nIndex ) );
245     }
246 
247     // rename existing style
248     if( pOldStyleSheet && bForceName )
249     {
250         pOldStyleSheet->SetName( aNewName );
251         aNewName = rStyleName;
252     }
253 
254     // create new style sheet
255     return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SFXSTYLEBIT_USERDEF ) );
256 }
257 
258 } // namespace
259 
MakeCellStyleSheet(ScStyleSheetPool & rPool,const String & rStyleName,bool bForceName)260 ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, bool bForceName )
261 {
262     return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PARA, bForceName );
263 }
264 
MakePageStyleSheet(ScStyleSheetPool & rPool,const String & rStyleName,bool bForceName)265 ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const String& rStyleName, bool bForceName )
266 {
267     return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PAGE, bForceName );
268 }
269 
270 // *** byte string import operations *** --------------------------------------
271 
ReadCString(SvStream & rStrm)272 ByteString ScfTools::ReadCString( SvStream& rStrm )
273 {
274     ByteString aRet;
275     sal_Char cChar;
276 
277     sal_uInt32 nLen = 0;
278     rStrm >> cChar;
279     while( cChar && nLen++ < STRING_MAXLEN )
280     {
281         aRet += cChar;
282         rStrm >> cChar;
283     }
284     // Callers assume that a 0-byte was read and may advance their book keeping
285     // counters by String.Len()+1, don't put back cChar!=0 if STRING_MAXLEN was
286     // reached.
287     return aRet;
288 }
289 
ReadCString(SvStream & rStrm,sal_Int32 & rnBytesLeft)290 ByteString ScfTools::ReadCString( SvStream& rStrm, sal_Int32& rnBytesLeft )
291 {
292     ByteString aRet;
293     sal_Char cChar;
294 
295     sal_uInt32 nLen = 0;
296     rStrm >> cChar;
297     rnBytesLeft--;
298     while( cChar && nLen++ < STRING_MAXLEN )
299     {
300         aRet += cChar;
301         rStrm >> cChar;
302         rnBytesLeft--;
303     }
304 
305     return aRet;
306 }
307 
AppendCStringWithLen(SvStream & rStrm,ByteString & rString,sal_uInt32 nLen)308 void ScfTools::AppendCStringWithLen( SvStream& rStrm, ByteString& rString, sal_uInt32 nLen )
309 {
310     sal_Char cChar;
311 
312     rStrm >> cChar;
313     while( cChar && nLen++ < STRING_MAXLEN )
314     {
315         rString += cChar;
316         rStrm >> cChar;
317     }
318 }
319 
AppendCString(SvStream & rStrm,String & rString,rtl_TextEncoding eTextEnc)320 void ScfTools::AppendCString( SvStream& rStrm, String& rString, rtl_TextEncoding eTextEnc )
321 {
322     ByteString aByteString;
323     AppendCStringWithLen( rStrm, aByteString, rString.Len() );
324     rString += String( aByteString, eTextEnc );
325 }
326 
327 // *** HTML table names <-> named range names *** -----------------------------
328 
GetHTMLDocName()329 const String& ScfTools::GetHTMLDocName()
330 {
331     static const String saHTMLDoc( RTL_CONSTASCII_USTRINGPARAM( "HTML_all" ) );
332     return saHTMLDoc;
333 }
334 
GetHTMLTablesName()335 const String& ScfTools::GetHTMLTablesName()
336 {
337     static const String saHTMLTables( RTL_CONSTASCII_USTRINGPARAM( "HTML_tables" ) );
338     return saHTMLTables;
339 }
340 
GetHTMLIndexPrefix()341 const String& ScfTools::GetHTMLIndexPrefix()
342 {
343     static const String saHTMLIndexPrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML_" ) );
344     return saHTMLIndexPrefix;
345 
346 }
347 
GetHTMLNamePrefix()348 const String& ScfTools::GetHTMLNamePrefix()
349 {
350     static const String saHTMLNamePrefix( RTL_CONSTASCII_USTRINGPARAM( "HTML__" ) );
351     return saHTMLNamePrefix;
352 }
353 
GetNameFromHTMLIndex(sal_uInt32 nIndex)354 String ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex )
355 {
356     String aName( GetHTMLIndexPrefix() );
357     aName += String::CreateFromInt32( static_cast< sal_Int32 >( nIndex ) );
358     return aName;
359 }
360 
GetNameFromHTMLName(const String & rTabName)361 String ScfTools::GetNameFromHTMLName( const String& rTabName )
362 {
363     String aName( GetHTMLNamePrefix() );
364     aName += rTabName;
365     return aName;
366 }
367 
IsHTMLDocName(const String & rSource)368 bool ScfTools::IsHTMLDocName( const String& rSource )
369 {
370     return rSource.EqualsIgnoreCaseAscii( GetHTMLDocName() );
371 }
372 
IsHTMLTablesName(const String & rSource)373 bool ScfTools::IsHTMLTablesName( const String& rSource )
374 {
375     return rSource.EqualsIgnoreCaseAscii( GetHTMLTablesName() );
376 }
377 
GetHTMLNameFromName(const String & rSource,String & rName)378 bool ScfTools::GetHTMLNameFromName( const String& rSource, String& rName )
379 {
380     rName.Erase();
381     if( rSource.EqualsIgnoreCaseAscii( GetHTMLNamePrefix(), 0, GetHTMLNamePrefix().Len() ) )
382     {
383         rName = rSource.Copy( GetHTMLNamePrefix().Len() );
384         ScGlobal::AddQuotes( rName, '"', false );
385     }
386     else if( rSource.EqualsIgnoreCaseAscii( GetHTMLIndexPrefix(), 0, GetHTMLIndexPrefix().Len() ) )
387     {
388         String aIndex( rSource.Copy( GetHTMLIndexPrefix().Len() ) );
389         if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.ToInt32() > 0) )
390             rName = aIndex;
391     }
392     return rName.Len() > 0;
393 }
394 
395 // ============================================================================
396 
ScFormatFilterPluginImpl()397 ScFormatFilterPluginImpl::ScFormatFilterPluginImpl()
398 {
399 }
400 
ScFilterCreate(void)401 SAL_DLLPUBLIC_EXPORT ScFormatFilterPlugin * SAL_CALL ScFilterCreate(void)
402 {
403     return new ScFormatFilterPluginImpl();
404 }
405 
406 // implementation class inside the filters
407 
408