libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmJson.c 108KB

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