Skip to content

Commit

Permalink
add userdata ptr to SAHooks, available during file open/removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxxen committed Dec 3, 2023
1 parent db794f5 commit 01ee129
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
16 changes: 9 additions & 7 deletions dbfopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,22 @@ DBFHandle SHPAPI_CALL DBFOpenLL(const char *pszFilename, const char *pszAccess,
memcpy(pszFullname + nLenWithoutExtension, ".dbf", 5);

DBFHandle psDBF = STATIC_CAST(DBFHandle, calloc(1, sizeof(DBFInfo)));
psDBF->fp = psHooks->FOpen(pszFullname, pszAccess);
psDBF->fp = psHooks->FOpen(pszFullname, pszAccess, psHooks->pvUserData);
memcpy(&(psDBF->sHooks), psHooks, sizeof(SAHooks));

if (psDBF->fp == SHPLIB_NULLPTR)
{
memcpy(pszFullname + nLenWithoutExtension, ".DBF", 5);
psDBF->fp = psDBF->sHooks.FOpen(pszFullname, pszAccess);
psDBF->fp =
psDBF->sHooks.FOpen(pszFullname, pszAccess, psHooks->pvUserData);
}

memcpy(pszFullname + nLenWithoutExtension, ".cpg", 5);
SAFile pfCPG = psHooks->FOpen(pszFullname, "r");
SAFile pfCPG = psHooks->FOpen(pszFullname, "r", psHooks->pvUserData);
if (pfCPG == SHPLIB_NULLPTR)
{
memcpy(pszFullname + nLenWithoutExtension, ".CPG", 5);
pfCPG = psHooks->FOpen(pszFullname, "r");
pfCPG = psHooks->FOpen(pszFullname, "r", psHooks->pvUserData);
}

free(pszFullname);
Expand Down Expand Up @@ -643,7 +644,7 @@ DBFHandle SHPAPI_CALL DBFCreateLL(const char *pszFilename,
/* -------------------------------------------------------------------- */
/* Create the file. */
/* -------------------------------------------------------------------- */
SAFile fp = psHooks->FOpen(pszFullname, "wb+");
SAFile fp = psHooks->FOpen(pszFullname, "wb+", psHooks->pvUserData);
if (fp == SHPLIB_NULLPTR)
{
free(pszFullname);
Expand All @@ -663,7 +664,8 @@ DBFHandle SHPAPI_CALL DBFCreateLL(const char *pszFilename,
}
if (ldid < 0)
{
SAFile fpCPG = psHooks->FOpen(pszFullname, "w");
SAFile fpCPG =
psHooks->FOpen(pszFullname, "w", psHooks->pvUserData);
psHooks->FWrite(
CONST_CAST(void *, STATIC_CAST(const void *, pszCodePage)),
strlen(pszCodePage), 1, fpCPG);
Expand All @@ -672,7 +674,7 @@ DBFHandle SHPAPI_CALL DBFCreateLL(const char *pszFilename,
}
if (pszCodePage == SHPLIB_NULLPTR || ldid >= 0)
{
psHooks->Remove(pszFullname);
psHooks->Remove(pszFullname, psHooks->pvUserData);
}

free(pszFullname);
Expand Down
6 changes: 4 additions & 2 deletions safileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#endif
#endif

static SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
static SAFile SADFOpen(const char *pszFilename, const char *pszAccess,
void *pvUserData)
{
return (SAFile)fopen(pszFilename, pszAccess);
}
Expand Down Expand Up @@ -74,7 +75,7 @@ static int SADFClose(SAFile file)
return fclose((FILE *)file);
}

static int SADRemove(const char *filename)
static int SADRemove(const char *filename, void *pvUserData)
{
return remove(filename);
}
Expand All @@ -97,6 +98,7 @@ void SASetupDefaultHooks(SAHooks *psHooks)

psHooks->Error = SADError;
psHooks->Atof = atof;
psHooks->pvUserData = NULL;
}

#ifdef SHPAPI_WINDOWS
Expand Down
5 changes: 3 additions & 2 deletions sbnsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ SBNSearchHandle SBNOpenDiskTree(const char *pszSBNFilename,
else
memcpy(&(hSBN->sHooks), psHooks, sizeof(SAHooks));

hSBN->fpSBN = hSBN->sHooks.FOpen(pszSBNFilename, "rb");
hSBN->fpSBN =
hSBN->sHooks.FOpen(pszSBNFilename, "rb", hSBN->sHooks.pvUserData);
if (hSBN->fpSBN == SHPLIB_NULLPTR)
{
free(hSBN);
Expand Down Expand Up @@ -626,7 +627,7 @@ static bool SBNSearchDiskInternal(SearchStruct *psSearch, int nDepth,

if (!psNode->bBBoxInit)
{
/* clang-format off */
/* clang-format off */
#ifdef sanity_checks
/* -------------------------------------------------------------------- */
/* Those tests only check that the shape bounding box in the bin */
Expand Down
6 changes: 4 additions & 2 deletions shapefil.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ extern "C"

typedef struct
{
SAFile (*FOpen)(const char *filename, const char *access);
SAFile (*FOpen)(const char *filename, const char *access,
void *pvUserData);
SAOffset (*FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file);
SAOffset (*FWrite)(const void *p, SAOffset size, SAOffset nmemb,
SAFile file);
SAOffset (*FSeek)(SAFile file, SAOffset offset, int whence);
SAOffset (*FTell)(SAFile file);
int (*FFlush)(SAFile file);
int (*FClose)(SAFile file);
int (*Remove)(const char *filename);
int (*Remove)(const char *filename, void *pvUserData);

void (*Error)(const char *message);
double (*Atof)(const char *str);
void *pvUserData;
} SAHooks;

void SHPAPI_CALL SASetupDefaultHooks(SAHooks *psHooks);
Expand Down
23 changes: 14 additions & 9 deletions shpopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@ SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess,
char *pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
memcpy(pszFullname, pszLayer, nLenWithoutExtension);
memcpy(pszFullname + nLenWithoutExtension, ".shp", 5);
psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess);
psSHP->fpSHP =
psSHP->sHooks.FOpen(pszFullname, pszAccess, psSHP->sHooks.pvUserData);
if (psSHP->fpSHP == SHPLIB_NULLPTR)
{
memcpy(pszFullname + nLenWithoutExtension, ".SHP", 5);
psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess);
psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess,
psSHP->sHooks.pvUserData);
}

if (psSHP->fpSHP == SHPLIB_NULLPTR)
Expand All @@ -363,11 +365,13 @@ SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszLayer, const char *pszAccess,
}

memcpy(pszFullname + nLenWithoutExtension, ".shx", 5);
psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess);
psSHP->fpSHX =
psSHP->sHooks.FOpen(pszFullname, pszAccess, psSHP->sHooks.pvUserData);
if (psSHP->fpSHX == SHPLIB_NULLPTR)
{
memcpy(pszFullname + nLenWithoutExtension, ".SHX", 5);
psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess);
psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess,
psSHP->sHooks.pvUserData);
}

