/* The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is expat. The Initial Developer of the Original Code is James Clark. Portions created by James Clark are Copyright (C) 1998 James Clark. All Rights Reserved. Contributor(s): */ #include "xmlparse.h" #include #include #include #include #include #ifdef _MSC_VER #include #endif #ifndef O_BINARY #ifdef _O_BINARY #define O_BINARY _O_BINARY #else #define O_BINARY 0 #endif #endif #ifdef _MSC_VER #include #endif #ifdef _DEBUG #define READ_SIZE 16 #else #define READ_SIZE (1024*8) #endif static void characterData(void *userData, const char *s, int len) { FILE *fp = userData; putc('"',fp); for (; len > 0; --len, ++s) { putc(*s,fp); } putc('"',fp); } /* Lexicographically comparing UTF-8 encoded attribute values, is equivalent to lexicographically comparing based on the character number. */ static int attcmp(const void *att1, const void *att2) { return strcmp(*(const char **)att1, *(const char **)att2); } static void startElement(void *userData, const char *name, const char **atts) { int nAtts; const char **p; FILE *fp = userData; putc('\n',fp); putc('(', fp); fputs(name, fp); putc('\n',fp); p = atts; while (*p) ++p; nAtts = (p - atts) >> 1; if (nAtts > 1) qsort((void *)atts, nAtts, sizeof(char *) * 2, attcmp); putc('(',fp); putc('\n',fp); while (*atts) { putc('(', fp); fputs(*atts++, fp); /* attribute */ fputs(" . ",fp); characterData(userData, *atts, strlen(*atts)); fputs(")\n",fp); atts++; } putc(')',fp); } static void endElement(void *userData, const char *name) { FILE *fp = userData; fputs("\n); close tag ",fp); fputs(name, fp); putc('\n', fp); } static void processingInstruction(void *userData, const char *target, const char *data) { FILE *fp = userData; fprintf(fp, "", target, data); } static void reportError(XML_Parser parser, const char *filename) { int code = XML_GetErrorCode(parser); const char *message = XML_ErrorString(code); if (message) fprintf(stdout, "%s:%d:%ld: %s\n", filename, XML_GetErrorLineNumber(parser), XML_GetErrorColumnNumber(parser), message); else fprintf(stderr, "%s: (unknown message %d)\n", filename, code); } static int processStream(const char *filename, XML_Parser parser) { int fd; if (!strcmp(filename,"-")) fd = fileno(stdin); else fd = open(filename, O_BINARY|O_RDONLY); if (fd < 0) { perror(filename); return 0; } for (;;) { int nread; char *buf = XML_GetBuffer(parser, READ_SIZE); if (!buf) { close(fd); fprintf(stderr, "%s: out of memory\n", filename); return 0; } nread = read(fd, buf, READ_SIZE); if (nread < 0) { perror(filename); close(fd); return 0; } if (!XML_ParseBuffer(parser, nread, nread == 0)) { reportError(parser, filename); close(fd); return 0; } if (nread == 0) { close(fd); break;; } } return 1; } static void usage(const char *prog) { fprintf(stderr, "usage: %s [-e encoding] file ...\n", prog); exit(1); } int main(int argc, char **argv) { int i; const char *encoding = NULL; #ifdef _MSC_VER _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); #endif if (argc < 2) usage(argv[0]); for (i = 1; i < argc; i++) { if ((argv[i][0] == '-') && argv[i][1]) { switch (argv[i][1]) { case 'e': encoding = argv[++i]; break; default: usage(argv[0]); break; } } else { FILE *fp = NULL; char *outName = 0; int result; XML_Parser parser = XML_ParserCreate(encoding); fp = stdout; XML_SetUserData(parser, fp); XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, characterData); XML_SetProcessingInstructionHandler(parser, processingInstruction); result = processStream(argv[i], parser); XML_ParserFree(parser); } } return 0; }