From ad22aa111061f338b40fa16676829e334d4a3392 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 3 Dec 2012 17:16:41 -0800 Subject: [PATCH] cmPrefs.h/c Added cmPrefsInit() as wrapper around cmPrefsInitialize(). --- cmPrefs.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- cmPrefs.h | 15 ++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/cmPrefs.c b/cmPrefs.c index 524adfd..49e9e50 100644 --- a/cmPrefs.c +++ b/cmPrefs.c @@ -249,7 +249,43 @@ cmPrNode_t* _cmPrefsPathToNodePtr( cmPr_t* p, const cmChar_t* pathStr, bool repo return _cmPrefsIdToNodePtr(p,id, reportErrFl); } -cmPrRC_t cmPrefsInitialize( cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, cmCtx_t* ctx ) +cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr ) +{ + cmPrRC_t rc = kOkPrRC; + + const cmChar_t* prefsDir = cmFsPrefsDir(); + const cmChar_t* prefsFn = NULL; + + if( fnName == NULL ) + fnName = cmFsAppName(); + + if( fnExt == NULL ) + fnExt = ".js"; + + // if the prefs directory does not exist then create it + if( cmFsIsDir(prefsDir) == false ) + { + if( cmFsMkDir(prefsDir) != kOkFsRC ) + { + rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference directory '%s' could not be created.",prefsDir); + goto errLabel; + } + } + + // create the preference file name + if((prefsFn = cmFsMakeFn( prefsDir, fnName, fnExt, NULL )) == NULL ) + { + rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference file name could not be formed."); + goto errLabel; + } + + // initialize the preference manager + rc = cmPrefsInitialize(ctx,prefsH,prefsFn,cbFunc,cbDataPtr); + + errLabel: + return rc; +} +cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr ) { cmPrRC_t rc = kOkPrRC; @@ -1238,7 +1274,7 @@ cmPrRC_t _cmPrefsCreateJsonNode( if( boolVal != NULL ) jsTypeId = *boolVal ? kTrueTId : kFalseTId; - if((jsnp = cmJsonInsertPair( p->jsH, jsnp, pathEleStr,jsTypeId, sv, rv, iv )) == NULL ) + if((jsnp = cmJsonInsertPair( p->jsH, jsnp, pathEleStr,jsTypeId, sv, iv, rv )) == NULL ) { rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node create failed for '%s'",cmStringNullGuard(pathString)); goto errLabel; @@ -1434,6 +1470,12 @@ cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn ) return rc; } +void cmPrefsReport( cmPrH_t h ) +{ + cmPr_t* p = _cmPrefsHandleToPtr(h); + cmJsonReport(p->jsH); +} + //============================================================================================= // cmPrefsTest() // @@ -1494,7 +1536,7 @@ void _cmPrintNodes( const cmPrNode_t* np ) void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn ) { cmPrH_t h = cmPrNullHandle; - if( cmPrefsInitialize(&h,ifn,NULL,NULL,ctx) != kOkPrRC ) + if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL) != kOkPrRC ) return; cmPr_t* p = _cmPrefsHandleToPtr(h); diff --git a/cmPrefs.h b/cmPrefs.h index 7b8dcd1..1fd8039 100644 --- a/cmPrefs.h +++ b/cmPrefs.h @@ -21,7 +21,8 @@ extern "C" { kInvalidIndexPrRC, kWriteFileFailPrRC, kNodeCreateFailPrRC, - kDuplicateIdPrRC + kDuplicateIdPrRC, + kFileSysFailPrRC }; enum @@ -34,8 +35,14 @@ extern "C" { extern cmPrH_t cmPrNullHandle; - // 'cbFunc' is optional - cmPrRC_t cmPrefsInitialize( cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, cmCtx_t* ctx ); + // cmPrefsInit() creates the preference directory if it does not exist + // according to cmFsPrefsDir(). It then forms the prefs file name as + // 'cmFsPrefsDir()/fnName.fnExt' and call cmPrefsInitialize(). + // Set 'fnName' to NULL to use cmFsAppName() as the pref file name. + // Set 'fnExt' to NULL to use '.js' as the pref file extension. + // 'cbFunc' and 'cbDataPtr' are optional in both versions. + cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr ); + cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr); cmPrRC_t cmPrefsFinalize( cmPrH_t* hp ); bool cmPrefsIsValid( cmPrH_t h ); @@ -169,6 +176,8 @@ extern "C" { // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used. cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn ); + void cmPrefsReport( cmPrH_t h ); + void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn );