Skip to content

Commit

Permalink
Initial das to csv converter
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiker committed Jul 4, 2024
1 parent 18c840f commit dfa1f36
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 22 deletions.
51 changes: 45 additions & 6 deletions das2/datum.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ bool das_datum_toTime(const das_datum* pThis, das_time* pDt)

/** Write a datum out as a string */
char* _das_datum_toStr(
const das_datum* pThis, char* sBuf, int nLen, int nFracDigits, bool bPrnUnits
const das_datum* pThis, char* sBuf, int nLen, int nFracDigits, bool bPrnUnits,
const char* sSep
){
if(nLen < 2) return NULL;
memset(sBuf, 0, nLen);
Expand Down Expand Up @@ -327,7 +328,11 @@ char* _das_datum_toStr(
size_t u = 0;
const das_idx_info* pInfo = NULL;
const das_byteseq* pBs = NULL;

const das_geovec* pVec = NULL;
das_datum dmSub;
int nUsed = 0;
char* pWrite = NULL;

int nWrote = 0;
switch(pThis->vt){

Expand Down Expand Up @@ -382,7 +387,7 @@ char* _das_datum_toStr(

while((u*3 < (nLen - 4))&&(u < pBs->sz)){

snprintf(sBuf + u*3, 3, "%hhX ", ((ubyte*)pBs->ptr)[u]);
snprintf(sBuf + u*3, 3, "%hhX%s", ((ubyte*)pBs->ptr)[u], sSep);
++u;
nWrote += 3;
}
Expand All @@ -393,9 +398,35 @@ char* _das_datum_toStr(

case vtIndex:
pInfo = (const das_idx_info*)pThis;
snprintf(sBuf, nLen - 1, "Offset: %zd, Count: %zu", pInfo->nOffset, pInfo->uCount);
snprintf(sBuf, nLen - 1, "Offset:%zd%sCount:%zu", pInfo->nOffset, sSep, pInfo->uCount);
nWrote = strlen(sBuf);
break;

case vtGeoVec:
pVec = (const das_geovec*)pThis;
pWrite = sBuf;
pWrite[0] = '\0';
for(u = 0; u < pVec->ncomp; ++u){
memset(&dmSub, 0, sizeof(das_datum));
memcpy(&dmSub, ((ubyte*)pVec) + pVec->esize * u, pVec->esize);
dmSub.vt = pVec->et;
dmSub.units = pThis->units;
dmSub.vsize = das_vt_size(dmSub.vt);

/* Call self for single component */
nUsed = strlen(pWrite);
pWrite += nUsed;
nLen -= strlen(pWrite);
_das_datum_toStr(&dmSub, pWrite, nLen, nFracDigits, false, sSep);

if(u < (pVec->ncomp - 1)){
nUsed = strlen(pWrite);
pWrite += nUsed;
nLen -= nUsed;
strncpy(pWrite, sSep, nLen);
}
}
break;

default:
strncpy(sBuf, "UNKNOWN", nLen -1);
Expand All @@ -422,12 +453,20 @@ char* _das_datum_toStr(
char* das_datum_toStr(
const das_datum* pThis, char* sStr, size_t uLen, int nFracDigits
){
return _das_datum_toStr(pThis, sStr, uLen, nFracDigits, true);
return _das_datum_toStr(pThis, sStr, uLen, nFracDigits, true, ";");
}

char* das_datum_toStrValOnly(
const das_datum* pThis, char* sStr, size_t uLen, int nFracDigits
){
return _das_datum_toStr(pThis, sStr, uLen, nFracDigits, false);
return _das_datum_toStr(pThis, sStr, uLen, nFracDigits, false, ";");
}

char* das_datum_toStrValOnlySep(
const das_datum* pThis, char* sStr, size_t uLen, int nFracDigits,
const char* sSep
){
return _das_datum_toStr(pThis, sStr, uLen, nFracDigits, false, sSep);
}


7 changes: 7 additions & 0 deletions das2/datum.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ DAS_API char* das_datum_toStrValOnly(
const das_datum* pThis, char* sStr, size_t uLen, int nFracDigits
);

/** Similar to das_datum_toStr, but can specify a separator for vectors.
* The separator is ignored if the element type is not multi-valued */
char* das_datum_toStrValOnlySep(
const das_datum* pThis, char* sStr, size_t uLen, int nFracDigits,
const char* sSep
);


/** Get a datum value as a double
*
Expand Down
2 changes: 1 addition & 1 deletion das2/dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ DAS_API const DasVar* DasDim_getVar(const DasDim* pThis, const char* sRole);
* @returns The number of defined variables
* @memberof DasDim
*/
#define DasDim_numVars(P) ((P)->uVars);
#define DasDim_numVars(P) ((P)->uVars)

/** Get a variable by index
*
Expand Down
23 changes: 23 additions & 0 deletions das2/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ size_t DasProp_size(const DasProp* pProp);
*/
const char* DasProp_value(const DasProp* pProp);

/* * Get a sub value for a multivalued property.
* (not implemented)
*
* If DasProp_isSet() or DasProp_isRange() returns true, then this property
* has sub values.
*
* @param pProp The property in question
*
* @param idx The index of the sub property, index 0 should always be defined.
*
* @param sBuf A buffer to receive the value.
*
* @param nLen The length of the buffer to recieve the value. Up to nLen - 1
* bytes will be copied in, then a null is appended. Output should
* always be null terminated even if there wasn't enough room for the
* entire sub-value.
*
* @returns The number of bytes needed to store the sub value along with it's
* terminating null. If this is greater then nLen, then the output
* has been truncated.
*/
/* bool DasProp_subValue(const DasProp* pProp, int idx, char* sBuf, size_t nLen); */

/** Get the value separator character for array-style properties
* @memberof DasProp
*/
Expand Down
Loading

0 comments on commit dfa1f36

Please sign in to comment.