Changeset 13274
- Timestamp:
- 12/06/07 09:00:51 (7 months ago)
- Files:
-
- trunk/gdal/ogr/ogrsf_frmts/shape/GNUmakefile (modified) (1 diff)
- trunk/gdal/ogr/ogrsf_frmts/shape/dbfopen.c (modified) (18 diffs)
- trunk/gdal/ogr/ogrsf_frmts/shape/makefile.vc (modified) (1 diff)
- trunk/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp (modified) (3 diffs)
- trunk/gdal/ogr/ogrsf_frmts/shape/shapefil.h (modified) (8 diffs)
- trunk/gdal/ogr/ogrsf_frmts/shape/shp_vsi.c (added)
- trunk/gdal/ogr/ogrsf_frmts/shape/shpopen.c (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/gdal/ogr/ogrsf_frmts/shape/GNUmakefile
r9372 r13274 3 3 include ../../../GDALmake.opt 4 4 5 OBJ = shape2ogr.o shpopen.o dbfopen.o shptree.o \5 OBJ = shape2ogr.o shpopen.o dbfopen.o shptree.o shp_vsi.o \ 6 6 ogrshapedriver.o ogrshapedatasource.o ogrshapelayer.o 7 7 8 CPPFLAGS := -DUSE_CPL -I.. -I../.. $(GDAL_INCLUDE) $(CPPFLAGS) 8 CPPFLAGS := -DSAOffset=vsi_l_offset -DUSE_CPL \ 9 -I.. -I../.. $(GDAL_INCLUDE) $(CPPFLAGS) 9 10 10 11 default: $(O_OBJ) trunk/gdal/ogr/ogrsf_frmts/shape/dbfopen.c
r12050 r13274 1 1 /****************************************************************************** 2 * $Id: dbfopen.c,v 1.7 3 2007/09/03 19:48:11fwarmerdam Exp $2 * $Id: dbfopen.c,v 1.74 2007/12/06 07:00:25 fwarmerdam Exp $ 3 3 * 4 4 * Project: Shapelib … … 35 35 * 36 36 * $Log: dbfopen.c,v $ 37 * Revision 1.74 2007/12/06 07:00:25 fwarmerdam 38 * dbfopen now using SAHooks for fileio 39 * 37 40 * Revision 1.73 2007/09/03 19:48:11 fwarmerdam 38 41 * move DBFReadAttribute() static dDoubleField into dbfinfo … … 109 112 #include <string.h> 110 113 111 SHP_CVSID("$Id: dbfopen.c,v 1.7 3 2007/09/03 19:48:11fwarmerdam Exp $")114 SHP_CVSID("$Id: dbfopen.c,v 1.74 2007/12/06 07:00:25 fwarmerdam Exp $") 112 115 113 116 #ifndef FALSE … … 177 180 /* descriptions. */ 178 181 /* -------------------------------------------------------------------- */ 179 fseek( psDBF->fp, 0, 0 ); 180 fwrite( abyHeader, XBASE_FLDHDR_SZ, 1, psDBF->fp ); 181 fwrite( psDBF->pszHeader, XBASE_FLDHDR_SZ, psDBF->nFields, psDBF->fp ); 182 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 ); 183 psDBF->sHooks.FWrite( abyHeader, XBASE_FLDHDR_SZ, 1, psDBF->fp ); 184 psDBF->sHooks.FWrite( psDBF->pszHeader, XBASE_FLDHDR_SZ, psDBF->nFields, 185 psDBF->fp ); 182 186 183 187 /* -------------------------------------------------------------------- */ … … 189 193 190 194 cNewline = 0x0d; 191 fwrite( &cNewline, 1, 1, psDBF->fp );195 psDBF->sHooks.FWrite( &cNewline, 1, 1, psDBF->fp ); 192 196 } 193 197 } … … 202 206 203 207 { 204 intnRecordOffset;208 SAOffset nRecordOffset; 205 209 206 210 if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 ) … … 208 212 psDBF->bCurrentRecordModified = FALSE; 209 213 210 nRecordOffset = psDBF->nRecordLength * psDBF->nCurrentRecord 211 + psDBF->nHeaderLength; 212 213 if( fseek( psDBF->fp, nRecordOffset, 0 ) != 0 214 || fwrite( psDBF->pszCurrentRecord, psDBF->nRecordLength, 215 1, psDBF->fp ) != 1 ) 214 nRecordOffset = 215 psDBF->nRecordLength * (SAOffset) psDBF->nCurrentRecord 216 + psDBF->nHeaderLength; 217 218 if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 ) != 0 219 || psDBF->sHooks.FWrite( psDBF->pszCurrentRecord, 220 psDBF->nRecordLength, 221 1, psDBF->fp ) != 1 ) 216 222 { 217 223 #ifdef USE_CPL … … 239 245 if( psDBF->nCurrentRecord != iRecord ) 240 246 { 241 int nRecordOffset;247 SAOffset nRecordOffset; 242 248 243 249 if( !DBFFlushRecord( psDBF ) ) 244 250 return FALSE; 245 251 246 nRecordOffset = psDBF->nRecordLength * iRecord + psDBF->nHeaderLength; 247 248 if( fseek( psDBF->fp, nRecordOffset, 0 ) != 0 ) 252 nRecordOffset = 253 psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength; 254 255 if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, SEEK_SET ) != 0 ) 249 256 { 250 257 #ifdef USE_CPL 251 258 CPLError( CE_Failure, CPLE_FileIO, 252 "fseek(% d) failed on DBF file.\n",253 nRecordOffset );259 "fseek(%ld) failed on DBF file.\n", 260 (long) nRecordOffset ); 254 261 #else 255 fprintf( stderr, "fseek(% d) failed on DBF file.\n",256 nRecordOffset );262 fprintf( stderr, "fseek(%ld) failed on DBF file.\n", 263 (long) nRecordOffset ); 257 264 #endif 258 265 return FALSE; 259 266 } 260 267 261 if( fread( psDBF->pszCurrentRecord, psDBF->nRecordLength,262 1, psDBF->fp ) != 1 )268 if( psDBF->sHooks.FRead( psDBF->pszCurrentRecord, 269 psDBF->nRecordLength, 1, psDBF->fp ) != 1 ) 263 270 { 264 271 #ifdef USE_CPL … … 294 301 DBFFlushRecord( psDBF ); 295 302 296 fseek( psDBF->fp, 0, 0 );297 fread( abyFileHeader, 32, 1, psDBF->fp );303 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 ); 304 psDBF->sHooks.FRead( abyFileHeader, 32, 1, psDBF->fp ); 298 305 299 306 abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256); … … 302 309 abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256); 303 310 304 fseek( psDBF->fp, 0, 0 );305 fwrite( abyFileHeader, 32, 1, psDBF->fp );306 307 fflush( psDBF->fp );311 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 ); 312 psDBF->sHooks.FWrite( abyFileHeader, 32, 1, psDBF->fp ); 313 314 psDBF->sHooks.FFlush( psDBF->fp ); 308 315 } 309 316 … … 318 325 319 326 { 327 SAHooks sHooks; 328 329 SASetupDefaultHooks( &sHooks ); 330 331 return DBFOpenLL( pszFilename, pszAccess, &sHooks ); 332 } 333 334 /************************************************************************/ 335 /* DBFOpen() */ 336 /* */ 337 /* Open a .dbf file. */ 338 /************************************************************************/ 339 340 DBFHandle SHPAPI_CALL 341 DBFOpenLL( const char * pszFilename, const char * pszAccess, SAHooks *psHooks ) 342 343 { 320 344 DBFHandle psDBF; 321 unsigned char *pabyBuf;345 unsigned char *pabyBuf; 322 346 int nFields, nHeadLen, iField, i; 323 347 char *pszBasename, *pszFullname; … … 355 379 356 380 psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) ); 357 psDBF->fp = fopen( pszFullname, pszAccess ); 381 psDBF->fp = psHooks->FOpen( pszFullname, pszAccess ); 382 memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) ); 358 383 359 384 if( psDBF->fp == NULL ) 360 385 { 361 386 sprintf( pszFullname, "%s.DBF", pszBasename ); 362 psDBF->fp = fopen(pszFullname, pszAccess );387 psDBF->fp = psDBF->sHooks.FOpen(pszFullname, pszAccess ); 363 388 } 364 389 … … 380 405 /* -------------------------------------------------------------------- */ 381 406 pabyBuf = (unsigned char *) malloc(500); 382 if( fread( pabyBuf, 32, 1, psDBF->fp ) != 1 )383 { 384 fclose( psDBF->fp );407 if( psDBF->sHooks.FRead( pabyBuf, 32, 1, psDBF->fp ) != 1 ) 408 { 409 psDBF->sHooks.FClose( psDBF->fp ); 385 410 free( pabyBuf ); 386 411 free( psDBF ); … … 405 430 psDBF->pszHeader = (char *) pabyBuf; 406 431 407 fseek( psDBF->fp, 32, 0 );408 if( fread( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )409 { 410 fclose( psDBF->fp );432 psDBF->sHooks.FSeek( psDBF->fp, 32, 0 ); 433 if( psDBF->sHooks.FRead( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 ) 434 { 435 psDBF->sHooks.FClose( psDBF->fp ); 411 436 free( pabyBuf ); 412 437 free( psDBF->pszCurrentRecord ); … … 482 507 /* Close, and free resources. */ 483 508 /* -------------------------------------------------------------------- */ 484 fclose( psDBF->fp );509 psDBF->sHooks.FClose( psDBF->fp ); 485 510 486 511 if( psDBF->panFieldOffset != NULL ) … … 511 536 512 537 { 538 SAHooks sHooks; 539 540 SASetupDefaultHooks( &sHooks ); 541 542 return DBFCreateLL( pszFilename, &sHooks ); 543 } 544 545 /************************************************************************/ 546 /* DBFCreate() */ 547 /* */ 548 /* Create a new .dbf file. */ 549 /************************************************************************/ 550 551 DBFHandle SHPAPI_CALL 552 DBFCreateLL( const char * pszFilename, SAHooks *psHooks ) 553 554 { 513 555 DBFHandle psDBF; 514 FILE *fp;556 SAFile fp; 515 557 char *pszFullname, *pszBasename; 516 558 int i; 559 char chZero = '\0'; 517 560 518 561 /* -------------------------------------------------------------------- */ … … 537 580 /* Create the file. */ 538 581 /* -------------------------------------------------------------------- */ 539 fp = fopen( pszFullname, "wb" );582 fp = psHooks->FOpen( pszFullname, "wb" ); 540 583 if( fp == NULL ) 541 584 return( NULL ); 542 543 fputc( 0, fp );544 fclose( fp );545 546 fp = fopen( pszFullname, "rb+" );585 586 psHooks->FWrite( &chZero, 1, 1, fp ); 587 psHooks->FClose( fp ); 588 589 fp = psHooks->FOpen( pszFullname, "rb+" ); 547 590 if( fp == NULL ) 548 591 return( NULL ); … … 555 598 psDBF = (DBFHandle) calloc(1,sizeof(DBFInfo)); 556 599 600 memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) ); 557 601 psDBF->fp = fp; 558 602 psDBF->nRecords = 0; trunk/gdal/ogr/ogrsf_frmts/shape/makefile.vc
r6927 r13274 1 1 2 2 OBJ = shape2ogr.obj shpopen.obj dbfopen.obj ogrshapedriver.obj \ 3 ogrshapedatasource.obj ogrshapelayer.obj shptree.obj 4 EXTRAFLAGS = -I.. -I..\.. /DSHAPELIB_DLLEXPORT -DUSE_CPL 3 ogrshapedatasource.obj ogrshapelayer.obj shptree.obj \ 4 shp_vsi.obj 5 EXTRAFLAGS = -I.. -I..\.. /DSHAPELIB_DLLEXPORT \ 6 -DUSE_CPL -DSAOffset=vsi_l_offset 5 7 6 8 GDAL_ROOT = ..\..\.. trunk/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp
r12930 r13274 758 758 /* Update .shp header. */ 759 759 /* -------------------------------------------------------------------- */ 760 nStartPos = ftell( hSHP->fpSHP );761 762 if( fseek( hSHP->fpSHP, 0, SEEK_SET ) != 0763 || fread( abyHeader, 100, 1, hSHP->fpSHP ) != 1 )760 nStartPos = hSHP->sHooks.FTell( hSHP->fpSHP ); 761 762 if( hSHP->sHooks.FSeek( hSHP->fpSHP, 0, SEEK_SET ) != 0 763 || hSHP->sHooks.FRead( abyHeader, 100, 1, hSHP->fpSHP ) != 1 ) 764 764 return FALSE; 765 765 766 766 *((GInt32 *) (abyHeader + 32)) = CPL_LSBWORD32( nNewGeomType ); 767 767 768 if( fseek( hSHP->fpSHP, 0, SEEK_SET ) != 0769 || fwrite( abyHeader, 100, 1, hSHP->fpSHP ) != 1 )768 if( hSHP->sHooks.FSeek( hSHP->fpSHP, 0, SEEK_SET ) != 0 769 || hSHP->sHooks.FWrite( abyHeader, 100, 1, hSHP->fpSHP ) != 1 ) 770 770 return FALSE; 771 771 772 if( fseek( hSHP->fpSHP, nStartPos, SEEK_SET ) != 0 )772 if( hSHP->sHooks.FSeek( hSHP->fpSHP, nStartPos, SEEK_SET ) != 0 ) 773 773 return FALSE; 774 774 … … 776 776 /* Update .shx header. */ 777 777 /* -------------------------------------------------------------------- */ 778 nStartPos = ftell( hSHP->fpSHX );779 780 if( fseek( hSHP->fpSHX, 0, SEEK_SET ) != 0781 || fread( abyHeader, 100, 1, hSHP->fpSHX ) != 1 )778 nStartPos = hSHP->sHooks.FTell( hSHP->fpSHX ); 779 780 if( hSHP->sHooks.FSeek( hSHP->fpSHX, 0, SEEK_SET ) != 0 781 || hSHP->sHooks.FRead( abyHeader, 100, 1, hSHP->fpSHX ) != 1 ) 782 782 return FALSE; 783 783 784 784 *((GInt32 *) (abyHeader + 32)) = CPL_LSBWORD32( nNewGeomType ); 785 785 786 if( fseek( hSHP->fpSHX, 0, SEEK_SET ) != 0787 || fwrite( abyHeader, 100, 1, hSHP->fpSHX ) != 1 )786 if( hSHP->sHooks.FSeek( hSHP->fpSHX, 0, SEEK_SET ) != 0 787 || hSHP->sHooks.FWrite( abyHeader, 100, 1, hSHP->fpSHX ) != 1 ) 788 788 return FALSE; 789 789 790 if( fseek( hSHP->fpSHX, nStartPos, SEEK_SET ) != 0 )790 if( hSHP->sHooks.FSeek( hSHP->fpSHX, nStartPos, SEEK_SET ) != 0 ) 791 791 return FALSE; 792 792 … … 819 819 if( hSHP != NULL ) 820 820 { 821 fflush( hSHP->fpSHP );822 fflush( hSHP->fpSHX );821 hSHP->sHooks.FFlush( hSHP->fpSHP ); 822 hSHP->sHooks.FFlush( hSHP->fpSHX ); 823 823 } 824 824 825 825 if( hDBF != NULL ) 826 fflush( hDBF->fp );826 hDBF->sHooks.FFlush( hDBF->fp ); 827 827 828 828 return OGRERR_NONE; trunk/gdal/ogr/ogrsf_frmts/shape/shapefil.h
r12930 r13274 38 38 * 39 39 * $Log: shapefil.h,v $ 40 * Revision 1.40 2007/12/06 07:00:25 fwarmerdam 41 * dbfopen now using SAHooks for fileio 42 * 43 * Revision 1.39 2007/12/04 20:37:56 fwarmerdam 44 * preliminary implementation of hooks api for io and errors 45 * 40 46 * Revision 1.38 2007/11/21 22:39:56 fwarmerdam 41 47 * close shx file in readonly mode (GDAL #1956) … … 99 105 #ifdef USE_CPL 100 106 #include "cpl_error.h" 107 #include "cpl_vsi.h" 101 108 #endif 102 109 … … 121 128 /* -------------------------------------------------------------------- */ 122 129 #define DISABLE_MULTIPATCH_MEASURE 123 130 124 131 /* -------------------------------------------------------------------- */ 125 132 /* SHPAPI_CALL */ … … 179 186 # define SHP_CVSID(string) 180 187 #endif 188 189 /* -------------------------------------------------------------------- */ 190 /* IO/Error hook functions. */ 191 /* -------------------------------------------------------------------- */ 192 typedef int *SAFile; 193 194 #ifndef SAOffset 195 typedef unsigned long SAOffset; 196 #endif 197 198 typedef struct { 199 SAFile (*FOpen) ( const char *filename, const char *path); 200 SAOffset (*FRead) ( void *p, SAOffset size, SAOffset nmemb, SAFile file); 201 SAOffset (*FWrite)( void *p, SAOffset size, SAOffset nmemb, SAFile file); 202 SAOffset (*FSeek) ( SAFile file, SAOffset offset, int whence ); 203 SAOffset (*FTell) ( SAFile file ); 204 int (*FFlush)( SAFile file ); 205 int (*FClose)( SAFile file ); 206 207 void (*Error) ( const char *message ); 208 } SAHooks; 209 210 void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks ); 181 211 182 212 /************************************************************************/ … … 185 215 typedef struct 186 216 { 187 FILE *fpSHP; 188 FILE *fpSHX; 217 SAHooks sHooks; 218 219 SAFile fpSHP; 220 SAFile fpSHX; 189 221 190 222 int nShapeType; /* SHPT_* */ … … 281 313 SHPOpen( const char * pszShapeFile, const char * pszAccess ); 282 314 SHPHandle SHPAPI_CALL 315 SHPOpenLL( const char *pszShapeFile, const char *pszAccess, 316 SAHooks *psHooks ); 317 SHPHandle SHPAPI_CALL 283 318 SHPCreate( const char * pszShapeFile, int nShapeType ); 319 SHPHandle SHPAPI_CALL 320 SHPCreateLL( const char * pszShapeFile, int nShapeType, 321 SAHooks *psHooks ); 284 322 void SHPAPI_CALL 285 323 SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType, … … 395 433 typedef struct 396 434 { 397 FILE *fp; 435 SAHooks sHooks; 436 437 SAFile fp; 398 438 399 439 int nRecords; … … 434 474 #define XBASE_FLDHDR_SZ 32 435 475 476 436 477 DBFHandle SHPAPI_CALL 437 478 DBFOpen( const char * pszDBFFile, const char * pszAccess ); 438 479 DBFHandle SHPAPI_CALL 480 DBFOpenLL( const char * pszDBFFile, const char * pszAccess, 481 SAHooks *psHooks ); 482 DBFHandle SHPAPI_CALL 439 483 DBFCreate( const char * pszDBFFile ); 484 DBFHandle SHPAPI_CALL 485 DBFCreateLL( const char * pszDBFFile, SAHooks *psHooks ); 440 486 441 487 int SHPAPI_CALL trunk/gdal/ogr/ogrsf_frmts/shape/shpopen.c
r12930 r13274 1 1 /****************************************************************************** 2 * $Id: shpopen.c,v 1.5 5 2007/11/21 22:39:56fwarmerdam Exp $2 * $Id: shpopen.c,v 1.57 2007/12/06 07:00:25 fwarmerdam Exp $ 3 3 * 4 4 * Project: Shapelib … … 35 35 * 36 36 * $Log: shpopen.c,v $ 37 * Revision 1.57 2007/12/06 07:00:25 fwarmerdam 38 * dbfopen now using SAHooks for fileio 39 * 40 * Revision 1.56 2007/12/04 20:37:56 fwarmerdam 41 * preliminary implementation of hooks api for io and errors 42 * 37 43 * Revision 1.55 2007/11/21 22:39:56 fwarmerdam 38 44 * close shx file in readonly mode (GDAL #1956) … … 220 226 #include <stdio.h> 221 227 222 SHP_CVSID("$Id: shpopen.c,v 1.5 5 2007/11/21 22:39:56fwarmerdam Exp $")228 SHP_CVSID("$Id: shpopen.c,v 1.57 2007/12/06 07:00:25 fwarmerdam Exp $") 223 229 224 230 typedef unsigned char uchar; … … 298 304 if (psSHP->fpSHX == NULL) 299 305 { 300 #ifdef USE_CPL 301 CPLError( CE_Failure, CPLE_NotSupported, 302 "SHPWriteHeader failed : SHX file is closed"); 303 #endif 306 psSHP->sHooks.Error( "SHPWriteHeader failed : SHX file is closed"); 304 307 return; 305 308 } … … 361 364 /* Write .shp file header. */ 362 365 /* -------------------------------------------------------------------- */ 363 if( fseek( psSHP->fpSHP, 0, 0 ) != 0 364 || fwrite( abyHeader, 100, 1, psSHP->fpSHP ) != 1 ) 365 { 366 #ifdef USE_CPL 367 CPLError( CE_Failure, CPLE_OpenFailed, 368 "Failure writing .shp header (%s)", VSIStrerror( errno ) ); 369 #endif 366 if( psSHP->sHooks.FSeek( psSHP->fpSHP, 0, 0 ) != 0 367 || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHP ) != 1 ) 368 { 369 psSHP->sHooks.Error( "Failure writing .shp header" ); 370 370 return; 371 371 } … … 378 378 if( !bBigEndian ) SwapWord( 4, abyHeader+24 ); 379 379 380 if( fseek( psSHP->fpSHX, 0, 0 ) != 0 381 || fwrite( abyHeader, 100, 1, psSHP->fpSHX ) != 1 ) 382 { 383 #ifdef USE_CPL 384 CPLError( CE_Failure, CPLE_OpenFailed, 385 "Failure writing .shx header (%s)", VSIStrerror( errno ) ); 386 #endif 380 if( psSHP->sHooks.FSeek( psSHP->fpSHX, 0, 0 ) != 0 381 || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHX ) != 1 ) 382 { 383 psSHP->sHooks.Error( "Failure writing .shx header" ); 387 384 return; 388 385 } … … 401 398 } 402 399 403 if( (int) fwrite( panSHX, sizeof(int32)*2, psSHP->nRecords, psSHP->fpSHX )400 if( (int)psSHP->sHooks.FWrite( panSHX, sizeof(int32)*2, psSHP->nRecords, psSHP->fpSHX ) 404 401 != psSHP->nRecords ) 405 402 { 406 #ifdef USE_CPL 407 CPLError( CE_Failure, CPLE_OpenFailed, 408 "Failure writing .shx contents (%s)", VSIStrerror( errno ) ); 409 #endif 403 psSHP->sHooks.Error( "Failure writing .shx contents" ); 410 404 } 411 405 … … 415 409 /* Flush to disk. */ 416 410 /* -------------------------------------------------------------------- */ 417 fflush( psSHP->fpSHP );418 fflush( psSHP->fpSHX );411 psSHP->sHooks.FFlush( psSHP->fpSHP ); 412 psSHP->sHooks.FFlush( psSHP->fpSHX ); 419 413 } 420 414 421 415 /************************************************************************/ 422 /* shpopen() */ 416 /* SHPOpen() */ 417 /************************************************************************/ 418 419 SHPHandle SHPAPI_CALL 420 SHPOpen( const char * pszLayer, const char * pszAccess ) 421 422 { 423 SAHooks sHooks; 424 425 SASetupDefaultHooks( &sHooks ); 426 427 return SHPOpenLL( pszLayer, pszAccess, &sHooks ); 428 } 429 430 /************************************************************************/ 431 /* SHPOpen() */ 423 432 /* */ 424 433 /* Open the .shp and .shx files based on the basename of the */ … … 427 436 428 437 SHPHandle SHPAPI_CALL 429 SHPOpen ( const char * pszLayer, const char * pszAccess )438 SHPOpenLL( const char * pszLayer, const char * pszAccess, SAHooks *psHooks ) 430 439 431 440 { … … 463 472 464 473 psSHP->bUpdated = FALSE; 474 memcpy( &(psSHP->sHooks), psHooks, sizeof(SAHooks) ); 465 475 466 476 /* -------------------------------------------------------------------- */ … … 483 493 /* -------------------------------------------------------------------- */ 484 494 pszFullname = (char *) malloc(strlen(pszBasename) + 5); 485 sprintf( pszFullname, "%s.shp", pszBasename ) ;486 psSHP->fpSHP = fopen(pszFullname, pszAccess );495 sprintf( pszFullname, "%s.shp", pszBasename ) ; 496 psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess ); 487 497 if( psSHP->fpSHP == NULL ) 488 498 { 489 499 sprintf( pszFullname, "%s.SHP", pszBasename ); 490 psSHP->fpSHP = fopen(pszFullname, pszAccess );500 psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess ); 491 501 } 492 502 … … 505 515 506 516 sprintf( pszFullname, "%s.shx", pszBasename ); 507 psSHP->fpSHX = fopen(pszFullname, pszAccess );517 psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess ); 508 518 if( psSHP->fpSHX == NULL ) 509 519 { 510 520 sprintf( pszFullname, "%s.SHX", pszBasename ); 511 psSHP->fpSHX = fopen(pszFullname, pszAccess );521 psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess ); 512 522 } 513 523 … … 519 529 pszBasename, pszBasename ); 520 530 #endif 521 fclose( psSHP->fpSHP );531 psSHP->sHooks.FClose( psSHP->fpSHP ); 522 532 free( psSHP ); 523 533 free( pszBasename ); … … 533 543 /* -------------------------------------------------------------------- */ 534 544 pabyBuf = (uchar *) malloc(100); 535 fread( pabyBuf, 100, 1, psSHP->fpSHP );545 psSHP->sHooks.FRead( pabyBuf, 100, 1, psSHP->fpSHP ); 536 546 537 547 psSHP->nFileSize = (pabyBuf[24] * 256 * 256 * 256 … … 543 553 /* Read SHX file Header info */ 544 554 /* -------------------------------------------------------------------- */ 545 if( fread( pabyBuf, 100, 1, psSHP->fpSHX ) != 1555 if( psSHP->sHooks.FRead( pabyBuf, 100, 1, psSHP->fpSHX ) != 1 546 556 || pabyBuf[0] != 0 547 557 || pabyBuf[1] != 0 … … 549 559 || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d) ) 550 560 { 551 #ifdef USE_CPL 552 CPLError( CE_Failure, CPLE_AppDefined, 553 ".shx file is unreadable, or corrupt." ); 554 #endif 555 fclose( psSHP->fpSHP ); 556 fclose( psSHP->fpSHX ); 561 psSHP->sHooks.Error( ".shx file is unreadable, or corrupt." ); 562 psSHP->sHooks.FClose( psSHP->fpSHP ); 563 psSHP->sHooks.FClose( psSHP->fpSHX ); 557 564 free( psSHP ); 558 565 … … 568 575 if( psSHP->nRecords < 0 || psSHP->nRecords > 256000000 ) 569 576 { 570 #ifdef USE_CPL 571 CPLError( CE_Failure, CPLE_AppDefined, 572 "Record count in .shp header is %d, which seems\n" 573 "unreasonable. Assuming header is corrupt.", 577 char szError[200]; 578 579 sprintf( szError, 580 "Record count in .shp header is %d, which seems\n" 581 "unreasonable. Assuming header is corrupt.", 574 582 psSHP->nRecords ); 575 #endif 576 fclose( psSHP->fpSHP );577 fclose( psSHP->fpSHX );583 psSHP->sHooks.Error( szError ); 584 psSHP->sHooks.FClose( psSHP->fpSHP ); 585 psSHP->sHooks.FClose( psSHP->fpSHX ); 578 586 free( psSHP ); 579 587 free(pabyBuf); … … 635 643 pabyBuf == NULL) 636 644 { 637 #ifdef USE_CPL 638 CPLError(CE_Failure, CPLE_AppDefined, 639 "Not enough memory to allocate requested memory (nRecords=%d, nParts=%d). " 640 "Probably broken SHP file", psSHP->nRecords ); 641 #endif 642 fclose( psSHP->fpSHP ); 643 fclose( psSHP->fpSHX ); 645 char szError[200]; 646 647 sprintf(szError, 648 "Not enough memory to allocate requested memory (nRecords=%d).\n" 649 "Probably broken SHP file", 650 psSHP->nRecords ); 651 psSHP->sHooks.Error( szError ); 652 psSHP->sHooks.FClose( psSHP->fpSHP ); 653 psSHP->sHooks.FClose( psSHP->fpSHX ); 644 654 if (psSHP->panRecOffset) free( psSHP->panRecOffset ); 645 655 if (psSHP->panRecSize) free( psSHP->panRecSize ); … … 649 659 } 650 660 651 if( (int) fread( pabyBuf, 8, psSHP->nRecords, psSHP->fpSHX )661 if( (int) psSHP->sHooks.FRead( pabyBuf, 8, psSHP->nRecords, psSHP->fpSHX ) 652 662 != psSHP->nRecords ) 653 663 { 654 #ifdef USE_CPL 655 CPLError( CE_Failure, CPLE_AppDefined, 656 "Failed to read all values for %d records in .shx file.", 657 psSHP->nRecords ); 658 #endif 664 char szError[200]; 665 666 sprintf( szError, 667 "Failed to read all values for %d records in .shx file.", 668 psSHP->nRecords ); 669 psSHP->sHooks.Error( szError ); 670 659 671 /* SHX is short or unreadable for some reason. */ 660 fclose( psSHP->fpSHP );661 fclose( psSHP->fpSHX );672 psSHP->sHooks.FClose( psSHP->fpSHP ); 673 psSHP->sHooks.FClose( psSHP->fpSHX ); 662 674 free( psSHP->panRecOffset ); 663 675 free( psSHP->panRecSize ); … … 671 683 if (strcmp(pszAccess, "rb") == 0) 672 684 { 673 fclose( psSHP->fpSHX );685 psSHP->sHooks.FClose( psSHP->fpSHX ); 674 686 psSHP->fpSHX = NULL; 675 687 } … … 719 731 720 732 if ( psSHP->fpSHX != NULL) 721 fclose( psSHP->fpSHX );722 fclose( psSHP->fpSHP );733 psSHP->sHooks.FClose( psSHP->fpSHX ); 734 psSHP->sHooks.FClose( psSHP->fpSHP ); 723 735 724 736 if( psSHP->pabyRec != NULL ) … … 772 784 773 785 { 786 SAHooks sHooks; 787 788 SASetupDefaultHooks( &sHooks ); 789 790 return SHPCreateLL( pszLayer, nShapeType, &sHooks ); 791 } 792 793 /************************************************************************/ 794 /* SHPCreate() */ 795 /* */ 796 /* Create a new shape file and return a handle to the open */ 797 /* shape file with read/write access. */ 798 /************************************************************************/ 799 800 SHPHandle SHPAPI_CALL 801 SHPCreateLL( const char * pszLayer, int nShapeType, SAHooks *psHooks ) 802 803 { 774 804 char *pszBasename, *pszFullname; 775 805 int i; 776 FILE *fpSHP, *fpSHX;806 SAFile fpSHP, fpSHX; 777 807 uchar abyHeader[100]; 778 808 int32 i32; … … 807 837 pszFullname = (char *) malloc(strlen(pszBasename) + 5); 808 838 sprintf( pszFullname, "%s.shp", pszBasename ); 809 fpSHP = fopen(pszFullname, "wb" );839 fpSHP = psHooks->FOpen(pszFullname, "wb" ); 810 840 if( fpSHP == NULL ) 811 841 { 812 #ifdef USE_CPL 813 CPLError( CE_Failure, CPLE_AppDefined, 814 "Failed to create file %s.", 815 pszFullname ); 816 #endif 842 psHooks->Error( "Failed to create file .shp file." ); 817 843 return( NULL ); 818 844 } 819 845 820 846 sprintf( pszFullname, "%s.shx", pszBasename ); 821 fpSHX = fopen(pszFullname, "wb" );847 fpSHX = psHooks->FOpen(pszFullname, "wb" ); 822 848 if( fpSHX == NULL ) 823 849 { 824 #ifdef USE_CPL 825 CPLError( CE_Failure, CPLE_AppDefined, 826 "Failed to create file %s.", 827 pszFullname ); 828 #endif 850 psHooks->Error( "Failed to create file .shx file." ); 829 851 return( NULL ); 830 852 } … … 863 885 /* Write .shp file header. */ 864 886 /* -------------------------------------------------------------------- */ 865 if( fwrite( abyHeader, 100, 1, fpSHP ) != 1 ) 866 { 867 #ifdef USE_CPL 868 CPLError( CE_Failure, CPLE_AppDefined, 869 "Failed to write .shp header." ); 870 #endif 887 if( psHooks->FWrite( abyHeader, 100, 1, fpSHP ) != 1 ) 888 { 889 psHooks->Error( "Failed to write .shp header." ); 871 890 return NULL; 872 891 } … … 879 898 if( !bBigEndian ) SwapWord( 4, abyHeader+24 ); 880 899 881 if( fwrite( abyHeader, 100, 1, fpSHX ) != 1 ) 882 { 883 #ifdef USE_CPL 884 CPLError( CE_Failure, CPLE_AppDefined, 885 "Failed to write .shx header." ); 886 #endif 900 if( psHooks->FWrite( abyHeader, 100, 1, fpSHX ) != 1 ) 901 { 902 psHooks->Error( "Failed to write .shx header." ); 887 903 return NULL; 888 904 } … … 891 907 /* Close the files, and then open them as regular existing files. */ 892 908 /* -------------------------------------------------------------------- */ 893 fclose( fpSHP );894 fclose( fpSHX );895 896 return( SHPOpen ( pszLayer, "r+b") );909 psHooks->FClose( fpSHP ); 910 psHooks->FClose( fpSHX ); 911 912 return( SHPOpenLL( pszLayer, "r+b", psHooks ) ); 897 913 } 898 914 … … 1037 1053 1038 1054 if( psObject->panPartStart[0] != 0 ) 1039 {1040 #ifdef USE_CPL1041 CPLError( CE_Failure, CPLE_AppDefined,1042 "panPartStart[0] != 0, patching internally. Please fix your code!\n" );1043 #else1044 fprintf( stderr, "panPartStart[0] != 0, patching internally. Please fix your code!\n" );1045 #endif1046 1055 psObject->panPartStart[0] = 0; 1047 }1048 1056 } 1049 1057 … … 1427 1435 /* Write out record. */ 1428 1436 /* -------------------------------------------------------------------- */ 1429 if( fseek( psSHP->fpSHP, nRecordOffset, 0 ) != 0 1430 || fwrite( pabyRec, nRecordSize, 1, psSHP->fpSHP ) < 1 ) 1431 { 1432 #ifdef USE_CPL 1433 CPLError( CE_Failure, CPLE_FileIO, 1434 "Error in fseek() or fwrite() writing object to .shp file." ); 1435 #endif 1437 if( psSHP->sHooks.FSeek( psSHP->fpSHP, nRecordOffset, 0 ) != 0 1438 || psSHP->sHooks.FWrite( pabyRec, nRecordSize, 1, psSHP->fpSHP ) < 1 ) 1439 { 1440 psSHP->sHooks.Error( "Error in psSHP->sHooks.FSeek() or fwrite() writing object to .shp file." ); 1436 1441 free( pabyRec ); 1437 1442 return -1; … … 1507 1512 if (psSHP->pabyRec == NULL) 1508 1513 { 1509 #ifdef USE_CPL 1510 CPLError(CE_Failure, CPLE_AppDefined, 1514 char szError[200]; 1515 1516 sprintf( szError, 1511 1517 "Not enough memory to allocate requested memory (nBufSize=%d). " 1512 1518 "Probably broken SHP file", psSHP->nBufSize ); 1513 #endif 1519 psSHP->sHooks.Error( szError ); 1514 1520 return NULL; 1515 1521 } … … 1519 1525 /* Read the record. */ 1520 1526 /* -------------------------------------------------------------------- */ 1521 if( fseek( psSHP->fpSHP, psSHP->panRecOffset[hEntity], 0 ) != 01522 || fread( psSHP->pabyRec, psSHP->panRecSize[hEntity]+8, 1,1527 if( psSHP->sHooks.FSeek( psSHP->fpSHP, psSHP->panRecOffset[hEntity], 0 ) != 0 1528 || psSHP->sHooks.FRead( psSHP->pabyRec, psSHP->panRecSize[hEntity]+8, 1, 1523 1529 psSHP->fpSHP ) != 1 ) 1524 1530 { … … 1528 1534 */ 1529 1535 1530 #ifdef USE_CPL 1531 CPLDebug( "Shape", "Error in fseek() or fread() reading object from .shp file." ); 1532 #endif 1536 psSHP->sHooks.Error( "Error in fseek() or fread() reading object from .shp file." ); 1533 1537 return NULL; 1534 1538 }
