1
0

fix: c++ issues with pl_json.h

This commit is contained in:
Jonathan Hoffstadt 2025-04-11 21:58:00 -05:00
parent be504fc942
commit 9dac7bcc73

View File

@ -13,8 +13,8 @@
*/
// library version (format XYYZZ)
#define PL_JSON_VERSION "1.0.1"
#define PL_JSON_VERSION_NUM 10001
#define PL_JSON_VERSION "1.0.0"
#define PL_JSON_VERSION_NUM 10000
/*
Index of this file:
@ -68,6 +68,7 @@ plJsonObject* pl_json_member_by_index(plJsonObject*, uint32_t uIndex);
void pl_json_member_list (plJsonObject*, char** pcListOut, uint32_t* puSizeOut, uint32_t* puLength);
bool pl_json_member_exist (plJsonObject*, const char* pcName);
plJsonType pl_json_get_type (plJsonObject*);
const char* pl_json_get_name (plJsonObject*);
// retrieve and cast values (default used if member isn't present)
int pl_json_int_member (plJsonObject*, const char* pcName, int iDefaultValue);
@ -114,12 +115,12 @@ void pl_json_add_bool_member (plJsonObject*, const char* pcName, bool);
void pl_json_add_string_member(plJsonObject*, const char* pcName, const char*);
// arrays
void pl_json_add_int_array (plJsonObject*, const char* pcName, int*, uint32_t uCount);
void pl_json_add_uint_array (plJsonObject*, const char* pcName, uint32_t*, uint32_t uCount);
void pl_json_add_float_array (plJsonObject*, const char* pcName, float*, uint32_t uCount);
void pl_json_add_double_array(plJsonObject*, const char* pcName, double*, uint32_t uCount);
void pl_json_add_bool_array (plJsonObject*, const char* pcName, bool*, uint32_t uCount);
void pl_json_add_string_array(plJsonObject*, const char* pcName, char**, uint32_t uCount);
void pl_json_add_int_array (plJsonObject*, const char* pcName, const int*, uint32_t uCount);
void pl_json_add_uint_array (plJsonObject*, const char* pcName, const uint32_t*, uint32_t uCount);
void pl_json_add_float_array (plJsonObject*, const char* pcName, const float*, uint32_t uCount);
void pl_json_add_double_array(plJsonObject*, const char* pcName, const double*, uint32_t uCount);
void pl_json_add_bool_array (plJsonObject*, const char* pcName, const bool*, uint32_t uCount);
void pl_json_add_string_array(plJsonObject*, const char* pcName, const char**, uint32_t uCount);
// objects & object arrays
plJsonObject* pl_json_add_member (plJsonObject*, const char* pcName); // returns object to be modified with above commands
@ -144,7 +145,7 @@ enum plJsonType_
#ifdef PL_JSON_IMPLEMENTATION
#ifndef PL_GLTF_EXTENSION_H
#ifndef CGLTF_IMPLEMENTATION
//-----------------------------------------------------------------------------
// [SECTION] jsmn.h
@ -709,7 +710,7 @@ static void pl__check_json_object(plJsonObject* ptJson, uint32_t* puBuffer
plJsonObject*
pl_json_new_root_object(const char* pcName)
{
plJsonObject* ptJson = PL_JSON_ALLOC(sizeof(plJsonObject));
plJsonObject* ptJson = (plJsonObject*)PL_JSON_ALLOC(sizeof(plJsonObject));
memset(ptJson, 0, sizeof(plJsonObject));
ptJson->tType = PL_JSON_TYPE_OBJECT;
ptJson->ptRootObject = ptJson;
@ -757,9 +758,14 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
uint32_t uLayer = 0;
uint32_t uCurrentTokenIndex = 0;
plJsonObject** sbtObjectStack = NULL;
*pptJsonOut = PL_JSON_ALLOC(sizeof(plJsonObject));
*pptJsonOut = (plJsonObject*)PL_JSON_ALLOC(sizeof(plJsonObject));
memset(*pptJsonOut, 0, sizeof(plJsonObject));
plJsonObject* ptJsonOut = *pptJsonOut;
if(ptJsonOut == NULL)
{
pl_sb_json_free(sbtTokens);
return false;
}
ptJsonOut->ptRootObject = ptJsonOut;
pl_sb_json_reserve(ptJsonOut->sbcBuffer, strlen(pcJson));
ptJsonOut->tType = pl__get_json_token_object_type(pcJson, &sbtTokens[uCurrentTokenIndex]);
@ -791,19 +797,19 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
{
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
pl_sb_json_push(ptParentObject->sbuValueLength, ptCurrentToken->end - ptCurrentToken->start);
pl_sb_json_push(ptParentObject->sbuValueLength, (uint32_t)(ptCurrentToken->end - ptCurrentToken->start));
ptParentObject->uChildrenFound++;
}
else
{
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
ptParentObject->uValueOffset = uBufferLocation;
ptParentObject->uValueLength = ptCurrentToken->end - ptCurrentToken->start;
ptParentObject->uValueLength = (uint32_t)(ptCurrentToken->end - ptCurrentToken->start);
ptParentObject->uChildrenFound++;
pl_sb_json_pop(sbtObjectStack);
}
@ -817,19 +823,19 @@ pl_load_json(const char* pcJson, plJsonObject** pptJsonOut)
if(ptParentObject->tType == PL_JSON_TYPE_ARRAY)
{
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
pl_sb_json_push(ptParentObject->sbuValueOffsets, uBufferLocation);
pl_sb_json_push(ptParentObject->sbuValueLength, ptCurrentToken->end - ptCurrentToken->start);
pl_sb_json_push(ptParentObject->sbuValueLength, (uint32_t)(ptCurrentToken->end - ptCurrentToken->start));
ptParentObject->uChildrenFound++;
}
else
{
const uint32_t uBufferLocation = pl_sb_json_size(ptJsonOut->sbcBuffer);
pl_sb_json_resize(ptJsonOut->sbcBuffer, uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1);
pl_sb_json_resize(ptJsonOut->sbcBuffer, (uint32_t)(uBufferLocation + ptCurrentToken->end - ptCurrentToken->start + 1));
memcpy(&ptJsonOut->sbcBuffer[uBufferLocation], &pcJson[ptCurrentToken->start], ptCurrentToken->end - ptCurrentToken->start);
ptParentObject->uValueOffset = uBufferLocation;
ptParentObject->uValueLength = ptCurrentToken->end - ptCurrentToken->start;
ptParentObject->uValueLength = (uint32_t)(ptCurrentToken->end - ptCurrentToken->start);
ptParentObject->uChildrenFound++;
pl_sb_json_pop(sbtObjectStack);
}
@ -1042,6 +1048,12 @@ pl_json_get_type(plJsonObject* ptJson)
return ptJson->tType;
}
const char*
pl_json_get_name(plJsonObject* ptJson)
{
return ptJson->acName;
}
bool
pl_json_member_exist(plJsonObject* ptJson, const char* pcName)
{
@ -1535,7 +1547,7 @@ pl_json_add_member_array(plJsonObject* ptJson, const char* pcName, uint32_t uSiz
}
void
pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, int* piValues, uint32_t uSize)
pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, const int* piValues, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1569,7 +1581,7 @@ pl_json_add_int_array(plJsonObject* ptJson, const char* pcName, int* piValues, u
}
void
pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, uint32_t* puValues, uint32_t uSize)
pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, const uint32_t* puValues, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1603,7 +1615,7 @@ pl_json_add_uint_array(plJsonObject* ptJson, const char* pcName, uint32_t* puVal
}
void
pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, float* pfValues, uint32_t uSize)
pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, const float* pfValues, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1637,7 +1649,7 @@ pl_json_add_float_array(plJsonObject* ptJson, const char* pcName, float* pfValue
}
void
pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, double* pdValues, uint32_t uSize)
pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, const double* pdValues, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1671,7 +1683,7 @@ pl_json_add_double_array(plJsonObject* ptJson, const char* pcName, double* pdVal
}
void
pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, bool* pbValues, uint32_t uSize)
pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, const bool* pbValues, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1705,7 +1717,7 @@ pl_json_add_bool_array(plJsonObject* ptJson, const char* pcName, bool* pbValues,
}
void
pl_json_add_string_array(plJsonObject* ptJson, const char* pcName, char** ppcBuffer, uint32_t uSize)
pl_json_add_string_array(plJsonObject* ptJson, const char* pcName, const char** ppcBuffer, uint32_t uSize)
{
ptJson->uChildCount++;
ptJson->uChildrenFound++;
@ -1860,7 +1872,7 @@ pl__write_json_object(plJsonObject* ptJson, char* pcBuffer, uint32_t* puBufferSi
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
char cPreviousChar = ' ';
if(pcPrevChar)
// if(pcPrevChar)
{
const char* pcPrevCharAddr = pcPrevChar - 1;
cPreviousChar = pcPrevCharAddr[0];
@ -2000,7 +2012,7 @@ pl__check_json_object(plJsonObject* ptJson, uint32_t* puBufferSize, uint32_t* pu
const char* pcPrevChar = &ptJson->ptRootObject->sbcBuffer[ptJson->sbuValueOffsets[i]];
char cPreviousChar = ' ';
if(pcPrevChar)
// if(pcPrevChar)
{
const char* pcPrevCharAddr = pcPrevChar - 1;
cPreviousChar = pcPrevCharAddr[0];