PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ SHPCreateLL()

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

Definition at line 999 of file shpopen.c.

1001 {
1002  char *pszFullname;
1003  SAFile fpSHP;
1004  SAFile fpSHX = SHPLIB_NULLPTR;
1005  uchar abyHeader[100];
1006  int32 i32;
1007  double dValue;
1008  int nLenWithoutExtension;
1009 
1010 /* -------------------------------------------------------------------- */
1011 /* Establish the byte order on this system. */
1012 /* -------------------------------------------------------------------- */
1013 #if !defined(bBigEndian)
1014  {
1015  int i = 1;
1016  if( *((uchar *) &i) == 1 )
1017  bBigEndian = FALSE;
1018  else
1019  bBigEndian = TRUE;
1020  }
1021 #endif
1022 
1023 /* -------------------------------------------------------------------- */
1024 /* Open the two files so we can write their headers. */
1025 /* -------------------------------------------------------------------- */
1026  nLenWithoutExtension = SHPGetLenWithoutExtension(pszLayer);
1027  pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
1028  memcpy(pszFullname, pszLayer, nLenWithoutExtension);
1029  memcpy(pszFullname + nLenWithoutExtension, ".shp", 5);
1030  fpSHP = psHooks->FOpen(pszFullname, "wb" );
1031  if( fpSHP == SHPLIB_NULLPTR )
1032  {
1033  char szErrorMsg[200];
1034  snprintf( szErrorMsg, sizeof(szErrorMsg),
1035  "Failed to create file %s: %s",
1036  pszFullname, strerror(errno) );
1037  psHooks->Error( szErrorMsg );
1038 
1039  goto error;
1040  }
1041 
1042  memcpy(pszFullname + nLenWithoutExtension, ".shx", 5);
1043  fpSHX = psHooks->FOpen(pszFullname, "wb" );
1044  if( fpSHX == SHPLIB_NULLPTR )
1045  {
1046  char szErrorMsg[200];
1047  snprintf( szErrorMsg, sizeof(szErrorMsg),
1048  "Failed to create file %s: %s",
1049  pszFullname, strerror(errno) );
1050  psHooks->Error( szErrorMsg );
1051  goto error;
1052  }
1053 
1054  free( pszFullname ); pszFullname = SHPLIB_NULLPTR;
1055 
1056 /* -------------------------------------------------------------------- */
1057 /* Prepare header block for .shp file. */
1058 /* -------------------------------------------------------------------- */
1059  memset( abyHeader, 0, sizeof(abyHeader) );
1060 
1061  abyHeader[2] = 0x27; /* magic cookie */
1062  abyHeader[3] = 0x0a;
1063 
1064  i32 = 50; /* file size */
1065  ByteCopy( &i32, abyHeader+24, 4 );
1066  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
1067 
1068  i32 = 1000; /* version */
1069  ByteCopy( &i32, abyHeader+28, 4 );
1070  if( bBigEndian ) SwapWord( 4, abyHeader+28 );
1071 
1072  i32 = nShapeType; /* shape type */
1073  ByteCopy( &i32, abyHeader+32, 4 );
1074  if( bBigEndian ) SwapWord( 4, abyHeader+32 );
1075 
1076  dValue = 0.0; /* set bounds */
1077  ByteCopy( &dValue, abyHeader+36, 8 );
1078  ByteCopy( &dValue, abyHeader+44, 8 );
1079  ByteCopy( &dValue, abyHeader+52, 8 );
1080  ByteCopy( &dValue, abyHeader+60, 8 );
1081 
1082 /* -------------------------------------------------------------------- */
1083 /* Write .shp file header. */
1084 /* -------------------------------------------------------------------- */
1085  if( psHooks->FWrite( abyHeader, 100, 1, fpSHP ) != 1 )
1086  {
1087  char szErrorMsg[200];
1088 
1089  snprintf( szErrorMsg, sizeof(szErrorMsg),
1090  "Failed to write .shp header: %s", strerror(errno) );
1091  szErrorMsg[sizeof(szErrorMsg)-1] = '\0';
1092  psHooks->Error( szErrorMsg );
1093 
1094  goto error;
1095  }
1096 
1097 /* -------------------------------------------------------------------- */
1098 /* Prepare, and write .shx file header. */
1099 /* -------------------------------------------------------------------- */
1100  i32 = 50; /* file size */
1101  ByteCopy( &i32, abyHeader+24, 4 );
1102  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
1103 
1104  if( psHooks->FWrite( abyHeader, 100, 1, fpSHX ) != 1 )
1105  {
1106  char szErrorMsg[200];
1107 
1108  snprintf( szErrorMsg, sizeof(szErrorMsg),
1109  "Failure writing .shx header: %s", strerror(errno) );
1110  szErrorMsg[sizeof(szErrorMsg)-1] = '\0';
1111  psHooks->Error( szErrorMsg );
1112 
1113  goto error;
1114  }
1115 
1116 /* -------------------------------------------------------------------- */
1117 /* Close the files, and then open them as regular existing files. */
1118 /* -------------------------------------------------------------------- */
1119  psHooks->FClose( fpSHP );
1120  psHooks->FClose( fpSHX );
1121 
1122  return( SHPOpenLL( pszLayer, "r+b", psHooks ) );
1123 
1124 error:
1125  if (pszFullname) free(pszFullname);
1126  if (fpSHP) psHooks->FClose( fpSHP );
1127  if (fpSHX) psHooks->FClose( fpSHX );
1128  return SHPLIB_NULLPTR;
1129 }
void * malloc(YYSIZE_T)
void free(void *)
int * SAFile
Definition: shapefil.h:283
SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess, SAHooks *psHooks)
Definition: shpopen.c:329
static int bBigEndian
Definition: shpopen.c:93
unsigned int int32
Definition: shpopen.c:54
unsigned char uchar
Definition: shpopen.c:49
static void SwapWord(int length, void *wordP)
Definition: shpopen.c:110
static int SHPGetLenWithoutExtension(const char *pszBasename)
Definition: shpopen.c:305
#define STATIC_CAST(type, x)
Definition: shpopen.c:100
#define TRUE
Definition: shpopen.c:59
#define FALSE
Definition: shpopen.c:58
#define SHPLIB_NULLPTR
Definition: shpopen.c:101
#define ByteCopy(a, b, c)
Definition: shpopen.c:62
void(* Error)(const char *message)
Definition: shapefil.h:299
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:290
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:292
int(* FClose)(SAFile file)
Definition: shapefil.h:296

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

Referenced by SHPCreate().

Here is the call graph for this function:
Here is the caller graph for this function: