From e5ee6432c1f28e86da35e840a5e6d8ac85795e43 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 5 Nov 2013 14:34:22 -0800 Subject: [PATCH] cmCsv.h/c : Added cmCsvInsertSymHex(),cmCsvSetCellHex(), and cmInsertHexColAfter(). Also fixed bug in the cmCsvInsertXXXColAfter() where the return cell pointer was not correctly set. --- cmCsv.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- cmCsv.h | 4 ++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/cmCsv.c b/cmCsv.c index 5b594e3..e18373e 100644 --- a/cmCsv.c +++ b/cmCsv.c @@ -729,6 +729,20 @@ unsigned cmCsvInsertSymUInt( cmCsvH_t h, unsigned v ) return cmInvalidId; } +unsigned cmCsvInsertSymHex( cmCsvH_t h, unsigned v ) +{ + const char* fmt = "0x%x"; + unsigned n = snprintf(NULL,0,fmt,v)+1; + char buf[n]; + + buf[0]= 0; + if( snprintf(buf,n,fmt,v) == n-1 ) + return cmCsvInsertSymText(h,buf); + + _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The unsigned int 0x%x could not be converted to text.",v); + return cmInvalidId; +} + unsigned cmCsvInsertSymFloat( cmCsvH_t h, float v ) { const char* fmt = "%f"; @@ -812,6 +826,24 @@ cmCsvRC_t cmCsvSetCellUInt( cmCsvH_t h, unsigned row, unsigned col, unsigned return kOkCsvRC; } +cmCsvRC_t cmCsvSetCellHex( cmCsvH_t h, unsigned row, unsigned col, unsigned v ) +{ + cmCsvCell_t* cp; + unsigned symId; + + if((cp = _cmCsvCellPtr(h,row,col)) == NULL ) + return cmErrLastRC(&_cmCsvHandleToPtr(h)->err); + + if((symId = cmCsvInsertSymHex(h,v)) == cmInvalidId ) + return cmErrLastRC(&_cmCsvHandleToPtr(h)->err); + + cp->symId = symId; + cp->flags &= !kTypeTMask; + cp->flags |= kIntCsvTFl; + + return kOkCsvRC; +} + cmCsvRC_t cmCsvSetCellFloat( cmCsvH_t h, unsigned row, unsigned col, float v ) { cmCsvCell_t* cp; @@ -985,7 +1017,7 @@ cmCsvRC_t cmCsvInsertTextColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCs cmCsvCell_t* ncp; if( cellPtrPtr != NULL ) - cellPtrPtr = NULL; + *cellPtrPtr = NULL; if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) if((rc = cmCsvSetCellText(h, ncp->row, ncp->col, text )) == kOkCsvRC ) @@ -1001,7 +1033,7 @@ cmCsvRC_t cmCsvInsertIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCs cmCsvCell_t* ncp; if( cellPtrPtr != NULL ) - cellPtrPtr = NULL; + *cellPtrPtr = NULL; if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) if((rc = cmCsvSetCellInt(h, ncp->row, ncp->col, val )) == kOkCsvRC ) @@ -1017,7 +1049,7 @@ cmCsvRC_t cmCsvInsertUIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCs cmCsvCell_t* ncp; if( cellPtrPtr != NULL ) - cellPtrPtr = NULL; + *cellPtrPtr = NULL; if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) if((rc = cmCsvSetCellUInt(h, ncp->row, ncp->col, val )) == kOkCsvRC ) @@ -1027,13 +1059,29 @@ cmCsvRC_t cmCsvInsertUIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCs return rc; } +cmCsvRC_t cmCsvInsertHexColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId ) +{ + cmCsvRC_t rc; + cmCsvCell_t* ncp; + + if( cellPtrPtr != NULL ) + *cellPtrPtr = NULL; + + if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) + if((rc = cmCsvSetCellHex(h, ncp->row, ncp->col, val )) == kOkCsvRC ) + if( cellPtrPtr != NULL ) + *cellPtrPtr = ncp; + + return rc; +} + cmCsvRC_t cmCsvInsertFloatColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, float val, unsigned lexTId ) { cmCsvRC_t rc; cmCsvCell_t* ncp; if( cellPtrPtr != NULL ) - cellPtrPtr = NULL; + *cellPtrPtr = NULL; if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) if((rc = cmCsvSetCellFloat(h, ncp->row, ncp->col, val )) == kOkCsvRC ) @@ -1049,7 +1097,7 @@ cmCsvRC_t cmCsvInsertDoubleColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCs cmCsvCell_t* ncp; if( cellPtrPtr != NULL ) - cellPtrPtr = NULL; + *cellPtrPtr = NULL; if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC ) if((rc = cmCsvSetCellDouble(h, ncp->row, ncp->col, val )) == kOkCsvRC ) diff --git a/cmCsv.h b/cmCsv.h index d3b7b06..e686c1c 100644 --- a/cmCsv.h +++ b/cmCsv.h @@ -109,13 +109,16 @@ extern "C" { unsigned cmCsvInsertSymText( cmCsvH_t h, const char* text ); unsigned cmCsvInsertSymInt( cmCsvH_t h, int v ); unsigned cmCsvInsertSymUInt( cmCsvH_t h, unsigned v ); + unsigned cmCsvInsertSymHex( cmCsvH_t h, unsigned v ); unsigned cmCsvInsertSymFloat( cmCsvH_t h, float v ); unsigned cmCsvInsertSymDouble( cmCsvH_t h, double v ); + // Set the value associated with a cell. cmCsvRC_t cmCsvSetCellText( cmCsvH_t h, unsigned row, unsigned col, const char* text ); cmCsvRC_t cmCsvSetCellInt( cmCsvH_t h, unsigned row, unsigned col, int v ); cmCsvRC_t cmCsvSetCellUInt( cmCsvH_t h, unsigned row, unsigned col, unsigned v ); + cmCsvRC_t cmCsvSetCellHex( cmCsvH_t h, unsigned row, unsigned col, unsigned v ); cmCsvRC_t cmCsvSetCellFloat( cmCsvH_t h, unsigned row, unsigned col, float v ); cmCsvRC_t cmCsvSetCellDouble( cmCsvH_t h, unsigned row, unsigned col, double v ); @@ -134,6 +137,7 @@ extern "C" { cmCsvRC_t cmCsvInsertTextColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, const char* val, unsigned lexTId ); cmCsvRC_t cmCsvInsertIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, int val, unsigned lexTId ); cmCsvRC_t cmCsvInsertUIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId ); + cmCsvRC_t cmCsvInsertHexColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId ); cmCsvRC_t cmCsvInsertFloatColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, float val, unsigned lexTId ); cmCsvRC_t cmCsvInsertDoubleColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, double val, unsigned lexTId );