33
33
#include "cpl_error.h"
34
34
#include "cpl_string.h"
36
CPL_CVSID("$Id: tigerfilebase.cpp 17225 2009-06-07 20:59:19Z rouault $");
36
CPL_CVSID("$Id: tigerfilebase.cpp 22961 2011-08-20 17:09:59Z rouault $");
38
38
/************************************************************************/
39
39
/* TigerFileBase() */
40
40
/************************************************************************/
42
TigerFileBase::TigerFileBase()
42
TigerFileBase::TigerFileBase( const TigerRecordInfo *psRTInfoIn,
43
const char *m_pszFileCodeIn )
45
46
pszShortModule = NULL;
305
309
strncpy( szValue, poFeature->GetFieldAsString( iField ),
306
310
sizeof(szValue) - 1 );
311
szValue[sizeof(szValue) - 1] = 0;
307
312
if( (int) strlen(szValue) < nEnd - nStart + 1 )
308
313
memset( szValue + strlen(szValue), ' ',
309
314
nEnd - nStart + 1 - strlen(szValue) );
440
445
/* -------------------------------------------------------------------- */
441
446
/* Does this file already exist? */
442
447
/* -------------------------------------------------------------------- */
443
const char *pszFilename;
445
450
pszFilename = poDS->BuildFilename( szFullModule, pszExtension );
447
452
fpPrimary = VSIFOpen( pszFilename, "ab" );
453
CPLFree(pszFilename);
448
454
if( fpPrimary == NULL )
456
462
/************************************************************************/
457
463
/* AddFieldDefns() */
458
464
/************************************************************************/
459
void TigerFileBase::AddFieldDefns(TigerRecordInfo *psRTInfo,
465
void TigerFileBase::AddFieldDefns(const TigerRecordInfo *psRTInfo,
460
466
OGRFeatureDefn *poFeatureDefn)
462
468
OGRFieldDefn oField("",OFTInteger);
533
/************************************************************************/
535
/************************************************************************/
537
int TigerFileBase::SetModule( const char * pszModule )
540
if (m_pszFileCode == NULL)
543
if( !OpenFile( pszModule, m_pszFileCode ) )
546
EstablishFeatureCount();
551
/************************************************************************/
553
/************************************************************************/
555
OGRFeature *TigerFileBase::GetFeature( int nRecordId )
558
char achRecord[OGR_TIGER_RECBUF_LEN];
560
if (psRTInfo == NULL)
563
if( nRecordId < 0 || nRecordId >= nFeatures )
565
CPLError( CE_Failure, CPLE_FileIO,
566
"Request for out-of-range feature %d of %s",
567
nRecordId, pszModule );
571
/* -------------------------------------------------------------------- */
572
/* Read the raw record data from the file. */
573
/* -------------------------------------------------------------------- */
574
if( fpPrimary == NULL )
577
if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
579
CPLError( CE_Failure, CPLE_FileIO,
580
"Failed to seek to %d of %s",
581
nRecordId * nRecordLength, pszModule );
585
if( VSIFRead( achRecord, psRTInfo->nRecordLength, 1, fpPrimary ) != 1 )
587
CPLError( CE_Failure, CPLE_FileIO,
588
"Failed to read record %d of %s",
589
nRecordId, pszModule );
593
/* -------------------------------------------------------------------- */
595
/* -------------------------------------------------------------------- */
596
OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
598
SetFields( psRTInfo, poFeature, achRecord );
603
/************************************************************************/
604
/* CreateFeature() */
605
/************************************************************************/
607
OGRErr TigerFileBase::CreateFeature( OGRFeature *poFeature )
610
char szRecord[OGR_TIGER_RECBUF_LEN];
612
if (psRTInfo == NULL)
613
return OGRERR_FAILURE;
615
if( !SetWriteModule( m_pszFileCode, psRTInfo->nRecordLength+2, poFeature ) )
616
return OGRERR_FAILURE;
618
memset( szRecord, ' ', psRTInfo->nRecordLength );
620
WriteFields( psRTInfo, poFeature, szRecord );
622
WriteRecord( szRecord, psRTInfo->nRecordLength, m_pszFileCode );