LCOV - code coverage report
Current view: top level - libreoffice/io/source/stm - streamhelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 103 0.0 %
Date: 2012-12-27 Functions: 0 13 0.0 %
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 <rtl/alloc.h>
      21             : 
      22             : #include <limits>
      23             : #include <string.h>
      24             : 
      25             : #include <com/sun/star/uno/Sequence.hxx>
      26             : 
      27             : #include <com/sun/star/uno/Exception.hpp>
      28             : 
      29             : using namespace ::com::sun::star::uno;
      30             : 
      31             : #include "streamhelper.hxx"
      32             : 
      33             : namespace io_stm {
      34             : 
      35           0 : void MemFIFO::write( const Sequence< sal_Int8 > &seq )
      36             :     throw ( I_FIFO_OutOfMemoryException,
      37             :             I_FIFO_OutOfBoundsException )
      38             : {
      39             :     try
      40             :     {
      41           0 :         writeAt(getSize(), seq );
      42             :     }
      43           0 :     catch( IRingBuffer_OutOfMemoryException & )
      44             :     {
      45           0 :         throw I_FIFO_OutOfMemoryException();
      46             :     }
      47           0 :     catch( IRingBuffer_OutOfBoundsException & )
      48             :     {
      49           0 :         throw I_FIFO_OutOfBoundsException();
      50             :     }
      51           0 : }
      52             : 
      53           0 : void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen ) throw (I_FIFO_OutOfBoundsException)
      54             : {
      55             :     try
      56             :     {
      57           0 :         readAt(0, seq , nBufferLen);
      58           0 :         forgetFromStart( nBufferLen );
      59             :     }
      60           0 :     catch ( IRingBuffer_OutOfBoundsException & )
      61             :     {
      62           0 :         throw I_FIFO_OutOfBoundsException();
      63             :     }
      64           0 : }
      65             : 
      66           0 : void MemFIFO::skip( sal_Int32 nBytesToSkip ) throw ( I_FIFO_OutOfBoundsException )
      67             : {
      68             :     try
      69             :     {
      70           0 :         forgetFromStart( nBytesToSkip );
      71             :     }
      72           0 :     catch( IRingBuffer_OutOfBoundsException & )
      73             :     {
      74           0 :         throw I_FIFO_OutOfBoundsException();
      75             :     }
      76           0 : }
      77             : 
      78             : 
      79             : 
      80           0 : MemRingBuffer::MemRingBuffer()
      81             : {
      82           0 :     m_nBufferLen            = 0;
      83           0 :     m_p                     = 0;
      84           0 :     m_nStart                = 0;
      85           0 :     m_nOccupiedBuffer       = 0;
      86           0 : }
      87             : 
      88           0 : MemRingBuffer::~MemRingBuffer()
      89             : {
      90           0 :     if( m_p ) {
      91           0 :         rtl_freeMemory( m_p );
      92             :     }
      93           0 : }
      94             : 
      95           0 : void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException)
      96             : {
      97           0 :     sal_Int32 nNewLen = 1;
      98             : 
      99           0 :     while( nMinSize > nNewLen ) {
     100           0 :         nNewLen = nNewLen << 1;
     101             :     }
     102             : 
     103             :     // buffer never shrinks !
     104           0 :     if( nNewLen < m_nBufferLen ) {
     105           0 :         nNewLen = m_nBufferLen;
     106             :     }
     107             : 
     108           0 :     if( nNewLen != m_nBufferLen ) {
     109           0 :         m_p = ( sal_Int8 * ) rtl_reallocateMemory( m_p , nNewLen );
     110           0 :         if( !m_p ) {
     111           0 :             throw IRingBuffer_OutOfMemoryException();
     112             :         }
     113             : 
     114           0 :         if( m_nStart + m_nOccupiedBuffer > m_nBufferLen ) {
     115           0 :             memmove( &( m_p[m_nStart+(nNewLen-m_nBufferLen)]) , &(m_p[m_nStart]) , m_nBufferLen - m_nStart );
     116           0 :             m_nStart += nNewLen - m_nBufferLen;
     117             :         }
     118           0 :         m_nBufferLen = nNewLen;
     119             :     }
     120           0 : }
     121             : 
     122             : 
     123           0 : void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const
     124             :                                                         throw(IRingBuffer_OutOfBoundsException)
     125             : {
     126           0 :     if( nPos + nBytesToRead > m_nOccupiedBuffer ) {
     127           0 :         throw IRingBuffer_OutOfBoundsException();
     128             :     }
     129             : 
     130           0 :     sal_Int32 nStartReadingPos = nPos + m_nStart;
     131           0 :     if( nStartReadingPos >= m_nBufferLen ) {
     132           0 :         nStartReadingPos -= m_nBufferLen;
     133             :     }
     134             : 
     135           0 :     seq.realloc( nBytesToRead );
     136             : 
     137           0 :     if( nStartReadingPos + nBytesToRead > m_nBufferLen ) {
     138           0 :         sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos;
     139           0 :         memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen );
     140           0 :         memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen );
     141             :     }
     142             :     else {
     143           0 :         memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead );
     144             :     }
     145           0 : }
     146             : 
     147             : 
     148           0 : void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq )
     149             :                                                         throw (IRingBuffer_OutOfBoundsException,
     150             :                                                                 IRingBuffer_OutOfMemoryException )
     151             : {
     152           0 :     checkInvariants();
     153           0 :     sal_Int32 nLen = seq.getLength();
     154             : 
     155           0 :     if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen )
     156             :     {
     157           0 :         throw IRingBuffer_OutOfBoundsException();
     158             :     }
     159             : 
     160           0 :     if( nPos + nLen - m_nOccupiedBuffer > 0 ) {
     161           0 :         resizeBuffer( nPos + seq.getLength() );
     162             :     }
     163             : 
     164           0 :     sal_Int32 nStartWritingIndex = m_nStart + nPos;
     165           0 :     if( nStartWritingIndex >= m_nBufferLen ) {
     166           0 :         nStartWritingIndex -= m_nBufferLen;
     167             :     }
     168             : 
     169           0 :     if( nLen + nStartWritingIndex > m_nBufferLen ) {
     170             :         // two area copy
     171           0 :         memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), m_nBufferLen-nStartWritingIndex );
     172           0 :         memcpy( m_p , &( seq.getConstArray()[m_nBufferLen-nStartWritingIndex] ),
     173           0 :                                         nLen - (m_nBufferLen-nStartWritingIndex) );
     174             : 
     175             :     }
     176             :     else {
     177             :         // one area copy
     178           0 :         memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen );
     179             :     }
     180           0 :     m_nOccupiedBuffer = Max( nPos + seq.getLength() , m_nOccupiedBuffer );
     181           0 :     checkInvariants();
     182           0 : }
     183             : 
     184             : 
     185           0 : sal_Int32 MemRingBuffer::getSize()  const throw()
     186             : {
     187           0 :     return m_nOccupiedBuffer;
     188             : }
     189             : 
     190           0 : void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
     191             : {
     192           0 :     checkInvariants();
     193           0 :     if( nBytesToForget > m_nOccupiedBuffer ) {
     194           0 :         throw IRingBuffer_OutOfBoundsException();
     195             :     }
     196           0 :     m_nStart += nBytesToForget;
     197           0 :     if( m_nStart >= m_nBufferLen ) {
     198           0 :         m_nStart = m_nStart - m_nBufferLen;
     199             :     }
     200           0 :     m_nOccupiedBuffer -= nBytesToForget;
     201           0 :     checkInvariants();
     202           0 : }
     203             : 
     204             : 
     205           0 : void MemRingBuffer::forgetFromEnd( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
     206             : {
     207           0 :     checkInvariants();
     208           0 :     if( nBytesToForget > m_nOccupiedBuffer ) {
     209           0 :         throw IRingBuffer_OutOfBoundsException();
     210             :     }
     211           0 :     m_nOccupiedBuffer -= nBytesToForget;
     212           0 :     checkInvariants();
     213           0 : }
     214             : 
     215             : 
     216           0 : void MemRingBuffer::shrink() throw ()
     217             : {
     218           0 :     checkInvariants();
     219             : 
     220             :     // Up to now, only shrinking of while buffer works.
     221             :     // No other shrinking supported up to now.
     222           0 :     if( ! m_nOccupiedBuffer ) {
     223           0 :         if( m_p ) {
     224           0 :             free( m_p );
     225             :         }
     226           0 :         m_p = 0;
     227           0 :         m_nBufferLen = 0;
     228           0 :         m_nStart = 0;
     229             :     }
     230             : 
     231           0 :     checkInvariants();
     232           0 : }
     233             : 
     234             : }
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10