LCOV - code coverage report
Current view: top level - libreoffice/basic/source/comp - buffer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 65 112 58.0 %
Date: 2012-12-27 Functions: 8 13 61.5 %
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             : 
      21             : #include "buffer.hxx"
      22             : #include "sbcomp.hxx"
      23             : 
      24             : const static sal_uInt32 UP_LIMIT=0xFFFFFF00L;
      25             : 
      26             : // The SbiBuffer will be expanded in increments of at least 16 Bytes.
      27             : // This is necessary, because many classes emanate from a buffer length
      28             : // of x*16 Bytes.
      29             : 
      30           8 : SbiBuffer::SbiBuffer( SbiParser* p, short n )
      31             : {
      32           8 :     pParser = p;
      33           8 :     n = ( (n + 15 ) / 16 ) * 16;
      34           8 :     if( !n ) n = 16;
      35           8 :     pBuf  = NULL;
      36           8 :     pCur  = NULL;
      37           8 :     nInc  = n;
      38             :     nSize =
      39           8 :     nOff  = 0;
      40           8 : }
      41             : 
      42           8 : SbiBuffer::~SbiBuffer()
      43             : {
      44           8 :     delete[] pBuf;
      45           8 : }
      46             : 
      47             : // Reach out the buffer
      48             : // This lead to the deletion of the buffer!
      49             : 
      50           8 : char* SbiBuffer::GetBuffer()
      51             : {
      52           8 :     char* p = pBuf;
      53           8 :     pBuf = NULL;
      54           8 :     pCur = NULL;
      55           8 :     return p;
      56             : }
      57             : 
      58             : // Test, if the buffer can contain n Bytes.
      59             : // In case of doubt it will be enlarged
      60             : 
      61        1221 : bool SbiBuffer::Check( sal_Int32 n )
      62             : {
      63        1221 :     if( !n )
      64             :     {
      65           0 :         return true;
      66             :     }
      67        1221 :     if( nOff + n  >  nSize )
      68             :     {
      69           8 :         if( nInc == 0 )
      70             :         {
      71           0 :             return false;
      72             :         }
      73             : 
      74           8 :         sal_Int32 nn = 0;
      75          24 :         while( nn < n )
      76             :         {
      77           8 :             nn = nn + nInc;
      78             :         }
      79             :         char* p;
      80           8 :         if( ( nSize + nn ) > UP_LIMIT )
      81             :         {
      82           0 :             p = NULL;
      83             :         }
      84             :         else
      85             :         {
      86           8 :             p = new char [nSize + nn];
      87             :         }
      88           8 :         if( !p )
      89             :         {
      90           0 :             pParser->Error( SbERR_PROG_TOO_LARGE );
      91           0 :             nInc = 0;
      92           0 :             delete[] pBuf; pBuf = NULL;
      93           0 :             return false;
      94             :         }
      95             :         else
      96             :         {
      97           8 :             if( nSize ) memcpy( p, pBuf, nSize );
      98           8 :             delete[] pBuf;
      99           8 :             pBuf = p;
     100           8 :             pCur = pBuf + nOff;
     101           8 :             nSize = nSize + nn;
     102             :         }
     103             :     }
     104        1221 :     return true;
     105             : }
     106             : 
     107             : // Patch of a Location
     108             : 
     109           0 : void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val )
     110             : {
     111           0 :     if( ( off + sizeof( sal_uInt32 ) ) < nOff )
     112             :     {
     113           0 :         sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF );
     114           0 :         sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 );
     115           0 :         sal_uInt8* p = (sal_uInt8*) pBuf + off;
     116           0 :         *p++ = (char) ( val1 & 0xFF );
     117           0 :         *p++ = (char) ( val1 >> 8 );
     118           0 :         *p++ = (char) ( val2 & 0xFF );
     119           0 :         *p   = (char) ( val2 >> 8 );
     120             :     }
     121           0 : }
     122             : 
     123             : // Forward References upon label und procedures
     124             : // establish a linkage. The beginning of the linkage is at the passed parameter,
     125             : // the end of the linkage is 0.
     126             : 
     127          12 : void SbiBuffer::Chain( sal_uInt32 off )
     128             : {
     129          12 :     if( off && pBuf )
     130             :     {
     131             :         sal_uInt8 *ip;
     132           4 :         sal_uInt32 i = off;
     133           4 :         sal_uInt32 val1 = (nOff & 0xFFFF);
     134           4 :         sal_uInt32 val2 = (nOff >> 16);
     135           4 :         do
     136             :         {
     137           4 :             ip = (sal_uInt8*) pBuf + i;
     138           4 :             sal_uInt8* pTmp = ip;
     139           4 :                      i =  *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
     140             : 
     141           4 :             if( i >= nOff )
     142             :             {
     143           0 :                 pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" );
     144           0 :                 break;
     145             :             }
     146           4 :             *ip++ = (char) ( val1 & 0xFF );
     147           4 :             *ip++ = (char) ( val1 >> 8 );
     148           4 :             *ip++ = (char) ( val2 & 0xFF );
     149           4 :             *ip   = (char) ( val2 >> 8 );
     150             :         } while( i );
     151             :     }
     152          12 : }
     153             : 
     154           0 : bool SbiBuffer::operator +=( sal_Int8 n )
     155             : {
     156           0 :     if( Check( 1 ) )
     157             :     {
     158           0 :         *pCur++ = (char) n;
     159           0 :         nOff += 1;
     160           0 :         return true;
     161             :     }
     162             :     else
     163             :     {
     164           0 :         return false;
     165             :     }
     166             : }
     167             : 
     168         231 : bool SbiBuffer::operator +=( sal_uInt8 n )
     169             : {
     170         231 :     if( Check( 1 ) )
     171             :     {
     172         231 :         *pCur++ = (char) n;
     173         231 :         nOff += 1;
     174         231 :         return true;
     175             :     }
     176             :     else
     177             :     {
     178           0 :         return false;
     179             :     }
     180             : }
     181             : 
     182           0 : bool SbiBuffer::operator +=( sal_Int16 n )
     183             : {
     184           0 :     if( Check( 2 ) )
     185             :     {
     186           0 :         *pCur++ = (char) ( n & 0xFF );
     187           0 :         *pCur++ = (char) ( n >> 8 );
     188           0 :         nOff += 2;
     189           0 :         return true;
     190             :     }
     191             :     else
     192             :     {
     193           0 :         return false;
     194             :     }
     195             : }
     196             : 
     197         660 : bool SbiBuffer::operator +=( sal_uInt16 n )
     198             : {
     199         660 :     if( Check( 2 ) )
     200             :     {
     201         660 :         *pCur++ = (char) ( n & 0xFF );
     202         660 :         *pCur++ = (char) ( n >> 8 );
     203         660 :         nOff += 2;
     204         660 :         return true;
     205             :     }
     206             :     else
     207             :     {
     208           0 :         return false;
     209             :     }
     210             : }
     211             : 
     212         330 : bool SbiBuffer::operator +=( sal_uInt32 n )
     213             : {
     214         330 :     if( Check( 4 ) )
     215             :     {
     216         330 :         sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF );
     217         330 :         sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 );
     218         330 :         if ( operator +=( n1 ) && operator +=( n2 ) )
     219             :         {
     220         330 :             return true;
     221             :         }
     222           0 :         return true;
     223             :     }
     224             :     else
     225             :     {
     226           0 :         return false;
     227             :     }
     228             : }
     229             : 
     230           0 : bool SbiBuffer::operator +=( sal_Int32 n )
     231             : {
     232           0 :     return operator +=( (sal_uInt32) n );
     233             : }
     234             : 
     235             : 
     236           0 : bool SbiBuffer::operator +=( const OUString& n )
     237             : {
     238           0 :     sal_uInt32 len = n.getLength() + 1;
     239           0 :     if( Check( len ) )
     240             :     {
     241           0 :         rtl::OString aByteStr(rtl::OUStringToOString(n, osl_getThreadTextEncoding()));
     242           0 :         memcpy( pCur, aByteStr.getStr(), len );
     243           0 :         pCur += len;
     244           0 :         nOff += len;
     245           0 :         return true;
     246             :     }
     247             :     else
     248             :     {
     249           0 :         return false;
     250             :     }
     251             : }
     252             : 
     253             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10