libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmXml.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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( _cmXmlAdvance(p) )
  219. return p->c;
  220. return NULL;
  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. {
  241. if( endChar=='?')
  242. {
  243. if((v1 = _cmXmlAdvanceToNext(p,"?")) != NULL )
  244. {
  245. v0 = l1+1;
  246. goto allocLabel;
  247. }
  248. }
  249. return _cmXmlSyntaxError(p);
  250. }
  251. // advance to the next non-white character
  252. if((v0 = _cmXmlAdvanceToNextNonWhite(p)) == NULL )
  253. return _cmXmlSyntaxError(p);
  254. // the first character in the value must be a single quote
  255. if( *p->c == '\'' )
  256. {
  257. if((v0 = _cmXmlAdvanceOne(p)) == NULL )
  258. return _cmXmlSyntaxError(p);
  259. // advance to the next single quote
  260. v1 = _cmXmlAdvanceToNext(p,"'");
  261. }
  262. else
  263. {
  264. v1 = _cmXmlAdvanceToNextWhiteOr(p,endChar,' ');
  265. }
  266. if( v1 == NULL )
  267. return _cmXmlSyntaxError(p);
  268. // advance past the ending single quote
  269. if( *p->c != endChar )
  270. if( _cmXmlAdvanceOne(p) == NULL )
  271. return _cmXmlSyntaxError(p);
  272. allocLabel:
  273. _cmXmlAttrAlloc(p, np, l0, l1-l0, v0, v1-v0 );
  274. // p->c now points just past the ending single quote
  275. return rc;
  276. }
  277. cmXmlRC_t _cmXmlParseAttrList( cmXml_t* p, cmChar_t endChar, cmXmlNode_t* np )
  278. {
  279. cmXmlRC_t rc = kOkXmlRC;
  280. while( *p->c != endChar && *p->c != '>' )
  281. if((rc = _cmXmlParseAttr(p,endChar,np)) != kOkXmlRC )
  282. break;
  283. if( *p->c == endChar )
  284. {
  285. // if this node is terminated at the end of its beginning tag
  286. if( endChar == '/' )
  287. {
  288. np->flags = cmSetFlag(np->flags,kClosedXmlFl);
  289. //p->stack = p->stack->parent;
  290. }
  291. if( _cmXmlAdvanceOne(p) == NULL )
  292. return _cmXmlSyntaxError(p);
  293. }
  294. if( *p->c != '>' )
  295. return _cmXmlSyntaxError(p);
  296. if( _cmXmlAdvancePast(p,">") == NULL )
  297. return _cmXmlSyntaxError(p);
  298. // p->c is now past the ending '>'
  299. return rc;
  300. }
  301. cmXmlRC_t _cmXmlParseDoctypeToken( cmXml_t* p, cmXmlNode_t* np )
  302. {
  303. const cmChar_t* t0 = NULL;
  304. const cmChar_t* t1 = NULL;
  305. // advance to the first char in the doctype token
  306. if((t0 = _cmXmlAdvanceToNextNonWhite(p) ) == NULL )
  307. {
  308. return _cmXmlSyntaxError(p);
  309. }
  310. // if the end of the tag was encountered
  311. if( *p->c == '>' )
  312. return kOkXmlRC;
  313. // if the token begins with a quote
  314. if( *p->c == '\'' )
  315. {
  316. if((t1 = _cmXmlAdvanceToNext(p,"'")) == NULL )
  317. return _cmXmlSyntaxError(p);
  318. if( _cmXmlAdvanceOne(p) == NULL )
  319. return _cmXmlSyntaxError(p);
  320. }
  321. else
  322. {
  323. if((t1 = _cmXmlAdvanceToNextWhiteOr(p,'>',' ')) == NULL )
  324. return _cmXmlSyntaxError(p);
  325. }
  326. // t1 and p->c now point just past the last character in the token
  327. return kOkXmlRC;
  328. }
  329. cmXmlRC_t _cmXmlParseDoctype( cmXml_t* p, cmXmlNode_t** newNodeRef )
  330. {
  331. cmXmlRC_t rc = kOkXmlRC;
  332. if((*newNodeRef = _cmXmlNodeAlloc(p,kDoctypeXmlFl | kClosedXmlFl,"DOCTYPE",strlen("DOCTYPE"))) == NULL )
  333. return cmErrLastRC(&p->err);
  334. while( *p->c != '>' )
  335. if((rc = _cmXmlParseDoctypeToken(p,*newNodeRef)) != kOkXmlRC )
  336. break;
  337. if( *p->c == '>' )
  338. _cmXmlAdvanceOne(p);
  339. return rc;
  340. }
  341. // Node tags are tags that begin with a '<' and are not
  342. // followed by any special character.
  343. cmXmlRC_t _cmXmlParseNodeTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  344. {
  345. cmXmlRC_t rc = kOkXmlRC;
  346. const cmChar_t* l0 = NULL;
  347. const cmChar_t* l1 = NULL;
  348. // Advance to the first character of the tag label.
  349. if((l0 = _cmXmlAdvanceToNextNonWhite(p)) == NULL )
  350. return _cmXmlSyntaxError(p);
  351. // Advance to the last character following the tag label.
  352. if((l1 = _cmXmlAdvanceToNextWhiteOr(p,'/','>')) == NULL )
  353. return _cmXmlSyntaxError(p);
  354. // Create the node.
  355. if( (*newNodeRef = _cmXmlNodeAlloc(p,kNormalXmlFl,l0,l1-l0)) == NULL )
  356. return cmErrLastRC(&p->err);
  357. // look for attributes
  358. if((rc = _cmXmlParseAttrList(p,'/',*newNodeRef)) != kOkXmlRC )
  359. return _cmXmlSyntaxError(p);
  360. // p->c is now past the ending '>'
  361. return rc;
  362. }
  363. cmXmlRC_t _cmXmlParseDeclTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  364. {
  365. assert( *p->c == '?' );
  366. const cmChar_t* l0 = NULL;
  367. const cmChar_t* l1 = NULL;
  368. if((l0 = _cmXmlAdvanceOne(p)) == NULL)
  369. return _cmXmlSyntaxError(p);
  370. if((l1 = _cmXmlAdvanceToNextWhiteOr(p,'?',' ')) == NULL )
  371. return _cmXmlSyntaxError(p);
  372. if( (*newNodeRef = _cmXmlNodeAlloc(p,kDeclXmlFl | kClosedXmlFl,l0,l1-l0)) == NULL )
  373. return cmErrLastRC(&p->err);
  374. return _cmXmlParseAttrList(p,'?',*newNodeRef);
  375. }
  376. cmXmlRC_t _cmXmlReadEndTag( cmXml_t* p, cmXmlNode_t* np )
  377. {
  378. const cmChar_t* l0 = NULL;
  379. const cmChar_t* l1 = NULL;
  380. assert( *p->c == '/' );
  381. // advance past the '/'
  382. if(( l0 = _cmXmlAdvanceOne(p)) == NULL )
  383. return _cmXmlSyntaxError(p);
  384. // advance to the ending '>'
  385. if(( l1 = _cmXmlAdvanceToNext(p,">")) == NULL )
  386. return _cmXmlSyntaxError(p);
  387. // advance past the
  388. if( _cmXmlAdvanceOne(p) == NULL )
  389. return _cmXmlSyntaxError(p);
  390. // trim trailing space on label
  391. l1 -= 1;
  392. while( l1>l0 && isspace(*l1) )
  393. --l1;
  394. // verify that the label has a length
  395. if( l0 == l1 )
  396. return _cmXmlSyntaxError(p);
  397. assert( !isspace(*l1) );
  398. // if the label on the top of the stack does not match this label
  399. if( strncmp( p->stack->label, l0, (l1-l0)+1 ) )
  400. return kOkXmlRC;
  401. // since we just parsed an end-tag there should be at least one node on the stack
  402. if( p->stack == NULL )
  403. return _cmXmlSyntaxError(p);
  404. p->stack->flags = cmSetFlag(p->stack->flags,kClosedXmlFl);
  405. // pop the stack
  406. //p->stack = p->stack->parent;
  407. return kOkXmlRC;
  408. }
  409. // *newNodeRef will be NULL on error or if the
  410. // the parsed tag was an end tag, or if the last line is comment node.
  411. cmXmlRC_t _cmXmlReadTag( cmXml_t* p, cmXmlNode_t** newNodeRef )
  412. {
  413. cmXmlRC_t rc = kOkXmlRC;
  414. assert(newNodeRef != NULL );
  415. *newNodeRef = NULL;
  416. // No leading '<' was found
  417. if( _cmXmlAdvancePast(p,"<") == NULL )
  418. {
  419. // error or EOF
  420. return _cmXmlIsEof(p) ? kOkXmlRC : cmErrLastRC(&p->err);
  421. }
  422. // examine the character following the opening '<'
  423. switch( *p->c )
  424. {
  425. // node end tag
  426. case '/':
  427. return _cmXmlReadEndTag(p,*newNodeRef);
  428. // declaration tag
  429. case '?':
  430. rc = _cmXmlParseDeclTag(p,newNodeRef);
  431. break;
  432. case '!':
  433. if( _cmXmlAdvanceOne(p) == NULL )
  434. return _cmXmlSyntaxError(p);
  435. switch( *p->c )
  436. {
  437. // comment node
  438. case '-':
  439. if( _cmXmlAdvancePast(p,"--") == NULL )
  440. return _cmXmlSyntaxError(p);
  441. if( _cmXmlAdvanceToNext(p,"->") == NULL )
  442. return _cmXmlSyntaxError(p);
  443. if( _cmXmlAdvanceOne(p) == NULL )
  444. return _cmXmlSyntaxError(p);
  445. // p->c is just after "-->"
  446. // Recurse to avoid returning NULL in newNodeRef.
  447. // (*newNodeRef can only be NULL if we just parsed an end-tag).
  448. return _cmXmlReadTag(p,newNodeRef);
  449. // DOCTYPE node
  450. case 'D':
  451. if( _cmXmlAdvancePast(p,"DOCTYPE")==NULL )
  452. return _cmXmlSyntaxError(p);
  453. if((rc = _cmXmlParseDoctype(p,newNodeRef)) != kOkXmlRC )
  454. return _cmXmlSyntaxError(p);
  455. // p->c is just after ">"
  456. break;
  457. default:
  458. return _cmXmlSyntaxError(p);
  459. }
  460. break;
  461. default:
  462. // normal node
  463. if((rc = _cmXmlParseNodeTag(p,newNodeRef)) != kOkXmlRC )
  464. return rc;
  465. // p->c is just after ">"
  466. }
  467. return rc;
  468. }
  469. cmXmlRC_t _cmXmlReadNode( cmXml_t* p, cmXmlNode_t* parent )
  470. {
  471. cmXmlRC_t rc;
  472. while( !_cmXmlIsEof(p) )
  473. {
  474. cmXmlNode_t* np = NULL;
  475. // Read a tag.
  476. if((rc = _cmXmlReadTag(p,&np)) != kOkXmlRC )
  477. return rc;
  478. // If we just read the parents end-tag
  479. if( cmIsFlag(parent->flags,kClosedXmlFl) )
  480. {
  481. assert(np==NULL && parent == p->stack );
  482. p->stack = p->stack->parent;
  483. return rc;
  484. }
  485. // if an end-tag was just read or node was created but closed then pop the stack
  486. if( np==NULL || (np==p->stack && cmIsFlag(np->flags,kClosedXmlFl)) )
  487. p->stack = p->stack->parent;
  488. // if we just read an end-tag or a special node then there is no node-body
  489. if( np == NULL || cmIsFlag(np->flags,kClosedXmlFl) )
  490. continue;
  491. // Advance to the node body.
  492. if( _cmXmlAdvanceToNextNonWhite(p) == NULL )
  493. return _cmXmlSyntaxError(p);
  494. // if the the node body contains nodes
  495. if( *p->c == '<' )
  496. {
  497. if((rc = _cmXmlReadNode(p,np)) != kOkXmlRC )
  498. return rc;
  499. }
  500. else // the node body contains a string
  501. {
  502. const cmChar_t* s0 = p->c;
  503. const cmChar_t* s1 = NULL;
  504. if((s1 = _cmXmlAdvanceToNext(p,"<")) == NULL )
  505. return _cmXmlSyntaxError(p);
  506. np->dataStr = cmLhAllocStrN(p->heapH,s0,s1-s0);
  507. }
  508. }
  509. return rc;
  510. }
  511. cmXmlRC_t cmXmlParse( cmXmlH_t h, const cmChar_t* fn )
  512. {
  513. cmXmlRC_t rc = kOkXmlRC;
  514. cmXml_t* p = _cmXmlHandleToPtr(h);
  515. cmXmlNode_t* np = NULL;
  516. cmLHeapClear( p->heapH, false );
  517. cmMemPtrFree(&p->b);
  518. if( (p->b = cmFileFnToBuf(fn, p->err.rpt, &p->bn )) == NULL )
  519. {
  520. rc = cmErrMsg(&p->err,kMemAllocErrXmlRC,"Unable to buffer the file '%s'.",cmStringNullGuard(fn));
  521. goto errLabel;
  522. }
  523. p->c = p->b;
  524. p->line = 1;
  525. if((np = _cmXmlNodeAlloc(p,kRootXmlFl,"root",strlen("root"))) == NULL )
  526. {
  527. rc = cmErrMsg(&p->err,kMemAllocErrXmlRC,"Root node alloc failed.");
  528. goto errLabel;
  529. }
  530. if((rc = _cmXmlReadNode(p,np)) != kOkXmlRC )
  531. goto errLabel;
  532. errLabel:
  533. return rc;
  534. }
  535. cmXmlRC_t cmXmlClear( cmXmlH_t h )
  536. {
  537. cmXmlRC_t rc = kOkXmlRC;
  538. return rc;
  539. }
  540. const cmXmlNode_t* cmXmlRoot( cmXmlH_t h )
  541. {
  542. cmXml_t* p = _cmXmlHandleToPtr(h);
  543. return p->root;
  544. }
  545. void _cmXmlPrintNode( const cmXmlNode_t* np, cmRpt_t* rpt, unsigned indent )
  546. {
  547. cmChar_t s[ indent + 1 ];
  548. memset(s,' ',indent);
  549. s[indent] = 0;
  550. // print indent and label
  551. cmRptPrintf(rpt,"%s%s: ",s,np->label);
  552. // print this node's attributes
  553. cmXmlAttr_t* ap = np->attr;
  554. for(; ap!=NULL; ap=ap->link)
  555. cmRptPrintf(rpt,"%s='%s' ",ap->label,ap->value);
  556. // print this nodes data string
  557. if( np->dataStr != NULL )
  558. cmRptPrintf(rpt," (%s)",np->dataStr);
  559. cmRptPrintf(rpt,"\n");
  560. // print this nodes children via recursion
  561. cmXmlNode_t* cnp = np->children;
  562. for(; cnp!=NULL; cnp=cnp->sibling )
  563. _cmXmlPrintNode(cnp,rpt,indent+2);
  564. }
  565. void cmXmlPrint( cmXmlH_t h , cmRpt_t* rpt )
  566. {
  567. cmXml_t* p = _cmXmlHandleToPtr(h);
  568. if( p->root != NULL )
  569. _cmXmlPrintNode(p->root,rpt,0);
  570. }
  571. const cmXmlNode_t* cmXmlSearch( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN )
  572. {
  573. // if the 'label' matches this node's label ...
  574. if( cmTextCmp(np->label,label) == 0 )
  575. {
  576. if( attrN == 0 )
  577. return np;
  578. unsigned matchN = 0;
  579. const cmXmlAttr_t* a = np->attr;
  580. unsigned i;
  581. // ... then check for attribute matches also.
  582. for(i=0; i<attrN; ++i)
  583. {
  584. for(; a!=NULL; a=a->link)
  585. {
  586. if( cmTextCmp(a->label,attrV[i].label) == 0 && cmTextCmp(a->value,attrV[i].value) == 0 )
  587. {
  588. ++matchN;
  589. // if a match was found for all attributes then the return np as the solution
  590. if( matchN == attrN )
  591. return np;
  592. break;
  593. }
  594. }
  595. }
  596. }
  597. // this node did not match - try each of this nodes children
  598. const cmXmlNode_t* cnp = np->children;
  599. for(; cnp!=NULL; cnp=cnp->sibling)
  600. if(( np = cmXmlSearch(cnp,label,attrV,attrN)) != NULL )
  601. return np; // a child matched
  602. // no match was found.
  603. return NULL;
  604. }
  605. const cmXmlNode_t* cmXmlSearchV( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, va_list vl )
  606. {
  607. while( label != NULL )
  608. {
  609. if((np = cmXmlSearch(np,label,attrV,attrN)) == NULL )
  610. return NULL;
  611. if((label = va_arg(vl,cmChar_t*)) != NULL)
  612. {
  613. attrV = va_arg(vl,const cmXmlAttr_t*);
  614. attrN = va_arg(vl,unsigned);
  615. }
  616. }
  617. return np;
  618. }
  619. const cmXmlNode_t* cmXmlSearchN( const cmXmlNode_t* np, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, ... )
  620. {
  621. va_list vl;
  622. va_start(vl,attrN);
  623. np = cmXmlSearchV(np,label,attrV,attrN,vl);
  624. va_end(vl);
  625. return np;
  626. }
  627. const cmXmlAttr_t* cmXmlFindAttrib( const cmXmlNode_t* np, const cmChar_t* label )
  628. {
  629. const cmXmlAttr_t* a = np->attr;
  630. for(; a!=NULL; a=a->link)
  631. if( cmTextCmp(a->label,label) == 0 )
  632. return a;
  633. return NULL;
  634. }
  635. cmXmlRC_t cmXmlAttrInt( const cmXmlNode_t* np, const cmChar_t* attrLabel, int* retRef )
  636. {
  637. const cmXmlAttr_t* a;
  638. if((a = cmXmlFindAttrib(np,attrLabel)) == NULL )
  639. return kNodeNotFoundXmlRC;
  640. assert(retRef != NULL);
  641. *retRef = 0;
  642. if( a->value != NULL )
  643. {
  644. errno = 0;
  645. // convert the string to an integer
  646. *retRef = strtol(a->value,NULL,10);
  647. if( errno != 0 )
  648. return kInvalidTypeXmlRC;
  649. }
  650. return kOkXmlRC;
  651. }
  652. cmXmlRC_t cmXmlAttrUInt( const cmXmlNode_t* np, const cmChar_t* attrLabel, unsigned* retRef )
  653. { return cmXmlAttrInt(np,attrLabel,(int*)retRef); }
  654. cmXmlRC_t cmXmlGetInt( const cmXmlNode_t* np, int* retRef, const cmChar_t* label, const cmXmlAttr_t* attrV, unsigned attrN, ... )
  655. {
  656. cmXmlRC_t rc = kNodeNotFoundXmlRC;
  657. va_list vl;
  658. va_start(vl,attrN);
  659. // find the requsted node
  660. if((np = cmXmlSearchV(np,label,attrV,attrN,vl)) != NULL )
  661. {
  662. // if the returned node does not have a data string
  663. if( np->dataStr == NULL )
  664. return kInvalidTypeXmlRC;
  665. errno = 0;
  666. // convert the string to an integer
  667. strtol(np->dataStr,NULL,10);
  668. if( errno != 0 )
  669. return kInvalidTypeXmlRC;
  670. rc = kOkXmlRC;
  671. }
  672. va_end(vl);
  673. return rc;
  674. }
  675. const cmXmlNode_t* _cmXmlNodeFindChild( const cmXmlNode_t* np, const cmChar_t* label )
  676. {
  677. const cmXmlNode_t* cnp = np->children;
  678. for(; cnp!=NULL; cnp=cnp->sibling)
  679. if( cmTextCmp(cnp->label,label) == 0 )
  680. return cnp;
  681. return NULL;
  682. }
  683. const cmChar_t* cmXmlNodeValueV( const cmXmlNode_t* np, va_list vl )
  684. {
  685. const cmChar_t* label;
  686. // for each node label
  687. while( (label = va_arg(vl,const cmChar_t*)) != NULL )
  688. if((np = _cmXmlNodeFindChild(np,label)) == NULL )
  689. break;
  690. return np==NULL ? NULL : np->dataStr;
  691. }
  692. const cmChar_t* cmXmlNodeValue( const cmXmlNode_t* np, ... )
  693. {
  694. va_list vl;
  695. va_start(vl,np);
  696. const cmChar_t* str = cmXmlNodeValueV(np,vl);
  697. va_end(vl);
  698. return str;
  699. }
  700. cmXmlRC_t cmXmlNodeIntV(const cmXmlNode_t* np, int* retRef, va_list vl )
  701. {
  702. const cmChar_t* valueStr;
  703. if((valueStr = cmXmlNodeValueV(np,vl)) == NULL )
  704. return kNodeNotFoundXmlRC;
  705. errno = 0;
  706. // convert the string to an integer
  707. *retRef = strtol(valueStr,NULL,10);
  708. if( errno != 0 )
  709. return kInvalidTypeXmlRC;
  710. return kOkXmlRC;
  711. }
  712. cmXmlRC_t cmXmlNodeUIntV(const cmXmlNode_t* np, unsigned* retRef, va_list vl )
  713. { return cmXmlNodeIntV(np,(int*)retRef,vl); }
  714. cmXmlRC_t cmXmlNodeDoubleV(const cmXmlNode_t* np, double* retRef, va_list vl )
  715. {
  716. const cmChar_t* valueStr;
  717. if((valueStr = cmXmlNodeValueV(np,vl)) == NULL )
  718. return kNodeNotFoundXmlRC;
  719. errno = 0;
  720. // convert the string to a double
  721. *retRef = strtod(valueStr,NULL);
  722. if( errno != 0 )
  723. return kInvalidTypeXmlRC;
  724. return kOkXmlRC;
  725. }
  726. cmXmlRC_t cmXmlNodeInt( const cmXmlNode_t* np, int* retRef, ... )
  727. {
  728. cmXmlRC_t rc;
  729. va_list vl;
  730. va_start(vl,retRef);
  731. rc = cmXmlNodeIntV(np,retRef,vl);
  732. va_end(vl);
  733. return rc;
  734. }
  735. cmXmlRC_t cmXmlNodeUInt( const cmXmlNode_t* np, unsigned* retRef, ... )
  736. {
  737. cmXmlRC_t rc;
  738. va_list vl;
  739. va_start(vl,retRef);
  740. rc = cmXmlNodeUIntV(np,retRef,vl);
  741. va_end(vl);
  742. return rc;
  743. }
  744. cmXmlRC_t cmXmlNodeDouble(const cmXmlNode_t* np, double* retRef, ...)
  745. {
  746. cmXmlRC_t rc;
  747. va_list vl;
  748. va_start(vl,retRef);
  749. rc = cmXmlNodeDoubleV(np,retRef,vl);
  750. va_end(vl);
  751. return rc;
  752. }
  753. unsigned _cmXmlLabelCount( const cmChar_t* firstLabel, va_list vl )
  754. {
  755. unsigned n = 0;
  756. if( firstLabel != NULL )
  757. {
  758. n = 1;
  759. va_list vl0;
  760. va_copy(vl0,vl);
  761. while( va_arg(vl0,const cmChar_t*) != NULL )
  762. n += 1;
  763. va_end(vl0);
  764. }
  765. return n;
  766. }
  767. const cmXmlNode_t* _cmXmlNodeHasChildR( const cmXmlNode_t* np, unsigned argIdx, const cmChar_t**argV, unsigned argN, const cmChar_t* attrLabel, const cmChar_t* valueStr)
  768. {
  769. const cmXmlNode_t* cnp = np->children;
  770. for(; cnp!=NULL; cnp=cnp->sibling)
  771. if( cmTextCmp(cnp->label,argV[argIdx]) == 0 )
  772. {
  773. // The node path ending at cnp matches all node labels up through argV[argIdx]
  774. // if there are still node labels to match in argV[]
  775. if( argIdx < argN-1 )
  776. return _cmXmlNodeHasChildR(cnp,argIdx+1,argV,argN,attrLabel,valueStr);
  777. // The path ending at cnp matches all node labels in argV[].
  778. // if an attr. label was given
  779. if( attrLabel != NULL )
  780. {
  781. const cmXmlAttr_t* a;
  782. if((a = cmXmlFindAttrib(cnp,attrLabel)) == NULL )
  783. continue;
  784. // if a value string was given
  785. if( valueStr != NULL )
  786. if( cmTextCmp(a->value,valueStr) != 0 )
  787. continue;
  788. }
  789. return cnp;
  790. }
  791. return NULL;
  792. }
  793. /*
  794. const cmXmlNode_t* _cmXmlNodeHasChildR( const cmXmlNode_t* np, unsigned argIdx, const cmChar_t**argV, unsigned argN, const cmChar_t* attrLabel, const cmChar_t* valueStr)
  795. {
  796. if( argIdx == argN )
  797. return NULL;
  798. const cmXmlNode_t* cnp = np->children;
  799. for(; cnp!=NULL; cnp=cnp->sibling)
  800. if( cmTextCmp(cnp->label,argV[argIdx]) == 0 )
  801. {
  802. const cmXmlNode_t* tnp;
  803. if((tnp = _cmXmlNodeHasChildR(cnp,argIdx+1,argV,argN,attrLabel,valueStr)) != NULL)
  804. {
  805. if( attrLabel != NULL )
  806. {
  807. const cmXmlAttr_t* a;
  808. if((a = cmXmlFindAttrib(tnp,attrLabel)) == NULL )
  809. continue;
  810. if( valueStr != NULL )
  811. {
  812. if( cmTextCmp(a->value,valueStr) != 0 )
  813. continue;
  814. }
  815. }
  816. }
  817. if( tnp != NULL )
  818. return tnp;
  819. }
  820. return NULL;
  821. }
  822. */
  823. 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 )
  824. {
  825. unsigned i;
  826. const cmChar_t* argV[ argN+1 ];
  827. argV[0] = label;
  828. for(i=1; i<argN+1; ++i)
  829. argV[i] = va_arg(vl,const cmChar_t*);
  830. return _cmXmlNodeHasChildR(np,0,argV,argN,attrLabel,valueStr);
  831. }
  832. bool cmXmlNodeHasChildV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  833. {
  834. return _cmXmlNodeHasChildV(np,label,vl,_cmXmlLabelCount(label,vl),NULL,NULL)!=NULL;
  835. }
  836. bool cmXmlNodeHasChild( const cmXmlNode_t* np, const cmChar_t* label, ... )
  837. {
  838. va_list vl;
  839. va_start(vl,label);
  840. bool fl = cmXmlNodeHasChildV(np,label,vl);
  841. va_end(vl);
  842. return fl;
  843. }
  844. bool _cmXmlNodeHasChildWithAttrAndValueV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl0, bool valueFl )
  845. {
  846. unsigned argN = _cmXmlLabelCount(label,vl0);
  847. unsigned n = valueFl ? 2 : 1;
  848. const cmChar_t* attrLabel = NULL;
  849. const cmChar_t* valueStr = NULL;
  850. const cmXmlNode_t* cnp = NULL;
  851. va_list vl1;
  852. unsigned i;
  853. va_copy(vl1,vl0);
  854. assert( argN > n-1 ); // an attribute label must be given.
  855. if( argN <= n-1 )
  856. goto errLabel;
  857. argN -= n;
  858. // advance vl0 to the attribute label
  859. for(i=1; i<argN; ++i)
  860. {
  861. attrLabel = va_arg(vl0,const cmChar_t*);
  862. assert( attrLabel != NULL );
  863. }
  864. // get the attr label
  865. attrLabel = va_arg(vl0,const cmChar_t*);
  866. if( valueFl )
  867. valueStr = va_arg(vl0,const cmChar_t*);
  868. cnp = _cmXmlNodeHasChildV(np,label,vl1,argN,attrLabel,valueStr);
  869. errLabel:
  870. va_end(vl1);
  871. return cnp != NULL;
  872. }
  873. bool cmXmlNodeHasChildWithAttrAndValueV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  874. { return _cmXmlNodeHasChildWithAttrAndValueV(np,label,vl,true); }
  875. bool cmXmlNodeHasChildWithAttrAndValue( const cmXmlNode_t* np, const cmChar_t* label, ... )
  876. {
  877. va_list vl;
  878. va_start(vl,label);
  879. bool fl = cmXmlNodeHasChildWithAttrAndValueV(np,label,vl);
  880. va_end(vl);
  881. return fl;
  882. }
  883. bool cmXmlNodeHasChildWithAttrV( const cmXmlNode_t* np, const cmChar_t* label, va_list vl )
  884. { return _cmXmlNodeHasChildWithAttrAndValueV(np,label,vl,false); }
  885. bool cmXmlNodeHasChildWithAttr( const cmXmlNode_t* np, const cmChar_t* label, ... )
  886. {
  887. va_list vl;
  888. va_start(vl,label);
  889. bool fl = cmXmlNodeHasChildWithAttrV(np,label,vl);
  890. va_end(vl);
  891. return fl;
  892. }
  893. cmXmlRC_t cmXmlTest( cmCtx_t* ctx, const cmChar_t* fn )
  894. {
  895. cmXmlRC_t rc = kOkXmlRC;
  896. cmXmlH_t h = cmXmlNullHandle;
  897. if((rc = cmXmlAlloc(ctx, &h, fn )) != kOkXmlRC )
  898. return cmErrMsg(&ctx->err,rc,"XML alloc failed.");
  899. if((rc = cmXmlParse(h,fn)) != kOkXmlRC )
  900. goto errLabel;
  901. cmXmlAttr_t aV[] =
  902. {
  903. { "id","P1"}
  904. };
  905. if( cmXmlSearch(cmXmlRoot(h),"part",aV,1) == NULL )
  906. {
  907. cmErrMsg(&ctx->err,kTestFailXmlRC,"Search failed.");
  908. goto errLabel;
  909. }
  910. //cmXmlPrint(h,&ctx->rpt);
  911. errLabel:
  912. cmXmlFree(&h);
  913. return rc;
  914. }