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 #ifdef HAVE_EXPAT_CONFIG_H
14 #include "expat_config.h"
26 #ifdef AMIGA_SHARED_LIB
27 #include <proto/expat.h>
36 #define O_BINARY _O_BINARY
45 #define READ_SIZE (1024*8)
55 reportError(XML_Parser parser
, const XML_Char
*filename
)
57 enum XML_Error code
= XML_GetErrorCode(parser
);
58 const XML_Char
*message
= XML_ErrorString(code
);
60 ftprintf(stdout
, T("%s:%d:%d: %s\n"),
62 XML_GetErrorLineNumber(parser
),
63 XML_GetErrorColumnNumber(parser
),
66 ftprintf(stderr
, T("%s: (unknown message %d)\n"), filename
, code
);
70 processFile(const void *data
, size_t size
,
71 const XML_Char
*filename
, void *args
)
73 XML_Parser parser
= ((PROCESS_ARGS
*)args
)->parser
;
74 int *retPtr
= ((PROCESS_ARGS
*)args
)->retPtr
;
75 if (XML_Parse(parser
, (const char *)data
, size
, 1) == XML_STATUS_ERROR
) {
76 reportError(parser
, filename
);
86 isAsciiLetter(XML_Char c
)
88 return (T('a') <= c
&& c
<= T('z')) || (T('A') <= c
&& c
<= T('Z'));
93 static const XML_Char
*
94 resolveSystemId(const XML_Char
*base
, const XML_Char
*systemId
,
100 || *systemId
== T('/')
102 || *systemId
== T('\\')
103 || (isAsciiLetter(systemId
[0]) && systemId
[1] == T(':'))
107 *toFree
= (XML_Char
*)malloc((tcslen(base
) + tcslen(systemId
) + 2)
111 tcscpy(*toFree
, base
);
113 if (tcsrchr(s
, T('/')))
114 s
= tcsrchr(s
, T('/')) + 1;
116 if (tcsrchr(s
, T('\\')))
117 s
= tcsrchr(s
, T('\\')) + 1;
124 externalEntityRefFilemap(XML_Parser parser
,
125 const XML_Char
*context
,
126 const XML_Char
*base
,
127 const XML_Char
*systemId
,
128 const XML_Char
*publicId
)
132 const XML_Char
*filename
;
133 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
135 args
.retPtr
= &result
;
136 args
.parser
= entParser
;
137 filename
= resolveSystemId(base
, systemId
, &s
);
138 XML_SetBase(entParser
, filename
);
139 if (!filemap(filename
, processFile
, &args
))
142 XML_ParserFree(entParser
);
147 processStream(const XML_Char
*filename
, XML_Parser parser
)
149 /* passing NULL for filename means read intput from stdin */
150 int fd
= 0; /* 0 is the fileno for stdin */
152 if (filename
!= NULL
) {
153 fd
= topen(filename
, O_BINARY
|O_RDONLY
);
161 char *buf
= (char *)XML_GetBuffer(parser
, READ_SIZE
);
163 if (filename
!= NULL
)
165 ftprintf(stderr
, T("%s: out of memory\n"),
166 filename
!= NULL
? filename
: "xmlwf");
169 nread
= read(fd
, buf
, READ_SIZE
);
171 tperror(filename
!= NULL
? filename
: "STDIN");
172 if (filename
!= NULL
)
176 if (XML_ParseBuffer(parser
, nread
, nread
== 0) == XML_STATUS_ERROR
) {
177 reportError(parser
, filename
!= NULL
? filename
: "STDIN");
178 if (filename
!= NULL
)
183 if (filename
!= NULL
)
192 externalEntityRefStream(XML_Parser parser
,
193 const XML_Char
*context
,
194 const XML_Char
*base
,
195 const XML_Char
*systemId
,
196 const XML_Char
*publicId
)
199 const XML_Char
*filename
;
201 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
202 filename
= resolveSystemId(base
, systemId
, &s
);
203 XML_SetBase(entParser
, filename
);
204 ret
= processStream(filename
, entParser
);
206 XML_ParserFree(entParser
);
211 XML_ProcessFile(XML_Parser parser
,
212 const XML_Char
*filename
,
217 if (!XML_SetBase(parser
, filename
)) {
218 ftprintf(stderr
, T("%s: out of memory"), filename
);
222 if (flags
& XML_EXTERNAL_ENTITIES
)
223 XML_SetExternalEntityRefHandler(parser
,
224 (flags
& XML_MAP_FILE
)
225 ? externalEntityRefFilemap
226 : externalEntityRefStream
);
227 if (flags
& XML_MAP_FILE
) {
229 args
.retPtr
= &result
;
230 args
.parser
= parser
;
231 if (!filemap(filename
, processFile
, &args
))
235 result
= processStream(filename
, parser
);