outline.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*****************************************************************
  2. * outline.c
  3. *
  4. * Copyright 1999, Clark Cooper
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the license contained in the
  9. * COPYING file that comes with the expat distribution.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  15. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  16. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  17. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. *
  19. * Read an XML document from standard input and print an element
  20. * outline on standard output.
  21. * Must be used with Expat compiled for UTF-8 output.
  22. */
  23. #include <stdio.h>
  24. #include <expat.h>
  25. #ifdef XML_LARGE_SIZE
  26. #if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
  27. #define XML_FMT_INT_MOD "I64"
  28. #else
  29. #define XML_FMT_INT_MOD "ll"
  30. #endif
  31. #else
  32. #define XML_FMT_INT_MOD "l"
  33. #endif
  34. #define BUFFSIZE 8192
  35. char Buff[BUFFSIZE];
  36. int Depth;
  37. static void XMLCALL
  38. start(void *data, const char *el, const char **attr)
  39. {
  40. int i;
  41. (void)data;
  42. for (i = 0; i < Depth; i++)
  43. printf(" ");
  44. printf("%s", el);
  45. for (i = 0; attr[i]; i += 2) {
  46. printf(" %s='%s'", attr[i], attr[i + 1]);
  47. }
  48. printf("\n");
  49. Depth++;
  50. }
  51. static void XMLCALL
  52. end(void *data, const char *el)
  53. {
  54. (void)data;
  55. (void)el;
  56. Depth--;
  57. }
  58. int
  59. main(int argc, char *argv[])
  60. {
  61. XML_Parser p = XML_ParserCreate(NULL);
  62. (void)argc;
  63. (void)argv;
  64. if (! p) {
  65. fprintf(stderr, "Couldn't allocate memory for parser\n");
  66. exit(-1);
  67. }
  68. XML_SetElementHandler(p, start, end);
  69. for (;;) {
  70. int done;
  71. int len;
  72. len = (int)fread(Buff, 1, BUFFSIZE, stdin);
  73. if (ferror(stdin)) {
  74. fprintf(stderr, "Read error\n");
  75. exit(-1);
  76. }
  77. done = feof(stdin);
  78. if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
  79. fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
  80. XML_GetCurrentLineNumber(p),
  81. XML_ErrorString(XML_GetErrorCode(p)));
  82. exit(-1);
  83. }
  84. if (done)
  85. break;
  86. }
  87. XML_ParserFree(p);
  88. return 0;
  89. }