mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-03-05 15:05:48 +02:00
Add mode, Pack, and StringId to the Pack type.
Add StringList, which is not a primitive type but rather an array of String types. Also update pckWriteToLog() to work after pckWriteEnd(), i.e. this->tagStackTop is NULL.
This commit is contained in:
parent
85a55bd401
commit
97155bad86
@ -125,11 +125,14 @@ typedef enum
|
||||
pckTypeMapStr = 7, // Maps to pckTypeStr
|
||||
pckTypeMapU32 = 8, // Maps to pckTypeU32
|
||||
pckTypeMapU64 = 9, // Maps to pckTypeU64
|
||||
pckTypeMapStrId = 10, // Maps to pckTypeStrId
|
||||
|
||||
// The empty positions before 15 can be used for new types that will be encoded entirely in the tag
|
||||
|
||||
pckTypeMapTime = 15, // Maps to pckTypeTime
|
||||
pckTypeMapBin = 16, // Maps to pckTypeBin
|
||||
pckTypeMapPack = 17, // Maps to pckTypePack
|
||||
pckTypeMapMode = 18, // Maps to pckTypeMode
|
||||
} PackTypeMap;
|
||||
|
||||
typedef struct PackTypeMapData
|
||||
@ -181,13 +184,16 @@ static const PackTypeMapData packTypeMapData[] =
|
||||
.type = pckTypeU64,
|
||||
.valueMultiBit = true,
|
||||
},
|
||||
{
|
||||
.type = pckTypeStrId,
|
||||
.valueMultiBit = true,
|
||||
},
|
||||
|
||||
// Placeholders for unused types that can be encoded entirely in the tag
|
||||
{0},
|
||||
{0},
|
||||
{0},
|
||||
{0},
|
||||
{0},
|
||||
|
||||
// Formats that require an extra byte to encode
|
||||
{
|
||||
@ -199,6 +205,14 @@ static const PackTypeMapData packTypeMapData[] =
|
||||
.valueSingleBit = true,
|
||||
.size = true,
|
||||
},
|
||||
{
|
||||
.type = pckTypePack,
|
||||
.size = true,
|
||||
},
|
||||
{
|
||||
.type = pckTypeMode,
|
||||
.valueMultiBit = true,
|
||||
},
|
||||
};
|
||||
|
||||
#define PACK_TYPE_MAP_SIZE (sizeof(packTypeMapData) / sizeof(PackTypeMapData))
|
||||
@ -295,7 +309,8 @@ pckReadNewBuf(const Buffer *buffer)
|
||||
FUNCTION_TEST_PARAM(BUFFER, buffer);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(buffer != NULL);
|
||||
if (buffer == NULL)
|
||||
FUNCTION_TEST_RETURN(NULL);
|
||||
|
||||
PackRead *this = pckReadNewInternal();
|
||||
this->bufferPtr = bufPtrConst(buffer);
|
||||
@ -795,6 +810,24 @@ pckReadI64(PackRead *this, PckReadI64Param param)
|
||||
FUNCTION_TEST_RETURN(cvtInt64FromZigZag(pckReadTag(this, ¶m.id, pckTypeMapI64, false)));
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
mode_t
|
||||
pckReadMode(PackRead *this, PckReadModeParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_READ, this);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_PARAM(MODE, param.defaultValue);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (pckReadNullInternal(this, ¶m.id))
|
||||
FUNCTION_TEST_RETURN(param.defaultValue);
|
||||
|
||||
FUNCTION_TEST_RETURN((mode_t)pckReadTag(this, ¶m.id, pckTypeMapMode, false));
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
void
|
||||
pckReadObjBegin(PackRead *this, PackIdParam param)
|
||||
@ -841,6 +874,54 @@ pckReadObjEnd(PackRead *this)
|
||||
FUNCTION_TEST_RETURN_VOID();
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackRead *
|
||||
pckReadPack(PackRead *this, PckReadPackParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_READ, this);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
Buffer *const buffer = pckReadPackBuf(this, param);
|
||||
PackRead *const result = pckReadNewBuf(buffer);
|
||||
|
||||
if (result != NULL)
|
||||
bufMove(buffer, result->memContext);
|
||||
|
||||
FUNCTION_TEST_RETURN(result);
|
||||
}
|
||||
|
||||
Buffer *
|
||||
pckReadPackBuf(PackRead *this, PckReadPackParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_READ, this);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (pckReadNullInternal(this, ¶m.id))
|
||||
FUNCTION_TEST_RETURN(NULL);
|
||||
|
||||
// Read the tag
|
||||
pckReadTag(this, ¶m.id, pckTypeMapPack, false);
|
||||
|
||||
// Get the pack size
|
||||
Buffer *result = bufNew((size_t)pckReadU64Internal(this));
|
||||
|
||||
// Read the pack out in chunks
|
||||
while (bufUsed(result) < bufSize(result))
|
||||
{
|
||||
size_t size = pckReadBuffer(this, bufRemains(result));
|
||||
bufCatC(result, this->bufferPtr, this->bufferPos, size);
|
||||
this->bufferPos += size;
|
||||
}
|
||||
|
||||
FUNCTION_TEST_RETURN(result);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
void *
|
||||
pckReadPtr(PackRead *this, PckReadPtrParam param)
|
||||
@ -898,6 +979,50 @@ pckReadStr(PackRead *this, PckReadStrParam param)
|
||||
FUNCTION_TEST_RETURN(result);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
StringId
|
||||
pckReadStrId(PackRead *this, PckReadStrIdParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_READ, this);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_PARAM(UINT64, param.defaultValue);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (pckReadNullInternal(this, ¶m.id))
|
||||
FUNCTION_TEST_RETURN(param.defaultValue);
|
||||
|
||||
FUNCTION_TEST_RETURN(pckReadTag(this, ¶m.id, pckTypeMapStrId, false));
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
StringList *
|
||||
pckReadStrLst(PackRead *const this, PckReadStrLstParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_READ, this);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (pckReadNullInternal(this, ¶m.id))
|
||||
FUNCTION_TEST_RETURN(NULL);
|
||||
|
||||
pckReadArrayBeginP(this, .id = param.id);
|
||||
|
||||
StringList *const result = strLstNew();
|
||||
|
||||
while (!pckReadNullP(this))
|
||||
strLstAdd(result, pckReadStrP(this));
|
||||
|
||||
pckReadArrayEndP(this);
|
||||
|
||||
FUNCTION_TEST_RETURN(result);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
time_t
|
||||
pckReadTime(PackRead *this, PckReadTimeParam param)
|
||||
@ -1425,6 +1550,26 @@ pckWriteI64(PackWrite *this, int64_t value, PckWriteI64Param param)
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWriteMode(PackWrite *this, mode_t value, PckWriteModeParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, this);
|
||||
FUNCTION_TEST_PARAM(UINT32, value);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_PARAM(BOOL, param.defaultWrite);
|
||||
FUNCTION_TEST_PARAM(MODE, param.defaultValue);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (!pckWriteDefaultNull(this, param.defaultWrite, value == param.defaultValue))
|
||||
pckWriteTag(this, pckTypeMapMode, param.id, value);
|
||||
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWriteObjBegin(PackWrite *this, PackIdParam param)
|
||||
@ -1466,6 +1611,36 @@ pckWriteObjEnd(PackWrite *this)
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWritePack(PackWrite *this, const PackWrite *value, PckWritePackParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, this);
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, value);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_PARAM(BOOL, param.defaultWrite);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (!pckWriteDefaultNull(this, false, value == NULL))
|
||||
{
|
||||
ASSERT(value != NULL);
|
||||
|
||||
// Write pack size
|
||||
pckWriteTag(this, pckTypeMapPack, param.id, 0);
|
||||
|
||||
// Write pack data
|
||||
const Buffer *packBuffer = pckWriteBuf(value);
|
||||
|
||||
pckWriteU64Internal(this, bufUsed(packBuffer));
|
||||
pckWriteBuffer(this, packBuffer);
|
||||
}
|
||||
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWritePtr(PackWrite *this, const void *value, PckWritePtrParam param)
|
||||
@ -1515,6 +1690,53 @@ pckWriteStr(PackWrite *this, const String *value, PckWriteStrParam param)
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWriteStrId(PackWrite *this, uint64_t value, PckWriteStrIdParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, this);
|
||||
FUNCTION_TEST_PARAM(UINT64, value);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_PARAM(BOOL, param.defaultWrite);
|
||||
FUNCTION_TEST_PARAM(UINT64, param.defaultValue);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (!pckWriteDefaultNull(this, param.defaultWrite, value == param.defaultValue))
|
||||
pckWriteTag(this, pckTypeMapStrId, param.id, value);
|
||||
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWriteStrLst(PackWrite *const this, const StringList *const value, const PckWriteStrLstParam param)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, this);
|
||||
FUNCTION_TEST_PARAM(STRING_LIST, value);
|
||||
FUNCTION_TEST_PARAM(UINT, param.id);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
|
||||
if (!pckWriteDefaultNull(this, false, value == NULL))
|
||||
{
|
||||
ASSERT(value != NULL);
|
||||
|
||||
pckWriteArrayBeginP(this, .id = param.id);
|
||||
|
||||
for (unsigned int valueIdx = 0; valueIdx < strLstSize(value); valueIdx++)
|
||||
pckWriteStrP(this, strLstGet(value, valueIdx));
|
||||
|
||||
pckWriteArrayEndP(this);
|
||||
}
|
||||
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
PackWrite *
|
||||
pckWriteTime(PackWrite *this, time_t value, PckWriteTimeParam param)
|
||||
@ -1602,9 +1824,23 @@ pckWriteEnd(PackWrite *this)
|
||||
FUNCTION_TEST_RETURN(this);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
const Buffer *
|
||||
pckWriteBuf(const PackWrite *this)
|
||||
{
|
||||
FUNCTION_TEST_BEGIN();
|
||||
FUNCTION_TEST_PARAM(PACK_WRITE, this);
|
||||
FUNCTION_TEST_END();
|
||||
|
||||
ASSERT(this != NULL);
|
||||
ASSERT(this->tagStackTop == NULL);
|
||||
|
||||
FUNCTION_TEST_RETURN(this->buffer);
|
||||
}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
String *
|
||||
pckWriteToLog(const PackWrite *this)
|
||||
{
|
||||
return strNewFmt("{depth: %u, idLast: %u}", lstSize(this->tagStack), this->tagStackTop->idLast);
|
||||
return strNewFmt("{depth: %u, idLast: %u}", lstSize(this->tagStack), this->tagStackTop == NULL ? 0 : this->tagStackTop->idLast);
|
||||
}
|
||||
|
@ -82,6 +82,9 @@ cumulative field ID. At the end of a container the numbering will continue from
|
||||
#ifndef COMMON_TYPE_PACK_H
|
||||
#define COMMON_TYPE_PACK_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Minimum number of extra bytes to allocate for packs that are growing or are likely to grow
|
||||
***********************************************************************************************************************************/
|
||||
@ -111,8 +114,11 @@ typedef enum
|
||||
pckTypeI32 = STRID6("i32", 0x1e7c91),
|
||||
pckTypeI64 = STRID6("i64", 0x208891),
|
||||
pckTypeObj = STRID5("obj", 0x284f0),
|
||||
pckTypeMode = STRID5("mode", 0x291ed0),
|
||||
pckTypePack = STRID5("pack", 0x58c300),
|
||||
pckTypePtr = STRID5("ptr", 0x4a900),
|
||||
pckTypeStr = STRID5("str", 0x4a930),
|
||||
pckTypeStrId = STRID5("strid", 0x44ca930),
|
||||
pckTypeTime = STRID5("time", 0x2b5340),
|
||||
pckTypeU32 = STRID6("u32", 0x1e7d51),
|
||||
pckTypeU64 = STRID6("u64", 0x208951),
|
||||
@ -214,6 +220,19 @@ typedef struct PckReadI64Param
|
||||
|
||||
int64_t pckReadI64(PackRead *this, PckReadI64Param param);
|
||||
|
||||
// Read mode
|
||||
typedef struct PckReadModeParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
unsigned int id;
|
||||
mode_t defaultValue;
|
||||
} PckReadModeParam;
|
||||
|
||||
#define pckReadModeP(this, ...) \
|
||||
pckReadMode(this, (PckReadModeParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
mode_t pckReadMode(PackRead *this, PckReadModeParam param);
|
||||
|
||||
// Move to a new parent mem context
|
||||
__attribute__((always_inline)) static inline PackRead *
|
||||
pckReadMove(PackRead *const this, MemContext *const parentNew)
|
||||
@ -232,6 +251,24 @@ void pckReadObjBegin(PackRead *this, PackIdParam param);
|
||||
|
||||
void pckReadObjEnd(PackRead *this);
|
||||
|
||||
// Read pack
|
||||
typedef struct PckReadPackParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
unsigned int id;
|
||||
} PckReadPackParam;
|
||||
|
||||
#define pckReadPackP(this, ...) \
|
||||
pckReadPack(this, (PckReadPackParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
PackRead *pckReadPack(PackRead *this, PckReadPackParam param);
|
||||
|
||||
// Read pack buffer
|
||||
#define pckReadPackBufP(this, ...) \
|
||||
pckReadPackBuf(this, (PckReadPackParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
Buffer *pckReadPackBuf(PackRead *this, PckReadPackParam param);
|
||||
|
||||
// Read pointer. Use with extreme caution. Pointers cannot be sent to another host -- they must only be used locally.
|
||||
typedef struct PckReadPtrParam
|
||||
{
|
||||
@ -257,6 +294,31 @@ typedef struct PckReadStrParam
|
||||
|
||||
String *pckReadStr(PackRead *this, PckReadStrParam param);
|
||||
|
||||
// Read string id
|
||||
typedef struct PckReadStrIdParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
unsigned int id;
|
||||
StringId defaultValue;
|
||||
} PckReadStrIdParam;
|
||||
|
||||
#define pckReadStrIdP(this, ...) \
|
||||
pckReadStrId(this, (PckReadStrIdParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
uint64_t pckReadStrId(PackRead *this, PckReadStrIdParam param);
|
||||
|
||||
// Read string list
|
||||
typedef struct PckReadStrLstParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
unsigned int id;
|
||||
} PckReadStrLstParam;
|
||||
|
||||
#define pckReadStrLstP(this, ...) \
|
||||
pckReadStrLst(this, (PckReadStrLstParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
StringList *pckReadStrLst(PackRead *const this, PckReadStrLstParam param);
|
||||
|
||||
// Read time
|
||||
typedef struct PckReadTimeParam
|
||||
{
|
||||
@ -387,6 +449,20 @@ typedef struct PckWriteI64Param
|
||||
|
||||
PackWrite *pckWriteI64(PackWrite *this, int64_t value, PckWriteI64Param param);
|
||||
|
||||
// Write mode
|
||||
typedef struct PckWriteModeParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
bool defaultWrite;
|
||||
unsigned int id;
|
||||
mode_t defaultValue;
|
||||
} PckWriteModeParam;
|
||||
|
||||
#define pckWriteModeP(this, value, ...) \
|
||||
pckWriteMode(this, value, (PckWriteModeParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
PackWrite *pckWriteMode(PackWrite *this, mode_t value, PckWriteModeParam param);
|
||||
|
||||
// Move to a new parent mem context
|
||||
__attribute__((always_inline)) static inline PackWrite *
|
||||
pckWriteMove(PackWrite *const this, MemContext *const parentNew)
|
||||
@ -411,6 +487,19 @@ PackWrite *pckWriteObjBegin(PackWrite *this, PackIdParam param);
|
||||
|
||||
PackWrite *pckWriteObjEnd(PackWrite *this);
|
||||
|
||||
// Write pack
|
||||
typedef struct PckWritePackParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
bool defaultWrite;
|
||||
unsigned int id;
|
||||
} PckWritePackParam;
|
||||
|
||||
#define pckWritePackP(this, value, ...) \
|
||||
pckWritePack(this, value, (PckWritePackParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
PackWrite *pckWritePack(PackWrite *this, const PackWrite *value, PckWritePackParam param);
|
||||
|
||||
// Write pointer. Use with extreme caution. Pointers cannot be sent to another host -- they must only be used locally.
|
||||
typedef struct PckWritePtrParam
|
||||
{
|
||||
@ -438,6 +527,32 @@ typedef struct PckWriteStrParam
|
||||
|
||||
PackWrite *pckWriteStr(PackWrite *this, const String *value, PckWriteStrParam param);
|
||||
|
||||
// Write string id
|
||||
typedef struct PckWriteStrIdParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
bool defaultWrite;
|
||||
unsigned int id;
|
||||
StringId defaultValue;
|
||||
} PckWriteStrIdParam;
|
||||
|
||||
#define pckWriteStrIdP(this, value, ...) \
|
||||
pckWriteStrId(this, value, (PckWriteStrIdParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
PackWrite *pckWriteStrId(PackWrite *this, uint64_t value, PckWriteStrIdParam param);
|
||||
|
||||
// Write string list
|
||||
typedef struct PckWriteStrLstParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
unsigned int id;
|
||||
} PckWriteStrLstParam;
|
||||
|
||||
#define pckWriteStrLstP(this, value, ...) \
|
||||
pckWriteStrLst(this, value, (PckWriteStrLstParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
PackWrite *pckWriteStrLst(PackWrite *const this, const StringList *const value, const PckWriteStrLstParam param);
|
||||
|
||||
// Write time
|
||||
typedef struct PckWriteTimeParam
|
||||
{
|
||||
@ -486,6 +601,13 @@ PackWrite *pckWriteU64(PackWrite *this, uint64_t value, PckWriteU64Param param);
|
||||
|
||||
PackWrite *pckWriteEnd(PackWrite *this);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Write Getters/Setters
|
||||
***********************************************************************************************************************************/
|
||||
// Get buffer the pack is writing to (returns NULL if pckWriteNewBuf() was not used to construct the object). This function is only
|
||||
// valid after pckWriteEndP() has been called.
|
||||
const Buffer *pckWriteBuf(const PackWrite *this);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Write Destructor
|
||||
***********************************************************************************************************************************/
|
||||
|
@ -65,12 +65,22 @@ String *hrnPackToStr(PackRead *read)
|
||||
strCatFmt(result, "%" PRId64, pckReadI64P(read, .id = id));
|
||||
break;
|
||||
|
||||
case pckTypeMode:
|
||||
strCatFmt(result, "%04o", pckReadModeP(read, .id = id));
|
||||
break;
|
||||
|
||||
case pckTypeObj:
|
||||
pckReadObjBeginP(read, .id = id);
|
||||
strCatFmt(result, "{%s}", strZ(hrnPackToStr(read)));
|
||||
pckReadObjEndP(read);
|
||||
break;
|
||||
|
||||
case pckTypePack:
|
||||
{
|
||||
strCatFmt(result, "<%s>", strZ(hrnPackToStr(pckReadPackP(read))));
|
||||
break;
|
||||
}
|
||||
|
||||
case pckTypePtr:
|
||||
strCatFmt(result, "%p", pckReadPtrP(read, .id = id));
|
||||
break;
|
||||
@ -79,6 +89,10 @@ String *hrnPackToStr(PackRead *read)
|
||||
strCatFmt(result, "%s", strZ(pckReadStrP(read, .id = id)));
|
||||
break;
|
||||
|
||||
case pckTypeStrId:
|
||||
strCatFmt(result, "%s", strZ(strIdToStr(pckReadStrIdP(read, .id = id))));
|
||||
break;
|
||||
|
||||
case pckTypeTime:
|
||||
strCatFmt(result, "%" PRId64, (int64_t)pckReadTimeP(read, .id = id));
|
||||
break;
|
||||
|
@ -55,6 +55,10 @@ testRun(void)
|
||||
TEST_RESULT_VOID(pckWriteObjBeginP(packWrite, .id = 28), "write obj begin");
|
||||
TEST_RESULT_VOID(pckWriteBoolP(packWrite, true), "write true");
|
||||
TEST_RESULT_VOID(pckWriteBoolP(packWrite, false, .defaultWrite = true), "write false");
|
||||
TEST_RESULT_VOID(pckWriteStrIdP(packWrite, pckTypeTime, .id = 5), "write strid");
|
||||
TEST_RESULT_VOID(pckWriteStrIdP(packWrite, pckTypeTime, .defaultValue = pckTypeTime), "write default strid");
|
||||
TEST_RESULT_VOID(pckWriteModeP(packWrite, 0707, .id = 7), "write mode");
|
||||
TEST_RESULT_VOID(pckWriteModeP(packWrite, 0644, .defaultValue = 0644), "write default mode");
|
||||
TEST_RESULT_VOID(pckWriteObjEndP(packWrite), "write obj end");
|
||||
TEST_RESULT_VOID(pckWriteArrayBeginP(packWrite, .id = 37), "write array begin");
|
||||
TEST_RESULT_VOID(pckWriteU64P(packWrite, 0, .defaultWrite = true), "write 0");
|
||||
@ -96,6 +100,24 @@ testRun(void)
|
||||
TEST_RESULT_VOID(pckWriteBinP(packWrite, NULL), "write bin NULL default");
|
||||
TEST_RESULT_VOID(pckWriteBinP(packWrite, bufNew(0)), "write bin zero length");
|
||||
|
||||
// Write pack
|
||||
PackWrite *packSub = pckWriteNewBuf(bufNew(128));
|
||||
pckWriteU64P(packSub, 345);
|
||||
pckWriteStrP(packSub, STRDEF("sub"), .id = 3);
|
||||
pckWriteEndP(packSub);
|
||||
|
||||
TEST_RESULT_VOID(pckWritePackP(packWrite, packSub), "write pack");
|
||||
TEST_RESULT_VOID(pckWritePackP(packWrite, NULL), "write null pack");
|
||||
|
||||
// Write string list
|
||||
StringList *const strList = strLstNew();
|
||||
strLstAddZ(strList, "a");
|
||||
strLstAddZ(strList, "bcd");
|
||||
|
||||
TEST_RESULT_VOID(pckWriteStrLstP(packWrite, strList), "write string list");
|
||||
TEST_RESULT_VOID(pckWriteStrLstP(packWrite, NULL), "write null string list");
|
||||
|
||||
// End pack
|
||||
TEST_RESULT_VOID(pckWriteEndP(packWrite), "end");
|
||||
TEST_RESULT_VOID(pckWriteFree(packWrite), "free");
|
||||
|
||||
@ -117,6 +139,8 @@ testRun(void)
|
||||
"{"
|
||||
"1:bool:true"
|
||||
", 2:bool:false"
|
||||
", 5:strid:time"
|
||||
", 7:mode:0707"
|
||||
"}"
|
||||
", 37:array:"
|
||||
"["
|
||||
@ -146,7 +170,9 @@ testRun(void)
|
||||
", 6:time:66"
|
||||
"]"
|
||||
", 49:bin:050403020100"
|
||||
", 51:bin:",
|
||||
", 51:bin:"
|
||||
", 52:pack:<1:u64:345, 3:str:sub>"
|
||||
", 54:array:[1:str:a, 2:str:bcd]",
|
||||
"check pack string");
|
||||
|
||||
TEST_RESULT_STR_Z(
|
||||
@ -164,6 +190,8 @@ testRun(void)
|
||||
"57" // 28, obj begin
|
||||
"28" // 1, bool
|
||||
"20" // 2, bool
|
||||
"aac0a6ad01" // 5, strid time
|
||||
"f903c703" // 7, mode 0707
|
||||
"00" // obj end
|
||||
"1801" // 37, array begin
|
||||
"90" // 1, u64, 0
|
||||
@ -191,6 +219,8 @@ testRun(void)
|
||||
"00" // array end
|
||||
"f80106050403020100" // 49, bin, 0x050403020100
|
||||
"f101" // 51, bin, zero length
|
||||
"f0020998d902790373756200" // 52, pack, 1:u64:345, 3:str:sub
|
||||
"11780161780362636400" // 54, strlst, 1:str:a, 2:str:bcd
|
||||
"00", // end
|
||||
"check pack hex");
|
||||
|
||||
@ -227,8 +257,12 @@ testRun(void)
|
||||
TEST_ERROR(pckReadArrayEndP(packRead), FormatError, "not in array");
|
||||
TEST_RESULT_BOOL(pckReadBoolP(packRead), true, "read true");
|
||||
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "read false");
|
||||
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "field 3 default is false");
|
||||
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "field 4 default is false");
|
||||
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 4), true, "field 4 is null");
|
||||
TEST_RESULT_UINT(pckReadStrIdP(packRead), pckTypeTime, "read strid");
|
||||
TEST_RESULT_UINT(pckReadStrIdP(packRead, .defaultValue = pckTypeTime), pckTypeTime, "read default strid");
|
||||
TEST_RESULT_UINT(pckReadModeP(packRead), 0707, "read mode");
|
||||
TEST_RESULT_UINT(pckReadModeP(packRead, .defaultValue = 0644), 0644, "read default mode");
|
||||
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "read default false");
|
||||
TEST_RESULT_VOID(pckReadObjEndP(packRead), "read object end");
|
||||
|
||||
@ -278,6 +312,12 @@ testRun(void)
|
||||
TEST_RESULT_PTR(pckReadBinP(packRead), NULL, "read bin null");
|
||||
TEST_RESULT_UINT(bufSize(pckReadBinP(packRead)), 0, "read bin zero length");
|
||||
|
||||
TEST_RESULT_STR_Z(hrnPackToStr(pckReadPackP(packRead)), "1:u64:345, 3:str:sub", "read pack");
|
||||
TEST_RESULT_PTR(pckReadPackP(packRead), NULL, "read null pack");
|
||||
|
||||
TEST_RESULT_STRLST_Z(pckReadStrLstP(packRead), "a\nbcd\n", "read string list");
|
||||
TEST_RESULT_PTR(pckReadStrLstP(packRead), NULL, "read null string list");
|
||||
|
||||
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 999), true, "field 999 is null");
|
||||
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 1000), 0, "field 1000 default is 0");
|
||||
|
||||
@ -299,14 +339,12 @@ testRun(void)
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TEST_TITLE("pack/unpack pointer");
|
||||
|
||||
pack = bufNew(0);
|
||||
|
||||
TEST_ASSIGN(packWrite, pckWriteNewBuf(pack), "new write");
|
||||
TEST_ASSIGN(packWrite, pckWriteNewBuf(bufNew(0)), "new write");
|
||||
TEST_RESULT_VOID(pckWritePtrP(packWrite, NULL), "write default pointer");
|
||||
TEST_RESULT_VOID(pckWritePtrP(packWrite, "sample"), "write pointer");
|
||||
TEST_RESULT_VOID(pckWriteEndP(packWrite), "write end");
|
||||
|
||||
TEST_ASSIGN(packRead, pckReadNewBuf(pack), "new read");
|
||||
TEST_ASSIGN(packRead, pckReadNewBuf(pckWriteBuf(packWrite)), "new read");
|
||||
TEST_RESULT_Z(pckReadPtrP(packRead), NULL, "read default pointer");
|
||||
TEST_RESULT_Z(pckReadPtrP(packRead, .id = 2), "sample", "read pointer");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user