From d0393c623c22de0c695efde5391e6cac846dd251 Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Sun, 30 Aug 2015 20:29:53 -0400 Subject: [PATCH] cmFile.h/c : Added cmWriteStr() and cmReadStr(). --- cmFile.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- cmFile.h | 17 +++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/cmFile.c b/cmFile.c index e126c26..3640304 100644 --- a/cmFile.c +++ b/cmFile.c @@ -36,7 +36,7 @@ cmFileRC_t _cmFileError( cmFile_t* p, cmFileRC_t rc, int errNumb, const cmChar_t return rc; } -cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt ) +cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlags_t flags, cmRpt_t* rpt ) { char mode[] = "/0/0/0"; cmFile_t* p = NULL; @@ -731,6 +731,57 @@ cmFileRC_t cmFileWriteBool( cmFileH_t h, const bool* buf, unsigned c { return cmFileWrite(h,buf,sizeof(buf[0])*cnt); } +cmFileRC_t cmFileWriteStr( cmFileH_t h, const cmChar_t* s ) +{ + cmFileRC_t rc; + + unsigned n = cmTextLength(s); + + if((rc = cmFileWriteUInt(h,&n,1)) != kOkFileRC ) + return rc; + + if( n > 0 ) + rc = cmFileWriteChar(h,s,n); + return rc; +} + + +cmFileRC_t cmFileReadStr( cmFileH_t h, cmChar_t** sRef, unsigned maxCharN ) +{ + unsigned n; + cmFileRC_t rc; + + assert(sRef != NULL ); + + *sRef = NULL; + + if( maxCharN == 0 ) + maxCharN = 16384; + + // read the string length + if((rc = cmFileReadUInt(h,&n,1)) != kOkFileRC ) + return rc; + + // verify that string isn't too long + if( n > maxCharN ) + { + cmFile_t* p = _cmFileHandleToPtr(h); + return cmErrMsg(&p->err,kBufAllocFailFileRC,"The stored string is larger than the maximum allowable size."); + } + + // allocate a read buffer + cmChar_t* s = cmMemAllocZ(cmChar_t,n+1); + + // fill the buffer from the file + if((rc = cmFileReadChar(h,s,n)) != kOkFileRC ) + return rc; + + s[n] = 0; // terminate the string + + *sRef = s; + + return rc; +} cmFileRC_t cmFilePrint( cmFileH_t h, const cmChar_t* text ) @@ -765,3 +816,14 @@ cmFileRC_t cmFilePrintf( cmFileH_t h, const cmChar_t* fmt, ... ) } +cmFileRC_t cmFileLastRC( cmFileH_t h ) +{ + cmFile_t* p = _cmFileHandleToPtr(h); + return cmErrLastRC(&p->err); +} + +cmFileRC_t cmFileSetRC( cmFileH_t h, cmFileRC_t rc ) +{ + cmFile_t* p = _cmFileHandleToPtr(h); + return cmErrSetRC(&p->err,rc); +} diff --git a/cmFile.h b/cmFile.h index 0143d28..bb97658 100644 --- a/cmFile.h +++ b/cmFile.h @@ -221,11 +221,28 @@ extern "C" { cmFileRC_t cmFileWriteDouble( cmFileH_t h, const double* buf, unsigned cnt ); cmFileRC_t cmFileWriteBool( cmFileH_t h, const bool* buf, unsigned cnt ); + // Write a string to a file as ... + // where N is the count of characters in the string. + cmFileRC_t cmFileWriteStr( cmFileH_t h, const cmChar_t* s ); + + // Read a string back from a file as written by cmFileWriteStr(). + // Note that the string will by string will be dynamically allocated + // and threfore must eventually be released via cmMemFree(). + // If maxCharN is set to zero then the default maximum string + // length is 16384. Note that this limit is used to prevent + // corrupt files from generating excessively long strings. + cmFileRC_t cmFileReadStr( cmFileH_t h, cmChar_t** sRef, unsigned maxCharN ); + // Formatted Text Output Functions: // Print formatted text to a file. cmFileRC_t cmFilePrint( cmFileH_t h, const cmChar_t* text ); cmFileRC_t cmFilePrintf( cmFileH_t h, const cmChar_t* fmt, ... ); cmFileRC_t cmFileVPrintf( cmFileH_t h, const cmChar_t* fmt, va_list vl ); + + + cmFileRC_t cmFileLastRC( cmFileH_t h ); + cmFileRC_t cmFileSetRC( cmFileH_t h, cmFileRC_t rc ); + //) //}