libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmPrefs.c 45KB

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