LTP GCOV extension - code coverage report
Current view: directory - frmts/pcidsk/sdk/core - pcidskbuffer.cpp
Test: gdal_filtered.info
Date: 2010-07-12 Instrumented lines: 85
Code covered: 84.7 % Executed lines: 72

       1                 : /******************************************************************************
       2                 :  *
       3                 :  * Purpose:  Implementation of the PCIDSKBuffer class.  This class is for
       4                 :  *           convenient parsing and formatting of PCIDSK ASCII headers.
       5                 :  * 
       6                 :  ******************************************************************************
       7                 :  * Copyright (c) 2009
       8                 :  * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
       9                 :  *
      10                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      11                 :  * copy of this software and associated documentation files (the "Software"),
      12                 :  * to deal in the Software without restriction, including without limitation
      13                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      14                 :  * and/or sell copies of the Software, and to permit persons to whom the
      15                 :  * Software is furnished to do so, subject to the following conditions:
      16                 :  *
      17                 :  * The above copyright notice and this permission notice shall be included
      18                 :  * in all copies or substantial portions of the Software.
      19                 :  *
      20                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      21                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      22                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      23                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      24                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      25                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      26                 :  * DEALINGS IN THE SOFTWARE.
      27                 :  ****************************************************************************/
      28                 : 
      29                 : #include "pcidsk_buffer.h"
      30                 : #include "pcidsk_exception.h"
      31                 : #include "core/pcidsk_utils.h"
      32                 : 
      33                 : #include <cstdlib>
      34                 : #include <cstring>
      35                 : #include <cstdio>
      36                 : 
      37                 : using namespace PCIDSK;
      38                 : 
      39                 : #ifdef _MSC_VER
      40                 : #ifndef snprintf
      41                 : #define snprintf _snprintf
      42                 : #endif // !defined(snprintf)
      43                 : #endif
      44                 : 
      45                 : /************************************************************************/
      46                 : /*                            PCIDSKBuffer()                            */
      47                 : /************************************************************************/
      48                 : 
      49            1227 : PCIDSKBuffer::PCIDSKBuffer( int size )
      50                 : 
      51                 : {
      52            1227 :     buffer_size = 0;
      53            1227 :     buffer = NULL;
      54                 : 
      55            1227 :     if( size > 0 )
      56             864 :         SetSize( size );
      57            1227 : }
      58                 : 
      59                 : /************************************************************************/
      60                 : /*                            PCIDSKBuffer()                            */
      61                 : /************************************************************************/
      62                 : 
      63             121 : PCIDSKBuffer::PCIDSKBuffer( const char *src, int size )
      64                 : 
      65                 : {
      66             121 :     buffer_size = 0;
      67             121 :     buffer = NULL;
      68                 : 
      69             121 :     SetSize( size );
      70             121 :     memcpy( buffer, src, size );
      71             121 : }
      72                 : 
      73                 : /************************************************************************/
      74                 : /*                           ~PCIDSKBuffer()                            */
      75                 : /************************************************************************/
      76                 : 
      77            1348 : PCIDSKBuffer::~PCIDSKBuffer()
      78                 : 
      79                 : {
      80            1348 :     free( buffer );
      81            1348 : }
      82                 : 
      83                 : /************************************************************************/
      84                 : /*                              SetSize()                               */
      85                 : /************************************************************************/
      86                 : 
      87            1669 : void PCIDSKBuffer::SetSize( int size )
      88                 : 
      89                 : {
      90            1669 :     buffer_size = size;
      91            1669 :     if( buffer == NULL )
      92            1348 :         buffer = (char *) malloc(size+1);
      93                 :     else
      94             321 :         buffer = (char *) realloc(buffer,size+1);
      95                 : 
      96            1669 :     if( buffer == NULL )
      97                 :     {
      98               0 :         buffer_size = 0;
      99                 :         ThrowPCIDSKException( "Out of memory allocating %d byte PCIDSKBuffer.",
     100               0 :                                size );
     101                 :     }
     102                 : 
     103            1669 :     buffer[size] = '\0';
     104            1669 : }
     105                 : 
     106                 : /************************************************************************/
     107                 : /*                                Get()                                 */
     108                 : /************************************************************************/
     109                 : 
     110            2338 : const char *PCIDSKBuffer::Get( int offset, int size ) const
     111                 : 
     112                 : {
     113            2338 :     Get( offset, size, work_field, 0 );
     114            2338 :     return work_field.c_str();
     115                 : }
     116                 : 
     117                 : /************************************************************************/
     118                 : /*                                Get()                                 */
     119                 : /************************************************************************/
     120                 : 
     121            6662 : void PCIDSKBuffer::Get( int offset, int size, std::string &target, int unpad ) const
     122                 : 
     123                 : {
     124            6662 :     if( offset + size > buffer_size )
     125               0 :         ThrowPCIDSKException( "Get() past end of PCIDSKBuffer." );
     126                 : 
     127            6662 :     if( unpad )
     128                 :     {
     129          281287 :         while( size > 0 && buffer[offset+size-1] == ' ' )
     130          272639 :             size--;
     131                 :     }
     132                 : 
     133            6662 :     target.assign( buffer + offset, size );
     134            6662 : }
     135                 : 
     136                 : /************************************************************************/
     137                 : /*                             GetUInt64()                              */
     138                 : /************************************************************************/
     139                 : 
     140             210 : uint64 PCIDSKBuffer::GetUInt64( int offset, int size ) const
     141                 : 
     142                 : {
     143             210 :     std::string value_str;
     144                 : 
     145             210 :     if( offset + size > buffer_size )
     146               0 :         ThrowPCIDSKException( "GetUInt64() past end of PCIDSKBuffer." );
     147                 : 
     148             210 :     value_str.assign( buffer + offset, size );
     149                 : 
     150             210 :     return atouint64(value_str.c_str());
     151                 : }
     152                 : 
     153                 : /************************************************************************/
     154                 : /*                               GetInt()                               */
     155                 : /************************************************************************/
     156                 : 
     157            1362 : int PCIDSKBuffer::GetInt( int offset, int size ) const
     158                 : 
     159                 : {
     160            1362 :     std::string value_str;
     161                 : 
     162            1362 :     if( offset + size > buffer_size )
     163               0 :         ThrowPCIDSKException( "GetInt() past end of PCIDSKBuffer." );
     164                 : 
     165            1362 :     value_str.assign( buffer + offset, size );
     166                 : 
     167            1362 :     return atoi(value_str.c_str());
     168                 : }
     169                 : 
     170                 : /************************************************************************/
     171                 : /*                             GetDouble()                              */
     172                 : /************************************************************************/
     173                 : 
     174            4209 : double PCIDSKBuffer::GetDouble( int offset, int size ) const
     175                 : 
     176                 : {
     177            4209 :     std::string value_str;
     178                 : 
     179            4209 :     if( offset + size > buffer_size )
     180               0 :         ThrowPCIDSKException( "GetDouble() past end of PCIDSKBuffer." );
     181                 : 
     182            4209 :     value_str.assign( buffer + offset, size );
     183                 : 
     184                 : /* -------------------------------------------------------------------- */
     185                 : /*      PCIDSK uses FORTRAN 'D' format for doubles - convert to 'E'     */
     186                 : /*      (C style) before calling atof.                                  */
     187                 : /* -------------------------------------------------------------------- */
     188                 :     int i;
     189                 : 
     190          113643 :     for( i = 0; i < size; i++ )
     191                 :     {
     192          109434 :         if( value_str[i] == 'D' )
     193            3478 :             value_str[i] = 'E';
     194                 :     }
     195                 : 
     196            4209 :     return atof(value_str.c_str());
     197                 : }
     198                 : 
     199                 : /************************************************************************/
     200                 : /*                                Put()                                 */
     201                 : /************************************************************************/
     202                 : 
     203           10832 : void PCIDSKBuffer::Put( const char *value, int offset, int size )
     204                 : 
     205                 : {
     206           10832 :     if( offset + size > buffer_size )
     207               0 :         ThrowPCIDSKException( "Put() past end of PCIDSKBuffer." );
     208                 : 
     209           10832 :     int v_size = strlen(value);
     210           10832 :     if( v_size > size )
     211               0 :         v_size = size;
     212                 : 
     213           10832 :     if( v_size < size )
     214            1338 :         memset( buffer + offset, ' ', size );
     215                 : 
     216           10832 :     memcpy( buffer + offset, value, v_size );
     217           10832 : }
     218                 : 
     219                 : /************************************************************************/
     220                 : /*                             Put(uint64)                              */
     221                 : /************************************************************************/
     222                 : 
     223            2054 : void PCIDSKBuffer::Put( uint64 value, int offset, int size )
     224                 : 
     225                 : {
     226                 :     char fmt[64];
     227                 :     char wrk[128];
     228                 : 
     229            2054 :     sprintf( fmt, "%%%d%sd", size, PCIDSK_FRMT_64_WITHOUT_PREFIX );
     230            2054 :     sprintf( wrk, fmt, value );
     231                 : 
     232            2054 :     Put( wrk, offset, size );
     233            2054 : }
     234                 : 
     235                 : /************************************************************************/
     236                 : /*                             Put(double)                              */
     237                 : /************************************************************************/
     238                 : 
     239                 : void PCIDSKBuffer::Put( double value, int offset, int size, 
     240            6888 :                         const char *fmt )
     241                 : 
     242                 : {
     243            6888 :     if( fmt == NULL )
     244               0 :         fmt = "%g";
     245                 : 
     246                 :     char wrk[128];
     247            6888 :     snprintf( wrk, 127, fmt, value );
     248                 : 
     249            6888 :     char *exponent = strstr(wrk,"E");
     250            6888 :     if( exponent != NULL )
     251            6174 :         *exponent = 'D';
     252                 : 
     253            6888 :     Put( wrk, offset, size );
     254            6888 : }
     255                 : 
     256                 : /************************************************************************/
     257                 : /*                             operator=()                              */
     258                 : /************************************************************************/
     259                 : 
     260               0 : PCIDSKBuffer &PCIDSKBuffer::operator=( const PCIDSKBuffer &src )
     261                 : 
     262                 : {
     263               0 :     SetSize( src.buffer_size );
     264               0 :     memcpy( buffer, src.buffer, buffer_size );
     265                 : 
     266               0 :     return *this;
     267                 : }

Generated by: LTP GCOV extension version 1.5