LCOV - code coverage report
Current view: top level - util - CubitFileIOWrapper.cpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 81 142 57.0 %
Date: 2020-06-30 00:58:45 Functions: 14 20 70.0 %
Branches: 39 106 36.8 %

           Branch data     Line data    Source code
       1                 :            : /*******************************************************************************
       2                 :            :     COPYRIGHT 2002 CATERPILLAR INC.  ALL RIGHTS RESERVED
       3                 :            : 
       4                 :            :     This program is the property of Caterpillar Inc., includes Caterpillar's
       5                 :            :     confidential and trade secret information, and is maintained as an
       6                 :            :     unpublished copyrighted work.  It is not to be copied or used by others
       7                 :            :     except under license from Caterpillar.  This program is also protected as an
       8                 :            :     unpublished work in accordance with the copyright act of 1976.  In the event
       9                 :            :     of either inadvertent or deliberate publication, Caterpillar Inc. intends to
      10                 :            :     maintain copyright protection for this work under the relevant copyright
      11                 :            :     laws pertaining to published works.  The inclusion of a copyright notice
      12                 :            :     hereon is precautionary only, and does not imply publication or disclosure.
      13                 :            : 
      14                 :            :  
      15                 :            :  Filename      : CubitFileIOWrapper.cpp
      16                 :            : 
      17                 :            :  Purpose       : Encapsulates file I/O operations for the Cubit file.
      18                 :            :            
      19                 :            :  Special Notes :
      20                 :            : 
      21                 :            :  Creator       : Will A. Helden
      22                 :            : 
      23                 :            :  Creation Date : 02/15/02
      24                 :            : 
      25                 :            :  Owner         : Will A. Helden
      26                 :            : 
      27                 :            : *******************************************************************************/
      28                 :            : 
      29                 :            : #include "CubitFileIOWrapper.hpp"
      30                 :            : #include <cstring>
      31                 :            : 
      32                 :            : using namespace NCubitFile;
      33                 :            : 
      34                 :            : ///////////////////////////////////////////////////////////////////////////////
      35                 :            : // CIOWrapper -
      36                 :            : ///////////////////////////////////////////////////////////////////////////////
      37                 :            : 
      38                 :       3168 : CIOWrapper::CIOWrapper(FILE* xpFile, UnsignedInt32 xintSourceEndian)
      39                 :            : {
      40         [ -  + ]:       3168 :     if(!xpFile)
      41                 :          0 :         throw CCubitFile::ePassedNullPointer;
      42                 :       3168 :     mpFile = xpFile;
      43                 :       3168 :     mintSwapEndian = (xintSourceEndian != CCubitFile::mintNativeEndian);
      44                 :       3168 :     mintBlockStart = mintBlockEnd = 0;
      45                 :       3168 : }
      46                 :            : 
      47                 :       2057 : CIOWrapper::CIOWrapper(UnsignedInt32 swap_endian, FILE* xpFile )
      48                 :            : {
      49         [ -  + ]:       2057 :     if(!xpFile)
      50                 :          0 :         throw CCubitFile::ePassedNullPointer;
      51                 :       2057 :     mpFile = xpFile;
      52                 :       2057 :     mintSwapEndian = swap_endian;
      53                 :       2057 :     mintBlockStart = mintBlockEnd = 0;
      54                 :       2057 : }
      55                 :            : 
      56                 :            : 
      57                 :         11 : CIOWrapper::CIOWrapper(FILE* xpFile, UnsignedInt32 xintAbsoluteOffset,
      58                 :         11 :                        UnsignedInt32 xintRelativeOffset)
      59                 :            : {
      60         [ -  + ]:         11 :     if(!xpFile)
      61                 :          0 :         throw CCubitFile::ePassedNullPointer;
      62                 :         11 :     mpFile = xpFile;
      63                 :         11 :     mintBlockStart = mintBlockEnd = 0;
      64                 :            : 
      65 [ +  - ][ -  + ]:         11 :     if(NCubitFile::SetLocation(mpFile, xintAbsoluteOffset + xintRelativeOffset, SEEK_SET))
      66                 :          0 :         throw CCubitFile::eFileSeekError;
      67                 :            :     UnsignedInt32 lintSourceEndian;
      68 [ +  - ][ -  + ]:         11 :     if(fread(&lintSourceEndian, sizeof(UnsignedInt32), 1, mpFile) != 1)
      69                 :          0 :         throw CCubitFile::eFileReadError;
      70                 :         11 :     mintSwapEndian = (lintSourceEndian != CCubitFile::mintNativeEndian);
      71                 :         11 : }
      72                 :            : 
      73                 :      10472 : CIOWrapper::~CIOWrapper()
      74                 :            : {
      75         [ -  + ]:       5236 : }
      76                 :            : 
      77                 :            :         
      78                 :         55 : UnsignedInt32 CIOWrapper::BeginWriteBlock(UnsignedInt32 xintAbsoluteOffset)
      79                 :            : // Begin the write of a continuous data block, return the location in the file
      80                 :            : // where the block begins as a relative offset from the passed value (or
      81                 :            : // absolute offset if the passed offset is 0).
      82                 :            : {
      83         [ -  + ]:         55 :     if(NCubitFile::SetLocation(mpFile, 0, SEEK_END))
      84                 :          0 :         throw CCubitFile::eFileSeekError;
      85                 :            :     UnsignedInt32 pos;
      86                 :         55 :     mintBlockEnd = mintBlockStart = pos = NCubitFile::GetLocation(mpFile);
      87                 :         55 :     return mintBlockStart - xintAbsoluteOffset;
      88                 :            : }
      89                 :            : 
      90                 :          0 : void CIOWrapper::BeginRewriteBlock(UnsignedInt32 xintAbsoluteOffset,
      91                 :            :                                    UnsignedInt32 xintRelativeOffset)
      92                 :            : {
      93                 :          0 :     mintBlockEnd = mintBlockStart = xintAbsoluteOffset + xintRelativeOffset;
      94         [ #  # ]:          0 :     if(NCubitFile::SetLocation(mpFile, mintBlockStart, SEEK_SET))
      95                 :          0 :         throw CCubitFile::eFileSeekError;
      96                 :          0 : }
      97                 :            : 
      98                 :      14476 : void CIOWrapper::Write(const UnsignedInt32* xpaintData, UnsignedInt32 xintCount)
      99                 :            : {
     100         [ -  + ]:      14476 :     if(fwrite(xpaintData, sizeof(UnsignedInt32), xintCount, mpFile) != xintCount)
     101                 :          0 :         throw CCubitFile::eFileWriteError;
     102                 :      14476 :     mintBlockEnd += (sizeof(UnsignedInt32) * xintCount);
     103                 :      14476 : }
     104                 :            : 
     105                 :         55 : void CIOWrapper::Write(const char* xpachrData, UnsignedInt32 xintCount,
     106                 :            :                        UnsignedInt32 xint32bitPadded)
     107                 :            : {
     108         [ +  - ]:         55 :     if(xintCount) {
     109         [ -  + ]:         55 :         if(fwrite(xpachrData, sizeof(char), xintCount, mpFile) != xintCount)
     110                 :          0 :             throw CCubitFile::eFileWriteError;
     111                 :            :     }
     112                 :         55 :     UnsignedInt32 lintMod = 0;
     113         [ -  + ]:         55 :     if(xint32bitPadded) {
     114                 :          0 :         lintMod = (xintCount % sizeof(UnsignedInt32));
     115         [ #  # ]:          0 :         if(lintMod) {
     116                 :          0 :             lintMod = sizeof(UnsignedInt32) - lintMod;
     117         [ #  # ]:          0 :             if(fwrite("\0\0\0\0", sizeof(char), lintMod, mpFile) != lintMod)
     118                 :          0 :                 throw CCubitFile::eFileWriteError;
     119                 :            :         }
     120                 :            :     }
     121                 :         55 :     mintBlockEnd += (sizeof(char) * (xintCount + lintMod));
     122                 :         55 : }
     123                 :            : 
     124                 :       2321 : void CIOWrapper::Write(const double* xpadblData, UnsignedInt32 xintCount)
     125                 :            : {
     126         [ -  + ]:       2321 :     if(fwrite(xpadblData, sizeof(double), xintCount, mpFile) != xintCount)
     127                 :          0 :         throw CCubitFile::eFileWriteError;
     128                 :       2321 :     mintBlockEnd += (sizeof(double) * xintCount);
     129                 :       2321 : }
     130                 :            : 
     131                 :       1012 : void CIOWrapper::Write(const char* xpachrData)
     132                 :            : // Write a null terminated string to the data block, the strings length is 
     133                 :            : // written first, followed by the data and padding to keep the file pointer on
     134                 :            : // a word boundary (for ease of debugging).
     135                 :            : {
     136         [ +  - ]:       1012 :     UnsignedInt32 lintLength = xpachrData ? std::strlen(xpachrData) : 0;
     137                 :       1012 :     UnsignedInt32 lintMod = (lintLength % sizeof(UnsignedInt32));
     138 [ +  - ][ -  + ]:       1012 :     if(fwrite(&lintLength, sizeof(UnsignedInt32), 1, mpFile) != 1)
     139                 :          0 :         throw CCubitFile::eFileWriteError;
     140         [ +  - ]:       1012 :     if(lintLength) {
     141 [ +  - ][ -  + ]:       1012 :         if(fwrite(xpachrData, sizeof(char), lintLength, mpFile) != lintLength)
     142                 :          0 :             throw CCubitFile::eFileWriteError;
     143         [ +  - ]:       1012 :         if(lintMod) {
     144                 :       1012 :             lintMod = sizeof(UnsignedInt32) - lintMod;
     145 [ +  - ][ -  + ]:       1012 :             if(fwrite("\0\0\0\0", sizeof(char),  lintMod, mpFile) != lintMod)
     146                 :          0 :                 throw CCubitFile::eFileWriteError;
     147                 :            :         }
     148                 :            :     }
     149                 :            :     mintBlockEnd +=
     150                 :       1012 :         (sizeof(UnsignedInt32) + sizeof(char) * (lintLength + lintMod));
     151                 :       1012 : }
     152                 :            : 
     153                 :          0 : UnsignedInt32 CIOWrapper::EndWriteBlock()
     154                 :            : // Completes the writing of a contiguous data block, checks for errors and
     155                 :            : // returns the length of the block.
     156                 :            : {
     157                 :          0 :     UnsignedInt32 lintBlockEnd = NCubitFile::GetLocation(mpFile);
     158         [ #  # ]:          0 :     if((UnsignedInt32)lintBlockEnd != mintBlockEnd)
     159                 :          0 :         throw CCubitFile::eCorruptBlock;
     160                 :          0 :     UnsignedInt32 lintLength = mintBlockEnd - mintBlockStart;
     161                 :          0 :     mintBlockEnd = mintBlockStart = 0;
     162                 :          0 :     return lintLength;
     163                 :            : }
     164                 :            : 
     165                 :            : 
     166                 :          0 : void CIOWrapper::BeginReadBlock(UnsignedInt32 xintAbsoluteOffset,
     167                 :            :                                 UnsignedInt32 xintRelativeOffset)
     168                 :            : // Begin the read of a contiguous data block.
     169                 :            : {
     170         [ #  # ]:          0 :     if(NCubitFile::SetLocation(mpFile, xintAbsoluteOffset + xintRelativeOffset, SEEK_SET))
     171                 :          0 :         throw CCubitFile::eFileSeekError;
     172                 :          0 : }
     173                 :            : 
     174                 :       7810 : void CIOWrapper::Read(UnsignedInt32* xpaintData, UnsignedInt32 xintCount)
     175                 :            : {
     176         [ -  + ]:       7810 :     if(fread(xpaintData, sizeof(UnsignedInt32), xintCount, mpFile) != xintCount)
     177                 :          0 :         throw CCubitFile::eFileReadError;
     178         [ -  + ]:       7810 :     if(mintSwapEndian)
     179                 :          0 :         SwapEndian<UnsignedInt32>(xintCount, xpaintData);
     180                 :       7810 : }
     181                 :            : 
     182                 :          0 : void CIOWrapper::Read(char* xpachrData, UnsignedInt32 xintCount,
     183                 :            :                       UnsignedInt32 xint32bitPadded)
     184                 :            : {
     185         [ #  # ]:          0 :     if(xintCount) {
     186         [ #  # ]:          0 :         if(fread(xpachrData, sizeof(char), xintCount, mpFile) != xintCount)
     187                 :          0 :             throw CCubitFile::eFileReadError;
     188                 :            :     }
     189         [ #  # ]:          0 :     if(xint32bitPadded) {
     190                 :            :         char lachrPad[8]; // , *lpachrData = NULL;
     191                 :          0 :         UnsignedInt32 lintMod = (xintCount % sizeof(UnsignedInt32));
     192         [ #  # ]:          0 :         if(lintMod) {
     193                 :          0 :             lintMod = sizeof(UnsignedInt32) - lintMod;
     194 [ #  # ][ #  # ]:          0 :             if(fread(&lachrPad, sizeof(char), lintMod, mpFile) != lintMod)
     195                 :          0 :                 throw CCubitFile::eFileReadError;
     196                 :            :         }
     197                 :            :     }
     198                 :          0 : }
     199                 :            : 
     200                 :       1474 : void CIOWrapper::Read(double* xpadblData, UnsignedInt32 xintCount)
     201                 :            : {
     202         [ -  + ]:       1474 :     if(fread(xpadblData, sizeof(double), xintCount, mpFile) != xintCount)
     203                 :          0 :         throw CCubitFile::eFileReadError;
     204         [ -  + ]:       1474 :     if(mintSwapEndian)
     205                 :          0 :         SwapEndian<double>(xintCount, xpadblData);
     206                 :       1474 : }
     207                 :            : 
     208                 :       1012 : char* CIOWrapper::Read()
     209                 :            : // Read a null terminated string from the data block, the string's length is 
     210                 :            : // read first, followed by the data and then any padding.
     211                 :            : {
     212                 :            :     UnsignedInt32 lintLength, lintMod;
     213                 :       1012 :     char lachrPad[8], *lpachrData = NULL;
     214 [ +  - ][ -  + ]:       1012 :     if(fread(&lintLength, sizeof(UnsignedInt32), 1, mpFile) != 1)
     215                 :          0 :         throw CCubitFile::eFileReadError;
     216         [ -  + ]:       1012 :     if(mintSwapEndian)
     217         [ #  # ]:          0 :         SwapEndian<UnsignedInt32>(1, &lintLength);
     218         [ +  - ]:       1012 :     if(lintLength) {
     219         [ +  - ]:       1012 :         lpachrData = new char[lintLength + 1];
     220         [ -  + ]:       1012 :         if(!lpachrData)
     221                 :          0 :             throw CCubitFile::eMemoryError;
     222 [ +  - ][ -  + ]:       1012 :         if(fread(lpachrData, sizeof(char), lintLength, mpFile) != lintLength) {
     223         [ #  # ]:          0 :                     delete[] lpachrData;
     224                 :          0 :             throw CCubitFile::eFileReadError;
     225                 :            :                 }       
     226                 :       1012 :         lpachrData[lintLength] = '\0';
     227                 :       1012 :         lintMod = (lintLength % sizeof(UnsignedInt32));
     228         [ +  - ]:       1012 :         if(lintMod) {
     229                 :       1012 :             lintMod = sizeof(UnsignedInt32) - lintMod;
     230 [ +  - ][ -  + ]:       1012 :             if(fread(&lachrPad, sizeof(char), lintMod, mpFile) != lintMod) {
     231         [ #  # ]:          0 :                                 delete[] lpachrData;
     232                 :          0 :                 throw CCubitFile::eFileReadError;
     233                 :            :                         }
     234                 :            :         }
     235                 :            :     }
     236                 :       1012 :     return lpachrData;
     237                 :            : }
     238                 :            : 
     239                 :          0 : void CIOWrapper::EndReadBlock()
     240                 :            : {
     241                 :          0 :     mintBlockStart = mintBlockEnd = 0;
     242                 :          0 : }
     243                 :            :     
     244                 :         55 : UnsignedInt32 NCubitFile::GetLocation(FILE* f)
     245                 :            : {
     246                 :            : #ifdef _MSC_VER
     247                 :            :   // normal ftell() returns long, which is a 32 bit signed integer.
     248                 :            :   // we use this to increase our 2 GB limit to 4 GB.
     249                 :            :   // To go past 4GB, we'd have to use a 64 bit integer instead of UnsignedInt32.
     250                 :            :   __int64 offset = _ftelli64(f);
     251                 :            : #else
     252                 :         55 :   long offset = ftell(f);
     253                 :            : #endif
     254         [ -  + ]:         55 :   if(offset == -1L)
     255                 :          0 :     throw CCubitFile::eFileTellError;
     256                 :         55 :   return static_cast<UnsignedInt32>(offset);
     257                 :            : }
     258                 :            : 
     259                 :          0 : UnsignedInt32 CIOWrapper::GetLocation()
     260                 :            : {
     261                 :          0 :   return NCubitFile::GetLocation(mpFile);
     262                 :            : }
     263                 :            : 
     264                 :         66 : int NCubitFile::SetLocation(FILE* f, UnsignedInt32 offset, int whence)
     265                 :            : {
     266                 :            : #ifdef _MSC_VER
     267                 :            :   return _fseeki64(f, offset, whence);
     268                 :            : #else
     269                 :         66 :   return fseek(f, offset, whence);
     270                 :            : #endif
     271                 :            : }

Generated by: LCOV version 1.11