848{
849 char *pszBasename = NULL, *pszFullname = NULL;
850 int i;
851 SAFile fpSHP = NULL, fpSHX = NULL;
852 uchar abyHeader[100];
854 double dValue;
855
856
857
858
859 i = 1;
860 if( *((
uchar *) &i) == 1 )
862 else
864
865
866
867
868
869 pszBasename = (
char *)
malloc(strlen(pszLayer)+5);
870 strcpy( pszBasename, pszLayer );
871 for( i = strlen(pszBasename)-1;
872 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
873 && pszBasename[i] != '\\';
874 i-- ) {}
875
876 if( pszBasename[i] == '.' )
877 pszBasename[i] = '\0';
878
879
880
881
882 pszFullname = (
char *)
malloc(strlen(pszBasename) + 5);
883 sprintf( pszFullname, "%s.shp", pszBasename );
884 fpSHP = psHooks->
FOpen(pszFullname,
"wb" );
885 if( fpSHP == NULL )
886 {
887 psHooks->
Error(
"Failed to create file .shp file." );
888 goto error;
889 }
890
891 sprintf( pszFullname, "%s.shx", pszBasename );
892 fpSHX = psHooks->
FOpen(pszFullname,
"wb" );
893 if( fpSHX == NULL )
894 {
895 psHooks->
Error(
"Failed to create file .shx file." );
896 goto error;
897 }
898
899 free( pszFullname ); pszFullname = NULL;
900 free( pszBasename ); pszBasename = NULL;
901
902
903
904
905 for( i = 0; i < 100; i++ )
906 abyHeader[i] = 0;
907
908 abyHeader[2] = 0x27;
909 abyHeader[3] = 0x0a;
910
911 i32 = 50;
914
915 i32 = 1000;
918
919 i32 = nShapeType;
922
923 dValue = 0.0;
924 ByteCopy( &dValue, abyHeader+36, 8 );
925 ByteCopy( &dValue, abyHeader+44, 8 );
926 ByteCopy( &dValue, abyHeader+52, 8 );
927 ByteCopy( &dValue, abyHeader+60, 8 );
928
929
930
931
932 if( psHooks->
FWrite( abyHeader, 100, 1, fpSHP ) != 1 )
933 {
934 psHooks->
Error(
"Failed to write .shp header." );
935 goto error;
936 }
937
938
939
940
941 i32 = 50;
944
945 if( psHooks->
FWrite( abyHeader, 100, 1, fpSHX ) != 1 )
946 {
947 psHooks->
Error(
"Failed to write .shx header." );
948 goto error;
949 }
950
951
952
953
956
957 return(
SHPOpenLL( pszLayer,
"r+b", psHooks ) );
958
959error:
960 if (pszFullname)
free(pszFullname);
961 if (pszBasename)
free(pszBasename);
962 if (fpSHP) psHooks->
FClose( fpSHP );
963 if (fpSHX) psHooks->
FClose( fpSHX );
964 return NULL;
965}
SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, SAHooks *psHooks)
static void SwapWord(int length, void *wordP)
#define ByteCopy(a, b, c)
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)