outline.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Read an XML document from standard input and print an element
  2. outline on standard output.
  3. Must be used with Expat compiled for UTF-8 output.
  4. __ __ _
  5. ___\ \/ /_ __ __ _| |_
  6. / _ \\ /| '_ \ / _` | __|
  7. | __// \| |_) | (_| | |_
  8. \___/_/\_\ .__/ \__,_|\__|
  9. |_| XML parser
  10. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  11. Copyright (c) 2000-2017 Expat development team
  12. Licensed under the MIT license:
  13. Permission is hereby granted, free of charge, to any person obtaining
  14. a copy of this software and associated documentation files (the
  15. "Software"), to deal in the Software without restriction, including
  16. without limitation the rights to use, copy, modify, merge, publish,
  17. distribute, sublicense, and/or sell copies of the Software, and to permit
  18. persons to whom the Software is furnished to do so, subject to the
  19. following conditions:
  20. The above copyright notice and this permission notice shall be included
  21. in all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  25. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  26. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  27. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  28. USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <stdio.h>
  31. #include <expat.h>
  32. #ifdef XML_LARGE_SIZE
  33. # define XML_FMT_INT_MOD "ll"
  34. #else
  35. # define XML_FMT_INT_MOD "l"
  36. #endif
  37. #ifdef XML_UNICODE_WCHAR_T
  38. # define XML_FMT_STR "ls"
  39. #else
  40. # define XML_FMT_STR "s"
  41. #endif
  42. #define BUFFSIZE 8192
  43. char Buff[BUFFSIZE];
  44. int Depth;
  45. static void XMLCALL
  46. start(void *data, const XML_Char *el, const XML_Char **attr) {
  47. int i;
  48. (void)data;
  49. for (i = 0; i < Depth; i++)
  50. printf(" ");
  51. printf("%" XML_FMT_STR, el);
  52. for (i = 0; attr[i]; i += 2) {
  53. printf(" %" XML_FMT_STR "='%" XML_FMT_STR "'", attr[i], attr[i + 1]);
  54. }
  55. printf("\n");
  56. Depth++;
  57. }
  58. static void XMLCALL
  59. end(void *data, const XML_Char *el) {
  60. (void)data;
  61. (void)el;
  62. Depth--;
  63. }
  64. int
  65. main(int argc, char *argv[]) {
  66. XML_Parser p = XML_ParserCreate(NULL);
  67. (void)argc;
  68. (void)argv;
  69. if (! p) {
  70. fprintf(stderr, "Couldn't allocate memory for parser\n");
  71. exit(-1);
  72. }
  73. XML_SetElementHandler(p, start, end);
  74. for (;;) {
  75. int done;
  76. int len;
  77. len = (int)fread(Buff, 1, BUFFSIZE, stdin);
  78. if (ferror(stdin)) {
  79. fprintf(stderr, "Read error\n");
  80. exit(-1);
  81. }
  82. done = feof(stdin);
  83. if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
  84. fprintf(stderr,
  85. "Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
  86. XML_GetCurrentLineNumber(p),
  87. XML_ErrorString(XML_GetErrorCode(p)));
  88. exit(-1);
  89. }
  90. if (done)
  91. break;
  92. }
  93. XML_ParserFree(p);
  94. return 0;
  95. }