704 int nLenWithoutExtension;
705 unsigned int nSHPFilesize;
707 unsigned int nCurrentSHPOffset = 100;
708 size_t nRealSHXContentSize = 100;
710 const char pszSHXAccess[] =
"w+b";
712 char abyReadedRecord[8];
713 unsigned int niRecord = 0;
714 unsigned int nRecordLength = 0;
715 unsigned int nRecordOffset = 50;
722 if( strcmp(pszAccess,
"rb+") == 0 || strcmp(pszAccess,
"r+b") == 0
723 || strcmp(pszAccess,
"r+") == 0 )
733 #if !defined(bBigEndian)
736 if( *((
uchar *) &i) == 1 )
749 memcpy(pszFullname, pszLayer, nLenWithoutExtension);
750 memcpy(pszFullname + nLenWithoutExtension,
".shp", 5);
751 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
754 memcpy(pszFullname + nLenWithoutExtension,
".SHP", 5);
755 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
760 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
763 pszFullname[nLenWithoutExtension] = 0;
764 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shp or %s.SHP.",
765 pszFullname, pszFullname );
766 psHooks->
Error( pszMessage );
778 if( psHooks->
FRead( pabyBuf, 100, 1, fpSHP ) != 1 )
780 psHooks->
Error(
".shp file is unreadable, or corrupt." );
789 nSHPFilesize = (
STATIC_CAST(
unsigned int, pabyBuf[24])<<24)|(pabyBuf[25]<<16)|
790 (pabyBuf[26]<<8)|pabyBuf[27];
791 if( nSHPFilesize < UINT_MAX / 2 )
794 nSHPFilesize = (UINT_MAX / 2) * 2;
796 memcpy(pszFullname + nLenWithoutExtension,
".shx", 5);
797 fpSHX = psHooks->
FOpen( pszFullname, pszSHXAccess );
800 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
802 pszFullname[nLenWithoutExtension] = 0;
803 snprintf( pszMessage, nMessageLen,
804 "Error opening file %s.shx for writing", pszFullname );
805 psHooks->
Error( pszMessage );
819 psHooks->
FSeek( fpSHP, 100, 0 );
821 memcpy( pabySHXHeader, pabyBuf, 100 );
822 psHooks->
FWrite( pabySHXHeader, 100, 1, fpSHX );
825 while( nCurrentSHPOffset < nSHPFilesize )
827 if( psHooks->
FRead( &niRecord, 4, 1, fpSHP ) == 1 &&
828 psHooks->
FRead( &nRecordLength, 4, 1, fpSHP ) == 1)
831 memcpy( abyReadedRecord, &nRecordOffset, 4 );
832 memcpy( abyReadedRecord + 4, &nRecordLength, 4 );
834 psHooks->
FWrite( abyReadedRecord, 8, 1, fpSHX );
838 nRecordOffset += nRecordLength + 4;
839 nCurrentSHPOffset += 8 + nRecordLength * 2;
841 psHooks->
FSeek( fpSHP, nCurrentSHPOffset, 0 );
842 nRealSHXContentSize += 8;
846 psHooks->
Error(
"Error parsing .shp to restore .shx" );
851 free( pabySHXHeader );
858 nRealSHXContentSize /= 2;
860 psHooks->
FSeek( fpSHX, 24, 0 );
861 psHooks->
FWrite( &nRealSHXContentSize, 4, 1, fpSHX );
866 free ( pszFullname );
867 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)