📄 xmlwf.c
字号:
/*The contents of this file are subject to the Mozilla Public LicenseVersion 1.0 (the "License"); you may not use this file except incompliance with the License. You may obtain a copy of the License athttp://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 theLicense for the specific language governing rights and limitationsunder 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) 1998James Clark. All Rights Reserved.Contributor(s):*/#include "xmlparse.h"#include "filemap.h"#include "codepage.h"#include <stdio.h>#include <stdlib.h>#include <stddef.h>#include <string.h>#include <fcntl.h>#ifdef _MSC_VER#include <io.h>#endif#ifdef _POSIX_SOURCE#include <unistd.h>#endif#ifndef O_BINARY#ifdef _O_BINARY#define O_BINARY _O_BINARY#else#define O_BINARY 0#endif#endif#ifdef _MSC_VER#include <crtdbg.h>#endif#ifdef _DEBUG#define READ_SIZE 16#else#define READ_SIZE (1024*8)#endif#ifdef XML_UNICODE#ifndef XML_UNICODE_WCHAR_T#error xmlwf requires a 16-bit Unicode-compatible wchar_t #endif#define T(x) L ## x#define ftprintf fwprintf#define tfopen _wfopen#define fputts fputws#define puttc putwc#define tcscmp wcscmp#define tcscpy wcscpy#define tcscat wcscat#define tcschr wcschr#define tcsrchr wcsrchr#define tcslen wcslen#define tperror _wperror#define topen _wopen#define tmain wmain#define tremove _wremove#else /* not XML_UNICODE */#define T(x) x#define ftprintf fprintf#define tfopen fopen#define fputts fputs#define puttc putc#define tcscmp strcmp#define tcscpy strcpy#define tcscat strcat#define tcschr strchr#define tcsrchr strrchr#define tcslen strlen#define tperror perror#define topen open#define tmain main#define tremove remove#endif /* not XML_UNICODE */static void characterData(void *userData, const XML_Char *s, int len){ FILE *fp = userData; for (; len > 0; --len, ++s) { switch (*s) { case T('&'): fputts(T("&"), fp); break; case T('<'): fputts(T("<"), fp); break; case T('>'): fputts(T(">"), fp); break; case T('"'): fputts(T("""), fp); break; case 9: case 10: case 13: ftprintf(fp, T("&#%d;"), *s); break; default: puttc(*s, fp); break; } }}/* 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 tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2);}static void startElement(void *userData, const XML_Char *name, const XML_Char **atts){ int nAtts; const XML_Char **p; FILE *fp = userData; puttc(T('<'), fp); fputts(name, fp); p = atts; while (*p) ++p; nAtts = (p - atts) >> 1; if (nAtts > 1) qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, attcmp); while (*atts) { puttc(T(' '), fp); fputts(*atts++, fp); puttc(T('='), fp); puttc(T('"'), fp); characterData(userData, *atts, tcslen(*atts)); puttc(T('"'), fp); atts++; } puttc(T('>'), fp);}static void endElement(void *userData, const XML_Char *name){ FILE *fp = userData; puttc(T('<'), fp); puttc(T('/'), fp); fputts(name, fp); puttc(T('>'), fp);}static void processingInstruction(void *userData, const XML_Char *target, const XML_Char *data){ FILE *fp = userData; puttc(T('<'), fp); puttc(T('?'), fp); fputts(target, fp); puttc(T(' '), fp); fputts(data, fp); puttc(T('?'), fp); puttc(T('>'), fp);}static void defaultCharacterData(XML_Parser parser, const XML_Char *s, int len){ XML_DefaultCurrent(parser);}static void defaultStartElement(XML_Parser parser, const XML_Char *name, const XML_Char **atts){ XML_DefaultCurrent(parser);}static void defaultEndElement(XML_Parser parser, const XML_Char *name){ XML_DefaultCurrent(parser);}static void defaultProcessingInstruction(XML_Parser parser, const XML_Char *target, const XML_Char *data){ XML_DefaultCurrent(parser);}static void markup(XML_Parser parser, const XML_Char *s, int len){ FILE *fp = XML_GetUserData(parser); for (; len > 0; --len, ++s) puttc(*s, fp);}staticvoid metaLocation(XML_Parser parser){ const XML_Char *uri = XML_GetBase(parser); if (uri) ftprintf(XML_GetUserData(parser), T(" uri=\"%s\""), uri); ftprintf(XML_GetUserData(parser), T(" byte=\"%ld\" line=\"%d\" col=\"%d\""), XML_GetCurrentByteIndex(parser), XML_GetCurrentLineNumber(parser), XML_GetCurrentColumnNumber(parser));}staticvoid metaStartElement(XML_Parser parser, const XML_Char *name, const XML_Char **atts){ FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("<starttag name=\"%s\""), name); metaLocation(parser); if (*atts) { fputts(T(">\n"), fp); do { ftprintf(fp, T("<attribute name=\"%s\" value=\""), atts[0]); characterData(fp, atts[1], tcslen(atts[1])); fputts(T("\"/>\n"), fp); } while (*(atts += 2)); fputts(T("</starttag>\n"), fp); } else fputts(T("/>\n"), fp);}staticvoid metaEndElement(XML_Parser parser, const XML_Char *name){ FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("<endtag name=\"%s\""), name); metaLocation(parser); fputts(T("/>\n"), fp);}staticvoid metaProcessingInstruction(XML_Parser parser, const XML_Char *target, const XML_Char *data){ FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("<pi target=\"%s\" data=\""), target); characterData(fp, data, tcslen(data)); puttc(T('"'), fp); metaLocation(parser); fputts(T("/>\n"), fp);}staticvoid metaCharacterData(XML_Parser parser, const XML_Char *s, int len){ FILE *fp = XML_GetUserData(parser); fputts(T("<chars str=\""), fp); characterData(fp, s, len); puttc(T('"'), fp); metaLocation(parser); fputts(T("/>\n"), fp);}staticvoid metaUnparsedEntityDecl(XML_Parser parser, const XML_Char *entityName, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId, const XML_Char *notationName){ FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("<entity name=\"%s\""), entityName); if (publicId) ftprintf(fp, T(" public=\"%s\""), publicId); fputts(T(" system=\""), fp); characterData(fp, systemId, tcslen(systemId)); puttc(T('"'), fp); ftprintf(fp, T(" notation=\"%s\""), notationName); metaLocation(parser); fputts(T("/>\n"), fp);}staticvoid metaNotationDecl(XML_Parser parser, const XML_Char *notationName, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId){ FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("<notation name=\"%s\""), notationName); if (publicId) ftprintf(fp, T(" public=\"%s\""), publicId); if (systemId) { fputts(T(" system=\""), fp); characterData(fp, systemId, tcslen(systemId)); puttc(T('"'), fp); } metaLocation(parser); fputts(T("/>\n"), fp);}typedef struct { XML_Parser parser; int *retPtr;} PROCESS_ARGS;staticvoid reportError(XML_Parser parser, const XML_Char *filename){ int code = XML_GetErrorCode(parser); const XML_Char *message = XML_ErrorString(code); if (message) ftprintf(stdout, T("%s:%d:%ld: %s\n"), filename, XML_GetErrorLineNumber(parser), XML_GetErrorColumnNumber(parser), message); else ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -