libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmXml.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 cmXmlTest( cmCtx_t* ctx, const cmChar_t* fn )
  768. {
  769. cmXmlRC_t rc = kOkXmlRC;
  770. cmXmlH_t h = cmXmlNullHandle;
  771. if((rc = cmXmlAlloc(ctx, &h, fn )) != kOkXmlRC )
  772. return cmErrMsg(&ctx->err,rc,"XML alloc failed.");
  773. if((rc = cmXmlParse(h,fn)) != kOkXmlRC )
  774. goto errLabel;
  775. cmXmlAttr_t aV[] =
  776. {
  777. { "id","P1"}
  778. };
  779. if( cmXmlSearch(cmXmlRoot(h),"part",aV,1) == NULL )
  780. {
  781. cmErrMsg(&ctx->err,kTestFailXmlRC,"Search failed.");
  782. goto errLabel;
  783. }
  784. //cmXmlPrint(h,&ctx->rpt);
  785. errLabel:
  786. cmXmlFree(&h);
  787. return rc;
  788. }