704 int nLenWithoutExtension;
705 unsigned int nSHPFilesize;
707 unsigned int nCurrentRecordOffset = 0;
708 unsigned int nCurrentSHPOffset = 100;
709 size_t nRealSHXContentSize = 100;
711 const char pszSHXAccess[] =
"w+b";
713 char abyReadedRecord[8];
714 unsigned int niRecord = 0;
715 unsigned int nRecordLength = 0;
716 unsigned int nRecordOffset = 50;
723 if( strcmp(pszAccess,
"rb+") == 0 || strcmp(pszAccess,
"r+b") == 0
724 || strcmp(pszAccess,
"r+") == 0 )
734 #if !defined(bBigEndian)
737 if( *((
uchar *) &i) == 1 )
750 memcpy(pszFullname, pszLayer, nLenWithoutExtension);
751 memcpy(pszFullname + nLenWithoutExtension,
".shp", 5);
752 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
755 memcpy(pszFullname + nLenWithoutExtension,
".SHP", 5);
756 fpSHP = psHooks->
FOpen(pszFullname, pszAccess );
761 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
764 pszFullname[nLenWithoutExtension] = 0;
765 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shp or %s.SHP.",
766 pszFullname, pszFullname );
767 psHooks->
Error( pszMessage );
779 if( psHooks->
FRead( pabyBuf, 100, 1, fpSHP ) != 1 )
781 psHooks->
Error(
".shp file is unreadable, or corrupt." );
790 nSHPFilesize = (
STATIC_CAST(
unsigned int, pabyBuf[24])<<24)|(pabyBuf[25]<<16)|
791 (pabyBuf[26]<<8)|pabyBuf[27];
792 if( nSHPFilesize < UINT_MAX / 2 )
795 nSHPFilesize = (UINT_MAX / 2) * 2;
797 memcpy(pszFullname + nLenWithoutExtension,
".shx", 5);
798 fpSHX = psHooks->
FOpen( pszFullname, pszSHXAccess );
801 size_t nMessageLen = strlen( pszFullname ) * 2 + 256;
803 pszFullname[nLenWithoutExtension] = 0;
804 snprintf( pszMessage, nMessageLen,
805 "Error opening file %s.shx for writing", pszFullname );
806 psHooks->
Error( pszMessage );
820 psHooks->
FSeek( fpSHP, 100, 0 );
822 memcpy( pabySHXHeader, pabyBuf, 100 );
823 psHooks->
FWrite( pabySHXHeader, 100, 1, fpSHX );
826 while( nCurrentSHPOffset < nSHPFilesize )
828 if( psHooks->
FRead( &niRecord, 4, 1, fpSHP ) == 1 &&
829 psHooks->
FRead( &nRecordLength, 4, 1, fpSHP ) == 1)
832 memcpy( abyReadedRecord, &nRecordOffset, 4 );
833 memcpy( abyReadedRecord + 4, &nRecordLength, 4 );
835 psHooks->
FWrite( abyReadedRecord, 8, 1, fpSHX );
839 nRecordOffset += nRecordLength + 4;
840 nCurrentRecordOffset += 8;
841 nCurrentSHPOffset += 8 + nRecordLength * 2;
843 psHooks->
FSeek( fpSHP, nCurrentSHPOffset, 0 );
844 nRealSHXContentSize += 8;
848 psHooks->
Error(
"Error parsing .shp to restore .shx" );
853 free( pabySHXHeader );
860 nRealSHXContentSize /= 2;
862 psHooks->
FSeek( fpSHX, 24, 0 );
863 psHooks->
FWrite( &nRealSHXContentSize, 4, 1, fpSHX );
868 free ( pszFullname );
869 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)