minixmlvalid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* $Id: minixmlvalid.c,v 1.7 2015/07/15 12:41:15 nanard Exp $ */
  2. /* MiniUPnP Project
  3. * http://miniupnp.tuxfamily.org/ or http://miniupnp.free.fr/
  4. * minixmlvalid.c :
  5. * validation program for the minixml parser
  6. *
  7. * (c) 2006-2011 Thomas Bernard */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "minixml.h"
  12. /* xml event structure */
  13. struct event {
  14. enum { ELTSTART, ELTEND, ATT, CHARDATA } type;
  15. const char * data;
  16. int len;
  17. };
  18. struct eventlist {
  19. int n;
  20. struct event * events;
  21. };
  22. /* compare 2 xml event lists
  23. * return 0 if the two lists are equals */
  24. int evtlistcmp(struct eventlist * a, struct eventlist * b)
  25. {
  26. int i;
  27. struct event * ae, * be;
  28. if(a->n != b->n)
  29. {
  30. printf("event number not matching : %d != %d\n", a->n, b->n);
  31. /*return 1;*/
  32. }
  33. for(i=0; i<a->n; i++)
  34. {
  35. ae = a->events + i;
  36. be = b->events + i;
  37. if( (ae->type != be->type)
  38. ||(ae->len != be->len)
  39. ||memcmp(ae->data, be->data, ae->len))
  40. {
  41. printf("Found a difference : %d '%.*s' != %d '%.*s'\n",
  42. ae->type, ae->len, ae->data,
  43. be->type, be->len, be->data);
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }
  49. /* Test data */
  50. static const char xmldata[] =
  51. "<xmlroot>\n"
  52. " <elt1 att1=\"attvalue1\" att2=\"attvalue2\">"
  53. "character data"
  54. "</elt1> \n \t"
  55. "<elt1b/>"
  56. "<elt1>\n<![CDATA[ <html>stuff !\n ]]> \n</elt1>\n"
  57. "<elt2a> \t<elt2b>chardata1</elt2b><elt2b> chardata2 </elt2b></elt2a>"
  58. "</xmlroot>";
  59. static const struct event evtref[] =
  60. {
  61. {ELTSTART, "xmlroot", 7},
  62. {ELTSTART, "elt1", 4},
  63. /* attributes */
  64. {CHARDATA, "character data", 14},
  65. {ELTEND, "elt1", 4},
  66. {ELTSTART, "elt1b", 5},
  67. {ELTSTART, "elt1", 4},
  68. {CHARDATA, " <html>stuff !\n ", 16},
  69. {ELTEND, "elt1", 4},
  70. {ELTSTART, "elt2a", 5},
  71. {ELTSTART, "elt2b", 5},
  72. {CHARDATA, "chardata1", 9},
  73. {ELTEND, "elt2b", 5},
  74. {ELTSTART, "elt2b", 5},
  75. {CHARDATA, " chardata2 ", 11},
  76. {ELTEND, "elt2b", 5},
  77. {ELTEND, "elt2a", 5},
  78. {ELTEND, "xmlroot", 7}
  79. };
  80. void startelt(void * data, const char * p, int l)
  81. {
  82. struct eventlist * evtlist = data;
  83. struct event * evt;
  84. evt = evtlist->events + evtlist->n;
  85. /*printf("startelt : %.*s\n", l, p);*/
  86. evt->type = ELTSTART;
  87. evt->data = p;
  88. evt->len = l;
  89. evtlist->n++;
  90. }
  91. void endelt(void * data, const char * p, int l)
  92. {
  93. struct eventlist * evtlist = data;
  94. struct event * evt;
  95. evt = evtlist->events + evtlist->n;
  96. /*printf("endelt : %.*s\n", l, p);*/
  97. evt->type = ELTEND;
  98. evt->data = p;
  99. evt->len = l;
  100. evtlist->n++;
  101. }
  102. void chardata(void * data, const char * p, int l)
  103. {
  104. struct eventlist * evtlist = data;
  105. struct event * evt;
  106. evt = evtlist->events + evtlist->n;
  107. /*printf("chardata : '%.*s'\n", l, p);*/
  108. evt->type = CHARDATA;
  109. evt->data = p;
  110. evt->len = l;
  111. evtlist->n++;
  112. }
  113. int testxmlparser(const char * xml, int size)
  114. {
  115. int r;
  116. struct eventlist evtlist;
  117. struct eventlist evtlistref;
  118. struct xmlparser parser;
  119. evtlist.n = 0;
  120. evtlist.events = malloc(sizeof(struct event)*100);
  121. if(evtlist.events == NULL)
  122. {
  123. fprintf(stderr, "Memory allocation error.\n");
  124. return -1;
  125. }
  126. memset(&parser, 0, sizeof(parser));
  127. parser.xmlstart = xml;
  128. parser.xmlsize = size;
  129. parser.data = &evtlist;
  130. parser.starteltfunc = startelt;
  131. parser.endeltfunc = endelt;
  132. parser.datafunc = chardata;
  133. parsexml(&parser);
  134. printf("%d events\n", evtlist.n);
  135. /* compare */
  136. evtlistref.n = sizeof(evtref)/sizeof(struct event);
  137. evtlistref.events = (struct event *)evtref;
  138. r = evtlistcmp(&evtlistref, &evtlist);
  139. free(evtlist.events);
  140. return r;
  141. }
  142. int main(int argc, char * * argv)
  143. {
  144. int r;
  145. (void)argc; (void)argv;
  146. r = testxmlparser(xmldata, sizeof(xmldata)-1);
  147. if(r)
  148. printf("minixml validation test failed\n");
  149. return r;
  150. }