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

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