if (psSHP->fpSHX == SHPLIB_NULLPTR)
Expand Down Expand Up @@ -708,11 +712,11 @@ int SHPAPI_CALL SHPRestoreSHX(const char *pszLayer, const char *pszAccess,
char *pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
memcpy(pszFullname, pszLayer, nLenWithoutExtension);
memcpy(pszFullname + nLenWithoutExtension, ".shp", 5);
SAFile fpSHP = psHooks->FOpen(pszFullname, pszAccess);
SAFile fpSHP = psHooks->FOpen(pszFullname, pszAccess, psHooks->pvUserData);
if (fpSHP == SHPLIB_NULLPTR)
{
memcpy(pszFullname + nLenWithoutExtension, ".SHP", 5);
fpSHP = psHooks->FOpen(pszFullname, pszAccess);
fpSHP = psHooks->FOpen(pszFullname, pszAccess, psHooks->pvUserData);
}

if (fpSHP == SHPLIB_NULLPTR)
Expand Down Expand Up @@ -756,7 +760,8 @@ int SHPAPI_CALL SHPRestoreSHX(const char *pszLayer, const char *pszAccess,

memcpy(pszFullname + nLenWithoutExtension, ".shx", 5);
const char pszSHXAccess[] = "w+b";
SAFile fpSHX = psHooks->FOpen(pszFullname, pszSHXAccess);
SAFile fpSHX =
psHooks->FOpen(pszFullname, pszSHXAccess, psHooks->pvUserData);
if (fpSHX == SHPLIB_NULLPTR)
{
size_t nMessageLen = strlen(pszFullname) * 2 + 256;
Expand Down Expand Up @@ -1037,7 +1042,7 @@ SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszLayer, int nShapeType,
char *pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
memcpy(pszFullname, pszLayer, nLenWithoutExtension);
memcpy(pszFullname + nLenWithoutExtension, ".shp", 5);
SAFile fpSHP = psHooks->FOpen(pszFullname, "w+b");
SAFile fpSHP = psHooks->FOpen(pszFullname, "w+b", psHooks->pvUserData);
if (fpSHP == SHPLIB_NULLPTR)
{
char szErrorMsg[200];
Expand All @@ -1050,7 +1055,7 @@ SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszLayer, int nShapeType,
}

memcpy(pszFullname + nLenWithoutExtension, ".shx", 5);
SAFile fpSHX = psHooks->FOpen(pszFullname, "w+b");
SAFile fpSHX = psHooks->FOpen(pszFullname, "w+b", psHooks->pvUserData);
if (fpSHX == SHPLIB_NULLPTR)
{
char szErrorMsg[200];
Expand Down
5 changes: 3 additions & 2 deletions shptree.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ SHPTreeDiskHandle SHPOpenDiskTree(const char *pszQIXFilename,
else
memcpy(&(hDiskTree->sHooks), psHooks, sizeof(SAHooks));

hDiskTree->fpQIX = hDiskTree->sHooks.FOpen(pszQIXFilename, "rb");
hDiskTree->fpQIX = hDiskTree->sHooks.FOpen(pszQIXFilename, "rb",
hDiskTree->sHooks.pvUserData);
if (hDiskTree->fpQIX == SHPLIB_NULLPTR)
{
free(hDiskTree);
Expand Down Expand Up @@ -1125,7 +1126,7 @@ int SHPWriteTreeLL(SHPTree *tree, const char *filename, const SAHooks *psHooks)
/* -------------------------------------------------------------------- */
/* Open the output file. */
/* -------------------------------------------------------------------- */
fp = psHooks->FOpen(filename, "wb");
fp = psHooks->FOpen(filename, "wb", psHooks->pvUserData);
if (fp == SHPLIB_NULLPTR)
{
return FALSE;
Expand Down

0 comments on commit 01ee129

Please sign in to comment.