Преглед изворни кода

cmPrefs.h/c: Added prefixPathStr to prefs.

master
kevin пре 11 година
родитељ
комит
9d76f9bf3b
2 измењених фајлова са 51 додато и 22 уклоњено
  1. 49
    20
      cmPrefs.c
  2. 2
    2
      cmPrefs.h

+ 49
- 20
cmPrefs.c Прегледај датотеку

49
   cmPrNode_t* idChainPtr;       //
49
   cmPrNode_t* idChainPtr;       //
50
   unsigned    id;               // next JSON node id
50
   unsigned    id;               // next JSON node id
51
   cmChar_t*   pathBuf;
51
   cmChar_t*   pathBuf;
52
-
52
+  cmChar_t*   pathPrefixStr;
53
 } cmPr_t;
53
 } cmPr_t;
54
 
54
 
55
 cmPr_t* _cmPrefsHandleToPtr( cmPrH_t h )
55
 cmPr_t* _cmPrefsHandleToPtr( cmPrH_t h )
171
 // Convert 'pathString' to a sequence of zero terminated sub-strings.
171
 // Convert 'pathString' to a sequence of zero terminated sub-strings.
172
 // The character string returned from this function must be released with a
172
 // The character string returned from this function must be released with a
173
 // call to cmMemFree()
173
 // call to cmMemFree()
174
-cmChar_t* _cmPrefsCreatePathStr( const cmChar_t* pathString, int* cnt )
174
+cmChar_t* _cmPrefsCreatePathStr( cmPr_t* p, const cmChar_t* pathString, int* cnt )
175
 {
175
 {
176
   assert( pathString != NULL );
176
   assert( pathString != NULL );
177
+  cmChar_t* pathStr;
177
 
178
 
178
   // duplicate the path string
179
   // duplicate the path string
179
-  cmChar_t* pathStr = cmMemAllocStr(pathString);
180
+  if( p->pathPrefixStr == NULL )
181
+    pathStr = cmMemAllocStr(pathString);
182
+  else
183
+    pathStr = cmTsPrintfP(NULL,"%s/%s",p->pathPrefixStr,pathString);
184
+
180
   int i = 0;
185
   int i = 0;
181
   int  n = 1;
186
   int  n = 1;
182
   for(i=0; pathStr[i]; ++i)
187
   for(i=0; pathStr[i]; ++i)
196
   int      i;
201
   int      i;
197
   unsigned retId = cmInvalidId;
202
   unsigned retId = cmInvalidId;
198
 
203
 
199
-  cmChar_t* path = _cmPrefsCreatePathStr(pathStr, &n );
204
+  cmChar_t* path = _cmPrefsCreatePathStr(p,pathStr, &n );
200
   
205
   
201
   const cmChar_t* pathArray[n];
206
   const cmChar_t* pathArray[n];
202
   const cmChar_t* cp = path;
207
   const cmChar_t* cp = path;
249
   return _cmPrefsIdToNodePtr(p,id, reportErrFl);
254
   return _cmPrefsIdToNodePtr(p,id, reportErrFl);
250
 }
255
 }
251
 
256
 
252
-cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
257
+cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr )
253
 {
258
 {
254
   cmPrRC_t rc = kOkPrRC;
259
   cmPrRC_t rc = kOkPrRC;
255
 
260
 
280
   }
285
   }
281
 
286
 
282
   // initialize the preference manager
287
   // initialize the preference manager
283
-  rc = cmPrefsInitialize(ctx,prefsH,prefsFn,cbFunc,cbDataPtr);
288
+  rc = cmPrefsInitialize(ctx,prefsH,prefsFn,cbFunc,cbDataPtr,pathPrefixStr);
284
 
289
 
285
  errLabel:
290
  errLabel:
286
   return rc;
291
   return rc;
287
 }
292
 }
288
-cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
293
+cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr )
289
 {
294
 {
290
   cmPrRC_t rc = kOkPrRC;
295
   cmPrRC_t rc = kOkPrRC;
291
 
296
 
337
   if( cbFunc != NULL )
342
   if( cbFunc != NULL )
338
     _cmPrefsInstallCallback(p,cbFunc,cbDataPtr);
343
     _cmPrefsInstallCallback(p,cbFunc,cbDataPtr);
339
 
344
 
340
-  // store the file name
341
-  p->fn = cmLHeapAllocZ( p->lhH, strlen(fn)+1 );
342
-  strcpy(p->fn,fn);
345
+  p->fn            = cmLhAllocStr( p->lhH, fn ); 
346
+  p->id            = kMaxVarPrId;
343
 
347
 
344
-  p->id = kMaxVarPrId;
348
+  if( pathPrefixStr!=NULL && strlen(pathPrefixStr)>0 )
349
+    p->pathPrefixStr = cmLhAllocStr(p->lhH, pathPrefixStr );
345
 
350
 
346
   hp->h = p;
351
   hp->h = p;
347
 
352
 
420
   return kOkPrRC;
425
   return kOkPrRC;
421
 }
426
 }
422
 
427
 
