Bläddra i källkod

cmPrefs.h/c Added cmPrefsInit() as wrapper around cmPrefsInitialize().

master
kevin 12 år sedan
förälder
incheckning
ad22aa1110
2 ändrade filer med 57 tillägg och 6 borttagningar
  1. 45
    3
      cmPrefs.c
  2. 12
    3
      cmPrefs.h

+ 45
- 3
cmPrefs.c Visa fil

@@ -249,7 +249,43 @@ cmPrNode_t* _cmPrefsPathToNodePtr( cmPr_t* p, const cmChar_t* pathStr, bool repo
249 249
   return _cmPrefsIdToNodePtr(p,id, reportErrFl);
250 250
 }
251 251
 
252
-cmPrRC_t cmPrefsInitialize( cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, cmCtx_t* ctx )
252
+cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
253
+{
254
+  cmPrRC_t rc = kOkPrRC;
255
+
256
+  const cmChar_t* prefsDir = cmFsPrefsDir();
257
+  const cmChar_t* prefsFn  = NULL;
258
+
259
+  if( fnName == NULL )
260
+    fnName = cmFsAppName();
261
+
262
+  if( fnExt == NULL )
263
+    fnExt = ".js";
264
+  
265
+  // if the prefs directory does not exist then create it
266
+  if( cmFsIsDir(prefsDir) == false )
267
+  {
268
+    if( cmFsMkDir(prefsDir) != kOkFsRC )
269
+    {
270
+      rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference directory '%s' could not be created.",prefsDir);
271
+      goto errLabel;
272
+    }
273
+  }
274
+
275
+  // create the preference file name
276
+  if((prefsFn = cmFsMakeFn( prefsDir, fnName, fnExt, NULL )) == NULL )
277
+  {
278
+    rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference file name could not be formed.");
279
+    goto errLabel;
280
+  }
281
+
282
+  // initialize the preference manager
283
+  rc = cmPrefsInitialize(ctx,prefsH,prefsFn,cbFunc,cbDataPtr);
284
+
285
+ errLabel:
286
+  return rc;
287
+}
288
+cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
253 289
 {
254 290
   cmPrRC_t rc = kOkPrRC;
255 291
 
@@ -1238,7 +1274,7 @@ cmPrRC_t  _cmPrefsCreateJsonNode(
1238 1274
         if( boolVal != NULL )
1239 1275
           jsTypeId = *boolVal ? kTrueTId : kFalseTId;
1240 1276
 
1241
-        if((jsnp = cmJsonInsertPair( p->jsH, jsnp, pathEleStr,jsTypeId, sv, rv, iv )) == NULL )
1277
+        if((jsnp = cmJsonInsertPair( p->jsH, jsnp, pathEleStr,jsTypeId, sv, iv, rv )) == NULL )
1242 1278
         {
1243 1279
           rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node create failed for '%s'",cmStringNullGuard(pathString));
1244 1280
           goto errLabel;
@@ -1434,6 +1470,12 @@ cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
1434 1470
   return rc;
1435 1471
 }
1436 1472
 
1473
+void cmPrefsReport( cmPrH_t h )
1474
+{
1475
+  cmPr_t*  p  = _cmPrefsHandleToPtr(h);
1476
+  cmJsonReport(p->jsH);
1477
+}
1478
+
1437 1479
 //=============================================================================================
1438 1480
 // cmPrefsTest()
1439 1481
 //
@@ -1494,7 +1536,7 @@ void _cmPrintNodes( const cmPrNode_t* np )
1494 1536
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
1495 1537
 {
1496 1538
   cmPrH_t h = cmPrNullHandle;
1497
-  if( cmPrefsInitialize(&h,ifn,NULL,NULL,ctx) != kOkPrRC )
1539
+  if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL) != kOkPrRC )
1498 1540
     return;
1499 1541
 
1500 1542
   cmPr_t* p = _cmPrefsHandleToPtr(h);

+ 12
- 3
cmPrefs.h Visa fil

@@ -21,7 +21,8 @@ extern "C" {
21 21
     kInvalidIndexPrRC,
22 22
     kWriteFileFailPrRC,
23 23
     kNodeCreateFailPrRC,
24
-    kDuplicateIdPrRC
24
+    kDuplicateIdPrRC,
25
+    kFileSysFailPrRC
25 26
   };
26 27
 
27 28
   enum
@@ -34,8 +35,14 @@ extern "C" {
34 35
 
35 36
   extern cmPrH_t cmPrNullHandle;
36 37
 
37
-  // 'cbFunc' is optional
38
-  cmPrRC_t cmPrefsInitialize( cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, cmCtx_t* ctx );
38
+  // cmPrefsInit() creates the preference directory if it does not exist 
39
+  // according to cmFsPrefsDir(). It then forms the prefs file name as
40
+  // 'cmFsPrefsDir()/fnName.fnExt' and call cmPrefsInitialize().
41
+  // Set 'fnName' to NULL to use cmFsAppName() as the pref file name.
42
+  // Set 'fnExt' to NULL to use '.js' as the pref file extension.
43
+  // 'cbFunc'  and 'cbDataPtr' are optional in both versions.
44
+  cmPrRC_t cmPrefsInit(   cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr );
45
+  cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr);
39 46
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
40 47
 
41 48
   bool cmPrefsIsValid( cmPrH_t h );
@@ -169,6 +176,8 @@ extern "C" {
169 176
   // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used.
170 177
   cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn );
171 178
 
179
+  void cmPrefsReport( cmPrH_t h );
180
+
172 181
 
173 182
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn );
174 183
     

Laddar…
Avbryt
Spara