LCOV - code coverage report
Current view: top level - sc/source/filter/excel - xltools.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 204 308 66.2 %
Date: 2015-06-13 12:38:46 Functions: 42 49 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <algorithm>
      21             : #include <math.h>
      22             : #include <sal/mathconf.h>
      23             : #include <unotools/fontcvt.hxx>
      24             : #include <sfx2/objsh.hxx>
      25             : #include <sal/macros.h>
      26             : #include <editeng/editstat.hxx>
      27             : #include <filter/msfilter/msvbahelper.hxx>
      28             : #include "xestream.hxx"
      29             : #include "document.hxx"
      30             : #include "docuno.hxx"
      31             : #include "editutil.hxx"
      32             : #include <formula/errorcodes.hxx>
      33             : #include "globstr.hrc"
      34             : #include "xlstyle.hxx"
      35             : #include "xlname.hxx"
      36             : #include "xistream.hxx"
      37             : #include "xiroot.hxx"
      38             : #include "xltools.hxx"
      39             : 
      40             : // GUID import/export
      41             : 
      42           8 : XclGuid::XclGuid()
      43             : {
      44           8 :     ::std::fill( mpnData, STATIC_ARRAY_END( mpnData ), 0 );
      45           8 : }
      46             : 
      47          30 : XclGuid::XclGuid(
      48             :         sal_uInt32 nData1, sal_uInt16 nData2, sal_uInt16 nData3,
      49             :         sal_uInt8 nData41, sal_uInt8 nData42, sal_uInt8 nData43, sal_uInt8 nData44,
      50             :         sal_uInt8 nData45, sal_uInt8 nData46, sal_uInt8 nData47, sal_uInt8 nData48 )
      51             : {
      52             :     // convert to little endian -> makes streaming easy
      53          30 :     UInt32ToSVBT32( nData1, mpnData );
      54          30 :     ShortToSVBT16( nData2, mpnData + 4 );
      55          30 :     ShortToSVBT16( nData3, mpnData + 6 );
      56          30 :     mpnData[  8 ] = nData41;
      57          30 :     mpnData[  9 ] = nData42;
      58          30 :     mpnData[ 10 ] = nData43;
      59          30 :     mpnData[ 11 ] = nData44;
      60          30 :     mpnData[ 12 ] = nData45;
      61          30 :     mpnData[ 13 ] = nData46;
      62          30 :     mpnData[ 14 ] = nData47;
      63          30 :     mpnData[ 15 ] = nData48;
      64          30 : }
      65             : 
      66          16 : bool operator==( const XclGuid& rCmp1, const XclGuid& rCmp2 )
      67             : {
      68          16 :     return ::std::equal( rCmp1.mpnData, STATIC_ARRAY_END( rCmp1.mpnData ), rCmp2.mpnData );
      69             : }
      70             : 
      71           0 : bool operator<( const XclGuid& rCmp1, const XclGuid& rCmp2 )
      72             : {
      73             :     return ::std::lexicographical_compare(
      74             :         rCmp1.mpnData, STATIC_ARRAY_END( rCmp1.mpnData ),
      75           0 :         rCmp2.mpnData, STATIC_ARRAY_END( rCmp2.mpnData ) );
      76             : }
      77             : 
      78          16 : XclImpStream& operator>>( XclImpStream& rStrm, XclGuid& rGuid )
      79             : {
      80          16 :     rStrm.Read( rGuid.mpnData, 16 );     // mpnData always in little endian
      81          16 :     return rStrm;
      82             : }
      83             : 
      84           5 : XclExpStream& operator<<( XclExpStream& rStrm, const XclGuid& rGuid )
      85             : {
      86           5 :     rStrm.Write( rGuid.mpnData, 16 );    // mpnData already in little endian
      87           5 :     return rStrm;
      88             : }
      89             : 
      90             : // Excel Tools
      91             : 
      92             : // GUID's
      93          10 : const XclGuid XclTools::maGuidStdLink(
      94             :     0x79EAC9D0, 0xBAF9, 0x11CE, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B );
      95             : 
      96          10 : const XclGuid XclTools::maGuidUrlMoniker(
      97             :     0x79EAC9E0, 0xBAF9, 0x11CE, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B );
      98             : 
      99          10 : const XclGuid XclTools::maGuidFileMoniker(
     100             :     0x00000303, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 );
     101             : 
     102             : // numeric conversion
     103             : 
     104       15655 : double XclTools::GetDoubleFromRK( sal_Int32 nRKValue )
     105             : {
     106             :     union
     107             :     {
     108             :         double fVal;
     109             :         sal_math_Double smD;
     110             :     };
     111       15655 :     fVal = 0.0;
     112             : 
     113       15655 :     if( ::get_flag( nRKValue, EXC_RK_INTFLAG ) )
     114             :     {
     115        1877 :         sal_Int32 nTemp = nRKValue >> 2;
     116        1877 :         ::set_flag< sal_Int32 >( nTemp, 0xE0000000, nRKValue < 0 );
     117        1877 :         fVal = nTemp;
     118             :     }
     119             :     else
     120             :     {
     121       13778 :         smD.w32_parts.msw = nRKValue & EXC_RK_VALUEMASK;
     122             :     }
     123             : 
     124       15655 :     if( ::get_flag( nRKValue, EXC_RK_100FLAG ) )
     125        1535 :         fVal /= 100.0;
     126             : 
     127       15655 :     return fVal;
     128             : }
     129             : 
     130        1241 : bool XclTools::GetRKFromDouble( sal_Int32& rnRKValue, double fValue )
     131             : {
     132             :     double fFrac, fInt;
     133             : 
     134             :     // integer
     135        1241 :     fFrac = modf( fValue, &fInt );
     136        1241 :     if( (fFrac == 0.0) && (fInt >= -536870912.0) && (fInt <= 536870911.0) ) // 2^29
     137             :     {
     138             :         rnRKValue
     139             :             = static_cast<sal_Int32>(
     140         946 :                 static_cast<sal_uInt32>(static_cast<sal_Int32>(fInt)) << 2)
     141         946 :             | EXC_RK_INT;
     142         946 :         return true;
     143             :     }
     144             : 
     145             :     // integer/100
     146         295 :     fFrac = modf( fValue * 100.0, &fInt );
     147         295 :     if( (fFrac == 0.0) && (fInt >= -536870912.0) && (fInt <= 536870911.0) )
     148             :     {
     149             :         rnRKValue
     150             :             = static_cast<sal_Int32>(
     151          60 :                 static_cast<sal_uInt32>(static_cast<sal_Int32>(fInt)) << 2)
     152          60 :             | EXC_RK_INT100;
     153          60 :         return true;
     154             :     }
     155             : 
     156             :     // double
     157         235 :     return false;
     158             : }
     159             : 
     160        1107 : sal_Int32 XclTools::GetScRotation( sal_uInt16 nXclRot, sal_Int32 nRotForStacked )
     161             : {
     162        1107 :     if( nXclRot == EXC_ROT_STACKED )
     163           7 :         return nRotForStacked;
     164             :     OSL_ENSURE( nXclRot <= 180, "XclTools::GetScRotation - illegal rotation angle" );
     165        1100 :     return static_cast< sal_Int32 >( (nXclRot <= 180) ? (100 * ((nXclRot > 90) ? (450 - nXclRot) : nXclRot)) : 0 );
     166             : }
     167             : 
     168         382 : sal_uInt8 XclTools::GetXclRotation( sal_Int32 nScRot )
     169             : {
     170         382 :     sal_Int32 nXclRot = nScRot / 100;
     171         382 :     if( (0 <= nXclRot) && (nXclRot <= 90) )
     172         381 :         return static_cast< sal_uInt8 >( nXclRot );
     173           1 :     if( nXclRot < 180 )
     174           0 :         return static_cast< sal_uInt8 >( 270 - nXclRot );
     175           1 :     if( nXclRot < 270 )
     176           0 :         return static_cast< sal_uInt8 >( nXclRot - 180 );
     177           1 :     if( nXclRot < 360 )
     178           1 :         return static_cast< sal_uInt8 >( 450 - nXclRot );
     179           0 :     return 0;
     180             : }
     181             : 
     182           4 : sal_uInt8 XclTools::GetXclRotFromOrient( sal_uInt8 nXclOrient )
     183             : {
     184           4 :     switch( nXclOrient )
     185             :     {
     186           4 :         case EXC_ORIENT_NONE:       return EXC_ROT_NONE;
     187           0 :         case EXC_ORIENT_STACKED:    return EXC_ROT_STACKED;
     188           0 :         case EXC_ORIENT_90CCW:      return EXC_ROT_90CCW;
     189           0 :         case EXC_ORIENT_90CW:       return EXC_ROT_90CW;
     190             :         default:    OSL_FAIL( "XclTools::GetXclRotFromOrient - unknown text orientation" );
     191             :     }
     192           0 :     return EXC_ROT_NONE;
     193             : }
     194             : 
     195         382 : sal_uInt8 XclTools::GetXclOrientFromRot( sal_uInt16 nXclRot )
     196             : {
     197         382 :     if( nXclRot == EXC_ROT_STACKED )
     198           0 :         return EXC_ORIENT_STACKED;
     199             :     OSL_ENSURE( nXclRot <= 180, "XclTools::GetXclOrientFromRot - unknown text rotation" );
     200         382 :     if( (45 < nXclRot) && (nXclRot <= 90) )
     201           1 :         return EXC_ORIENT_90CCW;
     202         381 :     if( (135 < nXclRot) && (nXclRot <= 180) )
     203           0 :         return EXC_ORIENT_90CW;
     204         381 :     return EXC_ORIENT_NONE;
     205             : }
     206             : 
     207           2 : sal_uInt8 XclTools::GetXclErrorCode( sal_uInt16 nScError )
     208             : {
     209             :     using namespace ScErrorCodes;
     210           2 :     switch( nScError )
     211             :     {
     212           0 :         case errIllegalArgument:        return EXC_ERR_VALUE;
     213           0 :         case errIllegalFPOperation:     return EXC_ERR_NUM;     // maybe DIV/0 or NUM...
     214           2 :         case errDivisionByZero:         return EXC_ERR_DIV0;
     215           0 :         case errIllegalParameter:       return EXC_ERR_VALUE;
     216           0 :         case errPairExpected:           return EXC_ERR_VALUE;
     217           0 :         case errOperatorExpected:       return EXC_ERR_VALUE;
     218           0 :         case errVariableExpected:       return EXC_ERR_VALUE;
     219           0 :         case errParameterExpected:      return EXC_ERR_VALUE;
     220           0 :         case errNoValue:                return EXC_ERR_VALUE;
     221           0 :         case errCircularReference:      return EXC_ERR_VALUE;
     222           0 :         case errNoCode:                 return EXC_ERR_NULL;
     223           0 :         case errNoRef:                  return EXC_ERR_REF;
     224           0 :         case errNoName:                 return EXC_ERR_NAME;
     225           0 :         case errNoAddin:                return EXC_ERR_NAME;
     226           0 :         case errNoMacro:                return EXC_ERR_NAME;
     227           0 :         case NOTAVAILABLE:              return EXC_ERR_NA;
     228             :     }
     229           0 :     return EXC_ERR_NA;
     230             : }
     231             : 
     232           0 : sal_uInt16 XclTools::GetScErrorCode( sal_uInt8 nXclError )
     233             : {
     234             :     using namespace ScErrorCodes;
     235           0 :     switch( nXclError )
     236             :     {
     237           0 :         case EXC_ERR_NULL:  return errNoCode;
     238           0 :         case EXC_ERR_DIV0:  return errDivisionByZero;
     239           0 :         case EXC_ERR_VALUE: return errNoValue;
     240           0 :         case EXC_ERR_REF:   return errNoRef;
     241           0 :         case EXC_ERR_NAME:  return errNoName;
     242           0 :         case EXC_ERR_NUM:   return errIllegalFPOperation;
     243           0 :         case EXC_ERR_NA:    return NOTAVAILABLE;
     244             :         default:            OSL_FAIL( "XclTools::GetScErrorCode - unknown error code" );
     245             :     }
     246           0 :     return NOTAVAILABLE;
     247             : }
     248             : 
     249           0 : double XclTools::ErrorToDouble( sal_uInt8 nXclError )
     250             : {
     251             :     union
     252             :     {
     253             :         double fVal;
     254             :         sal_math_Double smD;
     255             :     };
     256           0 :     ::rtl::math::setNan( &fVal );
     257           0 :     smD.nan_parts.fraction_lo = GetScErrorCode( nXclError );
     258           0 :     return fVal;
     259             : }
     260             : 
     261           1 : XclBoolError XclTools::ErrorToEnum( double& rfDblValue, bool bErrOrBool, sal_uInt8 nValue )
     262             : {
     263             :     XclBoolError eType;
     264           1 :     if( bErrOrBool )
     265             :     {
     266             :         // error value
     267           0 :         switch( nValue )
     268             :         {
     269           0 :             case EXC_ERR_NULL:  eType = xlErrNull;      break;
     270           0 :             case EXC_ERR_DIV0:  eType = xlErrDiv0;      break;
     271           0 :             case EXC_ERR_VALUE: eType = xlErrValue;     break;
     272           0 :             case EXC_ERR_REF:   eType = xlErrRef;       break;
     273           0 :             case EXC_ERR_NAME:  eType = xlErrName;      break;
     274           0 :             case EXC_ERR_NUM:   eType = xlErrNum;       break;
     275           0 :             case EXC_ERR_NA:    eType = xlErrNA;        break;
     276           0 :             default:            eType = xlErrUnknown;
     277             :         }
     278           0 :         rfDblValue = 0.0;
     279             :     }
     280             :     else
     281             :     {
     282             :         // Boolean value
     283           1 :         eType = nValue ? xlErrTrue : xlErrFalse;
     284           1 :         rfDblValue = nValue ? 1.0 : 0.0;
     285             :     }
     286           1 :     return eType;
     287             : }
     288             : 
     289        5001 : sal_uInt16 XclTools::GetTwipsFromInch( double fInches )
     290             : {
     291             :     return static_cast< sal_uInt16 >(
     292        5001 :         ::std::min( ::std::max( (fInches * EXC_TWIPS_PER_INCH + 0.5), 0.0 ), 65535.0 ) );
     293             : }
     294             : 
     295        2757 : sal_uInt16 XclTools::GetTwipsFromHmm( sal_Int32 nHmm )
     296             : {
     297        2757 :     return GetTwipsFromInch( static_cast< double >( nHmm ) / 1000.0 / CM_PER_INCH );
     298             : }
     299             : 
     300        3380 : double XclTools::GetInchFromTwips( sal_Int32 nTwips )
     301             : {
     302        3380 :     return static_cast< double >( nTwips ) / EXC_TWIPS_PER_INCH;
     303             : }
     304             : 
     305        2735 : double XclTools::GetInchFromHmm( sal_Int32 nHmm )
     306             : {
     307        2735 :     return GetInchFromTwips( GetTwipsFromHmm( nHmm ) );
     308             : }
     309             : 
     310           4 : sal_Int32 XclTools::GetHmmFromInch( double fInches )
     311             : {
     312           4 :     return static_cast< sal_Int32 >( fInches * CM_PER_INCH * 1000 );
     313             : }
     314             : 
     315           4 : sal_Int32 XclTools::GetHmmFromTwips( sal_Int32 nTwips )
     316             : {
     317           4 :     return GetHmmFromInch( GetInchFromTwips( nTwips ) );
     318             : }
     319             : 
     320         639 : sal_uInt16 XclTools::GetScColumnWidth( sal_uInt16 nXclWidth, long nScCharWidth )
     321             : {
     322         639 :     double fScWidth = static_cast< double >( nXclWidth ) / 256.0 * nScCharWidth + 0.5;
     323         639 :     return limit_cast< sal_uInt16 >( fScWidth );
     324             : }
     325             : 
     326      109824 : sal_uInt16 XclTools::GetXclColumnWidth( sal_uInt16 nScWidth, long nScCharWidth )
     327             : {
     328      109824 :     double fXclWidth = static_cast< double >( nScWidth ) * 256.0 / nScCharWidth + 0.5;
     329      109824 :     return limit_cast< sal_uInt16 >( fXclWidth );
     330             : }
     331             : 
     332         538 : double XclTools::GetXclDefColWidthCorrection( long nXclDefFontHeight )
     333             : {
     334         538 :     return 40960.0 / ::std::max( nXclDefFontHeight - 15L, 60L ) + 50.0;
     335             : }
     336             : 
     337             : // formatting
     338             : 
     339        1309 : Color XclTools::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt16 nXclPattern )
     340             : {
     341             :     // 0x00 == 0% transparence (full rPattColor)
     342             :     // 0x80 == 100% transparence (full rBackColor)
     343             :     static const sal_uInt8 pnRatioTable[] =
     344             :     {
     345             :         0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40,     // 00 - 07
     346             :         0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48,     // 08 - 15
     347             :         0x50, 0x70, 0x78                                    // 16 - 18
     348             :     };
     349        1309 :     return (nXclPattern < SAL_N_ELEMENTS( pnRatioTable )) ?
     350        1309 :         ScfTools::GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
     351             : }
     352             : 
     353             : // text encoding
     354             : 
     355             : namespace {
     356             : 
     357             : const struct XclCodePageEntry
     358             : {
     359             :     sal_uInt16                  mnCodePage;
     360             :     rtl_TextEncoding            meTextEnc;
     361             : }
     362             : pCodePageTable[] =
     363             : {
     364             :     {     437,  RTL_TEXTENCODING_IBM_437        },  // OEM US
     365             : //  {     720,  RTL_TEXTENCODING_IBM_720        },  // OEM Arabic
     366             :     {     737,  RTL_TEXTENCODING_IBM_737        },  // OEM Greek
     367             :     {     775,  RTL_TEXTENCODING_IBM_775        },  // OEM Baltic
     368             :     {     850,  RTL_TEXTENCODING_IBM_850        },  // OEM Latin I
     369             :     {     852,  RTL_TEXTENCODING_IBM_852        },  // OEM Latin II (Central European)
     370             :     {     855,  RTL_TEXTENCODING_IBM_855        },  // OEM Cyrillic
     371             :     {     857,  RTL_TEXTENCODING_IBM_857        },  // OEM Turkish
     372             : //  {     858,  RTL_TEXTENCODING_IBM_858        },  // OEM Multilingual Latin I with Euro
     373             :     {     860,  RTL_TEXTENCODING_IBM_860        },  // OEM Portuguese
     374             :     {     861,  RTL_TEXTENCODING_IBM_861        },  // OEM Icelandic
     375             :     {     862,  RTL_TEXTENCODING_IBM_862        },  // OEM Hebrew
     376             :     {     863,  RTL_TEXTENCODING_IBM_863        },  // OEM Canadian (French)
     377             :     {     864,  RTL_TEXTENCODING_IBM_864        },  // OEM Arabic
     378             :     {     865,  RTL_TEXTENCODING_IBM_865        },  // OEM Nordic
     379             :     {     866,  RTL_TEXTENCODING_IBM_866        },  // OEM Cyrillic (Russian)
     380             :     {     869,  RTL_TEXTENCODING_IBM_869        },  // OEM Greek (Modern)
     381             :     {     874,  RTL_TEXTENCODING_MS_874         },  // MS Windows Thai
     382             :     {     932,  RTL_TEXTENCODING_MS_932         },  // MS Windows Japanese Shift-JIS
     383             :     {     936,  RTL_TEXTENCODING_MS_936         },  // MS Windows Chinese Simplified GBK
     384             :     {     949,  RTL_TEXTENCODING_MS_949         },  // MS Windows Korean (Wansung)
     385             :     {     950,  RTL_TEXTENCODING_MS_950         },  // MS Windows Chinese Traditional BIG5
     386             :     {    1200,  RTL_TEXTENCODING_DONTKNOW       },  // Unicode (BIFF8) - return *_DONTKNOW to preserve old code page
     387             :     {    1250,  RTL_TEXTENCODING_MS_1250        },  // MS Windows Latin II (Central European)
     388             :     {    1251,  RTL_TEXTENCODING_MS_1251        },  // MS Windows Cyrillic
     389             :     {    1252,  RTL_TEXTENCODING_MS_1252        },  // MS Windows Latin I (BIFF4-BIFF8)
     390             :     {    1253,  RTL_TEXTENCODING_MS_1253        },  // MS Windows Greek
     391             :     {    1254,  RTL_TEXTENCODING_MS_1254        },  // MS Windows Turkish
     392             :     {    1255,  RTL_TEXTENCODING_MS_1255        },  // MS Windows Hebrew
     393             :     {    1256,  RTL_TEXTENCODING_MS_1256        },  // MS Windows Arabic
     394             :     {    1257,  RTL_TEXTENCODING_MS_1257        },  // MS Windows Baltic
     395             :     {    1258,  RTL_TEXTENCODING_MS_1258        },  // MS Windows Vietnamese
     396             :     {    1361,  RTL_TEXTENCODING_MS_1361        },  // MS Windows Korean (Johab)
     397             :     {   10000,  RTL_TEXTENCODING_APPLE_ROMAN    },  // Apple Roman
     398             :     {   32768,  RTL_TEXTENCODING_APPLE_ROMAN    },  // Apple Roman
     399             :     {   32769,  RTL_TEXTENCODING_MS_1252        }   // MS Windows Latin I (BIFF2-BIFF3)
     400             : };
     401             : const XclCodePageEntry* const pCodePageTableEnd = STATIC_ARRAY_END( pCodePageTable );
     402             : 
     403             : struct XclCodePageEntry_CPPred
     404             : {
     405          82 :     inline explicit     XclCodePageEntry_CPPred( sal_uInt16 nCodePage ) : mnCodePage( nCodePage ) {}
     406        1733 :     inline bool         operator()( const XclCodePageEntry& rEntry ) const { return rEntry.mnCodePage == mnCodePage; }
     407             :     sal_uInt16          mnCodePage;
     408             : };
     409             : 
     410             : struct XclCodePageEntry_TEPred
     411             : {
     412           0 :     inline explicit     XclCodePageEntry_TEPred( rtl_TextEncoding eTextEnc ) : meTextEnc( eTextEnc ) {}
     413           0 :     inline bool         operator()( const XclCodePageEntry& rEntry ) const { return rEntry.meTextEnc == meTextEnc; }
     414             :     rtl_TextEncoding    meTextEnc;
     415             : };
     416             : 
     417             : } // namespace
     418             : 
     419          82 : rtl_TextEncoding XclTools::GetTextEncoding( sal_uInt16 nCodePage )
     420             : {
     421          82 :     const XclCodePageEntry* pEntry = ::std::find_if( pCodePageTable, pCodePageTableEnd, XclCodePageEntry_CPPred( nCodePage ) );
     422          82 :     if( pEntry == pCodePageTableEnd )
     423             :     {
     424             :         OSL_TRACE( "XclTools::GetTextEncoding - unknown code page: 0x%04hX (%d)", nCodePage, nCodePage );
     425           0 :         return RTL_TEXTENCODING_DONTKNOW;
     426             :     }
     427          82 :     return pEntry->meTextEnc;
     428             : }
     429             : 
     430          17 : sal_uInt16 XclTools::GetXclCodePage( rtl_TextEncoding eTextEnc )
     431             : {
     432          17 :     if( eTextEnc == RTL_TEXTENCODING_UNICODE )
     433          17 :         return 1200;    // for BIFF8
     434             : 
     435           0 :     const XclCodePageEntry* pEntry = ::std::find_if( pCodePageTable, pCodePageTableEnd, XclCodePageEntry_TEPred( eTextEnc ) );
     436           0 :     if( pEntry == pCodePageTableEnd )
     437             :     {
     438             :         OSL_TRACE( "XclTools::GetXclCodePage - unsupported text encoding: %d", eTextEnc );
     439           0 :         return 1252;
     440             :     }
     441           0 :     return pEntry->mnCodePage;
     442             : }
     443             : 
     444         914 : OUString XclTools::GetXclFontName( const OUString& rFontName )
     445             : {
     446             :     // substitute with MS fonts
     447         914 :     OUString aNewName = GetSubsFontName(rFontName, SubsFontFlags::ONLYONE | SubsFontFlags::MS);
     448         914 :     return aNewName.isEmpty() ? rFontName : aNewName;
     449             : }
     450             : 
     451             : // built-in defined names
     452          10 : const OUString XclTools::maDefNamePrefix( "Excel_BuiltIn_" );
     453             : 
     454          10 : const OUString XclTools::maDefNamePrefixXml ( "_xlnm." );
     455             : 
     456             : static const sal_Char* const ppcDefNames[] =
     457             : {
     458             :     "Consolidate_Area",
     459             :     "Auto_Open",
     460             :     "Auto_Close",
     461             :     "Extract",
     462             :     "Database",
     463             :     "Criteria",
     464             :     "Print_Area",
     465             :     "Print_Titles",
     466             :     "Recorder",
     467             :     "Data_Form",
     468             :     "Auto_Activate",
     469             :     "Auto_Deactivate",
     470             :     "Sheet_Title",
     471             :     "_FilterDatabase"
     472             : };
     473             : 
     474          33 : OUString XclTools::GetXclBuiltInDefName( sal_Unicode cBuiltIn )
     475             : {
     476             :     OSL_ENSURE( SAL_N_ELEMENTS( ppcDefNames ) == EXC_BUILTIN_UNKNOWN,
     477             :         "XclTools::GetXclBuiltInDefName - built-in defined name list modified" );
     478             : 
     479          33 :     if( cBuiltIn < SAL_N_ELEMENTS( ppcDefNames ) )
     480          33 :         return OUString::createFromAscii(ppcDefNames[cBuiltIn]);
     481             :     else
     482           0 :         return OUString::number(cBuiltIn);
     483             : }
     484             : 
     485          32 : OUString XclTools::GetBuiltInDefName( sal_Unicode cBuiltIn )
     486             : {
     487          32 :     OUStringBuffer aBuf(maDefNamePrefix);
     488          32 :     aBuf.append(GetXclBuiltInDefName(cBuiltIn));
     489          32 :     return aBuf.makeStringAndClear();
     490             : }
     491             : 
     492           0 : OUString XclTools::GetBuiltInDefNameXml( sal_Unicode cBuiltIn )
     493             : {
     494           0 :     OUStringBuffer aBuf(maDefNamePrefixXml);
     495           0 :     aBuf.append(GetXclBuiltInDefName(cBuiltIn));
     496           0 :     return aBuf.makeStringAndClear();
     497             : }
     498             : 
     499           2 : sal_Unicode XclTools::GetBuiltInDefNameIndex( const OUString& rDefName )
     500             : {
     501           2 :     sal_Int32 nPrefixLen = maDefNamePrefix.getLength();
     502           2 :     if( rDefName.startsWithIgnoreAsciiCase( maDefNamePrefix ) )
     503             :     {
     504           0 :         for( sal_Unicode cBuiltIn = 0; cBuiltIn < EXC_BUILTIN_UNKNOWN; ++cBuiltIn )
     505             :         {
     506           0 :             OUString aBuiltInName(GetXclBuiltInDefName(cBuiltIn));
     507           0 :             sal_Int32 nBuiltInLen = aBuiltInName.getLength();
     508           0 :             if( rDefName.matchIgnoreAsciiCase( aBuiltInName, nPrefixLen ) )
     509             :             {
     510             :                 // name can be followed by underline or space character
     511           0 :                 sal_Int32 nNextCharPos = nPrefixLen + nBuiltInLen;
     512           0 :                 sal_Unicode cNextChar = (rDefName.getLength() > nNextCharPos) ? rDefName[nNextCharPos] : '\0';
     513           0 :                 if( (cNextChar == '\0') || (cNextChar == ' ') || (cNextChar == '_') )
     514           0 :                     return cBuiltIn;
     515             :             }
     516           0 :         }
     517             :     }
     518           2 :     return EXC_BUILTIN_UNKNOWN;
     519             : }
     520             : 
     521             : // built-in style names
     522             : 
     523          10 : const OUString XclTools::maStyleNamePrefix1( "Excel_BuiltIn_" );
     524          10 : const OUString XclTools::maStyleNamePrefix2( "Excel Built-in " );
     525             : 
     526             : static const sal_Char* const ppcStyleNames[] =
     527             : {
     528             :     "",                 // "Normal" not used directly, but localized "Default"
     529             :     "RowLevel_",        // outline level will be appended
     530             :     "ColumnLevel_",     // outline level will be appended
     531             :     "Comma",
     532             :     "Currency",
     533             :     "Percent",
     534             :     "Comma_0",
     535             :     "Currency_0",
     536             :     "Hyperlink",
     537             :     "Followed_Hyperlink"
     538             : };
     539             : 
     540         903 : OUString XclTools::GetBuiltInStyleName( sal_uInt8 nStyleId, const OUString& rName, sal_uInt8 nLevel )
     541             : {
     542         903 :     OUString aStyleName;
     543             : 
     544         903 :     if( nStyleId == EXC_STYLE_NORMAL )  // "Normal" becomes "Default" style
     545             :     {
     546          82 :         aStyleName = ScGlobal::GetRscString( STR_STYLENAME_STANDARD );
     547             :     }
     548             :     else
     549             :     {
     550         821 :         OUStringBuffer aBuf(maStyleNamePrefix1);
     551         821 :         if( nStyleId < SAL_N_ELEMENTS( ppcStyleNames ) )
     552         452 :             aBuf.appendAscii(ppcStyleNames[nStyleId]);
     553         369 :         else if (!rName.isEmpty())
     554         369 :             aBuf.append(rName);
     555             :         else
     556           0 :             aBuf.append(static_cast<sal_Int32>(nStyleId));
     557             : 
     558         821 :         if( (nStyleId == EXC_STYLE_ROWLEVEL) || (nStyleId == EXC_STYLE_COLLEVEL) )
     559          22 :             aBuf.append(static_cast<sal_Int32>(nLevel+1));
     560             : 
     561         821 :         aStyleName = aBuf.makeStringAndClear();
     562             :     }
     563             : 
     564         903 :     return aStyleName;
     565             : }
     566             : 
     567         252 : bool XclTools::IsBuiltInStyleName( const OUString& rStyleName, sal_uInt8* pnStyleId, sal_Int32* pnNextChar )
     568             : {
     569             :     // "Default" becomes "Normal"
     570         252 :     if (rStyleName.equals(ScGlobal::GetRscString(STR_STYLENAME_STANDARD)))
     571             :     {
     572         180 :         if( pnStyleId ) *pnStyleId = EXC_STYLE_NORMAL;
     573         180 :         if( pnNextChar ) *pnNextChar = rStyleName.getLength();
     574         180 :         return true;
     575             :     }
     576             : 
     577             :     // try the other built-in styles
     578          72 :     sal_uInt8 nFoundId = 0;
     579          72 :     sal_Int32 nNextChar = 0;
     580             : 
     581          72 :     sal_Int32 nPrefixLen = 0;
     582          72 :     if( rStyleName.startsWithIgnoreAsciiCase( maStyleNamePrefix1 ) )
     583           0 :         nPrefixLen = maStyleNamePrefix1.getLength();
     584          72 :     else if( rStyleName.startsWithIgnoreAsciiCase( maStyleNamePrefix2 ) )
     585           0 :         nPrefixLen = maStyleNamePrefix2.getLength();
     586          72 :     if( nPrefixLen > 0 )
     587             :     {
     588           0 :         for( sal_uInt8 nId = 0; nId < SAL_N_ELEMENTS( ppcStyleNames ); ++nId )
     589             :         {
     590           0 :             if( nId != EXC_STYLE_NORMAL )
     591             :             {
     592           0 :                 OUString aShortName = OUString::createFromAscii(ppcStyleNames[nId]);
     593           0 :                 if( rStyleName.matchIgnoreAsciiCase( aShortName, nPrefixLen ) &&
     594           0 :                     (nNextChar < nPrefixLen + aShortName.getLength()))
     595             :                 {
     596           0 :                     nFoundId = nId;
     597           0 :                     nNextChar = nPrefixLen + aShortName.getLength();
     598           0 :                 }
     599             :             }
     600             :         }
     601             :     }
     602             : 
     603          72 :     if( nNextChar > 0 )
     604             :     {
     605           0 :         if( pnStyleId ) *pnStyleId = nFoundId;
     606           0 :         if( pnNextChar ) *pnNextChar = nNextChar;
     607           0 :         return true;
     608             :     }
     609             : 
     610          72 :     if( pnStyleId ) *pnStyleId = EXC_STYLE_USERDEF;
     611          72 :     if( pnNextChar ) *pnNextChar = 0;
     612          72 :     return nPrefixLen > 0;  // also return true for unknown built-in styles
     613             : }
     614             : 
     615         230 : bool XclTools::GetBuiltInStyleId( sal_uInt8& rnStyleId, sal_uInt8& rnLevel, const OUString& rStyleName )
     616             : {
     617             :     sal_uInt8 nStyleId;
     618             :     sal_Int32 nNextChar;
     619         230 :     if( IsBuiltInStyleName( rStyleName, &nStyleId, &nNextChar ) && (nStyleId != EXC_STYLE_USERDEF) )
     620             :     {
     621         180 :         if( (nStyleId == EXC_STYLE_ROWLEVEL) || (nStyleId == EXC_STYLE_COLLEVEL) )
     622             :         {
     623           0 :             OUString aLevel = rStyleName.copy(nNextChar);
     624           0 :             sal_Int32 nLevel = aLevel.toInt32();
     625           0 :             if (OUString::number(nLevel) == aLevel && nLevel > 0 && nLevel <= EXC_STYLE_LEVELCOUNT)
     626             :             {
     627           0 :                 rnStyleId = nStyleId;
     628           0 :                 rnLevel = static_cast< sal_uInt8 >( nLevel - 1 );
     629           0 :                 return true;
     630           0 :             }
     631             :         }
     632         180 :         else if( rStyleName.getLength() == nNextChar )
     633             :         {
     634         180 :             rnStyleId = nStyleId;
     635         180 :             rnLevel = EXC_STYLE_NOLEVEL;
     636         180 :             return true;
     637             :         }
     638             :     }
     639          50 :     rnStyleId = EXC_STYLE_USERDEF;
     640          50 :     rnLevel = EXC_STYLE_NOLEVEL;
     641          50 :     return false;
     642             : }
     643             : 
     644             : // conditional formatting style names
     645             : 
     646          10 : const OUString XclTools::maCFStyleNamePrefix1( "Excel_CondFormat_" );
     647          10 : const OUString XclTools::maCFStyleNamePrefix2( "ConditionalStyle_" );
     648             : 
     649           2 : OUString XclTools::GetCondFormatStyleName( SCTAB nScTab, sal_Int32 nFormat, sal_uInt16 nCondition )
     650             : {
     651           2 :     OUStringBuffer aBuf(maCFStyleNamePrefix1);
     652           2 :     aBuf.append(static_cast<sal_Int32>(nScTab+1));
     653           2 :     aBuf.append('_');
     654           2 :     aBuf.append(static_cast<sal_Int32>(nFormat+1));
     655           2 :     aBuf.append('_');
     656           2 :     aBuf.append(static_cast<sal_Int32>(nCondition+1));
     657           2 :     return aBuf.makeStringAndClear();
     658             : }
     659             : 
     660          22 : bool XclTools::IsCondFormatStyleName( const OUString& rStyleName )
     661             : {
     662          22 :     if( rStyleName.startsWithIgnoreAsciiCase( maCFStyleNamePrefix1 ) )
     663           0 :         return true;
     664             : 
     665          22 :     if( rStyleName.startsWithIgnoreAsciiCase( maCFStyleNamePrefix2 ) )
     666          10 :         return true;
     667             : 
     668          12 :     return false;
     669             : }
     670             : 
     671             : // stream handling
     672             : 
     673          45 : void XclTools::SkipSubStream( XclImpStream& rStrm )
     674             : {
     675          45 :     bool bLoop = true;
     676        7407 :     while( bLoop && rStrm.StartNextRecord() )
     677             :     {
     678        7317 :         sal_uInt16 nRecId = rStrm.GetRecId();
     679        7317 :         bLoop = nRecId != EXC_ID_EOF;
     680        7317 :         if( (nRecId == EXC_ID2_BOF) || (nRecId == EXC_ID3_BOF) || (nRecId == EXC_ID4_BOF) || (nRecId == EXC_ID5_BOF) )
     681           0 :             SkipSubStream( rStrm );
     682             :     }
     683          45 : }
     684             : 
     685             : // Basic macro names
     686             : 
     687          10 : const OUString XclTools::maSbMacroPrefix( "vnd.sun.star.script:" );
     688          10 : const OUString XclTools::maSbMacroSuffix( "?language=Basic&location=document" );
     689             : 
     690           2 : OUString XclTools::GetSbMacroUrl( const OUString& rMacroName, SfxObjectShell* pDocShell )
     691             : {
     692             :     OSL_ENSURE( !rMacroName.isEmpty(), "XclTools::GetSbMacroUrl - macro name is empty" );
     693           2 :     ::ooo::vba::MacroResolvedInfo aMacroInfo = ::ooo::vba::resolveVBAMacro( pDocShell, rMacroName, false );
     694           2 :     if( aMacroInfo.mbFound )
     695           2 :         return ::ooo::vba::makeMacroURL( aMacroInfo.msResolvedMacro );
     696           0 :     return OUString();
     697             : }
     698             : 
     699           0 : OUString XclTools::GetXclMacroName( const OUString& rSbMacroUrl )
     700             : {
     701           0 :     sal_Int32 nSbMacroUrlLen = rSbMacroUrl.getLength();
     702           0 :     sal_Int32 nMacroNameLen = nSbMacroUrlLen - maSbMacroPrefix.getLength() - maSbMacroSuffix.getLength();
     703           0 :     if( (nMacroNameLen > 0) && rSbMacroUrl.startsWithIgnoreAsciiCase( maSbMacroPrefix ) &&
     704           0 :             rSbMacroUrl.endsWithIgnoreAsciiCase( maSbMacroSuffix ) )
     705             :     {
     706           0 :         sal_Int32 nPrjDot = rSbMacroUrl.indexOf( '.', maSbMacroPrefix.getLength() ) + 1;
     707           0 :         return rSbMacroUrl.copy( nPrjDot, nSbMacroUrlLen - nPrjDot - maSbMacroSuffix.getLength() );
     708             :     }
     709           0 :     return OUString();
     710             : }
     711             : 
     712             : // read/write colors
     713             : 
     714        1638 : XclImpStream& operator>>( XclImpStream& rStrm, Color& rColor )
     715             : {
     716        1638 :     sal_uInt8 nR = rStrm.ReaduInt8();
     717        1638 :     sal_uInt8 nG = rStrm.ReaduInt8();
     718        1638 :     sal_uInt8 nB = rStrm.ReaduInt8();
     719        1638 :     rStrm.Ignore( 1 );//nD
     720        1638 :     rColor.SetColor( RGB_COLORDATA( nR, nG, nB ) );
     721        1638 :     return rStrm;
     722             : }
     723             : 
     724          95 : XclExpStream& operator<<( XclExpStream& rStrm, const Color& rColor )
     725             : {
     726          95 :     return rStrm << rColor.GetRed() << rColor.GetGreen() << rColor.GetBlue() << sal_uInt8( 0 );
     727          30 : }
     728             : 
     729             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11