📄 ddfsubfielddefn.cpp
字号:
if( pnConsumedBytes != NULL ) { if( nMaxBytes == 0 ) *pnConsumedBytes = nLength; else *pnConsumedBytes = nLength+1; } return nLength; }}/************************************************************************//* ExtractStringData() *//************************************************************************//** * Extract a zero terminated string containing the data for this subfield. * Given a pointer to the data * for this subfield (from within a DDFRecord) this method will return the * data for this subfield. The number of bytes * consumed as part of this field can also be fetched. This number may * be one longer than the string length if there is a terminator character * used.<p> * * This function will return the raw binary data of a subfield for * types other than DDFString, including data past zero chars. This is * the standard way of extracting DDFBinaryString subfields for instance.<p> * * @param pachSourceData The pointer to the raw data for this field. This * may have come from DDFRecord::GetData(), taking into account skip factors * over previous subfields data. * @param nMaxBytes The maximum number of bytes that are accessable after * pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the number of * bytes consumed by this field should be written. May be NULL to ignore. * This is used as a skip factor to increment pachSourceData to point to the * next subfields data. * * @return A pointer to a buffer containing the data for this field. The * returned pointer is to an internal buffer which is invalidated on the * next ExtractStringData() call on this DDFSubfieldDefn(). It should not * be freed by the application. * * @see ExtractIntData(), ExtractFloatData() */const char *DDFSubfieldDefn::ExtractStringData( const char * pachSourceData, int nMaxBytes, int * pnConsumedBytes ){ int nLength = GetDataLength( pachSourceData, nMaxBytes, pnConsumedBytes );/* -------------------------------------------------------------------- *//* Do we need to grow the buffer. *//* -------------------------------------------------------------------- */ if( nMaxBufChars < nLength+1 ) { CPLFree( pachBuffer ); nMaxBufChars = nLength+1; pachBuffer = (char *) CPLMalloc(nMaxBufChars); }/* -------------------------------------------------------------------- *//* Copy the data to the buffer. We use memcpy() so that it *//* will work for binary data. *//* -------------------------------------------------------------------- */ memcpy( pachBuffer, pachSourceData, nLength ); pachBuffer[nLength] = '\0'; return pachBuffer;}/************************************************************************//* ExtractFloatData() *//************************************************************************//** * Extract a subfield value as a float. Given a pointer to the data * for this subfield (from within a DDFRecord) this method will return the * floating point data for this subfield. The number of bytes * consumed as part of this field can also be fetched. This method may be * called for any type of subfield, and will return zero if the subfield is * not numeric. * * @param pachSourceData The pointer to the raw data for this field. This * may have come from DDFRecord::GetData(), taking into account skip factors * over previous subfields data. * @param nMaxBytes The maximum number of bytes that are accessable after * pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the number of * bytes consumed by this field should be written. May be NULL to ignore. * This is used as a skip factor to increment pachSourceData to point to the * next subfields data. * * @return The subfield's numeric value (or zero if it isn't numeric). * * @see ExtractIntData(), ExtractStringData() */doubleDDFSubfieldDefn::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 ) return( *((GUInt16 *) abyData) ); else if( nFormatWidth == 4 ) return( *((GUInt32 *) abyData) ); else { CPLAssert( FALSE ); return 0.0; } case SInt: if( nFormatWidth == 1 ) return( *((signed char *) abyData) ); else if( nFormatWidth == 2 ) return( *((GInt16 *) abyData) ); else if( nFormatWidth == 4 ) return( *((GInt32 *) abyData) ); else { CPLAssert( FALSE ); return 0.0; } case FloatReal: if( nFormatWidth == 4 ) return( *((float *) abyData) ); else if( nFormatWidth == 8 ) return( *((double *) abyData) ); 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;}/************************************************************************//* ExtractIntData() *//************************************************************************//** * Extract a subfield value as an integer. Given a pointer to the data * for this subfield (from within a DDFRecord) this method will return the * int data for this subfield. The number of bytes * consumed as part of this field can also be fetched. This method may be * called for any type of subfield, and will return zero if the subfield is * not numeric. * * @param pachSourceData The pointer to the raw data for this field. This * may have come from DDFRecord::GetData(), taking into account skip factors * over previous subfields data. * @param nMaxBytes The maximum number of bytes that are accessable after * pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the number of * bytes consumed by this field should be written. May be NULL to ignore. * This is used as a skip factor to increment pachSourceData to point to the * next subfields data. * * @return The subfield's numeric value (or zero if it isn't numeric). * * @see ExtractFloatData(), ExtractStringData() */intDDFSubfieldDefn::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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -