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

cmXml.c 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmFloatTypes.h"
  6. #include "cmRpt.h"
  7. #include "cmErr.h"
  8. #include "cmCtx.h"
  9. #include "cmJson.h"
  10. #include "cmMem.h"
  11. #include "cmMallocDebug.h"
  12. #include "cmLex.h"
  13. #include "cmLinkedHeap.h"
  14. #include "cmFile.h"
  15. #include "cmXml.h"
  16. #include "cmText.h"
  17. /*
  18. To Do:
  19. 1) Escape node data strings and attribute values.
  20. 2) Attribute values must be quoted by they may be quoted with either single or double quotes.
  21. 3) Consider not buffering the XML file and reading directly from the file.
  22. */
  23. cmXmlH_t cmXmlNullHandle = cmSTATIC_NULL_HANDLE;
  24. typedef struct
  25. {
  26. cmErr_t err; //
  27. cmLHeapH_t heapH; // linked heap stores all node memory
  28. cmChar_t* b; // base of the text buffer
  29. unsigned bn; // length of the text buffer in characters
  30. cmChar_t* c; // current lexer position
  31. unsigned line; // lexer line number
  32. cmXmlNode_t* root; // root XML tree node
  33. cmXmlNode_t* doctype; // DOCTYPE node
  34. cmXmlNode_t* stack; // parsing stack
  35. } cmXml_t;
  36. cmXml_t* _cmXmlHandleToPtr( cmXmlH_t h )
  37. {
  38. cmXml_t* p = (cmXml_t*)h.h;
  39. assert( p != NULL );
  40. return p;
  41. }
  42. cmXmlRC_t _cmXmlFree( cmXml_t* p )
  43. {
  44. cmXmlRC_t rc = kOkXmlRC;
  45. cmLHeapDestroy( &p->heapH );
  46. cmMemPtrFree(&p->b);
  47. p->bn = 0;
  48. p->c = NULL;
  49. cmMemFree(p);
  50. return rc;
  51. }
  52. cmXmlRC_t cmXmlAlloc( cmCtx_t* ctx, cmXmlH_t* hp, const cmChar_t* fn )
  53. {
  54. cmXmlRC_t rc = kOkXmlRC;
  55. cmXml_t* p = NULL;
  56. // finalize before initialize
  57. if((rc = cmXmlFree(hp)) != kOkXmlRC )
  58. return rc;
  59. // allocate the main object record
  60. if((p = cmMemAllocZ( cmXml_t, 1 )) == NULL )
  61. return cmErrMsg(&ctx->err,kMemAllocErrXmlRC,"Object memory allocation failed.");
  62. cmErrSetup(&p->err,&ctx->rpt,"XML Parser");
  63. // allocate the linked heap mgr
  64. if( cmLHeapIsValid(p->heapH = cmLHeapCreate(1024,ctx)) == false )
  65. {
  66. rc = cmErrMsg(&p->err,kMemAllocErrXmlRC,"Linked heap object allocation failed.");
  67. goto errLabel;
  68. }
  69. hp->h = p;
  70. if( fn != NULL )
  71. if((rc = cmXmlParse(*hp,fn)) != kOkXmlRC )
  72. hp->h = NULL;
  73. errLabel:
  74. if(rc != kOkXmlRC )
  75. _cmXmlFree(p);
  76. return rc;
  77. }
  78. cmXmlRC_t cmXmlFree( cmXmlH_t* hp )
  79. {
  80. cmXmlRC_t rc = kOkXmlRC;
  81. if( hp==NULL || cmXmlIsValid(*hp)==false )
  82. return kOkXmlRC;
  83. cmXml_t* p = _cmXmlHandleToPtr(*hp);
  84. if((rc = _cmXmlFree(p)) != kOkXmlRC )
  85. return rc;
  86. hp->h = NULL;
  87. return rc;
  88. }
  89. bool cmXmlIsValid( cmXmlH_t h )
  90. { return h.h != NULL; }
  91. cmXmlRC_t _cmXmlSyntaxError( cmXml_t* p )
  92. {
  93. return cmErrMsg(&p->err,kSyntaxErrorXmlRC,"Syntax error on line %i.",p->line);
  94. }
  95. cmXmlNode_t* _cmXmlNodeAlloc( cmXml_t* p, unsigned flags, const cmChar_t* label, unsigned labelN )
  96. {
  97. cmXmlNode_t* np = cmLhAllocZ(p->heapH,cmXmlNode_t,1);
  98. np->parent = p->stack;
  99. if( p->stack != NULL )
  100. {
  101. if( p->stack->children == NULL )
  102. p->stack->children = np;
  103. else
  104. {
  105. cmXmlNode_t* n0p = NULL;
  106. cmXmlNode_t* n1p = p->stack->children;
  107. for(; n1p != NULL; n1p=n1p->sibling )
  108. n0p = n1p;
  109. n0p->sibling = np;
  110. }
  111. }
  112. // all new nodes are put on the top of the stack
  113. p->stack = np;
  114. // all nodes must have a valid 'type' flag
  115. if( (flags & kTypeXmlFlags) == 0 )
  116. {
  117. _cmXmlSyntaxError(p);
  118. return NULL;
  119. }
  120. // if this is the root node
  121. if( cmIsFlag(flags,kRootXmlFl) )
  122. {
  123. assert( p->root == NULL );
  124. p->root = np;
  125. }
  126. // if this is the 'doctype' node
  127. if( cmIsFlag(flags,kDoctypeXmlFl ) )
  128. p->doctype = np;
  129. if( label != NULL )
  130. np->label = cmLhAllocStrN(p->heapH,label,labelN);
  131. np->line = p->line;
  132. np->flags = flags;
  133. return np;
  134. }
  135. cmXmlNode_t* _cmXmlAttrAlloc( cmXml_t* p, cmXmlNode_t* np, const cmChar_t* label, unsigned labelN, const cmChar_t* value, unsigned valueN )
  136. {
  137. cmXmlAttr_t* ap = cmLhAllocZ(p->heapH, cmXmlAttr_t,1);
  138. if( label != NULL && labelN > 0 )
  139. ap->label = cmLhAllocStrN(p->heapH,label,labelN);
  140. if( value != NULL && valueN > 0 )
  141. ap->value = cmLhAllocStrN(p->heapH,value,valueN);
  142. ap->link = np->attr;
  143. np->attr = ap;
  144. return np;
  145. }
  146. bool _cmXmlIsEof( cmXml_t* p )
  147. { return p->c >= p->b + p->bn; }
  148. // Return false if EOF is encountered
  149. bool _cmXmlAdvance( cmXml_t* p )
  150. {
  151. if( _cmXmlIsEof(p) )
  152. return false;
  153. p->c += 1;
  154. if( *p->c == '\n' )
  155. p->line += 1;
  156. return true;
  157. }
  158. // Advance the cursor to the next non-white char
  159. // Return a pointer to a non-space character.
  160. // Return NULL if the EOF is encountered.
  161. const cmChar_t* _cmXmlAdvanceToNextNonWhite( cmXml_t* p )
  162. {
  163. if( _cmXmlIsEof(p) )
  164. return NULL;
  165. while( isspace(*p->c) )
  166. if( _cmXmlAdvance(p) == false )
  167. return NULL;
  168. return p->c;
  169. }
  170. // Advance to the next white space character or 'c'.
  171. // Returns a pointer to a white space or 'c'.
  172. const cmChar_t* _cmXmlAdvanceToNextWhiteOr( cmXml_t* p, cmChar_t c0, cmChar_t c1 )
  173. {
  174. if( _cmXmlIsEof(p) )
  175. return NULL;
  176. while( isspace(*p->c)==false && *p->c!=c0 && *p->c!=c1 )
  177. if(_cmXmlAdvance(p) == false )
  178. return NULL;
  179. return p->c;
  180. }
  181. // Advance past leading white space followed by 's'.
  182. // Note that 's' is expected to immediately follow any leading white space.
  183. // Returns a pointer to the character after 's'.
  184. // Returns NULL if 'c' is not encountered
  185. const cmChar_t* _cmXmlAdvancePast( cmXml_t* p, const cmChar_t* s )
  186. {
  187. if( _cmXmlIsEof(p) )
  188. return NULL;
  189. while( isspace(*p->c) )
  190. if( _cmXmlAdvance(p) == false )
  191. return NULL;
  192. for(; *s && *p->c == *s; ++s )
  193. if( _cmXmlAdvance(p) == false )
  194. return NULL;
  195. return *s==0 ? p->c : NULL;
  196. }
  197. // Advance past the current character and then
  198. // advance to the next occurrence of 's' and return
  199. // a pointer to the last char in 's'.
  200. const cmChar_t* _cmXmlAdvanceToNext( cmXml_t* p, cmChar_t* s )
  201. {
  202. unsigned i = 0;
  203. unsigned n = strlen(s);
  204. while( i<n && _cmXmlAdvance(p) )
  205. {
  206. if( i>0 && *p->c == s[i] )
  207. {
  208. i += 1;
  209. }
  210. else
  211. {
  212. i = *p->c==s[0];
  213. }
  214. }
  215. return p->c;
  216. }
  217. // Return the character following the current character.
  218. const cmChar_t* _cmXmlAdvanceOne( cmXml_t* p )
  219. {
  220. if( _cmXmlAdvance(p) )
  221. return p->c;
  222. return NULL;
  223. }
  224. cmXmlRC_t _cmXmlParseAttr( cmXml_t* p, cmChar_t endChar, cmXmlNode_t* np )
  225. {
  226. cmXmlRC_t rc = kOkXmlRC;
  227. const cmChar_t* l0 = NULL;
  228. const cmChar_t* l1 = NULL;
  229. const cmChar_t* v0 = NULL;
  230. const cmChar_t* v1 = NULL;
  231. // advance to the next label
  232. if(( l0 = _cmXmlAdvanceToNextNonWhite(p)) == NULL )
  233. return _cmXmlSyntaxError(p);
  234. // if the 'endChar' was encountered
  235. if( *p->c == endChar )
  236. return kOkXmlRC;
  237. // advance past last character in label
  238. if((l1 = _cmXmlAdvanceToNextWhiteOr(p,'=',' ')) == NULL )
  239. return _cmXmlSyntaxError(p);
  240. // advance past the next '='
  241. if( _cmXmlAdvancePast(p,"=") == NULL )
  242. {
  243. if( endChar=='?')
  244. {
  245. if((v1 = _cmXmlAdvanceToNext(p,"?")) != NULL )
  246. {
  247. v0 = l1+1;
  248. goto allocLabel;
  249. }
  250. }
  251. return _cmXmlSyntaxError(p);
  252. }
  253. // advance to the next non-white character
  254. if((v0 = _cmXmlAdvanceToNextNonWhite(p)) == NULL )
  255. return _cmXmlSyntaxError(p);
  256. // the first character in the value must be a single quote
  257. if( *p->c == '\'' )
  258. {
  259. if((v0 = _cmXmlAdvanceOne(p)) == NULL )
  260. return _cmXmlSyntaxError(p);
  261. // advance to the next single quote
  262. v1 = _cmXmlAdvanceToNext(p,"'");
  263. }
  264. else
  265. {
  266. v1 = _cmXmlAdvanceToNextWhiteOr(p,endChar,' ');
  267. }
  268. if( v1 == NULL )
  269. return _cmXmlSyntaxError(p);
  270. // advance past the ending single quote
  271. if( *p->c != endChar )
  272. if( _cmXmlAdvanceOne(p) == NULL )
  273. return _cmXmlSyntaxError(p);
  274. allocLabel:
  275. _cmXmlAttrAlloc(p, np, l0, l1-l0, v0, v1-v0 );
  276. // p->c now points just past the ending single quote
  277. return rc;
  278. }
  279. cmXmlRC_t _cmXmlParseAttrList( cmXml_t* p, cmChar_t endChar, cmXmlNode_t* np )
  280. {
  281. cmXmlRC_t rc = kOkXmlRC;
  282. while( *p->c != endChar && *p->c != '>' )
  283. if((rc = _cmXmlParseAttr(p,endChar,np)) != kOkXmlRC )
  284. break;
  285. if( *p->c == endChar )
  286. {
  287. // if this node is terminated at the end of its beginning tag
  288. if( endChar == '/' )
  289. {
  290. np->flags = cmSetFlag(np->flags,kClosedXmlFl);
  291. //p->stack = p->stack->parent;
  292. }
  293. if( _cmXmlAdvanceOne(p) == NULL )
  294. return _cmXmlSyntaxError(p);
  295. }
  296. if( *p->c != '>' )
  297. return _cmXmlSyntaxError(p);
  298. if( _cmXmlAdvancePast(p,">") == NULL )
  299. return _cmXmlSyntaxError(p);
  300. // p->c is now past the ending '>'
  301. return rc;
  302. }
  303. cmXmlRC_t _cmXmlParseDoctypeToken( cmXml_t* p, cmXmlNode_t* np )
  304. {
  305. const cmChar_t* t0 = NULL;
  306. const cmChar_t* t1 = NULL;
  307. // advance to the first char in the doctype token
  308. if((t0 = _cmXmlAdvanceToNextNonWhite(p) ) == NULL )
  309. {
  310. return _cmXmlSyntaxError(p);
  311. }
  312. // if the end of the tag was encountered
  313. if( *p->c == '>' )
  314. return kOkXmlRC;
  315. // if the token begins with a quote
  316. if( *p->c == '\'' )
  317. {
  318. if((t1 = _cmXmlAdvanceToNext(p,"'")) == NULL )
  319. return _cmXmlSyntaxError(p);
  320. if( _cmXmlAdvanceOne(p) == NULL )
  321. return _cmXmlSyntaxError(p);
  322. }
  323. else
  324. {
  325. if((t1 = _cmXmlAdvanceToNextWhiteOr(p,'>',' ')) == NULL )
  326. return _cmXmlSyntaxError(p);
  327. }
  328. // t1 and p->c now point just past the last character in the token
  329. return kOkXmlRC;
  330. }
  331. cmXmlRC_t _cmXmlParseDoctype( cmXml_t* p, cmXmlNode_t** newNodeRef )
  332. {
  333. cmXmlRC_t rc = kOkXmlRC;
  334. if((*newNodeRef = _cmXmlNodeAlloc(p,kDoctypeXmlFl | kClosedXmlFl,"DOCTYPE",strlen("DOCTYPE"))) == NULL )
  335. return cmErrLastRC(&p->err);
  336. while( *p->c != '>' )
  337. if((rc = _cmXmlParseDoctypeToken(p,*newNodeRef)) != kOkXmlRC )
  338. break;
  339. if( *p->c == '>' )
  340. _cmXmlAdvanceOne(p);
  341. return rc;
  342. }
  343. // Node tags are tags that begin with a '<' and are not
  344. // followed by any special character.
  345. cmXmlRC_t _cmXmlParseNodeTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  346. {
  347. cmXmlRC_t rc = kOkXmlRC;
  348. const cmChar_t* l0 = NULL;
  349. const cmChar_t* l1 = NULL;
  350. // Advance to the first character of the tag label.
  351. if((l0 = _cmXmlAdvanceToNextNonWhite(p)) == NULL )
  352. return _cmXmlSyntaxError(p);
  353. // Advance to the last character following the tag label.
  354. if((l1 = _cmXmlAdvanceToNextWhiteOr(p,'/','>')) == NULL )
  355. return _cmXmlSyntaxError(p);
  356. // Create the node.
  357. if( (*newNodeRef = _cmXmlNodeAlloc(p,kNormalXmlFl,l0,l1-l0)) == NULL )
  358. return cmErrLastRC(&p->err);
  359. // look for attributes
  360. if((rc = _cmXmlParseAttrList(p,'/',*newNodeRef)) != kOkXmlRC )
  361. return _cmXmlSyntaxError(p);
  362. // p->c is now past the ending '>'
  363. return rc;
  364. }
  365. cmXmlRC_t _cmXmlParseDeclTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  366. {
  367. assert( *p->c == '?' );
  368. const cmChar_t* l0 = NULL;
  369. const cmChar_t* l1 = NULL;
  370. if((l0 = _cmXmlAdvanceOne(p)) == NULL)
  371. return _cmXmlSyntaxError(p);
  372. if((l1 = _cmXmlAdvanceToNextWhiteOr(p,'?',' ')) == NULL )
  373. return _cmXmlSyntaxError(p);
  374. if( (*newNodeRef = _cmXmlNodeAlloc(p,kDeclXmlFl | kClosedXmlFl,l0,l1-l0)) == NULL )
  375. return cmErrLastRC(&p->err);
  376. return _cmXmlParseAttrList(p,'?',*newNodeRef);
  377. }
  378. cmXmlRC_t _cmXmlReadEndTag( cmXml_t* p, cmXmlNode_t* np )
  379. {
  380. const cmChar_t* l0 = NULL;
  381. const cmChar_t* l1 = NULL;
  382. assert( *p->c == '/' );
  383. // advance past the '/'
  384. if(( l0 = _cmXmlAdvanceOne(p)) == NULL )
  385. return _cmXmlSyntaxError(p);
  386. // advance to the ending '>'
  387. if(( l1 = _cmXmlAdvanceToNext(p,">")) == NULL )
  388. return _cmXmlSyntaxError(p);
  389. // advance past the
  390. if( _cmXmlAdvanceOne(p) == NULL )
  391. return _cmXmlSyntaxError(p);
  392. // trim trailing space on label
  393. l1 -= 1;
  394. while( l1>l0 && isspace(*l1) )
  395. --l1;
  396. // verify that the label has a length
  397. if( l0 == l1 )
  398. return _cmXmlSyntaxError(p);
  399. assert( !isspace(*l1) );
  400. // if the label on the top of the stack does not match this label
  401. if( strncmp( p->stack->label, l0, (l1-l0)+1 ) )
  402. return kOkXmlRC;
  403. // since we just parsed an end-tag there should be at least one node on the stack
  404. if( p->stack == NULL )
  405. return _cmXmlSyntaxError(p);
  406. p->stack->flags = cmSetFlag(p->stack->flags,kClosedXmlFl);
  407. // pop the stack
  408. //p->stack = p->stack->parent;
  409. return kOkXmlRC;
  410. }
  411. // *newNodeRef will be NULL on error or if the
  412. // the parsed tag was an end tag, or if the last line is comment node.
  413. cmXmlRC_t _cmXmlReadTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  414. {
  415. cmXmlRC_t rc = kOkXmlRC;
  416. assert(newNodeRef != NULL );
  417. *newNodeRef = NULL;
  418. // No leading '<' was found
  419. if( _cmXmlAdvancePast(p,"<") == NULL )
  420. {
  421. // error or EOF
  422. return _cmXmlIsEof(p) ? kOkXmlRC : cmErrLastRC(&p->err);
  423. }
  424. // examine the character following the opening '<'
  425. switch( *p->c )
  426. {
  427. // node end tag
  428. case '/':
  429. return _cmXmlReadEndTag(p,*newNodeRef);
  430. // declaration tag
  431. case '?':
  432. rc = _cmXmlParseDeclTag(p,newNodeRef);
  433. break;
  434. case '!':
  435. if( _cmXmlAdvanceOne(p) == NULL )
  436. return _cmXmlSyntaxError(p);
  437. switch( *p->c )
  438. {
  439. // comment node
  440. case '-':
  441. if( _cmXmlAdvancePast(p,"--") == NULL )
  442. return _cmXmlSyntaxError(p);
  443. if( _cmXmlAdvanceToNext(p,"->") == NULL )
  444. return _cmXmlSyntaxError(p);
  445. if( _cmXmlAdvanceOne(p) == NULL )
  446. return _cmXmlSyntaxError(p);
  447. // p->c is just after "-->"
  448. // Recurse to avoid returning NULL in newNodeRef.
  449. // (*newNodeRef can only be NULL if we just parsed an end-tag).
  450. return _cmXmlReadTag(p,newNodeRef);
  451. // DOCTYPE node
  452. case 'D':
  453. if( _cmXmlAdvancePast(p,"DOCTYPE")==NULL )
  454. return _cmXmlSyntaxError(p);
  455. if((rc = _cmXmlParseDoctype(p,newNodeRef)) != kOkXmlRC )
  456. return _cmXmlSyntaxError(p);
  457. // p->c is just after ">"
  458. break;
  459. default:
  460. return _cmXmlSyntaxError(p);
  461. }
  462. break;
  463. default:
  464. // normal node
  465. if((rc = _cmXmlParseNodeTag(p,newNodeRef)) != kOkXmlRC )
  466. return rc;
  467. // p->c is just after ">"
  468. }
  469. return rc;
  470. }
  471. cmXmlRC_t _cmXmlReadNode( cmXml_t* p, cmXmlNode_t* parent )
  472. {
  473. cmXmlRC_t rc;
  474. while( !_cmXmlIsEof(p) )
  475. {
  476. cmXmlNode_t* np = NULL;
  477. // Read a tag.
  478. if((rc = _cmXmlReadTag(p,&np)) != kOkXmlRC )
  479. return rc;
  480. // If we just read the parents end-tag
  481. if( cmIsFlag(parent->flags,kClosedXmlFl) )
  482. {
  483. assert(np==NULL && parent == p->stack );
  484. p->stack = p->stack->parent;
  485. return rc;
  486. }
  487. if( np==NULL && p->stack==NULL)
  488. break;
  489. // if an end-tag was just read or node was created but closed then pop the stack
  490. if( np==NULL || (np==p->stack && cmIsFlag(np->flags,kClosedXmlFl)) )
  491. p->stack = p->stack->parent;
  492. // if we just read an end-tag or a special node then there is no node-body
  493. if( np == NULL || cmIsFlag(np->flags,kClosedXmlFl) )
  494. continue;
  495. // Advance to the node body.
  496. if( _cmXmlAdvanceToNextNonWhite(p) == NULL )
  497. return _cmXmlSyntaxError(p);
  498. // if the the node body contains nodes
  499. if( *p->c == '<' )
  500. {
  501. if((rc = _cmXmlReadNode(p,np)) != kOkXmlRC )
  502. return rc;
  503. }
  504. else // the node body contains a string
  505. {
  506. const cmChar_t* s0 = p->c;
  507. const cmChar_t* s1 = NULL;
  508. if((s1 = _cmXmlAdvanceToNext(p,"<")) == NULL )
  509. return _cmXmlSyntaxError(p);
  510. np->dataStr = cmLhAllocStrN(p->heapH,s0,s1-s0);
  511. }
  512. }
  513. return rc;
  514. }
  515. cmXmlRC_t cmXmlParse( cmXmlH_t h, const cmChar_t* fn )
  516. {
  517. cmXmlRC_t rc = kOkXmlRC;
  518. cmXml_t* p = _cmXmlHandleToPtr(h);
  519. cmXmlNode_t* np = NULL;
  520. cmLHeapClear( p->heapH, false );
  521. cmMemPtrFree(&p->b);
  522. if( (p->b = cmFileFnToBuf(fn, p->err.rpt, &p->bn )) == NULL )
  523. {
  524. rc = cmErrMsg(&p->err,kMemAllocErrXmlRC,"Unable to buffer the file '%s'.",cmStringNullGuard(fn));
  525. goto errLabel;
  526. }
  527. p->c = p->b;
  528. p->line = 1;
  529. if((np = _cmXmlNodeAlloc(p,kRootXmlFl,"root",strlen("root"))) == NULL )
  530. {
  531. rc = cmErrMsg(&p->err,kMemAllocErrXmlRC,"Root node alloc failed.");
  532. goto errLabel;
  533. }
  534. if((rc = _cmXmlReadNode(p,np)) != kOkXmlRC )
  535. goto errLabel;
  536. errLabel:
  537. return rc;
  538. }
  539. cmXmlRC_t cmXmlClear( cmXmlH_t h )
  540. {
  541. cmXmlRC_t rc = kOkXmlRC;
  542. return rc;
  543. }
  544. const cmXmlNode_t* cmXmlRoot( cmXmlH_t h )
  545. {
  546. cmXml_t* p = _cmXmlHandleToPtr(h);
  547. return p->root;
  548. }
  549. void _cmXmlPrintNode( const cmXmlNode_t* np, cmRpt_t* rpt, unsigned indent )
  550. {
  551. cmChar_t s[ indent + 1 ];
  552. memset(s,' ',indent);
  553. s[indent] = 0;
  554. // print indent and label
  555. cmRptPrintf(rpt,"%s%s: ",s,np->label);
  556. // print this node's attributes
  557. cmXmlAttr_t* ap = np->attr;
  558. for(; ap!=NULL; ap=ap->link)
  559. cmRptPrintf(rpt,"%s='%s' ",ap->label,ap->value);
  560. // print this nodes data string
  561. if( np->dataStr != NULL )
  562. cmRptPrintf(rpt," (%s)",np->dataStr);
  563. cmRptPrintf(rpt,"\n");
  564. // print this nodes children via recursion
  565. cmXmlNode_t* cnp = np->children;
  566. for(; cnp!=NULL; cnp=cnp->sibling )
  567. _cmXmlPrintNode(cnp,rpt,indent+2);
  568. }
  569. void cmXmlPrint( cmXmlH_t h , cmRpt_t* rpt )
  570. {
  571. cmXml_t* p = _cmXmlHandleToPtr(h);
  572. if( p->root != NULL )
  573. _cmXmlPrintNode(p->root,rpt,0);
  574. }
  575. const cmXmlNode_t* cmXmlSearch( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN )
  576. {
  577. // if the 'label' matches this node's label ...
  578. if( cmTextCmp(np->label,label) == 0 )
  579. {
  580. if( attrN == 0 )
  581. return np;
  582. unsigned matchN = 0;
  583. const cmXmlAttr_t* a = np->attr;
  584. unsigned i;
  585. // ... then check for attribute matches also.
  586. for(i=0; i<attrN; ++i)
  587. {
  588. for(; a!=NULL; a=a->link)
  589. {
  590. if( cmTextCmp(a->label,attrV[i].label) == 0 && cmTextCmp(a->value,attrV[i].value) == 0 )
  591. {
  592. ++matchN;
  593. // if a match was found for all attributes then the return np as the solution
  594. if( matchN == attrN )
  595. return np;
  596. break;
  597. }
  598. }
  599. }
  600. }
  601. // this node did not match - try each of this nodes children
  602. const cmXmlNode_t* cnp = np->children;
  603. for(; cnp!=NULL; cnp=cnp->sibling)
  604. if(( np = cmXmlSearch(cnp,label,attrV,attrN)) != NULL )
  605. return np; // a child matched
  606. // no match was found.
  607. return NULL;
  608. }
  609. const cmXmlNode_t* cmXmlSearchV( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, va_list vl )
  610. {
  611. while( label != NULL )
  612. {
  613. if((np = cmXmlSearch(np,label,attrV,attrN)) == NULL )
  614. return NULL;
  615. if((label = va_arg(vl,cmChar_t*)) != NULL)
  616. {
  617. attrV = va_arg(vl,const cmXmlAttr_t*);
  618. attrN = va_arg(vl,unsigned);
  619. }
  620. }
  621. return np;
  622. }
  623. const cmXmlNode_t* cmXmlSearchN( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, ... )
  624. {
  625. va_list vl;
  626. va_start(vl,attrN);
  627. np = cmXmlSearchV(np,label,attrV,attrN,vl);
  628. va_end(vl);
  629. return np;
  630. }
  631. const cmXmlAttr_t* cmXmlFindAttrib( const cmXmlNode_t* np, const cmChar_t* label )
  632. {
  633. const cmXmlAttr_t* a = np->attr;
  634. for(; a!=NULL; a=a->link)
  635. if( cmTextCmp(a->label,label) == 0 )
  636. return a;
  637. return NULL;
  638. }
  639. cmXmlRC_t cmXmlAttrInt( const cmXmlNode_t* np, const cmChar_t* attrLabel, int* retRef )
  640. {
  641. const cmXmlAttr_t* a;
  642. if((a = cmXmlFindAttrib(np,attrLabel)) == NULL )
  643. return kNodeNotFoundXmlRC;
  644. assert(retRef != NULL);
  645. *retRef = 0;
  646. if( a->value != NULL )
  647. {
  648. errno = 0;
  649. // convert the string to an integer
  650. *retRef = strtol(a->value,NULL,10);
  651. if( errno != 0 )
  652. return kInvalidTypeXmlRC;
  653. }
  654. return kOkXmlRC;
  655. }
  656. cmXmlRC_t cmXmlAttrUInt( const cmXmlNode_t* np, const cmChar_t* attrLabel, unsigned* retRef )
  657. { return cmXmlAttrInt(np,attrLabel,(int*)retRef); }
  658. cmXmlRC_t cmXmlGetInt( const cmXmlNode_t* np, int* retRef, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, ... )
  659. {
  660. cmXmlRC_t rc = kNodeNotFoundXmlRC;
  661. va_list vl;
  662. va_start(vl,attrN);
  663. // find the requsted node
  664. if((np = cmXmlSearchV(np,label,attrV,attrN,vl)) != NULL )
  665. {
  666. // if the returned node does not have a data string
  667. if( np->dataStr == NULL )
  668. return kInvalidTypeXmlRC;
  669. errno = 0;
  670. // convert the string to an integer
  671. strtol(np->dataStr,NULL,10);
  672. if( errno != 0 )
  673. return kInvalidTypeXmlRC;
  674. rc = kOkXmlRC;
  675. }
  676. va_end(vl);
  677. return rc;
  678. }
  679. const cmXmlNode_t* _cmXmlNodeFindChild( const cmXmlNode_t* np, const cmChar_t* label )
  680. {
  681. const cmXmlNode_t* cnp = np->children;
  682. for(; cnp!=NULL; cnp=cnp->sibling)
  683. if( cmTextCmp(cnp->label,label) == 0 )
  684. return cnp;
  685. return NULL;
  686. }
  687. const cmChar_t* cmXmlNodeValueV( const cmXmlNode_t* np, va_list vl )
  688. {
  689. const cmChar_t* label;
  690. // for each node label
  691. while( (label = va_arg(vl,const cmChar_t*)) != NULL )
  692. if((np = _cmXmlNodeFindChild(np,label)) == NULL )
  693. break;
  694. return np==NULL ? NULL : np->dataStr;
  695. }
  696. const cmChar_t* cmXmlNodeValue( const cmXmlNode_t* np, ... )
  697. {
  698. va_list vl;
  699. va_start(vl,np);
  700. const cmChar_t* str = cmXmlNodeValueV(np,vl);
  701. va_end(vl);
  702. return str;
  703. }
  704. cmXmlRC_t cmXmlNodeIntV(const cmXmlNode_t* np, int* retRef, va_list vl )
  705. {
  706. const cmChar_t* valueStr;
  707. if((valueStr = cmXmlNodeValueV(np,vl)) == NULL )
  708. return kNodeNotFoundXmlRC;
  709. errno = 0;
  710. // convert the string to an integer
  711. *retRef = strtol(valueStr,NULL,10);
  712. if( errno != 0 )
  713. return kInvalidTypeXmlRC;
  714. return kOkXmlRC;
  715. }
  716. cmXmlRC_t cmXmlNodeUIntV(const cmXmlNode_t* np, unsigned* retRef, va_list vl )
  717. { return cmXmlNodeIntV(np,(int*)retRef,vl); }
  718. cmXmlRC_t cmXmlNodeDoubleV(const cmXmlNode_t* np, double* retRef, va_list vl )
  719. {
  720. const cmChar_t* valueStr;
  721. if((valueStr = cmXmlNodeValueV(np,vl)) == NULL )
  722. return kNodeNotFoundXmlRC;
  723. errno = 0;
  724. // convert the string to a double
  725. *retRef = strtod(valueStr,NULL);
  726. if( errno != 0 )
  727. return kInvalidTypeXmlRC;
  728. return kOkXmlRC;
  729. }
  730. cmXmlRC_t cmXmlNodeInt( const cmXmlNode_t* np, int* retRef, ... )
  731. {
  732. cmXmlRC_t rc;
  733. va_list vl;
  734. va_start(vl,retRef);
  735. rc = cmXmlNodeIntV(np,retRef,vl);
  736. va_end(vl);
  737. return rc;
  738. }
  739. cmXmlRC_t cmXmlNodeUInt( const cmXmlNode_t* np, unsigned* retRef, ... )
  740. {
  741. cmXmlRC_t rc;
  742. va_list vl;
  743. va_start(vl,retRef);
  744. rc = cmXmlNodeUIntV(np,retRef,vl);
  745. va_end(vl);
  746. return rc;
  747. }
  748. cmXmlRC_t cmXmlNodeDouble(const cmXmlNode_t* np, double* retRef, ...)
  749. {
  750. cmXmlRC_t rc;
  751. va_list vl;
  752. va_start(vl,retRef);
  753. rc = cmXmlNodeDoubleV(np,retRef,vl);
  754. va_end(vl);
  755. return rc;
  756. }
  757. unsigned _cmXmlLabelCount( const cmChar_t* firstLabel, va_list vl )
  758. {
  759. unsigned n = 0;
  760. if( firstLabel != NULL )
  761. {
  762. n = 1;
  763. va_list vl0;
  764. va_copy(vl0,vl);
  765. while( va_arg(vl0,const cmChar_t*) != NULL )
  766. n += 1;
  767. va_end(vl0);
  768. }
  769. return n;
  770. }
  771. const cmXmlNode_t* _cmXmlNodeHasChildR( const cmXmlNode_t* np, unsigned argIdx, const cmChar_t**argV, unsigned argN, const cmChar_t* attrLabel, const cmChar_t* valueStr)
  772. {
  773. const cmXmlNode_t* cnp = np->children;
  774. for(; cnp!=NULL; cnp=cnp->sibling)
  775. if( cmTextCmp(cnp->label,argV[argIdx]) == 0 )
  776. {
  777. // The node path ending at cnp matches all node labels up through argV[argIdx]
  778. // if there are still node labels to match in argV[]
  779. if( argIdx < argN-1 )
  780. return _cmXmlNodeHasChildR(cnp,argIdx+1,argV,argN,attrLabel,valueStr);
  781. // The path ending at cnp matches all node labels in argV[].
  782. // if an attr. label was given
  783. if( attrLabel != NULL )
  784. {
  785. const cmXmlAttr_t* a;
  786. if((a = cmXmlFindAttrib(cnp,attrLabel)) == NULL )
  787. continue;
  788. // if a value string was given
  789. if( valueStr != NULL )
  790. if( cmTextCmp(a->value,valueStr) != 0 )
  791. continue;
  792. }
  793. return cnp;
  794. }
  795. return NULL;
  796. }
  797. /*
  798. const cmXmlNode_t* _cmXmlNodeHasChildR( const cmXmlNode_t* np, unsigned argIdx, const cmChar_t**argV, unsigned argN, const cmChar_t* attrLabel, const cmChar_t* valueStr)
  799. {
  800. if( argIdx == argN )
  801. return NULL;
  802. const cmXmlNode_t* cnp = np->children;
  803. for(; cnp!=NULL; cnp=cnp->sibling)
  804. if( cmTextCmp(cnp->label,argV[argIdx]) == 0 )
  805. {
  806. const cmXmlNode_t* tnp;
  807. if((tnp = _cmXmlNodeHasChildR(cnp,argIdx+1,argV,argN,attrLabel,valueStr)) != NULL)
  808. {
  809. if( attrLabel != NULL )
  810. {
  811. const cmXmlAttr_t* a;
  812. if((a = cmXmlFindAttrib(tnp,attrLabel)) == NULL )
  813. continue;
  814. if( valueStr != NULL )
  815. {
  816. if( cmTextCmp(a->value,valueStr) != 0 )
  817. continue;
  818. }
  819. }
  820. }
  821. if( tnp != NULL )
  822. return tnp;
  823. }
  824. return NULL;
  825. }
  826. */
  827. const cmXmlNode_t* _cmXmlNodeHasChildV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl, unsigned argN, const cmChar_t* attrLabel, const cmChar_t* valueStr )
  828. {
  829. unsigned i;
  830. const cmChar_t* argV[ argN+1 ];
  831. argV[0] = label;
  832. for(i=1; i<argN+1; ++i)
  833. argV[i] = va_arg(vl,const cmChar_t*);
  834. return _cmXmlNodeHasChildR(np,0,argV,argN,attrLabel,valueStr);
  835. }
  836. bool cmXmlNodeHasChildV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  837. {
  838. return _cmXmlNodeHasChildV(np,label,vl,_cmXmlLabelCount(label,vl),NULL,NULL)!=NULL;
  839. }
  840. bool cmXmlNodeHasChild( const cmXmlNode_t* np, const cmChar_t* label, ... )
  841. {
  842. va_list vl;
  843. va_start(vl,label);
  844. bool fl = cmXmlNodeHasChildV(np,label,vl);
  845. va_end(vl);
  846. return fl;
  847. }
  848. bool _cmXmlNodeHasChildWithAttrAndValueV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl0, bool valueFl )
  849. {
  850. unsigned argN = _cmXmlLabelCount(label,vl0);
  851. unsigned n = valueFl ? 2 : 1;
  852. const cmChar_t* attrLabel = NULL;
  853. const cmChar_t* valueStr = NULL;
  854. const cmXmlNode_t* cnp = NULL;
  855. va_list vl1;
  856. unsigned i;
  857. va_copy(vl1,vl0);
  858. assert( argN > n-1 ); // an attribute label must be given.
  859. if( argN <= n-1 )
  860. goto errLabel;
  861. argN -= n;
  862. // advance vl0 to the attribute label
  863. for(i=1; i<argN; ++i)
  864. {
  865. attrLabel = va_arg(vl0,const cmChar_t*);
  866. assert( attrLabel != NULL );
  867. }
  868. // get the attr label
  869. attrLabel = va_arg(vl0,const cmChar_t*);
  870. if( valueFl )
  871. valueStr = va_arg(vl0,const cmChar_t*);
  872. cnp = _cmXmlNodeHasChildV(np,label,vl1,argN,attrLabel,valueStr);
  873. errLabel:
  874. va_end(vl1);
  875. return cnp != NULL;
  876. }
  877. bool cmXmlNodeHasChildWithAttrAndValueV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  878. { return _cmXmlNodeHasChildWithAttrAndValueV(np,label,vl,true); }
  879. bool cmXmlNodeHasChildWithAttrAndValue( const cmXmlNode_t* np, const cmChar_t* label, ... )
  880. {
  881. va_list vl;
  882. va_start(vl,label);
  883. bool fl = cmXmlNodeHasChildWithAttrAndValueV(np,label,vl);
  884. va_end(vl);
  885. return fl;
  886. }
  887. bool cmXmlNodeHasChildWithAttrV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  888. { return _cmXmlNodeHasChildWithAttrAndValueV(np,label,vl,false); }
  889. bool cmXmlNodeHasChildWithAttr( const cmXmlNode_t* np, const cmChar_t* label, ... )
  890. {
  891. va_list vl;
  892. va_start(vl,label);
  893. bool fl = cmXmlNodeHasChildWithAttrV(np,label,vl);
  894. va_end(vl);
  895. return fl;
  896. }
  897. cmXmlRC_t cmXmlTest( cmCtx_t* ctx, const cmChar_t* fn )
  898. {
  899. cmXmlRC_t rc = kOkXmlRC;
  900. cmXmlH_t h = cmXmlNullHandle;
  901. if((rc = cmXmlAlloc(ctx, &h, fn )) != kOkXmlRC )
  902. return cmErrMsg(&ctx->err,rc,"XML alloc failed.");
  903. if((rc = cmXmlParse(h,fn)) != kOkXmlRC )
  904. goto errLabel;
  905. cmXmlAttr_t aV[] =
  906. {
  907. { "id","P1"}
  908. };
  909. if( cmXmlSearch(cmXmlRoot(h),"part",aV,1) == NULL )
  910. {
  911. cmErrMsg(&ctx->err,kTestFailXmlRC,"Search failed.");
  912. goto errLabel;
  913. }
  914. //cmXmlPrint(h,&ctx->rpt);
  915. errLabel:
  916. cmXmlFree(&h);
  917. return rc;
  918. }