outline.c 3.5 KB

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