PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ SHPCreateLL()

SHPHandle SHPAPI_CALL SHPCreateLL ( const char *  pszLayer,
int  nShapeType,
SAHooks psHooks 
)

Definition at line 846 of file shpopen.c.

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

Referenced by SHPCreate().

848 {
849  char *pszBasename = NULL, *pszFullname = NULL;
850  int i;
851  SAFile fpSHP = NULL, fpSHX = NULL;
852  uchar abyHeader[100];
853  int32 i32;
854  double dValue;
855 
856 /* -------------------------------------------------------------------- */
857 /* Establish the byte order on this system. */
858 /* -------------------------------------------------------------------- */
859  i = 1;
860  if( *((uchar *) &i) == 1 )
861  bBigEndian = FALSE;
862  else
863  bBigEndian = TRUE;
864 
865 /* -------------------------------------------------------------------- */
866 /* Compute the base (layer) name. If there is any extension */
867 /* on the passed in filename we will strip it off. */
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 /* Open the two files so we can write their headers. */
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 /* Prepare header block for .shp file. */
904 /* -------------------------------------------------------------------- */
905  for( i = 0; i < 100; i++ )
906  abyHeader[i] = 0;
907 
908  abyHeader[2] = 0x27; /* magic cookie */
909  abyHeader[3] = 0x0a;
910 
911  i32 = 50; /* file size */
912  ByteCopy( &i32, abyHeader+24, 4 );
913  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
914 
915  i32 = 1000; /* version */
916  ByteCopy( &i32, abyHeader+28, 4 );
917  if( bBigEndian ) SwapWord( 4, abyHeader+28 );
918 
919  i32 = nShapeType; /* shape type */
920  ByteCopy( &i32, abyHeader+32, 4 );
921  if( bBigEndian ) SwapWord( 4, abyHeader+32 );
922 
923  dValue = 0.0; /* set bounds */
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 /* Write .shp file header. */
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 /* Prepare, and write .shx file header. */
940 /* -------------------------------------------------------------------- */
941  i32 = 50; /* file size */
942  ByteCopy( &i32, abyHeader+24, 4 );
943  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
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 /* Close the files, and then open them as regular existing files. */
953 /* -------------------------------------------------------------------- */
954  psHooks->FClose( fpSHP );
955  psHooks->FClose( fpSHX );
956 
957  return( SHPOpenLL( pszLayer, "r+b", psHooks ) );
958 
959 error:
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 }
unsigned int int32
Definition: shpopen.c:273
#define ByteCopy(a, b, c)
Definition: shpopen.c:281
int(* FClose)(SAFile file)
Definition: shapefil.h:261
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:257
#define FALSE
Definition: shpopen.c:277
static int bBigEndian
Definition: shpopen.c:293
void free(void *)
void * malloc(YYSIZE_T)
int * SAFile
Definition: shapefil.h:242
SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, SAHooks *psHooks)
Definition: shpopen.c:482
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:255
#define TRUE
Definition: shpopen.c:278
static void SwapWord(int length, void *wordP)
Definition: shpopen.c:302
unsigned char uchar
Definition: shpopen.c:268
void(* Error)(const char *message)
Definition: shapefil.h:264
Here is the call graph for this function:
Here is the caller graph for this function: