void rspfDDFSubfieldDefn::DumpData( const char * pachData, int nMaxBytes, FILE * fp ) { if( eType == DDFFloat ) fprintf( fp, " Subfield `%s' = %f\n", pszName, ExtractFloatData( pachData, nMaxBytes, NULL ) ); else if( eType == DDFInt ) fprintf( fp, " Subfield `%s' = %d\n", pszName, ExtractIntData( pachData, nMaxBytes, NULL ) ); else if( eType == DDFBinaryString ) { int nBytes, i; rspf_uint8 *pabyBString = (rspf_uint8 *) ExtractStringData( pachData, nMaxBytes, &nBytes ); fprintf( fp, " Subfield `%s' = 0x", pszName ); for( i = 0; i < std::min(nBytes,24); i++ ) fprintf( fp, "%02X", pabyBString[i] ); if( nBytes > 24 ) fprintf( fp, "%s", "..." ); fprintf( fp, "\n" ); } else fprintf( fp, " Subfield `%s' = `%s'\n", pszName, ExtractStringData( pachData, nMaxBytes, NULL ) ); }
double rspfDDFSubfieldDefn::ExtractFloatData( const char * pachSourceData, int nMaxBytes, int * pnConsumedBytes ) { switch( pszFormatString[0] ) { case 'A': case 'I': case 'R': case 'S': case 'C': return atof(ExtractStringData(pachSourceData, nMaxBytes, pnConsumedBytes)); case 'B': case 'b': { unsigned char abyData[8]; // CPLAssert( nFormatWidth <= nMaxBytes ); if( pnConsumedBytes != NULL ) *pnConsumedBytes = nFormatWidth; // Byte swap the data if it isn't in machine native format. // In any event we copy it into our buffer to ensure it is // word aligned. #ifdef CPL_LSB if( pszFormatString[0] == 'B' ) #else if( pszFormatString[0] == 'b' ) #endif { for( int i = 0; i < nFormatWidth; i++ ) abyData[nFormatWidth-i-1] = pachSourceData[i]; } else { memcpy( abyData, pachSourceData, nFormatWidth ); } // Interpret the bytes of data. switch( eBinaryFormat ) { case UInt: if( nFormatWidth == 1 ) { return( abyData[0] ); } else if( nFormatWidth == 2 ) { rspf_uint16* ptr = (rspf_uint16*) abyData; return *ptr; } else if( nFormatWidth == 4 ) { rspf_uint32* ptr = (rspf_uint32*) abyData; return *ptr; } else { // CPLAssert( false ); return 0.0; } case SInt: if( nFormatWidth == 1 ) { signed char* ptr = (signed char*) abyData; return *ptr; } else if( nFormatWidth == 2 ) { rspf_int16* ptr = (rspf_int16*) abyData; return *ptr; } else if( nFormatWidth == 4 ) { rspf_int32* ptr = (rspf_int32*) abyData; return *ptr; } else { // CPLAssert( false ); return 0.0; } case FloatReal: if( nFormatWidth == 4 ) { float* ptr = (float*) abyData; return *ptr; } else if( nFormatWidth == 8 ) { double* ptr = (double*) abyData; return *ptr; } else { // CPLAssert( false ); return 0.0; } case NotBinary: case FPReal: case FloatComplex: // CPLAssert( false ); return 0.0; } break; // end of 'b'/'B' case. } default: // CPLAssert( false ); return 0.0; } // CPLAssert( false ); return 0.0; }
int rspfDDFSubfieldDefn::ExtractIntData( const char * pachSourceData, int nMaxBytes, int * pnConsumedBytes ) { switch( pszFormatString[0] ) { case 'A': case 'I': case 'R': case 'S': case 'C': return atoi(ExtractStringData(pachSourceData, nMaxBytes, pnConsumedBytes)); case 'B': case 'b': { unsigned char abyData[8]; if( nFormatWidth > nMaxBytes ) { rspfNotify(rspfNotifyLevel_WARN) << "Attempt to extract int subfield %s with format %s\n" << "failed as only %d bytes available. Using zero." << pszName << pszFormatString << nMaxBytes << std::endl; return 0; } if( pnConsumedBytes != NULL ) *pnConsumedBytes = nFormatWidth; // Byte swap the data if it isn't in machine native format. // In any event we copy it into our buffer to ensure it is // word aligned. #ifdef CPL_LSB if( pszFormatString[0] == 'B' ) #else if( pszFormatString[0] == 'b' ) #endif { for( int i = 0; i < nFormatWidth; i++ ) abyData[nFormatWidth-i-1] = pachSourceData[i]; } else { memcpy( abyData, pachSourceData, nFormatWidth ); } // Interpret the bytes of data. switch( eBinaryFormat ) { case UInt: if( nFormatWidth == 4 ) { rspf_uint32* ptr = (rspf_uint32*) abyData; return *ptr; } else if( nFormatWidth == 1 ) { return( abyData[0] ); } else if( nFormatWidth == 2 ) { rspf_uint16* ptr = (rspf_uint16*)abyData; return *ptr; } else { // CPLAssert( false ); return 0; } case SInt: if( nFormatWidth == 4 ) { rspf_int32* ptr = (rspf_int32 *) abyData; return *ptr; } else if( nFormatWidth == 1 ) { signed char* ptr = (signed char *) abyData; return *ptr; } else if( nFormatWidth == 2 ) { rspf_int16* ptr = (rspf_int16 *) abyData; return *ptr; } else { // CPLAssert( false ); return 0; } case FloatReal: if( nFormatWidth == 4 ) { float* ptr = (float *) abyData; return (int) *ptr; } else if( nFormatWidth == 8 ) { double* ptr = (double *) abyData; return (int) *ptr; } else { // CPLAssert( false ); return 0; } case NotBinary: case FPReal: case FloatComplex: // CPLAssert( false ); return 0; } break; // end of 'b'/'B' case. } default: // CPLAssert( false ); return 0; } // CPLAssert( false ); return 0; }
int DDFSubfieldDefn::ExtractIntData( const char * pachSourceData, int nMaxBytes, int * pnConsumedBytes ) { switch( pszFormatString[0] ) { case 'A': case 'I': case 'R': case 'S': case 'C': return atoi(ExtractStringData(pachSourceData, nMaxBytes, pnConsumedBytes)); case 'B': case 'b': { unsigned char abyData[8]; if( nFormatWidth > nMaxBytes ) { CPLError( CE_Warning, CPLE_AppDefined, "Attempt to extract int subfield %s with format %s\n" "failed as only %d bytes available. Using zero.", pszName, pszFormatString, nMaxBytes ); return 0; } if( pnConsumedBytes != NULL ) *pnConsumedBytes = nFormatWidth; // Byte swap the data if it isn't in machine native format. // In any event we copy it into our buffer to ensure it is // word aligned. #ifdef CPL_LSB if( pszFormatString[0] == 'B' ) #else if( pszFormatString[0] == 'b' ) #endif { for( int i = 0; i < nFormatWidth; i++ ) abyData[nFormatWidth-i-1] = pachSourceData[i]; } else { memcpy( abyData, pachSourceData, nFormatWidth ); } // Interpret the bytes of data. switch( eBinaryFormat ) { case UInt: if( nFormatWidth == 4 ) return( (int) *((GUInt32 *) abyData) ); else if( nFormatWidth == 1 ) return( abyData[0] ); else if( nFormatWidth == 2 ) return( *((GUInt16 *) abyData) ); else { CPLAssert( FALSE ); return 0; } case SInt: if( nFormatWidth == 4 ) return( *((GInt32 *) abyData) ); else if( nFormatWidth == 1 ) return( *((signed char *) abyData) ); else if( nFormatWidth == 2 ) return( *((GInt16 *) abyData) ); else { CPLAssert( FALSE ); return 0; } case FloatReal: if( nFormatWidth == 4 ) return( (int) *((float *) abyData) ); else if( nFormatWidth == 8 ) return( (int) *((double *) abyData) ); else { CPLAssert( FALSE ); return 0; } case NotBinary: case FPReal: case FloatComplex: CPLAssert( FALSE ); return 0; } break; // end of 'b'/'B' case. } default: CPLAssert( FALSE ); return 0; } CPLAssert( FALSE ); return 0; }