void OGRDXFWriterDS::ScanForEntities( const char *pszFilename, const char *pszTarget ) { OGRDXFReader oReader; VSILFILE *fp; /* -------------------------------------------------------------------- */ /* Open the file and setup a reader. */ /* -------------------------------------------------------------------- */ fp = VSIFOpenL( pszFilename, "r" ); if( fp == NULL ) return; oReader.Initialize( fp ); /* -------------------------------------------------------------------- */ /* Add every code "5" line to our entities list. */ /* -------------------------------------------------------------------- */ char szLineBuf[257]; int nCode; const char *pszPortion = "HEADER"; while( (nCode = oReader.ReadValue( szLineBuf, sizeof(szLineBuf) )) != -1 ) { if( (nCode == 5 || nCode == 105) && EQUAL(pszTarget,pszPortion) ) { CPLString osEntity( szLineBuf ); if( CheckEntityID( osEntity ) ) CPLDebug( "DXF", "Encounted entity '%s' multiple times.", osEntity.c_str() ); else aosUsedEntities.insert( osEntity ); } if( nCode == 0 && EQUAL(szLineBuf,"SECTION") ) { nCode = oReader.ReadValue( szLineBuf, sizeof(szLineBuf) ); if( nCode == 2 && EQUAL(szLineBuf,"ENTITIES") ) pszPortion = "BODY"; if( nCode == 2 && EQUAL(szLineBuf,"OBJECTS") ) pszPortion = "TRAILER"; } } VSIFCloseL( fp ); }
bool OGRDXFWriterDS::TransferUpdateTrailer( VSILFILE *fpOut ) { /* -------------------------------------------------------------------- */ /* Open the file and setup a reader. */ /* -------------------------------------------------------------------- */ VSILFILE *l_fp = VSIFOpenL( osTrailerFile, "r" ); if( l_fp == NULL ) return false; OGRDXFReader oReader; oReader.Initialize( l_fp ); /* -------------------------------------------------------------------- */ /* Scan ahead to find the OBJECTS section. */ /* -------------------------------------------------------------------- */ char szLineBuf[257]; int nCode = 0; while( (nCode = oReader.ReadValue( szLineBuf, sizeof(szLineBuf) )) != -1 ) { if( nCode == 0 && EQUAL(szLineBuf,"SECTION") ) { nCode = oReader.ReadValue( szLineBuf, sizeof(szLineBuf) ); if( nCode == 2 && EQUAL(szLineBuf,"OBJECTS") ) break; } } if( nCode == -1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to find OBJECTS section in trailer file '%s'.", osTrailerFile.c_str() ); return false; } /* -------------------------------------------------------------------- */ /* Insert the "end of section" for ENTITIES, and the start of */ /* the OBJECTS section. */ /* -------------------------------------------------------------------- */ WriteValue( fpOut, 0, "ENDSEC" ); WriteValue( fpOut, 0, "SECTION" ); WriteValue( fpOut, 2, "OBJECTS" ); /* -------------------------------------------------------------------- */ /* Copy the remainder of the file. */ /* -------------------------------------------------------------------- */ while( (nCode = oReader.ReadValue( szLineBuf, sizeof(szLineBuf) )) != -1 ) { if( !WriteValue( fpOut, nCode, szLineBuf ) ) { VSIFCloseL( fp ); return false; } } VSIFCloseL( l_fp ); return true; }