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 45KB

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