1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
10 #ifdef COMPILED_FROM_DSP
11 #include "winconfig.h"
13 #include "expat_config.h"
30 #define O_BINARY _O_BINARY
39 #define READ_SIZE (1024*8)
49 reportError(XML_Parser parser
, const XML_Char
*filename
)
51 int code
= XML_GetErrorCode(parser
);
52 const XML_Char
*message
= XML_ErrorString(code
);
54 ftprintf(stdout
, T("%s:%d:%d: %s\n"),
56 XML_GetErrorLineNumber(parser
),
57 XML_GetErrorColumnNumber(parser
),
60 ftprintf(stderr
, T("%s: (unknown message %d)\n"), filename
, code
);
64 processFile(const void *data
, size_t size
,
65 const XML_Char
*filename
, void *args
)
67 XML_Parser parser
= ((PROCESS_ARGS
*)args
)->parser
;
68 int *retPtr
= ((PROCESS_ARGS
*)args
)->retPtr
;
69 if (XML_Parse(parser
, data
, size
, 1) == XML_STATUS_ERROR
) {
70 reportError(parser
, filename
);
80 isAsciiLetter(XML_Char c
)
82 return (T('a') <= c
&& c
<= T('z')) || (T('A') <= c
&& c
<= T('Z'));
87 static const XML_Char
*
88 resolveSystemId(const XML_Char
*base
, const XML_Char
*systemId
,
94 || *systemId
== T('/')
96 || *systemId
== T('\\')
97 || (isAsciiLetter(systemId
[0]) && systemId
[1] == T(':'))
101 *toFree
= (XML_Char
*)malloc((tcslen(base
) + tcslen(systemId
) + 2)
105 tcscpy(*toFree
, base
);
107 if (tcsrchr(s
, T('/')))
108 s
= tcsrchr(s
, T('/')) + 1;
110 if (tcsrchr(s
, T('\\')))
111 s
= tcsrchr(s
, T('\\')) + 1;
118 externalEntityRefFilemap(XML_Parser parser
,
119 const XML_Char
*context
,
120 const XML_Char
*base
,
121 const XML_Char
*systemId
,
122 const XML_Char
*publicId
)
126 const XML_Char
*filename
;
127 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
129 args
.retPtr
= &result
;
130 args
.parser
= entParser
;
131 filename
= resolveSystemId(base
, systemId
, &s
);
132 XML_SetBase(entParser
, filename
);
133 if (!filemap(filename
, processFile
, &args
))
136 XML_ParserFree(entParser
);
141 processStream(const XML_Char
*filename
, XML_Parser parser
)
143 /* passing NULL for filename means read intput from stdin */
144 int fd
= 0; /* 0 is the fileno for stdin */
146 if (filename
!= NULL
) {
147 fd
= topen(filename
, O_BINARY
|O_RDONLY
);
155 char *buf
= XML_GetBuffer(parser
, READ_SIZE
);
157 if (filename
!= NULL
)
159 ftprintf(stderr
, T("%s: out of memory\n"),
160 filename
!= NULL
? filename
: "xmlwf");
163 nread
= read(fd
, buf
, READ_SIZE
);
165 tperror(filename
!= NULL
? filename
: "STDIN");
166 if (filename
!= NULL
)
170 if (XML_ParseBuffer(parser
, nread
, nread
== 0) == XML_STATUS_ERROR
) {
171 reportError(parser
, filename
!= NULL
? filename
: "STDIN");
172 if (filename
!= NULL
)
177 if (filename
!= NULL
)
186 externalEntityRefStream(XML_Parser parser
,
187 const XML_Char
*context
,
188 const XML_Char
*base
,
189 const XML_Char
*systemId
,
190 const XML_Char
*publicId
)
193 const XML_Char
*filename
;
195 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
196 filename
= resolveSystemId(base
, systemId
, &s
);
197 XML_SetBase(entParser
, filename
);
198 ret
= processStream(filename
, entParser
);
200 XML_ParserFree(entParser
);
205 XML_ProcessFile(XML_Parser parser
,
206 const XML_Char
*filename
,
211 if (!XML_SetBase(parser
, filename
)) {
212 ftprintf(stderr
, T("%s: out of memory"), filename
);
216 if (flags
& XML_EXTERNAL_ENTITIES
)
217 XML_SetExternalEntityRefHandler(parser
,
218 (flags
& XML_MAP_FILE
)
219 ? externalEntityRefFilemap
220 : externalEntityRefStream
);
221 if (flags
& XML_MAP_FILE
) {
223 args
.retPtr
= &result
;
224 args
.parser
= parser
;
225 if (!filemap(filename
, processFile
, &args
))
229 result
= processStream(filename
, parser
);