1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
11 #ifdef COMPILED_FROM_DSP
12 #include "winconfig.h"
13 #elif defined(MACOS_CLASSIC)
14 #include "macconfig.h"
15 #elif defined(__amigaos4__)
16 #include "amigaconfig.h"
17 #elif defined(HAVE_EXPAT_CONFIG_H)
18 #include <expat_config.h>
19 #endif /* ndef COMPILED_FROM_DSP */
30 #ifdef AMIGA_SHARED_LIB
31 #include <proto/expat.h>
40 #define O_BINARY _O_BINARY
49 #define READ_SIZE (1024*8)
59 reportError(XML_Parser parser
, const XML_Char
*filename
)
61 enum XML_Error code
= XML_GetErrorCode(parser
);
62 const XML_Char
*message
= XML_ErrorString(code
);
64 ftprintf(stdout
, T("%s:%" XML_FMT_INT_MOD
"u:%" XML_FMT_INT_MOD
"u: %s\n"),
66 XML_GetErrorLineNumber(parser
),
67 XML_GetErrorColumnNumber(parser
),
70 ftprintf(stderr
, T("%s: (unknown message %d)\n"), filename
, code
);
73 /* This implementation will give problems on files larger than INT_MAX. */
75 processFile(const void *data
, size_t size
,
76 const XML_Char
*filename
, void *args
)
78 XML_Parser parser
= ((PROCESS_ARGS
*)args
)->parser
;
79 int *retPtr
= ((PROCESS_ARGS
*)args
)->retPtr
;
80 if (XML_Parse(parser
, (const char *)data
, (int)size
, 1) == XML_STATUS_ERROR
) {
81 reportError(parser
, filename
);
91 isAsciiLetter(XML_Char c
)
93 return (T('a') <= c
&& c
<= T('z')) || (T('A') <= c
&& c
<= T('Z'));
98 static const XML_Char
*
99 resolveSystemId(const XML_Char
*base
, const XML_Char
*systemId
,
105 || *systemId
== T('/')
107 || *systemId
== T('\\')
108 || (isAsciiLetter(systemId
[0]) && systemId
[1] == T(':'))
112 *toFree
= (XML_Char
*)malloc((tcslen(base
) + tcslen(systemId
) + 2)
116 tcscpy(*toFree
, base
);
118 if (tcsrchr(s
, T('/')))
119 s
= tcsrchr(s
, T('/')) + 1;
121 if (tcsrchr(s
, T('\\')))
122 s
= tcsrchr(s
, T('\\')) + 1;
129 externalEntityRefFilemap(XML_Parser parser
,
130 const XML_Char
*context
,
131 const XML_Char
*base
,
132 const XML_Char
*systemId
,
133 const XML_Char
*publicId
)
137 const XML_Char
*filename
;
138 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
140 args
.retPtr
= &result
;
141 args
.parser
= entParser
;
142 filename
= resolveSystemId(base
, systemId
, &s
);
143 XML_SetBase(entParser
, filename
);
144 if (!filemap(filename
, processFile
, &args
))
147 XML_ParserFree(entParser
);
152 processStream(const XML_Char
*filename
, XML_Parser parser
)
154 /* passing NULL for filename means read intput from stdin */
155 int fd
= 0; /* 0 is the fileno for stdin */
157 if (filename
!= NULL
) {
158 fd
= topen(filename
, O_BINARY
|O_RDONLY
);
166 char *buf
= (char *)XML_GetBuffer(parser
, READ_SIZE
);
168 if (filename
!= NULL
)
170 ftprintf(stderr
, T("%s: out of memory\n"),
171 filename
!= NULL
? filename
: "xmlwf");
174 nread
= read(fd
, buf
, READ_SIZE
);
176 tperror(filename
!= NULL
? filename
: "STDIN");
177 if (filename
!= NULL
)
181 if (XML_ParseBuffer(parser
, nread
, nread
== 0) == XML_STATUS_ERROR
) {
182 reportError(parser
, filename
!= NULL
? filename
: "STDIN");
183 if (filename
!= NULL
)
188 if (filename
!= NULL
)
197 externalEntityRefStream(XML_Parser parser
,
198 const XML_Char
*context
,
199 const XML_Char
*base
,
200 const XML_Char
*systemId
,
201 const XML_Char
*publicId
)
204 const XML_Char
*filename
;
206 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
207 filename
= resolveSystemId(base
, systemId
, &s
);
208 XML_SetBase(entParser
, filename
);
209 ret
= processStream(filename
, entParser
);
211 XML_ParserFree(entParser
);
216 XML_ProcessFile(XML_Parser parser
,
217 const XML_Char
*filename
,
222 if (!XML_SetBase(parser
, filename
)) {
223 ftprintf(stderr
, T("%s: out of memory"), filename
);
227 if (flags
& XML_EXTERNAL_ENTITIES
)
228 XML_SetExternalEntityRefHandler(parser
,
229 (flags
& XML_MAP_FILE
)
230 ? externalEntityRefFilemap
231 : externalEntityRefStream
);
232 if (flags
& XML_MAP_FILE
) {
234 args
.retPtr
= &result
;
235 args
.parser
= parser
;
236 if (!filemap(filename
, processFile
, &args
))
240 result
= processStream(filename
, parser
);