Separate file processing into xmlfile.c/h.

This commit is contained in:
James Clark 1998-11-25 08:31:50 +00:00
parent 11d8095453
commit 4b4d90b06b
4 changed files with 24 additions and 228 deletions

View file

@ -10,6 +10,7 @@ FILEMAP_OBJ=xmlwf/unixfilemap.o
OBJS=xmltok/xmltok.o \
xmltok/xmlrole.o \
xmlwf/xmlwf.o \
xmlwf/xmlfile.o \
xmlwf/codepage.o \
xmlparse/xmlparse.o \
xmlparse/hashtable.o \

View file

@ -28,6 +28,9 @@ xmlwf/filemap.h
xmlwf/readfilemap.c
xmlwf/unixfilemap.c
xmlwf/win32filemap.c
xmlwf/xmlfile.c
xmlwf/xmlfile.h
xmlwf/xmltchar.h
xmlwf/xmlwf.c
sample/elements.c
sample/build.bat

View file

@ -18,79 +18,20 @@ James 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
#include "xmlparse.h"
#include "codepage.h"
#include "xmlfile.h"
#include "xmltchar.h"
#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 */
#define NSSEP T('\001')
static void characterData(void *userData, const XML_Char *s, int len)
@ -419,149 +360,6 @@ void metaNotationDecl(XML_Parser parser,
fputts(T("/>\n"), fp);
}
typedef struct {
XML_Parser parser;
int *retPtr;
} PROCESS_ARGS;
static
void 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);
}
static
void processFile(const void *data, size_t size, const XML_Char *filename, void *args)
{
XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
if (!XML_Parse(parser, data, size, 1)) {
reportError(parser, filename);
*retPtr = 0;
}
else
*retPtr = 1;
}
static
int isAsciiLetter(XML_Char c)
{
return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
}
static
const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, XML_Char **toFree)
{
XML_Char *s;
*toFree = 0;
if (!base
|| *systemId == T('/')
#ifdef WIN32
|| *systemId == T('\\')
|| (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
#endif
)
return systemId;
*toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)*sizeof(XML_Char));
if (!*toFree)
return systemId;
tcscpy(*toFree, base);
s = *toFree;
if (tcsrchr(s, T('/')))
s = tcsrchr(s, T('/')) + 1;
#ifdef WIN32
if (tcsrchr(s, T('\\')))
s = tcsrchr(s, T('\\')) + 1;
#endif
tcscpy(s, systemId);
return *toFree;
}
static
int externalEntityRefFilemap(XML_Parser parser,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId)
{
int result;
XML_Char *s;
const XML_Char *filename;
XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
PROCESS_ARGS args;
args.retPtr = &result;
args.parser = entParser;
filename = resolveSystemId(base, systemId, &s);
XML_SetBase(entParser, filename);
if (!filemap(filename, processFile, &args))
result = 0;
free(s);
XML_ParserFree(entParser);
return result;
}
static
int processStream(const XML_Char *filename, XML_Parser parser)
{
int fd = topen(filename, O_BINARY|O_RDONLY);
if (fd < 0) {
tperror(filename);
return 0;
}
for (;;) {
int nread;
char *buf = XML_GetBuffer(parser, READ_SIZE);
if (!buf) {
close(fd);
ftprintf(stderr, T("%s: out of memory\n"), filename);
return 0;
}
nread = read(fd, buf, READ_SIZE);
if (nread < 0) {
tperror(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
int externalEntityRefStream(XML_Parser parser,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId)
{
XML_Char *s;
const XML_Char *filename;
int ret;
XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
filename = resolveSystemId(base, systemId, &s);
XML_SetBase(entParser, filename);
ret = processStream(filename, entParser);
free(s);
XML_ParserFree(entParser);
return ret;
}
static
int unknownEncodingConvert(void *data, const char *p)
@ -619,8 +417,7 @@ int tmain(int argc, XML_Char **argv)
int i;
const XML_Char *outputDir = 0;
const XML_Char *encoding = 0;
int useFilemap = 1;
int processExternalEntities = 0;
unsigned processFlags = XML_MAP_FILE;
int windowsCodePages = 0;
int outputType = 0;
int useNamespaces = 0;
@ -638,7 +435,7 @@ int tmain(int argc, XML_Char **argv)
}
j = 1;
if (argv[i][j] == T('r')) {
useFilemap = 0;
processFlags &= ~XML_MAP_FILE;
j++;
}
if (argv[i][j] == T('n')) {
@ -647,7 +444,7 @@ int tmain(int argc, XML_Char **argv)
j++;
}
if (argv[i][j] == T('x')) {
processExternalEntities = 1;
processFlags |= XML_EXTERNAL_ENTITIES;
j++;
}
if (argv[i][j] == T('w')) {
@ -764,24 +561,7 @@ int tmain(int argc, XML_Char **argv)
}
if (windowsCodePages)
XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0);
if (!XML_SetBase(parser, argv[i])) {
ftprintf(stderr, T("%s: out of memory"), argv[0]);
exit(1);
}
if (processExternalEntities)
XML_SetExternalEntityRefHandler(parser,
useFilemap
? externalEntityRefFilemap
: externalEntityRefStream);
if (useFilemap) {
PROCESS_ARGS args;
args.retPtr = &result;
args.parser = parser;
if (!filemap(argv[i], processFile, &args))
result = 0;
}
else
result = processStream(argv[i], parser);
result = XML_ProcessFile(parser, argv[i], processFlags);
if (outputDir) {
if (outputType == 'm')
fputts(T("</document>\n"), fp);

View file

@ -105,6 +105,10 @@ SOURCE=.\win32filemap.c
# End Source File
# Begin Source File
SOURCE=.\xmlfile.c
# End Source File
# Begin Source File
SOURCE=.\xmlwf.c
# End Source File
# End Group
@ -115,6 +119,14 @@ SOURCE=.\xmlwf.c
SOURCE=.\codepage.h
# End Source File
# Begin Source File
SOURCE=.\xmlfile.h
# End Source File
# Begin Source File
SOURCE=.\xmltchar.h
# End Source File
# End Group
# Begin Group "Resource Files"