libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072
  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. cmJsonNode_t* cmJsonFindPair( const cmJsonNode_t* np, const char* label )
  1081. {
  1082. cmJsRC_t rc = kOkJsRC;
  1083. cmJsonNode_t* vnp = NULL;
  1084. if((rc = _cmJsonFindMemberValue( np, label, kInvalidTId, &vnp )) != kOkJsRC )
  1085. return NULL;
  1086. assert( vnp != NULL );
  1087. return vnp->ownerPtr;
  1088. }
  1089. cmJsRC_t cmJsonMemberType( const cmJsonNode_t* np, const char* label, unsigned* typeIdRef )
  1090. {
  1091. cmJsRC_t rc = kOkJsRC;
  1092. cmJsonNode_t* vnp = NULL;
  1093. if( typeIdRef != NULL )
  1094. *typeIdRef = kInvalidTId;
  1095. if((rc = _cmJsonFindMemberValue( np, label, kInvalidTId, &vnp )) != kOkJsRC )
  1096. return rc;
  1097. assert( vnp != NULL );
  1098. if( typeIdRef != NULL )
  1099. *typeIdRef = vnp->typeId & kMaskTId;
  1100. return rc;
  1101. }
  1102. cmJsRC_t cmJsonUIntMember( const cmJsonNode_t* np, const char* label, unsigned* retPtr )
  1103. {
  1104. cmJsonNode_t* vp;
  1105. cmJsRC_t rc;
  1106. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1107. return rc;
  1108. return cmJsonUIntValue(vp,retPtr);
  1109. }
  1110. cmJsRC_t cmJsonIntMember( const cmJsonNode_t* np, const char* label, int* retPtr )
  1111. {
  1112. cmJsonNode_t* vp;
  1113. cmJsRC_t rc;
  1114. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1115. return rc;
  1116. return cmJsonIntValue(vp,retPtr);
  1117. }
  1118. cmJsRC_t cmJsonRealMember( const cmJsonNode_t* np, const char* label, double* retPtr )
  1119. {
  1120. cmJsonNode_t* vp;
  1121. cmJsRC_t rc;
  1122. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1123. return rc;
  1124. return cmJsonRealValue(vp,retPtr);
  1125. }
  1126. cmJsRC_t cmJsonBoolMember( const cmJsonNode_t* np, const char* label, bool* retPtr )
  1127. {
  1128. cmJsonNode_t* vp;
  1129. cmJsRC_t rc;
  1130. if((rc = _cmJsonFindMemberValue(np,label,kNumericTId,&vp)) != kOkJsRC )
  1131. return rc;
  1132. return cmJsonBoolValue(vp,retPtr);
  1133. }
  1134. cmJsRC_t cmJsonStringMember( const cmJsonNode_t* np, const char* label, const char** retPtrPtr )
  1135. {
  1136. cmJsonNode_t* vp;
  1137. cmJsRC_t rc;
  1138. *retPtrPtr = NULL;
  1139. if((rc = _cmJsonFindMemberValue(np,label,kStringTId,&vp)) != kOkJsRC )
  1140. return rc;
  1141. return cmJsonStringValue(vp,retPtrPtr);
  1142. }
  1143. cmJsRC_t cmJsonNodeMember( const cmJsonNode_t* np, const char* label, cmJsonNode_t** retPtrPtr )
  1144. {
  1145. cmJsonNode_t* vp;
  1146. cmJsRC_t rc;
  1147. *retPtrPtr = NULL;
  1148. if((rc = _cmJsonFindMemberValue(np,label,kArrayTId|kObjectTId,&vp)) != kOkJsRC )
  1149. return rc;
  1150. *retPtrPtr = vp;
  1151. return rc;
  1152. }
  1153. cmJsonNode_t* cmJsonNodeMemberValue( const cmJsonNode_t* np, const char* label )
  1154. {
  1155. assert( cmJsonIsObject(np) );
  1156. unsigned n = cmJsonChildCount(np);
  1157. unsigned j = 0;
  1158. for(; j<n; ++j)
  1159. {
  1160. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,j);
  1161. assert( cnp != NULL && cmJsonIsPair(cnp) );
  1162. if( strcmp(label,cmJsonPairLabel(cnp))==0 )
  1163. return cmJsonPairValue((cmJsonNode_t*)cnp);
  1164. }
  1165. return NULL;
  1166. }
  1167. cmJsRC_t cmJsonVMemberValues( const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, va_list vl )
  1168. {
  1169. unsigned typeId;
  1170. cmJsRC_t rc = kOkJsRC;
  1171. if( errLabelPtrPtr != NULL )
  1172. *errLabelPtrPtr = NULL;
  1173. const char* label;
  1174. while( ((label = va_arg(vl,const char*)) != NULL) && (rc==kOkJsRC) )
  1175. {
  1176. typeId = va_arg(vl,unsigned);
  1177. switch( typeId & kMaskTId )
  1178. {
  1179. case kObjectTId:
  1180. case kPairTId:
  1181. case kArrayTId:
  1182. {
  1183. cmJsonNode_t** nodePtrPtr = va_arg(vl, cmJsonNode_t**);
  1184. if((rc = cmJsonNodeMember(objectNodePtr,label,nodePtrPtr )) == kOkJsRC )
  1185. {
  1186. cmJsonNode_t* np = *nodePtrPtr;
  1187. if( (np->typeId & kMaskTId) != (typeId&kMaskTId) )
  1188. rc = kNodeCannotCvtJsRC;
  1189. }
  1190. }
  1191. break;
  1192. case kIntTId:
  1193. {
  1194. int* ip = va_arg(vl, int* );
  1195. assert(ip != NULL);
  1196. rc = cmJsonIntMember(objectNodePtr, label, ip);
  1197. }
  1198. break;
  1199. case kRealTId:
  1200. {
  1201. double* dp = va_arg(vl, double*);
  1202. assert(dp != NULL);
  1203. rc = cmJsonRealMember(objectNodePtr, label, dp);
  1204. }
  1205. break;
  1206. case kStringTId:
  1207. {
  1208. const char** cpp = va_arg(vl, const char**);
  1209. assert(cpp != NULL);
  1210. rc = cmJsonStringMember(objectNodePtr, label, cpp);
  1211. }
  1212. break;
  1213. case kTrueTId:
  1214. case kFalseTId:
  1215. case kBoolTId:
  1216. {
  1217. bool* bp = va_arg(vl, bool* );
  1218. rc = cmJsonBoolMember(objectNodePtr, label, bp);
  1219. }
  1220. break;
  1221. default:
  1222. // missing terminating NULL on the var args list???
  1223. assert(0);
  1224. break;
  1225. }
  1226. if( (rc == kNodeNotFoundJsRC) && cmIsFlag(typeId,kOptArgJsFl) )
  1227. rc = kOkJsRC;
  1228. if( rc != kOkJsRC && errLabelPtrPtr != NULL )
  1229. *errLabelPtrPtr = label;
  1230. }
  1231. return rc;
  1232. }
  1233. cmJsRC_t cmJsonMemberValues( const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ...)
  1234. {
  1235. va_list vl;
  1236. va_start(vl,errLabelPtrPtr);
  1237. cmJsRC_t rc = cmJsonVMemberValues(objectNodePtr,errLabelPtrPtr,vl);
  1238. va_end(vl);
  1239. return rc;
  1240. }
  1241. cmJsRC_t cmJsonVPathValues( cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objNodePtr, const char** errLabelPtrPtr, va_list vl )
  1242. {
  1243. cmJsRC_t rc = kOkJsRC;
  1244. cmJs_t* p = _cmJsonHandleToPtr(h);
  1245. if( errLabelPtrPtr != NULL )
  1246. *errLabelPtrPtr = NULL;
  1247. const char* path;
  1248. while( ((path = va_arg(vl,const char*)) != NULL) && (rc==kOkJsRC) )
  1249. {
  1250. unsigned typeId;
  1251. const cmJsonNode_t* vnp;
  1252. typeId = va_arg(vl,unsigned);
  1253. // find the requested pair value
  1254. if((rc = _cmJsonFindPathValue(p,pathPrefix,path,objNodePtr,&vnp)) != kOkJsRC )
  1255. break;
  1256. switch( typeId & kMaskTId )
  1257. {
  1258. case kObjectTId:
  1259. case kPairTId:
  1260. case kArrayTId:
  1261. {
  1262. const cmJsonNode_t** nodePtrPtr = va_arg(vl, const cmJsonNode_t**);
  1263. if( (vnp->typeId & kMaskTId) != (typeId & kMaskTId) )
  1264. rc = kNodeCannotCvtJsRC;
  1265. else
  1266. *nodePtrPtr = vnp;
  1267. }
  1268. break;
  1269. case kIntTId:
  1270. {
  1271. int* ip = va_arg(vl, int* );
  1272. assert(ip != NULL);
  1273. rc = cmJsonIntValue(vnp, ip);
  1274. }
  1275. break;
  1276. case kRealTId:
  1277. {
  1278. double* dp = va_arg(vl, double*);
  1279. assert(dp != NULL);
  1280. rc = cmJsonRealValue(vnp, dp);
  1281. }
  1282. break;
  1283. case kStringTId:
  1284. {
  1285. const char** cpp = va_arg(vl, const char**);
  1286. assert(cpp != NULL);
  1287. rc = cmJsonStringValue(vnp, cpp);
  1288. }
  1289. break;
  1290. case kTrueTId:
  1291. case kFalseTId:
  1292. {
  1293. bool* bp = va_arg(vl, bool* );
  1294. rc = cmJsonBoolValue(vnp, bp);
  1295. }
  1296. break;
  1297. default:
  1298. // missing terminating NULL on the var args list???
  1299. assert(0);
  1300. break;
  1301. }
  1302. if( (rc == kNodeNotFoundJsRC) && cmIsFlag(typeId,kOptArgJsFl) )
  1303. rc = kOkJsRC;
  1304. if( rc != kOkJsRC && errLabelPtrPtr != NULL )
  1305. *errLabelPtrPtr = path;
  1306. }
  1307. return rc;
  1308. }
  1309. cmJsRC_t cmJsonPathValues( cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ... )
  1310. {
  1311. va_list vl;
  1312. va_start(vl,errLabelPtrPtr);
  1313. cmJsRC_t rc = cmJsonVPathValues(h,pathPrefix,objectNodePtr,errLabelPtrPtr,vl);
  1314. va_end(vl);
  1315. return rc;
  1316. }
  1317. cmJsonNode_t* _cmJsonCreateNode2( cmJsonH_t h, unsigned newNodeTypeId, cmJsonNode_t* parentPtr )
  1318. {
  1319. cmJs_t* p = _cmJsonHandleToPtr(h);
  1320. cmJsonNode_t* np = NULL;
  1321. if((p->rc = _cmJsonCreateNode(p,parentPtr,newNodeTypeId,&np)) != kOkJsRC )
  1322. return NULL;
  1323. return np;
  1324. }
  1325. cmJsRC_t cmJsonCreate( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned typeId, const char* sv, int iv, double dv, cmJsonNode_t** rpp )
  1326. {
  1327. cmJsonNode_t* rp = NULL;
  1328. cmJsRC_t rc = kOkJsRC;
  1329. cmJs_t* p = _cmJsonHandleToPtr(h);
  1330. if( rpp != NULL )
  1331. *rpp = NULL;
  1332. switch( typeId )
  1333. {
  1334. case kObjectTId:
  1335. case kArrayTId:
  1336. if((rp = _cmJsonCreateNode2(h,typeId,parentPtr)) == NULL)
  1337. rc = p->rc;
  1338. else
  1339. {
  1340. if( rpp != NULL )
  1341. *rpp = rp;
  1342. }
  1343. break;
  1344. case kPairTId: rc = _cmJsonCreatePair(p,parentPtr,sv,rpp); break;
  1345. case kIntTId: rc = _cmJsonCreateInt(p,parentPtr,iv,rpp); break;
  1346. case kRealTId: rc = _cmJsonCreateReal(p,parentPtr,dv,rpp); break;
  1347. case kTrueTId: rc = _cmJsonCreateBool(p,parentPtr,true,rpp); break;
  1348. case kFalseTId: rc = _cmJsonCreateBool(p,parentPtr,false,rpp); break;
  1349. case kNullTId: rc = _cmJsonCreateNull(p,parentPtr,rpp); break;
  1350. case kStringTId: rc = _cmJsonCreateString(p,parentPtr,sv,strlen(sv),rpp); break;
  1351. default:
  1352. assert(0);
  1353. break;
  1354. }
  1355. return rc;
  1356. }
  1357. cmJsonNode_t* cmJsonCreateObject( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1358. { return _cmJsonCreateNode2(h,kObjectTId,parentPtr); }
  1359. cmJsonNode_t* cmJsonCreateArray( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1360. { return _cmJsonCreateNode2(h,kArrayTId,parentPtr); }
  1361. cmJsonNode_t* cmJsonCreatePair( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* label )
  1362. {
  1363. cmJsonNode_t* np;
  1364. cmJs_t* p = _cmJsonHandleToPtr(h);
  1365. if((_cmJsonCreatePair(p,parentPtr,label,&np)) != kOkJsRC )
  1366. return NULL;
  1367. return np;
  1368. }
  1369. cmJsRC_t cmJsonCreateString( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue )
  1370. {
  1371. cmJs_t* p = _cmJsonHandleToPtr(h);
  1372. return _cmJsonCreateString( p, parentPtr, stringValue, strlen(stringValue),NULL);;
  1373. }
  1374. cmJsRC_t cmJsonCreateStringN(cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue, unsigned stringCharCnt )
  1375. {
  1376. cmJs_t* p = _cmJsonHandleToPtr(h);
  1377. return _cmJsonCreateString( p, parentPtr, stringValue, stringCharCnt,NULL);;
  1378. }
  1379. cmJsRC_t cmJsonCreateInt( cmJsonH_t h, cmJsonNode_t* parentPtr, int value )
  1380. {
  1381. cmJs_t* p = _cmJsonHandleToPtr(h);
  1382. return _cmJsonCreateInt( p, parentPtr, value, NULL );
  1383. }
  1384. cmJsRC_t cmJsonCreateReal( cmJsonH_t h, cmJsonNode_t* parentPtr, double value )
  1385. {
  1386. cmJs_t* p = _cmJsonHandleToPtr(h);
  1387. return _cmJsonCreateReal( p, parentPtr, value, NULL );
  1388. }
  1389. cmJsRC_t cmJsonCreateBool( cmJsonH_t h, cmJsonNode_t* parentPtr, bool value )
  1390. {
  1391. cmJs_t* p = _cmJsonHandleToPtr(h);
  1392. return _cmJsonCreateBool( p, parentPtr, value,NULL );
  1393. }
  1394. cmJsRC_t cmJsonCreateNull( cmJsonH_t h, cmJsonNode_t* parentPtr )
  1395. {
  1396. cmJs_t* p = _cmJsonHandleToPtr(h);
  1397. return _cmJsonCreateNull( p, parentPtr, NULL );
  1398. }
  1399. cmJsRC_t cmJsonCreateStringArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const char** value )
  1400. {
  1401. cmJs_t* p = _cmJsonHandleToPtr(h);
  1402. cmJsRC_t rc = kOkJsRC;
  1403. cmJsonNode_t* np;
  1404. unsigned i;
  1405. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1406. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'bool' array.");
  1407. for(i=0; i<n; ++i)
  1408. if((rc = cmJsonCreateString(h,np,value[i])) != kOkJsRC )
  1409. return _cmJsonError(p,rc,"Unable to create 'bool' array element at index %i.",i);
  1410. return rc;
  1411. }
  1412. cmJsRC_t cmJsonCreateIntArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const int* value )
  1413. {
  1414. cmJs_t* p = _cmJsonHandleToPtr(h);
  1415. cmJsRC_t rc = kOkJsRC;
  1416. cmJsonNode_t* np;
  1417. unsigned i;
  1418. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1419. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'int' array.");
  1420. for(i=0; i<n; ++i)
  1421. if((rc = cmJsonCreateInt(h,np,value[i])) != kOkJsRC )
  1422. return _cmJsonError(p,rc,"Unable to create 'int' array element at index %i.",i);
  1423. return rc;
  1424. }
  1425. cmJsRC_t cmJsonCreateRealArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const double* value )
  1426. {
  1427. cmJs_t* p = _cmJsonHandleToPtr(h);
  1428. cmJsRC_t rc = kOkJsRC;
  1429. cmJsonNode_t* np;
  1430. unsigned i;
  1431. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1432. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'real' array.");
  1433. for(i=0; i<n; ++i)
  1434. if((rc = cmJsonCreateReal(h,np,value[i])) != kOkJsRC )
  1435. return _cmJsonError(p,rc,"Unable to create 'real' array element at index %i.",i);
  1436. return rc;
  1437. }
  1438. cmJsRC_t cmJsonCreateBoolArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const bool* value )
  1439. {
  1440. cmJs_t* p = _cmJsonHandleToPtr(h);
  1441. cmJsRC_t rc = kOkJsRC;
  1442. cmJsonNode_t* np;
  1443. unsigned i;
  1444. if((np = cmJsonCreateArray(h,parentPtr)) == NULL )
  1445. return _cmJsonError(p,cmErrLastRC(&p->err),"Unable to create 'bool' array.");
  1446. for(i=0; i<n; ++i)
  1447. if((rc = cmJsonCreateBool(h,np,value[i])) != kOkJsRC )
  1448. return _cmJsonError(p,rc,"Unable to create 'bool' array element at index %i.",i);
  1449. return rc;
  1450. }
  1451. cmJsRC_t cmJsonSetInt( cmJsonH_t h, cmJsonNode_t* np, int ival )
  1452. {
  1453. cmJs_t* p = _cmJsonHandleToPtr(h);
  1454. if( np->typeId != kIntTId )
  1455. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'int' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1456. np->u.intVal = ival;
  1457. p->modifiedFl = true;
  1458. return kOkJsRC;
  1459. }
  1460. cmJsRC_t cmJsonSetReal( cmJsonH_t h, cmJsonNode_t * np, double rval )
  1461. {
  1462. cmJs_t* p = _cmJsonHandleToPtr(h);
  1463. if( np->typeId != kRealTId )
  1464. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'real' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1465. np->u.realVal = rval;
  1466. p->modifiedFl = true;
  1467. return kOkJsRC;
  1468. }
  1469. cmJsRC_t cmJsonSetBool( cmJsonH_t h, cmJsonNode_t * np, bool bval )
  1470. {
  1471. cmJs_t* p = _cmJsonHandleToPtr(h);
  1472. if( np->typeId == kTrueTId || np->typeId==kFalseTId )
  1473. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'bool' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1474. np->u.boolVal = bval;
  1475. p->modifiedFl = true;
  1476. return kOkJsRC;
  1477. }
  1478. cmJsRC_t cmJsonSetString( cmJsonH_t h, cmJsonNode_t* np, const char* sval )
  1479. {
  1480. cmJs_t* p = _cmJsonHandleToPtr(h);
  1481. if( np->typeId != kStringTId )
  1482. return _cmJsonError(p,kInvalidNodeTypeJsRC,"Cannot assign type 'string' to node type '%s'.",_cmJsonNodeTypeIdToLabel(np->typeId));
  1483. unsigned sn = strlen(sval);
  1484. if( np->u.stringVal != NULL && strlen(np->u.stringVal) <= sn )
  1485. strcpy(np->u.stringVal,sval);
  1486. else
  1487. return _cmJsonSetString(p,np,sval,sn);
  1488. p->modifiedFl = true;
  1489. return kOkJsRC;
  1490. }
  1491. cmJsonNode_t* cmJsonInsertPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned typeId, const char* sv, int iv, double dv )
  1492. {
  1493. assert( objectNodePtr->typeId == kObjectTId );
  1494. cmJsRC_t rc;
  1495. cmJsonNode_t* pairNodePtr;
  1496. cmJs_t* p = _cmJsonHandleToPtr(h);
  1497. if((rc = _cmJsonCreatePair(p,objectNodePtr,label,&pairNodePtr)) != kOkJsRC )
  1498. return NULL;
  1499. assert( pairNodePtr != NULL );
  1500. if((rc = cmJsonCreate(h,pairNodePtr,typeId,sv,iv,dv,NULL)) != kOkJsRC )
  1501. return NULL;
  1502. return pairNodePtr;
  1503. }
  1504. cmJsonNode_t* cmJsonInsertPairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1505. {
  1506. assert( objectNodePtr->typeId == kObjectTId );
  1507. cmJsonNode_t* pairNodePtr;
  1508. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1509. return NULL;
  1510. return cmJsonCreateObject(h,pairNodePtr);
  1511. }
  1512. cmJsonNode_t* cmJsonInsertPairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1513. {
  1514. assert( objectNodePtr->typeId == kObjectTId );
  1515. cmJsonNode_t* pairNodePtr;
  1516. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1517. return NULL;
  1518. return cmJsonCreateArray(h,pairNodePtr);
  1519. }
  1520. cmJsonNode_t* cmJsonInsertPairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* pairLabel )
  1521. {
  1522. assert( objectNodePtr->typeId == kObjectTId );
  1523. cmJsonNode_t* pairNodePtr;
  1524. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1525. return NULL;
  1526. return cmJsonCreatePair(h,pairNodePtr,pairLabel);
  1527. }
  1528. cmJsRC_t cmJsonInsertPairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, int intVal )
  1529. {
  1530. assert( objectNodePtr->typeId == kObjectTId );
  1531. cmJsonNode_t* pairNodePtr;
  1532. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1533. return cmJsonErrorCode(h);
  1534. return cmJsonCreateInt(h,pairNodePtr,intVal);
  1535. }
  1536. cmJsRC_t cmJsonInsertPairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, double realVal )
  1537. {
  1538. assert( objectNodePtr->typeId == kObjectTId );
  1539. cmJsonNode_t* pairNodePtr;
  1540. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1541. return cmJsonErrorCode(h);
  1542. return cmJsonCreateReal(h,pairNodePtr,realVal);
  1543. }
  1544. cmJsRC_t cmJsonInsertPairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* stringVal )
  1545. { return cmJsonInsertPairStringN(h,objectNodePtr,label,stringVal,stringVal==NULL ? 0 : strlen(stringVal)); }
  1546. cmJsRC_t cmJsonInsertPairStringN( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* stringVal, unsigned stringCharCnt )
  1547. {
  1548. assert( objectNodePtr->typeId == kObjectTId );
  1549. cmJsonNode_t* pairNodePtr;
  1550. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1551. return cmJsonErrorCode(h);
  1552. return cmJsonCreateStringN(h,pairNodePtr,stringVal,stringCharCnt);
  1553. }
  1554. cmJsRC_t cmJsonInsertPairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, bool boolVal )
  1555. {
  1556. assert( objectNodePtr->typeId == kObjectTId );
  1557. cmJsonNode_t* pairNodePtr;
  1558. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1559. return cmJsonErrorCode(h);
  1560. return cmJsonCreateBool(h,pairNodePtr,boolVal);
  1561. }
  1562. cmJsRC_t cmJsonInsertPairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label )
  1563. {
  1564. assert( objectNodePtr->typeId == kObjectTId );
  1565. cmJsonNode_t* pairNodePtr;
  1566. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1567. return cmJsonErrorCode(h);
  1568. return cmJsonCreateNull(h,pairNodePtr);
  1569. }
  1570. cmJsRC_t cmJsonInsertPairIntArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values )
  1571. {
  1572. assert( objectNodePtr->typeId == kObjectTId );
  1573. cmJsonNode_t* pairNodePtr;
  1574. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1575. return cmJsonErrorCode(h);
  1576. return cmJsonCreateIntArray(h,pairNodePtr,n,values);
  1577. }
  1578. cmJsRC_t cmJsonInsertPairRealArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values )
  1579. {
  1580. assert( objectNodePtr->typeId == kObjectTId );
  1581. cmJsonNode_t* pairNodePtr;
  1582. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1583. return cmJsonErrorCode(h);
  1584. return cmJsonCreateRealArray(h,pairNodePtr,n,values);
  1585. }
  1586. cmJsRC_t cmJsonInsertPairStringArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values )
  1587. {
  1588. assert( objectNodePtr->typeId == kObjectTId );
  1589. cmJsonNode_t* pairNodePtr;
  1590. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1591. return cmJsonErrorCode(h);
  1592. return cmJsonCreateStringArray(h,pairNodePtr,n,values);
  1593. }
  1594. cmJsRC_t cmJsonInsertPairBoolArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values )
  1595. {
  1596. assert( objectNodePtr->typeId == kObjectTId );
  1597. cmJsonNode_t* pairNodePtr;
  1598. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) == NULL )
  1599. return cmJsonErrorCode(h);
  1600. return cmJsonCreateBoolArray(h,pairNodePtr,n,values);
  1601. }
  1602. cmJsonNode_t* cmJsonInsertPairIntArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values )
  1603. {
  1604. assert( objectNodePtr->typeId == kObjectTId );
  1605. cmJsonNode_t* pairNodePtr;
  1606. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1607. if( cmJsonCreateIntArray(h,pairNodePtr,n,values) != kOkJsRC )
  1608. return NULL;
  1609. return pairNodePtr;
  1610. }
  1611. cmJsonNode_t* cmJsonInsertPairRealArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values )
  1612. {
  1613. assert( objectNodePtr->typeId == kObjectTId );
  1614. cmJsonNode_t* pairNodePtr;
  1615. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1616. if( cmJsonCreateRealArray(h,pairNodePtr,n,values) != kOkJsRC )
  1617. return NULL;
  1618. return pairNodePtr;
  1619. }
  1620. cmJsonNode_t* cmJsonInsertPairStringArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values )
  1621. {
  1622. assert( objectNodePtr->typeId == kObjectTId );
  1623. cmJsonNode_t* pairNodePtr;
  1624. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1625. if( cmJsonCreateStringArray(h,pairNodePtr,n,values) != kOkJsRC )
  1626. return NULL;
  1627. return pairNodePtr;
  1628. }
  1629. cmJsonNode_t* cmJsonInsertPairBoolArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values )
  1630. {
  1631. assert( objectNodePtr->typeId == kObjectTId );
  1632. cmJsonNode_t* pairNodePtr;
  1633. if((pairNodePtr = cmJsonCreatePair(h,objectNodePtr,label)) != NULL )
  1634. if( cmJsonCreateBoolArray(h,pairNodePtr,n,values) != kOkJsRC )
  1635. return NULL;
  1636. return pairNodePtr;
  1637. }
  1638. 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 )
  1639. {
  1640. cmJsRC_t rc = kOkJsRC;
  1641. cmJs_t* p = _cmJsonHandleToPtr(h);
  1642. cmJsonNode_t* valNodePtr = NULL;
  1643. cmJsonNode_t* pairNodePtr = NULL;
  1644. assert( objectNodePtr->typeId == kObjectTId );
  1645. if( retNodePtrPtr != NULL )
  1646. *retNodePtrPtr = NULL;
  1647. // if a matching pair was not found ....
  1648. if(( valNodePtr = cmJsonFindValue(h,label,objectNodePtr,matchTypeMask)) == NULL )
  1649. {
  1650. // ... and insertion is not allowed then return error
  1651. if( insertFl == false )
  1652. return kNodeNotFoundJsRC;
  1653. // ... otherwise insert a new pair and return a pointer to it
  1654. pairNodePtr = cmJsonInsertPair(h,objectNodePtr,label,newTypeId,sv,iv,dv);
  1655. goto errLabel;
  1656. }
  1657. // ... otherwise a match was found to at least the pair label.
  1658. // If matchTypeMask was set to kInvalidTId then the type id
  1659. // of the found pair may not be the same as newTypeId. To handle
  1660. // this case we proceed by first deallocating all resources held
  1661. // by the found node and then proceeding by either creating
  1662. // deleting the found node and creating a replacement node
  1663. // (object,array,pair) or forcing the found node to a new
  1664. // type (int,real,true,false,string,null).
  1665. assert( valNodePtr != NULL);
  1666. pairNodePtr = valNodePtr->ownerPtr;
  1667. // release any resources held by the found node
  1668. switch( valNodePtr->typeId )
  1669. {
  1670. case kObjectTId:
  1671. case kArrayTId:
  1672. case kPairTId:
  1673. // remove the pair value node and replace it with a 'null' node.
  1674. if((rc =_cmJsonRemoveNode( p, valNodePtr, true, true )) != kOkJsRC )
  1675. goto errLabel;
  1676. break;
  1677. case kStringTId:
  1678. // deallocate string memory
  1679. _cmJsonSetString( p, valNodePtr, NULL, 0 );
  1680. break;
  1681. }
  1682. // relace the found node with the new node
  1683. switch( newTypeId )
  1684. {
  1685. case kObjectTId:
  1686. case kArrayTId:
  1687. case kPairTId:
  1688. {
  1689. cmJsonNode_t* newValueNodePtr = NULL;
  1690. // remove the current pair value ....
  1691. if((rc =_cmJsonRemoveNode( p, valNodePtr, true, false )) != kOkJsRC )
  1692. goto errLabel;
  1693. // new pair nodes should have the pair label in 'sv'
  1694. assert( newTypeId!=kPairTId || (newTypeId==kPairTId && sv != NULL ));
  1695. // if no new value was given or the new value is a pair then ...
  1696. if( nv == NULL || newTypeId == kPairTId )
  1697. {
  1698. // ... create a new blank array,obj or pair
  1699. if((rc = cmJsonCreate( h, pairNodePtr, newTypeId, sv, 0, 0, &newValueNodePtr )) != kOkJsRC )
  1700. goto errLabel;
  1701. }
  1702. // if the new value is a pair and no value node was given then set the
  1703. // new pairs value node to NULL
  1704. if( nv == NULL && newTypeId == kPairTId )
  1705. {
  1706. if((rc = _cmJsonCreateNull(p,newValueNodePtr,NULL)) != kOkJsRC )
  1707. goto errLabel;
  1708. }
  1709. // if a new value node was given
  1710. if( nv != NULL )
  1711. {
  1712. // the new child node should not already be linked to a parent
  1713. assert( nv->ownerPtr == NULL );
  1714. // if the new value is an obj or array then the new
  1715. // value node type id should be the same
  1716. assert( newTypeId==kPairTId || newTypeId==nv->typeId );
  1717. //
  1718. cmJsonNode_t* pp = newTypeId==kPairTId ? newValueNodePtr : pairNodePtr;
  1719. assert( pp->typeId == kPairTId );
  1720. // link in the child to the pair
  1721. if((rc = _cmJsonLinkInChild(p,pp,nv)) != kOkJsRC )
  1722. goto errLabel;
  1723. }
  1724. }
  1725. break;
  1726. // All cases below follow the same pattern:
  1727. // 1. Set the type id to the replacement value type id
  1728. // 2. Assign the value to the node.
  1729. // This sequence is safe because all resources were freed
  1730. // by the earlier switch.
  1731. case kStringTId:
  1732. valNodePtr->typeId = kStringTId;
  1733. _cmJsonSetString( p, valNodePtr, sv, strlen(sv) );
  1734. break;
  1735. case kIntTId:
  1736. valNodePtr->typeId =kIntTId;
  1737. valNodePtr->u.intVal = iv;
  1738. break;
  1739. case kRealTId:
  1740. valNodePtr->typeId = kRealTId;
  1741. valNodePtr->u.realVal = dv;
  1742. break;
  1743. case kTrueTId:
  1744. valNodePtr->typeId = kTrueTId;
  1745. valNodePtr->u.boolVal = true;
  1746. break;
  1747. case kFalseTId:
  1748. valNodePtr->typeId = kFalseTId;
  1749. valNodePtr->u.boolVal = false;
  1750. break;
  1751. case kNullTId:
  1752. valNodePtr->typeId = kNullTId;
  1753. break;
  1754. default:
  1755. { assert(0); }
  1756. }
  1757. errLabel:
  1758. if( rc == kOkJsRC )
  1759. {
  1760. if( retNodePtrPtr != NULL )
  1761. *retNodePtrPtr = pairNodePtr;
  1762. p->modifiedFl = true;
  1763. }
  1764. return rc;
  1765. }
  1766. 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 )
  1767. {
  1768. cmJsonNode_t* retNodePtr = NULL;
  1769. _cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,typeId,sv,iv,dv,nv,true,&retNodePtr );
  1770. return retNodePtr;
  1771. }
  1772. cmJsonNode_t* cmJsonInsertOrReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newObjNodePtr )
  1773. {
  1774. cmJsonNode_t* pairPtr;
  1775. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kObjectTId,NULL,0,0,newObjNodePtr)) == NULL )
  1776. return NULL;
  1777. return pairPtr->u.childPtr->siblingPtr;
  1778. }
  1779. cmJsonNode_t* cmJsonInsertOrReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr )
  1780. {
  1781. cmJsonNode_t* pairPtr;
  1782. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kArrayTId,NULL,0,0,newArrayNodePtr)) == NULL )
  1783. return NULL;
  1784. return pairPtr->u.childPtr->siblingPtr;
  1785. }
  1786. cmJsonNode_t* cmJsonInsertOrReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValNodePtr )
  1787. {
  1788. cmJsonNode_t* pairPtr;
  1789. if((pairPtr = cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kPairTId,newPairLabel,0,0,newPairValNodePtr)) == NULL )
  1790. return NULL;
  1791. return pairPtr->u.childPtr->siblingPtr;
  1792. }
  1793. cmJsRC_t cmJsonInsertOrReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal )
  1794. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kIntTId,NULL,intVal,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1795. cmJsRC_t cmJsonInsertOrReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal )
  1796. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kRealTId,NULL,0,realVal,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1797. cmJsRC_t cmJsonInsertOrReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* stringVal )
  1798. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kStringTId,stringVal,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1799. cmJsRC_t cmJsonInsertOrReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal )
  1800. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,boolVal ? kTrueTId : kFalseTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1801. cmJsRC_t cmJsonInsertOrReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask )
  1802. { return cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,kNullTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1803. 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 )
  1804. {
  1805. cmJsonNode_t* retNodePtr = NULL;
  1806. _cmJsonInsertOrReplacePair(h,objectNodePtr,label,matchTypeMask,newTypeId,sv,iv,dv,nv,false,&retNodePtr );
  1807. return retNodePtr;
  1808. }
  1809. cmJsonNode_t* cmJsonReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newPairNodePtr )
  1810. {
  1811. cmJsonNode_t* pairPtr;
  1812. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kObjectTId,NULL,0,0,newPairNodePtr)) == NULL )
  1813. return NULL;
  1814. return pairPtr->u.childPtr->siblingPtr;
  1815. }
  1816. cmJsonNode_t* cmJsonReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr )
  1817. {
  1818. cmJsonNode_t* pairPtr;
  1819. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kArrayTId,NULL,0,0,newArrayNodePtr)) == NULL )
  1820. return NULL;
  1821. return pairPtr->u.childPtr->siblingPtr;
  1822. }
  1823. cmJsonNode_t* cmJsonReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValNodePtr )
  1824. {
  1825. cmJsonNode_t* pairPtr;
  1826. if((pairPtr = cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kPairTId,newPairLabel,0,0,newPairValNodePtr)) == NULL )
  1827. return NULL;
  1828. return pairPtr->u.childPtr->siblingPtr;
  1829. }
  1830. cmJsRC_t cmJsonReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal )
  1831. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kIntTId,NULL,intVal,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1832. cmJsRC_t cmJsonReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal )
  1833. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kRealTId,NULL,0,realVal,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1834. cmJsRC_t cmJsonReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* stringVal )
  1835. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kStringTId,stringVal,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1836. cmJsRC_t cmJsonReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal )
  1837. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,boolVal ? kTrueTId : kFalseTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1838. cmJsRC_t cmJsonReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask )
  1839. { return cmJsonReplacePair(h,objectNodePtr,label,matchTypeMask,kNullTId,NULL,0,0,NULL) == NULL ? cmJsonErrorCode(h) : kOkJsRC; }
  1840. cmJsRC_t cmJsonVInsertPairs( cmJsonH_t h, cmJsonNode_t* objNodePtr, va_list vl )
  1841. {
  1842. cmJsRC_t rc = kOkJsRC;
  1843. assert( objNodePtr->typeId == kObjectTId );
  1844. const char* label;
  1845. while( ((label = va_arg(vl,const char*)) != NULL) && (rc == kOkJsRC) )
  1846. {
  1847. unsigned sel = va_arg(vl,unsigned);
  1848. switch( sel )
  1849. {
  1850. case kObjectTId:
  1851. if( cmJsonInsertPairObject(h,objNodePtr,label) == NULL )
  1852. rc = cmJsonErrorCode(h);
  1853. break;
  1854. case kArrayTId:
  1855. if( cmJsonInsertPairArray(h,objNodePtr,label) == NULL )
  1856. rc = cmJsonErrorCode(h);
  1857. break;
  1858. case kPairTId:
  1859. if( cmJsonInsertPairPair(h,objNodePtr,label, va_arg(vl,const char*)) == NULL )
  1860. rc = cmJsonErrorCode(h);
  1861. break;
  1862. case kIntTId:
  1863. rc = cmJsonInsertPairInt(h,objNodePtr,label, va_arg(vl,int) );
  1864. break;
  1865. case kRealTId:
  1866. rc = cmJsonInsertPairReal(h,objNodePtr,label, va_arg(vl,double) );
  1867. break;
  1868. case kStringTId:
  1869. rc = cmJsonInsertPairString(h,objNodePtr,label, va_arg(vl,const char *) );
  1870. break;
  1871. case kTrueTId:
  1872. case kFalseTId:
  1873. case kBoolTId:
  1874. rc = cmJsonInsertPairBool(h,objNodePtr,label, va_arg(vl,int) );
  1875. break;
  1876. case kNullTId:
  1877. rc = cmJsonInsertPairNull(h,objNodePtr,label );
  1878. break;
  1879. default:
  1880. // missing terminating NULL on the var args list???
  1881. assert(0);
  1882. break;
  1883. }
  1884. }
  1885. return rc;
  1886. }
  1887. cmJsRC_t cmJsonInsertPairs( cmJsonH_t h, cmJsonNode_t* objectNodePtr, ... )
  1888. {
  1889. va_list vl;
  1890. va_start(vl,objectNodePtr);
  1891. cmJsRC_t rc = cmJsonVInsertPairs(h,objectNodePtr,vl);
  1892. va_end(vl);
  1893. return rc;
  1894. }
  1895. cmJsonNode_t* cmJsonVCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, va_list vl )
  1896. {
  1897. cmJsonNode_t* np;
  1898. if((np = cmJsonCreateObject(h,parentPtr)) == NULL)
  1899. return NULL;
  1900. if( cmJsonVInsertPairs(h,np,vl) != kOkJsRC )
  1901. {
  1902. cmJsonRemoveNode(h,np,true);
  1903. return NULL;
  1904. }
  1905. return np;
  1906. }
  1907. cmJsonNode_t* cmJsonCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, ... )
  1908. {
  1909. va_list vl;
  1910. va_start(vl,parentPtr);
  1911. cmJsonNode_t* np = cmJsonVCreateFilledObject(h,parentPtr,vl);
  1912. va_end(vl);
  1913. return np;
  1914. }
  1915. void _cmJsonFreeNode( cmJs_t* p, cmJsonNode_t* np )
  1916. {
  1917. switch( np->typeId )
  1918. {
  1919. case kObjectTId:
  1920. case kPairTId:
  1921. case kArrayTId:
  1922. {
  1923. cmJsonNode_t* cnp = np->u.childPtr;
  1924. while( cnp != NULL )
  1925. {
  1926. cmJsonNode_t* nnp = cnp->siblingPtr;
  1927. _cmJsonFreeNode(p,cnp);
  1928. cnp = nnp;
  1929. }
  1930. }
  1931. break;
  1932. case kStringTId:
  1933. if( np->u.stringVal != NULL )
  1934. cmLHeapFree(p->heapH,np->u.stringVal);
  1935. break;
  1936. }
  1937. cmLHeapFree(p->heapH,np);
  1938. }
  1939. cmJsRC_t _cmJsonRemoveNode( cmJs_t* p, cmJsonNode_t* np, bool freeFl, bool balancePairsFl )
  1940. {
  1941. if(np == NULL )
  1942. return kOkJsRC;
  1943. cmJsonNode_t* parentPtr = np->ownerPtr;
  1944. // if np is the root ...
  1945. if( np == p->rootPtr )
  1946. {
  1947. // ... we only need to set the root to null to remove the node.
  1948. p->rootPtr = NULL;
  1949. }
  1950. else
  1951. {
  1952. if( parentPtr != NULL )
  1953. {
  1954. // get the parents first child
  1955. cmJsonNode_t* cnp = parentPtr->u.childPtr;
  1956. // if np is the first child then make the second child the first child
  1957. if( cnp == np )
  1958. {
  1959. if( parentPtr->typeId == kPairTId )
  1960. return _cmJsonError( p, kCannotRemoveLabelJsRC, "Cannot remove pair label nodes because this would invalidate the tree structure.");
  1961. parentPtr->u.childPtr = cnp->siblingPtr;
  1962. }
  1963. else
  1964. {
  1965. // otherwise unlink it from the child chain
  1966. while( cnp != NULL )
  1967. {
  1968. if( cnp->siblingPtr == np )
  1969. {
  1970. cnp->siblingPtr = np->siblingPtr;
  1971. // if the parent is a pair then the removed node is a
  1972. // 'pair value' which must be replaced with a null node in order
  1973. // to maintain a valid tree.
  1974. if( (parentPtr->typeId == kPairTId) && balancePairsFl)
  1975. _cmJsonCreateNull( p, parentPtr, NULL );
  1976. break;
  1977. }
  1978. cnp = cnp->siblingPtr;
  1979. }
  1980. assert( cnp != NULL );
  1981. }
  1982. }
  1983. }
  1984. // if the memory assoc'd with the removed node should be released
  1985. if( freeFl )
  1986. {
  1987. _cmJsonFreeNode(p,np);
  1988. /*
  1989. if( np->typeId == kStringTId )
  1990. {
  1991. cmLHeapFree(p->heapH,np->u.stringVal);
  1992. np->u.stringVal = NULL;
  1993. }
  1994. cmLHeapFree(p->heapH,np);
  1995. */
  1996. }
  1997. p->modifiedFl = true;
  1998. return kOkJsRC;
  1999. }
  2000. cmJsRC_t cmJsonRemoveNode( cmJsonH_t h, cmJsonNode_t* np, bool freeFl )
  2001. {
  2002. cmJs_t* p = _cmJsonHandleToPtr(h);
  2003. return _cmJsonRemoveNode( p, np, freeFl, true );
  2004. }
  2005. cmJsRC_t _cmJsonValidateErr( cmJs_t* p, const char* text )
  2006. {
  2007. if( p != NULL )
  2008. return _cmJsonSyntaxError(p,text);
  2009. return kValidateFailJsRC;
  2010. }
  2011. // 'p' is optional. If 'p' is set to NULL the function will return kValidFailJsRC if the
  2012. // tree rooted at 'np' is invalid and will not print an error message.
  2013. cmJsRC_t _cmJsonValidateNode( cmJs_t* p, const cmJsonNode_t* np, const cmJsonNode_t* parentPtr )
  2014. {
  2015. cmJsRC_t rc = kOkJsRC;
  2016. if( parentPtr != np->ownerPtr )
  2017. return _cmJsonValidateErr(p,"A child->parent link does not agree with a parent->child link.");
  2018. if( parentPtr == NULL )
  2019. {
  2020. if( np->typeId != kArrayTId && np->typeId != kObjectTId )
  2021. return _cmJsonValidateErr(p,"Only 'array' and 'object' nodes may be the root element.");
  2022. }
  2023. else
  2024. {
  2025. if( parentPtr->typeId != kArrayTId && parentPtr->typeId != kObjectTId && parentPtr->typeId != kPairTId )
  2026. return _cmJsonValidateErr(p,"Parent nodes must be either 'object','array' or 'pair' nodes.");
  2027. }
  2028. switch( np->typeId )
  2029. {
  2030. case kPairTId:
  2031. if( cmJsonChildCount(np) != 2 )
  2032. return _cmJsonValidateErr(p,"'pair' nodes must have exactly two children.");
  2033. if( np->u.childPtr->typeId != kStringTId )
  2034. return _cmJsonValidateErr(p,"The first child of 'pair' nodes must be a 'string' node.");
  2035. // fall through
  2036. case kObjectTId:
  2037. case kArrayTId:
  2038. {
  2039. // validate each child node
  2040. cmJsonNode_t* cnp = np->u.childPtr;
  2041. while(cnp != NULL)
  2042. {
  2043. if( cnp->ownerPtr != np )
  2044. return _cmJsonValidateErr(p,"A parent->child pointer was not validated with a child->parent pointer.");
  2045. if( np->typeId == kObjectTId && cnp->typeId != kPairTId )
  2046. return _cmJsonValidateErr(p,"All 'object' child nodes must be 'pair' nodes.");
  2047. if((rc = _cmJsonValidateNode(p,cnp,np)) != kOkJsRC )
  2048. return rc;
  2049. cnp = cnp->siblingPtr;
  2050. }
  2051. }
  2052. break;
  2053. case kStringTId:
  2054. case kIntTId:
  2055. case kRealTId:
  2056. case kNullTId:
  2057. case kTrueTId:
  2058. case kFalseTId:
  2059. default:
  2060. break;
  2061. }
  2062. return rc;
  2063. }
  2064. cmJsRC_t cmJsonValidateTree( cmJsonH_t h )
  2065. {
  2066. cmJs_t* p = _cmJsonHandleToPtr(h);
  2067. return _cmJsonValidateNode(p,p->rootPtr,NULL);
  2068. }
  2069. cmJsRC_t cmJsonValidate( const cmJsonNode_t* np )
  2070. { return _cmJsonValidateNode(NULL,np,np->ownerPtr); }
  2071. cmJsonNode_t* _cmJsonDuplicateNode( cmJs_t* p, const cmJsonNode_t* np, cmJsonNode_t* parentPtr )
  2072. {
  2073. cmJsonNode_t* newParentPtr = NULL;
  2074. cmJsonNode_t* newNodePtr = NULL;
  2075. cmJsRC_t rc = kOkJsRC;
  2076. switch( np->typeId )
  2077. {
  2078. case kObjectTId:
  2079. case kArrayTId:
  2080. rc = _cmJsonCreateNode(p,parentPtr,np->typeId,&newParentPtr);
  2081. break;
  2082. case kPairTId:
  2083. rc = _cmJsonCreatePair(p,parentPtr,np->u.childPtr->u.stringVal,&newParentPtr);
  2084. break;
  2085. case kIntTId:
  2086. rc = _cmJsonCreateInt(p,parentPtr,np->u.intVal,&newNodePtr);
  2087. break;
  2088. case kRealTId:
  2089. rc = _cmJsonCreateReal(p,parentPtr,np->u.realVal,&newNodePtr);
  2090. break;
  2091. case kStringTId:
  2092. rc = _cmJsonCreateString(p,parentPtr,np->u.stringVal,strlen(np->u.stringVal),&newNodePtr);
  2093. break;
  2094. case kTrueTId:
  2095. case kFalseTId:
  2096. rc = _cmJsonCreateBool(p,parentPtr,np->u.boolVal,&newNodePtr);
  2097. break;
  2098. case kNullTId:
  2099. rc = _cmJsonCreateNull(p,parentPtr,&newNodePtr);
  2100. }
  2101. if( rc != kOkJsRC )
  2102. return NULL;
  2103. if( newParentPtr != NULL )
  2104. {
  2105. newNodePtr = newParentPtr;
  2106. cmJsonNode_t* cnp = np->u.childPtr;
  2107. if(np->typeId == kPairTId)
  2108. cnp = np->u.childPtr->siblingPtr;
  2109. while( cnp != NULL )
  2110. {
  2111. if( _cmJsonDuplicateNode(p,cnp,newParentPtr) != kOkJsRC )
  2112. return NULL;
  2113. cnp = cnp->siblingPtr;
  2114. }
  2115. }
  2116. return newNodePtr;
  2117. }
  2118. cmJsonNode_t* cmJsonDuplicateNode( cmJsonH_t h, const cmJsonNode_t* np, cmJsonNode_t* parentPtr )
  2119. {
  2120. cmJs_t* p = _cmJsonHandleToPtr(h);
  2121. assert( _cmJsonValidateNode(p,np,NULL) == kOkJsRC );
  2122. return _cmJsonDuplicateNode(p,np,parentPtr);
  2123. }
  2124. cmJsRC_t cmJsonMergeObjectNodes( cmJsonH_t h, cmJsonNode_t* dstObjNodePtr, const cmJsonNode_t* srcObjNodePtr )
  2125. {
  2126. assert( dstObjNodePtr!=NULL && dstObjNodePtr->typeId == kObjectTId );
  2127. assert( srcObjNodePtr!=NULL && srcObjNodePtr->typeId == kObjectTId );
  2128. cmJsRC_t rc = kOkJsRC;
  2129. cmJs_t* p = _cmJsonHandleToPtr(h);
  2130. cmJsonNode_t* cnp = NULL;
  2131. const cmJsonNode_t* snp = srcObjNodePtr->u.childPtr;
  2132. while( snp!=NULL && rc==kOkJsRC )
  2133. {
  2134. cmJsonNode_t* dnp;
  2135. assert( snp->typeId == kPairTId );
  2136. // if the src pair was not found in the dst object ...
  2137. if((rc = _cmJsonFindMemberValue(dstObjNodePtr, cmJsonPairLabel(snp), snp->u.childPtr->siblingPtr->typeId, &dnp )) != kOkJsRC )
  2138. {
  2139. cmJsonNode_t* newPairNodePtr;
  2140. // the only acceptable error is kNodeNotFoundJsRC
  2141. // (in particular we reject kNodeCannotCvtJsRC to avoid duplicating
  2142. // pairs with the same name but different types)
  2143. if( rc != kNodeNotFoundJsRC )
  2144. goto errLabel;
  2145. // create the new pair and attach it to the dst obj node
  2146. if((rc = _cmJsonCreatePair(p,dstObjNodePtr,cmJsonPairLabel(snp),&newPairNodePtr)) != kOkJsRC )
  2147. goto errLabel;
  2148. // duplicate the src pair value node and attcmh it to the new dst node
  2149. if( _cmJsonDuplicateNode(p,snp->u.childPtr->siblingPtr,newPairNodePtr) == NULL )
  2150. rc = p->rc;
  2151. // set kTempJsFl on the new node to use possible cleanup on error later
  2152. newPairNodePtr->typeId = cmSetFlag(newPairNodePtr->typeId,kTempJsFl);
  2153. }
  2154. snp = snp->siblingPtr;
  2155. }
  2156. errLabel:
  2157. // for each dst obj pair
  2158. cnp = dstObjNodePtr->u.childPtr;
  2159. while( cnp != NULL)
  2160. {
  2161. // if this child is a new node
  2162. if( cmIsFlag(cnp->typeId,kTempJsFl) )
  2163. {
  2164. // clear the temp fl
  2165. cnp->typeId = cmClrFlag(cnp->typeId,kTempJsFl);
  2166. // if there was an error remove all new pairs
  2167. if( rc != kOkJsRC )
  2168. cmJsonRemoveNode(h,cnp,true);
  2169. }
  2170. cnp = cnp->siblingPtr;
  2171. }
  2172. return rc;
  2173. }
  2174. void _cmJsonSerialCopy( cmJsSerial_t* rp, const void* sp, unsigned sn )
  2175. {
  2176. assert( rp->nextPtr + sn <= rp->endPtr );
  2177. memcpy(rp->nextPtr,sp,sn);
  2178. rp->nextPtr += sn;
  2179. }
  2180. cmJsRC_t _cmJsonSerializeNode( const cmJsonNode_t* np, cmJsSerial_t* rp )
  2181. {
  2182. cmJsRC_t rc = kOkJsRC;
  2183. // on the first pass rp->basePtr will be NULL so collect size information
  2184. // on the second pass rp->basePtr will be set so copy out data
  2185. // write the type id of this node
  2186. if( rp->basePtr != NULL )
  2187. _cmJsonSerialCopy(rp,&np->typeId,sizeof(np->typeId));
  2188. else
  2189. rp->hdr.byteCnt += sizeof(np->typeId);
  2190. rp->hdr.nodeCnt++;
  2191. switch( np->typeId )
  2192. {
  2193. // write the child count
  2194. case kObjectTId:
  2195. case kArrayTId:
  2196. if( rp->basePtr == NULL )
  2197. rp->hdr.byteCnt += sizeof(unsigned);
  2198. else
  2199. {
  2200. unsigned n = cmJsonChildCount( np );
  2201. _cmJsonSerialCopy(rp,&n,sizeof(unsigned));
  2202. }
  2203. // fall through
  2204. case kPairTId:
  2205. {
  2206. cmJsonNode_t* cnp = np->u.childPtr;
  2207. while(cnp != NULL )
  2208. {
  2209. _cmJsonSerializeNode(cnp,rp);
  2210. cnp = cnp->siblingPtr;
  2211. }
  2212. }
  2213. break;
  2214. case kStringTId:
  2215. // write the string contents
  2216. if( rp->basePtr == NULL )
  2217. rp->hdr.byteCnt += strlen(np->u.stringVal) + 1;
  2218. else
  2219. _cmJsonSerialCopy( rp, np->u.stringVal, strlen(np->u.stringVal)+1 );
  2220. break;
  2221. case kIntTId:
  2222. // write the int value
  2223. if( rp->basePtr == NULL )
  2224. rp->hdr.byteCnt += sizeof(np->u.intVal);
  2225. else
  2226. _cmJsonSerialCopy(rp,&np->u.intVal,sizeof(np->u.intVal));
  2227. break;
  2228. case kRealTId:
  2229. // write the real value
  2230. if( rp->basePtr == NULL )
  2231. rp->hdr.byteCnt += sizeof(np->u.realVal);
  2232. else
  2233. _cmJsonSerialCopy(rp, &np->u.realVal, sizeof(np->u.realVal));
  2234. break;
  2235. }
  2236. return rc;
  2237. }
  2238. unsigned cmJsonSerialByteCount( const cmJsonNode_t* np )
  2239. {
  2240. cmJsRC_t rc;
  2241. cmJsSerial_t vr;
  2242. memset(&vr,0,sizeof(vr));
  2243. // make a first pass to determine the size of the buffer
  2244. if((rc = _cmJsonSerializeNode(np,&vr)) != kOkJsRC )
  2245. return rc;
  2246. // increment the buffer size to include the buffer header
  2247. return (4*sizeof(unsigned)) + vr.hdr.byteCnt;
  2248. }
  2249. cmJsRC_t cmJsonSerialize( const cmJsonNode_t* np, void* buf, unsigned bufByteCnt )
  2250. {
  2251. cmJsRC_t rc = kOkJsRC;
  2252. cmJsSerial_t vr;
  2253. memset(&vr,0,sizeof(vr));
  2254. // setup the serial buffer
  2255. vr.hdr.id = 'json';
  2256. vr.hdr.byteCnt = bufByteCnt - (2*sizeof(unsigned));
  2257. vr.hdr.version = 0;
  2258. vr.basePtr = buf;
  2259. vr.nextPtr = vr.basePtr;
  2260. vr.endPtr = vr.basePtr + bufByteCnt;
  2261. // write the header recd
  2262. _cmJsonSerialCopy(&vr, &vr.hdr.id, sizeof(unsigned));
  2263. _cmJsonSerialCopy(&vr, &vr.hdr.byteCnt, sizeof(unsigned));
  2264. _cmJsonSerialCopy(&vr, &vr.hdr.nodeCnt, sizeof(unsigned));
  2265. _cmJsonSerialCopy(&vr, &vr.hdr.version, sizeof(unsigned));
  2266. // copy the node data into the serial buffer
  2267. if((rc = _cmJsonSerializeNode(np,&vr)) != kOkJsRC )
  2268. return rc;
  2269. vr.basePtr = buf;
  2270. vr.nextPtr = vr.basePtr;
  2271. _cmJsonSerialCopy(&vr, &vr.hdr.id, sizeof(unsigned));
  2272. _cmJsonSerialCopy(&vr, &vr.hdr.byteCnt, sizeof(unsigned));
  2273. _cmJsonSerialCopy(&vr, &vr.hdr.nodeCnt, sizeof(unsigned));
  2274. return rc;
  2275. }
  2276. cmJsRC_t cmJsonSerializeTree( cmJsonH_t h, const cmJsonNode_t* np, void** bufPtrPtr, unsigned* bufByteCntPtr)
  2277. {
  2278. cmJsRC_t rc;
  2279. cmJsSerial_t vr;
  2280. memset(&vr,0,sizeof(vr));
  2281. cmJs_t* p = _cmJsonHandleToPtr(h);
  2282. assert( bufPtrPtr != NULL && bufByteCntPtr != NULL );
  2283. *bufPtrPtr = NULL;
  2284. *bufByteCntPtr = 0;
  2285. if( np == NULL )
  2286. np = p->rootPtr;
  2287. // validate the tree
  2288. if((rc = _cmJsonValidateNode(p,np,np->ownerPtr)) != kOkJsRC )
  2289. return rc;
  2290. // increment the buffer size to include the buffer header
  2291. p->serialByteCnt = cmJsonSerialByteCount(np);
  2292. // allocate the serial buffer memory
  2293. p->serialBufPtr = cmMemResize( char, p->serialBufPtr, p->serialByteCnt );
  2294. // serialize the tree
  2295. if((rc = cmJsonSerialize(np, p->serialBufPtr, p->serialByteCnt )) != kOkJsRC )
  2296. return rc;
  2297. *bufPtrPtr = p->serialBufPtr;
  2298. *bufByteCntPtr = p->serialByteCnt;
  2299. return rc;
  2300. }
  2301. const void* _cmJsonDeserialAdv( cmJsDeserial_t* rp, unsigned n )
  2302. {
  2303. const void* vp = rp->nextPtr;
  2304. rp->nextPtr += n;
  2305. assert(rp->nextPtr <= rp->endPtr);
  2306. assert(rp->nodeIdx <= rp->nodeCnt);
  2307. return vp;
  2308. }
  2309. int _cmJsonDeserialInt( cmJsDeserial_t* rp )
  2310. { return *(const int*)_cmJsonDeserialAdv(rp,sizeof(int)); }
  2311. unsigned _cmJsonDeserialUint( cmJsDeserial_t* rp )
  2312. { return *(const unsigned*)_cmJsonDeserialAdv(rp,sizeof(unsigned)); }
  2313. double _cmJsonDeserialReal( cmJsDeserial_t* rp )
  2314. { return *(const double*)_cmJsonDeserialAdv(rp,sizeof(double)); }
  2315. cmJsRC_t _cmJsonDeserializeNode( cmJs_t* p, cmJsonNode_t* parentPtr, cmJsDeserial_t* rp )
  2316. {
  2317. cmJsRC_t rc = kOkJsRC;
  2318. unsigned typeId = _cmJsonDeserialUint(rp);
  2319. unsigned childCnt = 0;
  2320. cmJsonNode_t* newParentPtr = NULL;
  2321. rp->nodeIdx++;
  2322. switch( typeId )
  2323. {
  2324. case kPairTId:
  2325. rc = _cmJsonCreateNode(p,parentPtr,typeId,&newParentPtr);
  2326. childCnt = 2;
  2327. break;
  2328. case kObjectTId:
  2329. case kArrayTId:
  2330. rc = _cmJsonCreateNode(p,parentPtr,typeId,&newParentPtr);
  2331. childCnt = _cmJsonDeserialUint(rp);
  2332. break;
  2333. case kStringTId:
  2334. {
  2335. unsigned sn = strlen(rp->nextPtr);
  2336. rc = _cmJsonCreateString( p, parentPtr, rp->nextPtr, sn,NULL);
  2337. _cmJsonDeserialAdv(rp,sn+1);
  2338. }
  2339. break;
  2340. case kIntTId:
  2341. {
  2342. int v = _cmJsonDeserialInt(rp);
  2343. rc = _cmJsonCreateInt( p, parentPtr, v, NULL );
  2344. }
  2345. break;
  2346. case kRealTId:
  2347. {
  2348. double v = _cmJsonDeserialReal(rp);
  2349. rc = _cmJsonCreateReal( p, parentPtr, v, NULL );
  2350. }
  2351. break;
  2352. case kNullTId:
  2353. rc = _cmJsonCreateNull( p, parentPtr, NULL );
  2354. break;
  2355. case kTrueTId:
  2356. rc = _cmJsonCreateBool( p, parentPtr, true, NULL );
  2357. break;
  2358. case kFalseTId:
  2359. rc = _cmJsonCreateBool( p, parentPtr, false, NULL );
  2360. break;
  2361. default:
  2362. assert(0);
  2363. break;
  2364. }
  2365. if( rc != kOkJsRC )
  2366. return rc;
  2367. // if the current node is a parent
  2368. if( childCnt > 0 )
  2369. {
  2370. unsigned i;
  2371. assert( newParentPtr != NULL );
  2372. for(i=0; i<childCnt; ++i)
  2373. if((rc= _cmJsonDeserializeNode( p, newParentPtr, rp )) != kOkJsRC )
  2374. return rc;
  2375. }
  2376. return rc;
  2377. }
  2378. cmJsRC_t _cmJsonDeserialize( cmJs_t* p, const void* vbuf, cmJsonNode_t* altRootPtr )
  2379. {
  2380. cmJsDeserial_t r;
  2381. cmJsonNode_t* rootPtr = altRootPtr == NULL ? p->rootPtr : altRootPtr;
  2382. const char* buf = (const char*)vbuf;
  2383. memset(&r,0,sizeof(r));
  2384. r.nextPtr = buf;
  2385. r.endPtr = buf + (4*sizeof(unsigned)); // the buf must at least contain a header
  2386. // read the buffer header
  2387. unsigned hdrId = _cmJsonDeserialUint(&r);
  2388. unsigned byteCnt = _cmJsonDeserialUint(&r);
  2389. r.nodeCnt = _cmJsonDeserialUint(&r);
  2390. /*unsigned version =*/ _cmJsonDeserialUint(&r);
  2391. if( hdrId != 'json' )
  2392. return _cmJsonError(p,kSerialErrJsRC,"The buffer does not have the correct header.");
  2393. if( byteCnt < (4*sizeof(unsigned)) )
  2394. return _cmJsonError(p,kSerialErrJsRC,"The buffer is too small to be contain any information.");
  2395. // change the buffer end pointer to the correct size based
  2396. // on the the byte count stored in the buffer
  2397. r.endPtr = buf + byteCnt + (2*sizeof(unsigned));
  2398. return _cmJsonDeserializeNode(p, rootPtr, &r );
  2399. }
  2400. cmJsRC_t cmJsonDeserialize( cmJsonH_t h, const void* bufPtr, cmJsonNode_t* altRootPtr )
  2401. {
  2402. cmJs_t* p = _cmJsonHandleToPtr(h);
  2403. return _cmJsonDeserialize(p,bufPtr,altRootPtr);
  2404. }
  2405. cmJsRC_t cmJsonLeafToString( const cmJsonNode_t* np, cmChar_t* buf, unsigned bufCharCnt )
  2406. {
  2407. const char* cp = NULL;
  2408. unsigned n = 0;
  2409. assert( buf!=NULL && bufCharCnt > 0 );
  2410. switch(np->typeId & kMaskTId )
  2411. {
  2412. case kStringTId:
  2413. cp = np->u.stringVal==NULL ? "" : np->u.stringVal;
  2414. break;
  2415. case kNullTId:
  2416. cp = "null";
  2417. break;
  2418. case kTrueTId:
  2419. cp = "true";
  2420. break;
  2421. case kFalseTId:
  2422. cp = "false";
  2423. break;
  2424. case kIntTId:
  2425. n = snprintf(buf,bufCharCnt,"%i",np->u.intVal)+1;
  2426. break;
  2427. case kRealTId:
  2428. n = snprintf(buf,bufCharCnt,"%f",np->u.realVal)+1;
  2429. break;
  2430. default:
  2431. assert(0);
  2432. return kInvalidNodeTypeJsRC;
  2433. }
  2434. if( cp != NULL )
  2435. {
  2436. n = strlen(cp)+1;
  2437. if( bufCharCnt < n )
  2438. n = bufCharCnt;
  2439. strncpy(buf,cp,n);
  2440. /*
  2441. n = strlen(np->u.stringVal)+1;
  2442. if( bufCharCnt < n )
  2443. n = bufCharCnt;
  2444. strncpy(buf,np->u.stringVal,n);
  2445. */
  2446. }
  2447. buf[bufCharCnt-1]=0;
  2448. assert( n>0 && n < bufCharCnt );
  2449. return n == bufCharCnt ? kBufTooSmallJsRC : kOkJsRC ;
  2450. }
  2451. cmJsRC_t _cmJsonRptCsvTokErr( cmJs_t* p, unsigned lineNo, cmLexH lexH, const char* iFn )
  2452. {
  2453. unsigned n = cmMin(31,cmLexTokenCharCount(lexH));
  2454. char b[n+1];
  2455. strncpy(b,cmLexTokenText(lexH),n);
  2456. b[n]=0;
  2457. return _cmJsonError( p, kCsvErrJsRC, "Unexpected token '%s' during CSV parse on line %i of '%s'.",b,cmLexCurrentLineNumber(lexH),cmStringNullGuard(iFn));
  2458. }
  2459. cmJsRC_t _cmJsonRptCsvTypeErr( cmJs_t* p, unsigned lineNo, cmLexH lexH, const char* iFn, const char* actualTypeStr, unsigned expTypeId )
  2460. {
  2461. unsigned n = cmMin(31,cmLexTokenCharCount(lexH));
  2462. char b[n+1];
  2463. strncpy(b,cmLexTokenText(lexH),n);
  2464. b[n]=0;
  2465. 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);
  2466. }
  2467. cmJsRC_t cmJsonFromCSV( cmJsonH_t h, const char* iFn, cmJsonNode_t* parentNodePtr, cmJsonNode_t** arrayNodePtrPtr )
  2468. {
  2469. enum
  2470. {
  2471. kCommaTokId = kUserLexTId+1,
  2472. kIntTokId,
  2473. kRealTokId,
  2474. kTrueTokId,
  2475. kFalseTokId,
  2476. kStringTokId,
  2477. kBoolTokId
  2478. };
  2479. typedef struct field_str
  2480. {
  2481. char* fieldLabel;
  2482. unsigned typeId;
  2483. struct field_str* linkPtr;
  2484. } field_t;
  2485. cmJsRC_t rc = kOkJsRC;
  2486. cmJs_t* p = _cmJsonHandleToPtr(h);
  2487. cmLexH lexH = cmLexInit( NULL, 0, 0, p->err.rpt );
  2488. field_t* fieldList = NULL;
  2489. cmJsonNode_t* arrayNodePtr = NULL;
  2490. cmJsonNode_t* objNodePtr = NULL;
  2491. unsigned lineNo = 0;
  2492. field_t* fieldPtr = NULL;
  2493. field_t* lp = NULL;
  2494. unsigned tokId;
  2495. unsigned fieldIdx = 0;
  2496. // validate the init state of the lexer
  2497. if( cmLexIsValid(lexH) == false )
  2498. {
  2499. rc = _cmJsonError( p, kLexErrJsRC, "Lexer initialization failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2500. goto errLabel;
  2501. }
  2502. // register CSV specific tokens
  2503. cmLexRegisterToken( lexH, kCommaTokId, ",");
  2504. cmLexRegisterToken( lexH, kIntTokId, "int");
  2505. cmLexRegisterToken( lexH, kRealTokId, "real");
  2506. cmLexRegisterToken( lexH, kTrueTokId, "true");
  2507. cmLexRegisterToken( lexH, kFalseTokId, "false");
  2508. cmLexRegisterToken( lexH, kStringTokId, "string");
  2509. cmLexRegisterToken( lexH, kBoolTokId, "bool");
  2510. // lex the file
  2511. if( cmLexSetFile( lexH, iFn ) != kOkLexRC )
  2512. {
  2513. rc = _cmJsonError( p, kLexErrJsRC, "Lex failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2514. goto errLabel;
  2515. }
  2516. // create the parent array
  2517. if((arrayNodePtr = cmJsonCreateArray( h, parentNodePtr )) == NULL )
  2518. {
  2519. rc = _cmJsonError( p, kCsvErrJsRC, "CSV array create failed during parse of '%s'.",cmStringNullGuard(iFn));
  2520. goto errLabel;
  2521. }
  2522. // iterate through the lexer file
  2523. while(((tokId = cmLexGetNextToken(lexH)) != kErrorLexTId) && (tokId != kEofLexTId) )
  2524. {
  2525. unsigned fieldTypeTokId = kInvalidTId;
  2526. switch( cmLexCurrentLineNumber(lexH) )
  2527. {
  2528. // line 1 contains the field type labels (e.g. int,real,string,true,false,bool)
  2529. case 1:
  2530. switch(tokId)
  2531. {
  2532. case kCommaTokId:
  2533. break;
  2534. case kIntTokId:
  2535. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kIntTId : fieldTypeTokId;
  2536. case kRealTokId:
  2537. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kRealTId : fieldTypeTokId;
  2538. case kTrueTokId:
  2539. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kTrueTId : fieldTypeTokId;
  2540. case kFalseTokId:
  2541. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kFalseTId : fieldTypeTokId;
  2542. case kBoolTokId:
  2543. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kFalseTId : fieldTypeTokId;
  2544. case kStringTokId:
  2545. fieldTypeTokId = (fieldTypeTokId==kInvalidTId) ? kStringTId : fieldTypeTokId;
  2546. // create and intitialize a new field
  2547. field_t* rp = cmMemAllocZ( field_t, 1 );
  2548. rp->fieldLabel = NULL;
  2549. rp->typeId = fieldTypeTokId;
  2550. rp->linkPtr = NULL;
  2551. // and add it to the end of the field list
  2552. if( fieldList == NULL )
  2553. fieldList = rp;
  2554. else
  2555. fieldPtr->linkPtr = rp;
  2556. // fieldPtr points to the end of the list
  2557. fieldPtr = rp;
  2558. break;
  2559. default:
  2560. rc = _cmJsonRptCsvTokErr( p, 1, lexH, iFn );
  2561. goto errLabel;
  2562. }
  2563. break;
  2564. // line 2 contains the field labels
  2565. case 2:
  2566. if( fieldIdx == 0 )
  2567. fieldPtr = fieldList;
  2568. ++fieldIdx;
  2569. switch(tokId)
  2570. {
  2571. case kCommaTokId:
  2572. break;
  2573. // all line 2 tokens must be identifiers or q-strings
  2574. case kIdentLexTId:
  2575. case kQStrLexTId:
  2576. if( fieldPtr == NULL )
  2577. {
  2578. rc = _cmJsonError( p, kCsvErrJsRC, "More fields on line 2 than type specifiers on line 1 of '%s'.",cmStringNullGuard(iFn));
  2579. goto errLabel;
  2580. }
  2581. else
  2582. {
  2583. // set the field name in the field list
  2584. unsigned n = cmLexTokenCharCount(lexH);
  2585. fieldPtr->fieldLabel = cmMemAllocZ( char, n+1 );
  2586. strncpy(fieldPtr->fieldLabel,cmLexTokenText(lexH),n);
  2587. fieldPtr->fieldLabel[n] = 0;
  2588. fieldPtr = fieldPtr->linkPtr;
  2589. }
  2590. break;
  2591. default:
  2592. rc = _cmJsonRptCsvTokErr( p, 2, lexH, iFn );
  2593. goto errLabel;
  2594. }
  2595. break;
  2596. // lines 3 to end of file contain data
  2597. default:
  2598. {
  2599. int ival = 0;
  2600. cmReal_t rval = 0;
  2601. // if we are starting a new line in the CSV file
  2602. if( lineNo != cmLexCurrentLineNumber(lexH) )
  2603. {
  2604. // verify that field ptr is pointing to the end of the field list
  2605. if( fieldPtr != NULL )
  2606. {
  2607. rc = _cmJsonError( p, kCsvErrJsRC, "Missing columns were detected on line %i in CSV file '%s'.", lineNo, cmStringNullGuard(iFn));
  2608. goto errLabel;
  2609. }
  2610. fieldPtr = fieldList;
  2611. lineNo = cmLexCurrentLineNumber(lexH);
  2612. // create the object to hold the fields on this line
  2613. if((objNodePtr = cmJsonCreateObject( h, arrayNodePtr )) == NULL )
  2614. {
  2615. rc = _cmJsonError( p, kCsvErrJsRC, "Object node create failed on line %i in CSV file '%s'.",lineNo,cmStringNullGuard(iFn));
  2616. goto errLabel;
  2617. }
  2618. }
  2619. if( tokId == kCommaTokId )
  2620. continue;
  2621. if( fieldPtr == NULL )
  2622. {
  2623. rc = _cmJsonError( p, kCsvErrJsRC, "More columns than fields on line %i in CSV file '%s'.", lineNo,cmStringNullGuard(iFn));
  2624. goto errLabel;
  2625. }
  2626. // given the tokens type convert the token string into a value
  2627. switch(tokId)
  2628. {
  2629. case kRealLexTId:
  2630. #ifdef CM_FLOAT_REAL
  2631. rval = cmLexTokenFloat(lexH);
  2632. #else
  2633. rval = cmLexTokenDouble(lexH);
  2634. #endif
  2635. ival = (int)rval;
  2636. if( fieldPtr->typeId == kStringTId )
  2637. {
  2638. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "numeric", fieldPtr->typeId );
  2639. goto errLabel;
  2640. }
  2641. break;
  2642. case kIntLexTId:
  2643. case kHexLexTId:
  2644. ival = cmLexTokenInt(lexH);
  2645. rval = ival;
  2646. if( fieldPtr->typeId == kStringTId )
  2647. {
  2648. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "numeric", fieldPtr->typeId );
  2649. goto errLabel;
  2650. }
  2651. break;
  2652. case kTrueTokId:
  2653. ival = 1;
  2654. rval = 1.0;
  2655. break;
  2656. case kFalseTokId:
  2657. ival = 0;
  2658. rval = 0.0;
  2659. break;
  2660. case kIdentLexTId:
  2661. case kQStrLexTId:
  2662. if( fieldPtr->typeId != kStringTId )
  2663. {
  2664. rc = _cmJsonRptCsvTypeErr(p, lineNo, lexH, iFn, "string", fieldPtr->typeId );
  2665. goto errLabel;
  2666. }
  2667. break;
  2668. default:
  2669. rc = _cmJsonRptCsvTokErr( p, lineNo, lexH, iFn );
  2670. goto errLabel;
  2671. }
  2672. // create the pair object from the current field label and value
  2673. switch(fieldPtr->typeId)
  2674. {
  2675. case kIntTId:
  2676. rc = cmJsonInsertPairInt( h, objNodePtr, fieldPtr->fieldLabel, ival );
  2677. break;
  2678. case kRealTId:
  2679. rc = cmJsonInsertPairReal( h, objNodePtr, fieldPtr->fieldLabel, rval );
  2680. break;
  2681. case kTrueTId:
  2682. case kFalseTId:
  2683. rc = cmJsonInsertPairBool( h, objNodePtr, fieldPtr->fieldLabel, ival );
  2684. break;
  2685. case kStringTId:
  2686. rc = cmJsonInsertPairStringN( h, objNodePtr, fieldPtr->fieldLabel, cmLexTokenText(lexH), cmLexTokenCharCount(lexH) );
  2687. break;
  2688. default:
  2689. {
  2690. assert(0);
  2691. goto errLabel;
  2692. }
  2693. }
  2694. if( rc != kOkJsRC )
  2695. goto errLabel;
  2696. fieldPtr = fieldPtr->linkPtr;
  2697. }
  2698. break;
  2699. }
  2700. }
  2701. errLabel:
  2702. if( cmLexFinal(&lexH) != kOkLexRC )
  2703. {
  2704. rc = _cmJsonError( p, kLexErrJsRC, "Lexer finalize failed on CSV parse of '%s'.",cmStringNullGuard(iFn));
  2705. goto errLabel;
  2706. }
  2707. lp = fieldList;
  2708. while( lp!=NULL )
  2709. {
  2710. field_t* pp = lp->linkPtr;
  2711. cmMemPtrFree(&lp->fieldLabel);
  2712. cmMemPtrFree(&lp);
  2713. lp = pp;
  2714. }
  2715. if( rc != kOkJsRC )
  2716. _cmJsonRemoveNode( p, arrayNodePtr, true, true );
  2717. if( rc == kOkJsRC && arrayNodePtrPtr != NULL )
  2718. *arrayNodePtrPtr = arrayNodePtr;
  2719. return rc;
  2720. }
  2721. cmJsRC_t cmJsonToCSV( cmJsonH_t h, const char* oFn, const cmJsonNode_t* arrayNodePtr )
  2722. {
  2723. assert( arrayNodePtr->typeId == kArrayTId );
  2724. typedef struct r_str
  2725. {
  2726. const char* fieldLabel;
  2727. unsigned typeId;
  2728. struct r_str* linkPtr;
  2729. } field_t;
  2730. cmJs_t* p = _cmJsonHandleToPtr(h);
  2731. cmJsRC_t rc = kOkJsRC;
  2732. unsigned arrayCnt = cmJsonChildCount(arrayNodePtr);
  2733. field_t* fieldList = NULL;
  2734. FILE* fp = NULL;
  2735. field_t* lp = NULL;
  2736. unsigned i,j;
  2737. // for each object in the array
  2738. for(i=0; i<arrayCnt; ++i)
  2739. {
  2740. const cmJsonNode_t* objNodePtr = cmJsonArrayElementC( arrayNodePtr, i );
  2741. assert( objNodePtr->typeId == kObjectTId );
  2742. // for each pair in the object
  2743. for(j=0; j<cmJsonChildCount(objNodePtr); ++j)
  2744. {
  2745. const cmJsonNode_t* pairNodePtr = cmJsonArrayElementC( objNodePtr, j );
  2746. const char* pairLabel = cmJsonPairLabel(pairNodePtr);
  2747. lp = fieldList;
  2748. // find this field in the field list
  2749. for(; lp != NULL; lp = lp->linkPtr )
  2750. if( strcmp(lp->fieldLabel,pairLabel) == 0)
  2751. {
  2752. unsigned typeId = cmJsonPairTypeId(pairNodePtr);
  2753. switch( typeId )
  2754. {
  2755. case kIntTId:
  2756. case kRealTId:
  2757. case kTrueTId:
  2758. case kFalseTId:
  2759. case kStringTId:
  2760. break;
  2761. default:
  2762. rc = _cmJsonError( p, kInvalidNodeTypeJsRC, "Field '%s' has type '%s' which cannot be written by cmJsonToCSV().",cmStringNullGuard(pairLabel),_cmJsonNodeTypeIdToLabel(typeId) );
  2763. goto errLabel;
  2764. }
  2765. if( typeId != lp->typeId )
  2766. {
  2767. 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) );
  2768. goto errLabel;
  2769. }
  2770. break;
  2771. }
  2772. // if this field was not found then insert it
  2773. if( lp == NULL )
  2774. {
  2775. field_t* rp = (field_t*)cmLHeapAlloc(p->heapH,sizeof(field_t));
  2776. rp->fieldLabel = pairLabel;
  2777. rp->linkPtr = fieldList;
  2778. rp->typeId = cmJsonPairTypeId(pairNodePtr);
  2779. fieldList = rp;
  2780. }
  2781. }
  2782. }
  2783. // create the output file
  2784. if((fp = fopen(oFn,"wt")) == NULL )
  2785. {
  2786. rc = _cmJsonError( p, kFileCreateErrJsRC, "CSV file '%s' create failed.", oFn );
  2787. goto errLabel;
  2788. }
  2789. // write the field type
  2790. lp = fieldList;
  2791. for(; lp!=NULL; lp=lp->linkPtr)
  2792. {
  2793. fprintf(fp,"%s", _cmJsonNodeTypeIdToLabel(lp->typeId));
  2794. if( lp->linkPtr != NULL )
  2795. fprintf(fp,",");
  2796. }
  2797. fprintf(fp,"\n");
  2798. // write the field label
  2799. lp = fieldList;
  2800. for(; lp!=NULL; lp=lp->linkPtr)
  2801. {
  2802. fprintf(fp,"\"%s\"", lp->fieldLabel);
  2803. if( lp->linkPtr != NULL )
  2804. fprintf(fp,",");
  2805. }
  2806. fprintf(fp,"\n");
  2807. // for each object in the array
  2808. for(i=0; i<arrayCnt; ++i)
  2809. {
  2810. unsigned j;
  2811. const cmJsonNode_t* objNodePtr = cmJsonArrayElementC( arrayNodePtr, i );
  2812. unsigned fieldCnt = cmJsonChildCount(objNodePtr);
  2813. lp = fieldList;
  2814. // for each field ...
  2815. for(j=0; lp!=NULL; lp=lp->linkPtr,++j)
  2816. {
  2817. cmJsonNode_t* valNodePtr;
  2818. // ... locate the pair given the field label
  2819. if((valNodePtr = cmJsonFindValue(h,lp->fieldLabel, objNodePtr, lp->typeId )) == NULL)
  2820. {
  2821. // no pair was found for the field label - output a NULL value
  2822. switch( lp->typeId )
  2823. {
  2824. case kIntTId:
  2825. fprintf(fp,"%i",0);
  2826. break;
  2827. case kRealTId:
  2828. fprintf(fp,"%f",0.0);
  2829. break;
  2830. case kTrueTId:
  2831. case kFalseTId:
  2832. fprintf(fp,"%i",0);
  2833. break;
  2834. case kStringTId:
  2835. fprintf(fp,"\"\"");
  2836. break;
  2837. default:
  2838. assert(0);
  2839. break;
  2840. }
  2841. //rc = _cmJsonError( p, kNodeNotFoundJsRC,"No field with label '%s' was found.", lp->fieldLabel);
  2842. //goto errLabel;
  2843. }
  2844. else
  2845. {
  2846. switch( valNodePtr->typeId )
  2847. {
  2848. case kIntTId:
  2849. fprintf(fp,"%i", valNodePtr->u.intVal);
  2850. break;
  2851. case kRealTId:
  2852. fprintf(fp,"%e", valNodePtr->u.realVal);
  2853. break;
  2854. case kTrueTId:
  2855. case kFalseTId:
  2856. fprintf(fp,"%i", valNodePtr->u.boolVal);
  2857. break;
  2858. case kStringTId:
  2859. fprintf(fp,"\"%s\"", valNodePtr->u.stringVal);
  2860. break;
  2861. default:
  2862. assert(0);
  2863. break;
  2864. }
  2865. }
  2866. if( j < fieldCnt-1 )
  2867. fprintf(fp,",");
  2868. }
  2869. fprintf(fp,"\n");
  2870. }
  2871. errLabel:
  2872. if( fp != NULL )
  2873. fclose(fp);
  2874. lp = fieldList;
  2875. while( lp!=NULL )
  2876. {
  2877. field_t* pp = lp->linkPtr;
  2878. cmLHeapFree(p->heapH,lp);
  2879. lp = pp;
  2880. }
  2881. return rc;
  2882. }
  2883. void _cmJsonPrintIndent( cmRpt_t* rpt, unsigned indent )
  2884. {
  2885. if( indent )
  2886. {
  2887. char spaces[indent+1];
  2888. spaces[indent]=0;
  2889. memset(spaces,' ',indent);
  2890. cmRptPrint(rpt,spaces);
  2891. }
  2892. }
  2893. void _cmJsonPrintNode( const cmJsonNode_t* np, cmRpt_t* rpt, unsigned indent )
  2894. {
  2895. unsigned childCnt = 0;
  2896. char eoObjStr[] = "}";
  2897. char eoArrStr[] = "]";
  2898. char eoPairStr[] = "\n";
  2899. char commaStr[] = ",\n";
  2900. char colonStr[] = ": ";
  2901. const char* eleStr = NULL;
  2902. const char* lastEleStr = NULL;
  2903. const char* eoStr = NULL;
  2904. unsigned localIndent = 0;
  2905. switch(np->typeId)
  2906. {
  2907. case kObjectTId:
  2908. cmRptPrint(rpt,"\n");
  2909. _cmJsonPrintIndent(rpt,indent);
  2910. cmRptPrint(rpt,"{\n");
  2911. childCnt = cmJsonChildCount(np);
  2912. eoStr = eoObjStr;
  2913. localIndent = 2;
  2914. break;
  2915. case kArrayTId:
  2916. cmRptPrint(rpt,"\n");
  2917. _cmJsonPrintIndent(rpt,indent);
  2918. cmRptPrint(rpt,"[\n");
  2919. childCnt = cmJsonChildCount(np);
  2920. eoStr = eoArrStr;
  2921. localIndent = 2;
  2922. eleStr = commaStr;
  2923. lastEleStr = "\n";
  2924. break;
  2925. case kPairTId:
  2926. childCnt = cmJsonChildCount(np);
  2927. eleStr = colonStr;
  2928. eoStr = eoPairStr;
  2929. break;
  2930. case kStringTId:
  2931. {
  2932. const char* fmt0 = "\"%s\" ";
  2933. const char* fmt1 = "%s";
  2934. const char* fmt = fmt0;
  2935. // if this string is the label part of a pair
  2936. if( np->u.stringVal != NULL && np->ownerPtr != NULL && cmJsonIsPair(np->ownerPtr) && np->ownerPtr->u.childPtr == np )
  2937. {
  2938. // and the label has no white space
  2939. char* cp = np->u.stringVal;
  2940. while( *cp!=0 && isspace(*cp)==false )
  2941. ++cp;
  2942. // then print without quotes
  2943. if( *cp == 0 )
  2944. fmt = fmt1;
  2945. }
  2946. cmRptPrintf(rpt,fmt,np->u.stringVal==NULL ? "" : np->u.stringVal);
  2947. }
  2948. break;
  2949. case kIntTId:
  2950. cmRptPrintf(rpt,"%i ",np->u.intVal);
  2951. break;
  2952. case kRealTId:
  2953. cmRptPrintf(rpt,"%f ",np->u.realVal);
  2954. break;
  2955. case kNullTId:
  2956. cmRptPrint(rpt,"null ");
  2957. break;
  2958. case kTrueTId:
  2959. cmRptPrint(rpt,"true ");
  2960. break;
  2961. case kFalseTId:
  2962. cmRptPrint(rpt,"false ");
  2963. break;
  2964. }
  2965. if( childCnt )
  2966. {
  2967. indent += localIndent;
  2968. unsigned i;
  2969. cmJsonNode_t* cnp = np->u.childPtr;
  2970. for(i=0; i<childCnt; ++i)
  2971. {
  2972. assert(cnp != NULL);
  2973. if( np->typeId != kPairTId )
  2974. _cmJsonPrintIndent(rpt,indent);
  2975. _cmJsonPrintNode(cnp,rpt,indent);
  2976. cnp = cnp->siblingPtr;
  2977. if( i < childCnt-1 && eleStr != NULL )
  2978. cmRptPrint(rpt,eleStr);
  2979. if( i == childCnt-1 && lastEleStr != NULL )
  2980. cmRptPrint(rpt,lastEleStr);
  2981. }
  2982. indent -= localIndent;
  2983. }
  2984. if( eoStr != NULL )
  2985. {
  2986. _cmJsonPrintIndent(rpt,indent);
  2987. cmRptPrint(rpt,eoStr);
  2988. }
  2989. }
  2990. void cmJsonPrintTree( const cmJsonNode_t* np, cmRpt_t* rpt )
  2991. {
  2992. _cmJsonPrintNode(np,rpt,0);
  2993. }
  2994. void _cmJsPrintFile(void* cmRptUserPtr, const cmChar_t* text)
  2995. {
  2996. cmFileH_t* hp = (cmFileH_t*)cmRptUserPtr;
  2997. cmFilePrint(*hp,text);
  2998. }
  2999. cmJsRC_t cmJsonWrite( cmJsonH_t h, const cmJsonNode_t* np, const cmChar_t* fn )
  3000. {
  3001. cmRpt_t rpt;
  3002. cmFileH_t fh = cmFileNullHandle;
  3003. cmJs_t* p = _cmJsonHandleToPtr(h);
  3004. if( np == NULL )
  3005. np = cmJsonRoot(h);
  3006. // create the output file
  3007. if( cmFileOpen(&fh,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  3008. return _cmJsonError( p, kFileCreateErrJsRC, "Output file '%s' create failed.", fn );
  3009. // setup a reporter to write to the file
  3010. cmRptSetup(&rpt,_cmJsPrintFile,_cmJsPrintFile,&fh);
  3011. // print the tree to the file
  3012. cmJsonPrintTree(np,&rpt);
  3013. // close the file
  3014. if( cmFileClose(&fh) != kOkFileRC )
  3015. return _cmJsonError( p, kFileCloseErrJsRC, "Output file '%s' close failed.", fn );
  3016. return kOkJsRC;
  3017. }
  3018. cmJsRC_t cmJsonReport( cmJsonH_t h )
  3019. {
  3020. cmJsRC_t rc;
  3021. cmJs_t* p = _cmJsonHandleToPtr(h);
  3022. if((rc = cmJsonValidateTree(h)) != kOkJsRC )
  3023. return rc;
  3024. if(p->rootPtr != NULL )
  3025. _cmJsonPrintNode(p->rootPtr,p->err.rpt,0);
  3026. return rc;
  3027. }
  3028. cmJsRC_t cmJsonErrorCode( cmJsonH_t h )
  3029. {
  3030. cmJs_t* p = _cmJsonHandleToPtr(h);
  3031. return p->rc;
  3032. }
  3033. void cmJsonClearErrorCode( cmJsonH_t h )
  3034. {
  3035. cmJs_t* p = _cmJsonHandleToPtr(h);
  3036. p->rc = kOkJsRC;
  3037. }
  3038. void _cmJsonTestVPrint( void* rptDataPtr, const char* fmt, va_list vl )
  3039. {
  3040. vfprintf(stdout,fmt,vl);
  3041. }
  3042. void _cmJsonTestPrint( void* userPtr, const cmChar_t* text )
  3043. {
  3044. fputs(text,stdout);
  3045. }
  3046. //{ { label:cmJsonEx }
  3047. //(
  3048. // cmJsonTest() demonstrates some JSON tree operations.
  3049. //)
  3050. //[
  3051. cmJsRC_t cmJsonTest( const char* fn, cmCtx_t* ctx )
  3052. {
  3053. cmJsRC_t rc = kOkJsRC;
  3054. cmJsRC_t rc1 = kOkJsRC;
  3055. cmJsonH_t h = cmJsonNullHandle;
  3056. cmJsonH_t h1 = cmJsonNullHandle;
  3057. void* sbp = NULL;
  3058. unsigned sbn = 0;
  3059. cmJsonNode_t* np = NULL;
  3060. cmRpt_t* rpt = &ctx->rpt;
  3061. // initialize an empty JSON tree
  3062. if((rc = cmJsonInitialize(&h,ctx)) != kOkJsRC )
  3063. goto errLabel;
  3064. // load the tree from a file
  3065. if((rc = cmJsonParseFile(h,fn,NULL)) != kOkJsRC )
  3066. goto errLabel;
  3067. // print the tree
  3068. cmJsonReport(h);
  3069. // find an array member named 'mem14'
  3070. if((np = cmJsonFindValue(h,"mem14",NULL,kArrayTId)) == NULL )
  3071. cmRptPrint(rpt,"'mem14' not found.\n");
  3072. else
  3073. {
  3074. cmRptPrint(rpt,"'mem14' found.\n");
  3075. cmJsonPrintTree(np,rpt);
  3076. }
  3077. // remove the array node from the tree
  3078. cmJsonRemoveNode(h,np, true);
  3079. cmRptPrint(rpt,"mem14 removed.\n");
  3080. // print the tree with the array node removed
  3081. cmJsonPrintTree( cmJsonRoot(h), rpt );
  3082. // serialize the tree into a dynamically allocated
  3083. // buffer sbp[sbn].
  3084. if((rc = cmJsonSerializeTree(h,NULL,&sbp,&sbn)) != kOkJsRC )
  3085. goto errLabel;
  3086. else
  3087. cmRptPrint(rpt,"***Serialize Ok.****\n");
  3088. // initialize an empty JSON tree
  3089. if((rc = cmJsonInitialize(&h1,ctx)) != kOkJsRC )
  3090. goto errLabel;
  3091. // deserialize sbp[sbn] into the empty tree
  3092. if((rc = cmJsonDeserialize(h1,sbp,NULL)) != kOkJsRC )
  3093. goto errLabel;
  3094. else
  3095. {
  3096. cmJsonPrintTree( cmJsonRoot(h1),rpt);
  3097. cmRptPrint(rpt,"***Deserialize Ok.****\n");
  3098. }
  3099. // find an member node named 'mem5'
  3100. if((np = cmJsonFindValue(h,"mem5",NULL,0)) == NULL )
  3101. cmRptPrint(rpt,"mem5 not found.");
  3102. // merge two sub-trees
  3103. if( cmJsonMergeObjectNodes( h, np->u.childPtr,
  3104. np->u.childPtr->siblingPtr) != kOkJsRC )
  3105. {
  3106. cmRptPrint(rpt,"merge failed.");
  3107. }
  3108. else
  3109. {
  3110. cmJsonReport(h);
  3111. }
  3112. errLabel:
  3113. // release the JSON trees
  3114. rc = cmJsonFinalize(&h);
  3115. rc1 = cmJsonFinalize(&h1);
  3116. return rc == kOkJsRC ? rc1 : rc;
  3117. }
  3118. //]
  3119. //}