2
0

minixmlvalid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* $Id: minixmlvalid.c,v 1.2 2006/11/30 11:31:55 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 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. return 1;
  30. for(i=0; i<a->n; i++)
  31. {
  32. ae = a->events + i;
  33. be = b->events + i;
  34. if( (ae->type != be->type)
  35. ||(ae->len != be->len)
  36. ||memcmp(ae->data, be->data, ae->len))
  37. {
  38. printf("Found a difference : %d '%.*s' != %d '%.*s'\n",
  39. ae->type, ae->len, ae->data,
  40. be->type, be->len, be->data);
  41. return 1;
  42. }
  43. }
  44. return 0;
  45. }
  46. /* Test data */
  47. static const char xmldata[] =
  48. "<xmlroot>\n"
  49. " <elt1 att1=\"attvalue1\" att2=\"attvalue2\">"
  50. "character data"
  51. "</elt1> \n \t"
  52. "<elt1b/>"
  53. "<elt2a> \t<elt2b>chardata1</elt2b><elt2b>chardata2</elt2b></elt2a>"
  54. "</xmlroot>";
  55. static const struct event evtref[] =
  56. {
  57. {ELTSTART, "xmlroot", 7},
  58. {ELTSTART, "elt1", 4},
  59. /* attributes */
  60. {CHARDATA, "character data", 14},
  61. {ELTEND, "elt1", 4},
  62. {ELTSTART, "elt1b", 5},
  63. {ELTSTART, "elt2a", 5},
  64. {ELTSTART, "elt2b", 5},
  65. {CHARDATA, "chardata1", 9},
  66. {ELTEND, "elt2b", 5},
  67. {ELTSTART, "elt2b", 5},
  68. {CHARDATA, "chardata2", 9},
  69. {ELTEND, "elt2b", 5},
  70. {ELTEND, "elt2a", 5},
  71. {ELTEND, "xmlroot", 7}
  72. };
  73. void startelt(void * data, const char * p, int l)
  74. {
  75. struct eventlist * evtlist = data;
  76. struct event * evt;
  77. evt = evtlist->events + evtlist->n;
  78. /*printf("startelt : %.*s\n", l, p);*/
  79. evt->type = ELTSTART;
  80. evt->data = p;
  81. evt->len = l;
  82. evtlist->n++;
  83. }
  84. void endelt(void * data, const char * p, int l)
  85. {
  86. struct eventlist * evtlist = data;
  87. struct event * evt;
  88. evt = evtlist->events + evtlist->n;
  89. /*printf("endelt : %.*s\n", l, p);*/
  90. evt->type = ELTEND;
  91. evt->data = p;
  92. evt->len = l;
  93. evtlist->n++;
  94. }
  95. void chardata(void * data, const char * p, int l)
  96. {
  97. struct eventlist * evtlist = data;
  98. struct event * evt;
  99. evt = evtlist->events + evtlist->n;
  100. /*printf("chardata : '%.*s'\n", l, p);*/
  101. evt->type = CHARDATA;
  102. evt->data = p;
  103. evt->len = l;
  104. evtlist->n++;
  105. }
  106. int testxmlparser(const char * xml, int size)
  107. {
  108. int r;
  109. struct eventlist evtlist;
  110. struct eventlist evtlistref;
  111. struct xmlparser parser;
  112. evtlist.n = 0;
  113. evtlist.events = malloc(sizeof(struct event)*100);
  114. memset(&parser, 0, sizeof(parser));
  115. parser.xmlstart = xml;
  116. parser.xmlsize = size;
  117. parser.data = &evtlist;
  118. parser.starteltfunc = startelt;
  119. parser.endeltfunc = endelt;
  120. parser.datafunc = chardata;
  121. parsexml(&parser);
  122. printf("%d events\n", evtlist.n);
  123. /* compare */
  124. evtlistref.n = sizeof(evtref)/sizeof(struct event);
  125. evtlistref.events = (struct event *)evtref;
  126. r = evtlistcmp(&evtlistref, &evtlist);
  127. free(evtlist.events);
  128. return r;
  129. }
  130. int main(int argc, char * * argv)
  131. {
  132. int r;
  133. r = testxmlparser(xmldata, sizeof(xmldata)-1);
  134. if(r)
  135. printf("minixml validation test failed\n");
  136. return r;
  137. }