423
-      unsigned cmPrefsId( cmPrH_t h, const cmChar_t* pathStr, bool reportErrFl )
424
-    {
425
-      cmPr_t* p = _cmPrefsHandleToPtr(h);
426
-      return _cmPrefsId(p,pathStr,reportErrFl);
427
-    }
428
+unsigned cmPrefsId( cmPrH_t h, const cmChar_t* pathStr, bool reportErrFl )
429
+{
430
+  cmPr_t* p = _cmPrefsHandleToPtr(h);
431
+  return _cmPrefsId(p,pathStr,reportErrFl);
432
+}
428
 
433
 
429
 unsigned cmPrefsEleCount( cmPrH_t h, unsigned id )
434
 unsigned cmPrefsEleCount( cmPrH_t h, unsigned id )
430
 {
435
 {
1198
 {
1203
 {
1199
   cmPrRC_t        rc            = kOkPrRC;
1204
   cmPrRC_t        rc            = kOkPrRC;
1200
   int             pathCnt       = 0;
1205
   int             pathCnt       = 0;
1201
-  cmChar_t*       pathStr       = _cmPrefsCreatePathStr(pathString,&pathCnt);
1206
+  cmChar_t*       pathStr       = _cmPrefsCreatePathStr(p,pathString,&pathCnt);
1202
   const cmChar_t* pathEleStr    = pathStr; 
1207
   const cmChar_t* pathEleStr    = pathStr; 
1203
   cmJsonNode_t*   jsnp          = cmJsonRoot(p->jsH);
1208
   cmJsonNode_t*   jsnp          = cmJsonRoot(p->jsH);
1204
   cmJsonNode_t*   jsPairNodePtr = NULL;
1209
   cmJsonNode_t*   jsPairNodePtr = NULL;
1455
   return p->dirtyFl;
1460
   return p->dirtyFl;
1456
 }
1461
 }
1457
 
1462
 
1463
+
1458
 cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
1464
 cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
1459
 {
1465
 {
1460
   cmPrRC_t rc = kOkPrRC;
1466
   cmPrRC_t rc = kOkPrRC;
1463
   if( fn == NULL )
1469
   if( fn == NULL )
1464
     fn = p->fn;
1470
     fn = p->fn;
1465
 
1471
 
1466
-  if( cmJsonWrite( p->jsH, cmJsonRoot(p->jsH), fn) != kOkJsRC )
1472
+ if( cmJsonWrite( p->jsH, cmJsonRoot(p->jsH), fn) != kOkJsRC )
1467
     return cmErrMsg(&p->err,kWriteFileFailPrRC,"Preference writing failed.");
1473
     return cmErrMsg(&p->err,kWriteFileFailPrRC,"Preference writing failed.");
1468
 
1474
 
1469
   p->dirtyFl = false;
1475
   p->dirtyFl = false;
1470
 
1476
 
1477
+ 
1471
   return rc;
1478
   return rc;
1472
 }
1479
 }
1473
 
1480
 
1534
   }  
1541
   }  
1535
 }
1542
 }
1536
  */
1543
  */
1537
-  void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
1544
+  void cmPrefsTest1( cmCtx_t* ctx, const char* ifn, const char* ofn )
1538
 {
1545
 {
1539
   cmPrH_t h = cmPrNullHandle;
1546
   cmPrH_t h = cmPrNullHandle;
1540
-  if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL) != kOkPrRC )
1547
+  if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL,"pref") != kOkPrRC )
1541
     return;
1548
     return;
1542
 
1549
 
1543
   cmPr_t* p = _cmPrefsHandleToPtr(h);
1550
   cmPr_t* p = _cmPrefsHandleToPtr(h);
1597
 
1604
 
1598
   cmPrefsFinalize(&h);
1605
   cmPrefsFinalize(&h);
1599
 }
1606
 }
1607
+
1608
+void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
1609
+{
1610
+  cmPrH_t h = cmPrNullHandle;
1611
+  if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL,"pref") != kOkPrRC )
1612
+    return;
1613
+
1614
+  cmPrefsCreateBool(  h, cmInvalidIdx, "cfg/flag", 0, true );
1615
+  cmPrefsCreateString(h, cmInvalidIdx, "cfg/string",0, "blah");
1616
+  cmPrefsCreateInt(   h, cmInvalidIdx, "cfg/stuff/thing0", 0, 1 );
1617
+  cmPrefsCreateInt(   h, cmInvalidIdx, "cfg/stuff/thing1", 0, 2 );
1618
+  const char* strArray[] = { "blah", "bloo", "blug" };
1619
+  cmPrefsCreateStringArray(h, cmInvalidId, "cfg/stuff/foo", 0, strArray, 3 );
1620
+
1621
+  cmPrefsPathSetInt( h, "cfg/stuff/thing0", 10 );
1622
+  
1623
+
1624
+  if( ofn != NULL )
1625
+    cmPrefsWrite(h,ofn);
1626
+
1627
+  cmPrefsFinalize(&h);
1628
+}

+ 2
- 2
cmPrefs.h Прегледај датотеку

41
   // Set 'fnName' to NULL to use cmFsAppName() as the pref file name.
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.
42
   // Set 'fnExt' to NULL to use '.js' as the pref file extension.
43
   // 'cbFunc'  and 'cbDataPtr' are optional in both versions.
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);
44
+  cmPrRC_t cmPrefsInit(   cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr );
45
+  cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr);
46
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
46
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
47
 
47
 
48
   bool cmPrefsIsValid( cmPrH_t h );
48
   bool cmPrefsIsValid( cmPrH_t h );

Loading…
Откажи
Сачувај