libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmPrefs.c 42KB

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