711 int nLenWithoutExtension;
712 unsigned int nSHPFilesize;
714 unsigned int nCurrentSHPOffset = 100;
715 size_t nRealSHXContentSize = 100;
717 const char pszSHXAccess[] =
"w+b";
719 char abyReadedRecord[8];
720 unsigned int niRecord = 0;
721 unsigned int nRecordLength = 0;
722 unsigned int nRecordOffset = 50;
729 if( strcmp(pszAccess,
"rb+") == 0 || strcmp(pszAccess,
"r+b") == 0
730 || strcmp(pszAccess,
"r+") == 0 )
740 #if !defined(bBigEndian)
743 if( *((
uchar *) &i) == 1 )
758 memcpy(pszFullname, pszLayer, nLenWithoutExtension);
759 memcpy(pszFullname + nLenWithoutExtension,
".shp", 5);
760 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
763 memcpy(pszFullname + nLenWithoutExtension,
".SHP", 5);
764 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
769 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
772 pszFullname[nLenWithoutExtension] = 0;
773 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shp or %s.SHP.",
774 pszFullname, pszFullname );
775 psHooks->
Error( pszMessage );
787 if( psHooks->
FRead( pabyBuf, 100, 1, fpSHP ) != 1 )
789 psHooks->
Error(
".shp file is unreadable, or corrupt." );
798 nSHPFilesize = (
STATIC_CAST(
unsigned int, pabyBuf[24])<<24)|(pabyBuf[25]<<16)|
799 (pabyBuf[26]<<8)|pabyBuf[27];
800 if( nSHPFilesize < UINT_MAX / 2 )
803 nSHPFilesize = (UINT_MAX / 2) * 2;
805 memcpy(pszFullname + nLenWithoutExtension,
".shx", 5);
806 fpSHX = psHooks->
FOpen( pszFullname, pszSHXAccess );
809 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
811 pszFullname[nLenWithoutExtension] = 0;
812 snprintf( pszMessage, nMessageLen,
813 "Error opening file %s.shx for writing", pszFullname );
814 psHooks->
Error( pszMessage );
828 psHooks->
FSeek( fpSHP, 100, 0 );
830 memcpy( pabySHXHeader, pabyBuf, 100 );
831 psHooks->
FWrite( pabySHXHeader, 100, 1, fpSHX );
834 while( nCurrentSHPOffset < nSHPFilesize )
836 if( psHooks->
FRead( &niRecord, 4, 1, fpSHP ) == 1 &&
837 psHooks->
FRead( &nRecordLength, 4, 1, fpSHP ) == 1)
840 memcpy( abyReadedRecord, &nRecordOffset, 4 );
841 memcpy( abyReadedRecord + 4, &nRecordLength, 4 );
843 psHooks->
FWrite( abyReadedRecord, 8, 1, fpSHX );
847 nRecordOffset += nRecordLength + 4;
848 nCurrentSHPOffset += 8 + nRecordLength * 2;
850 psHooks->
FSeek( fpSHP, nCurrentSHPOffset, 0 );
851 nRealSHXContentSize += 8;
855 psHooks->
Error(
"Error parsing .shp to restore .shx" );
860 free( pabySHXHeader );
867 nRealSHXContentSize /= 2;
869 psHooks->
FSeek( fpSHX, 24, 0 );
870 psHooks->
FWrite( &nRealSHXContentSize, 4, 1, fpSHX );
875 free ( pszFullname );
876 free ( pabySHXHeader );
static void SwapWord(int length, void *wordP)
static int SHPGetLenWithoutExtension(const char *pszBasename)
#define STATIC_CAST(type, x)
void(* Error)(const char *message)
SAFile(* FOpen)(const char *filename, const char *access)
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
int(* FClose)(SAFile file)
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)