Fixed incorrect cast which would cause 64-bit integers not to be set to a 64-bit...
[mirror-ossqm-expat.git] / xmlwf / xmlfile.c
blobedcf1a5ba167d8766646a3d402013ced13d804fb
1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stddef.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #ifdef COMPILED_FROM_DSP
11 #include "winconfig.h"
12 #else
13 #ifdef HAVE_EXPAT_CONFIG_H
14 #include "expat_config.h"
15 #endif
16 #endif
17 #include "expat.h"
18 #include "xmlfile.h"
19 #include "xmltchar.h"
20 #include "filemap.h"
22 #ifdef _MSC_VER
23 #include <io.h>
24 #endif
26 #ifdef AMIGA_SHARED_LIB
27 #include <proto/expat.h>
28 #endif
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
34 #ifndef O_BINARY
35 #ifdef _O_BINARY
36 #define O_BINARY _O_BINARY
37 #else
38 #define O_BINARY 0
39 #endif
40 #endif
42 #ifdef _DEBUG
43 #define READ_SIZE 16
44 #else
45 #define READ_SIZE (1024*8)
46 #endif
49 typedef struct {
50 XML_Parser parser;
51 int *retPtr;
52 } PROCESS_ARGS;
54 static void
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);
59 if (message)
60 ftprintf(stdout, T("%s:%d:%d: %s\n"),
61 filename,
62 XML_GetErrorLineNumber(parser),
63 XML_GetErrorColumnNumber(parser),
64 message);
65 else
66 ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
69 static void
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);
77 *retPtr = 0;
79 else
80 *retPtr = 1;
83 #ifdef WIN32
85 static int
86 isAsciiLetter(XML_Char c)
88 return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
91 #endif /* WIN32 */
93 static const XML_Char *
94 resolveSystemId(const XML_Char *base, const XML_Char *systemId,
95 XML_Char **toFree)
97 XML_Char *s;
98 *toFree = 0;
99 if (!base
100 || *systemId == T('/')
101 #ifdef WIN32
102 || *systemId == T('\\')
103 || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
104 #endif
106 return systemId;
107 *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)
108 * sizeof(XML_Char));
109 if (!*toFree)
110 return systemId;
111 tcscpy(*toFree, base);
112 s = *toFree;
113 if (tcsrchr(s, T('/')))
114 s = tcsrchr(s, T('/')) + 1;
115 #ifdef WIN32
116 if (tcsrchr(s, T('\\')))
117 s = tcsrchr(s, T('\\')) + 1;
118 #endif
119 tcscpy(s, systemId);
120 return *toFree;
123 static int
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)
130 int result;
131 XML_Char *s;
132 const XML_Char *filename;
133 XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
134 PROCESS_ARGS args;
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))
140 result = 0;
141 free(s);
142 XML_ParserFree(entParser);
143 return result;
146 static int
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);
154 if (fd < 0) {
155 tperror(filename);
156 return 0;
159 for (;;) {
160 int nread;
161 char *buf = (char *)XML_GetBuffer(parser, READ_SIZE);
162 if (!buf) {
163 if (filename != NULL)
164 close(fd);
165 ftprintf(stderr, T("%s: out of memory\n"),
166 filename != NULL ? filename : "xmlwf");
167 return 0;
169 nread = read(fd, buf, READ_SIZE);
170 if (nread < 0) {
171 tperror(filename != NULL ? filename : "STDIN");
172 if (filename != NULL)
173 close(fd);
174 return 0;
176 if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) {
177 reportError(parser, filename != NULL ? filename : "STDIN");
178 if (filename != NULL)
179 close(fd);
180 return 0;
182 if (nread == 0) {
183 if (filename != NULL)
184 close(fd);
185 break;;
188 return 1;
191 static int
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)
198 XML_Char *s;
199 const XML_Char *filename;
200 int ret;
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);
205 free(s);
206 XML_ParserFree(entParser);
207 return ret;
211 XML_ProcessFile(XML_Parser parser,
212 const XML_Char *filename,
213 unsigned flags)
215 int result;
217 if (!XML_SetBase(parser, filename)) {
218 ftprintf(stderr, T("%s: out of memory"), filename);
219 exit(1);
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) {
228 PROCESS_ARGS args;
229 args.retPtr = &result;
230 args.parser = parser;
231 if (!filemap(filename, processFile, &args))
232 result = 0;
234 else
235 result = processStream(filename, parser);
236 return result;