libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmXml.c 23KB

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