📄 ddfmodule.cpp
字号:
#include "stdafx.h"#include ".\iso8211.h"#include "CplConv.h"DDFModule::DDFModule(){ nFieldDefnCount = 0; papoFieldDefns = NULL; poRecord = NULL; papoClones = NULL; nCloneCount = nMaxCloneCount = 0; fpDDF = NULL; bReadOnly = TRUE; _interchangeLevel = '\0'; _inlineCodeExtensionIndicator = '\0'; _versionNumber = '\0'; _appIndicator = '\0'; _fieldControlLength = '\0'; strcpy( _extendedCharSet, " ! " ); _recLength = 0; _leaderIden = 'L'; _fieldAreaStart = 0; _sizeFieldLength = 0; _sizeFieldPos = 0; _sizeFieldTag = 0;}/************************************************************************//* ~DDFModule() *//************************************************************************//** * The destructor. */DDFModule::~DDFModule(){ Close();}/************************************************************************//* Close() *//************************************************************************//** * Close an ISO 8211 file. */void DDFModule::Close(){/* -------------------------------------------------------------------- *//* Close the file. *//* -------------------------------------------------------------------- */ if( fpDDF != NULL ) { VSIFClose( fpDDF ); fpDDF = NULL; }/* -------------------------------------------------------------------- *//* Cleanup the working record. *//* -------------------------------------------------------------------- */ if( poRecord != NULL ) { delete poRecord; poRecord = NULL; }/* -------------------------------------------------------------------- *//* Cleanup the clones. Deleting them will cause a callback to *//* remove them from the list. *//* -------------------------------------------------------------------- */ while( nCloneCount > 0 ) delete papoClones[0]; nMaxCloneCount = 0; CPLFree( papoClones ); papoClones = NULL; /* -------------------------------------------------------------------- *//* Cleanup the field definitions. *//* -------------------------------------------------------------------- */ int i; for( i = 0; i < nFieldDefnCount; i++ ) delete papoFieldDefns[i]; CPLFree( papoFieldDefns ); papoFieldDefns = NULL; nFieldDefnCount = 0;}/************************************************************************//* Open() *//************************************************************************//** * Open a ISO 8211 (DDF) file for reading. * * If the open succeeds the data descriptive record (DDR) will have been * read, and all the field and subfield definitions will be available. * * @param pszFilename The name of the file to open. * @param bFailQuietly If FALSE a CPL Error is issued for non-8211 files, * otherwise quietly return NULL. * * @return FALSE if the open fails or TRUE if it succeeds. Errors messages * are issued internally with CPLError(). */int DDFModule::Open( const char * pszFilename, int bFailQuietly ){ static const size_t nLeaderSize = 24;/* -------------------------------------------------------------------- *//* Close the existing file if there is one. *//* -------------------------------------------------------------------- */ if( fpDDF != NULL ) Close(); /* -------------------------------------------------------------------- *//* Open the file. *//* -------------------------------------------------------------------- */ fpDDF = VSIFOpen( pszFilename, "rb" ); if( fpDDF == NULL ) { if( !bFailQuietly ) CPLError( CE_Failure, CPLE_OpenFailed, "Unable to open DDF file `%s'.", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* Read the 24 byte leader. *//* -------------------------------------------------------------------- */ char achLeader[nLeaderSize]; if( VSIFRead( achLeader, 1, nLeaderSize, fpDDF ) != nLeaderSize ) { VSIFClose( fpDDF ); fpDDF = NULL; if( !bFailQuietly ) CPLError( CE_Failure, CPLE_FileIO, "Leader is short on DDF file `%s'.", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* Verify that this appears to be a valid DDF file. *//* -------------------------------------------------------------------- */ int i, bValid = TRUE; for( i = 0; i < (int)nLeaderSize; i++ ) { if( achLeader[i] < 32 || achLeader[i] > 126 ) bValid = FALSE; } if( achLeader[5] != '1' && achLeader[5] != '2' && achLeader[5] != '3' ) bValid = FALSE; if( achLeader[6] != 'L' ) bValid = FALSE; if( achLeader[8] != '1' && achLeader[8] != ' ' ) bValid = FALSE;/* -------------------------------------------------------------------- *//* Extract information from leader. *//* -------------------------------------------------------------------- */ if( bValid ) { _recLength = DDFScanInt( achLeader+0, 5 ); _interchangeLevel = achLeader[5]; _leaderIden = achLeader[6]; _inlineCodeExtensionIndicator = achLeader[7]; _versionNumber = achLeader[8]; _appIndicator = achLeader[9]; _fieldControlLength = DDFScanInt(achLeader+10,2); _fieldAreaStart = DDFScanInt(achLeader+12,5); _extendedCharSet[0] = achLeader[17]; _extendedCharSet[1] = achLeader[18]; _extendedCharSet[2] = achLeader[19]; _extendedCharSet[3] = '\0'; _sizeFieldLength = DDFScanInt(achLeader+20,1); _sizeFieldPos = DDFScanInt(achLeader+21,1); _sizeFieldTag = DDFScanInt(achLeader+23,1); if( _recLength < 12 || _fieldControlLength == 0 || _fieldAreaStart < 24 || _sizeFieldLength == 0 || _sizeFieldPos == 0 || _sizeFieldTag == 0 ) { bValid = FALSE; } }/* -------------------------------------------------------------------- *//* If the header is invalid, then clean up, report the error *//* and return. *//* -------------------------------------------------------------------- */ if( !bValid ) { VSIFClose( fpDDF ); fpDDF = NULL; if( !bFailQuietly ) CPLError( CE_Failure, CPLE_AppDefined, "File `%s' does not appear to have\n" "a valid ISO 8211 header.\n", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* Read the whole record info memory. *//* -------------------------------------------------------------------- */ char *pachRecord; pachRecord = (char *) CPLMalloc(_recLength); memcpy( pachRecord, achLeader, nLeaderSize ); if( VSIFRead( pachRecord+nLeaderSize, 1, _recLength-nLeaderSize, fpDDF ) != _recLength - nLeaderSize ) { if( !bFailQuietly ) CPLError( CE_Failure, CPLE_FileIO, "Header record is short on DDF file `%s'.", pszFilename ); return FALSE; }/* -------------------------------------------------------------------- *//* First make a pass counting the directory entries. *//* -------------------------------------------------------------------- */ int nFieldEntryWidth, nFDCount = 0; nFieldEntryWidth = _sizeFieldLength + _sizeFieldPos + _sizeFieldTag; for( i = nLeaderSize; i < _recLength; i += nFieldEntryWidth ) { if( pachRecord[i] == DDF_FIELD_TERMINATOR ) break; nFDCount++; }/* -------------------------------------------------------------------- *//* Allocate, and read field definitions. *//* -------------------------------------------------------------------- */ for( i = 0; i < nFDCount; i++ ) { char szTag[128]; int nEntryOffset = nLeaderSize + i*nFieldEntryWidth; int nFieldLength, nFieldPos; DDFFieldDefn *poFDefn; strncpy( szTag, pachRecord+nEntryOffset, _sizeFieldTag ); szTag[_sizeFieldTag] = '\0'; nEntryOffset += _sizeFieldTag; nFieldLength = DDFScanInt( pachRecord+nEntryOffset, _sizeFieldLength ); nEntryOffset += _sizeFieldLength; nFieldPos = DDFScanInt( pachRecord+nEntryOffset, _sizeFieldPos ); poFDefn = new DDFFieldDefn(); poFDefn->Initialize( this, szTag, nFieldLength, pachRecord+_fieldAreaStart+nFieldPos ); AddField( poFDefn ); } CPLFree( pachRecord ); /* -------------------------------------------------------------------- *//* Record the current file offset, the beginning of the first *//* data record. *//* -------------------------------------------------------------------- */ nFirstRecordOffset = VSIFTell( fpDDF ); return TRUE;}/************************************************************************//* Initialize() *//************************************************************************/int DDFModule::Initialize( char chInterchangeLevel, char chLeaderIden, char chCodeExtensionIndicator, char chVersionNumber, char chAppIndicator, const char *pszExtendedCharSet, int nSizeFieldLength, int nSizeFieldPos, int nSizeFieldTag ){ _interchangeLevel = chInterchangeLevel; _leaderIden = chLeaderIden; _inlineCodeExtensionIndicator = chCodeExtensionIndicator; _versionNumber = chVersionNumber; _appIndicator = chAppIndicator; strcpy( _extendedCharSet, pszExtendedCharSet ); _sizeFieldLength = nSizeFieldLength; _sizeFieldPos = nSizeFieldPos; _sizeFieldTag = nSizeFieldTag; return TRUE;}/************************************************************************//* Create() *//************************************************************************/int DDFModule::Create( const char *pszFilename ){ CPLAssert( fpDDF == NULL );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -