libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmPrefs.c 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmFileSys.h"
  10. #include "cmText.h"
  11. #include "cmJson.h"
  12. #include "cmPrefs.h"
  13. cmPrH_t cmPrNullHandle = cmSTATIC_NULL_HANDLE;
  14. typedef struct cmPrCb_str
  15. {
  16. cmPrefsOnChangeFunc_t cbFunc;
  17. void* cbDataPtr;
  18. struct cmPrCb_str* linkPtr;
  19. } cmPrCb_t;
  20. // Link record used by cmPrNode_t to form the
  21. // path labels for each pref. node.
  22. typedef struct cmPrPath_str
  23. {
  24. const cmChar_t* label;
  25. struct cmPrPath_str* parentPtr;
  26. } cmPrPath_t;
  27. // Every pair in the prefs JSON tree is given a unique id.
  28. // The 'path' chain forms a path of pair labels which names the variable.
  29. typedef struct cmPrNode_str
  30. {
  31. unsigned id; // unique id assigned to this pref node
  32. cmJsonNode_t* nodePtr; // pointer to the JSON value for this pref node
  33. cmPrPath_t* pathPtr; // pointer to the path label chain for this node
  34. struct cmPrNode_str* linkPtr; // link used to form the pref. node chain
  35. } cmPrNode_t;
  36. typedef struct
  37. {
  38. cmErr_t err; //
  39. cmJsonH_t jsH; // json tree
  40. cmLHeapH_t lhH; // linked heap handle
  41. cmChar_t* fn; // default filename
  42. bool dirtyFl; //
  43. cmPrCb_t* cbChainPtr; // callback record linked list
  44. cmPrNode_t* idChainPtr; //
  45. unsigned id; // next JSON node id
  46. cmChar_t* pathBuf;
  47. } cmPr_t;
  48. cmPr_t* _cmPrefsHandleToPtr( cmPrH_t h )
  49. {
  50. assert(h.h != NULL);
  51. return (cmPr_t*)h.h;
  52. }
  53. cmPrNode_t* _cmPrefsIdToNodePtr( cmPr_t* p, unsigned id, bool reportErrFl )
  54. {
  55. if( id == cmInvalidId )
  56. {
  57. cmErrMsg(&p->err,kInvalidIdPrRC,"No preference variables can use the id:%i.",id);
  58. return NULL;
  59. }
  60. cmPrNode_t* np = p->idChainPtr;
  61. for(; np!=NULL; np=np->linkPtr)
  62. if( np->id == id )
  63. return np;
  64. if( reportErrFl )
  65. cmErrMsg(&p->err,kVarNotFoundPrRC,"The preference variable associated with id:%i does not exist.",id);
  66. return NULL;
  67. }
  68. unsigned _cmPrefsCalcNextAvailId( cmPr_t* p )
  69. {
  70. while( _cmPrefsIdToNodePtr( p, p->id, false) != NULL )
  71. ++p->id;
  72. return p->id;
  73. }
  74. cmPrNode_t* _cmPrefsCreateNode( cmPr_t* p, const cmJsonNode_t* jsPairNodePtr, cmPrPath_t* parentPathPtr )
  75. {
  76. assert( jsPairNodePtr->typeId == kPairTId );
  77. cmPrNode_t* np = cmLHeapAllocZ( p->lhH, sizeof(cmPrNode_t) );
  78. np->id = cmInvalidId;
  79. np->pathPtr = cmLHeapAllocZ( p->lhH, sizeof(cmPrPath_t) );
  80. np->pathPtr->label = jsPairNodePtr->u.childPtr->u.stringVal;
  81. np->pathPtr->parentPtr = parentPathPtr;
  82. np->nodePtr = jsPairNodePtr->u.childPtr->siblingPtr;
  83. np->linkPtr = p->idChainPtr;
  84. p->idChainPtr = np;
  85. // object and pair nodes cannot be returned as pref values - so do not give them id's
  86. if( cmJsonIsObject(np->nodePtr)==false && cmJsonIsPair(np->nodePtr)==false )
  87. np->id = _cmPrefsCalcNextAvailId(p);
  88. return np;
  89. }
  90. void _cmPrefsBuildIdList( cmPr_t* p, const cmJsonNode_t* jsnPtr, cmPrPath_t* parentPathPtr )
  91. {
  92. assert( jsnPtr != NULL );
  93. if( jsnPtr->typeId == kPairTId )
  94. {
  95. cmPrNode_t* np = _cmPrefsCreateNode(p,jsnPtr,parentPathPtr);
  96. _cmPrefsBuildIdList( p, jsnPtr->u.childPtr->siblingPtr, np->pathPtr );
  97. }
  98. else
  99. if( cmJsonChildCount(jsnPtr) )
  100. {
  101. const cmJsonNode_t* np = jsnPtr->u.childPtr;
  102. for(; np != NULL; np = np->siblingPtr )
  103. _cmPrefsBuildIdList(p,np,parentPathPtr);
  104. }
  105. }
  106. void _cmPrefsInstallCallback( cmPr_t* p, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
  107. {
  108. cmPrCb_t* nrp = cmLHeapAllocZ( p->lhH, sizeof(cmPrCb_t));
  109. nrp->cbFunc = cbFunc;
  110. nrp->cbDataPtr = cbDataPtr;
  111. cmPrCb_t* cp = p->cbChainPtr;
  112. // if the cb chain is empty ...
  113. if(cp == NULL)
  114. p->cbChainPtr = nrp;// ... make the new cb the first on the chain
  115. else
  116. {
  117. // ... otherwise add the new cb to the end of the cb chain
  118. while( cp->linkPtr != NULL )
  119. cp = cp->linkPtr;
  120. cp->linkPtr = nrp;
  121. }
  122. }
  123. cmPrRC_t _cmPrefsFinalize( cmPr_t* p )
  124. {
  125. cmPrRC_t rc = kOkPrRC;
  126. if( cmLHeapIsValid(p->lhH) )
  127. cmLHeapDestroy(&p->lhH);
  128. if( cmJsonIsValid(p->jsH) )
  129. if( cmJsonFinalize(&p->jsH) != kOkJsRC )
  130. rc = cmErrMsg(&p->err,kJsonFailPrRC,"JSON finalization failed.");
  131. cmMemPtrFree(&p->pathBuf);
  132. cmMemFree(p);
  133. return rc;
  134. }
  135. // Convert 'pathString' to a sequence of zero terminated sub-strings.
  136. // The character string returned from this function must be released with a
  137. // call to cmMemFree()
  138. cmChar_t* _cmPrefsCreatePathStr( const cmChar_t* pathString, int* cnt )
  139. {
  140. assert( pathString != NULL );
  141. // duplicate the path string
  142. cmChar_t* pathStr = cmMemAllocStr(pathString);
  143. int i = 0;
  144. int n = 1;
  145. for(i=0; pathStr[i]; ++i)
  146. if( pathStr[i]=='/' )
  147. {
  148. pathStr[i] = 0;
  149. ++n;
  150. }
  151. *cnt = n;
  152. return pathStr;
  153. }
  154. unsigned _cmPrefsId( cmPr_t* p, const cmChar_t* pathStr, bool reportErrFl )
  155. {
  156. int n = 1;
  157. int i;
  158. unsigned retId = cmInvalidId;
  159. cmChar_t* path = _cmPrefsCreatePathStr(pathStr, &n );
  160. const cmChar_t* pathArray[n];
  161. const cmChar_t* cp = path;
  162. // store path parts in reverse order - because this
  163. // is the way the node sees the path
  164. for(i=n-1; i>=0; --i)
  165. {
  166. pathArray[i] = cp;
  167. cp += strlen(cp) + 1;
  168. }
  169. const cmPrNode_t* np = p->idChainPtr;
  170. // go down the id chain
  171. for(; retId==cmInvalidId && np != NULL; np=np->linkPtr)
  172. {
  173. unsigned i = 0;
  174. const cmPrPath_t* pp = np->pathPtr;
  175. // go down the path chain from leaf to root
  176. while( strcmp(pp->label,pathArray[i]) == 0 )
  177. {
  178. ++i;
  179. pp = pp->parentPtr;
  180. // if the match is complete
  181. if( i==n && pp==NULL)
  182. {
  183. retId = np->id;
  184. break;
  185. }
  186. }
  187. }
  188. if( retId==cmInvalidId && reportErrFl )
  189. cmErrMsg(&p->err,kVarNotFoundPrRC,"The preference variable '%s' was not found.",pathStr);
  190. cmMemFree(path);
  191. return retId;
  192. }
  193. cmPrNode_t* _cmPrefsPathToNodePtr( cmPr_t* p, const cmChar_t* pathStr, bool reportErrFl )
  194. {
  195. unsigned id;
  196. if((id = _cmPrefsId(p,pathStr,reportErrFl)) == cmInvalidId )
  197. return NULL;
  198. return _cmPrefsIdToNodePtr(p,id, reportErrFl);
  199. }
  200. cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* prefsH, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
  201. {
  202. cmPrRC_t rc = kOkPrRC;
  203. const cmChar_t* prefsDir = cmFsPrefsDir();
  204. const cmChar_t* prefsFn = NULL;
  205. if( fnName == NULL )
  206. fnName = cmFsAppName();
  207. if( fnExt == NULL )
  208. fnExt = ".js";
  209. // if the prefs directory does not exist then create it
  210. if( cmFsIsDir(prefsDir) == false )
  211. {
  212. if( cmFsMkDir(prefsDir) != kOkFsRC )
  213. {
  214. rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference directory '%s' could not be created.",prefsDir);
  215. goto errLabel;
  216. }
  217. }
  218. // create the preference file name
  219. if((prefsFn = cmFsMakeFn( prefsDir, fnName, fnExt, NULL )) == NULL )
  220. {
  221. rc = cmErrMsg(&ctx->err,kFileSysFailPrRC,"The preference file name could not be formed.");
  222. goto errLabel;
  223. }
  224. // initialize the preference manager
  225. rc = cmPrefsInitialize(ctx,prefsH,prefsFn,cbFunc,cbDataPtr);
  226. errLabel:
  227. return rc;
  228. }
  229. cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
  230. {
  231. cmPrRC_t rc = kOkPrRC;
  232. if((rc = cmPrefsFinalize(hp)) != kOkPrRC)
  233. return rc;
  234. cmPr_t* p = cmMemAllocZ( cmPr_t, 1 );
  235. cmErrSetup(&p->err,&ctx->rpt,"Preferences");
  236. // initialize a linked heap
  237. if( cmLHeapIsValid(p->lhH = cmLHeapCreate(1024,ctx))==false )
  238. {
  239. rc = cmErrMsg(&p->err,kLHeapFailPrRC,"Linked heap initialization failed.");
  240. goto errLabel;
  241. }
  242. // if the preference file exists
  243. if( cmFsIsFile(fn) )
  244. {
  245. // initialize the JSON tree from the preferences file
  246. if( cmJsonInitializeFromFile( &p->jsH, fn, ctx ) != kOkJsRC )
  247. {
  248. rc = cmErrMsg(&p->err,kJsonFailPrRC,"Preferences initialization failed on JSON initialization.");
  249. goto errLabel;
  250. }
  251. // build the id list
  252. _cmPrefsBuildIdList(p, cmJsonRoot(p->jsH), NULL );
  253. }
  254. else // otherwise create an empty JSON tree
  255. {
  256. if( cmJsonInitialize( &p->jsH, ctx ) != kOkJsRC )
  257. {
  258. rc = cmErrMsg(&p->err,kJsonFailPrRC,"An empty JSON tree could not be created.");
  259. goto errLabel;
  260. }
  261. if( cmJsonCreateObject( p->jsH, NULL ) == NULL )
  262. {
  263. rc = cmErrMsg(&p->err,kJsonFailPrRC,"The JSON root object could not be created.");
  264. goto errLabel;
  265. }
  266. }
  267. // install the callback function
  268. if( cbFunc != NULL )
  269. _cmPrefsInstallCallback(p,cbFunc,cbDataPtr);
  270. // store the file name
  271. p->fn = cmLHeapAllocZ( p->lhH, strlen(fn)+1 );
  272. strcpy(p->fn,fn);
  273. p->id = kMaxVarPrId;
  274. hp->h = p;
  275. errLabel:
  276. if( rc != kOkPrRC )
  277. {
  278. _cmPrefsFinalize(p);
  279. hp-> h = NULL;
  280. }
  281. return rc;
  282. }
  283. cmPrRC_t cmPrefsFinalize( cmPrH_t* hp )
  284. {
  285. cmPrRC_t rc = kOkPrRC;
  286. if( hp==NULL || hp->h == NULL )
  287. return kOkPrRC;
  288. if((rc = _cmPrefsFinalize( _cmPrefsHandleToPtr(*hp))) == kOkPrRC )
  289. return rc;
  290. hp->h = NULL;
  291. return rc;
  292. }
  293. bool cmPrefsIsValid( cmPrH_t h )
  294. { return h.h != NULL; }
  295. const cmChar_t* cmPrefsFileName( cmPrH_t h )
  296. {
  297. cmPr_t* p = _cmPrefsHandleToPtr(h);
  298. return p->fn;
  299. }
  300. cmPrRC_t cmPrefsRC( cmPrH_t h)
  301. { return cmErrLastRC(&_cmPrefsHandleToPtr(h)->err); }
  302. cmPrRC_t cmPrefsSetRC( cmPrH_t h, cmPrRC_t rc )
  303. { return cmErrSetRC(&_cmPrefsHandleToPtr(h)->err,rc); }
  304. cmPrRC_t cmPrefsInstallCallback( cmPrH_t h, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr )
  305. {
  306. cmPr_t* p = _cmPrefsHandleToPtr(h);
  307. _cmPrefsInstallCallback(p,cbFunc,cbDataPtr);
  308. return kOkPrRC;
  309. }
  310. cmPrRC_t cmPrefsRemoveCallback( cmPrH_t h, cmPrefsOnChangeFunc_t cbFunc )
  311. {
  312. cmPr_t* p = _cmPrefsHandleToPtr(h);
  313. cmPrCb_t* cc = p->cbChainPtr;
  314. cmPrCb_t* pc = NULL;
  315. while( cc == NULL )
  316. {
  317. if( cc->cbFunc == cbFunc )
  318. {
  319. if( pc == NULL )
  320. p->cbChainPtr = cc->linkPtr;
  321. else
  322. pc->linkPtr = cc->linkPtr;
  323. break;
  324. }
  325. pc = cc;
  326. cc = cc->linkPtr;
  327. }
  328. if( cc == NULL )
  329. return cmErrMsg(&p->err,kCbNotFoundPrRC,"The callback function %p could not be found.",cbFunc);
  330. return kOkPrRC;
  331. }
  332. unsigned cmPrefsId( cmPrH_t h, const cmChar_t* pathStr, bool reportErrFl )
  333. {
  334. cmPr_t* p = _cmPrefsHandleToPtr(h);
  335. return _cmPrefsId(p,pathStr,reportErrFl);
  336. }
  337. unsigned cmPrefsEleCount( cmPrH_t h, unsigned id )
  338. {
  339. cmPr_t* p = _cmPrefsHandleToPtr(h);
  340. cmPrNode_t* np = _cmPrefsIdToNodePtr(p,id,true);
  341. if( np == NULL )
  342. return -1;
  343. if( cmJsonIsArray(np->nodePtr) )
  344. return cmJsonChildCount(np->nodePtr);
  345. return 1;
  346. }
  347. void _cmPrefsFormPath( cmPr_t* p, const cmPrNode_t* np )
  348. {
  349. cmPrPath_t* pp = np->pathPtr;
  350. int n = 0;
  351. for(; pp!=NULL; pp=pp->parentPtr)
  352. ++n;
  353. const char* pathArray[n];
  354. unsigned i = n-1;
  355. for(pp = np->pathPtr; pp!=NULL; pp=pp->parentPtr,--i)
  356. pathArray[i] = pp->label;
  357. cmMemPtrFree(&p->pathBuf);
  358. for(i=0; i<n; ++i)
  359. {
  360. if( i > 0 )
  361. p->pathBuf = cmTextAppendSS(p->pathBuf,"/");
  362. p->pathBuf = cmTextAppendSS(p->pathBuf,pathArray[i]);
  363. }
  364. }
  365. cmPrRC_t _cmPrefsValue( cmPr_t* p, const cmPrNode_t* np, const cmJsonNode_t* jnp, bool* bvp, int* ivp, double* rvp, const cmChar_t** svp, unsigned retEleCnt )
  366. {
  367. cmJsRC_t jsRC = kOkJsRC;
  368. const char* typeLabel = NULL;
  369. // verify that adequate return space was provided
  370. if( retEleCnt < 1 )
  371. {
  372. _cmPrefsFormPath(p,np);
  373. return cmErrMsg(&p->err,kBufTooSmallPrRC,"No return space was provided for the preference variable '%s' (id:%i).",p->pathBuf,np->id);
  374. }
  375. if( bvp != NULL )
  376. {
  377. jsRC = cmJsonBoolValue(jnp,bvp);
  378. typeLabel = "bool";
  379. }
  380. else
  381. if( ivp != NULL )
  382. {
  383. jsRC = cmJsonIntValue(jnp,ivp);
  384. typeLabel = "int";
  385. }
  386. else
  387. if( rvp != NULL )
  388. {
  389. jsRC = cmJsonRealValue(jnp,rvp);
  390. typeLabel = "real";
  391. }
  392. else
  393. if( svp != NULL )
  394. {
  395. jsRC = cmJsonStringValue(jnp,svp);
  396. typeLabel = "string";
  397. }
  398. else
  399. {
  400. assert(0);
  401. }
  402. if( jsRC != kOkJsRC )
  403. {
  404. _cmPrefsFormPath(p,np);
  405. return cmErrMsg(&p->err,kCvtErrPrRC,"The perferences variable '%s' (id:%i) could not be converted to type '%s'.",p->pathBuf,np->id,typeLabel);
  406. }
  407. return kOkPrRC;
  408. }
  409. cmPrRC_t _cmPrefsGetValue( cmPrH_t h, unsigned id, bool* bvp, int* ivp, double* rvp, const cmChar_t** svp, unsigned* eleCntPtr, unsigned eleIdx )
  410. {
  411. cmPrRC_t rc = kOkPrRC;
  412. cmPr_t* p = _cmPrefsHandleToPtr(h);
  413. cmPrNode_t* np = NULL;
  414. // if no return buffer was given - do nothing
  415. if( *eleCntPtr == 0 )
  416. return kOkPrRC;
  417. // locate the pref node from 'id'
  418. if((np = _cmPrefsIdToNodePtr(p,id,true)) == NULL )
  419. {
  420. rc = cmErrMsg(&p->err,kVarNotFoundPrRC,"No preference variable was found for id:%i.",id);
  421. goto errLabel;
  422. }
  423. // if the requested pref. var is a scalar
  424. if( cmJsonIsArray(np->nodePtr) == false )
  425. {
  426. if((rc = _cmPrefsValue(p,np,np->nodePtr,bvp,ivp,rvp,svp,*eleCntPtr)) == kOkPrRC )
  427. *eleCntPtr = 1;
  428. }
  429. else // the request pref. var. is an array
  430. {
  431. unsigned i = 0;
  432. unsigned n = cmJsonChildCount(np->nodePtr);
  433. // if the entire array was requestedd
  434. if( eleIdx == cmInvalidIdx )
  435. {
  436. // if the return buffer is too small to hold all of the values.
  437. if( *eleCntPtr < n )
  438. {
  439. *eleCntPtr = 0;
  440. _cmPrefsFormPath(p,np);
  441. rc = cmErrMsg(&p->err,kBufTooSmallPrRC,"The return array for the preference variable '%s' (id:%i) is too small to hold '%i elements",p->pathBuf,np->id,n);
  442. goto errLabel;
  443. }
  444. // read each element
  445. for(i=0; i<n; ++i)
  446. {
  447. const cmJsonNode_t* cnp = cmJsonArrayElement(np->nodePtr,i);
  448. if((rc= _cmPrefsValue(p,np,cnp,bvp==NULL?NULL:bvp+i,ivp==NULL?NULL:ivp+i,rvp==NULL?NULL:rvp+i,svp==NULL?NULL:svp+i,1)) != kOkPrRC )
  449. goto errLabel;
  450. }
  451. *eleCntPtr = n;
  452. }
  453. else // a single ele of the array was requested
  454. {
  455. // validate the index
  456. if( eleIdx >= n )
  457. {
  458. _cmPrefsFormPath(p,np);
  459. rc = cmErrMsg(&p->err,kInvalidIndexPrRC,"The index %i is invalid for the variable '%s' of length %i.",eleIdx,p->pathBuf,n);
  460. goto errLabel;
  461. }
  462. // get the json element at array index 'eleIdx'
  463. const cmJsonNode_t* cnp = cmJsonArrayElement(np->nodePtr,eleIdx);
  464. assert(cnp != NULL );
  465. // read the element from the array
  466. if((rc = _cmPrefsValue(p,np,cnp,bvp,ivp,rvp,svp,*eleCntPtr)) == kOkPrRC )
  467. *eleCntPtr = 1;
  468. }
  469. }
  470. errLabel:
  471. if( rc != kOkPrRC )
  472. *eleCntPtr = 0;
  473. return rc;
  474. }
  475. cmPrRC_t cmPrefsGetBool( cmPrH_t h, unsigned id, bool* vp, unsigned* eleCntPtr )
  476. { return _cmPrefsGetValue(h, id, vp, NULL, NULL, NULL, eleCntPtr, cmInvalidIdx ); }
  477. cmPrRC_t cmPrefsGetInt( cmPrH_t h, unsigned id, int* vp, unsigned* eleCntPtr )
  478. { return _cmPrefsGetValue(h, id, NULL, vp, NULL, NULL, eleCntPtr, cmInvalidIdx ); }
  479. cmPrRC_t cmPrefsGetReal( cmPrH_t h, unsigned id, double* vp, unsigned* eleCntPtr )
  480. { return _cmPrefsGetValue(h, id, NULL, NULL, vp, NULL, eleCntPtr, cmInvalidIdx ); }
  481. cmPrRC_t cmPrefsGetString( cmPrH_t h, unsigned id, const cmChar_t** vp, unsigned* eleCntPtr )
  482. { return _cmPrefsGetValue(h, id, NULL, NULL, NULL, vp, eleCntPtr, cmInvalidIdx ); }
  483. bool cmPrefsBool( cmPrH_t h, unsigned id )
  484. {
  485. bool v = false;
  486. unsigned n = 1;
  487. cmPrefsGetBool(h,id,&v,&n);
  488. return v;
  489. }
  490. unsigned cmPrefsUInt( cmPrH_t h, unsigned id )
  491. {
  492. int v = 0;
  493. unsigned n = 1;
  494. cmPrefsGetInt(h,id,&v,&n);
  495. return (unsigned)v;
  496. }
  497. int cmPrefsInt( cmPrH_t h, unsigned id )
  498. {
  499. int v = 0;
  500. unsigned n = 1;
  501. cmPrefsGetInt(h,id,&v,&n);
  502. return v;
  503. }
  504. float cmPrefsFloat( cmPrH_t h, unsigned id )
  505. {
  506. double v = 0;
  507. unsigned n = 1;
  508. cmPrefsGetReal(h,id,&v,&n);
  509. return (float)v;
  510. }
  511. double cmPrefsReal( cmPrH_t h, unsigned id )
  512. {
  513. double v = 0;
  514. unsigned n = 1;
  515. cmPrefsGetReal(h,id,&v,&n);
  516. return v;
  517. }
  518. const cmChar_t* cmPrefsString( cmPrH_t h, unsigned id )
  519. {
  520. const cmChar_t* v = NULL;
  521. unsigned n = 1;
  522. if( cmPrefsGetString(h,id,&v,&n) == kOkPrRC )
  523. return v==NULL ? "" : v;
  524. return "";
  525. }
  526. bool cmPrefsBoolDef( cmPrH_t h, const cmChar_t* pathStr, bool dfltVal )
  527. {
  528. unsigned id;
  529. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  530. return dfltVal;
  531. return cmPrefsBool(h,id);
  532. }
  533. unsigned cmPrefsUIntDef( cmPrH_t h, const cmChar_t* pathStr, unsigned dfltVal )
  534. {
  535. unsigned id;
  536. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  537. return dfltVal;
  538. return cmPrefsUInt(h,id);
  539. }
  540. int cmPrefsIntDef( cmPrH_t h, const cmChar_t* pathStr, int dfltVal )
  541. {
  542. unsigned id;
  543. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  544. return dfltVal;
  545. return cmPrefsInt(h,id);
  546. }
  547. float cmPrefsFloatDef( cmPrH_t h, const cmChar_t* pathStr, float dfltVal )
  548. {
  549. unsigned id;
  550. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  551. return dfltVal;
  552. return cmPrefsFloat(h,id);
  553. }
  554. double cmPrefsRealDef( cmPrH_t h, const cmChar_t* pathStr, double dfltVal )
  555. {
  556. unsigned id;
  557. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  558. return dfltVal;
  559. return cmPrefsReal(h,id);
  560. }
  561. const cmChar_t* cmPrefsStringDef( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* dfltVal )
  562. {
  563. unsigned id;
  564. if( cmPrefsIsValid(h)==false || (id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  565. return dfltVal;
  566. const cmChar_t* v = cmPrefsString(h,id);
  567. return v==NULL || strlen(v)==0 ? dfltVal : v;
  568. }
  569. cmPrRC_t cmPrefsBoolRc( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr )
  570. {
  571. unsigned id;
  572. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  573. return kVarNotFoundPrRC;
  574. unsigned n = 1;
  575. return cmPrefsGetBool(h,id,retValPtr,&n);
  576. }
  577. cmPrRC_t cmPrefsUIntRc( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr )
  578. {
  579. unsigned id;
  580. unsigned n = 1;
  581. int v;
  582. cmPrRC_t rc;
  583. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  584. return kVarNotFoundPrRC;
  585. if((rc =cmPrefsGetInt(h,id,&v,&n)) == kOkPrRC )
  586. *retValPtr = v;
  587. return rc;
  588. }
  589. cmPrRC_t cmPrefsIntRc( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr )
  590. {
  591. unsigned id;
  592. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  593. return kVarNotFoundPrRC;
  594. unsigned n = 1;
  595. return cmPrefsGetInt(h,id,retValPtr,&n);
  596. }
  597. cmPrRC_t cmPrefsFloatRc( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr )
  598. {
  599. unsigned id;
  600. unsigned n = 1;
  601. double v;
  602. cmPrRC_t rc;
  603. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  604. return kVarNotFoundPrRC;
  605. if((rc =cmPrefsGetReal(h,id,&v,&n)) == kOkPrRC )
  606. *retValPtr = v;
  607. return rc;
  608. }
  609. cmPrRC_t cmPrefsRealRc( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr )
  610. {
  611. unsigned id;
  612. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  613. return kVarNotFoundPrRC;
  614. unsigned n = 1;
  615. return cmPrefsGetReal(h,id,retValPtr,&n);
  616. }
  617. cmPrRC_t cmPrefsStringRc( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr )
  618. {
  619. unsigned id;
  620. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  621. return kVarNotFoundPrRC;
  622. unsigned n = 1;
  623. return cmPrefsGetString(h,id,retValPtr,&n);
  624. }
  625. unsigned cmPrefsArrayElementCount( cmPrH_t h, unsigned id )
  626. {
  627. cmPrNode_t* np;
  628. cmPr_t* p = _cmPrefsHandleToPtr(h);
  629. // locate the pref node from 'id'
  630. if((np = _cmPrefsIdToNodePtr(p,id,true)) == NULL )
  631. {
  632. cmErrMsg(&p->err,kVarNotFoundPrRC,"No preference variable was found for id:%i.",id);
  633. return cmInvalidCnt;
  634. }
  635. // if the requested pref. var is a scalar
  636. if( cmJsonIsArray(np->nodePtr) )
  637. return cmJsonChildCount(np->nodePtr);
  638. return 0;
  639. }
  640. bool cmPrefsBoolEle( cmPrH_t h, unsigned id, unsigned idx )
  641. {
  642. bool v = false;;
  643. unsigned n = 1;
  644. _cmPrefsGetValue(h,id, &v, NULL, NULL, NULL, &n, idx );
  645. return v;
  646. }
  647. unsigned cmPrefsUIntEle( cmPrH_t h, unsigned id, unsigned idx )
  648. {
  649. int v = 0;
  650. unsigned n = 1;
  651. _cmPrefsGetValue(h,id, NULL, &v, NULL, NULL, &n, idx );
  652. return (unsigned)v;
  653. }
  654. int cmPrefsIntEle( cmPrH_t h, unsigned id, unsigned idx )
  655. {
  656. int v = 0;
  657. unsigned n = 1;
  658. _cmPrefsGetValue(h,id, NULL, &v, NULL, NULL, &n, idx );
  659. return v;
  660. }
  661. float cmPrefsFloatEle( cmPrH_t h, unsigned id, unsigned idx )
  662. {
  663. double v = 0;
  664. unsigned n = 1;
  665. _cmPrefsGetValue(h,id, NULL, NULL, &v, NULL, &n, idx );
  666. return (float)v;
  667. }
  668. double cmPrefsRealEle( cmPrH_t h, unsigned id, unsigned idx )
  669. {
  670. double v = 0;
  671. unsigned n = 1;
  672. _cmPrefsGetValue(h,id, NULL, NULL, &v, NULL, &n, idx );
  673. return v;
  674. }
  675. const cmChar_t* cmPrefsStringEle( cmPrH_t h, unsigned id, unsigned idx )
  676. {
  677. const cmChar_t* v = "";
  678. unsigned n = 1;
  679. if( _cmPrefsGetValue(h,id, NULL, NULL, NULL, &v, &n, idx ) == kOkPrRC )
  680. return v;
  681. return "";
  682. }
  683. cmPrRC_t _cmPrefsSetValue( cmPr_t* p, cmPrNode_t* np, cmJsonNode_t* jnp,const bool* bvp, const int* ivp, const double* rvp, const cmChar_t** svp, bool* deltaFlPtr )
  684. {
  685. cmJsRC_t jsRC = kOkJsRC;
  686. //const cmChar_t* typeLabel;
  687. if( bvp != NULL )
  688. {
  689. bool v;
  690. if((jsRC = cmJsonBoolValue(jnp,&v)) == kOkJsRC )
  691. if( (*deltaFlPtr = (v != *bvp)) )
  692. jsRC = cmJsonSetBool(p->jsH,jnp,*bvp);
  693. //typeLabel = "bool";
  694. }
  695. else
  696. if( ivp != NULL )
  697. {
  698. int v;
  699. if((jsRC = cmJsonIntValue(jnp,&v)) == kOkJsRC )
  700. if( (*deltaFlPtr = (v != *ivp)) )
  701. jsRC = cmJsonSetInt(p->jsH,jnp,*ivp);
  702. //typeLabel = "int";
  703. }
  704. else
  705. if( rvp != NULL )
  706. {
  707. double v;
  708. if((jsRC = cmJsonRealValue(jnp,&v)) == kOkJsRC )
  709. if( (*deltaFlPtr = (v != *rvp)) )
  710. jsRC = cmJsonSetReal(p->jsH,jnp,*rvp);
  711. //typeLabel = "real";
  712. }
  713. else
  714. if( svp != NULL )
  715. {
  716. const cmChar_t* v;
  717. if((jsRC = cmJsonStringValue(jnp,&v)) == kOkJsRC )
  718. {
  719. const cmChar_t* s0 = v ==NULL ? "" : v;
  720. const cmChar_t* s1 = *svp==NULL ? "" : *svp;
  721. if( (*deltaFlPtr = (strcmp(s0,s1)!=0)) )
  722. jsRC = cmJsonSetString(p->jsH,jnp,s1);
  723. }
  724. //typeLabel = "string";
  725. }
  726. else
  727. {
  728. assert(0);
  729. }
  730. if( jsRC != kOkJsRC )
  731. {
  732. _cmPrefsFormPath(p,np);
  733. return cmErrMsg(&p->err,kCvtErrPrRC,"The perferences variable '%s' (id:%i) could not be set.",p->pathBuf,np->id);
  734. }
  735. return kOkPrRC;
  736. }
  737. void _cmPrefsCallback( cmPr_t* p, unsigned id )
  738. {
  739. // notify any listeners that a node has changed values
  740. cmPrCb_t* cbNodePtr = p->cbChainPtr;
  741. for(; cbNodePtr!=NULL; cbNodePtr=cbNodePtr->linkPtr)
  742. {
  743. cmPrH_t h;
  744. h.h = p;
  745. cbNodePtr->cbFunc(h,cbNodePtr->cbDataPtr,id);
  746. }
  747. }
  748. cmPrRC_t _cmPrefsSetValues2( cmPr_t* p, cmPrNode_t* np, const bool* bvp, const int* ivp, const double* rvp, const cmChar_t** svp, const unsigned* eleCntPtr )
  749. {
  750. cmPrRC_t rc = kOkPrRC;
  751. bool deltaFl = false; // set to true if the value of the pref. var. identified by 'id' actually changes.
  752. // a scalar has been passed in and the existing json node is also a scalar
  753. if( cmJsonIsArray(np->nodePtr)== false && *eleCntPtr==1 )
  754. {
  755. rc = _cmPrefsSetValue(p,np,np->nodePtr,bvp,ivp,rvp,svp,&deltaFl);
  756. goto errLabel;
  757. }
  758. // the pref. variable is an array
  759. if( cmJsonIsArray(np->nodePtr) )
  760. {
  761. unsigned i;
  762. unsigned curArrCnt = cmJsonChildCount(np->nodePtr);
  763. unsigned n = cmMin(*eleCntPtr,curArrCnt);
  764. for(i=0; i<n; ++i)
  765. {
  766. cmJsonNode_t* jnp = cmJsonArrayElement(np->nodePtr,i);
  767. bool dfl = false;
  768. if((rc = _cmPrefsSetValue(p,np,jnp,bvp==NULL?NULL:bvp+i,ivp==NULL?NULL:ivp+i,rvp==NULL?NULL:rvp+i,svp==NULL?NULL:svp+i,&dfl)) != kOkPrRC )
  769. return rc;
  770. if( dfl )
  771. deltaFl = true;
  772. }
  773. // if elements should be removed
  774. if( curArrCnt > *eleCntPtr )
  775. {
  776. deltaFl = true; // ele's are being removed so the variable value is changing
  777. for(i=*eleCntPtr; i<curArrCnt; ++i)
  778. {
  779. cmJsonNode_t* jnp = cmJsonArrayElement(np->nodePtr,*eleCntPtr);
  780. assert(jnp!=NULL);
  781. cmJsonRemoveNode(p->jsH,jnp,true);
  782. }
  783. }
  784. // if elements should be added
  785. if( *eleCntPtr > curArrCnt )
  786. {
  787. unsigned typeId = cmInvalidId;
  788. cmJsonNode_t* jnp = NULL;
  789. cmJsonNode_t* snp = curArrCnt==0 ? NULL : cmJsonArrayElement(np->nodePtr,curArrCnt-1);
  790. assert(snp->ownerPtr==np->nodePtr && snp->siblingPtr==NULL);
  791. deltaFl = true; // ele's are being added so the variable value is changing
  792. if( ivp != NULL )
  793. typeId = kIntTId;
  794. else
  795. if( rvp != NULL )
  796. typeId = kRealTId;
  797. else
  798. if( svp != NULL )
  799. typeId = kStringTId;
  800. // for each new array element
  801. for(; i<*eleCntPtr; ++i)
  802. {
  803. if( bvp != NULL )
  804. typeId = bvp[i];
  805. // create the new array element node - the new node is automatically added to the end of the array
  806. if( cmJsonCreate(p->jsH, np->nodePtr, typeId, svp==NULL?NULL:svp[i], ivp==NULL?0:ivp[i], rvp==NULL?0:rvp[i], &jnp ) != kOkJsRC )
  807. {
  808. _cmPrefsFormPath(p,np);
  809. return cmErrMsg(&p->err,kJsonFailPrRC,"Unable to create a value node for '%s' (id=%i).",p->pathBuf,np->id);
  810. }
  811. }
  812. }
  813. errLabel:
  814. if( (rc == kOkPrRC) && deltaFl )
  815. {
  816. _cmPrefsCallback(p, np->id );
  817. // update the global delta flag
  818. p->dirtyFl = true;
  819. }
  820. return rc;
  821. }
  822. _cmPrefsFormPath(p,np);
  823. return cmErrMsg(&p->err,kCvtErrPrRC,"The new preference value could not be converted to the existing preference variable type for '%s' (id=%i).",p->pathBuf,np->id);
  824. }
  825. cmPrRC_t _cmPrefsSetValues( cmPrH_t h, unsigned id, const bool* bvp, const int* ivp, const double* rvp, const cmChar_t** svp, const unsigned* eleCntPtr )
  826. {
  827. cmPr_t* p = _cmPrefsHandleToPtr(h);
  828. cmPrNode_t* np;
  829. // locate the pref node
  830. if((np = _cmPrefsIdToNodePtr(p,id,true)) == NULL )
  831. return cmErrMsg(&p->err,kVarNotFoundPrRC,"The variable with id=%i was not found.",id);
  832. return _cmPrefsSetValues2( p, np, bvp, ivp, rvp, svp, eleCntPtr );
  833. }
  834. cmPrRC_t cmPrefsSetBoolArray( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr )
  835. { return _cmPrefsSetValues(h, id, vp, NULL, NULL, NULL, eleCntPtr ); }
  836. cmPrRC_t cmPrefsSetIntArray( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr )
  837. { return _cmPrefsSetValues(h, id, NULL, vp, NULL, NULL, eleCntPtr ); }
  838. cmPrRC_t cmPrefsSetRealArray( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr )
  839. { return _cmPrefsSetValues(h, id, NULL, NULL, vp, NULL, eleCntPtr ); }
  840. cmPrRC_t cmPrefsSetStringArray( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr )
  841. { return _cmPrefsSetValues(h, id, NULL, NULL, NULL, vp, eleCntPtr ); }
  842. cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, bool val )
  843. {
  844. unsigned n = 1;
  845. return cmPrefsSetBoolArray(h,id,&val,&n);
  846. }
  847. cmPrRC_t cmPrefsSetUInt( cmPrH_t h, unsigned id, unsigned val )
  848. {
  849. unsigned n = 1;
  850. int ival = (int)val;
  851. return cmPrefsSetIntArray(h,id,&ival,&n);
  852. }
  853. cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, int val )
  854. {
  855. unsigned n = 1;
  856. return cmPrefsSetIntArray(h,id,&val,&n);
  857. }
  858. cmPrRC_t cmPrefsSetFloat( cmPrH_t h, unsigned id, float val )
  859. {
  860. unsigned n = 1;
  861. double dval = val;
  862. return cmPrefsSetRealArray(h,id,&dval,&n);
  863. }
  864. cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, double val )
  865. {
  866. unsigned n = 1;
  867. return cmPrefsSetRealArray(h,id,&val,&n);
  868. }
  869. cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t* val )
  870. {
  871. unsigned n = 1;
  872. return cmPrefsSetStringArray(h,id,&val,&n);
  873. }
  874. cmPrRC_t cmPrefsPathSetBool( cmPrH_t h, const cmChar_t* pathStr, bool val )
  875. {
  876. unsigned id;
  877. unsigned n = 1;
  878. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  879. return kVarNotFoundPrRC;
  880. return cmPrefsSetBoolArray(h,id,&val,&n);
  881. }
  882. cmPrRC_t cmPrefsPathSetUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val )
  883. {
  884. unsigned id;
  885. unsigned n = 1;
  886. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  887. return kVarNotFoundPrRC;
  888. int ival = (int)val;
  889. return cmPrefsSetIntArray(h,id,&ival,&n);
  890. }
  891. cmPrRC_t cmPrefsPathSetInt( cmPrH_t h, const cmChar_t* pathStr, int val )
  892. {
  893. unsigned id;
  894. unsigned n = 1;
  895. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  896. return kVarNotFoundPrRC;
  897. return cmPrefsSetIntArray(h,id,&val,&n);
  898. }
  899. cmPrRC_t cmPrefsPathSetFloat( cmPrH_t h, const cmChar_t* pathStr, float val )
  900. {
  901. unsigned id;
  902. unsigned n = 1;
  903. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  904. return kVarNotFoundPrRC;
  905. double dval = val;
  906. return cmPrefsSetRealArray(h,id,&dval,&n);
  907. }
  908. cmPrRC_t cmPrefsPathSetReal( cmPrH_t h, const cmChar_t* pathStr, double val )
  909. {
  910. unsigned id;
  911. unsigned n = 1;
  912. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  913. return kVarNotFoundPrRC;
  914. return cmPrefsSetRealArray(h,id,&val,&n);
  915. }
  916. cmPrRC_t cmPrefsPathSetString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val )
  917. {
  918. unsigned id;
  919. unsigned n = 1;
  920. if((id = cmPrefsId(h,pathStr,true)) == cmInvalidId )
  921. return kVarNotFoundPrRC;
  922. return cmPrefsSetStringArray(h,id,&val,&n);
  923. }
  924. bool _cmPrefsValidateNodeType( cmJsonNode_t* np, unsigned jsTypeId, unsigned valCnt )
  925. {
  926. // if we are looking for a scalar node then the type must be jsTypeId
  927. if( valCnt == 1 )
  928. return np->typeId == jsTypeId;
  929. // if we are looking for an array node then the type must be kArrayTId ...
  930. if( np->typeId != kArrayTId )
  931. return false;
  932. unsigned n = cmJsonChildCount(np);
  933. unsigned i = 0;
  934. // ... and all of the array elements must be of type jsTypeId
  935. for(; i<n; ++i)
  936. {
  937. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
  938. assert( cnp != NULL );
  939. if( cnp->typeId != jsTypeId )
  940. return false;
  941. }
  942. return true;
  943. }
  944. //
  945. cmPrRC_t _cmPrefsCreateJsonNode(
  946. cmPr_t* p,
  947. unsigned id, // new variable id
  948. const cmChar_t* pathString, // new variable name
  949. unsigned jsTypeId, // type of new variable
  950. const cmChar_t** strVal, // value of new variable
  951. const double* realVal, //
  952. const int* intVal, //
  953. const bool* boolVal, //
  954. unsigned valCnt, // length of xxxVal[]
  955. bool setFlag ) // true== if same named variable already exists then set the value to xxxVal
  956. {
  957. cmPrRC_t rc = kOkPrRC;
  958. int pathCnt = 0;
  959. cmChar_t* pathStr = _cmPrefsCreatePathStr(pathString,&pathCnt);
  960. const cmChar_t* pathEleStr = pathStr;
  961. cmJsonNode_t* jsnp = cmJsonRoot(p->jsH);
  962. cmJsonNode_t* jsPairNodePtr = NULL;
  963. cmPrNode_t* pnp = NULL; // current pref node pointer
  964. bool existsFl = false; // set if variable already exists
  965. int i;
  966. assert( pathCnt >= 1 );
  967. if( id != cmInvalidId && id >= kMaxVarPrId )
  968. return cmErrMsg(&p->err,kNodeCreateFailPrRC,"User supplied id's must be less than 0x%x.",kMaxVarPrId);
  969. // for each path element
  970. for(i=0; i<pathCnt; ++i)
  971. {
  972. cmJsonNode_t* tnp = NULL;
  973. // find the child pair node of jsnp named 'pathEleStr'.
  974. if( (tnp = cmJsonNodeMemberValue(jsnp, pathEleStr )) == NULL )
  975. break;
  976. // the located json node must be an object (i<pathCnt-1) or the same type as jsTypeId (i=pathCnt-1)
  977. // This check guarantees that all nodes up to the leaf node are objects and the leaf node matches the created type.
  978. if( !(((i<pathCnt-1) && cmJsonIsObject(tnp)) || ((i==pathCnt-1) && _cmPrefsValidateNodeType(tnp,jsTypeId,valCnt))) )
  979. {
  980. rc = cmErrMsg(&p->err,kNodeCreateFailPrRC,"Node creation for '%s' failed because an existing node with the same name but different type (%i) was found.",pathString,tnp->typeId);
  981. goto errLabel;
  982. }
  983. // assert that the json nodes we are finding form a tree
  984. assert( (tnp->ownerPtr==NULL && jsnp==NULL) || (tnp->ownerPtr!=NULL && tnp->ownerPtr->ownerPtr==jsnp));
  985. jsnp = tnp;
  986. // find the pref node which refers to the located json node
  987. // (we need to track the pnp->pathPtr)
  988. pnp = p->idChainPtr;
  989. for(; pnp != NULL; pnp=pnp->linkPtr)
  990. if( pnp->nodePtr == jsnp )
  991. break;
  992. assert(pnp->pathPtr != NULL);
  993. // advance to the next path segment
  994. pathEleStr += strlen(pathEleStr) + 1;
  995. }
  996. // if i<pathCnt then json nodes must be created to
  997. // if i==pathCnt then a matching variable already exists
  998. if( i==pathCnt )
  999. {
  1000. // the reference variable already exists and assignment was requested (setFlag==true).
  1001. // but the assignment cannot be made until the 'id' is updated.
  1002. existsFl = true;
  1003. }
  1004. // we have followed 'pathString' to the last node which already
  1005. // exists in the JSON tree - now we must create new JSON nodes to reflect
  1006. // the remaining path elements
  1007. for(; i<pathCnt; ++i)
  1008. {
  1009. // if this is the last element in the path - then it is a pair
  1010. if( i==pathCnt-1 )
  1011. {
  1012. // create a scalar value pair node
  1013. if( valCnt == 1 )
  1014. {
  1015. const char* sv = strVal==NULL ? NULL : *strVal;
  1016. double rv = realVal==NULL ? 0.0 : *realVal;
  1017. int iv = intVal ==NULL ? 0 : *intVal;
  1018. if( boolVal != NULL )
  1019. jsTypeId = *boolVal ? kTrueTId : kFalseTId;
  1020. if((jsnp = cmJsonInsertPair( p->jsH, jsnp, pathEleStr,jsTypeId, sv, iv, rv )) == NULL )
  1021. {
  1022. rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node create failed for '%s'",cmStringNullGuard(pathString));
  1023. goto errLabel;
  1024. }
  1025. }
  1026. else // create a vector value pair node
  1027. {
  1028. switch( jsTypeId )
  1029. {
  1030. case kStringTId: jsnp = cmJsonInsertPairStringArray2(p->jsH, jsnp, pathEleStr, valCnt, strVal); break;
  1031. case kRealTId: jsnp = cmJsonInsertPairRealArray2( p->jsH, jsnp, pathEleStr, valCnt, realVal);break;
  1032. case kIntTId: jsnp = cmJsonInsertPairIntArray2( p->jsH, jsnp, pathEleStr, valCnt, intVal); break;
  1033. case kBoolTId: jsnp = cmJsonInsertPairBoolArray2( p->jsH, jsnp, pathEleStr, valCnt, boolVal ); break;
  1034. default:
  1035. {
  1036. rc = cmErrMsg(&p->err,kInvalidIdPrRC,"Preference node insert failed on '%s' due to invalid type id '%i'.",cmStringNullGuard(pathString),jsTypeId);
  1037. goto errLabel;
  1038. }
  1039. }
  1040. }
  1041. assert( jsnp != NULL );
  1042. jsPairNodePtr = jsnp;
  1043. }
  1044. else // this is not the last element in the path - create an object node
  1045. {
  1046. // create the object associated with this new level
  1047. if((jsnp = cmJsonInsertPairObject( p->jsH, jsnp, pathEleStr )) == NULL )
  1048. {
  1049. rc = cmErrMsg(&p->err,kJsonFailPrRC,"Preference node create failed because JSON node create failed on '%s'.",cmStringNullGuard(pathString));
  1050. goto errLabel;
  1051. }
  1052. jsPairNodePtr = jsnp->ownerPtr;
  1053. }
  1054. unsigned nxtId = p->id;
  1055. // create a pref node to associate with this new level
  1056. if((pnp = _cmPrefsCreateNode(p, jsPairNodePtr, pnp==NULL ? NULL : pnp->pathPtr )) == NULL )
  1057. {
  1058. rc = cmErrMsg(&p->err,kNodeCreateFailPrRC,"Creation failed for the '%s' element of the preference node '%s'.",cmStringNullGuard(pathEleStr),cmStringNullGuard(pathString));
  1059. goto errLabel;
  1060. }
  1061. // always leave internal nodes with id=cmInvalidId, leaf node id's will be set below
  1062. p->id = nxtId;
  1063. pnp->id = cmInvalidId;
  1064. pathEleStr += strlen(pathEleStr) + 1;
  1065. }
  1066. assert( pnp != NULL );
  1067. // if an preference variable 'id' was given then set it here
  1068. if( id == cmInvalidId )
  1069. pnp->id = _cmPrefsCalcNextAvailId(p);
  1070. else
  1071. {
  1072. if( existsFl == false )
  1073. if( _cmPrefsIdToNodePtr(p, id, false ) != NULL )
  1074. cmErrWarnMsg(&p->err,kDuplicateIdPrRC,"The preference variable id '%i' is used by multiple preference variables including '%s'.",id,cmStringNullGuard(pathString));
  1075. pnp->id = id;
  1076. }
  1077. // if the variable already existed and setFlag==true then update the variable value here
  1078. // (note that this cannot occur earlier because the 'id' has not yet been updated
  1079. // which might result in the incorrect id being set with the callback function)
  1080. if( existsFl && setFlag )
  1081. if((rc = _cmPrefsSetValues2(p, pnp, boolVal, intVal, realVal, strVal, &valCnt )) != kOkPrRC )
  1082. goto errLabel;
  1083. // if a new variable was created then notify the application of the new value
  1084. if(existsFl==false)
  1085. {
  1086. assert(pnp!=NULL);
  1087. _cmPrefsCallback(p,pnp->id);
  1088. }
  1089. errLabel:
  1090. cmMemFree(pathStr);
  1091. return rc;
  1092. }
  1093. cmPrRC_t cmPrefsCreateBool( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, bool val )
  1094. {
  1095. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1096. bool v = val;
  1097. return _cmPrefsCreateJsonNode(p, id, pathStr, kBoolTId, NULL, NULL, NULL, &v, 1, cmIsFlag(flags, kForceValuePrFl) );
  1098. }
  1099. cmPrRC_t cmPrefsCreateUInt( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, unsigned val )
  1100. {
  1101. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1102. int v = val;
  1103. return _cmPrefsCreateJsonNode(p, id, pathStr, kIntTId, NULL, NULL, &v, NULL, 1, cmIsFlag(flags, kForceValuePrFl) );
  1104. }
  1105. cmPrRC_t cmPrefsCreateInt( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, int val )
  1106. {
  1107. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1108. int v = val;
  1109. return _cmPrefsCreateJsonNode(p, id, pathStr, kIntTId, NULL, NULL, &v, NULL, 1, cmIsFlag(flags, kForceValuePrFl) );
  1110. }
  1111. cmPrRC_t cmPrefsCreateFloat( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, float val )
  1112. {
  1113. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1114. double v = val;
  1115. return _cmPrefsCreateJsonNode(p, id, pathStr, kRealTId, NULL, &v, NULL, NULL, 1, cmIsFlag(flags, kForceValuePrFl) );
  1116. }
  1117. cmPrRC_t cmPrefsCreateReal( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, double val )
  1118. {
  1119. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1120. double v = val;
  1121. return _cmPrefsCreateJsonNode(p, id, pathStr, kRealTId, NULL, &v,NULL, NULL, 1, cmIsFlag(flags, kForceValuePrFl) );
  1122. }
  1123. cmPrRC_t cmPrefsCreateString( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const cmChar_t* val )
  1124. {
  1125. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1126. const cmChar_t* v = val;
  1127. return _cmPrefsCreateJsonNode(p, id, pathStr, kStringTId, &v, NULL, NULL, NULL, 1, cmIsFlag(flags,kForceValuePrFl) );
  1128. }
  1129. cmPrRC_t cmPrefsCreateBoolArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const bool* val, unsigned eleCnt )
  1130. {
  1131. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1132. return _cmPrefsCreateJsonNode(p, id, pathStr, kBoolTId, NULL, NULL, NULL, val, eleCnt, cmIsFlag(flags, kForceValuePrFl) );
  1133. }
  1134. cmPrRC_t cmPrefsCreateUIntArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const unsigned* val, unsigned eleCnt )
  1135. {
  1136. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1137. return _cmPrefsCreateJsonNode(p, id, pathStr, kIntTId, NULL, NULL, (int*)val, NULL, eleCnt, cmIsFlag(flags, kForceValuePrFl) );
  1138. }
  1139. cmPrRC_t cmPrefsCreateIntArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const int* val, unsigned eleCnt )
  1140. {
  1141. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1142. return _cmPrefsCreateJsonNode(p, id, pathStr, kIntTId, NULL, NULL, val, NULL, eleCnt, cmIsFlag(flags, kForceValuePrFl) );
  1143. }
  1144. cmPrRC_t cmPrefsCreateFloatArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const float* val, unsigned eleCnt )
  1145. {
  1146. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1147. double* a = cmMemAlloc(double,eleCnt);
  1148. unsigned i;
  1149. for(i=0; i<eleCnt; ++i)
  1150. a[i] = val[i];
  1151. cmPrRC_t rc = _cmPrefsCreateJsonNode(p, id, pathStr, kRealTId, NULL, a, NULL, NULL, eleCnt, cmIsFlag(flags, kForceValuePrFl) );
  1152. cmMemFree(a);
  1153. return rc;
  1154. }
  1155. cmPrRC_t cmPrefsCreateRealArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const double* val, unsigned eleCnt )
  1156. {
  1157. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1158. return _cmPrefsCreateJsonNode(p, id, pathStr, kRealTId, NULL, val, NULL, NULL, eleCnt, cmIsFlag(flags, kForceValuePrFl) );
  1159. }
  1160. cmPrRC_t cmPrefsCreateStringArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const cmChar_t** val, unsigned eleCnt )
  1161. {
  1162. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1163. return _cmPrefsCreateJsonNode(p, id, pathStr, kStringTId, val, NULL, NULL, NULL, eleCnt, cmIsFlag(flags,kForceValuePrFl) );
  1164. }
  1165. bool cmPrefsIsDirty( cmPrH_t h )
  1166. {
  1167. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1168. return p->dirtyFl;
  1169. }
  1170. cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn )
  1171. {
  1172. cmPrRC_t rc = kOkPrRC;
  1173. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1174. if( fn == NULL )
  1175. fn = p->fn;
  1176. if( cmJsonWrite( p->jsH, cmJsonRoot(p->jsH), fn) != kOkJsRC )
  1177. return cmErrMsg(&p->err,kWriteFileFailPrRC,"Preference writing failed.");
  1178. p->dirtyFl = false;
  1179. return rc;
  1180. }
  1181. void cmPrefsReport( cmPrH_t h )
  1182. {
  1183. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1184. cmJsonReport(p->jsH);
  1185. }
  1186. //=============================================================================================
  1187. // cmPrefsTest()
  1188. //
  1189. void _cmPrintNode( const cmPrNode_t* np )
  1190. {
  1191. const cmPrPath_t* pp = np->pathPtr;
  1192. const cmJsonNode_t* jnp = np->nodePtr;
  1193. const cmJsonNode_t* jpp = jnp->ownerPtr;
  1194. printf("id:%i type:0x%x parent:0x%x ",np->id,jnp->typeId,jpp==NULL?0:jpp->typeId);
  1195. while( pp != NULL )
  1196. {
  1197. printf("%s ",pp->label);
  1198. pp = pp->parentPtr;
  1199. }
  1200. printf("\n");
  1201. }
  1202. void _cmPrintNodes( const cmPrNode_t* np )
  1203. {
  1204. if( np == NULL )
  1205. return;
  1206. _cmPrintNode(np);
  1207. _cmPrintNodes(np->linkPtr);
  1208. }
  1209. /* example preferences file
  1210. {
  1211. "cfg" :
  1212. {
  1213. "inAudDevIdx" : 0
  1214. "outAudDevIdx" : 0
  1215. "syncInputFl" : false
  1216. "midiPortBufByteCnt" : 8192
  1217. "msgQueueByteCnt" : 64768
  1218. "devFramesPerCycle" : 128
  1219. "dspFramesPerCycle" : 64
  1220. "audioBufCnt" : 3
  1221. "srate" : 44100.000000
  1222. "chNames" :
  1223. {
  1224. "left" : 0
  1225. "right" : 1
  1226. "array" :
  1227. [
  1228. 0 ,
  1229. 1 ,
  1230. 2
  1231. ]
  1232. }
  1233. }
  1234. }
  1235. */
  1236. void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn )
  1237. {
  1238. cmPrH_t h = cmPrNullHandle;
  1239. if( cmPrefsInitialize(ctx,&h,ifn,NULL,NULL) != kOkPrRC )
  1240. return;
  1241. cmPr_t* p = _cmPrefsHandleToPtr(h);
  1242. _cmPrintNodes(p->idChainPtr);
  1243. unsigned id;
  1244. id = cmPrefsId(h, "cfg/audioBufCnt", true );
  1245. printf("id:%i\n",id);
  1246. //id = cmPrefsId(h, "cfg/audioBuf", true );
  1247. //printf("id:%i\n",id);
  1248. int sr;
  1249. unsigned n=1;
  1250. cmPrefsGetInt(h, cmPrefsId(h,"cfg/srate",true), &sr, &n );
  1251. printf("sr:%i %i\n",sr,n);
  1252. cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), &sr, &n );
  1253. printf("sr:%i %i\n",sr,n);
  1254. int arr[4];
  1255. n = 4;
  1256. cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), arr, &n );
  1257. printf("array:%i %i %i n=%i\n",arr[0],arr[1],arr[2],n);
  1258. sr = 44100;
  1259. n = 1;
  1260. cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/srate",true), &sr, &n);
  1261. cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), &sr, &n );
  1262. printf("sr:%i %i\n",sr,n);
  1263. int sarr[] = {10,11,12,13 };
  1264. n = sizeof(sarr)/sizeof(sarr[0]);
  1265. cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/chNames/array",true), sarr, &n);
  1266. cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), sarr, &n );
  1267. printf("array:%i %i %i %i n=%i\n",sarr[0],sarr[1],sarr[2],sarr[3],n);
  1268. int tarr[] = {20,21 };
  1269. n = sizeof(tarr)/sizeof(tarr[0]);
  1270. cmPrefsSetIntArray(h, cmPrefsId(h,"cfg/chNames/array",true), tarr, &n);
  1271. cmPrefsGetInt(h, cmPrefsId(h,"cfg/chNames/array",true), tarr, &n );
  1272. printf("array:%i %i n=%i\n",tarr[0],tarr[1],n);
  1273. cmPrefsCreateBool(h, cmInvalidId, "cfg/flags/flag0", 0, true );
  1274. bool bv = cmPrefsBool(h, cmPrefsId(h,"cfg/flags/flag0",true));
  1275. printf("bool:%i\n",bv);
  1276. cmPrefsCreateString(h,cmInvalidId, "cfg/audioBufCnt",0,"hey");
  1277. const cmChar_t* strArray[] =
  1278. {
  1279. "snap", "crackle", "pop"
  1280. };
  1281. cmPrefsCreateStringArray(h, cmInvalidId, "cfg/chNames/strList",0,strArray,3);
  1282. cmPrefsWrite(h,ofn);
  1283. cmPrefsFinalize(&h);
  1284. }