Commit 50d55942 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Introduce and use generalized functions to serialize multiple strings.

parent aced0056
...@@ -3870,7 +3870,6 @@ gridTxCode () ...@@ -3870,7 +3870,6 @@ gridTxCode ()
enum { gridNint = 27, enum { gridNint = 27,
gridNdouble = 24, gridNdouble = 24,
gridNstrings= 8,
gridHasMaskFlag = 1 << 0, gridHasMaskFlag = 1 << 0,
gridHasGMEMaskFlag = 1 << 1, gridHasGMEMaskFlag = 1 << 1,
gridHasXValsFlag = 1 << 2, gridHasXValsFlag = 1 << 2,
...@@ -3900,6 +3899,11 @@ static int gridGetComponentFlags(const grid_t * gridP) ...@@ -3900,6 +3899,11 @@ static int gridGetComponentFlags(const grid_t * gridP)
} }
#define GRID_STR_SERIALIZE { gridP->xname, gridP->yname, \
gridP->xlongname, gridP->ylongname, \
gridP->xstdname, gridP->ystdname, \
gridP->xunits, gridP->yunits }
static int static int
gridGetPackSize(void * voidP, void *context) gridGetPackSize(void * voidP, void *context)
{ {
...@@ -3974,9 +3978,12 @@ gridGetPackSize(void * voidP, void *context) ...@@ -3974,9 +3978,12 @@ gridGetPackSize(void * voidP, void *context)
+ serializeGetSize(1, DATATYPE_UINT32, context)); + serializeGetSize(1, DATATYPE_UINT32, context));
} }
packBuffSize += {
serializeGetSize(gridNstrings * CDI_MAX_NAME , DATATYPE_TXT, context) const char *strTab[] = GRID_STR_SERIALIZE;
+ serializeGetSize(1, DATATYPE_UINT32, context); int numStr = (int)(sizeof (strTab) / sizeof (strTab[0]));
packBuffSize
+= serializeStrTabGetPackSize(strTab, numStr, context);
}
if (gridP->reference) if (gridP->reference)
{ {
...@@ -4007,11 +4014,6 @@ gridGetPackSize(void * voidP, void *context) ...@@ -4007,11 +4014,6 @@ gridGetPackSize(void * voidP, void *context)
return packBuffSize; return packBuffSize;
} }
#define GRID_STR_SERIALIZE { gridP->xname, gridP->yname, \
gridP->xlongname, gridP->ylongname, \
gridP->xstdname, gridP->ystdname, \
gridP->xunits, gridP->yunits }
void void
gridUnpack(char * unpackBuffer, int unpackBufferSize, gridUnpack(char * unpackBuffer, int unpackBufferSize,
int * unpackBufferPos, int originNamespace, void *context, int * unpackBufferPos, int originNamespace, void *context,
...@@ -4020,7 +4022,6 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize, ...@@ -4020,7 +4022,6 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
grid_t * gridP; grid_t * gridP;
uint32_t d; uint32_t d;
int memberMask, size; int memberMask, size;
char charBuffer[gridNstrings * CDI_MAX_NAME];
gridInit(); gridInit();
...@@ -4174,17 +4175,11 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize, ...@@ -4174,17 +4175,11 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
xassert(cdiCheckSum(DATATYPE_FLT, size, gridP->ybounds) == d); xassert(cdiCheckSum(DATATYPE_FLT, size, gridP->ybounds) == d);
} }
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
charBuffer, gridNstrings * CDI_MAX_NAME, DATATYPE_TXT, context);
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
&d, 1, DATATYPE_UINT32, context);
xassert(d == cdiCheckSum(DATATYPE_TXT, gridNstrings * CDI_MAX_NAME, charBuffer));
{ {
char *strTab[] = GRID_STR_SERIALIZE; char *strTab[] = GRID_STR_SERIALIZE;
size_t numStr = sizeof (strTab) / sizeof (strTab[0]); int numStr = sizeof (strTab) / sizeof (strTab[0]);
for (size_t i = 0; i < numStr; ++i) serializeStrTabUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
memcpy(strTab[i], charBuffer + CDI_MAX_NAME * i, CDI_MAX_NAME); strTab, numStr, context);
} }
if (memberMask & gridHasReferenceFlag) if (memberMask & gridHasReferenceFlag)
...@@ -4237,7 +4232,6 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -4237,7 +4232,6 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
int size; int size;
uint32_t d; uint32_t d;
int memberMask; int memberMask;
char charBuffer[gridNstrings * CDI_MAX_NAME];
{ {
int intBuffer[gridNint]; int intBuffer[gridNint];
...@@ -4279,7 +4273,8 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -4279,7 +4273,8 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
if (memberMask & gridHasRowLonFlag) if (memberMask & gridHasRowLonFlag)
{ {
xassert((size = gridP->nrowlon)); size = gridP->nrowlon;
xassert(size > 0);
serializePack(gridP->rowlon, size, DATATYPE_INT, serializePack(gridP->rowlon, size, DATATYPE_INT,
packBuffer, packBufferSize, packBufferPos, context); packBuffer, packBufferSize, packBufferPos, context);
d = cdiCheckSum(DATATYPE_INT , size, gridP->rowlon); d = cdiCheckSum(DATATYPE_INT , size, gridP->rowlon);
...@@ -4395,18 +4390,12 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -4395,18 +4390,12 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
} }
{ {
char *strTab[] = GRID_STR_SERIALIZE; const char *strTab[] = GRID_STR_SERIALIZE;
size_t numStr = sizeof (strTab) / sizeof (strTab[0]); int numStr = sizeof (strTab) / sizeof (strTab[0]);
for (size_t i = 0; i < numStr; ++i) serializeStrTabPack(strTab, numStr,
memcpy(charBuffer + CDI_MAX_NAME * i, strTab[i], CDI_MAX_NAME); packBuffer, packBufferSize, packBufferPos, context);
} }
serializePack( charBuffer, gridNstrings * CDI_MAX_NAME, DATATYPE_TXT,
packBuffer, packBufferSize, packBufferPos, context);
d = cdiCheckSum(DATATYPE_TXT, gridNstrings * CDI_MAX_NAME, charBuffer);
serializePack(&d, 1, DATATYPE_UINT32,
packBuffer, packBufferSize, packBufferPos, context);
if (memberMask & gridHasReferenceFlag) if (memberMask & gridHasReferenceFlag)
{ {
size = (int)strlen(gridP->reference) + 1; size = (int)strlen(gridP->reference) + 1;
...@@ -4445,6 +4434,8 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -4445,6 +4434,8 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
packBuffer, packBufferSize, packBufferPos, context); packBuffer, packBufferSize, packBufferPos, context);
} }
#undef GRID_STR_SERIALIZE
/* /*
* Local Variables: * Local Variables:
* c-file-style: "Java" * c-file-style: "Java"
......
...@@ -5,6 +5,12 @@ ...@@ -5,6 +5,12 @@
#ifndef SERIALIZE_H #ifndef SERIALIZE_H
#define SERIALIZE_H #define SERIALIZE_H
#include <string.h>
#include "cdi.h"
#include "cdi_cksum.h"
#include "error.h"
/* /*
* Generic interfaces for (de-)marshalling * Generic interfaces for (de-)marshalling
*/ */
...@@ -15,8 +21,65 @@ void serializeUnpack(const void *buf, int buf_size, int *position, ...@@ -15,8 +21,65 @@ void serializeUnpack(const void *buf, int buf_size, int *position,
void *data, int count, int datatype, void *context); void *data, int count, int datatype, void *context);
/* /*
* top-level de-marshalling function * (de-)marshalling function for common data structures
*/ */
static inline int
serializeStrTabGetPackSize(const char **strTab, int numStr,
void *context)
{
xassert(numStr >= 0);
int packBuffSize = 0;
for (size_t i = 0; i < (size_t)numStr; ++i)
{
size_t len = strlen(strTab[i]);
packBuffSize +=
serializeGetSize(1, DATATYPE_INT, context)
+ serializeGetSize((int)len, DATATYPE_TXT, context);
}
packBuffSize +=
serializeGetSize(1, DATATYPE_UINT32, context);
return packBuffSize;
}
static inline void
serializeStrTabPack(const char **strTab, int numStr,
void *buf, int buf_size, int *position, void *context)
{
uint32_t d = 0;
xassert(numStr >= 0);
for (size_t i = 0; i < (size_t)numStr; ++i)
{
size_t len = strlen(strTab[i]);
serializePack(&(int){(int)len}, 1, DATATYPE_INT,
buf, buf_size, position, context);
serializePack(strTab[i], (int)len, DATATYPE_TXT,
buf, buf_size, position, context);
d ^= cdiCheckSum(DATATYPE_TXT, (int)len, strTab[i]);
}
serializePack(&d, 1, DATATYPE_UINT32,
buf, buf_size, position, context);
}
static inline void
serializeStrTabUnpack(const void *buf, int buf_size, int *position,
char **strTab, int numStr, void *context)
{
uint32_t d, d2 = 0;
xassert(numStr >= 0);
for (size_t i = 0; i < (size_t)numStr; ++i)
{
int len;
serializeUnpack(buf, buf_size, position,
&len, 1, DATATYPE_INT, context);
serializeUnpack(buf, buf_size, position,
strTab[i], len, DATATYPE_TXT, context);
strTab[i][len] = '\0';
d2 ^= cdiCheckSum(DATATYPE_TXT, (size_t)len, strTab[i]);
}
serializeUnpack(buf, buf_size, position,
&d, 1, DATATYPE_UINT32, context);
xassert(d == d2);
}
/* /*
* Interfaces for marshalling within a single memory domain * Interfaces for marshalling within a single memory domain
......
...@@ -1359,7 +1359,6 @@ zaxisTxCode ( void ) ...@@ -1359,7 +1359,6 @@ zaxisTxCode ( void )
} }
enum { zaxisNint = 8, enum { zaxisNint = 8,
zaxisNstrings = 4,
vals = 1 << 0, vals = 1 << 0,
lbounds = 1 << 1, lbounds = 1 << 1,
ubounds = 1 << 2, ubounds = 1 << 2,
...@@ -1368,6 +1367,8 @@ enum { zaxisNint = 8, ...@@ -1368,6 +1367,8 @@ enum { zaxisNint = 8,
zaxisHasUUIDFlag = 1 << 5, zaxisHasUUIDFlag = 1 << 5,
}; };
#define ZAXIS_STR_SERIALIZE { zaxisP->name, zaxisP->longname, \
zaxisP->stdname, zaxisP->units }
static static
int zaxisGetMemberMask ( zaxis_t * zaxisP ) int zaxisGetMemberMask ( zaxis_t * zaxisP )
...@@ -1416,9 +1417,14 @@ zaxisGetPackSize(void * voidP, void *context) ...@@ -1416,9 +1417,14 @@ zaxisGetPackSize(void * voidP, void *context)
+ serializeGetSize(1, DATATYPE_UINT32, context); + serializeGetSize(1, DATATYPE_UINT32, context);
} }
packBufferSize += serializeGetSize(zaxisNstrings * CDI_MAX_NAME, DATATYPE_TXT, context) {
+ serializeGetSize(1, DATATYPE_UINT32, context) const char *strTab[] = ZAXIS_STR_SERIALIZE;
+ serializeGetSize(1, DATATYPE_UCHAR, context); size_t numStr = sizeof (strTab) / sizeof (strTab[0]);
packBufferSize
+= serializeStrTabGetPackSize(strTab, (int)numStr, context);
}
packBufferSize += serializeGetSize(1, DATATYPE_UCHAR, context);
if (!cdiUUIDIsNull(zaxisP->uuid)) if (!cdiUUIDIsNull(zaxisP->uuid))
packBufferSize += serializeGetSize(CDI_UUID_SIZE, DATATYPE_UCHAR, context); packBufferSize += serializeGetSize(CDI_UUID_SIZE, DATATYPE_UCHAR, context);
...@@ -1434,7 +1440,6 @@ zaxisUnpack(char * unpackBuffer, int unpackBufferSize, ...@@ -1434,7 +1440,6 @@ zaxisUnpack(char * unpackBuffer, int unpackBufferSize,
{ {
int intBuffer[zaxisNint], memberMask; int intBuffer[zaxisNint], memberMask;
uint32_t d; uint32_t d;
char charBuffer[zaxisNstrings * CDI_MAX_NAME];
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
intBuffer, zaxisNint, DATATYPE_INT, context); intBuffer, zaxisNint, DATATYPE_INT, context);
...@@ -1522,17 +1527,12 @@ zaxisUnpack(char * unpackBuffer, int unpackBufferSize, ...@@ -1522,17 +1527,12 @@ zaxisUnpack(char * unpackBuffer, int unpackBufferSize,
xassert(cdiCheckSum(DATATYPE_FLT64, size, zaxisP->vct) == d); xassert(cdiCheckSum(DATATYPE_FLT64, size, zaxisP->vct) == d);
} }
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, {
charBuffer, zaxisNstrings * CDI_MAX_NAME, DATATYPE_TXT, context); char *strTab[] = ZAXIS_STR_SERIALIZE;
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, int numStr = sizeof (strTab) / sizeof (strTab[0]);
&d, 1, DATATYPE_UINT32, context); serializeStrTabUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
strTab, numStr, context);
xassert(d == cdiCheckSum(DATATYPE_TXT, zaxisNstrings * CDI_MAX_NAME, charBuffer)); }
memcpy ( zaxisP->name, &charBuffer[CDI_MAX_NAME * 0], CDI_MAX_NAME );
memcpy ( zaxisP->longname, &charBuffer[CDI_MAX_NAME * 1], CDI_MAX_NAME );
memcpy ( zaxisP->stdname, &charBuffer[CDI_MAX_NAME * 2], CDI_MAX_NAME );
memcpy ( zaxisP->units, &charBuffer[CDI_MAX_NAME * 3], CDI_MAX_NAME );
serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos, serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
&zaxisP->positive, 1, DATATYPE_UCHAR, context); &zaxisP->positive, 1, DATATYPE_UCHAR, context);
...@@ -1551,7 +1551,6 @@ zaxisPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -1551,7 +1551,6 @@ zaxisPack(void * voidP, void * packBuffer, int packBufferSize,
int intBuffer[zaxisNint]; int intBuffer[zaxisNint];
int memberMask; int memberMask;
uint32_t d; uint32_t d;
char charBuffer[zaxisNstrings * CDI_MAX_NAME];
intBuffer[0] = zaxisP->self; intBuffer[0] = zaxisP->self;
intBuffer[1] = zaxisP->prec; intBuffer[1] = zaxisP->prec;
...@@ -1622,16 +1621,12 @@ zaxisPack(void * voidP, void * packBuffer, int packBufferSize, ...@@ -1622,16 +1621,12 @@ zaxisPack(void * voidP, void * packBuffer, int packBufferSize,
packBuffer, packBufferSize, packBufferPos, context); packBuffer, packBufferSize, packBufferPos, context);
} }
memcpy ( &charBuffer[CDI_MAX_NAME * 0], zaxisP->name, CDI_MAX_NAME ); {
memcpy ( &charBuffer[CDI_MAX_NAME * 1], zaxisP->longname, CDI_MAX_NAME ); const char *strTab[] = ZAXIS_STR_SERIALIZE;
memcpy ( &charBuffer[CDI_MAX_NAME * 2], zaxisP->stdname, CDI_MAX_NAME ); int numStr = sizeof (strTab) / sizeof (strTab[0]);
memcpy ( &charBuffer[CDI_MAX_NAME * 3], zaxisP->units, CDI_MAX_NAME ); serializeStrTabPack(strTab, numStr,
packBuffer, packBufferSize, packBufferPos, context);
serializePack(charBuffer, zaxisNstrings * CDI_MAX_NAME, DATATYPE_TXT, }
packBuffer, packBufferSize, packBufferPos, context);
d = cdiCheckSum(DATATYPE_TXT, zaxisNstrings * CDI_MAX_NAME, charBuffer);
serializePack(&d, 1, DATATYPE_UINT32,
packBuffer, packBufferSize, packBufferPos, context);
serializePack(&zaxisP->positive, 1, DATATYPE_UCHAR, serializePack(&zaxisP->positive, 1, DATATYPE_UCHAR,
packBuffer, packBufferSize, packBufferPos, context); packBuffer, packBufferSize, packBufferPos, context);
...@@ -1647,6 +1642,9 @@ void zaxisGetIndexList ( int nzaxis, int * zaxisResHs ) ...@@ -1647,6 +1642,9 @@ void zaxisGetIndexList ( int nzaxis, int * zaxisResHs )
{ {
reshGetResHListOfType ( nzaxis, zaxisResHs, &zaxisOps ); reshGetResHListOfType ( nzaxis, zaxisResHs, &zaxisOps );
} }
#undef ZAXIS_STR_SERIALIZE
/* /*
* Local Variables: * Local Variables:
* c-file-style: "Java" * c-file-style: "Java"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment