PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ SHPCreateLL()

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

Definition at line 997 of file shpopen.c.

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