PostGIS  2.1.10dev-r@@SVN_REVISION@@
SHPHandle SHPAPI_CALL SHPCreateLL ( const char *  pszShapeFile,
int  nShapeType,
SAHooks psHooks 
)

Definition at line 847 of file shpopen.c.

References ByteCopy, SAHooks::Error, FALSE, SAHooks::FClose, SAHooks::FOpen, SAHooks::FWrite, SHPOpenLL(), SwapWord(), and TRUE.

Referenced by SHPCreate().

849 {
850  char *pszBasename = NULL, *pszFullname = NULL;
851  int i;
852  SAFile fpSHP = NULL, fpSHX = NULL;
853  uchar abyHeader[100];
854  uint32 i32;
855  double dValue;
856 
857 /* -------------------------------------------------------------------- */
858 /* Establish the byte order on this system. */
859 /* -------------------------------------------------------------------- */
860  i = 1;
861  if( *((uchar *) &i) == 1 )
862  bBigEndian = FALSE;
863  else
864  bBigEndian = TRUE;
865 
866 /* -------------------------------------------------------------------- */
867 /* Compute the base (layer) name. If there is any extension */
868 /* on the passed in filename we will strip it off. */
869 /* -------------------------------------------------------------------- */
870  pszBasename = (char *) malloc(strlen(pszLayer)+5);
871  strcpy( pszBasename, pszLayer );
872  for( i = strlen(pszBasename)-1;
873  i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
874  && pszBasename[i] != '\\';
875  i-- ) {}
876 
877  if( pszBasename[i] == '.' )
878  pszBasename[i] = '\0';
879 
880 /* -------------------------------------------------------------------- */
881 /* Open the two files so we can write their headers. */
882 /* -------------------------------------------------------------------- */
883  pszFullname = (char *) malloc(strlen(pszBasename) + 5);
884  sprintf( pszFullname, "%s.shp", pszBasename );
885  fpSHP = psHooks->FOpen(pszFullname, "wb" );
886  if( fpSHP == NULL )
887  {
888  psHooks->Error( "Failed to create file .shp file." );
889  goto error;
890  }
891 
892  sprintf( pszFullname, "%s.shx", pszBasename );
893  fpSHX = psHooks->FOpen(pszFullname, "wb" );
894  if( fpSHX == NULL )
895  {
896  psHooks->Error( "Failed to create file .shx file." );
897  goto error;
898  }
899 
900  free( pszFullname ); pszFullname = NULL;
901  free( pszBasename ); pszBasename = NULL;
902 
903 /* -------------------------------------------------------------------- */
904 /* Prepare header block for .shp file. */
905 /* -------------------------------------------------------------------- */
906  for( i = 0; i < 100; i++ )
907  abyHeader[i] = 0;
908 
909  abyHeader[2] = 0x27; /* magic cookie */
910  abyHeader[3] = 0x0a;
911 
912  i32 = 50; /* file size */
913  ByteCopy( &i32, abyHeader+24, 4 );
914  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
915 
916  i32 = 1000; /* version */
917  ByteCopy( &i32, abyHeader+28, 4 );
918  if( bBigEndian ) SwapWord( 4, abyHeader+28 );
919 
920  i32 = nShapeType; /* shape type */
921  ByteCopy( &i32, abyHeader+32, 4 );
922  if( bBigEndian ) SwapWord( 4, abyHeader+32 );
923 
924  dValue = 0.0; /* set bounds */
925  ByteCopy( &dValue, abyHeader+36, 8 );
926  ByteCopy( &dValue, abyHeader+44, 8 );
927  ByteCopy( &dValue, abyHeader+52, 8 );
928  ByteCopy( &dValue, abyHeader+60, 8 );
929 
930 /* -------------------------------------------------------------------- */
931 /* Write .shp file header. */
932 /* -------------------------------------------------------------------- */
933  if( psHooks->FWrite( abyHeader, 100, 1, fpSHP ) != 1 )
934  {
935  psHooks->Error( "Failed to write .shp header." );
936  goto error;
937  }
938 
939 /* -------------------------------------------------------------------- */
940 /* Prepare, and write .shx file header. */
941 /* -------------------------------------------------------------------- */
942  i32 = 50; /* file size */
943  ByteCopy( &i32, abyHeader+24, 4 );
944  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
945 
946  if( psHooks->FWrite( abyHeader, 100, 1, fpSHX ) != 1 )
947  {
948  psHooks->Error( "Failed to write .shx header." );
949  goto error;
950  }
951 
952 /* -------------------------------------------------------------------- */
953 /* Close the files, and then open them as regular existing files. */
954 /* -------------------------------------------------------------------- */
955  psHooks->FClose( fpSHP );
956  psHooks->FClose( fpSHX );
957 
958  return( SHPOpenLL( pszLayer, "r+b", psHooks ) );
959 
960 error:
961  if (pszFullname) free(pszFullname);
962  if (pszBasename) free(pszBasename);
963  if (fpSHP) psHooks->FClose( fpSHP );
964  if (fpSHX) psHooks->FClose( fpSHX );
965  return NULL;
966 }
#define ByteCopy(a, b, c)
Definition: shpopen.c:282
int(* FClose)(SAFile file)
Definition: shapefil.h:262
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:258
#define FALSE
Definition: shpopen.c:278
static int bBigEndian
Definition: shpopen.c:294
unsigned int uint32
Definition: shpopen.c:274
int * SAFile
Definition: shapefil.h:243
SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, SAHooks *psHooks)
Definition: shpopen.c:483
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:256
#define TRUE
Definition: shpopen.c:279
static void SwapWord(int length, void *wordP)
Definition: shpopen.c:303
unsigned char uchar
Definition: shpopen.c:269
void(* Error)(const char *message)
Definition: shapefil.h:265

Here is the call graph for this function:

Here is the caller graph for this function: