LCOV - code coverage report
Current view: top level - libreoffice/basic/source/sbx - sbxbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 57 190 30.0 %
Date: 2012-12-27 Functions: 18 47 38.3 %
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 <tools/shl.hxx>
      21             : #include <tools/stream.hxx>
      22             : 
      23             : #include <basic/sbx.hxx>
      24             : #include <basic/sbxfac.hxx>
      25             : #include <basic/sbxbase.hxx>
      26             : 
      27             : #include <rtl/instance.hxx>
      28             : #include <rtl/ustring.hxx>
      29             : #include <boost/foreach.hpp>
      30             : 
      31             : // AppData-Structure for SBX:
      32             : 
      33       30466 : TYPEINIT0(SbxBase)
      34             : 
      35             : namespace
      36             : {
      37             :     class theSbxAppData : public rtl::Static<SbxAppData, theSbxAppData> {};
      38             : }
      39             : 
      40       10134 : SbxAppData& GetSbxData_Impl()
      41             : {
      42       10134 :     return theSbxAppData::get();
      43             : }
      44             : 
      45          28 : SbxAppData::~SbxAppData()
      46             : {
      47          14 :     delete pBasicFormater;
      48          14 : }
      49             : 
      50             : 
      51             : //////////////////////////////// SbxBase /////////////////////////////////
      52             : 
      53             : DBG_NAME(SbxBase);
      54             : 
      55       15424 : SbxBase::SbxBase()
      56             : {
      57             :     DBG_CTOR( SbxBase, 0 );
      58       15424 :     nFlags  = SBX_READWRITE;
      59       15424 : }
      60             : 
      61          75 : SbxBase::SbxBase( const SbxBase& r )
      62          75 :     : SvRefBase( r )
      63             : {
      64             :     DBG_CTOR( SbxBase, 0 );
      65          75 :     nFlags  = r.nFlags;
      66          75 : }
      67             : 
      68       10309 : SbxBase::~SbxBase()
      69             : {
      70             :     DBG_DTOR(SbxBase,0);
      71       20618 : }
      72             : 
      73           0 : SbxBase& SbxBase::operator=( const SbxBase& r )
      74             : {
      75             :     DBG_CHKTHIS( SbxBase, 0 );
      76           0 :     nFlags = r.nFlags;
      77           0 :     return *this;
      78             : }
      79             : 
      80           0 : SbxDataType SbxBase::GetType() const
      81             : {
      82             :     DBG_CHKTHIS( SbxBase, 0 );
      83           0 :     return SbxEMPTY;
      84             : }
      85             : 
      86           0 : SbxClassType SbxBase::GetClass() const
      87             : {
      88             :     DBG_CHKTHIS( SbxBase, 0 );
      89           0 :     return SbxCLASS_DONTCARE;
      90             : }
      91             : 
      92           0 : void SbxBase::Clear()
      93             : {
      94             :     DBG_CHKTHIS( SbxBase, 0 );
      95           0 : }
      96             : 
      97           0 : sal_Bool SbxBase::IsFixed() const
      98             : {
      99             :     DBG_CHKTHIS( SbxBase, 0 );
     100           0 :     return IsSet( SBX_FIXED );
     101             : }
     102             : 
     103       13415 : void SbxBase::SetModified( sal_Bool b )
     104             : {
     105             :     DBG_CHKTHIS( SbxBase, 0 );
     106       13415 :     if( IsSet( SBX_NO_MODIFY ) )
     107       13422 :         return;
     108       13408 :     if( b )
     109        9796 :         SetFlag( SBX_MODIFIED );
     110             :     else
     111        3612 :         ResetFlag( SBX_MODIFIED );
     112             : }
     113             : 
     114        4822 : SbxError SbxBase::GetError()
     115             : {
     116        4822 :     return GetSbxData_Impl().eSbxError;
     117             : }
     118             : 
     119           0 : void SbxBase::SetError( SbxError e )
     120             : {
     121           0 :     SbxAppData& r = GetSbxData_Impl();
     122           0 :     if( e && r.eSbxError == SbxERR_OK )
     123           0 :         r.eSbxError = e;
     124           0 : }
     125             : 
     126        4595 : sal_Bool SbxBase::IsError()
     127             : {
     128        4595 :     return sal_Bool( GetSbxData_Impl().eSbxError != SbxERR_OK );
     129             : }
     130             : 
     131         142 : void SbxBase::ResetError()
     132             : {
     133         142 :     GetSbxData_Impl().eSbxError = SbxERR_OK;
     134         142 : }
     135             : 
     136         379 : void SbxBase::AddFactory( SbxFactory* pFac )
     137             : {
     138         379 :     SbxAppData& r = GetSbxData_Impl();
     139             : 
     140             :     // From 1996-03-06: take the HandleLast-Flag into account
     141         379 :     sal_uInt16 nPos = r.aFacs.size(); // Insert position
     142         379 :     if( !pFac->IsHandleLast() )         // Only if not self HandleLast
     143             :     {
     144             :         // Rank new factory in front of factories with HandleLast
     145        1118 :         while( nPos > 0 &&
     146         360 :                 r.aFacs[ nPos-1 ].IsHandleLast() )
     147           0 :             nPos--;
     148             :     }
     149         379 :     r.aFacs.insert( r.aFacs.begin() + nPos, pFac );
     150         379 : }
     151             : 
     152         190 : void SbxBase::RemoveFactory( SbxFactory* pFac )
     153             : {
     154         190 :     SbxAppData& r = GetSbxData_Impl();
     155        3360 :     for(SbxFacs::iterator it = r.aFacs.begin(); it != r.aFacs.end(); ++it)
     156             :     {
     157        3360 :         if( &(*it) == pFac )
     158             :         {
     159         190 :             r.aFacs.release( it ).release(); break;
     160             :         }
     161             :     }
     162         190 : }
     163             : 
     164             : 
     165           0 : SbxBase* SbxBase::Create( sal_uInt16 nSbxId, sal_uInt32 nCreator )
     166             : {
     167             :     // #91626: Hack to skip old Basic dialogs
     168             :     // Problem: There does not exist a factory any more,
     169             :     // so we have to create a dummy SbxVariable instead
     170           0 :     if( nSbxId == 0x65 )    // Dialog Id
     171           0 :         return new SbxVariable;
     172             : 
     173           0 :     rtl::OUString aEmptyStr;
     174           0 :     if( nCreator == SBXCR_SBX )
     175           0 :       switch( nSbxId )
     176             :     {
     177           0 :         case SBXID_VALUE:       return new SbxValue;
     178           0 :         case SBXID_VARIABLE:    return new SbxVariable;
     179           0 :         case SBXID_ARRAY:       return new SbxArray;
     180           0 :         case SBXID_DIMARRAY:    return new SbxDimArray;
     181           0 :         case SBXID_OBJECT:      return new SbxObject( aEmptyStr );
     182           0 :         case SBXID_COLLECTION:  return new SbxCollection( aEmptyStr );
     183             :         case SBXID_FIXCOLLECTION:
     184           0 :                                 return new SbxStdCollection( aEmptyStr, aEmptyStr );
     185           0 :         case SBXID_METHOD:      return new SbxMethod( aEmptyStr, SbxEMPTY );
     186           0 :         case SBXID_PROPERTY:    return new SbxProperty( aEmptyStr, SbxEMPTY );
     187             :     }
     188             :     // Unknown type: go over the factories!
     189           0 :     SbxAppData& r = GetSbxData_Impl();
     190           0 :     SbxBase* pNew = NULL;
     191           0 :     BOOST_FOREACH(SbxFactory& rFac, r.aFacs)
     192             :     {
     193           0 :         pNew = rFac.Create( nSbxId, nCreator );
     194           0 :         if( pNew )
     195           0 :             break;
     196             :     }
     197             :     SAL_WARN_IF(!pNew, "basic", "No factory for SBX ID " << nSbxId);
     198           0 :     return pNew;
     199             : }
     200             : 
     201           6 : SbxObject* SbxBase::CreateObject( const rtl::OUString& rClass )
     202             : {
     203           6 :     SbxAppData& r = GetSbxData_Impl();
     204           6 :     SbxObject* pNew = NULL;
     205          36 :     BOOST_FOREACH(SbxFactory& rFac, r.aFacs)
     206             :     {
     207          36 :         pNew = rFac.CreateObject( rClass );
     208          36 :         if( pNew )
     209           6 :             break;
     210             :     }
     211             :     SAL_WARN_IF(!pNew, "basic", "No factory for object class " << rClass);
     212           6 :     return pNew;
     213             : }
     214             : 
     215           0 : SbxBase* SbxBase::Load( SvStream& rStrm )
     216             : {
     217             :     sal_uInt16 nSbxId, nFlags, nVer;
     218             :     sal_uInt32 nCreator, nSize;
     219           0 :     rStrm >> nCreator >> nSbxId >> nFlags >> nVer;
     220             : 
     221             :     // Correcting a foolishness of mine:
     222           0 :     if( nFlags & SBX_RESERVED )
     223           0 :         nFlags = ( nFlags & ~SBX_RESERVED ) | SBX_GBLSEARCH;
     224             : 
     225           0 :     sal_uIntPtr nOldPos = rStrm.Tell();
     226           0 :     rStrm >> nSize;
     227           0 :     SbxBase* p = Create( nSbxId, nCreator );
     228           0 :     if( p )
     229             :     {
     230           0 :         p->nFlags = nFlags;
     231           0 :         if( p->LoadData( rStrm, nVer ) )
     232             :         {
     233           0 :             sal_uIntPtr nNewPos = rStrm.Tell();
     234           0 :             nOldPos += nSize;
     235             :             DBG_ASSERT( nOldPos >= nNewPos, "SBX: Zu viele Daten eingelesen" );
     236           0 :             if( nOldPos != nNewPos )
     237           0 :                 rStrm.Seek( nOldPos );
     238           0 :             if( !p->LoadCompleted() )
     239             :             {
     240             :                 // Deleting of the object
     241           0 :                 SbxBaseRef aRef( p );
     242           0 :                 p = NULL;
     243             :             }
     244             :         }
     245             :         else
     246             :         {
     247           0 :             rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
     248             :             // Deleting of the object
     249           0 :             SbxBaseRef aRef( p );
     250           0 :             p = NULL;
     251             :         }
     252             :     }
     253             :     else
     254           0 :         rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
     255           0 :     return p;
     256             : }
     257             : 
     258             : // Skip the Sbx-Object inside the stream
     259           0 : void SbxBase::Skip( SvStream& rStrm )
     260             : {
     261             :     sal_uInt16 nSbxId, nFlags, nVer;
     262             :     sal_uInt32 nCreator, nSize;
     263           0 :     rStrm >> nCreator >> nSbxId >> nFlags >> nVer;
     264             : 
     265           0 :     sal_uIntPtr nStartPos = rStrm.Tell();
     266           0 :     rStrm >> nSize;
     267             : 
     268           0 :     rStrm.Seek( nStartPos + nSize );
     269           0 : }
     270             : 
     271           0 : sal_Bool SbxBase::Store( SvStream& rStrm )
     272             : {
     273             :     DBG_CHKTHIS( SbxBase, 0 );
     274           0 :     if( !( nFlags & SBX_DONTSTORE ) )
     275             :     {
     276           0 :         rStrm << (sal_uInt32) GetCreator()
     277           0 :               << (sal_uInt16) GetSbxId()
     278           0 :               << (sal_uInt16) GetFlags()
     279           0 :               << (sal_uInt16) GetVersion();
     280           0 :         sal_uIntPtr nOldPos = rStrm.Tell();
     281           0 :         rStrm << (sal_uInt32) 0L;
     282           0 :         sal_Bool bRes = StoreData( rStrm );
     283           0 :         sal_uIntPtr nNewPos = rStrm.Tell();
     284           0 :         rStrm.Seek( nOldPos );
     285           0 :         rStrm << (sal_uInt32) ( nNewPos - nOldPos );
     286           0 :         rStrm.Seek( nNewPos );
     287           0 :         if( rStrm.GetError() != SVSTREAM_OK )
     288           0 :             bRes = sal_False;
     289           0 :         if( bRes )
     290           0 :             bRes = StoreCompleted();
     291           0 :         return bRes;
     292             :     }
     293             :     else
     294           0 :         return sal_True;
     295             : }
     296             : 
     297           0 : sal_Bool SbxBase::LoadData( SvStream&, sal_uInt16 )
     298             : {
     299             :     DBG_CHKTHIS( SbxBase, 0 );
     300           0 :     return sal_False;
     301             : }
     302             : 
     303           0 : sal_Bool SbxBase::StoreData( SvStream& ) const
     304             : {
     305             :     DBG_CHKTHIS( SbxBase, 0 );
     306           0 :     return sal_False;
     307             : }
     308             : 
     309           0 : sal_Bool SbxBase::LoadPrivateData( SvStream&, sal_uInt16 )
     310             : {
     311             :     DBG_CHKTHIS( SbxBase, 0 );
     312           0 :     return sal_True;
     313             : }
     314             : 
     315           0 : sal_Bool SbxBase::StorePrivateData( SvStream& ) const
     316             : {
     317             :     DBG_CHKTHIS( SbxBase, 0 );
     318           0 :     return sal_True;
     319             : }
     320             : 
     321           0 : sal_Bool SbxBase::LoadCompleted()
     322             : {
     323             :     DBG_CHKTHIS( SbxBase, 0 );
     324           0 :     return sal_True;
     325             : }
     326             : 
     327           0 : sal_Bool SbxBase::StoreCompleted()
     328             : {
     329             :     DBG_CHKTHIS( SbxBase, 0 );
     330           0 :     return sal_True;
     331             : }
     332             : 
     333             : //////////////////////////////// SbxFactory ////////////////////////////////
     334             : 
     335         379 : SbxFactory::~SbxFactory()
     336             : {
     337         379 : }
     338             : 
     339           0 : SbxBase* SbxFactory::Create( sal_uInt16, sal_uInt32 )
     340             : {
     341           0 :     return NULL;
     342             : }
     343             : 
     344           0 : SbxObject* SbxFactory::CreateObject( const rtl::OUString& )
     345             : {
     346           0 :     return NULL;
     347             : }
     348             : 
     349             : ///////////////////////////////// SbxInfo //////////////////////////////////
     350             : 
     351          16 : SbxInfo::~SbxInfo()
     352          16 : {}
     353             : 
     354           0 : void SbxInfo::AddParam(const rtl::OUString& rName, SbxDataType eType, sal_uInt16 nFlags)
     355             : {
     356           0 :     aParams.push_back(new SbxParamInfo(rName, eType, nFlags));
     357           0 : }
     358             : 
     359           7 : const SbxParamInfo* SbxInfo::GetParam( sal_uInt16 n ) const
     360             : {
     361           7 :     if( n < 1 || n > aParams.size() )
     362           7 :         return NULL;
     363             :     else
     364           0 :         return &(aParams[n - 1]);
     365             : }
     366             : 
     367           0 : sal_Bool SbxInfo::LoadData( SvStream& rStrm, sal_uInt16 nVer )
     368             : {
     369           0 :     aParams.clear();
     370             :     sal_uInt16 nParam;
     371             :     aComment = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rStrm,
     372           0 :         RTL_TEXTENCODING_ASCII_US);
     373             :     aHelpFile = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rStrm,
     374           0 :         RTL_TEXTENCODING_ASCII_US);
     375           0 :     rStrm >> nHelpId >> nParam;
     376           0 :     while( nParam-- )
     377             :     {
     378             :         sal_uInt16 nType, nFlags;
     379           0 :         sal_uInt32 nUserData = 0;
     380             :         rtl::OUString aName = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rStrm,
     381           0 :             RTL_TEXTENCODING_ASCII_US);
     382           0 :         rStrm >> nType >> nFlags;
     383           0 :         if( nVer > 1 )
     384           0 :             rStrm >> nUserData;
     385           0 :         AddParam( aName, (SbxDataType) nType, nFlags );
     386           0 :         SbxParamInfo& p(aParams.back());
     387           0 :         p.nUserData = nUserData;
     388           0 :     }
     389           0 :     return sal_True;
     390             : }
     391             : 
     392           0 : sal_Bool SbxInfo::StoreData( SvStream& rStrm ) const
     393             : {
     394             :     write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rStrm, aComment,
     395           0 :         RTL_TEXTENCODING_ASCII_US );
     396             :     write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rStrm, aHelpFile,
     397           0 :         RTL_TEXTENCODING_ASCII_US);
     398           0 :     rStrm << nHelpId << static_cast<sal_uInt16>(aParams.size());
     399           0 :     for(SbxParams::const_iterator i = aParams.begin(); i != aParams.end(); ++i)
     400             :     {
     401           0 :         write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rStrm, i->aName,
     402           0 :             RTL_TEXTENCODING_ASCII_US);
     403           0 :         rStrm << (sal_uInt16) i->eType
     404           0 :               << (sal_uInt16) i->nFlags
     405           0 :               << (sal_uInt32) i->nUserData;
     406             :     }
     407           0 :     return sal_True;
     408             : }
     409             : 
     410             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10