From 67f462a501e0daae1b39a00372430e327831e22d Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 31 Oct 2012 18:01:51 -0700 Subject: [PATCH] cmPrefs.c/h Renamed 'Scalar' func's., Fixed bug in cmPrefsCreateXXX() --- cmPrefs.c | 115 ++++++++++++++++++++++++++++++++++++++++-------------- cmPrefs.h | 48 +++++++++++++++-------- 2 files changed, 117 insertions(+), 46 deletions(-) diff --git a/cmPrefs.c b/cmPrefs.c index 098a0e0..486c8a6 100644 --- a/cmPrefs.c +++ b/cmPrefs.c @@ -290,7 +290,7 @@ cmPrRC_t cmPrefsInitialize( cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc goto errLabel; } - if( cmJsonCreateObject( p->jsH, NULL ) != kOkJsRC ) + if( cmJsonCreateObject( p->jsH, NULL ) == NULL ) { rc = cmErrMsg(&p->err,kJsonFailPrRC,"The JSON root object could not be created."); goto errLabel; @@ -335,6 +335,12 @@ cmPrRC_t cmPrefsFinalize( cmPrH_t* hp ) bool cmPrefsIsValid( cmPrH_t h ) { return h.h != NULL; } +const cmChar_t* cmPrefsFileName( cmPrH_t h ) +{ + cmPr_t* p = _cmPrefsHandleToPtr(h); + return p->fn; +} + cmPrRC_t cmPrefsRC( cmPrH_t h) { return cmErrLastRC(&_cmPrefsHandleToPtr(h)->err); } @@ -665,7 +671,7 @@ const cmChar_t* cmPrefsStringDef( cmPrH_t h, const cmChar_t* pathStr, const cmCh } -cmPrRC_t cmPrefsScalarBool( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr ) +cmPrRC_t cmPrefsBoolRc( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr ) { unsigned id; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) @@ -675,7 +681,7 @@ cmPrRC_t cmPrefsScalarBool( cmPrH_t h, const cmChar_t* pathStr, bool* return cmPrefsGetBool(h,id,retValPtr,&n); } -cmPrRC_t cmPrefsScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr ) +cmPrRC_t cmPrefsUIntRc( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr ) { unsigned id; unsigned n = 1; @@ -691,7 +697,7 @@ cmPrRC_t cmPrefsScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigne return rc; } -cmPrRC_t cmPrefsScalarInt( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr ) +cmPrRC_t cmPrefsIntRc( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr ) { unsigned id; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) @@ -701,7 +707,7 @@ cmPrRC_t cmPrefsScalarInt( cmPrH_t h, const cmChar_t* pathStr, int* return cmPrefsGetInt(h,id,retValPtr,&n); } -cmPrRC_t cmPrefsScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr ) +cmPrRC_t cmPrefsFloatRc( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr ) { unsigned id; unsigned n = 1; @@ -717,7 +723,7 @@ cmPrRC_t cmPrefsScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float* return rc; } -cmPrRC_t cmPrefsScalarReal( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr ) +cmPrRC_t cmPrefsRealRc( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr ) { unsigned id; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) @@ -727,7 +733,7 @@ cmPrRC_t cmPrefsScalarReal( cmPrH_t h, const cmChar_t* pathStr, double* return cmPrefsGetReal(h,id,retValPtr,&n); } -cmPrRC_t cmPrefsScalarString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr ) +cmPrRC_t cmPrefsStringRc( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr ) { unsigned id; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) @@ -996,30 +1002,71 @@ cmPrRC_t _cmPrefsSetValues( cmPrH_t h, unsigned id, const bool* bvp, const int* return _cmPrefsSetValues2( p, np, bvp, ivp, rvp, svp, eleCntPtr ); } -cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr ) +cmPrRC_t cmPrefsSetBoolArray( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr ) { return _cmPrefsSetValues(h, id, vp, NULL, NULL, NULL, eleCntPtr ); } -cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr ) +cmPrRC_t cmPrefsSetIntArray( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr ) { return _cmPrefsSetValues(h, id, NULL, vp, NULL, NULL, eleCntPtr ); } -cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr ) +cmPrRC_t cmPrefsSetRealArray( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr ) { return _cmPrefsSetValues(h, id, NULL, NULL, vp, NULL, eleCntPtr ); } -cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr ) +cmPrRC_t cmPrefsSetStringArray( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr ) { return _cmPrefsSetValues(h, id, NULL, NULL, NULL, vp, eleCntPtr ); } -cmPrRC_t cmPrefsSetScalarBool( cmPrH_t h, const cmChar_t* pathStr, bool val ) +cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, bool val ) +{ + unsigned n = 1; + return cmPrefsSetBoolArray(h,id,&val,&n); +} + +cmPrRC_t cmPrefsSetUInt( cmPrH_t h, unsigned id, unsigned val ) +{ + unsigned n = 1; + int ival = (int)val; + return cmPrefsSetIntArray(h,id,&ival,&n); +} + +cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, int val ) +{ + unsigned n = 1; + return cmPrefsSetIntArray(h,id,&val,&n); +} + +cmPrRC_t cmPrefsSetFloat( cmPrH_t h, unsigned id, float val ) +{ + unsigned n = 1; + double dval = val; + return cmPrefsSetRealArray(h,id,&dval,&n); +} + +cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, double val ) +{ + unsigned n = 1; + return cmPrefsSetRealArray(h,id,&val,&n); +} + +cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t* val ) +{ + unsigned n = 1; + return cmPrefsSetStringArray(h,id,&val,&n); +} + + + + +cmPrRC_t cmPrefsPathSetBool( cmPrH_t h, const cmChar_t* pathStr, bool val ) { unsigned id; unsigned n = 1; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) return kVarNotFoundPrRC; - return cmPrefsSetBool(h,id,&val,&n); + return cmPrefsSetBoolArray(h,id,&val,&n); } -cmPrRC_t cmPrefsSetScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val ) +cmPrRC_t cmPrefsPathSetUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val ) { unsigned id; unsigned n = 1; @@ -1027,20 +1074,20 @@ cmPrRC_t cmPrefsSetScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned va return kVarNotFoundPrRC; int ival = (int)val; - return cmPrefsSetInt(h,id,&ival,&n); + return cmPrefsSetIntArray(h,id,&ival,&n); } -cmPrRC_t cmPrefsSetScalarInt( cmPrH_t h, const cmChar_t* pathStr, int val ) +cmPrRC_t cmPrefsPathSetInt( cmPrH_t h, const cmChar_t* pathStr, int val ) { unsigned id; unsigned n = 1; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) return kVarNotFoundPrRC; - return cmPrefsSetInt(h,id,&val,&n); + return cmPrefsSetIntArray(h,id,&val,&n); } -cmPrRC_t cmPrefsSetScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float val ) +cmPrRC_t cmPrefsPathSetFloat( cmPrH_t h, const cmChar_t* pathStr, float val ) { unsigned id; unsigned n = 1; @@ -1048,27 +1095,27 @@ cmPrRC_t cmPrefsSetScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float val ) return kVarNotFoundPrRC; double dval = val; - return cmPrefsSetReal(h,id,&dval,&n); + return cmPrefsSetRealArray(h,id,&dval,&n); } -cmPrRC_t cmPrefsSetScalarReal( cmPrH_t h, const cmChar_t* pathStr, double val ) +cmPrRC_t cmPrefsPathSetReal( cmPrH_t h, const cmChar_t* pathStr, double val ) { unsigned id; unsigned n = 1; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) return kVarNotFoundPrRC; - return cmPrefsSetReal(h,id,&val,&n); + return cmPrefsSetRealArray(h,id,&val,&n); } -cmPrRC_t cmPrefsSetScalarString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val ) +cmPrRC_t cmPrefsPathSetString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val ) { unsigned id; unsigned n = 1; if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId ) return kVarNotFoundPrRC; - return cmPrefsSetString(h,id,&val,&n); + return cmPrefsSetStringArray(h,id,&val,&n); } @@ -1168,7 +1215,7 @@ cmPrRC_t _cmPrefsCreateJsonNode( } // we have followed 'pathString' to the last node which already - // exists in the JSON tree - now we must new JSON nodes to reflect + // exists in the JSON tree - now we must create new JSON nodes to reflect // the remaining path elements for(; iid; + // create a pref node to associate with this new level - if((pnp = _cmPrefsCreateNode(p, jsPairNodePtr, pnp->pathPtr )) == NULL ) + if((pnp = _cmPrefsCreateNode(p, jsPairNodePtr, pnp==NULL ? NULL : pnp->pathPtr )) == NULL ) { rc = cmErrMsg(&p->err,kNodeCreateFailPrRC,"Creation failed for the '%s' element of the preference node '%s'.",cmStringNullGuard(pathEleStr),cmStringNullGuard(pathString)); goto errLabel; } + // always leave internal nodes with id=cmInvalidId, leaf node id's will be set below + p->id = nxtId; + pnp->id = cmInvalidId; + pathEleStr += strlen(pathEleStr) + 1; } assert( pnp != NULL ); // if an preference variable 'id' was given then set it here - if( id != cmInvalidId ) + if( id == cmInvalidId ) + pnp->id = _cmPrefsCalcNextAvailId(p); + else { if( _cmPrefsIdToNodePtr(p, id, false ) != NULL ) - cmErrWarnMsg(&p->err,kDuplicateIdPrRC,"The preference variable id '%i' is used by multiple preference variables including '%s'.",cmStringNullGuard(pathString)); + cmErrWarnMsg(&p->err,kDuplicateIdPrRC,"The preference variable id '%i' is used by multiple preference variables including '%s'.",id,cmStringNullGuard(pathString)); pnp->id = id; } @@ -1462,19 +1517,19 @@ void _cmPrintNodes( const cmPrNode_t* np ) sr = 44100; n = 1; - cmPrefsSetInt(h, cmPrefsId(h,"cfg/srate",true), &sr, &n); + cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/srate",true), &sr, &n); cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), &sr, &n ); printf("sr:%i %i\n",sr,n); int sarr[] = {10,11,12,13 }; n = sizeof(sarr)/sizeof(sarr[0]); - cmPrefsSetInt(h, cmPrefsId(h,"cfg/chNames/array",true), sarr, &n); + cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/chNames/array",true), sarr, &n); cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), sarr, &n ); printf("array:%i %i %i %i n=%i\n",sarr[0],sarr[1],sarr[2],sarr[3],n); int tarr[] = {20,21 }; n = sizeof(tarr)/sizeof(tarr[0]); - cmPrefsSetInt(h, cmPrefsId(h,"cfg/chNames/array",true), tarr, &n); + cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/chNames/array",true), tarr, &n); cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), tarr, &n ); printf("array:%i %i n=%i\n",tarr[0],tarr[1],n); diff --git a/cmPrefs.h b/cmPrefs.h index 02b595d..41def74 100644 --- a/cmPrefs.h +++ b/cmPrefs.h @@ -34,6 +34,8 @@ extern "C" { bool cmPrefsIsValid( cmPrH_t h ); + const cmChar_t* cmPrefsFileName( cmPrH_t h ); + // Return last RC. cmPrRC_t cmPrefsRC( cmPrH_t h); @@ -52,6 +54,7 @@ extern "C" { // On input *'eleCntPtr' must contain the number of elements in the buffer pointed to by 'vp'. // On return *'eleCntPtr' contains the actuall number of elements returned by the function. + // Set *eleCntPtr to 1 for scalar a values. cmPrRC_t cmPrefsGetBool( cmPrH_t h, unsigned id, bool* vp, unsigned* eleCntPtr ); cmPrRC_t cmPrefsGetInt( cmPrH_t h, unsigned id, int* vp, unsigned* eleCntPtr ); cmPrRC_t cmPrefsGetReal( cmPrH_t h, unsigned id, double* vp, unsigned* eleCntPtr ); @@ -76,12 +79,12 @@ extern "C" { const cmChar_t* cmPrefsStringDef( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* dfltVal ); // Get a scalar value. - cmPrRC_t cmPrefsScalarBool( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr ); - cmPrRC_t cmPrefsScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr ); - cmPrRC_t cmPrefsScalarInt( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr ); - cmPrRC_t cmPrefsScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr ); - cmPrRC_t cmPrefsScalarReal( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr ); - cmPrRC_t cmPrefsScalarString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr ); + cmPrRC_t cmPrefsBoolRc( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr ); + cmPrRC_t cmPrefsUIntRc( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr ); + cmPrRC_t cmPrefsIntRc( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr ); + cmPrRC_t cmPrefsFloatRc( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr ); + cmPrRC_t cmPrefsRealRc( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr ); + cmPrRC_t cmPrefsStringRc( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr ); // Simplified array interface - check cmPrefsRC() for errors @@ -108,17 +111,24 @@ extern "C" { // 3) For scalar (non-array) variables *eleCntPtr must be set to 1. // - cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr ); - cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr ); - cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr ); - cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr ); + cmPrRC_t cmPrefsSetBoolArray( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr ); + cmPrRC_t cmPrefsSetIntArray( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr ); + cmPrRC_t cmPrefsSetRealArray( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr ); + cmPrRC_t cmPrefsSetStringArray( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr ); - cmPrRC_t cmPrefsSetScalarBool( cmPrH_t h, const cmChar_t* pathStr, bool val ); - cmPrRC_t cmPrefsSetScalarUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val ); - cmPrRC_t cmPrefsSetScalarInt( cmPrH_t h, const cmChar_t* pathStr, int val ); - cmPrRC_t cmPrefsSetScalarFloat( cmPrH_t h, const cmChar_t* pathStr, float val ); - cmPrRC_t cmPrefsSetScalarReal( cmPrH_t h, const cmChar_t* pathStr, double val ); - cmPrRC_t cmPrefsSetScalarString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val ); + cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, bool val ); + cmPrRC_t cmPrefsSetUInt( cmPrH_t h, unsigned id, unsigned val ); + cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, int val ); + cmPrRC_t cmPrefsSetFloat( cmPrH_t h, unsigned id, float val ); + cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, double val ); + cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t* val ); + + cmPrRC_t cmPrefsPathSetBool( cmPrH_t h, const cmChar_t* pathStr, bool val ); + cmPrRC_t cmPrefsPathSetUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val ); + cmPrRC_t cmPrefsPathSetInt( cmPrH_t h, const cmChar_t* pathStr, int val ); + cmPrRC_t cmPrefsPathSetFloat( cmPrH_t h, const cmChar_t* pathStr, float val ); + cmPrRC_t cmPrefsPathSetReal( cmPrH_t h, const cmChar_t* pathStr, double val ); + cmPrRC_t cmPrefsPathSetString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val ); // Create a new preference variable and set it's value to 'val'. // If a variable with the same path and type already exists and kForceValuePrFl is set then update it's value to 'val'. @@ -126,6 +136,10 @@ extern "C" { // // If a variable with the same path but a different type exists then an error is returned. // + // The 'id' argument is optional. If 'id' is set to cmInvalidId then the + // variable will be automatically assigned an id. The value of the + // automatically assigned id can be found from the path string + // via cmPrefsId() // Set kForceValuePrFl enum { kForceValuePrFl=0x01 }; @@ -144,6 +158,8 @@ extern "C" { cmPrRC_t cmPrefsCreateStringArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const cmChar_t** val, unsigned eleCnt ); bool cmPrefsIsDirty( cmPrH_t h ); + + // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used. cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn );