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

◆ SHPCreateLL()

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

Definition at line 1006 of file shpopen.c.

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