Przeglądaj źródła

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

master
kevin 11 lat temu
rodzic
commit
ad22aa1110
2 zmienionych plików z 57 dodań i 6 usunięć
  1. 45
    3
      cmPrefs.c
  2. 12
    3
      cmPrefs.h

+ 45
- 3
cmPrefs.c Wyświetl plik

249
   return _cmPrefsIdToNodePtr(p,id, reportErrFl);
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
   cmPrRC_t rc = kOkPrRC;
290
   cmPrRC_t rc = kOkPrRC;
255
 
291
 
1238
         if( boolVal != NULL )
1274
         if( boolVal != NULL )
1239
           jsTypeId = *boolVal ? kTrueTId : kFalseTId;
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
           rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node create failed for '%s'",cmStringNullGuard(pathString));
1279
           rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node create failed for '%s'",cmStringNullGuard(pathString));
1244
           goto errLabel;
1280
           goto errLabel;
1434
   return rc;
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
 // cmPrefsTest()
1480
 // cmPrefsTest()
1439
 //
1481
 //
1494
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
1536
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
1495
 {
1537
 {
1496
   cmPrH_t h = cmPrNullHandle;
1538
   cmPrH_t h = cmPrNullHandle;
1497
-  if( cmPrefsInitialize(&h,ifn,NULL,NULL,ctx) != kOkPrRC )
1539
+  if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL) != kOkPrRC )
1498
     return;
1540
     return;
1499
 
1541
 
1500
   cmPr_t* p = _cmPrefsHandleToPtr(h);
1542
   cmPr_t* p = _cmPrefsHandleToPtr(h);

+ 12
- 3
cmPrefs.h Wyświetl plik

21
     kInvalidIndexPrRC,
21
     kInvalidIndexPrRC,
22
     kWriteFileFailPrRC,
22
     kWriteFileFailPrRC,
23
     kNodeCreateFailPrRC,
23
     kNodeCreateFailPrRC,
24
-    kDuplicateIdPrRC
24
+    kDuplicateIdPrRC,
25
+    kFileSysFailPrRC
25
   };
26
   };
26
 
27
 
27
   enum
28
   enum
34
 
35
 
35
   extern cmPrH_t cmPrNullHandle;
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
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
46
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
40
 
47
 
41
   bool cmPrefsIsValid( cmPrH_t h );
48
   bool cmPrefsIsValid( cmPrH_t h );
169
   // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used.
176
   // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used.
170
   cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn );
177
   cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn );
171
 
178
 
179
+  void cmPrefsReport( cmPrH_t h );
180
+
172
 
181
 
173
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn );
182
   void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn );
174
     
183
     

Ładowanie…
Anuluj
Zapisz