outline.c revision 1.4
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 24 25#include <stdio.h> 26#include <expat.h> 27 28#ifdef XML_LARGE_SIZE 29#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400 30#define XML_FMT_INT_MOD "I64" 31#else 32#define XML_FMT_INT_MOD "ll" 33#endif 34#else 35#define XML_FMT_INT_MOD "l" 36#endif 37 38#define BUFFSIZE 8192 39 40char Buff[BUFFSIZE]; 41 42int Depth; 43 44static void XMLCALL 45start(void *data, const char *el, const char **attr) 46{ 47 int i; 48 (void)data; 49 50 for (i = 0; i < Depth; i++) 51 printf(" "); 52 53 printf("%s", el); 54 55 for (i = 0; attr[i]; i += 2) { 56 printf(" %s='%s'", attr[i], attr[i + 1]); 57 } 58 59 printf("\n"); 60 Depth++; 61} 62 63static void XMLCALL 64end(void *data, const char *el) 65{ 66 (void)data; 67 (void)el; 68 69 Depth--; 70} 71 72int 73main(int argc, char *argv[]) 74{ 75 XML_Parser p = XML_ParserCreate(NULL); 76 (void)argc; 77 (void)argv; 78 79 if (! p) { 80 fprintf(stderr, "Couldn't allocate memory for parser\n"); 81 exit(-1); 82 } 83 84 XML_SetElementHandler(p, start, end); 85 86 for (;;) { 87 int done; 88 int len; 89 90 len = (int)fread(Buff, 1, BUFFSIZE, stdin); 91 if (ferror(stdin)) { 92 fprintf(stderr, "Read error\n"); 93 exit(-1); 94 } 95 done = feof(stdin); 96 97 if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) { 98 fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n", 99 XML_GetCurrentLineNumber(p), 100 XML_ErrorString(XML_GetErrorCode(p))); 101 exit(-1); 102 } 103 104 if (done) 105 break; 106 } 107 XML_ParserFree(p); 108 return 0; 109} 110