LCOV - code coverage report
Current view: top level - frmts/mrsid - mrsidstream.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 60 93 64.5 %
Date: 2024-05-07 17:03:27 Functions: 10 16 62.5 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Multi-resolution Seamless Image Database (MrSID)
       4             :  * Purpose:  Input/output stream wrapper for usage with LizardTech's
       5             :  *           MrSID SDK, implementation of the wrapper class methods.
       6             :  * Author:   Andrey Kiselev, dron@ak4719.spb.edu
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2008, Andrey Kiselev <dron@ak4719.spb.edu>
      10             :  * Copyright (c) 2008-2010, Even Rouault <even dot rouault at spatialys.com>
      11             :  *
      12             :  * Permission is hereby granted, free of charge, to any person obtaining a
      13             :  * copy of this software and associated documentation files (the "Software"),
      14             :  * to deal in the Software without restriction, including without limitation
      15             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16             :  * and/or sell copies of the Software, and to permit persons to whom the
      17             :  * Software is furnished to do so, subject to the following conditions:
      18             :  *
      19             :  * The above copyright notice and this permission notice shall be included
      20             :  * in all copies or substantial portions of the Software.
      21             :  *
      22             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28             :  * DEALINGS IN THE SOFTWARE.
      29             :  ****************************************************************************/
      30             : 
      31             : #include "cpl_error.h"
      32             : #include "mrsidstream.h"
      33             : 
      34             : using namespace LizardTech;
      35             : 
      36             : /************************************************************************/
      37             : /* ==================================================================== */
      38             : /*                              LTIVSIStream                             */
      39             : /* ==================================================================== */
      40             : /************************************************************************/
      41             : 
      42          62 : LTIVSIStream::LTIVSIStream()
      43          62 :     : poFileHandle(nullptr), nError(0), pnRefCount(nullptr), bIsOpen(FALSE)
      44             : {
      45          62 : }
      46             : 
      47             : /************************************************************************/
      48             : /*                             ~LTIVSIStream()                           */
      49             : /************************************************************************/
      50             : 
      51          62 : LTIVSIStream::~LTIVSIStream()
      52             : {
      53          62 :     if (poFileHandle)
      54             :     {
      55           3 :         (*pnRefCount)--;
      56           3 :         if (*pnRefCount == 0)
      57             :         {
      58           3 :             VSIFCloseL((VSILFILE *)poFileHandle);
      59           3 :             nError = errno;
      60           3 :             delete pnRefCount;
      61             :         }
      62             :     }
      63          62 : }
      64             : 
      65             : /************************************************************************/
      66             : /*                              initialize()                            */
      67             : /************************************************************************/
      68             : 
      69           3 : LT_STATUS LTIVSIStream::initialize(const char *pszFilename,
      70             :                                    const char *pszAccess)
      71             : {
      72           3 :     CPLAssert(poFileHandle == nullptr);
      73             : 
      74           3 :     errno = 0;
      75           3 :     poFileHandle = (VSIVirtualHandle *)VSIFOpenL(pszFilename, pszAccess);
      76           3 :     if (poFileHandle)
      77             :     {
      78           3 :         pnRefCount = new int;
      79           3 :         *pnRefCount = 1;
      80             :     }
      81           3 :     nError = errno;
      82             : 
      83           3 :     return poFileHandle ? LT_STS_Success : LT_STS_Failure;
      84             : }
      85             : 
      86             : /************************************************************************/
      87             : /*                              initialize()                            */
      88             : /************************************************************************/
      89             : 
      90           0 : LT_STATUS LTIVSIStream::initialize(LTIVSIStream *ltiVSIStream)
      91             : {
      92           0 :     CPLAssert(poFileHandle == nullptr);
      93             : 
      94           0 :     poFileHandle = ltiVSIStream->poFileHandle;
      95           0 :     if (poFileHandle)
      96             :     {
      97           0 :         pnRefCount = ltiVSIStream->pnRefCount;
      98           0 :         (*pnRefCount)++;
      99             :     }
     100             : 
     101           0 :     return poFileHandle ? LT_STS_Success : LT_STS_Failure;
     102             : }
     103             : 
     104             : /************************************************************************/
     105             : /*                                 isEOF()                              */
     106             : /************************************************************************/
     107             : 
     108           0 : bool LTIVSIStream::isEOF()
     109             : {
     110           0 :     CPLAssert(poFileHandle);
     111             : 
     112           0 :     errno = 0;
     113           0 :     bool bIsEOF = (0 != poFileHandle->Eof());
     114           0 :     nError = errno;
     115             : 
     116           0 :     return bIsEOF;
     117             : }
     118             : 
     119             : /************************************************************************/
     120             : /*                                 isOpen()                             */
     121             : /************************************************************************/
     122             : 
     123          16 : bool LTIVSIStream::isOpen()
     124             : {
     125          16 :     return poFileHandle != nullptr && bIsOpen;
     126             : }
     127             : 
     128             : /************************************************************************/
     129             : /*                                  open()                              */
     130             : /************************************************************************/
     131             : 
     132           8 : LT_STATUS LTIVSIStream::open()
     133             : {
     134           8 :     bIsOpen = poFileHandle != nullptr;
     135           8 :     return poFileHandle ? LT_STS_Success : LT_STS_Failure;
     136             : }
     137             : 
     138             : /************************************************************************/
     139             : /*                                  close()                             */
     140             : /************************************************************************/
     141             : 
     142           8 : LT_STATUS LTIVSIStream::close()
     143             : {
     144           8 :     CPLAssert(poFileHandle);
     145             : 
     146           8 :     bIsOpen = FALSE;
     147           8 :     errno = 0;
     148           8 :     if (poFileHandle->Seek(0, SEEK_SET) == 0)
     149           8 :         return LT_STS_Success;
     150             :     else
     151             :     {
     152           0 :         nError = errno;
     153           0 :         return LT_STS_Failure;
     154             :     }
     155             : }
     156             : 
     157             : /************************************************************************/
     158             : /*                                   read()                             */
     159             : /************************************************************************/
     160             : 
     161        4101 : lt_uint32 LTIVSIStream::read(lt_uint8 *pDest, lt_uint32 nBytes)
     162             : {
     163        4101 :     CPLAssert(poFileHandle);
     164             : 
     165        4101 :     errno = 0;
     166        4101 :     lt_uint32 nBytesRead = (lt_uint32)poFileHandle->Read(pDest, 1, nBytes);
     167        4101 :     nError = errno;
     168             : 
     169        4101 :     return nBytesRead;
     170             : }
     171             : 
     172             : /************************************************************************/
     173             : /*                                  write()                             */
     174             : /************************************************************************/
     175             : 
     176           0 : lt_uint32 LTIVSIStream::write(const lt_uint8 *pSrc, lt_uint32 nBytes)
     177             : {
     178           0 :     CPLAssert(poFileHandle);
     179             : 
     180           0 :     errno = 0;
     181           0 :     lt_uint32 nBytesWritten = (lt_uint32)poFileHandle->Write(pSrc, 1, nBytes);
     182           0 :     nError = errno;
     183             : 
     184           0 :     return nBytesWritten;
     185             : }
     186             : 
     187             : /************************************************************************/
     188             : /*                                   seek()                             */
     189             : /************************************************************************/
     190             : 
     191          72 : LT_STATUS LTIVSIStream::seek(lt_int64 nOffset, LTIOSeekDir nOrigin)
     192             : {
     193          72 :     CPLAssert(poFileHandle);
     194             : 
     195             :     int nWhence;
     196          72 :     switch (nOrigin)
     197             :     {
     198          65 :         case (LTIO_SEEK_DIR_BEG):
     199          65 :             nWhence = SEEK_SET;
     200          65 :             break;
     201             : 
     202           4 :         case (LTIO_SEEK_DIR_CUR):
     203             :         {
     204           4 :             nWhence = SEEK_CUR;
     205           4 :             if (nOffset < 0)
     206             :             {
     207           0 :                 nWhence = SEEK_SET;
     208           0 :                 nOffset += (lt_int64)poFileHandle->Tell();
     209             :             }
     210           4 :             break;
     211             :         }
     212             : 
     213           3 :         case (LTIO_SEEK_DIR_END):
     214           3 :             nWhence = SEEK_END;
     215           3 :             break;
     216             : 
     217           0 :         default:
     218           0 :             return LT_STS_Failure;
     219             :     }
     220             : 
     221          72 :     if (poFileHandle->Seek((vsi_l_offset)nOffset, nWhence) == 0)
     222          72 :         return LT_STS_Success;
     223             :     else
     224             :     {
     225           0 :         nError = errno;
     226           0 :         return LT_STS_Failure;
     227             :     }
     228             : }
     229             : 
     230             : /************************************************************************/
     231             : /*                               tell()                                 */
     232             : /************************************************************************/
     233             : 
     234          28 : lt_int64 LTIVSIStream::tell()
     235             : {
     236          28 :     CPLAssert(poFileHandle);
     237             : 
     238          28 :     errno = 0;
     239          28 :     lt_int64 nPos = (lt_int64)poFileHandle->Tell();
     240          28 :     nError = errno;
     241             : 
     242          28 :     return nPos;
     243             : }
     244             : 
     245             : /************************************************************************/
     246             : /*                               duplicate()                            */
     247             : /************************************************************************/
     248             : 
     249           0 : LTIOStreamInf *LTIVSIStream::duplicate()
     250             : {
     251           0 :     LTIVSIStream *poNew = new LTIVSIStream;
     252           0 :     poNew->initialize(this);
     253             : 
     254           0 :     return poNew;
     255             : }
     256             : 
     257             : /************************************************************************/
     258             : /*                             getLastError()                           */
     259             : /************************************************************************/
     260             : 
     261           0 : LT_STATUS LTIVSIStream::getLastError() const
     262             : {
     263           0 :     return nError;
     264             : }
     265             : 
     266             : /************************************************************************/
     267             : /*                                  getID()                             */
     268             : /************************************************************************/
     269             : 
     270           2 : const char *LTIVSIStream::getID() const
     271             : {
     272           2 :     return "LTIVSIStream:";
     273             : }

Generated by: LCOV version 1.14