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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmJson.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLex.h"
  11. #include "cmLinkedHeap.h"
  12. #include "cmFile.h"
  13. enum
  14. {
  15. kLCurlyLexTId = kUserLexTId+1,
  16. kRCurlyLexTId,
  17. kLHardLexTId,
  18. kRHardLexTId,
  19. kColonLexTId,
  20. kCommaLexTId,
  21. kTrueLexTId,
  22. kFalseLexTId,
  23. kNullLexTId
  24. };
  25. typedef struct
  26. {
  27. unsigned id;
  28. const char* text;
  29. } cmJsToken_t;
  30. // serialization buffer header
  31. typedef struct
  32. {
  33. unsigned id; // always set to 'json'
  34. unsigned byteCnt; // count of bytes following this field (total buf bytes = byteCnt + (2*sizeof(unsigned)))
  35. unsigned nodeCnt; // count of nodes in this buffer
  36. unsigned version; // buffer serialization version number
  37. } cmJsSerialHdr_t;
  38. // serialization helper record
  39. typedef struct
  40. {
  41. cmJsSerialHdr_t hdr;
  42. char* basePtr;
  43. char* nextPtr;
  44. char* endPtr;
  45. } cmJsSerial_t;
  46. // deserialization helper record
  47. typedef struct
  48. {
  49. unsigned nodeCnt;
  50. unsigned nodeIdx;
  51. const char* nextPtr;
  52. const char* endPtr;
  53. } cmJsDeserial_t;
  54. typedef struct
  55. {
  56. cmErr_t err; //
  57. cmLexH lexH; // parsing lexer
  58. cmLHeapH_t heapH; // linked heap stores all node memory
  59. cmJsonNode_t* rootPtr; // root of internal node tree
  60. cmJsonNode_t* basePtr; // base of parsing stack
  61. cmJsonNode_t* lastPtr; // top of parsing stack
  62. cmJsRC_t rc; // last error code
  63. char* serialBufPtr; // serial buffer pointer
  64. unsigned serialByteCnt; // count of bytes in serialBuf[]
  65. bool reportErrPosnFl;// report the file posn of syntax errors
  66. bool modifiedFl; // the tree has been modified since it was created.
  67. } cmJs_t;
  68. cmJsToken_t _cmJsTokenArray[] =
  69. {
  70. { kLCurlyLexTId, "{" },
  71. { kRCurlyLexTId, "}" },
  72. { kLHardLexTId, "[" },
  73. { kRHardLexTId, "]" },
  74. { kColonLexTId, ":" },
  75. { kCommaLexTId, "," },
  76. { kTrueLexTId, "true"},
  77. { kFalseLexTId, "false"},
  78. { kNullLexTId, "null" },
  79. { kErrorLexTId,""}
  80. };
  81. cmJsToken_t _cmJsNodeTypeLabel[] =
  82. {
  83. { kObjectTId, "object" },
  84. { kPairTId, "pair" },
  85. { kArrayTId, "array" },
  86. { kStringTId, "string" },
  87. { kIntTId, "int" },
  88. { kRealTId, "real" },
  89. { kNullTId, "null" },
  90. { kTrueTId, "true" },
  91. { kFalseTId, "false" },
  92. { kInvalidTId,"invalid"}
  93. };
  94. cmJsonH_t cmJsonNullHandle = cmSTATIC_NULL_HANDLE;
  95. cmJsRC_t _cmJsonRemoveNode( cmJs_t* p, cmJsonNode_t* np, bool freeFl, bool balancePairsFl );
  96. void _cmJsonFreeNode( cmJs_t* p, cmJsonNode_t* np);
  97. const char* _cmJsonNodeTypeIdToLabel( unsigned nodeTypeId )
  98. {
  99. unsigned i;
  100. for(i=0; _cmJsNodeTypeLabel[i].id != kInvalidTId; ++i)
  101. if( _cmJsNodeTypeLabel[i].id == nodeTypeId )
  102. break;
  103. return _cmJsNodeTypeLabel[i].text;
  104. }
  105. unsigned _cmJsonNodeTypeLabelToId( const char* typeLabel )
  106. {
  107. unsigned i;
  108. for(i=0; _cmJsNodeTypeLabel[i].id != kInvalidTId; ++i)
  109. if( strcmp(_cmJsNodeTypeLabel[i].text,typeLabel) == 0 )
  110. break;
  111. return _cmJsNodeTypeLabel[i].id;
  112. }
  113. cmJs_t* _cmJsonHandleToPtr( cmJsonH_t h )
  114. {
  115. cmJs_t* p = (cmJs_t*)h.h;
  116. assert(p != NULL);
  117. p->rc = kOkJsRC;
  118. return p;
  119. }
  120. cmJsRC_t _cmJsonError( cmJs_t* p, cmJsRC_t rc, const char* fmt, ... )
  121. {
  122. va_list vl;
  123. va_start(vl,fmt);
  124. rc = cmErrVMsg(&p->err,rc,fmt,vl);
  125. va_end(vl);
  126. return rc;
  127. }
  128. cmJsRC_t _cmJsonSyntaxError( cmJs_t* p, const char* fmt, ... )
  129. {
  130. int bn = 1024;
  131. char buf[bn+1];
  132. buf[0]=0;
  133. buf[bn]=0;
  134. va_list vl;
  135. va_start(vl,fmt);
  136. if( p->reportErrPosnFl )
  137. snprintf(buf,bn,"Syntax error on line:%i column:%i. ",cmLexCurrentLineNumber(p->lexH),cmLexCurrentColumnNumber(p->lexH));
  138. int n = strlen(buf);
  139. vsnprintf(buf+n,bn-n,fmt,vl);
  140. va_end(vl);
  141. return cmErrMsg(&p->err,kSyntaxErrJsRC,"%s",buf);
  142. }
  143. // Note that the stack is formed by a linked list
  144. // which is chained together using the nodes parentPtr.
  145. // This works beacause the parentPtr for object,array,
  146. // and pair nodes (the only nodes on the stack) is not
  147. // needed as long as the node is on the stack. Once the
  148. // node is popped the parentPtr is then set from the
  149. // new stack top.
  150. cmJsRC_t _cmJsonPushNode( cmJs_t* p, cmJsonNode_t* np )
  151. {
  152. np->ownerPtr = p->lastPtr;
  153. p->lastPtr = np;
  154. if( p->basePtr == NULL )
  155. p->basePtr = np;
  156. return kOkJsRC;
  157. }
  158. cmJsRC_t _cmJsonPopNode( cmJs_t* p )
  159. {
  160. if( p->lastPtr == NULL )
  161. return _cmJsonSyntaxError(p,"A parser stack underlow signalled a syntax error.");
  162. cmJsonNode_t* t = p->lastPtr;
  163. // remove the top element
  164. p->lastPtr = p->lastPtr->ownerPtr;
  165. // set the parent of the popped node
  166. t->ownerPtr = p->lastPtr;
  167. if( p->lastPtr == NULL )
  168. p->basePtr = NULL;
  169. return kOkJsRC;
  170. }
  171. cmJsRC_t cmJsonInitialize( cmJsonH_t* hp, cmCtx_t* ctx )
  172. {
  173. cmJsRC_t rc;
  174. cmJs_t* p;
  175. unsigned i;
  176. // finalize before initialize
  177. if((rc = cmJsonFinalize(hp)) != kOkJsRC )
  178. return rc;
  179. // allocate the main object record
  180. if((p = cmMemAllocZ( cmJs_t, 1 )) == NULL )
  181. return cmErrMsg(&ctx->err,kMemAllocErrJsRC,"Object memory allocation failed.");
  182. cmErrSetup(&p->err,&ctx->rpt,"JSON Parser");
  183. // allocate the linked heap mgr
  184. if( cmLHeapIsValid(p->heapH = cmLHeapCreate(1024,ctx)) == false )
  185. {
  186. rc = _cmJsonError(p,kMemAllocErrJsRC,"Linked heap object allocation failed.");
  187. goto errLabel;
  188. }
  189. // allocate the lexer
  190. if(cmLexIsValid(p->lexH = cmLexInit(NULL,0,0,&ctx->rpt)) == false )
  191. {
  192. rc = _cmJsonError(p,kLexErrJsRC,"Lex allocation failed.");
  193. goto errLabel;
  194. }
  195. // register json specific tokens with the lexer
  196. for(i=0; _cmJsTokenArray[i].id != kErrorLexTId; ++i)
  197. {
  198. cmRC_t lexRC;
  199. if( (lexRC = cmLexRegisterToken(p->lexH, _cmJsTokenArray[i].id, _cmJsTokenArray[i].text )) != kOkLexRC )
  200. {
  201. rc = _cmJsonError(p,kLexErrJsRC,"Lex token registration failed for:'%s'.\nLexer Error:%s",_cmJsTokenArray[i].text, cmLexRcToMsg(lexRC) );
  202. goto errLabel;
  203. }
  204. }
  205. hp->h = p;
  206. return kOkJsRC;
  207. errLabel:
  208. cmMemPtrFree(&p);
  209. if( cmLHeapIsValid(p->heapH) )
  210. cmLHeapDestroy(&p->heapH);
  211. if( cmLexIsValid(p->lexH) )
  212. cmLexFinal(&p->lexH);
  213. return rc;
  214. }
  215. cmJsRC_t cmJsonInitializeFromFile( cmJsonH_t* hp, const char* fn, cmCtx_t* ctx )
  216. {
  217. cmJsRC_t jsRC;
  218. if((jsRC = cmJsonInitialize(hp,ctx)) != kOkJsRC )
  219. return jsRC;
  220. if((jsRC = cmJsonParseFile(*hp,fn,NULL)) != kOkJsRC )
  221. cmJsonFinalize(hp);
  222. return jsRC;
  223. }
  224. cmJsRC_t cmJsonInitializeFromBuf( cmJsonH_t* hp, cmCtx_t* ctx, const char* buf, unsigned bufByteCnt )
  225. {
  226. cmJsRC_t jsRC;
  227. if((jsRC = cmJsonInitialize(hp,ctx)) != kOkJsRC )
  228. return jsRC;
  229. if((jsRC = cmJsonParse(*hp,buf,bufByteCnt,NULL)) != kOkJsRC )
  230. cmJsonFinalize(hp);
  231. return jsRC;
  232. }
  233. cmJsRC_t cmJsonFinalize( cmJsonH_t* hp )
  234. {
  235. cmRC_t lexRC;
  236. if( hp == NULL || hp->h == NULL )
  237. return kOkJsRC;
  238. cmJs_t* p = _cmJsonHandleToPtr(*hp);
  239. // free the internal heap object
  240. cmLHeapDestroy( &p->heapH );
  241. // free the lexer
  242. if( cmLexIsValid(p->lexH) )
  243. if((lexRC = cmLexFinal(&p->lexH)) != kOkLexRC )
  244. return _cmJsonError(p,kLexErrJsRC,"Lexer finalization failed.\nLexer Error:%s",cmLexRcToMsg(lexRC));
  245. cmMemPtrFree(&p->serialBufPtr);
  246. // free the handle
  247. cmMemPtrFree(&hp->h);
  248. return kOkJsRC;
  249. }
  250. bool cmJsonIsValid( cmJsonH_t h )
  251. { return h.h != NULL; }
  252. bool cmJsonIsModified( cmJsonH_t h )
  253. {
  254. cmJs_t* p = _cmJsonHandleToPtr(h);
  255. return p->modifiedFl;
  256. }
  257. cmJsRC_t _cmJsonLinkInChild( cmJs_t* p, cmJsonNode_t* parentPtr, cmJsonNode_t* np )
  258. {
  259. cmJsRC_t rc = kOkJsRC;
  260. np->ownerPtr = parentPtr;
  261. switch( parentPtr->typeId )
  262. {
  263. case kObjectTId:
  264. case kArrayTId:
  265. case kPairTId:
  266. {
  267. // if the parent is an 'object' then the child must be a 'pair'
  268. if( parentPtr->typeId == kObjectTId && np->typeId != kPairTId )
  269. rc = _cmJsonSyntaxError(p,"Expect only 'pair' nodes as children of 'objects'.");
  270. // if the parent is a 'pair' then is may have a max of two children
  271. if( parentPtr->typeId == kPairTId && cmJsonChildCount(parentPtr) >= 2 )
  272. rc = _cmJsonSyntaxError(p,"'pair' nodes may only have 2 children.");
  273. // insert the new node into the parent child list
  274. // if the new node is the first child
  275. if( parentPtr->u.childPtr == NULL )
  276. parentPtr->u.childPtr = np;
  277. else
  278. {
  279. // if the new node is the second or greater child
  280. cmJsonNode_t* lp = parentPtr->u.childPtr;
  281. while( lp->siblingPtr != NULL )
  282. lp = lp->siblingPtr;
  283. lp->siblingPtr = np;
  284. }
  285. }
  286. break;
  287. default:
  288. rc = _cmJsonSyntaxError(p,"'%s' nodes cannot be parent nodes.",_cmJsonNodeTypeIdToLabel(parentPtr->typeId));
  289. break;
  290. }
  291. return rc;
  292. }
  293. // This function creates nodes it also:
  294. // 1. inserts array elments into their parents child list
  295. // 2. inserts pairs into their parents member list
  296. // 3. assigns values to pairs
  297. cmJsRC_t _cmJsonCreateNode( cmJs_t* p, cmJsonNode_t* parentPtr, unsigned newNodeTypeId, cmJsonNode_t** npp )
  298. {
  299. cmJsRC_t rc = kOkJsRC;
  300. cmJsonNode_t* np;
  301. assert( npp != NULL );
  302. *npp = NULL;
  303. // allocate the new node
  304. if((np = cmLHeapAllocZ( p->heapH, sizeof(cmJsonNode_t) )) == NULL )
  305. return _cmJsonError(p,kMemAllocErrJsRC,"Error allocating node memory.");
  306. // set the new node type
  307. np->typeId = newNodeTypeId;
  308. np->ownerPtr = parentPtr;
  309. if( parentPtr == NULL )
  310. {
  311. if( newNodeTypeId != kObjectTId && newNodeTypeId != kArrayTId )
  312. rc = _cmJsonSyntaxError(p,"'%s' nodes must have a parent node.",_cmJsonNodeTypeIdToLabel(newNodeTypeId));
  313. }
  314. else
  315. {
  316. // if the parent is an 'object', 'array', or 'pair' then the
  317. // new node must be a child - insert it into the parent child list
  318. if((rc = _cmJsonLinkInChild( p, parentPtr, np )) != kOkJsRC )
  319. return rc;
  320. switch( newNodeTypeId )
  321. {
  322. case kObjectTId:
  323. case kArrayTId:
  324. break;
  325. case kPairTId:
  326. if( parentPtr == NULL || parentPtr->typeId != kObjectTId )
  327. rc = _cmJsonSyntaxError(p,"'pair' nodes must be the child of an object 'node'.");
  328. break;
  329. default:
  330. if( parentPtr == NULL )
  331. rc = _cmJsonSyntaxError(p,"'%s' nodes must have parent nodes.",_cmJsonNodeTypeIdToLabel(newNodeTypeId));
  332. }
  333. }
  334. // assign the return value
  335. *npp = np;
  336. if( p->rootPtr == NULL)
  337. {
  338. if( np->typeId != kObjectTId && np->typeId != kArrayTId )
  339. rc = _cmJsonSyntaxError(p,"The root object must be an 'object' or 'array'.");
  340. p->rootPtr = np;
  341. }
  342. p->modifiedFl = true;
  343. return rc;
  344. }
  345. cmJsRC_t _cmJsonCreateNumber( cmJs_t* p, cmJsonNode_t* parentPtr, unsigned nodeTId, cmJsonNode_t** npp )
  346. {
  347. // numbers may only occurr as children of a 'pair' or element of an 'array'
  348. if( (parentPtr==NULL) || (parentPtr->typeId != kPairTId && parentPtr->typeId != kArrayTId) )
  349. return _cmJsonSyntaxError(p, "The parent of scalar:%*s is not a 'pair' or 'array'.", cmLexTokenCharCount(p->lexH), cmLexTokenText(p->lexH) );
  350. return _cmJsonCreateNode(p,parentPtr,nodeTId,npp);
  351. }
  352. cmJsRC_t _cmJsonCreateReal(cmJs_t* p, cmJsonNode_t* parentPtr, double v, cmJsonNode_t** newNodePtrPtr )
  353. {
  354. cmJsRC_t rc;
  355. cmJsonNode_t* np = NULL;
  356. if( newNodePtrPtr != NULL )
  357. *newNodePtrPtr = NULL;
  358. if((rc= _cmJsonCreateNumber(p,parentPtr,kRealTId,&np)) == kOkJsRC )
  359. {
  360. np->u.realVal = v;
  361. if( newNodePtrPtr != NULL)
  362. *newNodePtrPtr = np;
  363. }
  364. return rc;
  365. }
  366. cmJsRC_t _cmJsonCreateInt(cmJs_t* p, cmJsonNode_t* parentPtr, int v, cmJsonNode_t** newNodePtrPtr )
  367. {
  368. cmJsRC_t rc;
  369. cmJsonNode_t* np = NULL;
  370. if( newNodePtrPtr != NULL )
  371. *newNodePtrPtr = NULL;
  372. if((rc= _cmJsonCreateNumber(p,parentPtr,kIntTId,&np)) == kOkJsRC )
  373. {
  374. np->u.intVal = v;
  375. if( newNodePtrPtr != NULL )
  376. *newNodePtrPtr = np;
  377. }
  378. return rc;
  379. }
  380. cmJsRC_t _cmJsonCreateBool(cmJs_t* p, cmJsonNode_t* parentPtr, bool fl, cmJsonNode_t** newNodePtrPtr )
  381. {
  382. cmJsRC_t rc;
  383. cmJsonNode_t* np = NULL;
  384. if( newNodePtrPtr != NULL )
  385. *newNodePtrPtr = NULL;
  386. if((rc= _cmJsonCreateNumber(p,parentPtr,fl?kTrueTId:kFalseTId,&np)) == kOkJsRC )
  387. {
  388. np->u.boolVal = fl;
  389. if( newNodePtrPtr != NULL )
  390. *newNodePtrPtr = np;
  391. }
  392. return rc;
  393. }
  394. cmJsRC_t _cmJsonCreateNull(cmJs_t* p, cmJsonNode_t* parentPtr, cmJsonNode_t** newNodePtrPtr )
  395. {
  396. cmJsRC_t rc;
  397. cmJsonNode_t* np = NULL;
  398. if( newNodePtrPtr != NULL )
  399. *newNodePtrPtr = NULL;
  400. if((rc = _cmJsonCreateNumber(p,parentPtr,kNullTId,&np)) == kOkJsRC )
  401. {
  402. if( newNodePtrPtr != NULL )
  403. *newNodePtrPtr = np;
  404. }
  405. return rc;
  406. }
  407. cmJsRC_t _cmJsonEscapeInt( cmJs_t* p, const char* sp, unsigned i, unsigned n, unsigned hexDigitCnt, char* rp )
  408. {
  409. char buf[hexDigitCnt+1];
  410. memset(buf,0,hexDigitCnt+1);
  411. // fill buf[] with the next two characters
  412. unsigned j;
  413. for(j=0; i<n && j<hexDigitCnt; ++i,++j)
  414. buf[j] = sp[i];
  415. // be sure the buffer was filled - we must get exactly two characters
  416. if( i == n )
  417. return _cmJsonError(p,kInvalidHexEscapeJsRC,"An invalid hex escape code was encountered.");
  418. // do the text to int conversion
  419. errno = 0;
  420. long val = strtol(buf,NULL,16);
  421. // validate the conversion
  422. if( errno != 0 )
  423. return _cmJsonError(p,kInvalidHexEscapeJsRC,"Hex escape value conversion failed.");
  424. // convert the long to a character
  425. if( val > 0xff )
  426. return _cmJsonError(p,kInvalidHexEscapeJsRC,"Hex escape value is out of range (0x00-0xff).");
  427. *rp = (char)val;
  428. return kOkJsRC;
  429. }
  430. cmJsRC_t _cmJsonEscapeString( cmJs_t* p, char* dp, const char* sp, unsigned n )
  431. {
  432. cmJsRC_t rc;
  433. unsigned hexDigCnt = 2; // count of digits in \u notation
  434. unsigned i,j;
  435. for(i=0,j=0; i<n; ++i,++j)
  436. {
  437. if( (sp[i] == '\\') && (i<(n-1) ))
  438. {
  439. switch( sp[i+1] )
  440. {
  441. case 'b': dp[j] = '\b'; break;
  442. case 'f': dp[j] = '\f'; break;
  443. case 'n': dp[j] = '\n'; break;
  444. case 'r': dp[j] = '\r'; break;
  445. case 't': dp[j] = '\t'; break;
  446. case 'u':
  447. {
  448. if((rc = _cmJsonEscapeInt( p, sp, i+2, n, hexDigCnt, dp+j )) != kOkJsRC )
  449. return rc;
  450. i += hexDigCnt; // skip hex digits
  451. }
  452. break;
  453. default:
  454. dp[j] = sp[i+1];
  455. break;
  456. }
  457. ++i; // skip the escape character '\'
  458. }
  459. else
  460. {
  461. dp[j] = sp[i];
  462. }
  463. }
  464. if( j < n )
  465. dp[j] = 0;
  466. return kOkJsRC;
  467. }
  468. // if 'cp' is NULL or 'cn' is 0 then the current string is dealloc'd
  469. // and the internal string value is left as NULL.
  470. cmJsRC_t _cmJsonSetString( cmJs_t* p, cmJsonNode_t* np, const char* cp, unsigned cn )
  471. {
  472. assert( np->typeId == kStringTId );
  473. cmJsRC_t rc = kOkJsRC;
  474. // deallocate the current string
  475. if( np->u.stringVal != NULL )
  476. {
  477. cmLHeapFree(p->heapH,np->u.stringVal);
  478. np->u.stringVal = NULL;
  479. }
  480. if( cp != NULL && cn>0 )
  481. {
  482. // allocate the node data memory to hold the string
  483. if((np->u.stringVal = cmLHeapAllocZ(p->heapH,cn+1)) == NULL )
  484. return _cmJsonError(p,kMemAllocErrJsRC,"Unable to allocate stirng memory.");
  485. // copy the string into the node data memory
  486. if((rc = _cmJsonEscapeString(p, np->u.stringVal, cp, cn)) != kOkJsRC )
  487. return rc;
  488. np->u.stringVal[cn]=0;
  489. }
  490. return rc;
  491. }
  492. cmJsRC_t _cmJsonCreateString(cmJs_t* p, cmJsonNode_t* parentPtr, const char* cp, unsigned cn, cmJsonNode_t** newNodePtrPtr )
  493. {
  494. cmJsRC_t rc;
  495. cmJsonNode_t* np = NULL;
  496. if( newNodePtrPtr != NULL )
  497. *newNodePtrPtr = NULL;
  498. // strings may only occurr as children of a 'pair' or element of an 'array'
  499. if( (parentPtr->typeId != kPairTId && parentPtr->typeId != kArrayTId ) )
  500. return _cmJsonSyntaxError(p, "The parent of string:%*s is not a 'pair', 'array', or 'object'.", cn,cp);
  501. // create the new node
  502. if((rc = _cmJsonCreateNode(p,parentPtr,kStringTId,&np)) != kOkJsRC )
  503. return rc;
  504. if((rc = _cmJsonSetString(p,np,cp,cn)) != kOkJsRC )
  505. return rc;
  506. /*
  507. // allocate the node data memory to hold the string
  508. if((np->u.stringVal = cmLHeapAllocZ(p->heapH,cn+1)) == NULL )
  509. return _cmJsonError(p,kMemAllocErrJsRC,"Unable to allocate stirng memory.");
  510. if((rc = _cmJsonEscapeString(p, np->u.stringVal, cp, cn)) != kOkJsRC )
  511. return rc;
  512. // copy the string into the node data memory
  513. //strncpy(np->u.stringVal,cp,cn);
  514. np->u.stringVal[cn]=0;
  515. */
  516. if( newNodePtrPtr != NULL )
  517. *newNodePtrPtr = np;
  518. return rc;
  519. }
  520. cmJsRC_t _cmJsonCreatePair( cmJs_t* p, cmJsonNode_t* parentPtr, const char* label, cmJsonNode_t** newNodePtrPtr )
  521. {
  522. cmJsRC_t rc;
  523. if((rc = _cmJsonCreateNode(p,parentPtr,kPairTId,newNodePtrPtr)) != kOkJsRC )
  524. return rc;
  525. return _cmJsonCreateString( p, *newNodePtrPtr, label, strlen(label), NULL);
  526. }
  527. // if fn == NULL then the buffer must contain the text to parse
  528. cmJsRC_t _cmJsonParse(cmJsonH_t h, const char* buf, unsigned bufCharCnt, const cmChar_t* fn, cmJsonNode_t* altRootPtr )
  529. {
  530. unsigned lexTId = kErrorLexTId;
  531. cmJs_t* p = _cmJsonHandleToPtr(h);
  532. cmJsonNode_t* cnp = altRootPtr == p->rootPtr ? altRootPtr : p->rootPtr;
  533. cmJsonNode_t* nnp = NULL;
  534. cmJsRC_t rc;
  535. p->reportErrPosnFl = true;
  536. // assign the text buffer and reset the lexer
  537. if( fn == NULL )
  538. rc = cmLexSetTextBuffer( p->lexH, buf, bufCharCnt );
  539. else
  540. rc = cmLexSetFile( p->lexH, fn );
  541. if( rc != kOkLexRC )
  542. return _cmJsonError( p, kLexErrJsRC, "Error setting lexer buffer.");
  543. // get the next token
  544. while( (lexTId = cmLexGetNextToken( p->lexH )) != kErrorLexTId && (lexTId != kEofLexTId ) && (rc==kOkJsRC) )
  545. {
  546. cnp = p->lastPtr;
  547. // if cnp is a pair and it's value has been assigned
  548. if( cnp != NULL && cnp->typeId == kPairTId && cmJsonChildCount(cnp)==2 )
  549. {
  550. if((rc = _cmJsonPopNode(p)) != kOkJsRC )
  551. break;
  552. cnp = p->lastPtr;
  553. }
  554. switch( lexTId )
  555. {
  556. case kRealLexTId: // real number
  557. rc = _cmJsonCreateReal(p, cnp, cmLexTokenDouble( p->lexH ), NULL);
  558. break;
  559. case kHexLexTId: // hexidecimal integer
  560. // allow hex integers to be equivalent to decimal integers
  561. case kIntLexTId: // decimal integer
  562. rc = _cmJsonCreateInt(p, cnp, cmLexTokenInt( p->lexH ), NULL );
  563. break;
  564. case kTrueLexTId: // true
  565. rc = _cmJsonCreateBool(p, cnp, true, NULL );
  566. break;
  567. case kFalseLexTId: // false
  568. rc = _cmJsonCreateBool(p, cnp, false, NULL );
  569. break;
  570. case kNullLexTId: // null
  571. rc = _cmJsonCreateNull(p, cnp, NULL );
  572. break;
  573. case kIdentLexTId: // identifier
  574. // allow identifiers to be equivalent to strings.
  575. case kQStrLexTId: // quoted string
  576. if( cnp == NULL )
  577. rc = _cmJsonSyntaxError(p,"Encountered a 'string' with no parent.");
  578. else
  579. if( cnp->typeId == kObjectTId )
  580. {
  581. if((rc = _cmJsonCreateNode(p,cnp,kPairTId,&nnp)) == kOkJsRC )
  582. {
  583. _cmJsonPushNode(p,nnp);
  584. cnp = nnp;
  585. }
  586. //if((rc = _cmJsonCreateNewParent(p,cnp,kPairTId)) == kOkJsRC )
  587. // cnp = p->lastPtr;
  588. }
  589. if( rc == kOkJsRC )
  590. rc = _cmJsonCreateString(p, cnp, cmLexTokenText(p->lexH), cmLexTokenCharCount(p->lexH), NULL);
  591. break;
  592. case kColonLexTId:
  593. if( cnp->typeId != kPairTId )
  594. rc = _cmJsonSyntaxError(p,"A colon was found outside of a 'pair' element.");
  595. break;
  596. case kLCurlyLexTId: // {
  597. //rc = _cmJsonCreateNewParent(p, cnp, kObjectTId );
  598. if((rc = _cmJsonCreateNode(p,cnp,kObjectTId,&nnp)) == kOkJsRC )
  599. _cmJsonPushNode(p,nnp);
  600. break;
  601. case kRCurlyLexTId: // }
  602. if( cnp == NULL || cnp->typeId != kObjectTId )
  603. rc = _cmJsonSyntaxError(p,"A '}' was found without an accompanying opening bracket.");
  604. else
  605. rc = _cmJsonPopNode(p);
  606. break;
  607. case kLHardLexTId: // [
  608. //rc = _cmJsonCreateNewParent(p, cnp, kArrayTId);
  609. if((rc = _cmJsonCreateNode(p,cnp,kArrayTId,&nnp)) == kOkJsRC )
  610. _cmJsonPushNode(p,nnp);
  611. break;
  612. case kRHardLexTId: // ]
  613. if( cnp == NULL || cnp->typeId != kArrayTId )
  614. rc = _cmJsonSyntaxError(p,"A ']' was found without an accompanying opening bracket.");
  615. else
  616. rc = _cmJsonPopNode(p);
  617. break;
  618. case kCommaLexTId: // ,
  619. if( (cnp==NULL) || (cnp->typeId != kArrayTId && cnp->typeId != kObjectTId) )
  620. rc = _cmJsonSyntaxError(p,"Commas may only occur in 'array' and 'object' nodes.");
  621. break;
  622. case kSpaceLexTId: // white space
  623. case kBlockCmtLexTId: // block comment
  624. case kLineCmtLexTId: // line comment
  625. assert(0);
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. if( lexTId == kErrorLexTId )
  632. rc = _cmJsonSyntaxError( p, "The lexer failed: %s.", cmLexRcToMsg(cmLexErrorRC(p->lexH)));
  633. p->reportErrPosnFl = false;
  634. p->modifiedFl = false;
  635. return rc;
  636. }
  637. cmJsRC_t cmJsonParse( cmJsonH_t h, const char* buf, unsigned bufCharCnt, cmJsonNode_t* altRootPtr )
  638. { return _cmJsonParse(h,buf,bufCharCnt,NULL,altRootPtr); }
  639. cmJsRC_t cmJsonParseFile( cmJsonH_t h, const char* fn, cmJsonNode_t* altRootPtr )
  640. { return _cmJsonParse(h,NULL,0,fn,altRootPtr); }
  641. cmJsonNode_t* cmJsonRoot( cmJsonH_t h )
  642. {
  643. cmJs_t* p = _cmJsonHandleToPtr(h);
  644. return p->rootPtr;
  645. }
  646. cmJsRC_t cmJsonClearTree( cmJsonH_t h )
  647. {
  648. cmJs_t* p = _cmJsonHandleToPtr(h);
  649. p->rootPtr = NULL;
  650. p->basePtr = NULL;
  651. p->lastPtr = NULL;
  652. cmLHeapClear(p->heapH,true);
  653. return kOkJsRC;
  654. }
  655. bool cmJsonIsObject( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kObjectTId); }
  656. bool cmJsonIsArray( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kArrayTId); }
  657. bool cmJsonIsPair( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kPairTId); }
  658. bool cmJsonIsString( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kStringTId); }
  659. bool cmJsonIsInt( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kIntTId); }
  660. bool cmJsonIsReal( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kRealTId); }
  661. bool cmJsonIsBool( const cmJsonNode_t* np ) { return cmIsFlag(np->typeId,kTrueTId | kFalseTId); }
  662. unsigned cmJsonChildCount( const cmJsonNode_t* np )
  663. {
  664. if( np == NULL )
  665. return 0;
  666. unsigned n = 0;
  667. switch( np->typeId )
  668. {
  669. case kObjectTId:
  670. case kArrayTId:
  671. case kPairTId:
  672. {
  673. const cmJsonNode_t* lp = np->u.childPtr;
  674. while( lp != NULL )
  675. {
  676. ++n;
  677. lp = lp->siblingPtr;
  678. }
  679. }
  680. break;
  681. default:
  682. break;
  683. }
  684. return n;
  685. }
  686. cmJsonNode_t* cmJsonArrayElement( cmJsonNode_t* np, unsigned index )
  687. {
  688. unsigned i;
  689. assert( index < cmJsonChildCount(np));
  690. np = np->u.childPtr;
  691. for(i=0; i<index; ++i)
  692. np = np->siblingPtr;
  693. return np;
  694. }
  695. const cmJsonNode_t* cmJsonArrayElementC( const cmJsonNode_t* np, unsigned index )
  696. { return cmJsonArrayElement( (cmJsonNode_t*)np, index ); }
  697. const char* cmJsonPairLabel( const cmJsonNode_t* pairPtr )
  698. {
  699. assert( pairPtr->typeId == kPairTId );
  700. if( pairPtr->typeId != kPairTId )
  701. return NULL;
  702. return pairPtr->u.childPtr->u.stringVal;
  703. }
  704. unsigned cmJsonPairTypeId( const cmJsonNode_t* pairPtr )
  705. {
  706. assert( pairPtr->typeId == kPairTId );
  707. return pairPtr->u.childPtr->siblingPtr->typeId;
  708. }
  709. cmJsonNode_t* cmJsonPairValue( cmJsonNode_t* pairPtr )
  710. {
  711. assert( pairPtr->typeId == kPairTId );
  712. if( pairPtr->typeId != kPairTId )
  713. return NULL;
  714. return pairPtr->u.childPtr->siblingPtr;
  715. }
  716. cmJsonNode_t* cmJsonFindValue( cmJsonH_t h, const char* label, const cmJsonNode_t* np, unsigned keyTypeMask )
  717. {
  718. cmJs_t* p = _cmJsonHandleToPtr(h);
  719. if( np == NULL )
  720. np = p->rootPtr;
  721. if( np == NULL )
  722. return NULL;
  723. // we are only interested in pairs
  724. if( np->typeId == kPairTId )
  725. {
  726. // pairs must have exactly two nodes - the first must be a string
  727. assert(np->u.childPtr != NULL && np->u.childPtr->typeId == kStringTId && np->u.childPtr->siblingPtr != NULL );
  728. if( strcmp(cmJsonPairLabel(np),label) == 0 )
  729. {
  730. if( (keyTypeMask==kInvalidTId) || (keyTypeMask & np->u.childPtr->siblingPtr->typeId) )
  731. return np->u.childPtr->siblingPtr;
  732. }
  733. }
  734. // if the node is an object,array, or pair ...
  735. if( np->typeId==kObjectTId || np->typeId==kArrayTId || np->typeId==kPairTId )
  736. {
  737. // ... then recurse on its children
  738. cmJsonNode_t* cnp = np->u.childPtr;
  739. while(cnp != NULL)
  740. {
  741. cmJsonNode_t* rp;
  742. if((rp = cmJsonFindValue(h,label,cnp,keyTypeMask)) != NULL )
  743. return rp;
  744. cnp = cnp->siblingPtr;
  745. }
  746. }
  747. return NULL;
  748. }
  749. cmJsRC_t _cmJsonFindPathValue( cmJs_t* p, const char* pathPrefix, const char* path, const cmJsonNode_t* rp, const cmJsonNode_t** rpp )
  750. {
  751. cmJsRC_t rc = kOkJsRC;
  752. if( rp == NULL )
  753. rp = p->rootPtr;
  754. if( rp == NULL )
  755. return kOkJsRC;
  756. assert( cmJsonIsObject(rp));
  757. assert( rpp != NULL );
  758. *rpp = NULL;
  759. // create a copy of the path
  760. unsigned i,j;
  761. unsigned sn = (pathPrefix==NULL ? 0 : strlen(pathPrefix)) + strlen(path) + 1; // add one for the possible extra seperator
  762. char ss[ 1024 ];
  763. char* sp = NULL;
  764. char* sm = NULL;
  765. char* sb = ss;
  766. // don't put more than 1k on the stack
  767. if( sn + 1 > 1024 )
  768. {
  769. sm = cmMemAllocZ(char,sn+1);
  770. sb = sm;
  771. }
  772. sp = sb;
  773. sp[0] = 0;
  774. if(pathPrefix != NULL )
  775. {
  776. strcpy(sp,pathPrefix);
  777. // if pathPrefix does not end in a '/' then insert one
  778. if( sp[ strlen(sp)-1 ] != '/' )
  779. strcat(sp,"/");
  780. }
  781. // the '/' has already been inserted - skip any leading '/' character in path
  782. strcat(sp,path[0]=='/' ? path+1 : path );
  783. // terminate each path label with a '/0'.
  784. sp = ss;
  785. for(i=0,j=0; sp < ss + sn; ++sp, ++i )
  786. if( *sp == '/' )
  787. {
  788. *sp = 0;
  789. ++j;
  790. }
  791. if( i > 0 )
  792. {
  793. sp = sb;
  794. while( sp < sb + sn )
  795. {
  796. // labeled values are always associated with pairs and
  797. // pairs only exist as the children of objects.
  798. if( cmJsonIsObject(rp) == false )
  799. {
  800. rc = cmErrMsg(&p->err,kInvalidNodeTypeJsRC,"A non-object node was encountered on a object path.");
  801. break;
  802. }
  803. // get the count of pairs in this object
  804. unsigned cn = cmJsonChildCount( rp );
  805. cmJsonNode_t* cp = rp->u.childPtr;
  806. unsigned k;
  807. for(k=0; k<cn; ++k)
  808. {
  809. // all children of an object must be pairs
  810. assert( cp != NULL && cmJsonIsPair(cp) );
  811. // if this is the labeled pair we are looking for
  812. if( strcmp(cmJsonPairLabel(cp),sp) == 0 )
  813. break;
  814. cp = cp->siblingPtr;
  815. }
  816. // if the search failed
  817. if( k == cn || cp == NULL )
  818. {
  819. rc = cmErrMsg(&p->err,kNodeNotFoundJsRC,"The path label '%s' could not be found.",cmStringNullGuard(sp));
  820. break;
  821. }
  822. // take the value of the located pair to continue the search
  823. rp = cmJsonPairValue(cp);
  824. // advance to the next label
  825. sp += strlen(sp) + 1;
  826. }
  827. }
  828. cmMemPtrFree(&sm);
  829. *rpp = rp;
  830. return rc;
  831. }
  832. cmJsRC_t _cmJsonPathToValueError( cmJs_t* p, cmJsRC_t rc, const char* pathPrefix, const char* path, const char* typeLabel )
  833. {
  834. if( pathPrefix != NULL )
  835. cmErrMsg(&p->err,rc,"The JSON value at '%s/%s' could not be converted to a '%s'.",cmStringNullGuard(pathPrefix),cmStringNullGuard(path),cmStringNullGuard(typeLabel));
  836. else
  837. cmErrMsg(&p->err,rc,"The JSON value at '%s' could not be converted to a '%s'.",cmStringNullGuard(path),cmStringNullGuard(typeLabel));
  838. return rc;
  839. }
  840. cmJsRC_t cmJsonPathToValueNode( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, const cmJsonNode_t** nodePtrPtr )
  841. {
  842. cmJsRC_t rc = kOkJsRC;
  843. cmJs_t* p = _cmJsonHandleToPtr(h);
  844. if( objectNodePtr == NULL )
  845. objectNodePtr = p->rootPtr;
  846. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,nodePtrPtr)) != kOkJsRC )
  847. return rc;
  848. return rc;
  849. }
  850. cmJsRC_t cmJsonPathToBool( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, bool* retValPtr )
  851. {
  852. const cmJsonNode_t* rp;
  853. cmJsRC_t rc = kOkJsRC;
  854. cmJs_t* p = _cmJsonHandleToPtr(h);
  855. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  856. return rc;
  857. if((rc = cmJsonBoolValue(rp,retValPtr)) != kOkJsRC )
  858. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"bool");
  859. return rc;
  860. }
  861. cmJsRC_t cmJsonPathToInt( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, int* retValPtr )
  862. {
  863. const cmJsonNode_t* rp;
  864. cmJsRC_t rc = kOkJsRC;
  865. cmJs_t* p = _cmJsonHandleToPtr(h);
  866. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  867. return rc;
  868. if((rc = cmJsonIntValue(rp,retValPtr)) != kOkJsRC )
  869. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"bool");
  870. return rc;
  871. }
  872. cmJsRC_t cmJsonPathToUInt( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, unsigned* retValPtr )
  873. {
  874. const cmJsonNode_t* rp;
  875. cmJsRC_t rc = kOkJsRC;
  876. cmJs_t* p = _cmJsonHandleToPtr(h);
  877. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  878. return rc;
  879. if((rc = cmJsonUIntValue(rp,retValPtr)) != kOkJsRC )
  880. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"unsigned integer");
  881. return rc;
  882. }
  883. cmJsRC_t cmJsonPathToReal( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, double* retValPtr )
  884. {
  885. const cmJsonNode_t* rp;
  886. cmJsRC_t rc = kOkJsRC;
  887. cmJs_t* p = _cmJsonHandleToPtr(h);
  888. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  889. return rc;
  890. if((rc = cmJsonRealValue(rp,retValPtr)) != kOkJsRC )
  891. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"real");
  892. return rc;
  893. }
  894. cmJsRC_t cmJsonPathToString( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, const char** retValPtr )
  895. {
  896. const cmJsonNode_t* rp = NULL;
  897. cmJsRC_t rc = kOkJsRC;
  898. cmJs_t* p = _cmJsonHandleToPtr(h);
  899. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  900. return rc;
  901. if((rc = cmJsonStringValue(rp,retValPtr)) != kOkJsRC )
  902. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"string");
  903. return rc;
  904. }
  905. cmJsRC_t cmJsonPathToPair( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr )
  906. {
  907. const cmJsonNode_t* rp;
  908. cmJsRC_t rc = kOkJsRC;
  909. cmJs_t* p = _cmJsonHandleToPtr(h);
  910. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  911. return rc;
  912. if((rc = cmJsonPairNode(rp,retValPtr)) != kOkJsRC )
  913. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"pair");
  914. return rc;
  915. }
  916. cmJsRC_t cmJsonPathToArray( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr )
  917. {
  918. const cmJsonNode_t* rp;
  919. cmJsRC_t rc = kOkJsRC;
  920. cmJs_t* p = _cmJsonHandleToPtr(h);
  921. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  922. return rc;
  923. if((rc = cmJsonArrayNode(rp,retValPtr)) != kOkJsRC )
  924. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"array");
  925. return rc;
  926. }
  927. cmJsRC_t cmJsonPathToObject( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr )
  928. {
  929. const cmJsonNode_t* rp;
  930. cmJsRC_t rc = kOkJsRC;
  931. cmJs_t* p = _cmJsonHandleToPtr(h);
  932. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objectNodePtr,&rp)) != kOkJsRC )
  933. return rc;
  934. if((rc = cmJsonObjectNode(rp,retValPtr)) != kOkJsRC )
  935. return _cmJsonPathToValueError(p,rc,pathPrefix,path,"object");
  936. return rc;
  937. }
  938. const cmJsonNode_t* cmJsonFindPathValueC( cmJsonH_t h, const char* path, const cmJsonNode_t* np, unsigned typeIdMask )
  939. {
  940. cmJs_t* p = _cmJsonHandleToPtr(h);
  941. const cmJsonNode_t* rp = NULL;
  942. cmJsRC_t rc = kOkJsRC;
  943. if((rc = _cmJsonFindPathValue(p,NULL,path,np,&rp)) != kOkJsRC )
  944. {
  945. // validate the return type
  946. if( rp != NULL )
  947. if( (typeIdMask!=kInvalidTId) && (typeIdMask & rp->typeId)==0 )
  948. {
  949. cmErrMsg(&p->err,kInvalidNodeTypeJsRC,"The value at the end of the path '%s' did not match the requested type.",cmStringNullGuard(path));
  950. rp = NULL;
  951. }
  952. }
  953. return rp;
  954. }
  955. cmJsonNode_t* cmJsonFindPathValue( cmJsonH_t h, const char* path, const cmJsonNode_t* np, unsigned typeIdMask )
  956. { return (cmJsonNode_t*)cmJsonFindPathValueC(h,path,np,typeIdMask ); }
  957. cmJsRC_t _cmJsonFindMemberValue( const cmJsonNode_t* np, const char* label, unsigned keyTypeId, cmJsonNode_t** npp )
  958. {
  959. *npp = NULL;
  960. // the src node must be an object
  961. if( np->typeId != kObjectTId )
  962. return kNodeNotFoundJsRC;
  963. // for each member pair
  964. const cmJsonNode_t* cnp = np->u.childPtr;
  965. while( cnp != NULL )
  966. {
  967. assert( (cnp->typeId & kMaskTId) == kPairTId );
  968. // if the labels match ...
  969. if( strcmp( label, cmJsonPairLabel(cnp)) == 0 )
  970. {
  971. // ... and the type flags match ...
  972. if( (keyTypeId==kInvalidTId || cmIsFlag(cnp->u.childPtr->siblingPtr->typeId,keyTypeId) ) )
  973. {
  974. *npp = cnp->u.childPtr->siblingPtr;
  975. return kOkJsRC; // ... then the key was found.
  976. }
  977. // ... label match but wrong type ... this is considered an error
  978. return kNodeCannotCvtJsRC;
  979. }
  980. cnp = cnp->siblingPtr;
  981. }
  982. return kNodeNotFoundJsRC;
  983. }
  984. cmJsRC_t cmJsonUIntValue( const cmJsonNode_t* vp, unsigned* retPtr )
  985. {
  986. cmJsRC_t rc = kOkJsRC;
  987. if( vp == NULL )
  988. return kNodeCannotCvtJsRC;
  989. switch(vp->typeId)
  990. {
  991. case kIntTId: *retPtr = vp->u.intVal; break;
  992. case kRealTId: *retPtr = (unsigned)vp->u.realVal; break;
  993. case kTrueTId: *retPtr = 1; break;
  994. case kFalseTId: *retPtr = 0; break;
  995. default:
  996. rc = kNodeCannotCvtJsRC;
  997. }
  998. return rc;
  999. }
  1000. cmJsRC_t cmJsonIntValue( const cmJsonNode_t* vp, int* retPtr )
  1001. {
  1002. cmJsRC_t rc = kOkJsRC;
  1003. if( vp == NULL )
  1004. return kNodeCannotCvtJsRC;
  1005. switch(vp->typeId)
  1006. {
  1007. case kIntTId: *retPtr = vp->u.intVal; break;
  1008. case kRealTId: *retPtr = (int)vp->u.realVal; break;
  1009. case kTrueTId: *retPtr = 1; break;
  1010. case kFalseTId: *retPtr = 0; break;
  1011. default:
  1012. rc = kNodeCannotCvtJsRC;
  1013. }
  1014. return rc;
  1015. }
  1016. cmJsRC_t cmJsonRealValue( const cmJsonNode_t* vp, double* retPtr )
  1017. {
  1018. cmJsRC_t rc = kOkJsRC;
  1019. if( vp == NULL )
  1020. return kNodeCannotCvtJsRC;
  1021. switch(vp->typeId)
  1022. {
  1023. case kIntTId: *retPtr = vp->u.intVal; break;
  1024. case kRealTId: *retPtr = vp->u.realVal; break;
  1025. case kTrueTId: *retPtr = 1; break;
  1026. case kFalseTId: *retPtr = 0; break;
  1027. default:
  1028. rc = kNodeCannotCvtJsRC;
  1029. }
  1030. return rc;
  1031. }
  1032. cmJsRC_t cmJsonBoolValue( const cmJsonNode_t* vp, bool* retPtr )
  1033. {
  1034. cmJsRC_t rc = kOkJsRC;
  1035. if( vp == NULL )
  1036. return kNodeCannotCvtJsRC;
  1037. switch(vp->typeId)
  1038. {
  1039. case kIntTId: *retPtr = vp->u.intVal != 0; break;
  1040. case kRealTId: *retPtr = (int)vp->u.realVal != 0; break;
  1041. case kTrueTId: *retPtr = 1; break;
  1042. case kFalseTId: *retPtr = 0; break;
  1043. default:
  1044. rc = kNodeCannotCvtJsRC;
  1045. }
  1046. return rc;
  1047. }
  1048. cmJsRC_t cmJsonStringValue( const cmJsonNode_t* vp, const char **retPtrPtr )
  1049. {
  1050. cmJsRC_t rc = kOkJsRC;
  1051. if( vp == NULL || vp->typeId != kStringTId )
  1052. return kNodeCannotCvtJsRC;
  1053. *retPtrPtr = vp->u.stringVal;
  1054. return rc;
  1055. }
  1056. cmJsRC_t cmJsonPairNode( const cmJsonNode_t* vp, cmJsonNode_t **retPtrPtr )
  1057. {
  1058. cmJsRC_t rc = kOkJsRC;
  1059. if( !cmJsonIsPair(vp) )
  1060. return kNodeCannotCvtJsRC;
  1061. *retPtrPtr = (cmJsonNode_t*)vp;
  1062. return rc;
  1063. }
  1064. cmJsRC_t cmJsonArrayNode( const cmJsonNode_t* vp, cmJsonNode_t **retPtrPtr )
  1065. {
  1066. cmJsRC_t rc = kOkJsRC;
  1067. if( !cmJsonIsArray(vp) )
  1068. return kNodeCannotCvtJsRC;
  1069. *retPtrPtr = (cmJsonNode_t*)vp;
  1070. return rc;
  1071. }
  1072. cmJsRC_t cmJsonObjectNode( const cmJsonNode_t* vp, cmJsonNode_t **retPtrPtr )
  1073. {
  1074. cmJsRC_t rc = kOkJsRC;
  1075. if( !cmJsonIsObject(vp) )
  1076. return kNodeCannotCvtJsRC;
  1077. *retPtrPtr = (cmJsonNode_t*)vp;
  1078. return rc;
  1079. }
  1080. cmJsRC_t cmJsonUIntMember( const cmJsonNode_t* np, const char* label, unsigned* retPtr )
  1081. {
  1082. cmJsonNode_t* vp;
  1083. cmJsRC_t rc;
  1084. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1085. return rc;
  1086. return cmJsonUIntValue(vp,retPtr);
  1087. }
  1088. cmJsRC_t cmJsonIntMember( const cmJsonNode_t* np, const char* label, int* retPtr )
  1089. {
  1090. cmJsonNode_t* vp;
  1091. cmJsRC_t rc;
  1092. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1093. return rc;
  1094. return cmJsonIntValue(vp,retPtr);
  1095. }
  1096. cmJsRC_t cmJsonRealMember( const cmJsonNode_t* np, const char* label, double* retPtr )
  1097. {
  1098. cmJsonNode_t* vp;
  1099. cmJsRC_t rc;
  1100. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1101. return rc;
  1102. return cmJsonRealValue(vp,retPtr);
  1103. }
  1104. cmJsRC_t cmJsonBoolMember( const cmJsonNode_t* np, const char* label, bool* retPtr )
  1105. {
  1106. cmJsonNode_t* vp;
  1107. cmJsRC_t rc;
  1108. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1109. return rc;
  1110. return cmJsonBoolValue(vp,retPtr);
  1111. }
  1112. cmJsRC_t cmJsonStringMember( const cmJsonNode_t* np, const char* label, const char** retPtrPtr )
  1113. {
  1114. cmJsonNode_t* vp;
  1115. cmJsRC_t rc;
  1116. *retPtrPtr = NULL;
  1117. if((rc = _cmJsonFindMemberValue(np,label,kStringTId,&vp)) != kOkJsRC )
  1118. return rc;
  1119. return cmJsonStringValue(vp,retPtrPtr);
  1120. }
  1121. cmJsRC_t cmJsonNodeMember( const cmJsonNode_t* np, const char* label, cmJsonNode_t** retPtrPtr )
  1122. {
  1123. cmJsonNode_t* vp;
  1124. cmJsRC_t rc;
  1125. *retPtrPtr = NULL;
  1126. if((rc = _cmJsonFindMemberValue(np,label,kArrayTId|kObjectTId,&vp)) != kOkJsRC )
  1127. return rc;
  1128. *retPtrPtr = vp;
  1129. return rc;
  1130. }
  1131. cmJsonNode_t* cmJsonNodeMemberValue( const cmJsonNode_t* np, const char* label )
  1132. {
  1133. assert( cmJsonIsObject(np) );
  1134. unsigned n = cmJsonChildCount(np);
  1135. unsigned j = 0;
  1136. for(; j<n; ++j)
  1137. {
  1138. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,j);
  1139. assert( cnp != NULL && cmJsonIsPair(cnp) );
  1140. if( strcmp(label,cmJsonPairLabel(cnp))==0 )
  1141. return cmJsonPairValue((cmJsonNode_t*)cnp);
  1142. }
  1143. return NULL;
  1144. }
  1145. cmJsRC_t cmJsonVMemberValues( const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, va_list vl )
  1146. {
  1147. unsigned typeId;
  1148. cmJsRC_t rc = kOkJsRC;
  1149. if( errLabelPtrPtr != NULL )
  1150. *errLabelPtrPtr = NULL;
  1151. const char* label;
  1152. while( ((label = va_arg(vl,const char*)) != NULL) && (rc==kOkJsRC) )
  1153. {
  1154. typeId = va_arg(vl,unsigned);
  1155. switch( typeId & kMaskTId )
  1156. {
  1157. case kObjectTId:
  1158. case kPairTId:
  1159. case kArrayTId:
  1160. {
  1161. cmJsonNode_t** nodePtrPtr = va_arg(vl, cmJsonNode_t**);
  1162. if((rc = cmJsonNodeMember(objectNodePtr,label,nodePtrPtr )) == kOkJsRC )
  1163. {
  1164. cmJsonNode_t* np = *nodePtrPtr;
  1165. if( (np->typeId & kMaskTId) != (typeId&kMaskTId) )
  1166. rc = kNodeCannotCvtJsRC;
  1167. }
  1168. }
  1169. break;
  1170. case kIntTId:
  1171. {
  1172. int* ip = va_arg(vl, int* );
  1173. assert(ip != NULL);
  1174. rc = cmJsonIntMember(objectNodePtr, label, ip);
  1175. }
  1176. break;
  1177. case kRealTId:
  1178. {
  1179. double* dp = va_arg(vl, double*);
  1180. assert(dp != NULL);
  1181. rc = cmJsonRealMember(objectNodePtr, label, dp);
  1182. }
  1183. break;
  1184. case kStringTId:
  1185. {
  1186. const char** cpp = va_arg(vl, const char**);
  1187. assert(cpp != NULL);
  1188. rc = cmJsonStringMember(objectNodePtr, label, cpp);
  1189. }
  1190. break;
  1191. case kTrueTId:
  1192. case kFalseTId:
  1193. case kBoolTId:
  1194. {
  1195. bool* bp = va_arg(vl, bool* );
  1196. rc = cmJsonBoolMember(objectNodePtr, label, bp);
  1197. }
  1198. break;
  1199. default:
  1200. // missing terminating NULL on the var args list???
  1201. assert(0);
  1202. break;
  1203. }
  1204. if( (rc == kNodeNotFoundJsRC) && cmIsFlag(typeId,kOptArgJsFl) )
  1205. rc = kOkJsRC;
  1206. if( rc != kOkJsRC && errLabelPtrPtr != NULL )
  1207. *errLabelPtrPtr = label;
  1208. }
  1209. return rc;
  1210. }
  1211. cmJsRC_t cmJsonMemberValues( const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ...)
  1212. {
  1213. va_list vl;
  1214. va_start(vl,errLabelPtrPtr);
  1215. cmJsRC_t rc = cmJsonVMemberValues(objectNodePtr,errLabelPtrPtr,vl);
  1216. va_end(vl);
  1217. return rc;
  1218. }
  1219. cmJsRC_t cmJsonVPathValues( cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objNodePtr, const char** errLabelPtrPtr, va_list vl )
  1220. {
  1221. cmJsRC_t rc = kOkJsRC;
  1222. cmJs_t* p = _cmJsonHandleToPtr(h);
  1223. if( errLabelPtrPtr != NULL )
  1224. *errLabelPtrPtr = NULL;
  1225. const char* path;
  1226. while( ((path = va_arg(vl,const char*)) != NULL) && (rc==kOkJsRC) )
  1227. {
  1228. unsigned typeId;
  1229. const cmJsonNode_t* vnp;
  1230. typeId = va_arg(vl,unsigned);
  1231. // find the requested pair value
  1232. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objNodePtr,&vnp)) != kOkJsRC )
  1233. break;
  1234. switch( typeId & kMaskTId )
  1235. {
  1236. case kObjectTId:
  1237. case kPairTId:
  1238. case kArrayTId:
  1239. {
  1240. const cmJsonNode_t** nodePtrPtr = va_arg(vl, const cmJsonNode_t**);
  1241. if( (vnp->typeId & kMaskTId) != (typeId & kMaskTId) )
  1242. rc = kNodeCannotCvtJsRC;
  1243. else
  1244. *nodePtrPtr = vnp;
  1245. }
  1246. break;
  1247. case kIntTId:
  1248. {
  1249. int* ip = va_arg(vl, int* );
  1250. assert(ip != NULL);
  1251. rc = cmJsonIntValue(vnp, ip);
  1252. }
  1253. break;
  1254. case kRealTId:
  1255. {
  1256. double* dp = va_arg(vl, double*);
  1257. assert(dp != NULL);
  1258. rc = cmJsonRealValue(vnp, dp);
  1259. }
  1260. break;
  1261. case kStringTId:
  1262. {
  1263. const char** cpp = va_arg(vl, const char**);
  1264. assert(cpp != NULL);
  1265. rc = cmJsonStringValue(vnp, cpp);
  1266. }
  1267. break;
  1268. case kTrueTId:
  1269. case kFalseTId:
  1270. {
  1271. bool* bp = va_arg(vl, bool* );
  1272. rc = cmJsonBoolValue(vnp, bp);
  1273. }
  1274. break;
  1275. default:
  1276. // missing terminating NULL on the var args list???
  1277. assert(0);
  1278. break;
  1279. }
  1280. if( (rc == kNodeNotFoundJsRC) && cmIsFlag(typeId,kOptArgJsFl) )
  1281. rc = kOkJsRC;
  1282. if( rc != kOkJsRC && errLabelPtrPtr != NULL )
  1283. *errLabelPtrPtr = path;
  1284. }
  1285. return rc;
  1286. }
  1287. cmJsRC_t cmJsonPathValues( cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ... )
  1288. {
  1289. va_list vl;
  1290. va_start(vl,errLabelPtrPtr);
  1291. cmJsRC_t rc = cmJsonVPathValues(h,pathPrefix,objectNodePtr,errLabelPtrPtr,vl);
  1292. va_end(vl);
  1293. return rc;
  1294. }
  1295. cmJsonNode_t* _cmJsonCreateNode2( cmJsonH_t h, unsigned newNodeTypeId, cmJsonNode_t* parentPtr )
  1296. {
  1297. cmJs_t* p = _cmJsonHandleToPtr(h);
  1298. cmJsonNode_t* np = NULL;
  1299. if((p->rc = _cmJsonCreateNode(p,parentPtr,newNodeTypeId,&np)) != kOkJsRC )
  1300. return NULL;
  1301. return np;
  1302. }
  1303. cmJsRC_t cmJsonCreate( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned typeId, const char* sv, int iv, double dv, cmJsonNode_t** rpp )
  1304. {
  1305. cmJsonNode_t* rp = NULL;
  1306. cmJsRC_t rc = kOkJsRC;
  1307. cmJs_t* p = _cmJsonHandleToPtr(h);
  1308. if( rpp != NULL )
  1309. *rpp = NULL;
  1310. switch( typeId )
  1311. {
  1312. case kObjectTId:
  1313. case kArrayTId:
  1314. if((rp = _cmJsonCreateNode2(h,typeId,parentPtr)) == NULL)
  1315. rc = p->rc;
  1316. else
  1317. {
  1318. if( rpp != NULL )
  1319. *rpp = rp;
  1320. }
  1321. break;
  1322. case kPairTId: rc = _cmJsonCreatePair(p,parentPtr,sv,rpp); break;
  1323. case kIntTId: rc = _cmJsonCreateInt(p,parentPtr,iv,rpp); break;
  1324. case kRealTId: rc = _cmJsonCreateReal(p,parentPtr,dv,rpp); break;
  1325. case kTrueTId: rc = _cmJsonCreateBool(p,parentPtr,true,rpp); break;
  1326. case kFalseTId: rc = _cmJsonCreateBool(p,parentPtr,false,rpp); break;
  1327. case kNullTId: rc = _cmJsonCreateNull(p,parentPtr,rpp); break;
  1328. case kStringTId: rc = _cmJsonCreateString(p,parentPtr,sv,strlen(sv),rpp); break;
  1329. default:
  1330. assert(0);
  1331. break;
  1332. }
  1333. return rc;
  1334. }
  1335. cmJsonNode_t* cmJsonCreateObject( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1336. { return _cmJsonCreateNode2(h,kObjectTId,parentPtr); }
  1337. cmJsonNode_t* cmJsonCreateArray( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1338. { return _cmJsonCreateNode2(h,kArrayTId,parentPtr); }
  1339. cmJsonNode_t* cmJsonCreatePair( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* label )
  1340. {
  1341. cmJsonNode_t* np;
  1342. cmJs_t* p = _cmJsonHandleToPtr(h);
  1343. if((_cmJsonCreatePair(p,parentPtr,label,&np)) != kOkJsRC )
  1344. return NULL;
  1345. return np;
  1346. }
  1347. cmJsRC_t cmJsonCreateString( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue )
  1348. {
  1349. cmJs_t* p = _cmJsonHandleToPtr(h);
  1350. return _cmJsonCreateString( p, parentPtr, stringValue, strlen(stringValue),NULL);;
  1351. }
  1352. cmJsRC_t cmJsonCreateStringN(cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue, unsigned stringCharCnt )
  1353. {
  1354. cmJs_t* p = _cmJsonHandleToPtr(h);
  1355. return _cmJsonCreateString( p, parentPtr, stringValue, stringCharCnt,NULL);;
  1356. }
  1357. cmJsRC_t cmJsonCreateInt( cmJsonH_t h, cmJsonNode_t* parentPtr, int value )
  1358. {
  1359. cmJs_t* p = _cmJsonHandleToPtr(h);
  1360. return _cmJsonCreateInt( p, parentPtr, value, NULL );
  1361. }
  1362. cmJsRC_t cmJsonCreateReal( cmJsonH_t h, cmJsonNode_t* parentPtr, double value )
  1363. {
  1364. cmJs_t* p = _cmJsonHandleToPtr(h);
  1365. return _cmJsonCreateReal( p, parentPtr, value, NULL );
  1366. }
  1367. cmJsRC_t cmJsonCreateBool( cmJsonH_t h, cmJsonNode_t* parentPtr, bool value )
  1368. {
  1369. cmJs_t* p = _cmJsonHandleToPtr(h);
  1370. return _cmJsonCreateBool( p, parentPtr, value,NULL );
  1371. }
  1372. cmJsRC_t cmJsonCreateNull( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1373. {
  1374. cmJs_t* p = _cmJsonHandleToPtr(h);
  1375. return _cmJsonCreateNull( p, parentPtr, NULL );
  1376. }
  1377. cmJsRC_t cmJsonCreateStringArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const char** value )
  1378. {
  1379. cmJs_t* p = _cmJsonHandleToPtr(h);
  1380. cmJsRC_t rc = kOkJsRC;
  1381. cmJsonNode_t* np;
  1382. unsigned i;
  1383. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1384. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'bool' array.");
  1385. for(i=0; i<n; ++i)
  1386. if((rc = cmJsonCreateString(h,np,value[i])) != kOkJsRC )
  1387. return _cmJsonError(p,rc,"Unable to create 'bool' array element at index %i.",i);
  1388. return rc;
  1389. }
  1390. cmJsRC_t cmJsonCreateIntArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const int* value )
  1391. {
  1392. cmJs_t* p = _cmJsonHandleToPtr(h);
  1393. cmJsRC_t rc = kOkJsRC;
  1394. cmJsonNode_t* np;
  1395. unsigned i;
  1396. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1397. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'int' array.");
  1398. for(i=0; i<n; ++i)
  1399. if((rc = cmJsonCreateInt(h,np,value[i])) != kOkJsRC )
  1400. return _cmJsonError(p,rc,"Unable to create 'int' array element at index %i.",i);
  1401. return rc;
  1402. }
  1403. cmJsRC_t cmJsonCreateRealArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const double* value )
  1404. {
  1405. cmJs_t* p = _cmJsonHandleToPtr(h);
  1406. cmJsRC_t rc = kOkJsRC;
  1407. cmJsonNode_t* np;
  1408. unsigned i;
  1409. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1410. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'real' array.");
  1411. for(i=0; i<n; ++i)
  1412. if((rc = cmJsonCreateReal(h,np,value[i])) != kOkJsRC )
  1413. return _cmJsonError(p,rc,"Unable to create 'real' array element at index %i.",i);
  1414. return rc;
  1415. }
  1416. cmJsRC_t cmJsonCreateBoolArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const bool* value )
  1417. {
  1418. cmJs_t* p = _cmJsonHandleToPtr(h);
  1419. cmJsRC_t rc = kOkJsRC;
  1420. cmJsonNode_t* np;
  1421. unsigned i;
  1422. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1423. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'bool' array.");
  1424. for(i=0; i<n; ++i)
  1425. if((rc = cmJsonCreateBool(h,np,value[i])) != kOkJsRC )
  1426. return _cmJsonError(p,rc,"Unable to create 'bool' array element at index %i.",i);
  1427. return rc;
  1428. }
  1429. cmJsRC_t cmJsonSetInt( cmJsonH_t h, cmJsonNode_t* np, int ival )
  1430. {
  1431. cmJs_t* p = _cmJsonHandleToPtr(h);
  1432. if( np->typeId != kIntTId )
  1433. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'int' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1434. np->u.intVal = ival;
  1435. p->modifiedFl = true;
  1436. return kOkJsRC;
  1437. }
  1438. cmJsRC_t cmJsonSetReal( cmJsonH_t h, cmJsonNode_t * np, double rval )
  1439. {
  1440. cmJs_t* p = _cmJsonHandleToPtr(h);
  1441. if( np->typeId != kRealTId )
  1442. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'real' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1443. np->u.realVal = rval;
  1444. p->modifiedFl = true;
  1445. return kOkJsRC;
  1446. }
  1447. cmJsRC_t cmJsonSetBool( cmJsonH_t h, cmJsonNode_t * np, bool bval )
  1448. {
  1449. cmJs_t* p = _cmJsonHandleToPtr(h);
  1450. if( np->typeId == kTrueTId || np->typeId==kFalseTId )
  1451. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'bool' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1452. np->u.boolVal = bval;
  1453. p->modifiedFl = true;
  1454. return kOkJsRC;
  1455. }
  1456. cmJsRC_t cmJsonSetString( cmJsonH_t h, cmJsonNode_t* np, const char* sval )
  1457. {
  1458. cmJs_t* p = _cmJsonHandleToPtr(h);
  1459. if( np->typeId != kStringTId )
  1460. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'string' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1461. unsigned sn = strlen(sval);
  1462. if( np->u.stringVal != NULL && strlen(np->u.stringVal) <= sn )
  1463. strcpy(np->u.stringVal,sval);
  1464. else
  1465. return _cmJsonSetString(p,np,sval,sn);
  1466. p->modifiedFl = true;
  1467. return kOkJsRC;
  1468. }
  1469. cmJsonNode_t* cmJsonInsertPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned typeId, const char* sv, int iv, double dv )
  1470. {
  1471. assert( objectNodePtr->typeId == kObjectTId );
  1472. cmJsRC_t rc;
  1473. cmJsonNode_t* pairNodePtr;
  1474. cmJs_t* p = _cmJsonHandleToPtr(h);
  1475. if((rc = _cmJsonCreatePair(p,objectNodePtr,label,&pairNodePtr)) != kOkJsRC )
  1476. return NULL;
  1477. assert( pairNodePtr != NULL );
  1478. if((rc = cmJsonCreate(h,pairNodePtr,typeId,sv,iv,dv,NULL)) != kOkJsRC )
  1479. return NULL;
  1480. return pairNodePtr;
  1481. }
  1482. cmJsonNode_t* cmJsonInsertPairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1483. {
  1484. assert( objectNodePtr->typeId == kObjectTId );
  1485. cmJsonNode_t* pairNodePtr;
  1486. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1487. return NULL;
  1488. return cmJsonCreateObject(h,pairNodePtr);
  1489. }
  1490. cmJsonNode_t* cmJsonInsertPairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1491. {
  1492. assert( objectNodePtr->typeId == kObjectTId );
  1493. cmJsonNode_t* pairNodePtr;
  1494. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1495. return NULL;
  1496. return cmJsonCreateArray(h,pairNodePtr);
  1497. }
  1498. cmJsonNode_t* cmJsonInsertPairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* pairLabel )
  1499. {
  1500. assert( objectNodePtr->typeId == kObjectTId );
  1501. cmJsonNode_t* pairNodePtr;
  1502. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1503. return NULL;
  1504. return cmJsonCreatePair(h,pairNodePtr,pairLabel);
  1505. }
  1506. cmJsRC_t cmJsonInsertPairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, int intVal )
  1507. {
  1508. assert( objectNodePtr->typeId == kObjectTId );
  1509. cmJsonNode_t* pairNodePtr;
  1510. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1511. return cmJsonErrorCode(h);
  1512. return cmJsonCreateInt(h,pairNodePtr,intVal);
  1513. }
  1514. cmJsRC_t cmJsonInsertPairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, double realVal )
  1515. {
  1516. assert( objectNodePtr->typeId == kObjectTId );
  1517. cmJsonNode_t* pairNodePtr;
  1518. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1519. return cmJsonErrorCode(h);
  1520. return cmJsonCreateReal(h,pairNodePtr,realVal);
  1521. }
  1522. cmJsRC_t cmJsonInsertPairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* stringVal )
  1523. { return cmJsonInsertPairStringN(h,objectNodePtr,label,stringVal,stringVal==NULL ? 0 : strlen(stringVal)); }
  1524. cmJsRC_t cmJsonInsertPairStringN( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* stringVal, unsigned stringCharCnt )
  1525. {
  1526. assert( objectNodePtr->typeId == kObjectTId );
  1527. cmJsonNode_t* pairNodePtr;
  1528. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1529. return cmJsonErrorCode(h);
  1530. return cmJsonCreateStringN(h,pairNodePtr,stringVal,stringCharCnt);
  1531. }
  1532. cmJsRC_t cmJsonInsertPairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, bool boolVal )
  1533. {
  1534. assert( objectNodePtr->typeId == kObjectTId );
  1535. cmJsonNode_t* pairNodePtr;
  1536. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1537. return cmJsonErrorCode(h);
  1538. return cmJsonCreateBool(h,pairNodePtr,boolVal);
  1539. }
  1540. cmJsRC_t cmJsonInsertPairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1541. {
  1542. assert( objectNodePtr->typeId == kObjectTId );
  1543. cmJsonNode_t* pairNodePtr;
  1544. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1545. return cmJsonErrorCode(h);
  1546. return cmJsonCreateNull(h,pairNodePtr);
  1547. }
  1548. cmJsRC_t cmJsonInsertPairIntArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values )
  1549. {
  1550. assert( objectNodePtr->typeId == kObjectTId );
  1551. cmJsonNode_t* pairNodePtr;
  1552. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1553. return cmJsonErrorCode(h);
  1554. return cmJsonCreateIntArray(h,pairNodePtr,n,values);
  1555. }
  1556. cmJsRC_t cmJsonInsertPairRealArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values )
  1557. {
  1558. assert( objectNodePtr->typeId == kObjectTId );
  1559. cmJsonNode_t* pairNodePtr;
  1560. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1561. return cmJsonErrorCode(h);
  1562. return cmJsonCreateRealArray(h,pairNodePtr,n,values);
  1563. }
  1564. cmJsRC_t cmJsonInsertPairStringArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values )
  1565. {
  1566. assert( objectNodePtr->typeId == kObjectTId );
  1567. cmJsonNode_t* pairNodePtr;
  1568. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1569. return cmJsonErrorCode(h);
  1570. return cmJsonCreateStringArray(h,pairNodePtr,n,values);
  1571. }
  1572. cmJsRC_t cmJsonInsertPairBoolArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values )
  1573. {
  1574. assert( objectNodePtr->typeId == kObjectTId );
  1575. cmJsonNode_t* pairNodePtr;
  1576. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1577. return cmJsonErrorCode(h);
  1578. return cmJsonCreateBoolArray(h,pairNodePtr,n,values);
  1579. }
  1580. cmJsonNode_t* cmJsonInsertPairIntArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values )
  1581. {
  1582. assert( objectNodePtr->typeId == kObjectTId );
  1583. cmJsonNode_t* pairNodePtr;
  1584. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1585. if( cmJsonCreateIntArray(h,pairNodePtr,n,values) != kOkJsRC )
  1586. return NULL;
  1587. return pairNodePtr;
  1588. }
  1589. cmJsonNode_t* cmJsonInsertPairRealArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values )
  1590. {
  1591. assert( objectNodePtr->typeId == kObjectTId );
  1592. cmJsonNode_t* pairNodePtr;
  1593. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1594. if( cmJsonCreateRealArray(h,pairNodePtr,n,values) != kOkJsRC )
  1595. return NULL;
  1596. return pairNodePtr;
  1597. }
  1598. cmJsonNode_t* cmJsonInsertPairStringArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values )
  1599. {
  1600. assert( objectNodePtr->typeId == kObjectTId );
  1601. cmJsonNode_t* pairNodePtr;
  1602. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1603. if( cmJsonCreateStringArray(h,pairNodePtr,n,values) != kOkJsRC )
  1604. return NULL;
  1605. return pairNodePtr;
  1606. }
  1607. cmJsonNode_t* cmJsonInsertPairBoolArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values )
  1608. {
  1609. assert( objectNodePtr->typeId == kObjectTId );
  1610. cmJsonNode_t* pairNodePtr;
  1611. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1612. if( cmJsonCreateBoolArray(h,pairNodePtr,n,values) != kOkJsRC )
  1613. return NULL;
  1614. return pairNodePtr;
  1615. }
  1616. cmJsRC_t _cmJsonInsertOrReplacePair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, unsigned newTypeId, const char* sv, int iv, double dv, cmJsonNode_t* nv, bool insertFl, cmJsonNode_t** retNodePtrPtr )
  1617. {
  1618. cmJsRC_t rc = kOkJsRC;
  1619. cmJs_t* p = _cmJsonHandleToPtr(h);
  1620. cmJsonNode_t* valNodePtr = NULL;
  1621. cmJsonNode_t* pairNodePtr = NULL;
  1622. assert( objectNodePtr->typeId == kObjectTId );
  1623. if( retNodePtrPtr != NULL )
  1624. *retNodePtrPtr = NULL;
  1625. // if a matching pair was not found ....
  1626. if(( valNodePtr = cmJsonFindValue(h,label,objectNodePtr,matchTypeMask)) == NULL )
  1627. {
  1628. // ... and insertion is not allowed then return error
  1629. if( insertFl == false )
  1630. return kNodeNotFoundJsRC;
  1631. // ... otherwise insert a new pair and return a pointer to it
  1632. pairNodePtr = cmJsonInsertPair(h,objectNodePtr,label,newTypeId,sv,iv,dv);
  1633. goto errLabel;
  1634. }
  1635. // ... otherwise a match was found to at least the pair label.
  1636. // If matchTypeMask was set to kInvalidTId then the type id
  1637. // of the found pair may not be the same as newTypeId. To handle
  1638. // this case we proceed by first deallocating all resources held
  1639. // by the found node and then proceeding by either creating
  1640. // deleting the found node and creating a replacement node
  1641. // (object,array,pair) or forcing the found node to a new
  1642. // type (int,real,true,false,string,null).
  1643. assert( valNodePtr != NULL);
  1644. pairNodePtr = valNodePtr->ownerPtr;
  1645. // release any resources held by the found node
  1646. switch( valNodePtr->typeId )
  1647. {
  1648. case kObjectTId:
  1649. case kArrayTId:
  1650. case kPairTId:
  1651. // remove the pair value node and replace it with a 'null' node.
  1652. if((rc =_cmJsonRemoveNode( p, valNodePtr, true, true )) != kOkJsRC )
  1653. goto errLabel;
  1654. break;
  1655. case kStringTId:
  1656. // deallocate string memory
  1657. _cmJsonSetString( p, valNodePtr, NULL, 0 );
  1658. break;
  1659. }
  1660. // relace the found node with the new node
  1661. switch( newTypeId )
  1662. {
  1663. case kObjectTId:
  1664. case kArrayTId:
  1665. case kPairTId:
  1666. {
  1667. cmJsonNode_t* newValueNodePtr = NULL;
  1668. // remove the current pair value ....
  1669. if((rc =_cmJsonRemoveNode( p, valNodePtr, true, false )) != kOkJsRC )
  1670. goto errLabel;
  1671. // new pair nodes should have the pair label in 'sv'
  1672. assert( newTypeId!=kPairTId || (newTypeId==kPairTId && sv != NULL ));
  1673. // if no new value was given or the new value is a pair then ...
  1674. if( nv == NULL || newTypeId == kPairTId )
  1675. {
  1676. // ... create a new blank array,obj or pair
  1677. if((rc = cmJsonCreate( h, pairNodePtr, newTypeId, sv, 0, 0, &newValueNodePtr )) != kOkJsRC )
  1678. goto errLabel;
  1679. }
  1680. // if the new value is a pair and no value node was given then set the
  1681. // new pairs value node to NULL
  1682. if( nv == NULL && newTypeId == kPairTId )
  1683. {
  1684. if((rc = _cmJsonCreateNull(p,newValueNodePtr,NULL)) != kOkJsRC )
  1685. goto errLabel;
  1686. }
  1687. // if a new value node was given
  1688. if( nv != NULL )
  1689. {
  1690. // the new child node should not already be linked to a parent
  1691. assert( nv->ownerPtr == NULL );
  1692. // if the new value is an obj or array then the new
  1693. // value node type id should be the same
  1694. assert( newTypeId==kPairTId || newTypeId==nv->typeId );
  1695. //
  1696. cmJsonNode_t* pp = newTypeId==kPairTId ? newValueNodePtr : pairNodePtr;
  1697. assert( pp->typeId == kPairTId );
  1698. // link in the child to the pair
  1699. if((rc = _cmJsonLinkInChild(p,pp,nv)) != kOkJsRC )
  1700. goto errLabel;
  1701. }
  1702. }
  1703. break;
  1704. // All cases below follow the same pattern:
  1705. // 1. Set the type id to the replacement value type id
  1706. // 2. Assign the value to the node.
  1707. // This sequence is safe because all resources were freed
  1708. // by the earlier switch.
  1709. case kStringTId:
  1710. valNodePtr->typeId = kStringTId;
  1711. _cmJsonSetString( p, valNodePtr, sv, strlen(sv) );
  1712. break;
  1713. case kIntTId:
  1714. valNodePtr->typeId =kIntTId;
  1715. valNodePtr->u.intVal = iv;
  1716. break;
  1717. case kRealTId:
  1718. valNodePtr->typeId = kRealTId;
  1719. valNodePtr->u.realVal = dv;
  1720. break;
  1721. case kTrueTId:
  1722. valNodePtr->typeId = kTrueTId;
  1723. valNodePtr->u.boolVal = true;
  1724. break;
  1725. case kFalseTId:
  1726. valNodePtr->typeId = kFalseTId;
  1727. valNodePtr->u.boolVal = false;
  1728. break;
  1729. case kNullTId:
  1730. valNodePtr->typeId = kNullTId;
  1731. break;
  1732. default:
  1733. { assert(0); }
  1734. }
  1735. errLabel:
  1736. if( rc == kOkJsRC )
  1737. {
  1738. if( retNodePtrPtr != NULL )
  1739. *retNodePtrPtr = pairNodePtr;
  1740. p->modifiedFl = true;
  1741. }
  1742. return rc;
  1743. }
  1744. cmJsonNode_t* cmJsonInsertOrReplacePair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, unsigned typeId, const char* sv, int iv, double dv, cmJsonNode_t* nv )
  1745. {
  1746. cmJsonNode_t* retNodePtr = NULL;
  1747. _cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,typeId,sv,iv,dv,nv,true,&retNodePtr );
  1748. return retNodePtr;
  1749. }
  1750. cmJsonNode_t* cmJsonInsertOrReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newObjNodePtr )
  1751. {
  1752. cmJsonNode_t* pairPtr;
  1753. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kObjectTId,NULL,0,0,newObjNodePtr)) == NULL )
  1754. return NULL;
  1755. return pairPtr->u.childPtr->siblingPtr;
  1756. }
  1757. cmJsonNode_t* cmJsonInsertOrReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr )
  1758. {
  1759. cmJsonNode_t* pairPtr;
  1760. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kArrayTId,NULL,0,0,newArrayNodePtr)) == NULL )
  1761. return NULL;
  1762. return pairPtr->u.childPtr->siblingPtr;
  1763. }
  1764. cmJsonNode_t* cmJsonInsertOrReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValNodePtr )
  1765. {
  1766. cmJsonNode_t* pairPtr;
  1767. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kPairTId,newPairLabel,0,0,newPairValNodePtr)) == NULL )
  1768. return NULL;
  1769. return pairPtr->u.childPtr->siblingPtr;
  1770. }
  1771. cmJsRC_t cmJsonInsertOrReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal )
  1772. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kIntTId,NULL,intVal,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1773. cmJsRC_t cmJsonInsertOrReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal )
  1774. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kRealTId,NULL,0,realVal,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1775. cmJsRC_t cmJsonInsertOrReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* stringVal )
  1776. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kStringTId,stringVal,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1777. cmJsRC_t cmJsonInsertOrReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal )
  1778. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,boolVal ? kTrueTId : kFalseTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1779. cmJsRC_t cmJsonInsertOrReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask )
  1780. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kNullTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1781. cmJsonNode_t* cmJsonReplacePair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, unsigned newTypeId, const char* sv, int iv, double dv, cmJsonNode_t* nv )
  1782. {
  1783. cmJsonNode_t* retNodePtr = NULL;
  1784. _cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,newTypeId,sv,iv,dv,nv,false,&retNodePtr );
  1785. return retNodePtr;
  1786. }
  1787. cmJsonNode_t* cmJsonReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newPairNodePtr )
  1788. {
  1789. cmJsonNode_t* pairPtr;
  1790. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kObjectTId,NULL,0,0,newPairNodePtr)) == NULL )
  1791. return NULL;
  1792. return pairPtr->u.childPtr->siblingPtr;
  1793. }
  1794. cmJsonNode_t* cmJsonReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr )
  1795. {
  1796. cmJsonNode_t* pairPtr;
  1797. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kArrayTId,NULL,0,0,newArrayNodePtr)) == NULL )
  1798. return NULL;
  1799. return pairPtr->u.childPtr->siblingPtr;
  1800. }
  1801. cmJsonNode_t* cmJsonReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValNodePtr )
  1802. {
  1803. cmJsonNode_t* pairPtr;
  1804. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kPairTId,newPairLabel,0,0,newPairValNodePtr)) == NULL )
  1805. return NULL;
  1806. return pairPtr->u.childPtr->siblingPtr;
  1807. }
  1808. cmJsRC_t cmJsonReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal )
  1809. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kIntTId,NULL,intVal,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1810. cmJsRC_t cmJsonReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal )
  1811. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kRealTId,NULL,0,realVal,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1812. cmJsRC_t cmJsonReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* stringVal )
  1813. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kStringTId,stringVal,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1814. cmJsRC_t cmJsonReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal )
  1815. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,boolVal ? kTrueTId : kFalseTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1816. cmJsRC_t cmJsonReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask )
  1817. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kNullTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1818. cmJsRC_t cmJsonVInsertPairs( cmJsonH_t h, cmJsonNode_t* objNodePtr, va_list vl )
  1819. {
  1820. cmJsRC_t rc = kOkJsRC;
  1821. assert( objNodePtr->typeId == kObjectTId );
  1822. const char* label;
  1823. while( ((label = va_arg(vl,const char*)) != NULL) && (rc == kOkJsRC) )
  1824. {
  1825. unsigned sel = va_arg(vl,unsigned);
  1826. switch( sel )
  1827. {
  1828. case kObjectTId:
  1829. if( cmJsonInsertPairObject(h,objNodePtr,label) == NULL )
  1830. rc = cmJsonErrorCode(h);
  1831. break;
  1832. case kArrayTId:
  1833. if( cmJsonInsertPairArray(h,objNodePtr,label) == NULL )
  1834. rc = cmJsonErrorCode(h);
  1835. break;
  1836. case kPairTId:
  1837. if( cmJsonInsertPairPair(h,objNodePtr,label, va_arg(vl,const char*)) == NULL )
  1838. rc = cmJsonErrorCode(h);
  1839. break;
  1840. case kIntTId:
  1841. rc = cmJsonInsertPairInt(h,objNodePtr,label, va_arg(vl,int) );
  1842. break;
  1843. case kRealTId:
  1844. rc = cmJsonInsertPairReal(h,objNodePtr,label, va_arg(vl,double) );
  1845. break;
  1846. case kStringTId:
  1847. rc = cmJsonInsertPairString(h,objNodePtr,label, va_arg(vl,const char *) );
  1848. break;
  1849. case kTrueTId:
  1850. case kFalseTId:
  1851. case kBoolTId:
  1852. rc = cmJsonInsertPairBool(h,objNodePtr,label, va_arg(vl,int) );
  1853. break;
  1854. case kNullTId:
  1855. rc = cmJsonInsertPairNull(h,objNodePtr,label );
  1856. break;
  1857. default:
  1858. // missing terminating NULL on the var args list???
  1859. assert(0);
  1860. break;
  1861. }
  1862. }
  1863. return rc;
  1864. }
  1865. cmJsRC_t cmJsonInsertPairs( cmJsonH_t h, cmJsonNode_t* objectNodePtr, ... )
  1866. {
  1867. va_list vl;
  1868. va_start(vl,objectNodePtr);
  1869. cmJsRC_t rc = cmJsonVInsertPairs(h,objectNodePtr,vl);
  1870. va_end(vl);
  1871. return rc;
  1872. }
  1873. cmJsonNode_t* cmJsonVCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, va_list vl )
  1874. {
  1875. cmJsonNode_t* np;
  1876. if((np = cmJsonCreateObject(h,parentPtr)) == NULL)
  1877. return NULL;
  1878. if( cmJsonVInsertPairs(h,np,vl) != kOkJsRC )
  1879. {
  1880. cmJsonRemoveNode(h,np,true);
  1881. return NULL;
  1882. }
  1883. return np;
  1884. }
  1885. cmJsonNode_t* cmJsonCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, ... )
  1886. {
  1887. va_list vl;
  1888. va_start(vl,parentPtr);
  1889. cmJsonNode_t* np = cmJsonVCreateFilledObject(h,parentPtr,vl);
  1890. va_end(vl);
  1891. return np;
  1892. }
  1893. void _cmJsonFreeNode( cmJs_t* p, cmJsonNode_t* np )
  1894. {
  1895. switch( np->typeId )
  1896. {
  1897. case kObjectTId:
  1898. case kPairTId:
  1899. case kArrayTId:
  1900. {
  1901. cmJsonNode_t* cnp = np->u.childPtr;
  1902. while( cnp != NULL )
  1903. {
  1904. cmJsonNode_t* nnp = cnp->siblingPtr;
  1905. _cmJsonFreeNode(p,cnp);
  1906. cnp = nnp;
  1907. }
  1908. }
  1909. break;
  1910. case kStringTId:
  1911. if( np->u.stringVal != NULL )
  1912. cmLHeapFree(p->heapH,np->u.stringVal);
  1913. break;
  1914. }
  1915. cmLHeapFree(p->heapH,np);
  1916. }
  1917. cmJsRC_t _cmJsonRemoveNode( cmJs_t* p, cmJsonNode_t* np, bool freeFl, bool balancePairsFl )
  1918. {
  1919. if(np == NULL )
  1920. return kOkJsRC;
  1921. cmJsonNode_t* parentPtr = np->ownerPtr;
  1922. // if np is the root ...
  1923. if( np == p->rootPtr )
  1924. {
  1925. // ... we only need to set the root to null to remove the node.
  1926. p->rootPtr = NULL;
  1927. }
  1928. else
  1929. {
  1930. if( parentPtr != NULL )
  1931. {
  1932. // get the parents first child
  1933. cmJsonNode_t* cnp = parentPtr->u.childPtr;
  1934. // if np is the first child then make the second child the first child
  1935. if( cnp == np )
  1936. {
  1937. if( parentPtr->typeId == kPairTId )
  1938. return _cmJsonError( p, kCannotRemoveLabelJsRC, "Cannot remove pair label nodes because this would invalidate the tree structure.");
  1939. parentPtr->u.childPtr = cnp->siblingPtr;
  1940. }
  1941. else
  1942. {
  1943. // otherwise unlink it from the child chain
  1944. while( cnp != NULL )
  1945. {
  1946. if( cnp->siblingPtr == np )
  1947. {
  1948. cnp->siblingPtr = np->siblingPtr;
  1949. // if the parent is a pair then the removed node is a
  1950. // 'pair value' which must be replaced with a null node in order
  1951. // to maintain a valid tree.
  1952. if( (parentPtr->typeId == kPairTId) && balancePairsFl)
  1953. _cmJsonCreateNull( p, parentPtr, NULL );
  1954. break;
  1955. }
  1956. cnp = cnp->siblingPtr;
  1957. }
  1958. assert( cnp != NULL );
  1959. }
  1960. }
  1961. }
  1962. // if the memory assoc'd with the removed node should be released
  1963. if( freeFl )
  1964. {
  1965. _cmJsonFreeNode(p,np);
  1966. /*
  1967. if( np->typeId == kStringTId )
  1968. {
  1969. cmLHeapFree(p->heapH,np->u.stringVal);
  1970. np->u.stringVal = NULL;
  1971. }
  1972. cmLHeapFree(p->heapH,np);
  1973. */
  1974. }
  1975. p->modifiedFl = true;
  1976. return kOkJsRC;
  1977. }
  1978. cmJsRC_t cmJsonRemoveNode( cmJsonH_t h, cmJsonNode_t* np, bool freeFl )
  1979. {
  1980. cmJs_t* p = _cmJsonHandleToPtr(h);
  1981. return _cmJsonRemoveNode( p, np, freeFl, true );
  1982. }
  1983. cmJsRC_t _cmJsonValidateErr( cmJs_t* p, const char* text )
  1984. {
  1985. if( p != NULL )
  1986. return _cmJsonSyntaxError(p,text);
  1987. return kValidateFailJsRC;
  1988. }
  1989. // 'p' is optional. If 'p' is set to NULL the function will return kValidFailJsRC if the
  1990. // tree rooted at 'np' is invalid and will not print an error message.
  1991. cmJsRC_t _cmJsonValidateNode( cmJs_t* p, const cmJsonNode_t* np, const cmJsonNode_t* parentPtr )
  1992. {
  1993. cmJsRC_t rc = kOkJsRC;
  1994. if( parentPtr != np->ownerPtr )
  1995. return _cmJsonValidateErr(p,"A child->parent link does not agree with a parent->child link.");
  1996. if( parentPtr == NULL )
  1997. {
  1998. if( np->typeId != kArrayTId && np->typeId != kObjectTId )
  1999. return _cmJsonValidateErr(p,"Only 'array' and 'object' nodes may be the root element.");
  2000. }
  2001. else
  2002. {
  2003. if( parentPtr->typeId != kArrayTId && parentPtr->typeId != kObjectTId && parentPtr->typeId != kPairTId )
  2004. return _cmJsonValidateErr(p,"Parent nodes must be either 'object','array' or 'pair' nodes.");
  2005. }
  2006. switch( np->typeId )
  2007. {
  2008. case kPairTId:
  2009. if( cmJsonChildCount(np) != 2 )
  2010. return _cmJsonValidateErr(p,"'pair' nodes must have exactly two children.");
  2011. if( np->u.childPtr->typeId != kStringTId )
  2012. return _cmJsonValidateErr(p,"The first child of 'pair' nodes must be a 'string' node.");
  2013. // fall through
  2014. case kObjectTId:
  2015. case kArrayTId:
  2016. {
  2017. // validate each child node
  2018. cmJsonNode_t* cnp = np->u.childPtr;
  2019. while(cnp != NULL)
  2020. {
  2021. if( cnp->ownerPtr != np )
  2022. return _cmJsonValidateErr(p,"A parent->child pointer was not validated with a child->parent pointer.");
  2023. if( np->typeId == kObjectTId && cnp->typeId != kPairTId )
  2024. return _cmJsonValidateErr(p,"All 'object' child nodes must be 'pair' nodes.");
  2025. if((rc = _cmJsonValidateNode(p,cnp,np)) != kOkJsRC )
  2026. return rc;
  2027. cnp = cnp->siblingPtr;
  2028. }
  2029. }
  2030. break;
  2031. case kStringTId:
  2032. case kIntTId:
  2033. case kRealTId:
  2034. case kNullTId:
  2035. case kTrueTId:
  2036. case kFalseTId:
  2037. default:
  2038. break;
  2039. }
  2040. return rc;
  2041. }
  2042. cmJsRC_t cmJsonValidateTree( cmJsonH_t h )
  2043. {
  2044. cmJs_t* p = _cmJsonHandleToPtr(h);
  2045. return _cmJsonValidateNode(p,p->rootPtr,NULL);
  2046. }
  2047. cmJsRC_t cmJsonValidate( const cmJsonNode_t* np )
  2048. { return _cmJsonValidateNode(NULL,np,np->ownerPtr); }
  2049. cmJsonNode_t* _cmJsonDuplicateNode( cmJs_t* p, const cmJsonNode_t* np, cmJsonNode_t* parentPtr )
  2050. {
  2051. cmJsonNode_t* newParentPtr = NULL;
  2052. cmJsonNode_t* newNodePtr = NULL;
  2053. cmJsRC_t rc = kOkJsRC;
  2054. switch( np->typeId )
  2055. {
  2056. case kObjectTId:
  2057. case kArrayTId:
  2058. rc = _cmJsonCreateNode(p,parentPtr,np->typeId,&newParentPtr);
  2059. break;
  2060. case kPairTId:
  2061. rc = _cmJsonCreatePair(p,parentPtr,np->u.childPtr->u.stringVal,&newParentPtr);
  2062. break;
  2063. case kIntTId:
  2064. rc = _cmJsonCreateInt(p,parentPtr,np->u.intVal,&newNodePtr);
  2065. break;
  2066. case kRealTId:
  2067. rc = _cmJsonCreateReal(p,parentPtr,np->u.realVal,&newNodePtr);
  2068. break;
  2069. case kStringTId:
  2070. rc = _cmJsonCreateString(p,parentPtr,np->u.stringVal,strlen(np->u.stringVal),&newNodePtr);
  2071. break;
  2072. case kTrueTId:
  2073. case kFalseTId:
  2074. rc = _cmJsonCreateBool(p,parentPtr,np->u.boolVal,&newNodePtr);
  2075. break;
  2076. case kNullTId:
  2077. rc = _cmJsonCreateNull(p,parentPtr,&newNodePtr);
  2078. }
  2079. if( rc != kOkJsRC )
  2080. return NULL;
  2081. if( newParentPtr != NULL )
  2082. {
  2083. newNodePtr = newParentPtr;
  2084. cmJsonNode_t* cnp = np->u.childPtr;
  2085. if(np->typeId == kPairTId)
  2086. cnp = np->u.childPtr->siblingPtr;
  2087. while( cnp != NULL )
  2088. {
  2089. if( _cmJsonDuplicateNode(p,cnp,newParentPtr) != kOkJsRC )
  2090. return NULL;
  2091. cnp = cnp->siblingPtr;
  2092. }
  2093. }
  2094. return newNodePtr;
  2095. }
  2096. cmJsonNode_t* cmJsonDuplicateNode( cmJsonH_t h, const cmJsonNode_t* np, cmJsonNode_t* parentPtr )
  2097. {
  2098. cmJs_t* p = _cmJsonHandleToPtr(h);
  2099. assert( _cmJsonValidateNode(p,np,NULL) == kOkJsRC );
  2100. return _cmJsonDuplicateNode(p,np,parentPtr);
  2101. }
  2102. cmJsRC_t cmJsonMergeObjectNodes( cmJsonH_t h, cmJsonNode_t* dstObjNodePtr, const cmJsonNode_t* srcObjNodePtr )
  2103. {
  2104. assert( dstObjNodePtr!=NULL && dstObjNodePtr->typeId == kObjectTId );
  2105. assert( srcObjNodePtr!=NULL && srcObjNodePtr->typeId == kObjectTId );
  2106. cmJsRC_t rc = kOkJsRC;
  2107. cmJs_t* p = _cmJsonHandleToPtr(h);
  2108. cmJsonNode_t* cnp = NULL;
  2109. const cmJsonNode_t* snp = srcObjNodePtr->u.childPtr;
  2110. while( snp!=NULL && rc==kOkJsRC )
  2111. {
  2112. cmJsonNode_t* dnp;
  2113. assert( snp->typeId == kPairTId );
  2114. // if the src pair was not found in the dst object ...
  2115. if((rc = _cmJsonFindMemberValue(dstObjNodePtr, cmJsonPairLabel(snp), snp->u.childPtr->siblingPtr->typeId, &dnp )) != kOkJsRC )
  2116. {
  2117. cmJsonNode_t* newPairNodePtr;
  2118. // the only acceptable error is kNodeNotFoundJsRC
  2119. // (in particular we reject kNodeCannotCvtJsRC to avoid duplicating
  2120. // pairs with the same name but different types)
  2121. if( rc != kNodeNotFoundJsRC )
  2122. goto errLabel;
  2123. // create the new pair and attach it to the dst obj node
  2124. if((rc = _cmJsonCreatePair(p,dstObjNodePtr,cmJsonPairLabel(snp),&newPairNodePtr)) != kOkJsRC )
  2125. goto errLabel;
  2126. // duplicate the src pair value node and attcmh it to the new dst node
  2127. if( _cmJsonDuplicateNode(p,snp->u.childPtr->siblingPtr,newPairNodePtr) == NULL )
  2128. rc = p->rc;
  2129. // set kTempJsFl on the new node to use possible cleanup on error later
  2130. newPairNodePtr->typeId = cmSetFlag(newPairNodePtr->typeId,kTempJsFl);
  2131. }
  2132. snp = snp->siblingPtr;
  2133. }
  2134. errLabel:
  2135. // for each dst obj pair
  2136. cnp = dstObjNodePtr->u.childPtr;
  2137. while( cnp != NULL)
  2138. {
  2139. // if this child is a new node
  2140. if( cmIsFlag(cnp->typeId,kTempJsFl) )
  2141. {
  2142. // clear the temp fl
  2143. cnp->typeId = cmClrFlag(cnp->typeId,kTempJsFl);
  2144. // if there was an error remove all new pairs
  2145. if( rc != kOkJsRC )
  2146. cmJsonRemoveNode(h,cnp,true);
  2147. }
  2148. cnp = cnp->siblingPtr;
  2149. }
  2150. return rc;
  2151. }
  2152. void _cmJsonSerialCopy( cmJsSerial_t* rp, const void* sp, unsigned sn )
  2153. {
  2154. assert( rp->nextPtr + sn <= rp->endPtr );
  2155. memcpy(rp->nextPtr,sp,sn);
  2156. rp->nextPtr += sn;
  2157. }
  2158. cmJsRC_t _cmJsonSerializeNode( const cmJsonNode_t* np, cmJsSerial_t* rp )
  2159. {
  2160. cmJsRC_t rc = kOkJsRC;
  2161. // on the first pass rp->basePtr will be NULL so collect size information
  2162. // on the second pass rp->basePtr will be set so copy out data
  2163. // write the type id of this node
  2164. if( rp->basePtr != NULL )
  2165. _cmJsonSerialCopy(rp,&np->typeId,sizeof(np->typeId));
  2166. else
  2167. rp->hdr.byteCnt += sizeof(np->typeId);
  2168. rp->hdr.nodeCnt++;
  2169. switch( np->typeId )
  2170. {
  2171. // write the child count
  2172. case kObjectTId:
  2173. case kArrayTId:
  2174. if( rp->basePtr == NULL )
  2175. rp->hdr.byteCnt += sizeof(unsigned);
  2176. else
  2177. {
  2178. unsigned n = cmJsonChildCount( np );
  2179. _cmJsonSerialCopy(rp,&n,sizeof(unsigned));
  2180. }
  2181. // fall through
  2182. case kPairTId:
  2183. {
  2184. cmJsonNode_t* cnp = np->u.childPtr;
  2185. while(cnp != NULL )
  2186. {
  2187. _cmJsonSerializeNode(cnp,rp);
  2188. cnp = cnp->siblingPtr;
  2189. }
  2190. }
  2191. break;
  2192. case kStringTId:
  2193. // write the string contents
  2194. if( rp->basePtr == NULL )
  2195. rp->hdr.byteCnt += strlen(np->u.stringVal) + 1;
  2196. else
  2197. _cmJsonSerialCopy( rp, np->u.stringVal, strlen(np->u.stringVal)+1 );
  2198. break;
  2199. case kIntTId:
  2200. // write the int value
  2201. if( rp->basePtr == NULL )
  2202. rp->hdr.byteCnt += sizeof(np->u.intVal);
  2203. else
  2204. _cmJsonSerialCopy(rp,&np->u.intVal,sizeof(np->u.intVal));
  2205. break;
  2206. case kRealTId:
  2207. // write the real value
  2208. if( rp->basePtr == NULL )
  2209. rp->hdr.byteCnt += sizeof(np->u.realVal);
  2210. else
  2211. _cmJsonSerialCopy(rp, &np->u.realVal, sizeof(np->u.realVal));
  2212. break;
  2213. }
  2214. return rc;
  2215. }
  2216. unsigned cmJsonSerialByteCount( const cmJsonNode_t* np )
  2217. {
  2218. cmJsRC_t rc;
  2219. cmJsSerial_t vr;
  2220. memset(&vr,0,sizeof(vr));
  2221. // make a first pass to determine the size of the buffer
  2222. if((rc = _cmJsonSerializeNode(np,&vr)) != kOkJsRC )
  2223. return rc;
  2224. // increment the buffer size to include the buffer header
  2225. return (4*sizeof(unsigned)) + vr.hdr.byteCnt;
  2226. }
  2227. cmJsRC_t cmJsonSerialize( const cmJsonNode_t* np, void* buf, unsigned bufByteCnt )
  2228. {
  2229. cmJsRC_t rc = kOkJsRC;
  2230. cmJsSerial_t vr;
  2231. memset(&vr,0,sizeof(vr));
  2232. // setup the serial buffer
  2233. vr.hdr.id = 'json';
  2234. vr.hdr.byteCnt = bufByteCnt - (2*sizeof(unsigned));
  2235. vr.hdr.version = 0;
  2236. vr.basePtr = buf;
  2237. vr.nextPtr = vr.basePtr;
  2238. vr.endPtr = vr.basePtr + bufByteCnt;
  2239. // write the header recd
  2240. _cmJsonSerialCopy(&vr, &vr.hdr.id, sizeof(unsigned));
  2241. _cmJsonSerialCopy(&vr, &vr.hdr.byteCnt, sizeof(unsigned));
  2242. _cmJsonSerialCopy(&vr, &vr.hdr.nodeCnt, sizeof(unsigned));
  2243. _cmJsonSerialCopy(&vr, &vr.hdr.version, sizeof(unsigned));
  2244. // copy the node data into the serial buffer
  2245. if((rc = _cmJsonSerializeNode(np,&vr)) != kOkJsRC )
  2246. return rc;
  2247. vr.basePtr = buf;
  2248. vr.nextPtr = vr.basePtr;
  2249. _cmJsonSerialCopy(&vr, &vr.hdr.id, sizeof(unsigned));
  2250. _cmJsonSerialCopy(&vr, &vr.hdr.byteCnt, sizeof(unsigned));
  2251. _cmJsonSerialCopy(&vr, &vr.hdr.nodeCnt, sizeof(unsigned));
  2252. return rc;
  2253. }
  2254. cmJsRC_t cmJsonSerializeTree( cmJsonH_t h, const cmJsonNode_t* np, void** bufPtrPtr, unsigned* bufByteCntPtr)
  2255. {
  2256. cmJsRC_t rc;
  2257. cmJsSerial_t vr;
  2258. memset(&vr,0,sizeof(vr));
  2259. cmJs_t* p = _cmJsonHandleToPtr(h);
  2260. assert( bufPtrPtr != NULL && bufByteCntPtr != NULL );
  2261. *bufPtrPtr = NULL;
  2262. *bufByteCntPtr = 0;
  2263. if( np == NULL )
  2264. np = p->rootPtr;
  2265. // validate the tree
  2266. if((rc = _cmJsonValidateNode(p,np,np->ownerPtr)) != kOkJsRC )
  2267. return rc;
  2268. // increment the buffer size to include the buffer header
  2269. p->serialByteCnt = cmJsonSerialByteCount(np);
  2270. // allocate the serial buffer memory
  2271. p->serialBufPtr = cmMemResize( char, p->serialBufPtr, p->serialByteCnt );
  2272. // serialize the tree
  2273. if((rc = cmJsonSerialize(np, p->serialBufPtr, p->serialByteCnt )) != kOkJsRC )
  2274. return rc;
  2275. *bufPtrPtr = p->serialBufPtr;
  2276. *bufByteCntPtr = p->serialByteCnt;
  2277. return rc;
  2278. }
  2279. const void* _cmJsonDeserialAdv( cmJsDeserial_t* rp, unsigned n )
  2280. {
  2281. const void* vp = rp->nextPtr;
  2282. rp->nextPtr += n;
  2283. assert(rp->nextPtr <= rp->endPtr);
  2284. assert(rp->nodeIdx <= rp->nodeCnt);
  2285. return vp;
  2286. }
  2287. int _cmJsonDeserialInt( cmJsDeserial_t* rp )
  2288. { return *(const int*)_cmJsonDeserialAdv(rp,sizeof(int)); }
  2289. unsigned _cmJsonDeserialUint( cmJsDeserial_t* rp )
  2290. { return *(const unsigned*)_cmJsonDeserialAdv(rp,sizeof(unsigned)); }
  2291. double _cmJsonDeserialReal( cmJsDeserial_t* rp )
  2292. { return *(const double*)_cmJsonDeserialAdv(rp,sizeof(double)); }
  2293. cmJsRC_t _cmJsonDeserializeNode( cmJs_t* p, cmJsonNode_t* parentPtr, cmJsDeserial_t* rp )
  2294. {
  2295. cmJsRC_t rc = kOkJsRC;
  2296. unsigned typeId = _cmJsonDeserialUint(rp);
  2297. unsigned childCnt = 0;
  2298. cmJsonNode_t* newParentPtr = NULL;
  2299. rp->nodeIdx++;
  2300. switch( typeId )
  2301. {
  2302. case kPairTId:
  2303. rc = _cmJsonCreateNode(p,parentPtr,typeId,&newParentPtr);
  2304. childCnt = 2;
  2305. break;
  2306. case kObjectTId:
  2307. case kArrayTId:
  2308. rc = _cmJsonCreateNode(p,parentPtr,typeId,&newParentPtr);
  2309. childCnt = _cmJsonDeserialUint(rp);
  2310. break;
  2311. case kStringTId:
  2312. {
  2313. unsigned sn = strlen(rp->nextPtr);
  2314. rc = _cmJsonCreateString( p, parentPtr, rp->nextPtr, sn,NULL);
  2315. _cmJsonDeserialAdv(rp,sn+1);
  2316. }
  2317. break;
  2318. case kIntTId:
  2319. {
  2320. int v = _cmJsonDeserialInt(rp);
  2321. rc = _cmJsonCreateInt( p, parentPtr, v, NULL );
  2322. }
  2323. break;
  2324. case kRealTId:
  2325. {
  2326. double v = _cmJsonDeserialReal(rp);
  2327. rc = _cmJsonCreateReal( p, parentPtr, v, NULL );
  2328. }
  2329. break;
  2330. case kNullTId:
  2331. rc = _cmJsonCreateNull( p, parentPtr, NULL );
  2332. break;
  2333. case kTrueTId:
  2334. rc = _cmJsonCreateBool( p, parentPtr, true, NULL );
  2335. break;
  2336. case kFalseTId:
  2337. rc = _cmJsonCreateBool( p, parentPtr, false, NULL );
  2338. break;
  2339. default:
  2340. assert(0);
  2341. break;
  2342. }
  2343. if( rc != kOkJsRC )
  2344. return rc;
  2345. // if the current node is a parent
  2346. if( childCnt > 0 )
  2347. {
  2348. unsigned i;
  2349. assert( newParentPtr != NULL );
  2350. for(i=0; i<childCnt; ++i)
  2351. if((rc= _cmJsonDeserializeNode( p, newParentPtr, rp )) != kOkJsRC )
  2352. return rc;
  2353. }
  2354. return rc;
  2355. }
  2356. cmJsRC_t _cmJsonDeserialize( cmJs_t* p, const void* vbuf, cmJsonNode_t* altRootPtr )
  2357. {
  2358. cmJsDeserial_t r;
  2359. cmJsonNode_t* rootPtr = altRootPtr == NULL ? p->rootPtr : altRootPtr;
  2360. const char* buf = (const char*)vbuf;
  2361. memset(&r,0,sizeof(r));
  2362. r.nextPtr = buf;
  2363. r.endPtr = buf + (4*sizeof(unsigned)); // the buf must at least contain a header
  2364. // read the buffer header
  2365. unsigned hdrId = _cmJsonDeserialUint(&r);
  2366. unsigned byteCnt = _cmJsonDeserialUint(&r);
  2367. r.nodeCnt = _cmJsonDeserialUint(&r);
  2368. /*unsigned version =*/ _cmJsonDeserialUint(&r);
  2369. if( hdrId != 'json' )
  2370. return _cmJsonError(p,kSerialErrJsRC,"The buffer does not have the correct header.");
  2371. if( byteCnt < (4*sizeof(unsigned)) )
  2372. return _cmJsonError(p,kSerialErrJsRC,"The buffer is too small to be contain any information.");
  2373. // change the buffer end pointer to the correct size based
  2374. // on the the byte count stored in the buffer
  2375. r.endPtr = buf + byteCnt + (2*sizeof(unsigned));
  2376. return _cmJsonDeserializeNode(p, rootPtr, &r );
  2377. }
  2378. cmJsRC_t cmJsonDeserialize( cmJsonH_t h, const void* bufPtr, cmJsonNode_t* altRootPtr )
  2379. {
  2380. cmJs_t* p = _cmJsonHandleToPtr(h);
  2381. return _cmJsonDeserialize(p,bufPtr,altRootPtr);
  2382. }
  2383. cmJsRC_t cmJsonLeafToString( const cmJsonNode_t* np, cmChar_t* buf, unsigned bufCharCnt )
  2384. {
  2385. const char* cp = NULL;
  2386. unsigned n = 0;
  2387. assert( buf!=NULL && bufCharCnt > 0 );
  2388. switch(np->typeId & kMaskTId )
  2389. {
  2390. case kStringTId:
  2391. cp = np->u.stringVal==NULL ? "" : np->u.stringVal;
  2392. break;
  2393. case kNullTId:
  2394. cp = "null";
  2395. break;
  2396. case kTrueTId:
  2397. cp = "true";
  2398. break;
  2399. case kFalseTId:
  2400. cp = "false";
  2401. break;
  2402. case kIntTId:
  2403. n = snprintf(buf,bufCharCnt,"%i",np->u.intVal)+1;
  2404. break;
  2405. case kRealTId:
  2406. n = snprintf(buf,bufCharCnt,"%f",np->u.realVal)+1;
  2407. break;
  2408. default:
  2409. assert(0);
  2410. return kInvalidNodeTypeJsRC;
  2411. }
  2412. if( cp != NULL )
  2413. {
  2414. n = strlen(cp)+1;
  2415. if( bufCharCnt < n )
  2416. n = bufCharCnt;
  2417. strncpy(buf,cp,n);
  2418. /*
  2419. n = strlen(np->u.stringVal)+1;
  2420. if( bufCharCnt < n )
  2421. n = bufCharCnt;
  2422. strncpy(buf,np->u.stringVal,n);
  2423. */
  2424. }
  2425. buf[bufCharCnt-1]=0;
  2426. assert( n>0 && n < bufCharCnt );
  2427. return n == bufCharCnt ? kBufTooSmallJsRC : kOkJsRC ;
  2428. }
  2429. cmJsRC_t _cmJsonRptCsvTokErr( cmJs_t* p, unsigned lineNo, cmLexH lexH, const char* iFn )
  2430. {
  2431. unsigned n = cmMin(31,cmLexTokenCharCount(lexH));
  2432. char b[n+1];
  2433. strncpy(b,cmLexTokenText(lexH),n);
  2434. b[n]=0;
  2435. return _cmJsonError( p, kCsvErrJsRC, "Unexpected token '%s' during CSV parse on line %i of '%s'.",b,cmLexCurrentLineNumber(lexH),cmStringNullGuard(iFn));
  2436. }
  2437. cmJsRC_t _cmJsonRptCsvTypeErr( cmJs_t* p, unsigned lineNo, cmLexH lexH, const char* iFn, const char* actualTypeStr, unsigned expTypeId )
  2438. {
  2439. unsigned n = cmMin(31,cmLexTokenCharCount(lexH));
  2440. char b[n+1];
  2441. strncpy(b,cmLexTokenText(lexH),n);
  2442. b[n]=0;
  2443. return _cmJsonError( p, kCsvErrJsRC, "Unexpected token '%s' during CSV parse on line %i of '%s'.\nExpected type:%s actual type:%s",b,cmLexCurrentLineNumber(lexH),cmStringNullGuard(iFn),_cmJsonNodeTypeIdToLabel(expTypeId),actualTypeStr);
  2444. }
  2445. cmJsRC_t cmJsonFromCSV( cmJsonH_t h, const char* iFn, cmJsonNode_t* parentNodePtr, cmJsonNode_t** arrayNodePtrPtr )
  2446. {
  2447. enum
  2448. {
  2449. kCommaTokId = kUserLexTId+1,
  2450. kIntTokId,
  2451. kRealTokId,
  2452. kTrueTokId,
  2453. kFalseTokId,
  2454. kStringTokId,
  2455. kBoolTokId
  2456. };
  2457. typedef struct field_str
  2458. {
  2459. char* fieldLabel;
  2460. unsigned typeId;
  2461. struct field_str* linkPtr;
  2462. } field_t;
  2463. cmJsRC_t rc = kOkJsRC;
  2464. cmJs_t* p = _cmJsonHandleToPtr(h);
  2465. cmLexH lexH = cmLexInit( NULL, 0, 0, p->err.rpt );
  2466. field_t* fieldList = NULL;
  2467. cmJsonNode_t* arrayNodePtr = NULL;
  2468. cmJsonNode_t* objNodePtr = NULL;
  2469. unsigned lineNo = 0;
  2470. field_t* fieldPtr = NULL;
  2471. field_t* lp = NULL;
  2472. unsigned tokId;
  2473. unsigned fieldIdx = 0;
  2474. // validate the init state of the lexer
  2475. if( cmLexIsValid(lexH) == false )
  2476. {
  2477. rc = _cmJsonError( p, kLexErrJsRC, "Lexer initialization failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2478. goto errLabel;
  2479. }
  2480. // register CSV specific tokens
  2481. cmLexRegisterToken( lexH, kCommaTokId, ",");
  2482. cmLexRegisterToken( lexH, kIntTokId, "int");
  2483. cmLexRegisterToken( lexH, kRealTokId, "real");
  2484. cmLexRegisterToken( lexH, kTrueTokId, "true");
  2485. cmLexRegisterToken( lexH, kFalseTokId, "false");
  2486. cmLexRegisterToken( lexH, kStringTokId, "string");
  2487. cmLexRegisterToken( lexH, kBoolTokId, "bool");
  2488. // lex the file
  2489. if( cmLexSetFile( lexH, iFn ) != kOkLexRC )
  2490. {
  2491. rc = _cmJsonError( p, kLexErrJsRC, "Lex failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2492. goto errLabel;
  2493. }
  2494. // create the parent array
  2495. if((arrayNodePtr = cmJsonCreateArray( h, parentNodePtr )) == NULL )
  2496. {
  2497. rc = _cmJsonError( p, kCsvErrJsRC, "CSV array create failed during parse of '%s'.",cmStringNullGuard(iFn));
  2498. goto errLabel;
  2499. }
  2500. // iterate through the lexer file
  2501. while(((tokId = cmLexGetNextToken(lexH)) != kErrorLexTId) && (tokId != kEofLexTId) )
  2502. {
  2503. unsigned fieldTypeTokId = kInvalidTId;
  2504. switch( cmLexCurrentLineNumber(lexH) )
  2505. {
  2506. // line 1 contains the field type labels (e.g. int,real,string,true,false,bool)
  2507. case 1:
  2508. switch(tokId)
  2509. {
  2510. case kCommaTokId:
  2511. break;
  2512. case kIntTokId:
  2513. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kIntTId : fieldTypeTokId;
  2514. case kRealTokId:
  2515. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kRealTId : fieldTypeTokId;
  2516. case kTrueTokId:
  2517. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kTrueTId : fieldTypeTokId;
  2518. case kFalseTokId:
  2519. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kFalseTId : fieldTypeTokId;
  2520. case kBoolTokId:
  2521. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kFalseTId : fieldTypeTokId;
  2522. case kStringTokId:
  2523. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kStringTId : fieldTypeTokId;
  2524. // create and intitialize a new field
  2525. field_t* rp = cmMemAllocZ( field_t, 1 );
  2526. rp->fieldLabel = NULL;
  2527. rp->typeId = fieldTypeTokId;
  2528. rp->linkPtr = NULL;
  2529. // and add it to the end of the field list
  2530. if( fieldList == NULL )
  2531. fieldList = rp;
  2532. else
  2533. fieldPtr->linkPtr = rp;
  2534. // fieldPtr points to the end of the list
  2535. fieldPtr = rp;
  2536. break;
  2537. default:
  2538. rc = _cmJsonRptCsvTokErr( p, 1, lexH, iFn );
  2539. goto errLabel;
  2540. }
  2541. break;
  2542. // line 2 contains the field labels
  2543. case 2:
  2544. if( fieldIdx == 0 )
  2545. fieldPtr = fieldList;
  2546. ++fieldIdx;
  2547. switch(tokId)
  2548. {
  2549. case kCommaTokId:
  2550. break;
  2551. // all line 2 tokens must be identifiers or q-strings
  2552. case kIdentLexTId:
  2553. case kQStrLexTId:
  2554. if( fieldPtr == NULL )
  2555. {
  2556. rc = _cmJsonError( p, kCsvErrJsRC, "More fields on line 2 than type specifiers on line 1 of '%s'.",cmStringNullGuard(iFn));
  2557. goto errLabel;
  2558. }
  2559. else
  2560. {
  2561. // set the field name in the field list
  2562. unsigned n = cmLexTokenCharCount(lexH);
  2563. fieldPtr->fieldLabel = cmMemAllocZ( char, n+1 );
  2564. strncpy(fieldPtr->fieldLabel,cmLexTokenText(lexH),n);
  2565. fieldPtr->fieldLabel[n] = 0;
  2566. fieldPtr = fieldPtr->linkPtr;
  2567. }
  2568. break;
  2569. default:
  2570. rc = _cmJsonRptCsvTokErr( p, 2, lexH, iFn );
  2571. goto errLabel;
  2572. }
  2573. break;
  2574. // lines 3 to end of file contain data
  2575. default:
  2576. {
  2577. int ival = 0;
  2578. cmReal_t rval = 0;
  2579. // if we are starting a new line in the CSV file
  2580. if( lineNo != cmLexCurrentLineNumber(lexH) )
  2581. {
  2582. // verify that field ptr is pointing to the end of the field list
  2583. if( fieldPtr != NULL )
  2584. {
  2585. rc = _cmJsonError( p, kCsvErrJsRC, "Missing columns were detected on line %i in CSV file '%s'.", lineNo, cmStringNullGuard(iFn));
  2586. goto errLabel;
  2587. }
  2588. fieldPtr = fieldList;
  2589. lineNo = cmLexCurrentLineNumber(lexH);
  2590. // create the object to hold the fields on this line
  2591. if((objNodePtr = cmJsonCreateObject( h, arrayNodePtr )) == NULL )
  2592. {
  2593. rc = _cmJsonError( p, kCsvErrJsRC, "Object node create failed on line %i in CSV file '%s'.",lineNo,cmStringNullGuard(iFn));
  2594. goto errLabel;
  2595. }
  2596. }
  2597. if( tokId == kCommaTokId )
  2598. continue;
  2599. if( fieldPtr == NULL )
  2600. {
  2601. rc = _cmJsonError( p, kCsvErrJsRC, "More columns than fields on line %i in CSV file '%s'.", lineNo,cmStringNullGuard(iFn));
  2602. goto errLabel;
  2603. }
  2604. // given the tokens type convert the token string into a value
  2605. switch(tokId)
  2606. {
  2607. case kRealLexTId:
  2608. #ifdef CM_FLOAT_REAL
  2609. rval = cmLexTokenFloat(lexH);
  2610. #else
  2611. rval = cmLexTokenDouble(lexH);
  2612. #endif
  2613. ival = (int)rval;
  2614. if( fieldPtr->typeId == kStringTId )
  2615. {
  2616. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "numeric", fieldPtr->typeId );
  2617. goto errLabel;
  2618. }
  2619. break;
  2620. case kIntLexTId:
  2621. case kHexLexTId:
  2622. ival = cmLexTokenInt(lexH);
  2623. rval = ival;
  2624. if( fieldPtr->typeId == kStringTId )
  2625. {
  2626. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "numeric", fieldPtr->typeId );
  2627. goto errLabel;
  2628. }
  2629. break;
  2630. case kTrueTokId:
  2631. ival = 1;
  2632. rval = 1.0;
  2633. break;
  2634. case kFalseTokId:
  2635. ival = 0;
  2636. rval = 0.0;
  2637. break;
  2638. case kIdentLexTId:
  2639. case kQStrLexTId:
  2640. if( fieldPtr->typeId != kStringTId )
  2641. {
  2642. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "string", fieldPtr->typeId );
  2643. goto errLabel;
  2644. }
  2645. break;
  2646. default:
  2647. rc = _cmJsonRptCsvTokErr( p, lineNo, lexH, iFn );
  2648. goto errLabel;
  2649. }
  2650. // create the pair object from the current field label and value
  2651. switch(fieldPtr->typeId)
  2652. {
  2653. case kIntTId:
  2654. rc = cmJsonInsertPairInt( h, objNodePtr, fieldPtr->fieldLabel, ival );
  2655. break;
  2656. case kRealTId:
  2657. rc = cmJsonInsertPairReal( h, objNodePtr, fieldPtr->fieldLabel, rval );
  2658. break;
  2659. case kTrueTId:
  2660. case kFalseTId:
  2661. rc = cmJsonInsertPairBool( h, objNodePtr, fieldPtr->fieldLabel, ival );
  2662. break;
  2663. case kStringTId:
  2664. rc = cmJsonInsertPairStringN( h, objNodePtr, fieldPtr->fieldLabel, cmLexTokenText(lexH), cmLexTokenCharCount(lexH) );
  2665. break;
  2666. default:
  2667. {
  2668. assert(0);
  2669. goto errLabel;
  2670. }
  2671. }
  2672. if( rc != kOkJsRC )
  2673. goto errLabel;
  2674. fieldPtr = fieldPtr->linkPtr;
  2675. }
  2676. break;
  2677. }
  2678. }
  2679. errLabel:
  2680. if( cmLexFinal(&lexH) != kOkLexRC )
  2681. {
  2682. rc = _cmJsonError( p, kLexErrJsRC, "Lexer finalize failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2683. goto errLabel;
  2684. }
  2685. lp = fieldList;
  2686. while( lp!=NULL )
  2687. {
  2688. field_t* pp = lp->linkPtr;
  2689. cmMemPtrFree(&lp->fieldLabel);
  2690. cmMemPtrFree(&lp);
  2691. lp = pp;
  2692. }
  2693. if( rc != kOkJsRC )
  2694. _cmJsonRemoveNode( p, arrayNodePtr, true, true );
  2695. if( rc == kOkJsRC && arrayNodePtrPtr != NULL )
  2696. *arrayNodePtrPtr = arrayNodePtr;
  2697. return rc;
  2698. }
  2699. cmJsRC_t cmJsonToCSV( cmJsonH_t h, const char* oFn, const cmJsonNode_t* arrayNodePtr )
  2700. {
  2701. assert( arrayNodePtr->typeId == kArrayTId );
  2702. typedef struct r_str
  2703. {
  2704. const char* fieldLabel;
  2705. unsigned typeId;
  2706. struct r_str* linkPtr;
  2707. } field_t;
  2708. cmJs_t* p = _cmJsonHandleToPtr(h);
  2709. cmJsRC_t rc = kOkJsRC;
  2710. unsigned arrayCnt = cmJsonChildCount(arrayNodePtr);
  2711. field_t* fieldList = NULL;
  2712. FILE* fp = NULL;
  2713. field_t* lp = NULL;
  2714. unsigned i,j;
  2715. // for each object in the array
  2716. for(i=0; i<arrayCnt; ++i)
  2717. {
  2718. const cmJsonNode_t* objNodePtr = cmJsonArrayElementC( arrayNodePtr, i );
  2719. assert( objNodePtr->typeId == kObjectTId );
  2720. // for each pair in the object
  2721. for(j=0; j<cmJsonChildCount(objNodePtr); ++j)
  2722. {
  2723. const cmJsonNode_t* pairNodePtr = cmJsonArrayElementC( objNodePtr, j );
  2724. const char* pairLabel = cmJsonPairLabel(pairNodePtr);
  2725. lp = fieldList;
  2726. // find this field in the field list
  2727. for(; lp != NULL; lp = lp->linkPtr )
  2728. if( strcmp(lp->fieldLabel,pairLabel) == 0)
  2729. {
  2730. unsigned typeId = cmJsonPairTypeId(pairNodePtr);
  2731. switch( typeId )
  2732. {
  2733. case kIntTId:
  2734. case kRealTId:
  2735. case kTrueTId:
  2736. case kFalseTId:
  2737. case kStringTId:
  2738. break;
  2739. default:
  2740. rc = _cmJsonError( p, kInvalidNodeTypeJsRC, "Field '%s' has type '%s' which cannot be written by cmJsonToCSV().",cmStringNullGuard(pairLabel),_cmJsonNodeTypeIdToLabel(typeId) );
  2741. goto errLabel;
  2742. }
  2743. if( typeId != lp->typeId )
  2744. {
  2745. rc = _cmJsonError( p, kInvalidNodeTypeJsRC, "All nodes for a field label '%s' do not have the same type. '%s' != '%s'",cmStringNullGuard(pairLabel), _cmJsonNodeTypeIdToLabel(typeId), _cmJsonNodeTypeIdToLabel(lp->typeId) );
  2746. goto errLabel;
  2747. }
  2748. break;
  2749. }
  2750. // if this field was not found then insert it
  2751. if( lp == NULL )
  2752. {
  2753. field_t* rp = (field_t*)cmLHeapAlloc(p->heapH,sizeof(field_t));
  2754. rp->fieldLabel = pairLabel;
  2755. rp->linkPtr = fieldList;
  2756. rp->typeId = cmJsonPairTypeId(pairNodePtr);
  2757. fieldList = rp;
  2758. }
  2759. }
  2760. }
  2761. // create the output file
  2762. if((fp = fopen(oFn,"wt")) == NULL )
  2763. {
  2764. rc = _cmJsonError( p, kFileCreateErrJsRC, "CSV file '%s' create failed.", oFn );
  2765. goto errLabel;
  2766. }
  2767. // write the field type
  2768. lp = fieldList;
  2769. for(; lp!=NULL; lp=lp->linkPtr)
  2770. {
  2771. fprintf(fp,"%s", _cmJsonNodeTypeIdToLabel(lp->typeId));
  2772. if( lp->linkPtr != NULL )
  2773. fprintf(fp,",");
  2774. }
  2775. fprintf(fp,"\n");
  2776. // write the field label
  2777. lp = fieldList;
  2778. for(; lp!=NULL; lp=lp->linkPtr)
  2779. {
  2780. fprintf(fp,"\"%s\"", lp->fieldLabel);
  2781. if( lp->linkPtr != NULL )
  2782. fprintf(fp,",");
  2783. }
  2784. fprintf(fp,"\n");
  2785. // for each object in the array
  2786. for(i=0; i<arrayCnt; ++i)
  2787. {
  2788. unsigned j;
  2789. const cmJsonNode_t* objNodePtr = cmJsonArrayElementC( arrayNodePtr, i );
  2790. unsigned fieldCnt = cmJsonChildCount(objNodePtr);
  2791. lp = fieldList;
  2792. // for each field ...
  2793. for(j=0; lp!=NULL; lp=lp->linkPtr,++j)
  2794. {
  2795. cmJsonNode_t* valNodePtr;
  2796. // ... locate the pair given the field label
  2797. if((valNodePtr = cmJsonFindValue(h,lp->fieldLabel, objNodePtr, lp->typeId )) == NULL)
  2798. {
  2799. // no pair was found for the field label - output a NULL value
  2800. switch( lp->typeId )
  2801. {
  2802. case kIntTId:
  2803. fprintf(fp,"%i",0);
  2804. break;
  2805. case kRealTId:
  2806. fprintf(fp,"%f",0.0);
  2807. break;
  2808. case kTrueTId:
  2809. case kFalseTId:
  2810. fprintf(fp,"%i",0);
  2811. break;
  2812. case kStringTId:
  2813. fprintf(fp,"\"\"");
  2814. break;
  2815. default:
  2816. assert(0);
  2817. break;
  2818. }
  2819. //rc = _cmJsonError( p, kNodeNotFoundJsRC,"No field with label '%s' was found.", lp->fieldLabel);
  2820. //goto errLabel;
  2821. }
  2822. else
  2823. {
  2824. switch( valNodePtr->typeId )
  2825. {
  2826. case kIntTId:
  2827. fprintf(fp,"%i", valNodePtr->u.intVal);
  2828. break;
  2829. case kRealTId:
  2830. fprintf(fp,"%e", valNodePtr->u.realVal);
  2831. break;
  2832. case kTrueTId:
  2833. case kFalseTId:
  2834. fprintf(fp,"%i", valNodePtr->u.boolVal);
  2835. break;
  2836. case kStringTId:
  2837. fprintf(fp,"\"%s\"", valNodePtr->u.stringVal);
  2838. break;
  2839. default:
  2840. assert(0);
  2841. break;
  2842. }
  2843. }
  2844. if( j < fieldCnt-1 )
  2845. fprintf(fp,",");
  2846. }
  2847. fprintf(fp,"\n");
  2848. }
  2849. errLabel:
  2850. if( fp != NULL )
  2851. fclose(fp);
  2852. lp = fieldList;
  2853. while( lp!=NULL )
  2854. {
  2855. field_t* pp = lp->linkPtr;
  2856. cmLHeapFree(p->heapH,lp);
  2857. lp = pp;
  2858. }
  2859. return rc;
  2860. }
  2861. void _cmJsonPrintIndent( cmRpt_t* rpt, unsigned indent )
  2862. {
  2863. if( indent )
  2864. {
  2865. char spaces[indent+1];
  2866. spaces[indent]=0;
  2867. memset(spaces,' ',indent);
  2868. cmRptPrint(rpt,spaces);
  2869. }
  2870. }
  2871. void _cmJsonPrintNode( const cmJsonNode_t* np, cmRpt_t* rpt, unsigned indent )
  2872. {
  2873. unsigned childCnt = 0;
  2874. char eoObjStr[] = "}";
  2875. char eoArrStr[] = "]";
  2876. char eoPairStr[] = "\n";
  2877. char commaStr[] = ",\n";
  2878. char colonStr[] = ": ";
  2879. const char* eleStr = NULL;
  2880. const char* lastEleStr = NULL;
  2881. const char* eoStr = NULL;
  2882. unsigned localIndent = 0;
  2883. switch(np->typeId)
  2884. {
  2885. case kObjectTId:
  2886. cmRptPrint(rpt,"\n");
  2887. _cmJsonPrintIndent(rpt,indent);
  2888. cmRptPrint(rpt,"{\n");
  2889. childCnt = cmJsonChildCount(np);
  2890. eoStr = eoObjStr;
  2891. localIndent = 2;
  2892. break;
  2893. case kArrayTId:
  2894. cmRptPrint(rpt,"\n");
  2895. _cmJsonPrintIndent(rpt,indent);
  2896. cmRptPrint(rpt,"[\n");
  2897. childCnt = cmJsonChildCount(np);
  2898. eoStr = eoArrStr;
  2899. localIndent = 2;
  2900. eleStr = commaStr;
  2901. lastEleStr = "\n";
  2902. break;
  2903. case kPairTId:
  2904. childCnt = cmJsonChildCount(np);
  2905. eleStr = colonStr;
  2906. eoStr = eoPairStr;
  2907. break;
  2908. case kStringTId:
  2909. {
  2910. const char* fmt0 = "\"%s\" ";
  2911. const char* fmt1 = "%s";
  2912. const char* fmt = fmt0;
  2913. // if this string is the label part of a pair
  2914. if( np->u.stringVal != NULL && np->ownerPtr != NULL && cmJsonIsPair(np->ownerPtr) && np->ownerPtr->u.childPtr == np )
  2915. {
  2916. // and the label has no white space
  2917. char* cp = np->u.stringVal;
  2918. while( *cp!=0 && isspace(*cp)==false )
  2919. ++cp;
  2920. // then print without quotes
  2921. if( *cp == 0 )
  2922. fmt = fmt1;
  2923. }
  2924. cmRptPrintf(rpt,fmt,np->u.stringVal==NULL ? "" : np->u.stringVal);
  2925. }
  2926. break;
  2927. case kIntTId:
  2928. cmRptPrintf(rpt,"%i ",np->u.intVal);
  2929. break;
  2930. case kRealTId:
  2931. cmRptPrintf(rpt,"%f ",np->u.realVal);
  2932. break;
  2933. case kNullTId:
  2934. cmRptPrint(rpt,"null ");
  2935. break;
  2936. case kTrueTId:
  2937. cmRptPrint(rpt,"true ");
  2938. break;
  2939. case kFalseTId:
  2940. cmRptPrint(rpt,"false ");
  2941. break;
  2942. }
  2943. if( childCnt )
  2944. {
  2945. indent += localIndent;
  2946. unsigned i;
  2947. cmJsonNode_t* cnp = np->u.childPtr;
  2948. for(i=0; i<childCnt; ++i)
  2949. {
  2950. assert(cnp != NULL);
  2951. if( np->typeId != kPairTId )
  2952. _cmJsonPrintIndent(rpt,indent);
  2953. _cmJsonPrintNode(cnp,rpt,indent);
  2954. cnp = cnp->siblingPtr;
  2955. if( i < childCnt-1 && eleStr != NULL )
  2956. cmRptPrint(rpt,eleStr);
  2957. if( i == childCnt-1 && lastEleStr != NULL )
  2958. cmRptPrint(rpt,lastEleStr);
  2959. }
  2960. indent -= localIndent;
  2961. }
  2962. if( eoStr != NULL )
  2963. {
  2964. _cmJsonPrintIndent(rpt,indent);
  2965. cmRptPrint(rpt,eoStr);
  2966. }
  2967. }
  2968. void cmJsonPrintTree( const cmJsonNode_t* np, cmRpt_t* rpt )
  2969. {
  2970. _cmJsonPrintNode(np,rpt,0);
  2971. }
  2972. void _cmJsPrintFile(void* cmRptUserPtr, const cmChar_t* text)
  2973. {
  2974. cmFileH_t* hp = (cmFileH_t*)cmRptUserPtr;
  2975. cmFilePrint(*hp,text);
  2976. }
  2977. cmJsRC_t cmJsonWrite( cmJsonH_t h, const cmJsonNode_t* np, const cmChar_t* fn )
  2978. {
  2979. cmRpt_t rpt;
  2980. cmFileH_t fh = cmFileNullHandle;
  2981. cmJs_t* p = _cmJsonHandleToPtr(h);
  2982. if( np == NULL )
  2983. np = cmJsonRoot(h);
  2984. // create the output file
  2985. if( cmFileOpen(&fh,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  2986. return _cmJsonError( p, kFileCreateErrJsRC, "Output file '%s' create failed.", fn );
  2987. // setup a reporter to write to the file
  2988. cmRptSetup(&rpt,_cmJsPrintFile,_cmJsPrintFile,&fh);
  2989. // print the tree to the file
  2990. cmJsonPrintTree(np,&rpt);
  2991. // close the file
  2992. if( cmFileClose(&fh) != kOkFileRC )
  2993. return _cmJsonError( p, kFileCloseErrJsRC, "Output file '%s' close failed.", fn );
  2994. return kOkJsRC;
  2995. }
  2996. cmJsRC_t cmJsonReport( cmJsonH_t h )
  2997. {
  2998. cmJsRC_t rc;
  2999. cmJs_t* p = _cmJsonHandleToPtr(h);
  3000. if((rc = cmJsonValidateTree(h)) != kOkJsRC )
  3001. return rc;
  3002. if(p->rootPtr != NULL )
  3003. _cmJsonPrintNode(p->rootPtr,p->err.rpt,0);
  3004. return rc;
  3005. }
  3006. cmJsRC_t cmJsonErrorCode( cmJsonH_t h )
  3007. {
  3008. cmJs_t* p = _cmJsonHandleToPtr(h);
  3009. return p->rc;
  3010. }
  3011. void cmJsonClearErrorCode( cmJsonH_t h )
  3012. {
  3013. cmJs_t* p = _cmJsonHandleToPtr(h);
  3014. p->rc = kOkJsRC;
  3015. }
  3016. void _cmJsonTestVPrint( void* rptDataPtr, const char* fmt, va_list vl )
  3017. {
  3018. vfprintf(stdout,fmt,vl);
  3019. }
  3020. void _cmJsonTestPrint( void* userPtr, const cmChar_t* text )
  3021. {
  3022. fputs(text,stdout);
  3023. }
  3024. //{ { label:cmJsonEx }
  3025. //(
  3026. // cmJsonTest() demonstrates some JSON tree operations.
  3027. //)
  3028. //[
  3029. cmJsRC_t cmJsonTest( const char* fn, cmCtx_t* ctx )
  3030. {
  3031. cmJsRC_t rc = kOkJsRC;
  3032. cmJsRC_t rc1 = kOkJsRC;
  3033. cmJsonH_t h = cmJsonNullHandle;
  3034. cmJsonH_t h1 = cmJsonNullHandle;
  3035. void* sbp = NULL;
  3036. unsigned sbn = 0;
  3037. cmJsonNode_t* np = NULL;
  3038. cmRpt_t* rpt = &ctx->rpt;
  3039. // initialize an empty JSON tree
  3040. if((rc = cmJsonInitialize(&h,ctx)) != kOkJsRC )
  3041. goto errLabel;
  3042. // load the tree from a file
  3043. if((rc = cmJsonParseFile(h,fn,NULL)) != kOkJsRC )
  3044. goto errLabel;
  3045. // print the tree
  3046. cmJsonReport(h);
  3047. // find an array member named 'mem14'
  3048. if((np = cmJsonFindValue(h,"mem14",NULL,kArrayTId)) == NULL )
  3049. cmRptPrint(rpt,"'mem14' not found.\n");
  3050. else
  3051. {
  3052. cmRptPrint(rpt,"'mem14' found.\n");
  3053. cmJsonPrintTree(np,rpt);
  3054. }
  3055. // remove the array node from the tree
  3056. cmJsonRemoveNode(h,np, true);
  3057. cmRptPrint(rpt,"mem14 removed.\n");
  3058. // print the tree with the array node removed
  3059. cmJsonPrintTree( cmJsonRoot(h), rpt );
  3060. // serialize the tree into a dynamically allocated
  3061. // buffer sbp[sbn].
  3062. if((rc = cmJsonSerializeTree(h,NULL,&sbp,&sbn)) != kOkJsRC )
  3063. goto errLabel;
  3064. else
  3065. cmRptPrint(rpt,"***Serialize Ok.****\n");
  3066. // initialize an empty JSON tree
  3067. if((rc = cmJsonInitialize(&h1,ctx)) != kOkJsRC )
  3068. goto errLabel;
  3069. // deserialize sbp[sbn] into the empty tree
  3070. if((rc = cmJsonDeserialize(h1,sbp,NULL)) != kOkJsRC )
  3071. goto errLabel;
  3072. else
  3073. {
  3074. cmJsonPrintTree( cmJsonRoot(h1),rpt);
  3075. cmRptPrint(rpt,"***Deserialize Ok.****\n");
  3076. }
  3077. // find an member node named 'mem5'
  3078. if((np = cmJsonFindValue(h,"mem5",NULL,0)) == NULL )
  3079. cmRptPrint(rpt,"mem5 not found.");
  3080. // merge two sub-trees
  3081. if( cmJsonMergeObjectNodes( h, np->u.childPtr,
  3082. np->u.childPtr->siblingPtr) != kOkJsRC )
  3083. {
  3084. cmRptPrint(rpt,"merge failed.");
  3085. }
  3086. else
  3087. {
  3088. cmJsonReport(h);
  3089. }
  3090. errLabel:
  3091. // release the JSON trees
  3092. rc = cmJsonFinalize(&h);
  3093. rc1 = cmJsonFinalize(&h1);
  3094. return rc == kOkJsRC ? rc1 : rc;
  3095. }
  3096. //]
  3097. //}