]>
git.saurik.com Git - wxWidgets.git/blob - src/expat/xmlwf/xmlfile.c
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(__amigaos__)
16 #include "amigaconfig.h"
17 #elif defined(__WATCOMC__)
18 #include "watcomconfig.h"
19 #elif defined(HAVE_EXPAT_CONFIG_H)
20 #include <expat_config.h>
21 #endif /* ndef COMPILED_FROM_DSP */
28 #if (defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__LINUX__)))
32 #if defined(__amigaos__) && defined(__USE_INLINE__)
33 #include <proto/expat.h>
42 #define O_BINARY _O_BINARY
51 #define READ_SIZE (1024*8)
61 reportError(XML_Parser parser
, const XML_Char
*filename
)
63 enum XML_Error code
= XML_GetErrorCode(parser
);
64 const XML_Char
*message
= XML_ErrorString(code
);
66 ftprintf(stdout
, T("%s:%" XML_FMT_INT_MOD
"u:%" XML_FMT_INT_MOD
"u: %s\n"),
68 XML_GetErrorLineNumber(parser
),
69 XML_GetErrorColumnNumber(parser
),
72 ftprintf(stderr
, T("%s: (unknown message %d)\n"), filename
, code
);
75 /* This implementation will give problems on files larger than INT_MAX. */
77 processFile(const void *data
, size_t size
,
78 const XML_Char
*filename
, void *args
)
80 XML_Parser parser
= ((PROCESS_ARGS
*)args
)->parser
;
81 int *retPtr
= ((PROCESS_ARGS
*)args
)->retPtr
;
82 if (XML_Parse(parser
, (const char *)data
, (int)size
, 1) == XML_STATUS_ERROR
) {
83 reportError(parser
, filename
);
90 #if (defined(WIN32) || defined(__WATCOMC__))
93 isAsciiLetter(XML_Char c
)
95 return (T('a') <= c
&& c
<= T('z')) || (T('A') <= c
&& c
<= T('Z'));
100 static const XML_Char
*
101 resolveSystemId(const XML_Char
*base
, const XML_Char
*systemId
,
107 || *systemId
== T('/')
108 #if (defined(WIN32) || defined(__WATCOMC__))
109 || *systemId
== T('\\')
110 || (isAsciiLetter(systemId
[0]) && systemId
[1] == T(':'))
114 *toFree
= (XML_Char
*)malloc((tcslen(base
) + tcslen(systemId
) + 2)
118 tcscpy(*toFree
, base
);
120 if (tcsrchr(s
, T('/')))
121 s
= tcsrchr(s
, T('/')) + 1;
122 #if (defined(WIN32) || defined(__WATCOMC__))
123 if (tcsrchr(s
, T('\\')))
124 s
= tcsrchr(s
, T('\\')) + 1;
131 externalEntityRefFilemap(XML_Parser parser
,
132 const XML_Char
*context
,
133 const XML_Char
*base
,
134 const XML_Char
*systemId
,
135 const XML_Char
*publicId
)
139 const XML_Char
*filename
;
140 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
142 args
.retPtr
= &result
;
143 args
.parser
= entParser
;
144 filename
= resolveSystemId(base
, systemId
, &s
);
145 XML_SetBase(entParser
, filename
);
146 if (!filemap(filename
, processFile
, &args
))
149 XML_ParserFree(entParser
);
154 processStream(const XML_Char
*filename
, XML_Parser parser
)
156 /* passing NULL for filename means read intput from stdin */
157 int fd
= 0; /* 0 is the fileno for stdin */
159 if (filename
!= NULL
) {
160 fd
= topen(filename
, O_BINARY
|O_RDONLY
);
168 char *buf
= (char *)XML_GetBuffer(parser
, READ_SIZE
);
170 if (filename
!= NULL
)
172 ftprintf(stderr
, T("%s: out of memory\n"),
173 filename
!= NULL
? filename
: "xmlwf");
176 nread
= read(fd
, buf
, READ_SIZE
);
178 tperror(filename
!= NULL
? filename
: "STDIN");
179 if (filename
!= NULL
)
183 if (XML_ParseBuffer(parser
, nread
, nread
== 0) == XML_STATUS_ERROR
) {
184 reportError(parser
, filename
!= NULL
? filename
: "STDIN");
185 if (filename
!= NULL
)
190 if (filename
!= NULL
)
199 externalEntityRefStream(XML_Parser parser
,
200 const XML_Char
*context
,
201 const XML_Char
*base
,
202 const XML_Char
*systemId
,
203 const XML_Char
*publicId
)
206 const XML_Char
*filename
;
208 XML_Parser entParser
= XML_ExternalEntityParserCreate(parser
, context
, 0);
209 filename
= resolveSystemId(base
, systemId
, &s
);
210 XML_SetBase(entParser
, filename
);
211 ret
= processStream(filename
, entParser
);
213 XML_ParserFree(entParser
);
218 XML_ProcessFile(XML_Parser parser
,
219 const XML_Char
*filename
,
224 if (!XML_SetBase(parser
, filename
)) {
225 ftprintf(stderr
, T("%s: out of memory"), filename
);
229 if (flags
& XML_EXTERNAL_ENTITIES
)
230 XML_SetExternalEntityRefHandler(parser
,
231 (flags
& XML_MAP_FILE
)
232 ? externalEntityRefFilemap
233 : externalEntityRefStream
);
234 if (flags
& XML_MAP_FILE
) {
236 args
.retPtr
= &result
;
237 args
.parser
= parser
;
238 if (!filemap(filename
, processFile
, &args
))
242 result
= processStream(filename
, parser
);