libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmXml.c 24KB

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