testsimplexml.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /** **************************************************************************
  2. * testsimplexml.c
  3. *
  4. * Copyright 2008 Bryan Ischo <[email protected]>
  5. *
  6. * This file is part of libs3.
  7. *
  8. * libs3 is free software: you can redistribute it and/or modify it under the
  9. * terms of the GNU Lesser General Public License as published by the Free
  10. * Software Foundation, version 3 or above of the License. You can also
  11. * redistribute and/or modify it under the terms of the GNU General Public
  12. * License, version 2 or above of the License.
  13. *
  14. * In addition, as a special exception, the copyright holders give
  15. * permission to link the code of this library and its programs with the
  16. * OpenSSL library, and distribute linked combinations including the two.
  17. *
  18. * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
  19. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  21. * details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * version 3 along with libs3, in a file named COPYING. If not, see
  25. * <https://www.gnu.org/licenses/>.
  26. *
  27. * You should also have received a copy of the GNU General Public License
  28. * version 2 along with libs3, in a file named COPYING-GPLv2. If not, see
  29. * <http://www.gnu.org/licenses/>.
  30. *
  31. ************************************************************************** **/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <time.h>
  36. #include "simplexml.h"
  37. static S3Status simpleXmlCallback(const char *elementPath, const char *data,
  38. int dataLen, void *callbackData)
  39. {
  40. (void) callbackData;
  41. printf("[%s]: [%.*s]\n", elementPath, dataLen, data);
  42. return S3StatusOK;
  43. }
  44. // The only argument allowed is a specification of the random seed to use
  45. int main(int argc, char **argv)
  46. {
  47. if (argc > 1) {
  48. char *arg = argv[1];
  49. int seed = 0;
  50. while (*arg) {
  51. seed *= 10;
  52. seed += (*arg++ - '0');
  53. }
  54. srand(seed);
  55. }
  56. else {
  57. srand(time(0));
  58. }
  59. SimpleXml simpleXml;
  60. simplexml_initialize(&simpleXml, &simpleXmlCallback, 0);
  61. // Read chunks of 10K from stdin, and then feed them in random chunks
  62. // to simplexml_add
  63. char inbuf[10000];
  64. int amt_read;
  65. while ((amt_read = fread(inbuf, 1, sizeof(inbuf), stdin)) > 0) {
  66. char *buf = inbuf;
  67. while (amt_read) {
  68. int amt = (rand() % amt_read) + 1;
  69. S3Status status = simplexml_add(&simpleXml, buf, amt);
  70. if (status != S3StatusOK) {
  71. fprintf(stderr, "ERROR: Parse failure: %d\n", status);
  72. simplexml_deinitialize(&simpleXml);
  73. return -1;
  74. }
  75. buf += amt, amt_read -= amt;
  76. }
  77. }
  78. simplexml_deinitialize(&simpleXml);
  79. return 0;
  80. }