Przeglądaj źródła

cmPrefs.h/c: Added prefixPathStr to prefs.

master
kevin 11 lat temu
rodzic
commit
9d76f9bf3b
2 zmienionych plików z 51 dodań i 22 usunięć
  1. 49
    20
      cmPrefs.c
  2. 2
    2
      cmPrefs.h

+ 49
- 20
cmPrefs.c Wyświetl plik

@@ -49,7 +49,7 @@ typedef struct
49 49
   cmPrNode_t* idChainPtr;       //
50 50
   unsigned    id;               // next JSON node id
51 51
   cmChar_t*   pathBuf;
52
-
52
+  cmChar_t*   pathPrefixStr;
53 53
 } cmPr_t;
54 54
 
55 55
 cmPr_t* _cmPrefsHandleToPtr( cmPrH_t h )
@@ -171,12 +171,17 @@ cmPrRC_t _cmPrefsFinalize( cmPr_t* p )
171 171
 // Convert 'pathString' to a sequence of zero terminated sub-strings.
172 172
 // The character string returned from this function must be released with a
173 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 176
   assert( pathString != NULL );
177
+  cmChar_t* pathStr;
177 178
 
178 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 185
   int i = 0;
181 186
   int  n = 1;
182 187
   for(i=0; pathStr[i]; ++i)
@@ -196,7 +201,7 @@ unsigned _cmPrefsId( cmPr_t* p, const cmChar_t* pathStr, bool reportErrFl )
196 201
   int      i;
197 202
   unsigned retId = cmInvalidId;
198 203
 
199
-  cmChar_t* path = _cmPrefsCreatePathStr(pathStr, &n );
204
+  cmChar_t* path = _cmPrefsCreatePathStr(p,pathStr, &n );
200 205
   
201 206
   const cmChar_t* pathArray[n];
202 207
   const cmChar_t* cp = path;
@@ -249,7 +254,7 @@ cmPrNode_t* _cmPrefsPathToNodePtr( cmPr_t* p, const cmChar_t* pathStr, bool repo
249 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 259
   cmPrRC_t rc = kOkPrRC;
255 260
 
@@ -280,12 +285,12 @@ cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, con
280 285
   }
281 286
 
282 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 290
  errLabel:
286 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 295
   cmPrRC_t rc = kOkPrRC;
291 296
 
@@ -337,11 +342,11 @@ cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPre
337 342
   if( cbFunc != NULL )
338 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 351
   hp->h = p;
347 352
 
@@ -420,11 +425,11 @@ cmPrRC_t cmPrefsRemoveCallback(  cmPrH_t h, cmPrefsOnChangeFunc_t cbFunc )
420 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 434
 unsigned cmPrefsEleCount( cmPrH_t h, unsigned id )
430 435
 {
@@ -1198,7 +1203,7 @@ cmPrRC_t  _cmPrefsCreateJsonNode(
1198 1203
 {
1199 1204
   cmPrRC_t        rc            = kOkPrRC;
1200 1205
   int             pathCnt       = 0;
1201
-  cmChar_t*       pathStr       = _cmPrefsCreatePathStr(pathString,&pathCnt);
1206
+  cmChar_t*       pathStr       = _cmPrefsCreatePathStr(p,pathString,&pathCnt);
1202 1207
   const cmChar_t* pathEleStr    = pathStr; 
1203 1208
   cmJsonNode_t*   jsnp          = cmJsonRoot(p->jsH);
1204 1209
   cmJsonNode_t*   jsPairNodePtr = NULL;
@@ -1455,6 +1460,7 @@ bool     cmPrefsIsDirty( cmPrH_t h )
1455 1460
   return p->dirtyFl;
1456 1461
 }
1457 1462
 
1463
+
1458 1464
 cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
1459 1465
 {
1460 1466
   cmPrRC_t rc = kOkPrRC;
@@ -1463,11 +1469,12 @@ cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
1463 1469
   if( fn == NULL )
1464 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 1473
     return cmErrMsg(&p->err,kWriteFileFailPrRC,"Preference writing failed.");
1468 1474
 
1469 1475
   p->dirtyFl = false;
1470 1476
 
1477
+ 
1471 1478
   return rc;
1472 1479
 }
1473 1480
 
@@ -1534,10 +1541,10 @@ void _cmPrintNodes( const cmPrNode_t* np )
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 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 1548
     return;
1542 1549
 
1543 1550
   cmPr_t* p = _cmPrefsHandleToPtr(h);
@@ -1597,3 +1604,25 @@ void _cmPrintNodes( const cmPrNode_t* np )
1597 1604
 
1598 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 Wyświetl plik

@@ -41,8 +41,8 @@ extern "C" {
41 41
   // Set 'fnName' to NULL to use cmFsAppName() as the pref file name.
42 42
   // Set 'fnExt' to NULL to use '.js' as the pref file extension.
43 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 46
   cmPrRC_t cmPrefsFinalize(   cmPrH_t* hp );
47 47
 
48 48
   bool cmPrefsIsValid( cmPrH_t h );

Ładowanie…
Anuluj
Zapisz