author | Claus Gittinger <cg@exept.de> |
Wed, 10 Nov 1999 17:20:51 +0100 | |
changeset 4978 | 0d75a92f1e56 |
parent 4597 | 9628a80e23ec |
child 5100 | 58ac5885aa41 |
permissions | -rw-r--r-- |
3746 | 1 |
" |
2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ReadWriteStream subclass:#ExternalStream |
|
14 |
instanceVariableNames:'filePointer mode buffered binary eolMode hitEOF didWrite |
|
15 |
lastErrorNumber readAhead' |
|
16 |
classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal |
|
17 |
InvalidModeSignal OpenErrorSignal StreamNotOpenSignal |
|
18 |
InvalidOperationSignal DefaultEOLMode ReadMode ReadWriteMode |
|
19 |
WriteMode AppendMode CreateReadWriteMode StreamIOErrorSignal' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Streams-External' |
|
22 |
! |
|
23 |
||
24 |
!ExternalStream primitiveDefinitions! |
|
25 |
%{ |
|
26 |
||
27 |
#include <stdio.h> |
|
28 |
#define _STDIO_H_INCLUDED_ |
|
29 |
||
30 |
#ifndef NO_FCNTL_H |
|
31 |
# include <fcntl.h> |
|
32 |
# define _FCNTL_H_INCLUDED_ |
|
33 |
#endif |
|
34 |
||
35 |
#include <errno.h> |
|
36 |
#define _ERRNO_H_INCLUDED_ |
|
37 |
||
38 |
#ifndef SEEK_SET |
|
39 |
# define SEEK_SET 0 |
|
40 |
#endif |
|
41 |
#ifndef SEEK_CUR |
|
42 |
# define SEEK_CUR 1 |
|
43 |
#endif |
|
44 |
#ifndef SEEK_END |
|
45 |
# define SEEK_END 2 |
|
46 |
#endif |
|
47 |
||
3782 | 48 |
#define CLEAR_ERRNO /* nothing */ |
3746 | 49 |
|
50 |
#ifdef LATER |
|
51 |
#define __isFilePointer(x) (__Class(x) == ExternalStream__FilePointer) |
|
52 |
#define __CHANGECLASS(o, x) (__qClass(o) = (x), o) |
|
53 |
#define __MKFILEPOINTER(f) __CHANGECLASS(__MKEXTERNALADDRESS(f), ExternalStream__FilePointer) |
|
54 |
#else |
|
55 |
#define __MKFILEPOINTER(f) __MKEXTERNALADDRESS(f) |
|
56 |
#endif |
|
57 |
||
3947 | 58 |
#define NO_STDIO |
3746 | 59 |
#undef __HANDLE_INTERRUPTS__ |
60 |
#define __HANDLE_INTERRUPTS__ |
|
61 |
#undef __BEGIN_INTERRUPTABLE__ |
|
62 |
#undef __END_INTERRUPTABLE__ |
|
63 |
#define __BEGIN_INTERRUPTABLE__ |
|
64 |
#define __END_INTERRUPTABLE__ |
|
3947 | 65 |
#ifdef i386 |
66 |
# define _X86_ |
|
67 |
#endif |
|
3746 | 68 |
|
69 |
#undef INT |
|
3947 | 70 |
#undef Array |
71 |
#undef Number |
|
72 |
#undef Method |
|
73 |
#undef Point |
|
74 |
#undef Rectangle |
|
75 |
#undef Block |
|
4298 | 76 |
#undef Time |
77 |
#undef Date |
|
3746 | 78 |
|
79 |
#define NOATOM |
|
80 |
#define NOGDICAPMASKS |
|
81 |
#define NOMETAFILE |
|
82 |
#define NOMINMAX |
|
83 |
//#define NOOPENFILE |
|
84 |
#define NOSOUND |
|
85 |
#define NOWH |
|
86 |
//#define NOCOMM |
|
87 |
#define NOKANJI |
|
88 |
#define NOCRYPT |
|
89 |
//#define NOMCX |
|
90 |
#define WIN32_LEAN_AND_MEAN |
|
3947 | 91 |
|
92 |
#include <windows.h> |
|
93 |
#include <winsock.h> /* */ |
|
94 |
#if !defined(__BORLANDC__) |
|
3746 | 95 |
# define stat _stat |
96 |
#endif |
|
97 |
||
3947 | 98 |
#ifdef __DEF_Array |
99 |
# define Array __DEF_Array |
|
100 |
#endif |
|
101 |
#ifdef __DEF_Number |
|
102 |
# define Number __DEF_Number |
|
103 |
#endif |
|
104 |
#ifdef __DEF_Method |
|
105 |
# define Method __DEF_Method |
|
106 |
#endif |
|
107 |
#ifdef __DEF_Point |
|
108 |
# define Point __DEF_Point |
|
109 |
#endif |
|
110 |
#ifdef __DEF_Block |
|
111 |
# define Block __DEF_Block |
|
112 |
#endif |
|
4298 | 113 |
#ifdef __DEF_Time |
114 |
# define Time __DEF_Time |
|
115 |
#endif |
|
116 |
#ifdef __DEF_Date |
|
117 |
# define Date __DEF_Date |
|
118 |
#endif |
|
3746 | 119 |
|
120 |
#define INT int |
|
121 |
||
122 |
#ifdef NO_STDIO |
|
123 |
//# define __HANDLEVal(o) (HFILE)(__externalAddressVal(o)) |
|
124 |
int _rtl_read(); |
|
125 |
int _rtl_write(); |
|
3947 | 126 |
|
3782 | 127 |
# ifdef __BORLANDC__ |
128 |
# define READ(ret,f, cp, n) \ |
|
4362 | 129 |
{ int __res;\ |
130 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&__res)==0) \ |
|
131 |
(ret) = __STX_API_CALL4((void*)recv,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n), (void *)0);\ |
|
132 |
else \ |
|
133 |
(ret) = __STX_C_CALL3((void*)_rtl_read,(void *)fileno(f), (void *)(cp), (void *)(n));\ |
|
134 |
} |
|
3782 | 135 |
# define WRITE(ret,f, cp, n) \ |
4362 | 136 |
{ int __res;\ |
137 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&__res)==0) \ |
|
138 |
(ret) = __STX_API_CALL4((void*)send,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n), (void *)0);\ |
|
139 |
else \ |
|
140 |
(ret) = __STX_C_CALL3((void*)_rtl_write,(void *)fileno(f), (void *)(cp), (void *)(n));\ |
|
141 |
} |
|
3782 | 142 |
# else /* MSC */ |
143 |
# define READ(ret,f, cp, n) \ |
|
4362 | 144 |
{ int __res;\ |
145 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&__res)==0) \ |
|
146 |
(ret) = __STX_API_CALL4((void*)recv,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n), (void *)0);\ |
|
147 |
else { \ |
|
148 |
(ret) = __STX_API_CALL5((void*)ReadFile,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n),(void *)&__res,(void *)0);\ |
|
149 |
if (ret) \ |
|
150 |
ret = __res; \ |
|
151 |
} \ |
|
152 |
} |
|
3782 | 153 |
# define WRITE(ret,f, cp, n) \ |
4362 | 154 |
{ int __res;\ |
155 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&__res)==0) \ |
|
156 |
(ret) = __STX_API_CALL4((void*)send,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n), (void *)0);\ |
|
157 |
else {\ |
|
158 |
(ret) = __STX_API_CALL5((void*)WriteFile,(void *)_get_osfhandle(fileno(f)), (void *)(cp), (void *)(n),(void *)&__res,(void*)0);\ |
|
159 |
if (ret) \ |
|
160 |
ret = __res; \ |
|
161 |
} \ |
|
162 |
} |
|
3782 | 163 |
# endif /* MSC */ |
164 |
||
3746 | 165 |
# define FFLUSH(fp) fflush(fp) |
166 |
# undef STDIO_NEEDS_FSEEK |
|
167 |
# define FILEPOINTER FILE * |
|
168 |
# define FILENO(f) fileno(f) |
|
3782 | 169 |
|
170 |
#else /* use STDIO */ |
|
171 |
||
3746 | 172 |
int _rtl_read(); |
173 |
int _rtl_write(); |
|
4362 | 174 |
# define STDIO_NEEDS_FSEEK |
175 |
# define FILEPOINTER FILE * |
|
176 |
# define read(f, cp, n) __STX_C_CALL3((void*)_rtl_read,(void *)(f), (void *)(cp), (void *)(n)) |
|
177 |
# define write(f, cp, n) __STX_C_CALL3((void*)_rtl_write,(void *)(f), (void *)(cp), (void *)(n)) |
|
178 |
# define FFLUSH(fp) fflush(fp) |
|
179 |
# define FILENO(f) fileno(f) |
|
180 |
||
181 |
#endif /* use STDIO */ |
|
3746 | 182 |
|
183 |
/* |
|
184 |
* stdio library requires an fseek before reading whenever a file |
|
185 |
* is open for read/write and the last operation was a write. |
|
186 |
* (also vice-versa). |
|
187 |
* All code should use the following macro before doing reads: |
|
188 |
*/ |
|
189 |
#ifdef STDIO_NEEDS_FSEEK |
|
190 |
# define OPT_FSEEK(f, pos, whence) fseek(f, pos, whence) |
|
191 |
#else |
|
192 |
# define OPT_FSEEK(f, pos, whence) /* nothing */ |
|
193 |
#endif |
|
194 |
||
195 |
#ifdef NO_STDIO |
|
196 |
//#define __READING__(f) |
|
197 |
//#define __WRITING__(f) |
|
4362 | 198 |
# define __READING__(f) \ |
3746 | 199 |
if ((__INST(didWrite) != false) \ |
200 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
201 |
__INST(didWrite) = false; \ |
|
202 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
203 |
} |
|
204 |
||
4362 | 205 |
# define __WRITING__(f) \ |
3746 | 206 |
if ((__INST(didWrite) != true) \ |
207 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
208 |
__INST(didWrite) = true; \ |
|
209 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
210 |
} |
|
4362 | 211 |
|
212 |
# define __UNGETC__(c, f, isBuffered) \ |
|
3746 | 213 |
if (isBuffered) { \ |
214 |
ungetc((c), (f)); \ |
|
215 |
} else { \ |
|
216 |
__INST(readAhead) = __MKSMALLINT((c)); \ |
|
217 |
} |
|
4362 | 218 |
|
219 |
# define __READBYTE__(ret, f, buf, isBuffered) \ |
|
3746 | 220 |
if (isBuffered) { \ |
221 |
for (;;) { \ |
|
222 |
CLEAR_ERRNO; \ |
|
223 |
(ret) = getc(f); \ |
|
224 |
if ((ret) >= 0) { \ |
|
225 |
*(buf) = (ret); \ |
|
226 |
(ret) = 1; \ |
|
227 |
} else if (ferror(f)) { \ |
|
228 |
if (errno == EINTR) { \ |
|
229 |
clearerr(f); \ |
|
230 |
continue; \ |
|
231 |
} \ |
|
232 |
} else \ |
|
233 |
(ret) = 0; \ |
|
234 |
break; \ |
|
235 |
} \ |
|
236 |
} else { \ |
|
237 |
OBJ rA = __INST(readAhead); \ |
|
238 |
if (rA != nil) { \ |
|
4362 | 239 |
*(buf) = (char)__intVal(rA); \ |
3746 | 240 |
__INST(readAhead) = nil; \ |
241 |
(ret) = 1; \ |
|
242 |
} else { \ |
|
243 |
for (;;) { \ |
|
244 |
CLEAR_ERRNO; \ |
|
4362 | 245 |
READ(ret,f, buf, 1); \ |
3746 | 246 |
if ((ret) >= 0 || errno != EINTR) \ |
247 |
break; \ |
|
248 |
} \ |
|
249 |
} \ |
|
250 |
} |
|
4362 | 251 |
|
252 |
/* |
|
253 |
* read_bytes into a c-buffer |
|
254 |
* (which may NOT move) |
|
255 |
*/ |
|
256 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered) \ |
|
3746 | 257 |
(ret) = 0; \ |
258 |
if (isBuffered) { \ |
|
259 |
int __offs = 0; \ |
|
260 |
while (__offs < (cnt)) { \ |
|
261 |
CLEAR_ERRNO; \ |
|
262 |
(ret) = getc(f); \ |
|
263 |
if ((ret) < 0) { \ |
|
264 |
if (ferror(f)) { \ |
|
265 |
if (errno == EINTR) { \ |
|
266 |
clearerr(f); \ |
|
267 |
continue; \ |
|
268 |
} \ |
|
269 |
} else { \ |
|
270 |
(ret) = 0; \ |
|
271 |
} \ |
|
272 |
break; \ |
|
273 |
} \ |
|
274 |
(buf)[__offs++] = (ret); \ |
|
275 |
} \ |
|
276 |
if (__offs > 0) \ |
|
277 |
(ret) = __offs; \ |
|
278 |
} else { \ |
|
279 |
int __offs = 0; \ |
|
280 |
\ |
|
281 |
while (__offs < (cnt)) { \ |
|
282 |
OBJ rA = __INST(readAhead); \ |
|
283 |
if (rA != nil) { \ |
|
284 |
(buf)[__offs] = __intVal(rA); \ |
|
285 |
__INST(readAhead) = nil; \ |
|
286 |
(ret) = 1; \ |
|
287 |
} else { \ |
|
288 |
CLEAR_ERRNO; \ |
|
289 |
READ(ret,f, (buf)+__offs, (cnt)-__offs); \ |
|
290 |
if ((ret) <= 0) { \ |
|
291 |
if ((ret) < 0 && errno == EINTR) { \ |
|
292 |
continue; \ |
|
293 |
} \ |
|
294 |
break; \ |
|
295 |
} \ |
|
296 |
} \ |
|
297 |
__offs += (ret); \ |
|
298 |
} \ |
|
299 |
if (__offs > 0) \ |
|
300 |
(ret) = __offs; \ |
|
301 |
} |
|
4362 | 302 |
|
303 |
#else /* use STDIO */ |
|
304 |
||
305 |
# define __READING__(f) \ |
|
306 |
if ((__INST(didWrite) != false) \ |
|
307 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
308 |
__INST(didWrite) = false; \ |
|
309 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
310 |
} |
|
311 |
||
312 |
# define __WRITING__(f) \ |
|
313 |
if ((__INST(didWrite) != true) \ |
|
314 |
&& (__INST(mode) == @symbol(readwrite))) { \ |
|
315 |
__INST(didWrite) = true; \ |
|
316 |
OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
|
317 |
} |
|
318 |
||
319 |
# define __UNGETC__(c, f, isBuffered) \ |
|
320 |
if (isBuffered) { \ |
|
321 |
ungetc((c), (f)); \ |
|
322 |
} else { \ |
|
323 |
__INST(readAhead) = __MKSMALLINT((c)); \ |
|
324 |
} |
|
325 |
||
326 |
# define __READBYTE__(ret, f, buf, isBuffered) \ |
|
327 |
if (isBuffered) { \ |
|
328 |
for (;;) { \ |
|
329 |
CLEAR_ERRNO; \ |
|
330 |
(ret) = getc(f); \ |
|
331 |
if ((ret) >= 0) { \ |
|
332 |
*(buf) = (ret); \ |
|
333 |
(ret) = 1; \ |
|
334 |
} else if (ferror(f)) { \ |
|
335 |
if (errno == EINTR) { \ |
|
336 |
__HANDLE_INTERRUPTS__; \ |
|
337 |
clearerr(f); \ |
|
338 |
continue; \ |
|
339 |
} \ |
|
340 |
} else \ |
|
341 |
(ret) = 0; \ |
|
342 |
break; \ |
|
343 |
} \ |
|
344 |
} else { \ |
|
345 |
OBJ rA = __INST(readAhead); \ |
|
346 |
if (rA != nil) { \ |
|
347 |
*(buf) = __intVal(rA); \ |
|
348 |
__INST(readAhead) = nil; \ |
|
349 |
(ret) = 1; \ |
|
350 |
} else { \ |
|
351 |
for (;;) { \ |
|
352 |
CLEAR_ERRNO; \ |
|
353 |
(ret) = read(fileno(f), buf, 1); \ |
|
354 |
if ((ret) >= 0 || errno != EINTR) \ |
|
355 |
break; \ |
|
356 |
__HANDLE_INTERRUPTS__; \ |
|
357 |
} \ |
|
358 |
} \ |
|
359 |
} |
|
360 |
||
361 |
/* |
|
362 |
* read_bytes into a c-buffer |
|
363 |
* (which may NOT move) |
|
364 |
*/ |
|
365 |
# define __READBYTES__(ret, f, buf, cnt, isBuffered) \ |
|
3746 | 366 |
(ret) = 0; \ |
367 |
if (isBuffered) { \ |
|
368 |
int __offs = 0; \ |
|
369 |
while (__offs < (cnt)) { \ |
|
370 |
CLEAR_ERRNO; \ |
|
371 |
(ret) = getc(f); \ |
|
372 |
if ((ret) < 0) { \ |
|
373 |
if (ferror(f)) { \ |
|
374 |
if (errno == EINTR) { \ |
|
375 |
__HANDLE_INTERRUPTS__; \ |
|
376 |
clearerr(f); \ |
|
377 |
continue; \ |
|
378 |
} \ |
|
379 |
} else { \ |
|
380 |
(ret) = 0; \ |
|
381 |
} \ |
|
382 |
break; \ |
|
383 |
} \ |
|
384 |
(buf)[__offs++] = (ret); \ |
|
385 |
} \ |
|
386 |
if (__offs > 0) \ |
|
387 |
(ret) = __offs; \ |
|
388 |
} else { \ |
|
389 |
int __offs = 0; \ |
|
390 |
int fd = fileno(f); \ |
|
391 |
\ |
|
392 |
while (__offs < (cnt)) { \ |
|
393 |
OBJ rA = __INST(readAhead); \ |
|
394 |
if (rA != nil) { \ |
|
395 |
(buf)[__offs] = __intVal(rA); \ |
|
396 |
__INST(readAhead) = nil; \ |
|
397 |
(ret) = 1; \ |
|
398 |
} else { \ |
|
399 |
CLEAR_ERRNO; \ |
|
400 |
(ret) = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
401 |
if ((ret) <= 0) { \ |
|
402 |
if ((ret) < 0 && errno == EINTR) { \ |
|
403 |
__HANDLE_INTERRUPTS__; \ |
|
404 |
continue; \ |
|
405 |
} \ |
|
406 |
break; \ |
|
407 |
} \ |
|
408 |
} \ |
|
409 |
__offs += (ret); \ |
|
410 |
} \ |
|
411 |
if (__offs > 0) \ |
|
412 |
(ret) = __offs; \ |
|
413 |
} |
|
414 |
||
4362 | 415 |
#endif /* use STDIO */ |
416 |
||
417 |
#ifndef NO_STDIO |
|
418 |
||
419 |
# if defined(F_GETFL) && defined(F_SETFL) && defined(FNDELAY) |
|
420 |
# define GETFLAGS(fd) fcntl(fd, F_GETFL, 0); |
|
421 |
||
422 |
# define SETFLAGS(fd, flags) fcntl(fd, F_SETFL, flags) |
|
423 |
||
424 |
# define SETNONBLOCKING(fd) \ |
|
425 |
{ \ |
|
426 |
int flags; \ |
|
427 |
flags = fcntl(fd, F_GETFL, 0); \ |
|
428 |
if (flags >= 0) { \ |
|
3746 | 429 |
fcntl(fd, F_SETFL, flags | FNDELAY); \ |
4362 | 430 |
} \ |
3746 | 431 |
} |
4362 | 432 |
# else |
433 |
# define GETFLAGS(fd) 0 |
|
434 |
# define SETFLAGS(fd, flags) /* nothing */ |
|
435 |
# define SETNONBLOCKING(fd) /* nothing */ |
|
436 |
# endif |
|
3746 | 437 |
#endif |
438 |
||
439 |
#ifdef NO_STDIO |
|
440 |
#define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered) \ |
|
441 |
{ \ |
|
442 |
int __offs = 0; \ |
|
443 |
int oldFlags; \ |
|
444 |
\ |
|
445 |
(ret) = 0; \ |
|
446 |
if (isBuffered) { \ |
|
447 |
while (__offs < (cnt)) { \ |
|
448 |
CLEAR_ERRNO; \ |
|
449 |
(ret) = getc(f); \ |
|
450 |
if ((ret) < 0) { \ |
|
451 |
if (ferror(f)) { \ |
|
452 |
if (errno == EINTR) { \ |
|
453 |
clearerr(f); \ |
|
454 |
continue; \ |
|
455 |
} \ |
|
456 |
} else { \ |
|
457 |
(ret) = 0; \ |
|
458 |
} \ |
|
459 |
break; \ |
|
460 |
} \ |
|
461 |
(buf)[__offs++] = (ret); \ |
|
462 |
} \ |
|
463 |
if (__offs > 0) \ |
|
464 |
(ret) = __offs; \ |
|
465 |
} else { \ |
|
466 |
while (__offs < (cnt)) { \ |
|
467 |
OBJ rA = __INST(readAhead); \ |
|
468 |
if (rA != nil) { \ |
|
469 |
(buf)[__offs] = __intVal(rA); \ |
|
470 |
__INST(readAhead) = nil; \ |
|
471 |
(ret) = 1; \ |
|
472 |
__offs += (ret); \ |
|
473 |
continue; \ |
|
474 |
} \ |
|
475 |
CLEAR_ERRNO; \ |
|
3823 | 476 |
{ \ |
477 |
int res; \ |
|
4362 | 478 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0) { \ |
479 |
if (res > 0) { \ |
|
480 |
if (res > ((cnt)-__offs)) \ |
|
481 |
res = (cnt)-__offs; \ |
|
482 |
READ(ret,f, (buf)+__offs, res); \ |
|
483 |
} else { \ |
|
484 |
(ret) = 0; \ |
|
485 |
} \ |
|
486 |
} else if (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)) { \ |
|
487 |
if (res > 0) { \ |
|
488 |
if (res > ((cnt)-__offs)) \ |
|
489 |
res = (cnt)-__offs; \ |
|
490 |
READ(ret,f, (buf)+__offs, res); \ |
|
491 |
} else \ |
|
492 |
ret = 0; \ |
|
493 |
} else { \ |
|
494 |
READ(ret,f, (buf)+__offs, (cnt)-__offs); \ |
|
3823 | 495 |
} \ |
496 |
} \ |
|
3746 | 497 |
if ((ret) < 0) { \ |
498 |
if (errno == EINTR) { \ |
|
499 |
continue; \ |
|
500 |
} \ |
|
501 |
break; \ |
|
502 |
} \ |
|
503 |
__offs += (ret); \ |
|
504 |
break; \ |
|
505 |
} \ |
|
506 |
if (__offs > 0) \ |
|
507 |
(ret) = __offs; \ |
|
508 |
} \ |
|
509 |
} |
|
4362 | 510 |
|
511 |
#else /* use STDIO */ |
|
512 |
||
3746 | 513 |
#define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered) \ |
514 |
{ \ |
|
515 |
int __offs = 0; \ |
|
516 |
int oldFlags; \ |
|
517 |
\ |
|
518 |
(ret) = 0; \ |
|
519 |
oldFlags = GETFLAGS(fileno(f)); \ |
|
520 |
SETNONBLOCKING(fileno(f)); \ |
|
521 |
if (isBuffered) { \ |
|
522 |
while (__offs < (cnt)) { \ |
|
523 |
CLEAR_ERRNO; \ |
|
524 |
(ret) = getc(f); \ |
|
525 |
if ((ret) < 0) { \ |
|
526 |
if (ferror(f)) { \ |
|
527 |
if (errno == EINTR) { \ |
|
528 |
__HANDLE_INTERRUPTS__; \ |
|
529 |
clearerr(f); \ |
|
530 |
continue; \ |
|
531 |
} \ |
|
532 |
} else { \ |
|
533 |
(ret) = 0; \ |
|
534 |
} \ |
|
535 |
break; \ |
|
536 |
} \ |
|
537 |
(buf)[__offs++] = (ret); \ |
|
538 |
} \ |
|
539 |
if (__offs > 0) \ |
|
540 |
(ret) = __offs; \ |
|
541 |
} else { \ |
|
542 |
int fd = fileno(f); \ |
|
543 |
\ |
|
544 |
while (__offs < (cnt)) { \ |
|
545 |
OBJ rA = __INST(readAhead); \ |
|
546 |
if (rA != nil) { \ |
|
547 |
(buf)[__offs] = __intVal(rA); \ |
|
548 |
__INST(readAhead) = nil; \ |
|
549 |
(ret) = 1; \ |
|
550 |
__offs += (ret); \ |
|
551 |
continue; \ |
|
552 |
} \ |
|
553 |
CLEAR_ERRNO; \ |
|
554 |
(ret) = read(fd, (buf)+__offs, (cnt)-__offs); \ |
|
555 |
if ((ret) < 0) { \ |
|
556 |
if (errno == EINTR) { \ |
|
557 |
__HANDLE_INTERRUPTS__; \ |
|
558 |
continue; \ |
|
559 |
} \ |
|
560 |
break; \ |
|
561 |
} \ |
|
562 |
__offs += (ret); \ |
|
563 |
break; \ |
|
564 |
} \ |
|
565 |
if (__offs > 0) \ |
|
566 |
(ret) = __offs; \ |
|
567 |
} \ |
|
568 |
SETFLAGS(fileno(f), oldFlags); \ |
|
569 |
} |
|
570 |
||
4362 | 571 |
#endif /* use STDIO */ |
3746 | 572 |
|
573 |
/* |
|
574 |
* read_bytes into an object |
|
575 |
* (which may be moved by GC) |
|
576 |
*/ |
|
577 |
#ifdef NO_STDIO |
|
4362 | 578 |
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3746 | 579 |
{ \ |
580 |
int __ooffs = obj_offs; \ |
|
581 |
int __offs = 0; \ |
|
582 |
char *buf = (char *)(obj); \ |
|
583 |
\ |
|
584 |
(ret) = 0; \ |
|
585 |
if (isBuffered) { \ |
|
586 |
while (__offs < (cnt)) { \ |
|
587 |
CLEAR_ERRNO; \ |
|
588 |
(ret) = getc(f); \ |
|
589 |
if ((ret) < 0) { \ |
|
590 |
if (ferror(f)) { \ |
|
591 |
if (errno == EINTR) { \ |
|
592 |
clearerr(f); \ |
|
593 |
/* refetch */ \ |
|
594 |
buf = (char *)(obj); \ |
|
595 |
continue; \ |
|
596 |
} \ |
|
597 |
} else { \ |
|
598 |
(ret) = 0; \ |
|
599 |
} \ |
|
600 |
break; \ |
|
601 |
} \ |
|
602 |
(buf)[__ooffs+__offs] = (ret); \ |
|
603 |
__offs++; \ |
|
604 |
} \ |
|
605 |
if (__offs > 0) \ |
|
606 |
(ret) = __offs; \ |
|
607 |
} else { \ |
|
608 |
while (__offs < (cnt)) { \ |
|
609 |
char __buf[1024]; \ |
|
610 |
OBJ rA = __INST(readAhead); \ |
|
611 |
if (rA != nil) { \ |
|
612 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
613 |
__INST(readAhead) = nil; \ |
|
614 |
(ret) = 1; \ |
|
615 |
} else { \ |
|
616 |
int l; \ |
|
617 |
CLEAR_ERRNO; \ |
|
618 |
l = (cnt)-__offs; \ |
|
619 |
if ( l > 1024) \ |
|
620 |
l = 1024; \ |
|
4362 | 621 |
READ(ret,f, __buf, l); \ |
3746 | 622 |
if ((ret) <= 0) { \ |
623 |
if ((ret) < 0 && errno == EINTR) { \ |
|
624 |
continue; \ |
|
625 |
} \ |
|
626 |
break; \ |
|
627 |
} \ |
|
628 |
} \ |
|
629 |
/* refetch */ \ |
|
4362 | 630 |
buf = (volatile char *)(obj); \ |
3746 | 631 |
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \ |
632 |
__offs += (ret); \ |
|
633 |
} \ |
|
634 |
if (__offs > 0) \ |
|
635 |
(ret) = __offs; \ |
|
636 |
} \ |
|
637 |
} |
|
4362 | 638 |
#else /* use STDIO */ |
639 |
# define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
|
3746 | 640 |
{ \ |
641 |
int __ooffs = obj_offs; \ |
|
642 |
int __offs = 0; \ |
|
643 |
char *buf = (char *)(obj); \ |
|
644 |
\ |
|
645 |
(ret) = 0; \ |
|
646 |
if (isBuffered) { \ |
|
647 |
while (__offs < (cnt)) { \ |
|
648 |
CLEAR_ERRNO; \ |
|
649 |
(ret) = getc(f); \ |
|
650 |
if ((ret) < 0) { \ |
|
651 |
if (ferror(f)) { \ |
|
652 |
if (errno == EINTR) { \ |
|
653 |
__HANDLE_INTERRUPTS__; \ |
|
654 |
clearerr(f); \ |
|
655 |
/* refetch */ \ |
|
656 |
buf = (char *)(obj); \ |
|
657 |
continue; \ |
|
658 |
} \ |
|
659 |
} else { \ |
|
660 |
(ret) = 0; \ |
|
661 |
} \ |
|
662 |
break; \ |
|
663 |
} \ |
|
664 |
(buf)[__ooffs+__offs] = (ret); \ |
|
665 |
__offs++; \ |
|
666 |
} \ |
|
667 |
if (__offs > 0) \ |
|
668 |
(ret) = __offs; \ |
|
669 |
} else { \ |
|
670 |
int fd = fileno(f); \ |
|
671 |
\ |
|
672 |
while (__offs < (cnt)) { \ |
|
673 |
OBJ rA = __INST(readAhead); \ |
|
674 |
if (rA != nil) { \ |
|
675 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
676 |
__INST(readAhead) = nil; \ |
|
677 |
(ret) = 1; \ |
|
678 |
} else { \ |
|
679 |
CLEAR_ERRNO; \ |
|
680 |
(ret) = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
681 |
if ((ret) <= 0) { \ |
|
682 |
if ((ret) < 0 && errno == EINTR) { \ |
|
683 |
__HANDLE_INTERRUPTS__; \ |
|
684 |
/* refetch */ \ |
|
685 |
buf = (char *)(obj); \ |
|
686 |
continue; \ |
|
687 |
} \ |
|
688 |
break; \ |
|
689 |
} \ |
|
690 |
} \ |
|
691 |
__offs += (ret); \ |
|
692 |
} \ |
|
693 |
if (__offs > 0) \ |
|
694 |
(ret) = __offs; \ |
|
695 |
} \ |
|
696 |
} |
|
697 |
||
4362 | 698 |
#endif /* use STDIO */ |
699 |
||
3746 | 700 |
#ifdef NO_STDIO |
4362 | 701 |
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered)\ |
702 |
{ \ |
|
703 |
int __ooffs = obj_offs; \ |
|
704 |
int __offs = 0; \ |
|
705 |
char *buf = (char *)(obj); \ |
|
706 |
\ |
|
707 |
(ret) = 0; \ |
|
708 |
if (isBuffered) { \ |
|
709 |
while (__offs < (cnt)) { \ |
|
710 |
CLEAR_ERRNO; \ |
|
711 |
(ret) = getc(f); \ |
|
712 |
if ((ret) < 0) { \ |
|
713 |
if (ferror(f)) { \ |
|
714 |
if (errno == EINTR) { \ |
|
715 |
clearerr(f); \ |
|
716 |
/* refetch */ \ |
|
717 |
buf = (char *)(obj); \ |
|
718 |
continue; \ |
|
719 |
} \ |
|
720 |
} else { \ |
|
721 |
(ret) = 0; \ |
|
722 |
} \ |
|
723 |
break; \ |
|
724 |
} \ |
|
725 |
(buf)[__ooffs+__offs] = (ret); \ |
|
726 |
__offs++; \ |
|
727 |
} \ |
|
728 |
if (__offs > 0) \ |
|
729 |
(ret) = __offs; \ |
|
730 |
} else { \ |
|
731 |
while (__offs < (cnt)) { \ |
|
732 |
char __buf[1024]; \ |
|
733 |
OBJ rA = __INST(readAhead); \ |
|
734 |
if (rA != nil) { \ |
|
735 |
(buf)[__ooffs+__offs] = __intVal(rA);\ |
|
736 |
__INST(readAhead) = nil; \ |
|
737 |
(ret) = 1; \ |
|
738 |
__offs += (ret); \ |
|
739 |
continue; \ |
|
740 |
} \ |
|
741 |
{ \ |
|
742 |
int l; \ |
|
743 |
int res; \ |
|
744 |
CLEAR_ERRNO; \ |
|
745 |
l = (cnt)-__offs; \ |
|
746 |
if ( l > 1024) \ |
|
747 |
l = 1024; \ |
|
748 |
if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0) { \ |
|
749 |
if (res > 0) { \ |
|
750 |
if (res > l) \ |
|
751 |
res = l; \ |
|
752 |
READ(ret,f, __buf, res); \ |
|
753 |
} else { \ |
|
754 |
(ret) = 0; \ |
|
755 |
} \ |
|
756 |
} else if (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)) { \ |
|
757 |
if (res > 0) { \ |
|
758 |
if (res > l) \ |
|
759 |
res = l; \ |
|
760 |
READ(ret,f, __buf, res); \ |
|
761 |
} else \ |
|
762 |
ret = 0; \ |
|
763 |
} else { \ |
|
764 |
READ(ret,f, __buf, l); \ |
|
765 |
} \ |
|
766 |
if ((ret) < 0 && errno == EINTR) { \ |
|
767 |
continue; \ |
|
768 |
} \ |
|
769 |
} \ |
|
3746 | 770 |
/* refetch */ \ |
771 |
buf = (volatile char *)(obj); \ |
|
772 |
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \ |
|
773 |
__offs += (ret); \ |
|
774 |
break; \ |
|
775 |
} \ |
|
776 |
if (__offs > 0) \ |
|
777 |
(ret) = __offs; \ |
|
778 |
} \ |
|
779 |
} |
|
780 |
||
781 |
#else |
|
4362 | 782 |
# define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3746 | 783 |
{ \ |
784 |
int __ooffs = obj_offs; \ |
|
785 |
int __offs = 0; \ |
|
786 |
char *buf = (char *)(obj); \ |
|
787 |
\ |
|
788 |
(ret) = 0; \ |
|
789 |
if (isBuffered) { \ |
|
790 |
while (__offs < (cnt)) { \ |
|
791 |
CLEAR_ERRNO; \ |
|
792 |
(ret) = getc(f); \ |
|
793 |
if ((ret) < 0) { \ |
|
794 |
if (ferror(f)) { \ |
|
795 |
if (errno == EINTR) { \ |
|
796 |
__HANDLE_INTERRUPTS__; \ |
|
797 |
clearerr(f); \ |
|
798 |
/* refetch */ \ |
|
799 |
buf = (char *)(obj); \ |
|
800 |
continue; \ |
|
801 |
} \ |
|
802 |
} else { \ |
|
803 |
(ret) = 0; \ |
|
804 |
} \ |
|
805 |
break; \ |
|
806 |
} \ |
|
807 |
(buf)[__ooffs+__offs] = (ret); \ |
|
808 |
__offs++; \ |
|
809 |
} \ |
|
810 |
if (__offs > 0) \ |
|
811 |
(ret) = __offs; \ |
|
812 |
} else { \ |
|
813 |
int fd = fileno(f); \ |
|
814 |
\ |
|
815 |
while (__offs < (cnt)) { \ |
|
816 |
OBJ rA = __INST(readAhead); \ |
|
817 |
if (rA != nil) { \ |
|
818 |
(buf)[__ooffs+__offs] = __intVal(rA); \ |
|
819 |
__INST(readAhead) = nil; \ |
|
820 |
(ret) = 1; \ |
|
821 |
__offs += (ret); \ |
|
822 |
continue; \ |
|
823 |
} \ |
|
824 |
CLEAR_ERRNO; \ |
|
825 |
(ret) = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \ |
|
826 |
if ((ret) < 0 && errno == EINTR) { \ |
|
827 |
__HANDLE_INTERRUPTS__; \ |
|
828 |
/* refetch */ \ |
|
829 |
buf = (char *)(obj); \ |
|
830 |
continue; \ |
|
831 |
} \ |
|
832 |
__offs += (ret); \ |
|
833 |
break; \ |
|
834 |
} \ |
|
835 |
if (__offs > 0) \ |
|
836 |
(ret) = __offs; \ |
|
837 |
} \ |
|
838 |
} |
|
839 |
||
840 |
||
841 |
#endif |
|
842 |
||
843 |
#ifdef NO_STDIO |
|
4362 | 844 |
# define __WRITEBYTE__(ret, f, buf, isBuffered) \ |
3746 | 845 |
if (isBuffered) { \ |
846 |
for (;;) { \ |
|
847 |
CLEAR_ERRNO; \ |
|
848 |
ret = putc(*(buf), f); \ |
|
849 |
if ((ret) >= 0) { \ |
|
850 |
(ret) = 1; \ |
|
851 |
} else if (ferror(f)) { \ |
|
852 |
if (errno == EINTR) { \ |
|
853 |
clearerr(f); \ |
|
854 |
continue; \ |
|
855 |
} \ |
|
856 |
} else \ |
|
857 |
(ret) = 0; \ |
|
858 |
break; \ |
|
859 |
} \ |
|
860 |
} else { \ |
|
861 |
for (;;) { \ |
|
862 |
CLEAR_ERRNO; \ |
|
4362 | 863 |
WRITE(ret,f, buf, 1); \ |
3746 | 864 |
if ((ret) >= 0 || errno != EINTR) \ |
865 |
break; \ |
|
866 |
} \ |
|
867 |
} |
|
868 |
#else |
|
4362 | 869 |
# define __WRITEBYTE__(ret, f, buf, isBuffered) \ |
3746 | 870 |
if (isBuffered) { \ |
871 |
for (;;) { \ |
|
872 |
CLEAR_ERRNO; \ |
|
873 |
ret = putc(*(buf), f); \ |
|
874 |
if ((ret) >= 0) { \ |
|
875 |
(ret) = 1; \ |
|
876 |
} else if (ferror(f)) { \ |
|
877 |
if (errno == EINTR) { \ |
|
878 |
__HANDLE_INTERRUPTS__; \ |
|
879 |
clearerr(f); \ |
|
880 |
continue; \ |
|
881 |
} \ |
|
882 |
} else \ |
|
883 |
(ret) = 0; \ |
|
884 |
break; \ |
|
885 |
} \ |
|
886 |
} else { \ |
|
887 |
for (;;) { \ |
|
888 |
CLEAR_ERRNO; \ |
|
889 |
(ret) = write(fileno(f), buf, 1); \ |
|
890 |
if ((ret) >= 0 || errno != EINTR) \ |
|
891 |
break; \ |
|
892 |
__HANDLE_INTERRUPTS__; \ |
|
893 |
} \ |
|
894 |
} |
|
895 |
#endif |
|
896 |
||
897 |
/* |
|
898 |
* write_bytes from a c-buffer |
|
899 |
* (which may NOT move) |
|
900 |
*/ |
|
901 |
#ifdef NO_STDIO |
|
4362 | 902 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered) \ |
3746 | 903 |
(ret) = 0; \ |
904 |
if (isBuffered) { \ |
|
905 |
int __offs = 0; \ |
|
906 |
while (__offs < (cnt)) { \ |
|
907 |
CLEAR_ERRNO; \ |
|
908 |
ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
4362 | 909 |
if ((ret) <= 0) { \ |
3746 | 910 |
if (ferror(f)) { \ |
911 |
if (errno == EINTR) { \ |
|
912 |
clearerr(f); \ |
|
913 |
continue; \ |
|
914 |
} \ |
|
915 |
break; \ |
|
916 |
} else { \ |
|
917 |
(ret) = 0; \ |
|
918 |
} \ |
|
919 |
} \ |
|
920 |
__offs += (ret); \ |
|
921 |
} \ |
|
922 |
if (__offs > 0) \ |
|
923 |
(ret) = __offs; \ |
|
924 |
} else { \ |
|
925 |
int __offs = 0; \ |
|
926 |
while (__offs < (cnt)) { \ |
|
927 |
CLEAR_ERRNO; \ |
|
4362 | 928 |
WRITE(ret,f, (buf)+__offs, (cnt)-__offs); \ |
3746 | 929 |
if (ret <= 0) { \ |
930 |
if (ret < 0 && errno == EINTR) { \ |
|
931 |
continue; \ |
|
932 |
} \ |
|
933 |
break; \ |
|
934 |
} \ |
|
935 |
__offs += (ret); \ |
|
936 |
} \ |
|
937 |
if (__offs > 0) \ |
|
938 |
(ret) = __offs; \ |
|
939 |
} |
|
940 |
#else |
|
4362 | 941 |
# define __WRITEBYTES__(ret, f, buf, cnt, isBuffered) \ |
3746 | 942 |
(ret) = 0; \ |
943 |
if (isBuffered) { \ |
|
944 |
int __offs = 0; \ |
|
945 |
while (__offs < (cnt)) { \ |
|
946 |
CLEAR_ERRNO; \ |
|
947 |
ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\ |
|
4362 | 948 |
if ((ret) <= 0) { \ |
3746 | 949 |
if (ferror(f)) { \ |
950 |
if (errno == EINTR) { \ |
|
951 |
__HANDLE_INTERRUPTS__; \ |
|
952 |
clearerr(f); \ |
|
953 |
continue; \ |
|
954 |
} \ |
|
955 |
break; \ |
|
956 |
} else { \ |
|
957 |
(ret) = 0; \ |
|
958 |
} \ |
|
959 |
} \ |
|
960 |
__offs += (ret); \ |
|
961 |
} \ |
|
962 |
if (__offs > 0) \ |
|
963 |
(ret) = __offs; \ |
|
964 |
} else { \ |
|
965 |
int __offs = 0; \ |
|
966 |
while (__offs < (cnt)) { \ |
|
967 |
CLEAR_ERRNO; \ |
|
968 |
ret = write(fileno(f), (buf)+__offs, (cnt)-__offs);\ |
|
969 |
if (ret <= 0) { \ |
|
970 |
if (ret < 0 && errno == EINTR) { \ |
|
971 |
__HANDLE_INTERRUPTS__; \ |
|
972 |
continue; \ |
|
973 |
} \ |
|
974 |
break; \ |
|
975 |
} \ |
|
976 |
__offs += (ret); \ |
|
977 |
} \ |
|
978 |
if (__offs > 0) \ |
|
979 |
(ret) = __offs; \ |
|
980 |
} |
|
981 |
#endif |
|
982 |
||
983 |
/* |
|
984 |
* write_bytes from an object |
|
985 |
* (which may be moved around by GC) |
|
986 |
*/ |
|
987 |
#ifdef NO_STDIO |
|
4362 | 988 |
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3746 | 989 |
{ \ |
990 |
int __ooffs = obj_offs; \ |
|
991 |
int __offs = 0; \ |
|
992 |
char *buf = (char *)(obj); \ |
|
993 |
\ |
|
994 |
(ret) = 0; \ |
|
995 |
if (isBuffered) { \ |
|
996 |
while (__offs < (cnt)) { \ |
|
997 |
CLEAR_ERRNO; \ |
|
4362 | 998 |
ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
3746 | 999 |
if ((ret) <= 0) { \ |
1000 |
if (ferror(f)) { \ |
|
1001 |
if (errno == EINTR) { \ |
|
1002 |
/* refetch */ \ |
|
1003 |
buf = (char *)(obj); \ |
|
1004 |
clearerr(f); \ |
|
1005 |
continue; \ |
|
1006 |
} \ |
|
1007 |
break; \ |
|
1008 |
} else { \ |
|
1009 |
(ret) = 0; \ |
|
1010 |
} \ |
|
1011 |
} \ |
|
1012 |
__offs += (ret); \ |
|
1013 |
} \ |
|
1014 |
if (__offs > 0) \ |
|
1015 |
(ret) = __offs; \ |
|
1016 |
} else { \ |
|
1017 |
while (__offs < (cnt)) { \ |
|
1018 |
char __buf[1024]; \ |
|
1019 |
int l; \ |
|
1020 |
CLEAR_ERRNO; \ |
|
1021 |
l = (cnt)-__offs; \ |
|
1022 |
if ( l > 1024) \ |
|
1023 |
l = 1024; \ |
|
1024 |
/* refetch */ \ |
|
1025 |
buf = (volatile char *)(obj); \ |
|
1026 |
memcpy(__buf,(buf)+__ooffs+__offs,l); \ |
|
4362 | 1027 |
WRITE(ret,f, __buf, l); \ |
3746 | 1028 |
if (ret <= 0) { \ |
1029 |
if (ret < 0 && errno == EINTR) { \ |
|
1030 |
continue; \ |
|
1031 |
} \ |
|
1032 |
break; \ |
|
1033 |
} \ |
|
1034 |
__offs += (ret); \ |
|
1035 |
} \ |
|
1036 |
if (__offs > 0) \ |
|
1037 |
(ret) = __offs; \ |
|
1038 |
} \ |
|
1039 |
} |
|
1040 |
#else |
|
4362 | 1041 |
# define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered) \ |
3746 | 1042 |
{ \ |
1043 |
int __ooffs = obj_offs; \ |
|
1044 |
int __offs = 0; \ |
|
1045 |
char *buf = (char *)(obj); \ |
|
1046 |
\ |
|
1047 |
(ret) = 0; \ |
|
1048 |
if (isBuffered) { \ |
|
1049 |
while (__offs < (cnt)) { \ |
|
1050 |
CLEAR_ERRNO; \ |
|
4362 | 1051 |
ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \ |
3746 | 1052 |
if ((ret) <= 0) { \ |
1053 |
if (ferror(f)) { \ |
|
1054 |
if (errno == EINTR) { \ |
|
1055 |
__HANDLE_INTERRUPTS__; \ |
|
1056 |
/* refetch */ \ |
|
1057 |
buf = (char *)(obj); \ |
|
1058 |
clearerr(f); \ |
|
1059 |
continue; \ |
|
1060 |
} \ |
|
1061 |
break; \ |
|
1062 |
} else { \ |
|
1063 |
(ret) = 0; \ |
|
1064 |
} \ |
|
1065 |
} \ |
|
1066 |
__offs += (ret); \ |
|
1067 |
} \ |
|
1068 |
if (__offs > 0) \ |
|
1069 |
(ret) = __offs; \ |
|
1070 |
} else { \ |
|
1071 |
while (__offs < (cnt)) { \ |
|
1072 |
CLEAR_ERRNO; \ |
|
4362 | 1073 |
ret = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs); \ |
3746 | 1074 |
if (ret <= 0) { \ |
1075 |
if (ret < 0 && errno == EINTR) { \ |
|
1076 |
__HANDLE_INTERRUPTS__; \ |
|
1077 |
/* refetch */ \ |
|
1078 |
buf = (char *)(obj); \ |
|
1079 |
continue; \ |
|
1080 |
} \ |
|
1081 |
break; \ |
|
1082 |
} \ |
|
1083 |
__offs += (ret); \ |
|
1084 |
} \ |
|
1085 |
if (__offs > 0) \ |
|
1086 |
(ret) = __offs; \ |
|
1087 |
} \ |
|
1088 |
} |
|
1089 |
#endif |
|
1090 |
||
1091 |
/* |
|
1092 |
* on some systems errno is a macro ... check for it here |
|
1093 |
*/ |
|
1094 |
#ifndef errno |
|
1095 |
extern errno; |
|
1096 |
#endif |
|
1097 |
||
1098 |
%} |
|
1099 |
! ! |
|
1100 |
||
1101 |
!ExternalStream class methodsFor:'documentation'! |
|
1102 |
||
1103 |
copyright |
|
1104 |
" |
|
1105 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
1106 |
All Rights Reserved |
|
1107 |
||
1108 |
This software is furnished under a license and may be used |
|
1109 |
only in accordance with the terms of that license and with the |
|
1110 |
inclusion of the above copyright notice. This software may not |
|
1111 |
be provided or otherwise made available to, or used by, any |
|
1112 |
other person. No title to or ownership of the software is |
|
1113 |
hereby transferred. |
|
1114 |
" |
|
1115 |
! |
|
1116 |
||
1117 |
documentation |
|
1118 |
" |
|
1119 |
ExternalStream defines protocol common to Streams which have a file-descriptor and |
|
1120 |
represent some file or communicationChannel of the underlying OperatingSystem. |
|
1121 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc. |
|
1122 |
||
1123 |
ExternalStreams can be in two modes: text- (the default) and binary-mode. |
|
1124 |
In text-mode, the elements read/written are characters; |
|
1125 |
while in binary-mode the basic elements are bytes which read/write as SmallIntegers |
|
1126 |
in the range 0..255. |
|
1127 |
||
1128 |
Also, the stream can be either in buffered or unbuffered mode. In buffered mode, |
|
1129 |
data is not written until either a cr is written (in text mode) or a synchronizeOutput |
|
1130 |
is sent (in both modes). |
|
1131 |
||
1132 |
The underlying OperatingSystem streams may either be closed explicitely (sending a close) |
|
1133 |
or just forgotten - in this case, the garbage collector will eventually collect the |
|
1134 |
object AND a close will be performed automatically (but you will NOT know when this |
|
1135 |
happens - so it is recommended, that you close your files when no longer needed). |
|
1136 |
Closing is also suggested, since if smalltalk is finished (be it by purpose, or due to |
|
1137 |
some crash) the data will not be in the file, if unclosed. |
|
1138 |
All streams understand the close message, so it never hurts to use it (it is defined as |
|
1139 |
a noop in one of the superclasses). |
|
1140 |
||
1141 |
Most of the methods found here redefine inherited methods for better performance, |
|
1142 |
since I/O from/to files should be fast. |
|
1143 |
||
1144 |
Recovering a snapshot: |
|
1145 |
not all streams can be restored to the state they had before - see the implementation of |
|
1146 |
reOpen in subclasses for more information. |
|
1147 |
For streams sitting on some communication channel (i.e. Pipes and Sockets) you should |
|
1148 |
reestablish the stream upon image restart (make someone dependent on ObjectMemory). |
|
1149 |
FileStreams are reopened and positioned to their offset they had at snapshot time. |
|
1150 |
This may fail, if the file was removed or renamed - or lead to confusion |
|
1151 |
if the contents changed in the meantime. |
|
1152 |
Therefore, it is a good idea to reopen files and check for these things at restart time. |
|
1153 |
||
1154 |
[Instance variables:] |
|
1155 |
||
1156 |
filePointer <Integer> the unix FILE*; somehow mapped to an integer |
|
1157 |
(notice: not the fd) |
|
1158 |
on Windows: the fileHandle |
|
1159 |
mode <Symbol> #readwrite, #readonly or #writeonly |
|
1160 |
buffered <Boolean> true, if buffered (i.e. collects characters - does |
|
1161 |
not output immediately) |
|
1162 |
binary <Boolean> true if in binary mode (reads bytes instead of chars) |
|
1163 |
eolMode <Symbol> one of nil, #cr or #crlf. |
|
1164 |
determines how lines should be terminated. |
|
1165 |
nil -> newLine (as in Unix); |
|
1166 |
#crlf -> with cr-lf (as in MSDOS) |
|
1167 |
#cr -> with cr (as in VMS) |
|
1168 |
hitEOF <Boolean> true, if EOF was reached |
|
1169 |
||
1170 |
lastErrorNumber <Integer> the value of errno (only valid right after the error - |
|
1171 |
updated with next i/o operation) |
|
1172 |
||
1173 |
[Class variables:] |
|
1174 |
Lobby <Registry> keeps track of used ext-streams (to free up FILE*'s) |
|
1175 |
||
1176 |
StreamErrorSignal <Signal> parent of all stream errors (see Stream class) |
|
1177 |
InvalidReadSignal <Signal> raised on read from writeonly stream |
|
1178 |
InvalidWriteSignal <Signal> raised on write to readonly stream |
|
1179 |
InvalidModeSignal <Signal> raised on text I/O with binary-stream |
|
1180 |
or binary I/O with text-stream |
|
1181 |
OpenErrorSignal <Signal> raised if open fails |
|
1182 |
StreamNotOpenSignal <Signal> raised on I/O with non-open stream |
|
1183 |
||
1184 |
Additional notes: |
|
1185 |
This class is implemented using the underlying stdio-c library package, which |
|
1186 |
has both advantages and disadvantages: since it is portable (posix defined), porting |
|
1187 |
ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library |
|
1188 |
has big problems handling unbounded Streams, since the EOF handling in stdio is |
|
1189 |
not prepared for data to arrive after EOF has been reached - time will show, if we need |
|
1190 |
a complete rewrite for UnboundedStream ... |
|
1191 |
||
1192 |
Also, depending on the system, the stdio library behaves infriendly when signals |
|
1193 |
occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal |
|
1194 |
is handled transparently - on SYS5.3 (i.e. non unixes :-) the read operation returns |
|
1195 |
an error and errno is set to EINTR. |
|
1196 |
Thats what the ugly code around all getc-calls is for ... |
|
1197 |
Since things get more and more ugly - we will rewrite ExternalStream |
|
1198 |
completely, to NOT use any stdio stuff (and do its buffering itself). |
|
1199 |
||
1200 |
Notice that typical stdio's use a single errno global variable to return an error code, |
|
1201 |
this was bad design in the stdio lib (right from the very beginning), since its much |
|
1202 |
harder to deal with this in the presence of lightweight processes, where errno gets |
|
1203 |
overwritten by an I/O operation done in another thread. (stdio should have been written |
|
1204 |
to return errno as a negative number ...). |
|
1205 |
To deal with this, the scheduler treats errno like a per-thread private variable, |
|
1206 |
and saves/restores the errno setting when switching to another thread. |
|
1207 |
(Notice that some thread packages do this also, but ST/X's thread implementation |
|
1208 |
does not depend on those, but instead uses a portable private package). |
|
1209 |
||
1210 |
Finally, if an stdio-stream is open for both reading and writing, we have to call |
|
1211 |
fseek whenever we are about to read after write and vice versa. |
|
1212 |
Two macros (__READING__ and __WRITING__) have been defined to be used before every |
|
1213 |
fread/fgetc and fwrite/putc respectively. |
|
1214 |
||
1215 |
[author:] |
|
1216 |
Claus Gittinger |
|
1217 |
Stefan Vogel (many, many fixes ...) |
|
4362 | 1218 |
Manfred Dierolf (dos port) |
3746 | 1219 |
|
1220 |
[see also:] |
|
1221 |
FileStream Socket PipeStream |
|
1222 |
Filename OperatingSystem |
|
1223 |
" |
|
1224 |
! |
|
1225 |
||
1226 |
examples |
|
1227 |
" |
|
1228 |
open a file, read the contents and display it in a textView: |
|
1229 |
||
1230 |
|topView scrollPane textView fileStream text| |
|
1231 |
||
1232 |
topView := StandardSystemView new. |
|
1233 |
topView label:'contents of Makefile'. |
|
1234 |
||
1235 |
scrollPane := HVScrollableView in:topView. |
|
1236 |
scrollPane origin:0.0@0.0 corner:1.0@1.0. |
|
4362 | 1237 |
|
3746 | 1238 |
textView := EditTextView new. |
1239 |
scrollPane scrolledView:textView. |
|
1240 |
||
1241 |
fileStream := 'Makefile' asFilename readStream. |
|
1242 |
text := fileStream upToEnd. |
|
1243 |
fileStream close. |
|
1244 |
||
1245 |
textView contents:text. |
|
1246 |
||
1247 |
topView open. |
|
1248 |
||
1249 |
||
1250 |
||
1251 |
Notice, all of the above can also be done (simply) as: |
|
1252 |
||
1253 |
EditTextView openOn:'Makefile' |
|
1254 |
" |
|
1255 |
! ! |
|
1256 |
||
1257 |
!ExternalStream class methodsFor:'initialization'! |
|
1258 |
||
1259 |
initDefaultEOLMode |
|
1260 |
OperatingSystem isUNIXlike ifTrue:[ |
|
1261 |
"/ unix EOL conventions |
|
1262 |
DefaultEOLMode := #nl |
|
1263 |
] ifFalse:[ |
|
1264 |
OperatingSystem isVMSlike ifTrue:[ |
|
1265 |
"/ vms EOL conventions |
|
1266 |
DefaultEOLMode := #cr |
|
1267 |
] ifFalse:[ |
|
1268 |
"/ msdos EOL conventions |
|
1269 |
DefaultEOLMode := #crlf |
|
1270 |
] |
|
1271 |
] |
|
1272 |
! |
|
1273 |
||
1274 |
initModeStrings |
|
1275 |
"initialize modeStrings which are passed down to the underlying |
|
1276 |
fopen/fdopen functions." |
|
1277 |
||
1278 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
1279 |
ReadMode := 'rb'. |
|
1280 |
ReadWriteMode := 'rb+'. |
|
1281 |
WriteMode := 'wb'. |
|
1282 |
AppendMode := 'ab+'. |
|
1283 |
CreateReadWriteMode := 'wb+'. |
|
1284 |
] ifFalse:[ |
|
1285 |
ReadMode := 'r'. |
|
1286 |
ReadWriteMode := 'r+'. |
|
1287 |
WriteMode := 'w'. |
|
1288 |
AppendMode := 'a+'. |
|
1289 |
CreateReadWriteMode := 'w+'. |
|
1290 |
] |
|
1291 |
! |
|
1292 |
||
1293 |
initialize |
|
1294 |
OpenErrorSignal isNil ifTrue:[ |
|
1295 |
OpenErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
|
1296 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
1297 |
OpenErrorSignal notifierString:'open error'. |
|
1298 |
||
1299 |
InvalidReadSignal := ReadErrorSignal newSignalMayProceed:false. |
|
1300 |
InvalidReadSignal nameClass:self message:#invalidReadSignal. |
|
1301 |
InvalidReadSignal notifierString:'read error'. |
|
1302 |
||
1303 |
InvalidWriteSignal := WriteErrorSignal newSignalMayProceed:false. |
|
1304 |
InvalidWriteSignal nameClass:self message:#invalidWriteSignal. |
|
1305 |
InvalidWriteSignal notifierString:'write error'. |
|
1306 |
||
1307 |
InvalidModeSignal := StreamErrorSignal newSignalMayProceed:false. |
|
1308 |
InvalidModeSignal nameClass:self message:#invalidModeSignal. |
|
1309 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
|
1310 |
||
1311 |
InvalidOperationSignal := StreamErrorSignal newSignalMayProceed:false. |
|
1312 |
InvalidOperationSignal nameClass:self message:#invalidOperationSignal. |
|
1313 |
InvalidOperationSignal notifierString:'unsupported file operation'. |
|
1314 |
||
1315 |
StreamNotOpenSignal := StreamErrorSignal newSignalMayProceed:false. |
|
1316 |
StreamNotOpenSignal nameClass:self message:#streamNotOpenSignal. |
|
1317 |
StreamNotOpenSignal notifierString:'stream is not open'. |
|
1318 |
||
1319 |
StreamIOErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
|
1320 |
StreamIOErrorSignal nameClass:self message:#streamIOErrorSignal. |
|
1321 |
StreamIOErrorSignal notifierString:'I/O error'. |
|
1322 |
]. |
|
1323 |
||
1324 |
Lobby isNil ifTrue:[ |
|
1325 |
Lobby := Registry new. |
|
1326 |
||
1327 |
"want to get informed when returning from snapshot" |
|
1328 |
ObjectMemory addDependent:self |
|
1329 |
]. |
|
1330 |
self initDefaultEOLMode. |
|
1331 |
self initModeStrings. |
|
1332 |
||
1333 |
"Modified: / 21.5.1998 / 16:33:53 / cg" |
|
1334 |
! |
|
1335 |
||
1336 |
reOpenFiles |
|
1337 |
"reopen all files (if possible) after a snapShot load" |
|
1338 |
||
1339 |
Lobby do:[:aFileStream | |
|
1340 |
aFileStream reOpen |
|
1341 |
]. |
|
1342 |
! |
|
1343 |
||
1344 |
update:something with:aParameter from:changedObject |
|
1345 |
"have to reopen files when returning from snapshot" |
|
1346 |
||
1347 |
something == #returnFromSnapshot ifTrue:[ |
|
1348 |
self reOpenFiles. |
|
1349 |
self initDefaultEOLMode |
|
1350 |
] |
|
1351 |
||
1352 |
"Created: 15.6.1996 / 15:19:59 / cg" |
|
1353 |
! ! |
|
1354 |
||
1355 |
!ExternalStream class methodsFor:'instance creation'! |
|
1356 |
||
1357 |
forFileDescriptor:aFileDescriptor mode:modeString |
|
1358 |
"given a fileDescriptor, create an ExternalStream object |
|
1359 |
to operate on this fd. |
|
1360 |
The mode-argument is passed to the fdopen call. |
|
1361 |
This may be used to wrap fd's as returned by user |
|
1362 |
primitive code, or to wrap pipe-fds into externalStreams." |
|
1363 |
||
1364 |
|newStream| |
|
1365 |
||
1366 |
newStream := self basicNew. |
|
1367 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
|
1368 |
^ newStream connectTo:aFileDescriptor withMode:modeString |
|
1369 |
||
1370 |
" |
|
1371 |
the example below will probably fail (15 is a random FD): |
|
1372 |
||
1373 |
|s| |
|
1374 |
||
1375 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
|
1376 |
s next. |
|
1377 |
" |
|
1378 |
||
1379 |
"Created: 29.2.1996 / 18:05:00 / cg" |
|
1380 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
|
1381 |
! |
|
1382 |
||
1383 |
forReadWriteToFileDescriptor:aFileDescriptor |
|
1384 |
"given a fileDescriptor, create an ExternalStream object |
|
1385 |
to read/write from/to this fd. This may be used to wrap fd's |
|
1386 |
as returned by user primitive code, or to wrap pipe- |
|
1387 |
filedescriptors into externalStreams." |
|
1388 |
||
1389 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadWriteMode) readwrite |
|
1390 |
||
1391 |
" |
|
1392 |
the example below will probably fail (15 is a random FD): |
|
1393 |
||
1394 |
|s| |
|
1395 |
||
1396 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
|
1397 |
s next. |
|
1398 |
" |
|
1399 |
||
1400 |
"Created: 29.2.1996 / 18:15:08 / cg" |
|
1401 |
"Modified: 29.2.1996 / 18:16:25 / cg" |
|
1402 |
! |
|
1403 |
||
1404 |
forReadingFromFileDescriptor:aFileDescriptor |
|
1405 |
"given a fileDescriptor, create an ExternalStream object |
|
1406 |
to read from this fd. This may be used to wrap fd's |
|
1407 |
as returned by user primitive code, or to wrap pipe- |
|
1408 |
filedescriptors into externalStreams." |
|
1409 |
||
1410 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadMode) readonly |
|
1411 |
||
1412 |
" |
|
1413 |
the example below will probably fail (15 is a random FD): |
|
1414 |
||
1415 |
|s| |
|
1416 |
||
1417 |
s := ExternalStream forReadingFromFileDescriptor:15. |
|
1418 |
s next. |
|
1419 |
" |
|
1420 |
||
1421 |
" |
|
1422 |
|pipe readFd writeFd rs ws| |
|
1423 |
||
1424 |
'create OS pipe ...'. |
|
1425 |
||
1426 |
pipe := OperatingSystem makePipe. |
|
1427 |
readFd := pipe at:1. |
|
1428 |
writeFd := pipe at:2. |
|
1429 |
||
1430 |
'connect Smalltalk streams ...'. |
|
1431 |
||
1432 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
|
1433 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
|
1434 |
||
1435 |
'read ...'. |
|
1436 |
[ |
|
1437 |
1 to:10 do:[:i | |
|
1438 |
Transcript showCR:rs nextLine |
|
1439 |
]. |
|
1440 |
rs close. |
|
1441 |
] forkAt:7. |
|
1442 |
||
1443 |
'write ...'. |
|
1444 |
[ |
|
1445 |
1 to:10 do:[:i | |
|
1446 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1447 |
]. |
|
1448 |
ws close. |
|
1449 |
] fork. |
|
1450 |
||
1451 |
" |
|
1452 |
||
1453 |
"Created: 29.2.1996 / 18:14:24 / cg" |
|
1454 |
"Modified: 29.2.1996 / 18:25:02 / cg" |
|
1455 |
! |
|
1456 |
||
1457 |
forWritingToFileDescriptor:aFileDescriptor |
|
1458 |
"given a fileDescriptor, create an ExternalStream object |
|
1459 |
to write to this fd. This may be used to wrap fd's |
|
1460 |
as returned by user primitive code, or to wrap pipe- |
|
1461 |
filedescriptors into externalStreams." |
|
1462 |
||
1463 |
^ (self forFileDescriptor:aFileDescriptor mode:WriteMode) writeonly |
|
1464 |
||
1465 |
" |
|
1466 |
the example below will probably fail (15 is a random FD): |
|
1467 |
||
1468 |
|s| |
|
1469 |
||
1470 |
s := ExternalStream forWritingToFileDescriptor:15. |
|
1471 |
s binary. |
|
1472 |
s nextPut:1. |
|
1473 |
" |
|
1474 |
||
1475 |
"Created: 29.2.1996 / 18:14:43 / cg" |
|
1476 |
"Modified: 29.2.1996 / 18:15:54 / cg" |
|
1477 |
! |
|
1478 |
||
1479 |
makePTYPair |
|
1480 |
"return an array with two streams - the first one is the master, |
|
1481 |
the second the slave of a ptym/ptys pair. |
|
1482 |
This is much like a bidirectional pipe, but allows signals & |
|
1483 |
control chars to be passed through the connection. |
|
1484 |
This is needed to execute a shell in a view. |
|
1485 |
This is the higher level equivalent of OperatingSystem>>makePTYPair |
|
1486 |
(which returns an array of file-descriptors)." |
|
1487 |
||
1488 |
|ptyPair m s| |
|
1489 |
||
1490 |
ptyPair := OperatingSystem makePTYPair. |
|
1491 |
ptyPair notNil ifTrue:[ |
|
1492 |
m := self forReadWriteToFileDescriptor:(ptyPair at:1). |
|
1493 |
s := self forReadWriteToFileDescriptor:(ptyPair at:2). |
|
1494 |
ptyPair at:1 put:m. |
|
1495 |
ptyPair at:2 put:s. |
|
1496 |
^ ptyPair |
|
1497 |
]. |
|
1498 |
^ nil |
|
1499 |
||
1500 |
" |
|
1501 |
ExternalStream makePTYPair. |
|
1502 |
" |
|
1503 |
||
1504 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
|
1505 |
! |
|
1506 |
||
1507 |
makePipe |
|
1508 |
"return an array with two streams - the first one for reading, |
|
1509 |
the second for writing. |
|
1510 |
This is the higher level equivalent of OperatingSystem>>makePipe |
|
1511 |
(which returns an array of file-descriptors)." |
|
1512 |
||
1513 |
|pipe rs ws| |
|
1514 |
||
1515 |
pipe := OperatingSystem makePipe. |
|
1516 |
||
1517 |
pipe notNil ifTrue:[ |
|
1518 |
rs := self forReadingFromFileDescriptor:(pipe at:1). |
|
1519 |
ws := self forWritingToFileDescriptor:(pipe at:2). |
|
1520 |
^ Array with:rs with:ws |
|
1521 |
]. |
|
1522 |
^ nil |
|
1523 |
||
1524 |
" |
|
1525 |
|pipe rs ws| |
|
1526 |
||
1527 |
pipe := ExternalStream makePipe. |
|
1528 |
rs := pipe at:1. |
|
1529 |
ws := pipe at:2. |
|
1530 |
||
1531 |
'read ...'. |
|
1532 |
[ |
|
1533 |
1 to:10 do:[:i | |
|
1534 |
Transcript showCR:rs nextLine |
|
1535 |
]. |
|
1536 |
rs close. |
|
1537 |
] forkAt:7. |
|
1538 |
||
1539 |
'write ...'. |
|
1540 |
[ |
|
1541 |
1 to:10 do:[:i | |
|
1542 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1543 |
]. |
|
1544 |
ws close. |
|
1545 |
] fork. |
|
1546 |
" |
|
1547 |
||
1548 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
|
1549 |
! |
|
1550 |
||
1551 |
new |
|
1552 |
|newStream| |
|
1553 |
||
1554 |
newStream := self basicNew. |
|
1555 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
|
1556 |
^ newStream |
|
1557 |
! ! |
|
1558 |
||
1559 |
!ExternalStream class methodsFor:'Signal constants'! |
|
1560 |
||
1561 |
inaccessibleSignal |
|
1562 |
"ST-80 compatibility: return openErrorSignal" |
|
1563 |
||
1564 |
^ self openErrorSignal |
|
1565 |
||
1566 |
"Created: 2.7.1996 / 12:27:16 / stefan" |
|
1567 |
! |
|
1568 |
||
1569 |
invalidModeSignal |
|
1570 |
"return the signal raised when doing text-I/O with a binary stream |
|
1571 |
or binary-I/O with a text stream" |
|
1572 |
||
1573 |
^ InvalidModeSignal |
|
1574 |
! |
|
1575 |
||
1576 |
invalidOperationSignal |
|
1577 |
"return the signal raised when an unsupported or invalid |
|
1578 |
I/O operation is attempted" |
|
1579 |
||
1580 |
^ InvalidOperationSignal |
|
1581 |
! |
|
1582 |
||
1583 |
invalidReadSignal |
|
1584 |
"return the signal raised when reading from writeonly streams" |
|
1585 |
||
1586 |
^ InvalidReadSignal |
|
1587 |
! |
|
1588 |
||
1589 |
invalidWriteSignal |
|
1590 |
"return the signal raised when writing to readonly streams" |
|
1591 |
||
1592 |
^ InvalidWriteSignal |
|
1593 |
! |
|
1594 |
||
1595 |
openErrorSignal |
|
1596 |
"return the signal raised when a file open failed" |
|
1597 |
||
1598 |
^ OpenErrorSignal |
|
1599 |
! |
|
1600 |
||
1601 |
streamIOErrorSignal |
|
1602 |
"return the signal raised when an I/O error occurs. |
|
1603 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
1604 |
which is no longer available and has been mounted soft)" |
|
1605 |
||
1606 |
^ StreamIOErrorSignal |
|
1607 |
||
1608 |
"Created: / 21.5.1998 / 16:32:55 / cg" |
|
1609 |
! |
|
1610 |
||
1611 |
streamNotOpenSignal |
|
1612 |
"return the signal raised on I/O with closed streams" |
|
1613 |
||
1614 |
^ StreamNotOpenSignal |
|
1615 |
! ! |
|
1616 |
||
1617 |
!ExternalStream class methodsFor:'error handling'! |
|
1618 |
||
1619 |
lastErrorNumber |
|
1620 |
"return the errno of the last error" |
|
1621 |
||
1622 |
^ LastErrorNumber |
|
1623 |
||
1624 |
" |
|
1625 |
ExternalStream lastErrorNumber |
|
1626 |
" |
|
1627 |
! |
|
1628 |
||
1629 |
lastErrorString |
|
1630 |
"return a message string describing the last error" |
|
1631 |
||
1632 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
1633 |
||
1634 |
" |
|
1635 |
ExternalStream lastErrorString |
|
1636 |
" |
|
1637 |
! ! |
|
1638 |
||
1639 |
!ExternalStream methodsFor:'Squeak compatibility'! |
|
1640 |
||
1641 |
readOnly |
|
1642 |
"Squeak compatibility: make the stream readOnly" |
|
1643 |
||
1644 |
mode := #readonly |
|
1645 |
||
1646 |
"Modified: 20.10.1997 / 19:23:04 / cg" |
|
1647 |
"Created: 20.10.1997 / 19:23:19 / cg" |
|
1648 |
! ! |
|
1649 |
||
1650 |
!ExternalStream methodsFor:'accessing'! |
|
1651 |
||
1652 |
binary |
|
1653 |
"switch to binary mode - default is text" |
|
1654 |
||
1655 |
binary := true |
|
1656 |
! |
|
1657 |
||
1658 |
buffered:aBoolean |
|
1659 |
"turn buffering on or off - default is on" |
|
1660 |
||
1661 |
buffered := aBoolean |
|
1662 |
! |
|
1663 |
||
1664 |
contents |
|
1665 |
"return the contents of the file from the current position up-to |
|
1666 |
the end. If the stream is in binary mode, a ByteArray containing |
|
1667 |
the byte values is returned. |
|
1668 |
In text-mode, a collection of strings, each representing one line, |
|
1669 |
is returned." |
|
1670 |
||
1671 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
1672 |
||
1673 |
binary ifTrue:[ |
|
1674 |
"adding to a ByteArray produces quadratic time-space |
|
1675 |
behavior - therefore we allocate chunks, and concatenate them |
|
1676 |
at the end." |
|
1677 |
||
1678 |
chunks := OrderedCollection new. |
|
1679 |
sizes := OrderedCollection new. |
|
1680 |
byteCount := 0. |
|
1681 |
[self atEnd] whileFalse:[ |
|
1682 |
chunk := ByteArray uninitializedNew:4096. |
|
1683 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
1684 |
cnt notNil ifTrue:[ |
|
1685 |
chunks add:chunk. |
|
1686 |
sizes add:cnt. |
|
1687 |
byteCount := byteCount + cnt |
|
1688 |
] |
|
1689 |
]. |
|
1690 |
||
1691 |
"now, create one big ByteArray" |
|
1692 |
bytes := ByteArray uninitializedNew:byteCount. |
|
1693 |
offset := 1. |
|
1694 |
1 to:chunks size do:[:index | |
|
1695 |
chunk := chunks at:index. |
|
1696 |
cnt := sizes at:index. |
|
1697 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
1698 |
offset := offset + cnt |
|
1699 |
]. |
|
1700 |
^ bytes |
|
1701 |
]. |
|
1702 |
||
1703 |
text := StringCollection new. |
|
1704 |
[self atEnd] whileFalse:[ |
|
1705 |
l := self nextLine. |
|
1706 |
l isNil ifTrue:[ |
|
1707 |
^ text |
|
1708 |
]. |
|
1709 |
text add:l |
|
1710 |
]. |
|
1711 |
^ text |
|
1712 |
! |
|
1713 |
||
1714 |
contentsOfEntireFile |
|
1715 |
"ST-80 compatibility: return contents as String" |
|
1716 |
||
1717 |
^ self contents asString. |
|
1718 |
||
1719 |
"Modified: 3.7.1996 / 13:22:16 / stefan" |
|
1720 |
! |
|
1721 |
||
1722 |
contentsSpecies |
|
1723 |
"return the kind of object to be returned by sub-collection builders |
|
1724 |
(such as upTo)" |
|
1725 |
||
1726 |
binary ifTrue:[ |
|
1727 |
^ ByteArray |
|
1728 |
]. |
|
1729 |
^ String |
|
1730 |
! |
|
1731 |
||
1732 |
eolMode:aSymbolOrNil |
|
1733 |
"specify how end-of-line (EOL) is to be marked. |
|
1734 |
The argument may be one of: |
|
1735 |
#crlf -> add a CR-NL, as in MSDOS |
|
1736 |
#cr -> add a CR, as in VMS |
|
1737 |
#nl -> add a NL, as in Unix |
|
1738 |
anyOther -> like #nl |
|
1739 |
" |
|
1740 |
||
1741 |
eolMode := aSymbolOrNil |
|
1742 |
! |
|
1743 |
||
1744 |
fileDescriptor |
|
1745 |
"return the fileDescriptor of the receiver - |
|
1746 |
notice: this one returns the underlying OSs fileDescriptor - |
|
1747 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1748 |
||
1749 |
%{ /* NOCONTEXT */ |
|
1750 |
OBJ fp; |
|
1751 |
||
1752 |
if ((fp = __INST(filePointer)) != nil) { |
|
4206 | 1753 |
FILEPOINTER f; |
1754 |
||
3746 | 1755 |
f = __FILEVal(fp); |
1756 |
RETURN ( __MKINT(fileno(f)) ); |
|
1757 |
} |
|
1758 |
%}. |
|
1759 |
^ self errorNotOpen |
|
1760 |
! |
|
1761 |
||
1762 |
filePointer |
|
1763 |
"return the filePointer of the receiver - |
|
1764 |
notice: for portability stdio is used; this means you will get |
|
1765 |
a FILE * - not a fileDescriptor. |
|
1766 |
(what you really get is a corresponding integer). |
|
1767 |
You cannot do much with the returned value |
|
1768 |
- except passing it to a primitive, for example." |
|
1769 |
||
1770 |
^ filePointer |
|
1771 |
! |
|
1772 |
||
1773 |
lineEndTransparent |
|
1774 |
eolMode := #cr |
|
1775 |
! |
|
1776 |
||
1777 |
readonly |
|
1778 |
"set access mode to readonly" |
|
1779 |
||
1780 |
mode := #readonly |
|
1781 |
! |
|
1782 |
||
1783 |
readwrite |
|
1784 |
"set access mode to readwrite" |
|
1785 |
||
1786 |
mode := #readwrite |
|
1787 |
! |
|
1788 |
||
1789 |
text |
|
1790 |
"switch to text mode - default is text" |
|
1791 |
||
1792 |
binary := false |
|
1793 |
! |
|
1794 |
||
1795 |
useCRLF:aBoolean |
|
1796 |
"turn on or off CRLF sending (instead of LF only) - default is off. |
|
1797 |
This method is provided for backward compatibility - see #eolMode: |
|
1798 |
which offers another choice." |
|
1799 |
||
1800 |
aBoolean ifTrue:[ |
|
1801 |
eolMode := #crlf |
|
1802 |
] ifFalse:[ |
|
1803 |
eolMode := #nl |
|
1804 |
]. |
|
1805 |
! |
|
1806 |
||
1807 |
writeonly |
|
1808 |
"set access mode to writeonly" |
|
1809 |
||
1810 |
mode := #writeonly |
|
1811 |
! ! |
|
1812 |
||
1813 |
!ExternalStream methodsFor:'error handling'! |
|
1814 |
||
1815 |
argumentMustBeCharacter |
|
1816 |
"{ Pragma: +optSpace }" |
|
1817 |
||
1818 |
"report an error, that the argument must be a character" |
|
1819 |
||
1820 |
^ self error:'argument must be a character' |
|
1821 |
! |
|
1822 |
||
1823 |
argumentMustBeInteger |
|
1824 |
"{ Pragma: +optSpace }" |
|
1825 |
||
1826 |
"report an error, that the argument must be an integer" |
|
1827 |
||
1828 |
^ self error:'argument must be an integer' |
|
1829 |
! |
|
1830 |
||
1831 |
argumentMustBeString |
|
1832 |
"{ Pragma: +optSpace }" |
|
1833 |
||
1834 |
"report an error, that the argument must be a string" |
|
1835 |
||
1836 |
^ self error:'argument must be a string' |
|
1837 |
! |
|
1838 |
||
1839 |
errorBinary |
|
1840 |
"{ Pragma: +optSpace }" |
|
1841 |
||
1842 |
"report an error, that the stream is in binary mode" |
|
1843 |
||
1844 |
^ self invalidModeSignal |
|
1845 |
raiseRequestWith:self |
|
1846 |
errorString:(self class name , ' is in binary mode') |
|
1847 |
in:thisContext sender |
|
1848 |
! |
|
1849 |
||
1850 |
errorNotBinary |
|
1851 |
"{ Pragma: +optSpace }" |
|
1852 |
||
1853 |
"report an error, that the stream is not in binary mode" |
|
1854 |
||
1855 |
^ self invalidModeSignal |
|
1856 |
raiseRequestWith:self |
|
1857 |
errorString:(self class name , ' is not in binary mode') |
|
1858 |
in:thisContext sender |
|
1859 |
! |
|
1860 |
||
1861 |
errorNotBuffered |
|
1862 |
"{ Pragma: +optSpace }" |
|
1863 |
||
1864 |
"report an error, that the stream is not in buffered mode" |
|
1865 |
||
1866 |
^ StreamErrorSignal |
|
1867 |
raiseRequestWith:self |
|
1868 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
1869 |
in:thisContext sender |
|
1870 |
! |
|
1871 |
||
1872 |
errorNotOpen |
|
1873 |
"{ Pragma: +optSpace }" |
|
1874 |
||
1875 |
"report an error, that the stream has not been opened" |
|
1876 |
||
1877 |
^ StreamNotOpenSignal |
|
1878 |
raiseRequestWith:self |
|
1879 |
errorString:(self class name , ' not open') |
|
1880 |
in:thisContext sender |
|
1881 |
||
1882 |
"Modified: / 19.5.1998 / 22:07:52 / cg" |
|
1883 |
! |
|
1884 |
||
1885 |
errorOpen |
|
1886 |
"{ Pragma: +optSpace }" |
|
1887 |
||
1888 |
"report an error, that the stream is already opened" |
|
1889 |
||
1890 |
^ self openErrorSignal |
|
1891 |
raiseRequestWith:self |
|
1892 |
errorString:(self class name , ' is already open') |
|
1893 |
in:thisContext sender |
|
1894 |
! |
|
1895 |
||
1896 |
errorReadOnly |
|
1897 |
"{ Pragma: +optSpace }" |
|
1898 |
||
1899 |
"report an error, that the stream is a readOnly stream" |
|
1900 |
||
1901 |
^ self invalidWriteSignal |
|
1902 |
raiseRequestWith:self |
|
1903 |
errorString:(self class name , ' is readonly') |
|
1904 |
in:thisContext sender |
|
1905 |
! |
|
1906 |
||
1907 |
errorUnsupportedOperation |
|
1908 |
"{ Pragma: +optSpace }" |
|
1909 |
||
1910 |
"report an error, that some unsupported operation was attempted" |
|
1911 |
||
1912 |
^ self invalidOperationSignal |
|
1913 |
raiseRequestWith:self |
|
1914 |
errorString:'unsupported operation' |
|
1915 |
in:thisContext sender |
|
1916 |
! |
|
1917 |
||
1918 |
errorWriteOnly |
|
1919 |
"{ Pragma: +optSpace }" |
|
1920 |
||
1921 |
"report an error, that the stream is a writeOnly stream" |
|
1922 |
||
1923 |
^ self invalidReadSignal |
|
1924 |
raiseRequestWith:self |
|
1925 |
errorString:(self class name , ' is writeonly') |
|
1926 |
in:thisContext sender |
|
1927 |
! |
|
1928 |
||
1929 |
ioError |
|
1930 |
"{ Pragma: +optSpace }" |
|
1931 |
||
1932 |
"report an error, that some I/O error occured. |
|
1933 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
1934 |
which is no longer available and has been mounted soft)" |
|
1935 |
||
1936 |
^ StreamIOErrorSignal |
|
1937 |
raiseRequestWith:self |
|
1938 |
errorString:('I/O error: ' , self lastErrorString) |
|
1939 |
in:thisContext sender |
|
1940 |
||
1941 |
"Modified: / 21.5.1998 / 16:33:13 / cg" |
|
1942 |
! |
|
1943 |
||
1944 |
lastErrorNumber |
|
1945 |
"return the last error" |
|
1946 |
||
1947 |
^ lastErrorNumber |
|
1948 |
! |
|
1949 |
||
1950 |
lastErrorString |
|
1951 |
"return a message string describing the last error" |
|
1952 |
||
1953 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
1954 |
^ 'I/O error' |
|
1955 |
]. |
|
1956 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
1957 |
! |
|
1958 |
||
1959 |
openError |
|
1960 |
"{ Pragma: +optSpace }" |
|
1961 |
||
1962 |
"report an error, that the open failed" |
|
1963 |
||
1964 |
^ self class openErrorSignal |
|
1965 |
raiseRequestWith:self |
|
1966 |
errorString:('error on open: ' , self lastErrorString) |
|
1967 |
in:thisContext sender |
|
1968 |
||
1969 |
"Modified: / 28.1.1998 / 14:37:42 / stefan" |
|
1970 |
! |
|
1971 |
||
1972 |
readError |
|
1973 |
"{ Pragma: +optSpace }" |
|
1974 |
||
1975 |
"report an error, that some read error occured" |
|
1976 |
||
1977 |
^ ReadErrorSignal |
|
1978 |
raiseRequestWith:self |
|
1979 |
errorString:('read error: ' , self lastErrorString) |
|
1980 |
in:thisContext sender |
|
1981 |
! |
|
1982 |
||
1983 |
writeError |
|
1984 |
"{ Pragma: +optSpace }" |
|
1985 |
||
1986 |
"report an error, that some write error occured" |
|
1987 |
||
1988 |
^ WriteErrorSignal |
|
1989 |
raiseRequestWith:self |
|
1990 |
errorString:('write error: ' , self lastErrorString) |
|
1991 |
in:thisContext sender |
|
1992 |
! ! |
|
1993 |
||
1994 |
!ExternalStream methodsFor:'instance release'! |
|
1995 |
||
1996 |
closeFile |
|
1997 |
"low level close - may be redefined in subclasses" |
|
1998 |
||
1999 |
%{ |
|
2000 |
OBJ fp; |
|
2001 |
||
2002 |
if ((fp = __INST(filePointer)) != nil) { |
|
4206 | 2003 |
FILEPOINTER f; |
2004 |
||
3746 | 2005 |
__INST(filePointer) = nil; |
4206 | 2006 |
f = __FILEVal(fp); |
2007 |
||
3746 | 2008 |
#ifdef NO_STDIO |
4206 | 2009 |
__STX_C_CALL1((void*)fclose, (void*)f); |
3746 | 2010 |
#else |
4206 | 2011 |
# if 0 |
3746 | 2012 |
__BEGIN_INTERRUPTABLE__ |
4206 | 2013 |
# endif |
2014 |
__STX_C_CALL1((void*)fclose, (void*)f); |
|
2015 |
# if 0 |
|
3746 | 2016 |
__END_INTERRUPTABLE__ |
4206 | 2017 |
# endif |
3746 | 2018 |
#endif |
2019 |
} |
|
2020 |
%} |
|
2021 |
! |
|
2022 |
||
2023 |
disposed |
|
2024 |
"some Stream has been collected - close the file if not already done" |
|
2025 |
||
2026 |
self closeFile |
|
2027 |
! |
|
2028 |
||
2029 |
setFileDescriptor:anInteger mode:openMode |
|
2030 |
"set the filePointer based upon a given fileDescriptor - |
|
2031 |
notice: this one is based on the underlying OSs fileDescriptor - |
|
2032 |
this may not be available on all platforms (i.e. non unix systems)." |
|
2033 |
||
2034 |
%{ /* NOCONTEXT */ |
|
2035 |
||
2036 |
FILEPOINTER f; |
|
2037 |
OBJ fp; |
|
2038 |
FILE *fdopen(); |
|
2039 |
||
2040 |
if (__isSmallInteger(anInteger) && |
|
2041 |
__isString(openMode) && |
|
2042 |
(f = fdopen(__intVal(anInteger), __stringVal(openMode))) != 0 |
|
2043 |
) { |
|
2044 |
__PROTECT__(self); |
|
2045 |
fp = __MKFILEPOINTER(f); |
|
2046 |
__UNPROTECT__(self); |
|
2047 |
__INST(filePointer) = fp; __STORE(self, fp); |
|
2048 |
RETURN (self); |
|
2049 |
} |
|
2050 |
%}. |
|
2051 |
^ self primitiveFailed |
|
2052 |
! |
|
2053 |
||
2054 |
setFilePointer:something |
|
2055 |
"set the filePointer to the given one; |
|
2056 |
low level private & friend interface; may also be used to connect to some |
|
2057 |
externally provided file handle." |
|
2058 |
||
2059 |
filePointer := something |
|
2060 |
! |
|
2061 |
||
2062 |
shallowCopyForFinalization |
|
2063 |
"return a copy for finalization-registration; |
|
2064 |
since all we need at finalization time is the fileDescriptor, |
|
2065 |
a cheaper copy is possible." |
|
2066 |
||
2067 |
^ self class basicNew setFilePointer:filePointer |
|
2068 |
! |
|
2069 |
||
2070 |
shutDown |
|
2071 |
"close the stream - added for protocol compatibility with PipeStream. |
|
2072 |
see comment there" |
|
2073 |
||
3949 | 2074 |
self isOpen ifTrue:[ |
4206 | 2075 |
Lobby unregister:self. |
2076 |
self closeFile |
|
3949 | 2077 |
] |
3746 | 2078 |
|
2079 |
"Modified: 30.8.1996 / 00:39:21 / cg" |
|
2080 |
! ! |
|
2081 |
||
2082 |
!ExternalStream methodsFor:'line reading/writing'! |
|
2083 |
||
2084 |
nextLine |
|
2085 |
"read the next line (characters up to newline). |
|
2086 |
Return a string containing those characters excluding the newline. |
|
2087 |
If the previous-to-last character is a cr, this is also removed, |
|
2088 |
so its possible to read alien (i.e. ms-dos) text as well. |
|
2089 |
The line must be shorter than 16K characters - otherwise an error is signalled." |
|
2090 |
||
2091 |
|line| |
|
2092 |
||
2093 |
%{ /* STACK:34000 */ |
|
2094 |
||
2095 |
FILEPOINTER f; |
|
2096 |
int len, ret; |
|
2097 |
char buffer[32*1024]; |
|
2098 |
char *rslt, *nextPtr, *limit; |
|
2099 |
int fd, ch; |
|
2100 |
int _buffered; |
|
2101 |
OBJ fp; |
|
2102 |
int lineTooLong = 0; |
|
2103 |
int cutOff = 0; |
|
2104 |
||
2105 |
__INST(lastErrorNumber) = nil; |
|
2106 |
if (((fp = __INST(filePointer)) != nil) |
|
2107 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2108 |
&& (__INST(binary) != true) |
|
2109 |
) { |
|
2110 |
f = __FILEVal(fp); |
|
2111 |
buffer[0] = '\0'; |
|
2112 |
||
2113 |
_buffered = (__INST(buffered) == true); |
|
2114 |
if (_buffered) { |
|
2115 |
__READING__(f); |
|
2116 |
} |
|
2117 |
||
2118 |
rslt = nextPtr = buffer; |
|
2119 |
limit = buffer + sizeof(buffer) - 2; |
|
2120 |
||
2121 |
for (;;) { |
|
2122 |
__READBYTE__(ret, f, nextPtr, _buffered); |
|
2123 |
if (ret <= 0) { |
|
2124 |
if (nextPtr == buffer) |
|
2125 |
rslt = NULL; |
|
2126 |
if (ret == 0) { |
|
2127 |
__INST(hitEOF) = true; |
|
2128 |
break; |
|
2129 |
} else { |
|
2130 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2131 |
goto err; |
|
2132 |
} |
|
2133 |
} |
|
2134 |
||
2135 |
if (*nextPtr == '\n') { |
|
2136 |
cutOff = 1; |
|
2137 |
*nextPtr = '\0'; |
|
2138 |
break; |
|
2139 |
} |
|
2140 |
if (*nextPtr == '\r') { |
|
2141 |
char peekChar; |
|
2142 |
||
2143 |
/* |
|
2144 |
* peek ahead for a newLine ... |
|
2145 |
*/ |
|
2146 |
__READBYTE__(ret, f, &peekChar, _buffered); |
|
2147 |
if (ret <= 0) { |
|
2148 |
cutOff = 1; |
|
2149 |
*nextPtr = '\0'; |
|
2150 |
if (ret == 0) { |
|
2151 |
__INST(hitEOF) = true; |
|
2152 |
break; |
|
2153 |
} |
|
2154 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2155 |
goto err; |
|
2156 |
} |
|
2157 |
||
2158 |
if (peekChar == '\n') { |
|
2159 |
cutOff = 2; |
|
2160 |
*nextPtr = '\0'; |
|
2161 |
break; |
|
2162 |
} |
|
2163 |
||
2164 |
__UNGETC__(peekChar, f, _buffered); |
|
2165 |
||
2166 |
cutOff = 1; |
|
2167 |
*nextPtr = '\0'; |
|
2168 |
break; |
|
2169 |
} |
|
2170 |
||
2171 |
nextPtr++; |
|
2172 |
if (nextPtr >= limit) { |
|
2173 |
*nextPtr = '\0'; |
|
2174 |
lineTooLong = 1; |
|
2175 |
if (@global(InfoPrinting) == true) { |
|
2176 |
fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n"); |
|
2177 |
} |
|
2178 |
break; |
|
2179 |
} |
|
2180 |
} |
|
2181 |
||
2182 |
if (rslt != NULL) { |
|
2183 |
len = nextPtr-buffer; |
|
2184 |
||
2185 |
if (__INST(position) != nil) { |
|
2186 |
int __p; |
|
2187 |
||
2188 |
__p = __intVal(__INST(position)) + len + cutOff; |
|
2189 |
__INST(position) = __MKSMALLINT(__p); |
|
2190 |
} |
|
2191 |
/* remove any EOL character */ |
|
2192 |
if (len != 0) { |
|
2193 |
if (buffer[len-1] == '\n') { |
|
2194 |
buffer[--len] = '\0'; |
|
2195 |
} |
|
2196 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
|
2197 |
buffer[--len] = '\0'; |
|
2198 |
} |
|
2199 |
} |
|
2200 |
line = __MKSTRING_L(buffer, len); |
|
2201 |
if (! lineTooLong) { |
|
2202 |
RETURN ( line ); |
|
2203 |
} |
|
2204 |
} |
|
2205 |
} |
|
2206 |
err: ; |
|
2207 |
%}. |
|
2208 |
line notNil ifTrue:[ |
|
2209 |
"/ the line as read is longer than 32k characters (boy - what a line) |
|
2210 |
"/ The exception could be handled by reading more and returning the |
|
2211 |
"/ concatenation in your exception handler (the receiver and the partial |
|
2212 |
"/ line are passed as parameter) |
|
2213 |
||
4597
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2214 |
LineTooLongErrorSignal isHandled ifTrue:[ |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2215 |
^ LineTooLongErrorSignal |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2216 |
raiseRequestWith:(Array with:self with:line) |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2217 |
errorString:('line too long read error') |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2218 |
]. |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2219 |
^ line , self nextLine |
3746 | 2220 |
]. |
2221 |
||
2222 |
hitEOF ifTrue:[^ self pastEnd]. |
|
2223 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2224 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2225 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2226 |
^ self errorBinary |
|
2227 |
! |
|
2228 |
||
2229 |
nextPutLine:aString |
|
2230 |
"write the characters in aString and append an end-of-Line marker |
|
2231 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
2232 |
||
2233 |
%{ |
|
2234 |
FILEPOINTER f; |
|
2235 |
int len, cnt, len1, _buffered; |
|
2236 |
OBJ pos, fp; |
|
2237 |
char *cp; |
|
2238 |
int o_offs; |
|
2239 |
||
2240 |
__INST(lastErrorNumber) = nil; |
|
2241 |
if (((fp = __INST(filePointer)) != nil) |
|
2242 |
&& (__INST(mode) != @symbol(readonly)) |
|
2243 |
&& (__INST(binary) != true) |
|
2244 |
&& __isString(aString) |
|
2245 |
) { |
|
2246 |
f = __FILEVal(fp); |
|
2247 |
len = __stringSize(aString); |
|
2248 |
||
2249 |
if (_buffered = (__INST(buffered) == true)) { |
|
2250 |
__WRITING__(f) |
|
2251 |
} |
|
2252 |
#ifdef BUGGY |
|
2253 |
__WRITEBYTES__(cnt, f, __stringVal(aString), len, _buffered); |
|
2254 |
#else |
|
2255 |
o_offs = (char *)__stringVal(aString)-(char *)aString; |
|
2256 |
__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered); |
|
2257 |
#endif |
|
2258 |
if (cnt == len) { |
|
2259 |
OBJ mode = __INST(eolMode); |
|
2260 |
||
2261 |
len1 = len; |
|
2262 |
||
2263 |
if (mode == @symbol(cr)) { |
|
2264 |
cp = "\r"; len = 1; |
|
2265 |
} else if (mode == @symbol(crlf)) { |
|
2266 |
cp = "\r\n"; len = 2; |
|
2267 |
} else { |
|
2268 |
cp = "\n"; len = 1; |
|
2269 |
} |
|
2270 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
|
2271 |
||
2272 |
if (cnt > 0) { |
|
2273 |
pos = __INST(position); |
|
2274 |
if (pos != nil) { |
|
2275 |
int __p; |
|
2276 |
||
2277 |
__p = __intVal(pos) + len1 + cnt; |
|
2278 |
__INST(position) = __MKSMALLINT(__p); |
|
2279 |
} |
|
2280 |
RETURN ( self ); |
|
2281 |
} |
|
2282 |
} |
|
2283 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2284 |
} |
|
2285 |
%}. |
|
2286 |
super nextPutLine:aString. |
|
2287 |
! |
|
2288 |
||
2289 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
2290 |
"read from aStream up to and including a line starting with aStringOrNil |
|
2291 |
and append all lines to self. |
|
2292 |
Can be used to copy/create large files or copy from a pipe/socket. |
|
2293 |
||
2294 |
If aStringOrNil is nil or not matched, copy preceeds to the end. |
|
2295 |
(this allows for example to read a Socket and transfer the data quickly |
|
2296 |
into a file - without creating zillions of temporary strings)" |
|
2297 |
||
2298 |
|srcFilePointer readError line| |
|
2299 |
||
2300 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
2301 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
2302 |
srcFilePointer := aStream filePointer. |
|
2303 |
srcFilePointer isNil ifTrue:[aStream errorNotOpen. ^ self]. |
|
2304 |
||
2305 |
%{ /* STACK:2000 */ |
|
2306 |
||
2307 |
#ifndef NO_STDIO |
|
2308 |
FILEPOINTER dst; |
|
2309 |
FILEPOINTER src; |
|
2310 |
char *matchString; |
|
2311 |
int matchLen = 0; |
|
2312 |
char buffer[1024]; |
|
2313 |
||
2314 |
__INST(lastErrorNumber) = nil; |
|
2315 |
if (__isSmallInteger(srcFilePointer)) { |
|
2316 |
if ((aStringOrNil == nil) || __isString(aStringOrNil)) { |
|
2317 |
if (aStringOrNil != nil) { |
|
2318 |
matchString = (char *) __stringVal(aStringOrNil); |
|
2319 |
matchLen = __stringSize(aStringOrNil); |
|
2320 |
} |
|
2321 |
dst = __FILEVal(__INST(filePointer)); |
|
2322 |
src = __FILEVal(srcFilePointer); |
|
2323 |
__BEGIN_INTERRUPTABLE__ |
|
2324 |
errno = 0; |
|
2325 |
||
2326 |
__WRITING__(dst) |
|
2327 |
||
2328 |
for (;;) { |
|
2329 |
if (fgets(buffer, sizeof(buffer)-1, src) == NULL) { |
|
2330 |
if (ferror(src)) { |
|
2331 |
readError = __MKSMALLINT(errno); |
|
2332 |
__END_INTERRUPTABLE__ |
|
2333 |
goto err; |
|
2334 |
} |
|
2335 |
break; |
|
2336 |
} |
|
2337 |
if (fputs(buffer, dst) == EOF) { |
|
2338 |
if (ferror(dst)) { |
|
2339 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2340 |
__END_INTERRUPTABLE__ |
|
2341 |
goto err; |
|
2342 |
} |
|
2343 |
break; |
|
2344 |
} |
|
2345 |
#ifndef OLD |
|
2346 |
if (__INST(buffered) == false) { |
|
2347 |
FFLUSH(dst); |
|
2348 |
} |
|
2349 |
#endif |
|
2350 |
if (matchLen) { |
|
2351 |
if (strncmp(matchString, buffer, matchLen) == 0) |
|
2352 |
break; |
|
2353 |
} |
|
2354 |
} |
|
2355 |
__END_INTERRUPTABLE__ |
|
2356 |
__INST(position) = nil; |
|
2357 |
RETURN (self); |
|
2358 |
} |
|
2359 |
} |
|
2360 |
#endif /* ! NO_STDIO */ |
|
2361 |
err: ; |
|
2362 |
%}. |
|
2363 |
readError ifTrue:[ |
|
2364 |
aStream setLastErrorNumber:readError. |
|
2365 |
aStream readError. |
|
2366 |
^ self |
|
2367 |
]. |
|
2368 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
2369 |
buffered ifFalse:[self errorNotBuffered. ^ self]. |
|
2370 |
||
2371 |
" |
|
2372 |
argument error or unimplemented; try it linewise |
|
2373 |
" |
|
2374 |
[aStream atEnd] whileFalse:[ |
|
2375 |
line := aStream nextLine. |
|
2376 |
line isNil ifTrue:[ |
|
2377 |
^ self. |
|
2378 |
]. |
|
2379 |
self nextPutLine:line. |
|
2380 |
(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[ |
|
2381 |
^ self |
|
2382 |
] |
|
2383 |
]. |
|
2384 |
! |
|
2385 |
||
2386 |
peekForLineStartingWith:aString |
|
2387 |
"read ahead for next line starting with aString; |
|
2388 |
return the line-string if found, or nil if EOF is encountered. |
|
2389 |
If matched, not advance position behond that line |
|
2390 |
i.e. nextLine will read the matched line. |
|
2391 |
If not matched, reposition to original position for firther reading." |
|
2392 |
||
2393 |
|firstPos lastPos line| |
|
2394 |
||
2395 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2396 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2397 |
binary ifTrue:[^ self errorBinary]. |
|
2398 |
buffered ifFalse:[^ self errorNotBuffered]. |
|
2399 |
||
2400 |
%{ /* STACK: 2000 */ |
|
2401 |
#ifndef NO_STDIO |
|
2402 |
||
2403 |
FILEPOINTER f; |
|
2404 |
int l; |
|
2405 |
char buffer[1024]; |
|
2406 |
char *cp; |
|
2407 |
char *matchString; |
|
2408 |
int gotFirst = 0; |
|
2409 |
long firstpos, lastpos; |
|
2410 |
# define FTELL(__f__, __pos__) __pos__ = ftell(__f__) |
|
2411 |
# define FSEEK(__f__, __pos__) fseek(__f__, __pos__, SEEK_SET) |
|
2412 |
||
2413 |
__INST(lastErrorNumber) = nil; |
|
2414 |
if (__isString(aString)) { |
|
2415 |
matchString = (char *) __stringVal(aString); |
|
2416 |
l = __stringSize(aString); |
|
2417 |
||
2418 |
f = __FILEVal(__INST(filePointer)); |
|
2419 |
__READING__(f) |
|
2420 |
||
2421 |
for (;;) { |
|
2422 |
FTELL(f, lastpos); |
|
2423 |
if (!gotFirst) { |
|
2424 |
firstpos = lastpos; |
|
2425 |
gotFirst = 1; |
|
2426 |
} |
|
2427 |
||
2428 |
__BEGIN_INTERRUPTABLE__ |
|
2429 |
do { |
|
2430 |
cp = fgets(buffer, sizeof(buffer)-1, f); |
|
2431 |
} while ((cp == NULL) && ferror(f) && (errno == EINTR) && (clearerr(f), 1)); |
|
2432 |
buffer[sizeof(buffer)-1] = '\0'; |
|
2433 |
__END_INTERRUPTABLE__ |
|
2434 |
||
2435 |
if (cp == NULL) { |
|
2436 |
if (ferror(f)) { |
|
2437 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2438 |
goto err; |
|
2439 |
} else { |
|
2440 |
FSEEK(f, firstpos); |
|
2441 |
RETURN (nil); |
|
2442 |
} |
|
2443 |
} |
|
2444 |
if (strncmp(cp, matchString, l) == 0) { |
|
2445 |
FSEEK(f, lastpos); |
|
2446 |
break; |
|
2447 |
} |
|
2448 |
} |
|
2449 |
/* remove EOL character */ |
|
2450 |
cp = buffer; |
|
2451 |
while (*cp && (*cp != '\n') && (*cp != '\r')) cp++; |
|
2452 |
*cp = '\0'; |
|
2453 |
RETURN ( __MKSTRING(buffer) ); |
|
2454 |
} |
|
2455 |
err: ; |
|
2456 |
||
2457 |
# undef FTELL |
|
2458 |
# undef FSEEK |
|
2459 |
||
2460 |
#endif |
|
2461 |
%}. |
|
2462 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2463 |
||
2464 |
"/ try using generic code ... |
|
2465 |
||
2466 |
firstPos := self position. |
|
2467 |
[self atEnd] whileFalse:[ |
|
2468 |
lastPos := self position. |
|
2469 |
line := self nextLine. |
|
2470 |
line isNil ifTrue:[ |
|
2471 |
self position:firstPos. |
|
2472 |
^ nil |
|
2473 |
]. |
|
2474 |
(line startsWith:aString) ifTrue:[ |
|
2475 |
self position:lastPos. |
|
2476 |
^ line |
|
2477 |
] |
|
2478 |
]. |
|
2479 |
self position:firstPos. |
|
2480 |
^ nil |
|
2481 |
! |
|
2482 |
||
2483 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
2484 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
2485 |
return the index in aCollection if found, nil otherwise.. |
|
2486 |
If no match, do not change position; otherwise advance right before the |
|
2487 |
matched line so that nextLine will return this line." |
|
2488 |
||
2489 |
|line startPos linePos index| |
|
2490 |
||
2491 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2492 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2493 |
binary ifTrue:[^ self errorBinary]. |
|
2494 |
||
2495 |
startPos := self position. |
|
2496 |
[self atEnd] whileFalse:[ |
|
2497 |
linePos := self position. |
|
2498 |
line := self nextLine. |
|
2499 |
line notNil ifTrue:[ |
|
2500 |
index := 1. |
|
2501 |
aCollectionOfStrings do:[:prefix | |
|
2502 |
(line startsWith:prefix) ifTrue:[ |
|
2503 |
self position:linePos. |
|
2504 |
^ index |
|
2505 |
]. |
|
2506 |
index := index + 1 |
|
2507 |
] |
|
2508 |
] |
|
2509 |
]. |
|
2510 |
self position:startPos. |
|
2511 |
^ nil |
|
2512 |
! ! |
|
2513 |
||
2514 |
!ExternalStream methodsFor:'misc functions'! |
|
2515 |
||
2516 |
async:aBoolean |
|
2517 |
"set/clear the async attribute - if set, the availability of data on |
|
2518 |
the receiver will trigger an ioInterrupt. |
|
2519 |
If cleared (which is the default) no special notification is made. |
|
2520 |
Notice: |
|
2521 |
not every OS supports this |
|
2522 |
- check with OS>>supportsIOInterrupts before using" |
|
2523 |
||
2524 |
|fd| |
|
2525 |
||
2526 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2527 |
||
2528 |
OperatingSystem supportsIOInterrupts ifFalse:[ |
|
2529 |
^ self errorUnsupportedOperation |
|
2530 |
]. |
|
2531 |
||
2532 |
fd := self fileDescriptor. |
|
2533 |
aBoolean ifTrue:[ |
|
2534 |
^ OperatingSystem enableIOInterruptsOn:fd |
|
2535 |
]. |
|
2536 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
2537 |
||
2538 |
"Modified: 11.1.1997 / 17:50:21 / cg" |
|
2539 |
! |
|
2540 |
||
2541 |
backStep |
|
2542 |
"step back one element - |
|
2543 |
redefined, since position is redefined here" |
|
2544 |
||
2545 |
self position:(self position - 1) |
|
2546 |
! |
|
2547 |
||
2548 |
blocking:aBoolean |
|
2549 |
"set/clear the blocking attribute - if set (which is the default) |
|
2550 |
a read (using next) on the receiver will block until data is available. |
|
2551 |
If cleared, a read operation will immediately return with a value of |
|
2552 |
nil. |
|
2553 |
Turning off blocking is useful when reading from PipeStreams |
|
2554 |
or Sockets, and the amount of data to be read is not known |
|
2555 |
in advance. However, the data must then be read using #nextBytes: |
|
2556 |
methods, in order to avoid other (pastEnd) exceptions." |
|
2557 |
||
2558 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2559 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor) |
|
2560 |
||
2561 |
"Modified: / 11.1.1997 / 17:48:01 / cg" |
|
2562 |
"Modified: / 15.1.1998 / 11:49:48 / stefan" |
|
2563 |
! |
|
2564 |
||
2565 |
close |
|
2566 |
"close the stream - tell operating system" |
|
2567 |
||
3948 | 2568 |
self isOpen ifTrue:[ |
4206 | 2569 |
Lobby unregister:self. |
2570 |
self closeFile. |
|
3948 | 2571 |
] |
2572 |
||
2573 |
"Modified: / 21.12.1998 / 11:19:54 / cg" |
|
3746 | 2574 |
! |
2575 |
||
2576 |
create |
|
2577 |
"create the stream |
|
2578 |
- this must be redefined in subclass" |
|
2579 |
||
2580 |
^ self subclassResponsibility |
|
2581 |
! |
|
2582 |
||
2583 |
ioctl:ioctlNumber |
|
2584 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2585 |
on the underlying file; no arguments are passed." |
|
2586 |
||
2587 |
^ self ioctl:ioctlNumber with:nil |
|
2588 |
! |
|
2589 |
||
2590 |
ioctl:ioctlNumber with:arg |
|
2591 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2592 |
on the underlying file; the argument is passed as argument. |
|
2593 |
This is not used by ST/X, but provided for special situations |
|
2594 |
- for example, to control proprietrary I/O devices. |
|
2595 |
||
2596 |
Since the type of the argument depends on the ioctl being |
|
2597 |
performed, different arg types are allowed here. |
|
2598 |
If the argument is nil, an ioctl without argument is performed. |
|
2599 |
If the argument is an integral number, its directly passed; |
|
2600 |
if its a kind of ByteArray (ByteArray, String or Structure), |
|
2601 |
or external data (ExternalBytes or ExternalAddress), |
|
2602 |
a pointer to the data is passed. |
|
2603 |
This allows performing most ioctls |
|
2604 |
- however, it might be tricky to setup the buffer. |
|
2605 |
Be careful in what you pass - ST/X cannot validate its correctness." |
|
2606 |
||
2607 |
%{ |
|
2608 |
#if !defined(MSDOS_LIKE) |
|
2609 |
FILEPOINTER f; |
|
2610 |
int ret, ioNum; |
|
2611 |
OBJ fp; |
|
2612 |
INT ioArg; |
|
2613 |
||
2614 |
__INST(lastErrorNumber) = nil; |
|
2615 |
if ((fp = __INST(filePointer)) != nil) { |
|
2616 |
if (__isSmallInteger(ioctlNumber) |
|
2617 |
&& (__isSmallInteger(arg) |
|
2618 |
|| (arg == nil) |
|
2619 |
|| __isBytes(arg) |
|
2620 |
|| __isExternalBytes(arg) |
|
2621 |
|| __isExternalAddress(arg))) { |
|
2622 |
f = __FILEVal(fp); |
|
2623 |
ioNum = __intVal(ioctlNumber); |
|
2624 |
||
2625 |
__BEGIN_INTERRUPTABLE__ |
|
2626 |
do { |
|
2627 |
errno = 0; |
|
2628 |
if (arg == nil) { |
|
2629 |
ioArg = 0; |
|
2630 |
} else if (__isSmallInteger(arg)) { |
|
2631 |
ioArg = __intVal(arg); |
|
2632 |
} else if (__isExternalBytes(arg)) { |
|
2633 |
ioArg = (INT)(__externalBytesAddress(arg)); |
|
2634 |
} else if (__isExternalAddress(arg)) { |
|
2635 |
ioArg = (INT)(__externalAddressVal(arg)); |
|
2636 |
} else { |
|
2637 |
ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element); |
|
2638 |
} |
|
2639 |
ret = ioctl(fileno(f), ioNum, ioArg); |
|
2640 |
} while ((ret < 0) && (errno == EINTR)); |
|
2641 |
__END_INTERRUPTABLE__ |
|
2642 |
||
2643 |
if (ret >= 0) { |
|
2644 |
RETURN ( __MKSMALLINT(ret) ); |
|
2645 |
} |
|
2646 |
__INST(position) = nil; |
|
2647 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2648 |
} |
|
2649 |
} |
|
2650 |
#endif |
|
2651 |
%}. |
|
2652 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
2653 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2654 |
" |
|
2655 |
argument is not an integer |
|
2656 |
" |
|
2657 |
(ioctlNumber isMemberOf:SmallInteger) ifFalse:[^ self primitiveFailed]. |
|
2658 |
" |
|
2659 |
the system does not support ioctl (MSDOS or VMS) |
|
2660 |
" |
|
2661 |
^ self errorUnsupportedOperation |
|
2662 |
! |
|
2663 |
||
2664 |
open |
|
2665 |
"open the stream |
|
2666 |
- this must be redefined in subclass" |
|
2667 |
||
2668 |
^ self subclassResponsibility |
|
2669 |
! |
|
2670 |
||
2671 |
position |
|
2672 |
"return the position |
|
2673 |
- this must be redefined in subclass" |
|
2674 |
||
2675 |
^ self subclassResponsibility |
|
2676 |
! |
|
2677 |
||
2678 |
position:anInteger |
|
2679 |
"set the position |
|
2680 |
- this must be redefined in subclass" |
|
2681 |
||
2682 |
^ self subclassResponsibility |
|
2683 |
! |
|
2684 |
||
2685 |
reset |
|
2686 |
"set the read position to the beginning of the collection" |
|
2687 |
||
2688 |
self position:"0" 1 |
|
2689 |
! |
|
2690 |
||
2691 |
setToEnd |
|
2692 |
"redefined since it must be implemented differently" |
|
2693 |
||
2694 |
^ self subclassResponsibility |
|
2695 |
! |
|
2696 |
||
2697 |
truncateTo:newSize |
|
2698 |
"truncate the underlying OS file to newSize. |
|
2699 |
Warning: this may not be implemented on all platforms." |
|
2700 |
||
2701 |
%{ |
|
2702 |
#ifdef HAS_FTRUNCATE |
|
2703 |
FILEPOINTER f; |
|
2704 |
OBJ fp; |
|
2705 |
||
2706 |
if (((fp = __INST(filePointer)) != nil) |
|
2707 |
&& (__INST(mode) != @symbol(readonly))) { |
|
2708 |
if (__isSmallInteger(newSize)) { |
|
2709 |
f = __FILEVal(fp); |
|
2710 |
||
2711 |
if (__INST(buffered) == true) { |
|
2712 |
__READING__(f) |
|
2713 |
FFLUSH(f); |
|
2714 |
OPT_FSEEK(f, 0L, SEEK_END); /* needed in stdio */ |
|
2715 |
} |
|
2716 |
ftruncate(fileno(f), __intVal(newSize)); |
|
2717 |
RETURN (self); |
|
2718 |
} |
|
2719 |
} |
|
2720 |
#endif |
|
2721 |
%}. |
|
2722 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
2723 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
2724 |
self errorUnsupportedOperation |
|
2725 |
||
2726 |
" |
|
2727 |
|s| |
|
2728 |
||
2729 |
s := 'test' asFilename writeStream. |
|
2730 |
s next:1000 put:$a. |
|
2731 |
s truncateTo:100. |
|
2732 |
s close. |
|
2733 |
||
2734 |
('test' asFilename fileSize) printNL |
|
2735 |
" |
|
2736 |
! ! |
|
2737 |
||
2738 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
2739 |
||
2740 |
nextAvailableBytes:count into:anObject startingAt:start |
|
2741 |
"read the next count bytes into an object and return the number of |
|
2742 |
bytes read or the number of bytes read, if EOF is encountered before, |
|
2743 |
or no more bytes are available for reading (from the pipe/socket). |
|
2744 |
||
2745 |
If the receiver is some socket/pipe-like stream, an exception |
|
2746 |
is raised if the connection is broken. |
|
2747 |
||
2748 |
Notice, that in contrast to other methods, |
|
2749 |
this does NOT return nil on EOF, but the actual count. |
|
2750 |
Thus allowing read of partial blocks. |
|
2751 |
||
2752 |
The object must have non-pointer indexed instvars |
|
2753 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
2754 |
If anObject is a string or byteArray and reused, this provides the |
|
2755 |
fastest possible physical I/O (since no new objects are allocated). |
|
2756 |
||
2757 |
Use with care - non object oriented I/O. |
|
2758 |
Warning: in general, you cannot use this method to pass data from other |
|
2759 |
architectures since it does not care for byte order or float representation." |
|
2760 |
||
2761 |
%{ |
|
2762 |
FILEPOINTER f; |
|
2763 |
int cnt, offs, ret, _buffered; |
|
2764 |
int objSize, nInstVars, nInstBytes; |
|
2765 |
char *cp; |
|
2766 |
char *extPtr; |
|
2767 |
OBJ pos, fp, oClass; |
|
2768 |
int o_offs; |
|
2769 |
||
2770 |
__INST(lastErrorNumber) = nil; |
|
2771 |
if (((fp = __INST(filePointer)) != nil) |
|
2772 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2773 |
&& __bothSmallInteger(count, start) |
|
2774 |
) { |
|
2775 |
f = __FILEVal(fp); |
|
2776 |
||
2777 |
cnt = __intVal(count); |
|
2778 |
offs = __intVal(start) - 1; |
|
2779 |
||
2780 |
oClass = __Class(anObject); |
|
2781 |
if (oClass == ExternalBytes) { |
|
2782 |
OBJ sz; |
|
2783 |
||
2784 |
nInstBytes = 0; |
|
2785 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
2786 |
sz = __externalBytesSize(anObject); |
|
2787 |
if (__isSmallInteger(sz)) { |
|
2788 |
objSize = __intVal(sz); |
|
2789 |
} else { |
|
2790 |
objSize = 0; /* unknown */ |
|
2791 |
} |
|
2792 |
} else { |
|
2793 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
2794 |
case BYTEARRAY: |
|
2795 |
case WORDARRAY: |
|
2796 |
case LONGARRAY: |
|
2797 |
case SWORDARRAY: |
|
2798 |
case SLONGARRAY: |
|
2799 |
case FLOATARRAY: |
|
2800 |
case DOUBLEARRAY: |
|
2801 |
break; |
|
2802 |
default: |
|
2803 |
goto bad; |
|
2804 |
} |
|
2805 |
extPtr = (char *)0; |
|
2806 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2807 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
2808 |
objSize = __Size(anObject) - nInstBytes; |
|
2809 |
} |
|
2810 |
||
2811 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
2812 |
_buffered = (__INST(buffered) == true); |
|
2813 |
if (_buffered) { |
|
2814 |
__READING__(f); |
|
2815 |
} |
|
2816 |
||
2817 |
if (extPtr) { |
|
2818 |
__READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
|
2819 |
} else { |
|
2820 |
/* |
|
2821 |
* on interrupt, anObject may be moved to another location. |
|
2822 |
* So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__, |
|
2823 |
* to get a new address. |
|
2824 |
*/ |
|
2825 |
offs += nInstBytes; |
|
2826 |
__READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
|
2827 |
} |
|
2828 |
if (ret > 0) { |
|
2829 |
pos = __INST(position); |
|
2830 |
if (pos != nil) { |
|
2831 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
|
2832 |
} |
|
2833 |
RETURN (__MKSMALLINT(ret)); |
|
2834 |
} |
|
2835 |
if (ret == 0) { |
|
2836 |
__INST(hitEOF) = true; |
|
2837 |
} else /* ret < 0 */ { |
|
2838 |
__INST(position) = nil; |
|
2839 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2840 |
} |
|
2841 |
} |
|
2842 |
} |
|
2843 |
bad: ; |
|
2844 |
%}. |
|
2845 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
|
2846 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2847 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2848 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2849 |
" |
|
2850 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
2851 |
" |
|
2852 |
^ self primitiveFailed |
|
2853 |
! |
|
2854 |
||
2855 |
nextByte |
|
2856 |
"read the next byte and return it as an Integer; return nil on error. |
|
2857 |
This is allowed in both text and binary modes, always returning the |
|
2858 |
bytes binary value as an integer in 0..255." |
|
2859 |
||
2860 |
%{ |
|
2861 |
FILEPOINTER f; |
|
2862 |
unsigned char byte; |
|
2863 |
int ret, _buffered; |
|
2864 |
OBJ fp; |
|
2865 |
||
2866 |
__INST(lastErrorNumber) = nil; |
|
2867 |
if (((fp = __INST(filePointer)) != nil) |
|
2868 |
&& (__INST(mode) != @symbol(writeonly))) { |
|
2869 |
f = __FILEVal(fp); |
|
2870 |
||
2871 |
_buffered = (__INST(buffered) == true); |
|
2872 |
if (_buffered) { |
|
2873 |
__READING__(f) |
|
2874 |
} |
|
2875 |
__READBYTE__(ret, f, &byte, _buffered); |
|
2876 |
if (ret > 0) { |
|
2877 |
if (__INST(position) != nil) |
|
2878 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
|
2879 |
RETURN (__MKSMALLINT(byte)); |
|
2880 |
} |
|
2881 |
||
2882 |
if (ret == 0) { |
|
2883 |
__INST(hitEOF) = true; |
|
2884 |
} else /* ret < 0 */ { |
|
2885 |
__INST(position) = nil; |
|
2886 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2887 |
} |
|
2888 |
} |
|
2889 |
%}. |
|
2890 |
hitEOF ifTrue:[^ self pastEnd]. |
|
2891 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2892 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2893 |
^ self errorWriteOnly |
|
2894 |
! |
|
2895 |
||
2896 |
nextBytes:count into:anObject startingAt:start |
|
2897 |
"read the next count bytes into an object and return the number of |
|
2898 |
bytes read or the number of bytes read, if EOF is encountered before. |
|
2899 |
If the receiver is some socket/pipe-like stream, an exception |
|
2900 |
is raised if the connection is broken. |
|
2901 |
||
2902 |
Warning: if used with a pipe/socket, this blocks until the requested number |
|
2903 |
of bytes have been read. See #nextAvailableBytes:into:startingAt: |
|
2904 |
to only read whats there. |
|
2905 |
||
2906 |
Notice, that in contrast to other methods, |
|
2907 |
this does NOT return nil on EOF, but the actual count. |
|
2908 |
Thus allowing read of partial blocks. |
|
2909 |
||
2910 |
The object must have non-pointer indexed instvars |
|
2911 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray), |
|
2912 |
or an externalBytes object (with known size). |
|
2913 |
If anObject is a string or byteArray and reused, this provides the |
|
2914 |
fastest possible physical I/O (since no new objects are allocated). |
|
2915 |
||
2916 |
Use with care - non object oriented I/O. |
|
2917 |
Warning: in general, you cannot use this method to pass data from other |
|
2918 |
architectures (unless you prepared the buffer with care), |
|
2919 |
since it does not care for byte order or float representation." |
|
2920 |
||
2921 |
%{ |
|
2922 |
FILEPOINTER f; |
|
2923 |
int cnt, offs, ret, _buffered; |
|
2924 |
int objSize, nInstVars, nInstBytes; |
|
2925 |
char *extPtr; |
|
2926 |
OBJ pos, fp, oClass; |
|
2927 |
||
2928 |
__INST(lastErrorNumber) = nil; |
|
2929 |
if (((fp = __INST(filePointer)) != nil) |
|
2930 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2931 |
&& __bothSmallInteger(count, start) |
|
2932 |
) { |
|
2933 |
f = __FILEVal(fp); |
|
2934 |
||
2935 |
cnt = __intVal(count); |
|
2936 |
offs = __intVal(start) - 1; |
|
2937 |
||
2938 |
oClass = __Class(anObject); |
|
2939 |
if (oClass == ExternalBytes) { |
|
2940 |
OBJ sz; |
|
2941 |
||
2942 |
nInstBytes = 0; |
|
2943 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
2944 |
sz = __externalBytesSize(anObject); |
|
2945 |
if (__isSmallInteger(sz)) { |
|
2946 |
objSize = __intVal(sz); |
|
2947 |
} else { |
|
2948 |
objSize = 0; /* unknown */ |
|
2949 |
} |
|
2950 |
} else { |
|
2951 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
2952 |
case BYTEARRAY: |
|
2953 |
case WORDARRAY: |
|
2954 |
case LONGARRAY: |
|
2955 |
case SWORDARRAY: |
|
2956 |
case SLONGARRAY: |
|
2957 |
case FLOATARRAY: |
|
2958 |
case DOUBLEARRAY: |
|
2959 |
break; |
|
2960 |
default: |
|
2961 |
goto bad; |
|
2962 |
} |
|
2963 |
extPtr = (char *)0; |
|
2964 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2965 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
2966 |
objSize = __Size(anObject) - nInstBytes; |
|
2967 |
} |
|
2968 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
2969 |
_buffered = (__INST(buffered) == true); |
|
2970 |
if (_buffered) { |
|
2971 |
__READING__(f); |
|
2972 |
} |
|
2973 |
||
2974 |
if (extPtr) { |
|
2975 |
__READBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
|
2976 |
} else { |
|
2977 |
/* |
|
2978 |
* on interrupt, anObject may be moved to another location. |
|
2979 |
* So we pass anObject, and the offset to the __READBYTES_OBJ__ macro. |
|
2980 |
*/ |
|
2981 |
offs += nInstBytes; |
|
2982 |
__READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
|
2983 |
} |
|
2984 |
||
2985 |
if (ret > 0) { |
|
2986 |
pos = __INST(position); |
|
2987 |
if (pos != nil) { |
|
2988 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
|
2989 |
} |
|
2990 |
RETURN (__MKSMALLINT(ret)); |
|
2991 |
} |
|
2992 |
if (ret == 0) { |
|
2993 |
__INST(hitEOF) = true; |
|
2994 |
} else /* ret < 0 */ { |
|
2995 |
__INST(position) = nil; |
|
2996 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2997 |
} |
|
2998 |
} |
|
2999 |
} |
|
3000 |
bad: ; |
|
3001 |
%}. |
|
3002 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
|
3003 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3004 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3005 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3006 |
" |
|
3007 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
3008 |
" |
|
3009 |
^ self primitiveFailed |
|
3010 |
! |
|
3011 |
||
3012 |
nextLong |
|
3013 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
3014 |
The returned value may be a LargeInteger. |
|
3015 |
(msb-first for compatibility with other smalltalks)" |
|
3016 |
||
3017 |
^ self nextUnsignedLongMSB:true |
|
3018 |
! |
|
3019 |
||
3020 |
nextLongMSB:msbFlag |
|
3021 |
"Read four bytes and return the value as a 32-bit signed Integer, |
|
3022 |
which may be a LargeInteger. |
|
3023 |
If msbFlag is true, value is read with most-significant byte first, |
|
3024 |
otherwise least-significant byte comes first. |
|
3025 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
3026 |
Works in both binary and text modes." |
|
3027 |
||
3028 |
%{ |
|
3029 |
OBJ fp; |
|
3030 |
||
3031 |
__INST(lastErrorNumber) = nil; |
|
3032 |
if (((fp = __INST(filePointer)) != nil) |
|
3033 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3034 |
) { |
|
3035 |
FILEPOINTER f; |
|
3036 |
int ret; |
|
3037 |
int value; |
|
3038 |
char buffer[4]; |
|
3039 |
||
3040 |
f = __FILEVal(fp); |
|
3041 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
|
3042 |
||
3043 |
if (ret == 4) { |
|
3044 |
if (__INST(position) != nil) { |
|
3045 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
|
3046 |
} |
|
3047 |
if (msbFlag == true) { |
|
3048 |
value = (buffer[0] & 0xFF); |
|
3049 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3050 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3051 |
value = (value << 8) | (buffer[3] & 0xFF); |
|
3052 |
} else { |
|
3053 |
value = (buffer[3] & 0xFF); |
|
3054 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3055 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3056 |
value = (value << 8) | (buffer[0] & 0xFF); |
|
3057 |
} |
|
3058 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
|
3059 |
RETURN ( __MKSMALLINT(value)); |
|
3060 |
} |
|
3061 |
RETURN ( __MKLARGEINT(value) ); |
|
3062 |
} |
|
3063 |
||
3064 |
if (ret < 0) { |
|
3065 |
__INST(position) = nil; |
|
3066 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3067 |
} else /* ret == 0 */ { |
|
3068 |
__INST(hitEOF) = true; |
|
3069 |
} |
|
3070 |
} |
|
3071 |
%}. |
|
3072 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3073 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3074 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3075 |
^ self readError. |
|
3076 |
||
3077 |
! |
|
3078 |
||
3079 |
nextShortMSB:msbFlag |
|
3080 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
3081 |
If msbFlag is true, value is read with most-significant byte first, |
|
3082 |
otherwise least-significant byte comes first. |
|
3083 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3084 |
Works in both binary and text modes." |
|
3085 |
||
3086 |
%{ |
|
3087 |
OBJ fp; |
|
3088 |
||
3089 |
__INST(lastErrorNumber) = nil; |
|
3090 |
if (((fp = __INST(filePointer)) != nil) |
|
3091 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3092 |
) { |
|
3093 |
FILEPOINTER f; |
|
3094 |
int ret; |
|
3095 |
short value; |
|
3096 |
char buffer[2]; |
|
3097 |
||
3098 |
f = __FILEVal(fp); |
|
3099 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
3100 |
||
3101 |
if (ret == 2) { |
|
3102 |
if (__INST(position) != nil) { |
|
3103 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
3104 |
} |
|
3105 |
if (msbFlag == true) { |
|
3106 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
3107 |
} else { |
|
3108 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
3109 |
} |
|
3110 |
RETURN (__MKSMALLINT(value)); |
|
3111 |
} |
|
3112 |
||
3113 |
if (ret < 0) { |
|
3114 |
__INST(position) = nil; |
|
3115 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3116 |
} else /* ret == 0 */ { |
|
3117 |
__INST(hitEOF) = true; |
|
3118 |
} |
|
3119 |
} |
|
3120 |
%}. |
|
3121 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3122 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3123 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3124 |
^ self readError. |
|
3125 |
! |
|
3126 |
||
3127 |
nextUnsignedLongMSB:msbFlag |
|
3128 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
3129 |
a LargeInteger. |
|
3130 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
3131 |
least-significant byte comes first. |
|
3132 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
3133 |
Works in both binary and text modes." |
|
3134 |
||
3135 |
%{ |
|
3136 |
OBJ fp; |
|
3137 |
||
3138 |
__INST(lastErrorNumber) = nil; |
|
3139 |
if (((fp = __INST(filePointer)) != nil) |
|
3140 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3141 |
) { |
|
3142 |
FILEPOINTER f; |
|
3143 |
int ret; |
|
3144 |
unsigned INT value; |
|
3145 |
unsigned char buffer[4]; |
|
3146 |
||
3147 |
f = __FILEVal(fp); |
|
3148 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
|
3149 |
||
3150 |
if (ret == 4) { |
|
3151 |
if (__INST(position) != nil) { |
|
3152 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
|
3153 |
} |
|
3154 |
if (msbFlag == true) { |
|
3155 |
value = (buffer[0] & 0xFF); |
|
3156 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3157 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3158 |
value = (value << 8) | (buffer[3] & 0xFF); |
|
3159 |
} else { |
|
3160 |
value = (buffer[3] & 0xFF); |
|
3161 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3162 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3163 |
value = (value << 8) | (buffer[0] & 0xFF); |
|
3164 |
} |
|
3165 |
#ifdef alpha64 |
|
3166 |
value &= 0xFFFFFFFF; |
|
3167 |
#endif |
|
3168 |
if (value <= _MAX_INT) { |
|
3169 |
RETURN ( __MKSMALLINT(value)); |
|
3170 |
} |
|
3171 |
RETURN ( __MKULARGEINT(value) ); |
|
3172 |
} |
|
3173 |
||
3174 |
if (ret < 0) { |
|
3175 |
__INST(position) = nil; |
|
3176 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3177 |
} else /* ret == 0 */ { |
|
3178 |
__INST(hitEOF) = true; |
|
3179 |
} |
|
3180 |
} |
|
3181 |
%}. |
|
3182 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3183 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3184 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3185 |
^ self readError. |
|
3186 |
! |
|
3187 |
||
3188 |
nextUnsignedShortMSB:msbFlag |
|
3189 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
3190 |
If msbFlag is true, value is read with most-significant byte first, |
|
3191 |
otherwise least-significant byte comes first. |
|
3192 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3193 |
Works in both binary and text modes." |
|
3194 |
||
3195 |
%{ |
|
3196 |
OBJ fp; |
|
3197 |
||
3198 |
__INST(lastErrorNumber) = nil; |
|
3199 |
if (((fp = __INST(filePointer)) != nil) |
|
3200 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3201 |
) { |
|
3202 |
FILEPOINTER f; |
|
3203 |
int ret; |
|
3204 |
unsigned int value; |
|
3205 |
char buffer[2]; |
|
3206 |
||
3207 |
f = __FILEVal(fp); |
|
3208 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
3209 |
||
3210 |
if (ret == 2) { |
|
3211 |
if (__INST(position) != nil) { |
|
3212 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
3213 |
} |
|
3214 |
if (msbFlag == true) { |
|
3215 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
3216 |
} else { |
|
3217 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
3218 |
} |
|
3219 |
RETURN (__MKSMALLINT(value)); |
|
3220 |
} |
|
3221 |
||
3222 |
if (ret < 0) { |
|
3223 |
__INST(position) = nil; |
|
3224 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3225 |
} else /* ret == 0 */ { |
|
3226 |
__INST(hitEOF) = true; |
|
3227 |
} |
|
3228 |
} |
|
3229 |
%}. |
|
3230 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3231 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3232 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3233 |
^ self readError. |
|
3234 |
! |
|
3235 |
||
3236 |
nextWord |
|
3237 |
"in text-mode: |
|
3238 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
|
3239 |
return a string containing those characters. |
|
3240 |
in binary-mode: |
|
3241 |
read two bytes (msb-first) and return the value as a 16-bit |
|
3242 |
unsigned Integer (for compatibility with other smalltalks)" |
|
3243 |
||
3244 |
binary ifTrue:[ |
|
3245 |
^ self nextUnsignedShortMSB:true |
|
3246 |
]. |
|
3247 |
^ self nextAlphaNumericWord |
|
3248 |
! ! |
|
3249 |
||
3250 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
3251 |
||
3252 |
nextPutByte:aByteValue |
|
3253 |
"write a byte. |
|
3254 |
Works in both binary and text modes." |
|
3255 |
||
3256 |
%{ |
|
3257 |
FILEPOINTER f; |
|
3258 |
char c; |
|
3259 |
OBJ pos, fp; |
|
3260 |
int cnt, _buffered; |
|
3261 |
||
3262 |
__INST(lastErrorNumber) = nil; |
|
3263 |
if (((fp = __INST(filePointer)) != nil) |
|
3264 |
&& (__INST(mode) != @symbol(readonly)) |
|
3265 |
&& __isSmallInteger(aByteValue) |
|
3266 |
||
3267 |
) { |
|
3268 |
c = __intVal(aByteValue); |
|
3269 |
f = __FILEVal(fp); |
|
3270 |
if (_buffered = (__INST(buffered) == true)) { |
|
3271 |
__WRITING__(f) |
|
3272 |
} |
|
3273 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
|
3274 |
if (cnt == 1) { |
|
3275 |
pos = __INST(position); |
|
3276 |
if (pos != nil) |
|
3277 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3278 |
RETURN (self); |
|
3279 |
} |
|
3280 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3281 |
} |
|
3282 |
%}. |
|
3283 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3284 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3285 |
self writeError. |
|
3286 |
! |
|
3287 |
||
3288 |
nextPutBytes:count from:anObject startingAt:start |
|
3289 |
"write count bytes from an object starting at index start. |
|
3290 |
return the number of bytes written - which could be 0. |
|
3291 |
The object must have non-pointer indexed instvars |
|
3292 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
|
3293 |
or an externalBytes object (with known size). |
|
3294 |
||
3295 |
Use with care - non object oriented i/o. |
|
3296 |
Warning: in general, you cannot use this method to pass data to other |
|
3297 |
architectures (unless you prepared the buffer with care), |
|
3298 |
since it does not care for byte order or float representation." |
|
3299 |
||
3300 |
%{ |
|
3301 |
FILEPOINTER f; |
|
3302 |
int cnt, len, offs, ret; |
|
3303 |
int objSize, nInstVars, nInstBytes, _buffered; |
|
3304 |
char *extPtr; |
|
3305 |
OBJ oClass, pos, fp; |
|
3306 |
||
3307 |
__INST(lastErrorNumber) = nil; |
|
3308 |
if (((fp = __INST(filePointer)) != nil) |
|
3309 |
&& (__INST(mode) != @symbol(readonly)) |
|
3310 |
&& __bothSmallInteger(count, start) |
|
3311 |
) { |
|
3312 |
len = __intVal(count); |
|
3313 |
offs = __intVal(start) - 1; |
|
3314 |
f = __FILEVal(fp); |
|
3315 |
||
3316 |
oClass = __Class(anObject); |
|
3317 |
if (oClass == ExternalBytes) { |
|
3318 |
OBJ sz; |
|
3319 |
||
3320 |
nInstBytes = 0; |
|
3321 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
3322 |
sz = __externalBytesSize(anObject); |
|
3323 |
if (__isSmallInteger(sz)) { |
|
3324 |
objSize = __intVal(sz); |
|
3325 |
} else { |
|
3326 |
objSize = 0; /* unknown */ |
|
3327 |
} |
|
3328 |
} else { |
|
3329 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
3330 |
case BYTEARRAY: |
|
3331 |
case WORDARRAY: |
|
3332 |
case LONGARRAY: |
|
3333 |
case SWORDARRAY: |
|
3334 |
case SLONGARRAY: |
|
3335 |
case FLOATARRAY: |
|
3336 |
case DOUBLEARRAY: |
|
3337 |
break; |
|
3338 |
default: |
|
3339 |
goto bad; |
|
3340 |
} |
|
3341 |
extPtr = (char *)0; |
|
3342 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3343 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3344 |
objSize = __Size(anObject) - nInstBytes; |
|
3345 |
} |
|
3346 |
if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) { |
|
3347 |
if (_buffered = (__INST(buffered) == true)) { |
|
3348 |
__WRITING__(f) |
|
3349 |
} |
|
3350 |
||
3351 |
if (extPtr) { |
|
3352 |
__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered); |
|
3353 |
} else { |
|
3354 |
/* |
|
3355 |
* on interrupt, anObject may be moved to another location. |
|
3356 |
* So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro. |
|
3357 |
*/ |
|
3358 |
offs += nInstBytes; |
|
3359 |
__WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered); |
|
3360 |
} |
|
3361 |
||
3362 |
if (cnt >= 0) { |
|
3363 |
pos = __INST(position); |
|
3364 |
if (pos != nil) |
|
3365 |
__INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
3366 |
RETURN ( __MKSMALLINT(cnt) ); |
|
3367 |
} |
|
3368 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3369 |
} |
|
3370 |
} |
|
3371 |
bad: ; |
|
3372 |
%}. |
|
3373 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3374 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3375 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3376 |
^ self primitiveFailed |
|
3377 |
! |
|
3378 |
||
3379 |
nextPutLong:aNumber MSB:msbFlag |
|
3380 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
3381 |
true, data is written most-significant byte first; otherwise least |
|
3382 |
first. |
|
3383 |
Works in both binary and text modes." |
|
3384 |
||
3385 |
%{ |
|
3386 |
int num; |
|
3387 |
char bytes[4]; |
|
3388 |
FILEPOINTER f; |
|
3389 |
int cnt, _buffered; |
|
3390 |
OBJ fp, pos; |
|
3391 |
||
3392 |
__INST(lastErrorNumber) = nil; |
|
3393 |
if (((fp = __INST(filePointer)) != nil) |
|
3394 |
&& (__INST(mode) != @symbol(readonly)) |
|
3395 |
&& __isSmallInteger(aNumber) |
|
3396 |
) { |
|
3397 |
num = __intVal(aNumber); |
|
3398 |
if (msbFlag == true) { |
|
3399 |
bytes[0] = (num >> 24) & 0xFF; |
|
3400 |
bytes[1] = (num >> 16) & 0xFF; |
|
3401 |
bytes[2] = (num >> 8) & 0xFF; |
|
3402 |
bytes[3] = num & 0xFF; |
|
3403 |
} else { |
|
3404 |
bytes[3] = (num >> 24) & 0xFF; |
|
3405 |
bytes[2] = (num >> 16) & 0xFF; |
|
3406 |
bytes[1] = (num >> 8) & 0xFF; |
|
3407 |
bytes[0] = num & 0xFF; |
|
3408 |
} |
|
3409 |
||
3410 |
f = __FILEVal(fp); |
|
3411 |
if (_buffered = (__INST(buffered) == true)) { |
|
3412 |
__WRITING__(f) |
|
3413 |
} |
|
3414 |
__WRITEBYTES__(cnt, f, bytes, 4, _buffered); |
|
3415 |
||
3416 |
if (cnt == 4) { |
|
3417 |
if ((pos = __INST(position)) != nil) { |
|
3418 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 4); |
|
3419 |
} |
|
3420 |
RETURN ( self ); |
|
3421 |
} |
|
3422 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3423 |
} |
|
3424 |
%}. |
|
3425 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3426 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3427 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
3428 |
||
3429 |
aNumber isInteger ifTrue:[ |
|
3430 |
^ super nextPutLong:aNumber MSB:msbFlag |
|
3431 |
]. |
|
3432 |
self argumentMustBeInteger |
|
3433 |
! |
|
3434 |
||
3435 |
nextPutShort:aNumber MSB:msbFlag |
|
3436 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
3437 |
true, data is written most-significant byte first; otherwise least |
|
3438 |
first. |
|
3439 |
Works in both binary and text modes." |
|
3440 |
||
3441 |
%{ |
|
3442 |
int num; |
|
3443 |
char bytes[2]; |
|
3444 |
FILEPOINTER f; |
|
3445 |
int cnt, _buffered; |
|
3446 |
OBJ fp, pos; |
|
3447 |
||
3448 |
__INST(lastErrorNumber) = nil; |
|
3449 |
if (((fp = __INST(filePointer)) != nil) |
|
3450 |
&& (__INST(mode) != @symbol(readonly)) |
|
3451 |
&& __isSmallInteger(aNumber) |
|
3452 |
) { |
|
3453 |
num = __intVal(aNumber); |
|
3454 |
if (msbFlag == true) { |
|
3455 |
bytes[0] = (num >> 8) & 0xFF; |
|
3456 |
bytes[1] = num & 0xFF; |
|
3457 |
} else { |
|
3458 |
bytes[1] = (num >> 8) & 0xFF; |
|
3459 |
bytes[0] = num & 0xFF; |
|
3460 |
} |
|
3461 |
||
3462 |
f = __FILEVal(fp); |
|
3463 |
if (_buffered = (__INST(buffered) == true)) { |
|
3464 |
__WRITING__(f) |
|
3465 |
} |
|
3466 |
__WRITEBYTES__(cnt, f, bytes, 2, _buffered); |
|
3467 |
||
3468 |
if (cnt == 2) { |
|
3469 |
if ((pos = __INST(position)) != nil) { |
|
3470 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 2); |
|
3471 |
} |
|
3472 |
RETURN ( self ); |
|
3473 |
} |
|
3474 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3475 |
} |
|
3476 |
%}. |
|
3477 |
lastErrorNumber notNil ifTrue:[^ self writeError. ^ self]. |
|
3478 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3479 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3480 |
self argumentMustBeInteger |
|
3481 |
! ! |
|
3482 |
||
3483 |
!ExternalStream methodsFor:'private'! |
|
3484 |
||
3485 |
clearEOF |
|
3486 |
hitEOF := false |
|
3487 |
! |
|
3488 |
||
3489 |
connectTo:aFileDescriptor withMode:openmode |
|
3490 |
"connect a fileDescriptor; openmode is the string defining the way to open. |
|
3491 |
This can be used to connect an extrnally provided fileDescriptor (from |
|
3492 |
primitive code) or a pipeFileDescriptor (as returned by makePipe) to |
|
3493 |
a Stream object. |
|
3494 |
The openMode ('r', 'w' etc.) must match the mode in which |
|
3495 |
the fileDescriptor was originally opened (otherwise i/o errors will be reported later)." |
|
3496 |
||
3497 |
|retVal| |
|
3498 |
||
3499 |
filePointer notNil ifTrue:[^ self errorOpen]. |
|
3500 |
%{ |
|
3501 |
FILEPOINTER f; |
|
3502 |
OBJ fp; |
|
3503 |
FILE *fdopen(); |
|
3504 |
||
3505 |
if (__isSmallInteger(aFileDescriptor) |
|
3506 |
&& (__qClass(openmode)== @global(String))) { |
|
3507 |
f = (FILEPOINTER) fdopen(__intVal(aFileDescriptor), (char *)__stringVal(openmode)); |
|
3508 |
if (f == NULL) { |
|
3509 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3510 |
__INST(position) = nil; |
|
3511 |
} else { |
|
3512 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
|
3513 |
__INST(position) = __MKSMALLINT(1); |
|
3514 |
retVal = self; |
|
3515 |
} |
|
3516 |
} |
|
3517 |
%}. |
|
3518 |
retVal notNil ifTrue:[ |
|
3519 |
self buffered:true. "default is buffered" |
|
3520 |
Lobby register:self |
|
3521 |
]. |
|
3522 |
lastErrorNumber notNil ifTrue:[ |
|
3523 |
" |
|
3524 |
the open failed for some reason ... |
|
3525 |
" |
|
3526 |
^ self openError |
|
3527 |
]. |
|
3528 |
^ retVal |
|
3529 |
! |
|
3530 |
||
3531 |
open:aPath withMode:mode |
|
3532 |
"low level open; opens the file/device and sets the filePointer instance |
|
3533 |
variable. Careful: this does not care for any other state." |
|
3534 |
||
3535 |
|ok| |
|
3536 |
||
3537 |
ok := false. |
|
3538 |
%{ |
|
3539 |
FILE *f; |
|
3540 |
OBJ fp; |
|
3541 |
||
3542 |
if (__isString(aPath)) { |
|
3543 |
#ifdef NO_STDIO |
|
3544 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(mode)); |
|
3545 |
#else |
|
3546 |
__BEGIN_INTERRUPTABLE__ |
|
3547 |
do { |
|
3548 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(mode)); |
|
3549 |
} while ((f == NULL) && (errno == EINTR)); |
|
3550 |
__END_INTERRUPTABLE__ |
|
3551 |
#endif |
|
3552 |
if (f == NULL) { |
|
3553 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3554 |
} else { |
|
3555 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
|
3556 |
ok = true; |
|
3557 |
} |
|
3558 |
} |
|
3559 |
%}. |
|
3560 |
ok ifFalse:[ |
|
3561 |
" |
|
3562 |
the open failed for some reason ... |
|
3563 |
" |
|
3564 |
^ self openError |
|
3565 |
]. |
|
3566 |
Lobby register:self. |
|
3567 |
! |
|
3568 |
||
3569 |
reOpen |
|
3570 |
"sent after snapin to reopen streams. |
|
3571 |
cannot reopen here since I am abstract and have no device knowledge" |
|
3572 |
||
3573 |
self class name errorPrint. ' [warning]: automatic reOpen not supported - stream closed' errorPrintCR. |
|
3574 |
filePointer := nil. |
|
3575 |
Lobby unregister:self. |
|
3576 |
||
3577 |
"Modified: 10.1.1997 / 17:50:44 / cg" |
|
3578 |
! |
|
3579 |
||
3580 |
setLastError:aNumber |
|
3581 |
lastErrorNumber := aNumber |
|
3582 |
! ! |
|
3583 |
||
3584 |
!ExternalStream methodsFor:'queries'! |
|
3585 |
||
3586 |
isBinary |
|
3587 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
3588 |
The default when created is false." |
|
3589 |
||
3590 |
^ binary |
|
3591 |
! |
|
3592 |
||
3593 |
isExternalStream |
|
3594 |
"return true, if the receiver is some kind of externalStream; |
|
3595 |
true is returned here - the method redefined from Object." |
|
3596 |
||
3597 |
^ true |
|
3598 |
! |
|
3599 |
||
3948 | 3600 |
isOpen |
3601 |
"return true, if the stream is open" |
|
3602 |
||
3603 |
^ filePointer notNil |
|
3604 |
||
3605 |
"Created: / 21.12.1998 / 11:19:10 / cg" |
|
3606 |
! |
|
3607 |
||
3746 | 3608 |
isReadable |
3609 |
"return true, if this stream can be read from" |
|
3610 |
||
3611 |
^ (mode ~~ #writeonly) |
|
3612 |
! |
|
3613 |
||
3614 |
isWritable |
|
3615 |
"return true, if this stream can be written to" |
|
3616 |
||
3617 |
^ (mode ~~ #readonly) |
|
3618 |
! ! |
|
3619 |
||
3620 |
!ExternalStream methodsFor:'reading'! |
|
3621 |
||
3622 |
next |
|
3623 |
"return the next element; advance read position. |
|
3624 |
In binary mode, an integer is returned, otherwise a character. |
|
3625 |
If there are no more elements, nil is returned." |
|
3626 |
||
3627 |
%{ |
|
3628 |
FILEPOINTER f; |
|
3629 |
int ret, _buffered; |
|
3630 |
OBJ pos, fp; |
|
3631 |
unsigned char ch; |
|
3632 |
||
3633 |
__INST(lastErrorNumber) = nil; |
|
3634 |
if (((fp = __INST(filePointer)) != nil) |
|
3635 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3636 |
) { |
|
3637 |
f = __FILEVal(fp); |
|
3638 |
||
3639 |
_buffered = (__INST(buffered) == true); |
|
3640 |
if (_buffered) { |
|
3641 |
__READING__(f) |
|
3642 |
} |
|
3643 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3644 |
||
3645 |
if (ret > 0) { |
|
3646 |
pos = __INST(position); |
|
3647 |
if (__isSmallInteger(pos)) { |
|
3648 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3649 |
} else { |
|
3650 |
__INST(position) = nil; |
|
3651 |
} |
|
3652 |
if (__INST(binary) == true) { |
|
3653 |
RETURN ( __MKSMALLINT(ch) ); |
|
3654 |
} |
|
3655 |
RETURN ( __MKCHARACTER(ch) ); |
|
3656 |
} |
|
3657 |
||
3658 |
__INST(position) = nil; |
|
3659 |
if (ret < 0) { |
|
3660 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3661 |
} else /* ret == 0 */ { |
|
3662 |
__INST(hitEOF) = true; |
|
3663 |
} |
|
3664 |
} |
|
3665 |
%}. |
|
3666 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3667 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3668 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3669 |
^ self errorWriteOnly |
|
3670 |
! |
|
3671 |
||
3672 |
next:count |
|
3673 |
"return the next count elements of the stream as a collection. |
|
3674 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
3675 |
||
3676 |
|coll nRead| |
|
3677 |
||
3678 |
binary ifTrue:[ |
|
3679 |
coll := ByteArray uninitializedNew:count |
|
3680 |
] ifFalse:[ |
|
3681 |
coll := String new:count |
|
3682 |
]. |
|
3683 |
nRead := self nextBytes:count into:coll startingAt:1. |
|
3684 |
||
3685 |
"/ for readStream protocol compatibility, |
|
3686 |
"/ we must raise an exception here. |
|
3687 |
||
3688 |
nRead < count ifTrue:[ |
|
3689 |
^ self pastEnd |
|
3690 |
]. |
|
3691 |
^ coll |
|
3692 |
||
3693 |
"Modified: 11.1.1997 / 17:44:17 / cg" |
|
3694 |
! |
|
3695 |
||
3696 |
peek |
|
3697 |
"return the element to be read next without advancing read position. |
|
3698 |
In binary mode, an integer is returned, otherwise a character. |
|
3699 |
If there are no more elements, nil is returned." |
|
3700 |
||
3701 |
%{ |
|
3702 |
FILEPOINTER f; |
|
3703 |
unsigned char c; |
|
3704 |
int ret, _buffered; |
|
3705 |
OBJ fp; |
|
3706 |
||
3707 |
#if 0 |
|
3708 |
if (__INST(readAhead) != nil) { |
|
3709 |
RETURN (__INST(readAhead)); |
|
3710 |
} |
|
3711 |
#endif |
|
3712 |
__INST(lastErrorNumber) = nil; |
|
3713 |
||
3714 |
if (((fp = __INST(filePointer)) != nil) |
|
3715 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3716 |
) { |
|
3717 |
f = __FILEVal(fp); |
|
3718 |
_buffered = (__INST(buffered) == true); |
|
3719 |
if (_buffered) { |
|
3720 |
__READING__(f) |
|
3721 |
} |
|
3722 |
__READBYTE__(ret, f, &c, _buffered); |
|
3723 |
||
3724 |
if (ret > 0) { |
|
3725 |
__UNGETC__(c, f, _buffered); |
|
3726 |
||
3727 |
if (__INST(binary) == true) { |
|
3728 |
RETURN ( __MKSMALLINT(c) ); |
|
3729 |
} |
|
3730 |
RETURN ( __MKCHARACTER(c) ); |
|
3731 |
} |
|
3732 |
if (ret < 0) { |
|
3733 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3734 |
} else /* ret == 0 */ { |
|
3735 |
__INST(hitEOF) = true; |
|
3736 |
} |
|
3737 |
} |
|
3738 |
%}. |
|
3739 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3740 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3741 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3742 |
^ self errorWriteOnly |
|
3743 |
! |
|
3744 |
||
3745 |
upToEnd |
|
3746 |
"return a collection of the elements up-to the end. |
|
3747 |
Return nil if the stream-end is reached before." |
|
3748 |
||
3749 |
|answerStream ch| |
|
3750 |
||
3751 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
3752 |
[self atEnd] whileFalse:[ |
|
3753 |
ch := self next. |
|
3754 |
ch notNil ifTrue:[ |
|
3755 |
answerStream nextPut:ch |
|
3756 |
] |
|
3757 |
]. |
|
3758 |
^ answerStream contents |
|
3759 |
||
3760 |
"Created: 25.4.1996 / 14:40:31 / cg" |
|
3761 |
! ! |
|
3762 |
||
3763 |
!ExternalStream methodsFor:'reimplemented for speed'! |
|
3764 |
||
3765 |
nextAlphaNumericWord |
|
3766 |
"read the next word (i.e. up to non letter-or-digit) after first |
|
3767 |
skipping any whiteSpace. |
|
3768 |
Return a string containing those characters. |
|
3769 |
There is a limit of 1023 characters in the word - if longer, |
|
3770 |
it is truncated." |
|
3771 |
||
3772 |
%{ /* STACK: 2000 */ |
|
3773 |
FILEPOINTER f; |
|
3774 |
int len; |
|
3775 |
char buffer[1024]; |
|
3776 |
unsigned char ch; |
|
3777 |
int cnt = 0; |
|
3778 |
int ret, _buffered; |
|
3779 |
OBJ fp; |
|
3780 |
||
3781 |
if (((fp = __INST(filePointer)) != nil) |
|
3782 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3783 |
) { |
|
3784 |
f = __FILEVal(fp); |
|
3785 |
_buffered = (__INST(buffered) == true); |
|
3786 |
if (_buffered) { |
|
3787 |
__READING__(f) |
|
3788 |
} |
|
3789 |
||
3790 |
/* |
|
3791 |
* skip whiteSpace first ... |
|
3792 |
*/ |
|
3793 |
do { |
|
3794 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3795 |
if (ret > 0) |
|
3796 |
cnt++; |
|
3797 |
else |
|
3798 |
break; |
|
3799 |
} while ( |
|
3800 |
#ifndef NON_ASCII |
|
3801 |
(ch < ' ') || |
|
3802 |
#endif |
|
3803 |
(ch == ' ' ) || (ch == '\t') || (ch == '\r') |
|
3804 |
|| (ch == '\n') || (ch == 0x0b)); |
|
3805 |
||
3806 |
len = 0; |
|
3807 |
while (ret > 0 && |
|
3808 |
(((ch >= 'a') && (ch <= 'z')) || |
|
3809 |
((ch >= 'A') && (ch <= 'Z')) || |
|
3810 |
((ch >= '0') && (ch <= '9'))) |
|
3811 |
) { |
|
3812 |
buffer[len++] = ch; |
|
3813 |
if (len >= (sizeof(buffer)-1)) { |
|
3814 |
/* emergency */ |
|
3815 |
break; |
|
3816 |
} |
|
3817 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3818 |
if (ret > 0) |
|
3819 |
cnt++; |
|
3820 |
else |
|
3821 |
break; |
|
3822 |
} |
|
3823 |
||
3824 |
if (ret <= 0) { |
|
3825 |
if (ret < 0) { |
|
3826 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3827 |
goto err; |
|
3828 |
} |
|
3829 |
else if (ret == 0) |
|
3830 |
__INST(hitEOF) = true; |
|
3831 |
} else { |
|
3832 |
__UNGETC__(ch, f, _buffered); |
|
3833 |
cnt--; |
|
3834 |
} |
|
3835 |
||
3836 |
if (__INST(position) != nil) { |
|
3837 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + cnt); |
|
3838 |
} |
|
3839 |
if (len != 0) { |
|
3840 |
buffer[len] = '\0'; |
|
3841 |
RETURN ( __MKSTRING_L(buffer, len) ); |
|
3842 |
} |
|
3843 |
} |
|
3844 |
err: ; |
|
3845 |
%}. |
|
3846 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3847 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3848 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3849 |
^ self errorWriteOnly |
|
3850 |
! |
|
3851 |
||
3852 |
nextChunk |
|
3853 |
"return the next chunk, i.e. all characters up to the next |
|
3854 |
exclamation mark. Within the chunk, exclamation marks have to be doubled - |
|
3855 |
except within primitive code (this exception was added to make it easier |
|
3856 |
to edit primitive code with external editors). This means, that other |
|
3857 |
Smalltalks cannot always read chunks containing primitive code - but that |
|
3858 |
doesnt really matter, since C-primitives are an ST/X feature anyway. |
|
3859 |
Reimplemented here for more speed." |
|
3860 |
||
3861 |
|retVal outOfMemory buffer| |
|
3862 |
||
3863 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3864 |
binary ifTrue:[^ self errorBinary]. |
|
3865 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3866 |
||
3867 |
%{ /* STACK: 12000 */ |
|
3868 |
/* |
|
3869 |
* the main trick (and a bit of complication) here |
|
3870 |
* is to read the first 8k into a stack-buffer. |
|
3871 |
* Since most chunks fit into that, |
|
3872 |
* this avoids creating lots of garbage for thos small chunks. |
|
3873 |
*/ |
|
3874 |
FILEPOINTER f; |
|
3875 |
int nread = 0, done = 0; |
|
3876 |
unsigned char c, lastC, peekC; |
|
3877 |
int ret; |
|
3878 |
unsigned char *bufferPtr = (unsigned char *)0; |
|
3879 |
unsigned char fastBuffer[8000]; |
|
3880 |
REGISTER int index; |
|
3881 |
int currSize, fastFlag, _buffered; |
|
3882 |
int atBeginOfLine = 1, inPrimitive = 0; |
|
3883 |
int haveCR = 0, haveNL = 0; |
|
3884 |
||
3885 |
__INST(lastErrorNumber) = nil; |
|
3886 |
f = __FILEVal(__INST(filePointer)); |
|
3887 |
||
3888 |
_buffered = (__INST(buffered) == true); |
|
3889 |
if (_buffered) { |
|
3890 |
__READING__(f) |
|
3891 |
} |
|
3892 |
||
3893 |
/* |
|
3894 |
* skip spaces |
|
3895 |
*/ |
|
3896 |
c = '\n'; |
|
3897 |
while (! done) { |
|
3898 |
lastC = c; |
|
3899 |
__READBYTE__(ret, f, &c, _buffered); |
|
3900 |
if (ret <= 0) { |
|
3901 |
goto err; |
|
3902 |
} |
|
3903 |
nread++; |
|
3904 |
atBeginOfLine = 0; |
|
3905 |
switch (c) { |
|
3906 |
case '\n': |
|
3907 |
case ' ': |
|
3908 |
case '\t': |
|
3909 |
case '\r': |
|
3910 |
case '\b': |
|
3911 |
case '\014': |
|
3912 |
break; |
|
3913 |
||
3914 |
default: |
|
3915 |
atBeginOfLine = (lastC == '\n'); |
|
3916 |
done = 1; |
|
3917 |
break; |
|
3918 |
} |
|
3919 |
} |
|
3920 |
||
3921 |
/* |
|
3922 |
* read chunk into a buffer |
|
3923 |
*/ |
|
3924 |
bufferPtr = fastBuffer; fastFlag = 1; |
|
3925 |
currSize = sizeof(fastBuffer); |
|
3926 |
||
3927 |
index = 0; |
|
3928 |
for (;;) { |
|
3929 |
__READBYTE__(ret, f, &peekC, _buffered); |
|
3930 |
if ((c == '%') && atBeginOfLine && ret > 0) { |
|
3931 |
if (peekC == '{') { |
|
3932 |
inPrimitive = 1; |
|
3933 |
} else if (peekC == '}') { |
|
3934 |
inPrimitive = 0; |
|
3935 |
} |
|
3936 |
} else if (c == '!' && !inPrimitive) { |
|
3937 |
if (ret > 0 && peekC == '!') { |
|
3938 |
/* |
|
3939 |
* convert double-! to a single ! |
|
3940 |
*/ |
|
3941 |
__READBYTE__(ret, f, &peekC, _buffered); |
|
3942 |
if (ret > 0) |
|
3943 |
nread++; |
|
3944 |
} else { |
|
3945 |
/* |
|
3946 |
* End of chunk, push back lookahead character |
|
3947 |
* and leave loop. |
|
3948 |
*/ |
|
3949 |
if (ret > 0) { |
|
3950 |
__UNGETC__(peekC, f, _buffered); |
|
3951 |
} else |
|
3952 |
ret = 1; |
|
3953 |
break; |
|
3954 |
} |
|
3955 |
} |
|
3956 |
||
3957 |
/* |
|
3958 |
* do we have to resize the buffer ? |
|
3959 |
*/ |
|
3960 |
if ((index+2) >= currSize) { |
|
3961 |
OBJ newBuffer; |
|
3962 |
unsigned char *nbp; |
|
3963 |
||
3964 |
newBuffer = __MKEMPTYSTRING(currSize * 2); |
|
3965 |
if (!fastFlag) { |
|
3966 |
/* |
|
3967 |
* refetch - buffer could be moved by GC |
|
3968 |
*/ |
|
3969 |
bufferPtr = __stringVal(buffer); |
|
3970 |
} |
|
3971 |
if (newBuffer == nil) { |
|
3972 |
/* |
|
3973 |
* mhmh - chunk seems to be very big .... |
|
3974 |
*/ |
|
3975 |
outOfMemory = true; |
|
3976 |
goto err; |
|
3977 |
} |
|
3978 |
nbp = __stringVal(newBuffer); |
|
3979 |
bcopy(bufferPtr, nbp, index); |
|
3980 |
bufferPtr = nbp; |
|
3981 |
bufferPtr[index] = '\0'; |
|
3982 |
buffer = newBuffer; |
|
3983 |
fastFlag = 0; |
|
3984 |
currSize = currSize * 2; |
|
3985 |
} |
|
3986 |
||
3987 |
if (!fastFlag) { |
|
3988 |
/* |
|
3989 |
* old buffer may have moved (interrupt while reading) |
|
3990 |
* - refetch pointer |
|
3991 |
*/ |
|
3992 |
bufferPtr = __stringVal(buffer); |
|
3993 |
} |
|
3994 |
||
3995 |
/* |
|
3996 |
* filter out cr-nl; make it nl |
|
3997 |
*/ |
|
3998 |
if (c == '\n') { |
|
3999 |
haveNL = 1; |
|
4000 |
if (index > 0) { |
|
4001 |
if (bufferPtr[index-1] == '\r') { |
|
4002 |
index--; |
|
4003 |
} |
|
4004 |
} |
|
4005 |
} else { |
|
4006 |
if (c == '\r') { |
|
4007 |
haveCR = 1; |
|
4008 |
} |
|
4009 |
} |
|
4010 |
bufferPtr[index++] = c; |
|
4011 |
if (ret <= 0) { |
|
4012 |
if (ret == 0) |
|
4013 |
break; /* End of chunk reached */ |
|
4014 |
goto err; |
|
4015 |
} |
|
4016 |
nread++; |
|
4017 |
atBeginOfLine = (c == '\n'); |
|
4018 |
c = peekC; |
|
4019 |
} |
|
4020 |
||
4021 |
/* |
|
4022 |
* care for CR-limited files. |
|
4023 |
* Replace CR's by NL's |
|
4024 |
*/ |
|
4025 |
if (!haveNL && haveCR) { |
|
4026 |
int i; |
|
4027 |
||
4028 |
for (i=index-1; i>=0; i--) { |
|
4029 |
if (bufferPtr[i] == '\r') { |
|
4030 |
bufferPtr[i] = '\n'; |
|
4031 |
} |
|
4032 |
} |
|
4033 |
} |
|
4034 |
||
4035 |
/* |
|
4036 |
* make it a string |
|
4037 |
* be careful here - allocating a new string may make the |
|
4038 |
* existing buffer move around. Need to check if copying from |
|
4039 |
* fast (C) buffer or from real (ST) buffer. |
|
4040 |
*/ |
|
4041 |
if (fastFlag) { |
|
4042 |
retVal = __MKSTRING_L(bufferPtr, index); |
|
4043 |
} else { |
|
4044 |
retVal = __MKSTRING_ST_L(buffer, index); |
|
4045 |
} |
|
4046 |
||
4047 |
err: ; |
|
4048 |
if (ret <= 0) { |
|
4049 |
if (ret == 0) |
|
4050 |
__INST(hitEOF) = true; |
|
4051 |
else /* ret < 0 */ |
|
4052 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4053 |
} |
|
4054 |
if (__INST(position) != nil) { |
|
4055 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + nread); |
|
4056 |
} |
|
4057 |
%}. |
|
4058 |
retVal isNil ifTrue:[ |
|
4059 |
"/ |
|
4060 |
"/ arrive here with retVal==nil either on error or premature EOF |
|
4061 |
"/ or if running out of malloc-memory |
|
4062 |
"/ |
|
4063 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4064 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4065 |
outOfMemory == true ifTrue:[ |
|
4066 |
" |
|
4067 |
buffer memory allocation failed. |
|
4068 |
When we arrive here, there was no memory available for the |
|
4069 |
chunk. (seems to be too big of a chunk ...) |
|
4070 |
Bad luck - you should increase the ulimit and/or swap space on your machine. |
|
4071 |
" |
|
4072 |
^ ObjectMemory allocationFailureSignal raise. |
|
4073 |
] |
|
4074 |
]. |
|
4075 |
^ retVal |
|
4076 |
! |
|
4077 |
||
4078 |
nextMatchFor:anObject |
|
4079 |
"skip all objects up-to and including anObject, return anObject on success, |
|
4080 |
nil if end-of-file is reached before. The next read operation will return |
|
4081 |
the element after anObject. |
|
4082 |
Only single byte characters are currently supported." |
|
4083 |
||
4084 |
%{ |
|
4085 |
FILEPOINTER f; |
|
4086 |
int ret, peekValue; |
|
4087 |
char c; |
|
4088 |
OBJ fp; |
|
4089 |
||
4090 |
__INST(lastErrorNumber) = nil; |
|
4091 |
if (((fp = __INST(filePointer)) != nil) |
|
4092 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4093 |
) { |
|
4094 |
if ((__INST(binary) == true) && __isSmallInteger(anObject)) { |
|
4095 |
peekValue = __intVal(anObject) & 0xFF; |
|
4096 |
} else { |
|
4097 |
if ((__INST(binary) != true) && __isCharacter(anObject)) { |
|
4098 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
4099 |
} else { |
|
4100 |
peekValue = -1; |
|
4101 |
} |
|
4102 |
} |
|
4103 |
||
4104 |
if (peekValue >= 0) { |
|
4105 |
int _buffered; |
|
4106 |
||
4107 |
__INST(position) = nil; |
|
4108 |
f = __FILEVal(fp); |
|
4109 |
||
4110 |
if (_buffered = (__INST(buffered) == true)) { |
|
4111 |
__READING__(f) |
|
4112 |
} |
|
4113 |
||
4114 |
for (;;) { |
|
4115 |
__READBYTE__(ret, f, &c, _buffered); |
|
4116 |
if (ret <= 0) { |
|
4117 |
if (ret < 0) |
|
4118 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4119 |
else |
|
4120 |
__INST(hitEOF) = true; |
|
4121 |
break; |
|
4122 |
} |
|
4123 |
if (c == peekValue) { |
|
4124 |
RETURN (anObject); |
|
4125 |
} |
|
4126 |
} |
|
4127 |
} |
|
4128 |
} |
|
4129 |
%}. |
|
4130 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4131 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4132 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4133 |
^ super nextMatchFor:anObject |
|
4134 |
! |
|
4135 |
||
4136 |
peekFor:anObject |
|
4137 |
"return true and move past next element, if next == something. |
|
4138 |
Otherwise, stay and return false. False is also returned |
|
4139 |
when EOF is encountered. |
|
4140 |
The argument must be an integer if in binary, a character if in |
|
4141 |
text mode; only single byte characters are currently supported." |
|
4142 |
||
4143 |
%{ |
|
4144 |
FILEPOINTER f; |
|
4145 |
unsigned char c; |
|
4146 |
int ret, peekValue, _buffered; |
|
4147 |
OBJ fp; |
|
4148 |
||
4149 |
__INST(lastErrorNumber) = nil; |
|
4150 |
if (((fp = __INST(filePointer)) != nil) |
|
4151 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4152 |
) { |
|
4153 |
if (__INST(binary) == true) { |
|
4154 |
if (__isSmallInteger(anObject)) { |
|
4155 |
peekValue = __intVal(anObject) & 0xFF; |
|
4156 |
} else { |
|
4157 |
goto bad; |
|
4158 |
} |
|
4159 |
} else { |
|
4160 |
if (__isCharacter(anObject)) { |
|
4161 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
4162 |
} else { |
|
4163 |
goto bad; |
|
4164 |
} |
|
4165 |
} |
|
4166 |
||
4167 |
f = __FILEVal(fp); |
|
4168 |
||
4169 |
_buffered = (__INST(buffered) == true); |
|
4170 |
if (_buffered) { |
|
4171 |
__READING__(f) |
|
4172 |
} |
|
4173 |
__READBYTE__(ret, f, &c, _buffered); |
|
4174 |
if (ret > 0) { |
|
4175 |
if (c == peekValue) { |
|
4176 |
OBJ pos; |
|
4177 |
||
4178 |
if ((pos = __INST(position)) != nil) { |
|
4179 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
4180 |
} |
|
4181 |
RETURN (true); |
|
4182 |
} |
|
4183 |
__UNGETC__(c, f, _buffered); |
|
4184 |
RETURN (false); |
|
4185 |
} |
|
4186 |
if (ret < 0) { |
|
4187 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4188 |
} else /* ret == 0 */ |
|
4189 |
__INST(hitEOF) = true; |
|
4190 |
} |
|
4191 |
bad: ; |
|
4192 |
%}. |
|
4193 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4194 |
hitEOF ifTrue:[self pastEnd. ^ false]. |
|
4195 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4196 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4197 |
^ super peekFor:anObject |
|
4198 |
! |
|
4199 |
||
4200 |
skipLine |
|
4201 |
"read the next line (characters up to newline) skip only; |
|
4202 |
return nil if EOF reached, self otherwise. |
|
4203 |
Not allowed in binary mode." |
|
4204 |
||
4205 |
%{ /* STACK:2000 */ |
|
4206 |
||
4207 |
FILEPOINTER f; |
|
4208 |
OBJ fp; |
|
4209 |
int ret, _buffered; |
|
4210 |
char c; |
|
4211 |
||
4212 |
__INST(lastErrorNumber) = nil; |
|
4213 |
if (((fp = __INST(filePointer)) != nil) |
|
4214 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4215 |
&& (__INST(binary) != true) |
|
4216 |
) { |
|
4217 |
f = __FILEVal(fp); |
|
4218 |
if (_buffered = (__INST(buffered) == true)) { |
|
4219 |
__READING__(f) |
|
4220 |
} |
|
4221 |
for (;;) { |
|
4222 |
__READBYTE__(ret, f, &c, _buffered); |
|
4223 |
if (ret > 0) { |
|
4224 |
if (c == '\n') |
|
4225 |
RETURN(self) |
|
4226 |
} else |
|
4227 |
break; |
|
4228 |
} |
|
4229 |
if (ret < 0) { |
|
4230 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4231 |
} else /* ret == 0 */ { |
|
4232 |
__INST(hitEOF) = true; |
|
4233 |
} |
|
4234 |
} |
|
4235 |
%}. |
|
4236 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4237 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
|
4238 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4239 |
binary ifTrue:[self errorBinary. ^ self]. |
|
4240 |
self errorWriteOnly |
|
4241 |
! |
|
4242 |
||
4243 |
skipSeparators |
|
4244 |
"skip all whitespace; next will return next non-white-space character |
|
4245 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
4246 |
- reimplemented for speed" |
|
4247 |
||
4248 |
%{ |
|
4249 |
FILEPOINTER f; |
|
4250 |
char c; |
|
4251 |
int ret, _buffered; |
|
4252 |
OBJ fp; |
|
4253 |
||
4254 |
__INST(lastErrorNumber) = nil; |
|
4255 |
if (((fp = __INST(filePointer)) != nil) |
|
4256 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4257 |
&& (__INST(binary) != true) |
|
4258 |
) { |
|
4259 |
f = __FILEVal(fp); |
|
4260 |
_buffered = (__INST(buffered) == true); |
|
4261 |
if (_buffered) { |
|
4262 |
__READING__(f) |
|
4263 |
} |
|
4264 |
||
4265 |
while (1) { |
|
4266 |
__READBYTE__(ret, f, &c, _buffered); |
|
4267 |
if (ret <= 0) { |
|
4268 |
if (ret < 0) { |
|
4269 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4270 |
} else /* ret == 0 */ { |
|
4271 |
__INST(hitEOF) = true; |
|
4272 |
} |
|
4273 |
break; |
|
4274 |
} |
|
4275 |
switch (c) { |
|
4276 |
case ' ': |
|
4277 |
case '\t': |
|
4278 |
case '\n': |
|
4279 |
case '\r': |
|
4280 |
case '\b': |
|
4281 |
case '\f': |
|
4282 |
break; |
|
4283 |
||
4284 |
default: |
|
4285 |
__UNGETC__(c, f, _buffered); |
|
4286 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4287 |
} |
|
4288 |
} |
|
4289 |
} |
|
4290 |
%}. |
|
4291 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4292 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4293 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4294 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
4295 |
^ self errorBinary. |
|
4296 |
! |
|
4297 |
||
4298 |
skipSeparatorsExceptCR |
|
4299 |
"skip all whitespace but no newlines; |
|
4300 |
next will return next non-white-space character |
|
4301 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
4302 |
- reimplemented for speed" |
|
4303 |
||
4304 |
%{ |
|
4305 |
FILEPOINTER f; |
|
4306 |
char c; |
|
4307 |
int ret, _buffered; |
|
4308 |
OBJ fp; |
|
4309 |
||
4310 |
__INST(lastErrorNumber) = nil; |
|
4311 |
if (((fp = __INST(filePointer)) != nil) |
|
4312 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4313 |
&& (__INST(binary) != true) |
|
4314 |
) { |
|
4315 |
f = __FILEVal(fp); |
|
4316 |
_buffered = (__INST(buffered) == true); |
|
4317 |
if (_buffered) { |
|
4318 |
__READING__(f) |
|
4319 |
} |
|
4320 |
while (1) { |
|
4321 |
__READBYTE__(ret, f, &c, _buffered); |
|
4322 |
if (ret <= 0) { |
|
4323 |
if (ret < 0) { |
|
4324 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4325 |
} else /* ret == 0 */ { |
|
4326 |
__INST(hitEOF) = true; |
|
4327 |
} |
|
4328 |
break; |
|
4329 |
} |
|
4330 |
switch (c) { |
|
4331 |
case ' ': |
|
4332 |
case '\t': |
|
4333 |
case '\b': |
|
4334 |
case '\f': |
|
4335 |
break; |
|
4336 |
||
4337 |
default: |
|
4338 |
__UNGETC__(c, f, _buffered); |
|
4339 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4340 |
} |
|
4341 |
} |
|
4342 |
} |
|
4343 |
%}. |
|
4344 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4345 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4346 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4347 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
4348 |
^ self errorBinary. |
|
4349 |
! |
|
4350 |
||
4351 |
skipThrough:aCharacter |
|
4352 |
"skip all characters up-to and including aCharacter. Return the receiver if |
|
4353 |
skip was successful, otherwise (i.e. if not found) return nil. |
|
4354 |
The next read operation will return the character after aCharacter. |
|
4355 |
The argument, aCharacter must be character, or integer when in binary mode. |
|
4356 |
Only single byte characters are currently supported." |
|
4357 |
||
4358 |
%{ |
|
4359 |
FILEPOINTER f; |
|
4360 |
REGISTER int cSearch; |
|
4361 |
unsigned char c; |
|
4362 |
int ret, _buffered; |
|
4363 |
OBJ fp; |
|
4364 |
||
4365 |
__INST(lastErrorNumber) = nil; |
|
4366 |
if (((fp = __INST(filePointer)) != nil) |
|
4367 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4368 |
) { |
|
4369 |
if (__INST(binary) == true) { |
|
4370 |
/* searched for object must be a smallInteger */ |
|
4371 |
if (! __isSmallInteger(aCharacter)) goto badArgument; |
|
4372 |
cSearch = __intVal(aCharacter); |
|
4373 |
} else { |
|
4374 |
/* searched for object must be a character */ |
|
4375 |
if (! __isCharacter(aCharacter)) goto badArgument; |
|
4376 |
cSearch = __intVal(_characterVal(aCharacter)); |
|
4377 |
} |
|
4378 |
/* Q: should we just say: "not found" ? */ |
|
4379 |
if ((cSearch < 0) || (cSearch > 255)) goto badArgument; |
|
4380 |
||
4381 |
f = __FILEVal(fp); |
|
4382 |
if (_buffered = (__INST(buffered) == true)) { |
|
4383 |
__READING__(f) |
|
4384 |
} |
|
4385 |
||
4386 |
while (1) { |
|
4387 |
__READBYTE__(ret, f, &c, _buffered); |
|
4388 |
if (ret <= 0) { |
|
4389 |
if (ret < 0) { |
|
4390 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4391 |
} else /* ret == 0 */ { |
|
4392 |
__INST(hitEOF) = true; |
|
4393 |
} |
|
4394 |
break; |
|
4395 |
} |
|
4396 |
if (c == cSearch) { |
|
4397 |
RETURN (self); |
|
4398 |
} |
|
4399 |
} |
|
4400 |
} |
|
4401 |
badArgument: ; |
|
4402 |
%}. |
|
4403 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4404 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
|
4405 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4406 |
(mode == #writeonly) ifTrue:[self errorWriteOnly. ^ self]. |
|
4407 |
" |
|
4408 |
argument must be integer/character in binary mode, |
|
4409 |
character in text mode |
|
4410 |
" |
|
4411 |
self error:'invalid argument'. |
|
4412 |
||
4413 |
" |
|
4414 |
|s| |
|
4415 |
s := 'Makefile' asFilename readStream. |
|
4416 |
s skipThrough:$=. |
|
4417 |
s next:10 |
|
4418 |
" |
|
4419 |
! |
|
4420 |
||
4421 |
skipToAll:aString |
|
4422 |
"skip for the sequence given by the argument, aCollection; |
|
4423 |
return nil if not found, self otherwise. |
|
4424 |
On a successful match, the next read will return characters of aString." |
|
4425 |
||
4426 |
|oldPos| |
|
4427 |
||
4428 |
oldPos := self position. |
|
4429 |
(self skipThroughAll:aString) isNil ifTrue:[ |
|
4430 |
" |
|
4431 |
restore position |
|
4432 |
" |
|
4433 |
self position:oldPos. |
|
4434 |
^ nil |
|
4435 |
]. |
|
4436 |
" |
|
4437 |
position before match-string |
|
4438 |
" |
|
4439 |
self position:(self position - aString size). |
|
4440 |
^ self |
|
4441 |
||
4442 |
" |
|
4443 |
|s| |
|
4444 |
s := 'Makefile' asFilename readStream. |
|
4445 |
s skipToAll:'are'. |
|
4446 |
s next:10 |
|
4447 |
" |
|
4448 |
||
4449 |
"Modified: 26.6.1996 / 09:22:05 / cg" |
|
4450 |
! ! |
|
4451 |
||
4452 |
!ExternalStream methodsFor:'testing'! |
|
4453 |
||
4454 |
atEnd |
|
4455 |
"return true, if position is at end" |
|
4456 |
||
4457 |
%{ |
|
4458 |
FILEPOINTER f; |
|
4459 |
OBJ fp; |
|
4460 |
char c; |
|
4461 |
int ret, _buffered; |
|
4462 |
||
4463 |
if (__INST(hitEOF) == true) { |
|
4464 |
RETURN (true); |
|
4465 |
} |
|
4466 |
||
3907 | 4467 |
if (__bothSmallInteger(__INST(position), __INST(readLimit))) { |
4468 |
if (__intVal(__INST(position)) > __intVal(__INST(readLimit))) { |
|
4469 |
RETURN (true); |
|
4470 |
} |
|
4471 |
} |
|
3746 | 4472 |
__INST(lastErrorNumber) = nil; |
4473 |
||
4474 |
if ((fp = __INST(filePointer)) != nil) { |
|
4475 |
f = __FILEVal(fp); |
|
4476 |
||
4477 |
if (_buffered = (__INST(buffered) == true)) { |
|
4478 |
__READING__(f); |
|
4479 |
} else { |
|
4480 |
if (__INST(readAhead) != nil) { |
|
4481 |
RETURN (false); |
|
4482 |
} |
|
4483 |
} |
|
4484 |
||
4485 |
/* |
|
4486 |
* read ahead ... |
|
4487 |
*/ |
|
4488 |
do { |
|
4489 |
#ifdef NO_STDIO |
|
3823 | 4490 |
errno = 0; |
3746 | 4491 |
#else |
4492 |
__BEGIN_INTERRUPTABLE__ |
|
4493 |
#endif |
|
4494 |
__READBYTE__(ret, f, &c, _buffered); |
|
4495 |
#ifndef NO_STDIO |
|
4496 |
__END_INTERRUPTABLE__ |
|
4497 |
#endif |
|
4498 |
} while ((ret < 0) && (errno == EINTR)); |
|
4499 |
||
4500 |
#if defined(WIN32) && defined(EPIPE) |
|
4501 |
if ((ret < 0) && (errno == EPIPE)) |
|
4502 |
{ |
|
4503 |
ret = 0; |
|
4504 |
errno = 0; |
|
4505 |
} |
|
4506 |
#endif |
|
4507 |
if (ret > 0) { |
|
4508 |
__UNGETC__(c&0xff, f, _buffered); |
|
4509 |
RETURN (false); |
|
4510 |
} |
|
4511 |
||
4512 |
if (ret == 0) { |
|
4513 |
__INST(hitEOF) = true; |
|
4514 |
RETURN (true); |
|
4515 |
} |
|
4516 |
||
4517 |
/* ret < 0 */ |
|
4518 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4519 |
} |
|
4520 |
%}. |
|
4521 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4522 |
^ self errorNotOpen |
|
4523 |
! |
|
4524 |
||
4525 |
canReadWithoutBlocking |
|
4526 |
"return true, if any data is available for reading (i.e. |
|
4527 |
a read operation will not block the smalltalk process), false otherwise." |
|
4528 |
||
4529 |
|fd| |
|
4530 |
||
4531 |
readAhead notNil ifTrue:[^ true]. |
|
4532 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4533 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4534 |
||
4535 |
fd := self fileDescriptor. |
|
4536 |
^ OperatingSystem readCheck:fd |
|
4537 |
||
4538 |
" |
|
4539 |
|pipe| |
|
4540 |
||
4541 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
4542 |
pipe canReadWithoutBlocking ifTrue:[ |
|
4543 |
Transcript showCR:'data available' |
|
4544 |
] ifFalse:[ |
|
4545 |
Transcript showCR:'no data available' |
|
4546 |
]. |
|
4547 |
pipe close |
|
4548 |
" |
|
4549 |
||
4550 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
|
4551 |
! |
|
4552 |
||
4553 |
canWriteWithoutBlocking |
|
4554 |
"return true, if data can be written into the stream |
|
4555 |
(i.e. a write operation will not block the smalltalk process)." |
|
4556 |
||
4557 |
|fd| |
|
4558 |
||
4559 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4560 |
mode == #readonly ifTrue:[^ self errorReadOnly]. |
|
4561 |
||
4562 |
fd := self fileDescriptor. |
|
4563 |
^ OperatingSystem writeCheck:fd |
|
4564 |
! |
|
4565 |
||
4566 |
numAvailable |
|
4567 |
"return the number of bytes available for reading" |
|
4568 |
||
4569 |
|fd| |
|
4570 |
||
4571 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4572 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4573 |
||
4574 |
fd := self fileDescriptor. |
|
4575 |
^ OperatingSystem numAvailableForReadOn:fd. |
|
4576 |
||
4577 |
"/ self atEnd ifTrue:[^ 0]. |
|
4578 |
"/ self canReadWithoutBlocking ifTrue:[ |
|
4579 |
"/ ^ 1 |
|
4580 |
"/ ]. |
|
4581 |
"/ ^ 0 |
|
4582 |
||
4583 |
"Created: / 4.2.1998 / 16:56:01 / cg" |
|
4584 |
"Modified: / 4.2.1998 / 17:31:11 / cg" |
|
4585 |
! ! |
|
4586 |
||
4587 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
4588 |
||
4589 |
readWait |
|
4590 |
"suspend the current process, until the receiver |
|
4591 |
becomes ready for reading. If data is already available, |
|
4592 |
return immediate. |
|
4593 |
The other threads are not affected by the wait." |
|
4594 |
||
4595 |
self readWaitWithTimeoutMs:nil |
|
4596 |
! |
|
4597 |
||
4598 |
readWaitTimeoutMs:timeout |
|
4599 |
"ST-80 compatibility" |
|
4600 |
^ self readWaitWithTimeoutMs:timeout |
|
4601 |
! |
|
4602 |
||
4603 |
readWaitWithTimeout:timeout |
|
4604 |
"suspend the current process, until the receiver |
|
4605 |
becomes ready for reading or a timeout (in seconds) expired. |
|
4606 |
If data is already available, return immediate. |
|
4607 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4608 |
The other threads are not affected by the wait." |
|
4609 |
||
4610 |
^ self readWaitWithTimeoutMs:timeout * 1000 |
|
4611 |
! |
|
4612 |
||
4613 |
readWaitWithTimeoutMs:timeout |
|
4614 |
"suspend the current process, until the receiver |
|
4615 |
becomes ready for reading or a timeout (in milliseconds) expired. |
|
4616 |
If data is already available, return immediate. |
|
4617 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4618 |
The other threads are not affected by the wait." |
|
4619 |
||
4620 |
|fd inputSema hasData wasBlocked| |
|
4621 |
||
4622 |
readAhead notNil ifTrue:[^ false]. |
|
4623 |
||
4624 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4625 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4626 |
||
4627 |
fd := self fileDescriptor. |
|
4628 |
(OperatingSystem readCheck:fd) ifTrue:[^ false]. |
|
4629 |
||
4630 |
wasBlocked := OperatingSystem blockInterrupts. |
|
4631 |
hasData := OperatingSystem readCheck:fd. |
|
4632 |
hasData ifFalse:[ |
|
4633 |
inputSema := Semaphore new name:'readWait'. |
|
4634 |
[ |
|
4635 |
timeout notNil ifTrue:[ |
|
4636 |
Processor signal:inputSema afterMilliseconds:timeout |
|
4637 |
]. |
|
4638 |
Processor signal:inputSema onInput:fd. |
|
4639 |
Processor activeProcess state:#ioWait. |
|
4640 |
inputSema wait. |
|
4641 |
Processor disableSemaphore:inputSema. |
|
4642 |
hasData := OperatingSystem readCheck:fd |
|
4643 |
] valueOnUnwindDo:[ |
|
4644 |
Processor disableSemaphore:inputSema. |
|
4645 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4646 |
] |
|
4647 |
]. |
|
4648 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4649 |
^ hasData not |
|
4650 |
! |
|
4651 |
||
4652 |
readWriteWait |
|
4653 |
"suspend the current process, until the receiver |
|
4654 |
becomes ready for writing or reading. |
|
4655 |
Return immediate if the receiver is already ready. |
|
4656 |
The other threads are not affected by the wait. |
|
4657 |
This is actually only used with sockets, to wait for a connect to |
|
4658 |
be finished." |
|
4659 |
||
4660 |
self readWriteWaitWithTimeoutMs:nil |
|
4661 |
! |
|
4662 |
||
4663 |
readWriteWaitWithTimeoutMs:timeout |
|
4664 |
"suspend the current process, until the receiver |
|
4665 |
becomes ready for reading or writing or a timeout (in seconds) expired. |
|
4666 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4667 |
Return immediate if the receiver is already ready. |
|
4668 |
The other threads are not affected by the wait. |
|
4669 |
This is actually only used with sockets, to wait for a connect to |
|
4670 |
be finished." |
|
4671 |
||
4672 |
|fd sema canread canReadWrite wasBlocked| |
|
4673 |
||
4674 |
filePointer isNil ifTrue:[ |
|
4675 |
^ self errorNotOpen |
|
4676 |
]. |
|
4677 |
||
4678 |
fd := self fileDescriptor. |
|
4679 |
(OperatingSystem readWriteCheck:fd) ifTrue:[^ false]. |
|
4680 |
||
4681 |
wasBlocked := OperatingSystem blockInterrupts. |
|
4682 |
canReadWrite := OperatingSystem readWriteCheck:fd. |
|
4683 |
canReadWrite ifFalse:[ |
|
4684 |
sema := Semaphore new name:'readWriteWait'. |
|
4685 |
[ |
|
4686 |
timeout notNil ifTrue:[ |
|
4687 |
Processor signal:sema afterMilliseconds:timeout |
|
4688 |
]. |
|
4689 |
Processor signal:sema onOutput:fd. |
|
4690 |
Processor signal:sema onInput:fd. |
|
4691 |
Processor activeProcess state:#ioWait. |
|
4692 |
sema wait. |
|
4693 |
Processor disableSemaphore:sema. |
|
4694 |
canReadWrite := OperatingSystem readWriteCheck:fd |
|
4695 |
] valueOnUnwindDo:[ |
|
4696 |
Processor disableSemaphore:sema. |
|
4697 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4698 |
] |
|
4699 |
]. |
|
4700 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4701 |
^ canReadWrite not |
|
4702 |
! |
|
4703 |
||
4704 |
writeWait |
|
4705 |
"suspend the current process, until the receiver |
|
4706 |
becomes ready for writing. |
|
4707 |
Return immediate if the receiver is already ready. |
|
4708 |
The other threads are not affected by the wait." |
|
4709 |
||
4710 |
self writeWaitWithTimeoutMs:nil |
|
4711 |
! |
|
4712 |
||
4713 |
writeWaitTimeoutMs:timeout |
|
4714 |
"ST-80 compatibility" |
|
4715 |
^ self writeWaitWithTimeoutMs:timeout |
|
4716 |
! |
|
4717 |
||
4718 |
writeWaitWithTimeout:timeout |
|
4719 |
"suspend the current process, until the receiver |
|
4720 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4721 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4722 |
Return immediate if the receiver is already ready. |
|
4723 |
The other threads are not affected by the wait." |
|
4724 |
||
4725 |
^ self writeWaitWithTimeoutMs:timeout * 1000 |
|
4726 |
! |
|
4727 |
||
4728 |
writeWaitWithTimeoutMs:timeout |
|
4729 |
"suspend the current process, until the receiver |
|
4730 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4731 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4732 |
Return immediate if the receiver is already ready. |
|
4733 |
The other threads are not affected by the wait." |
|
4734 |
||
4735 |
|fd outputSema canWrite wasBlocked| |
|
4736 |
||
4737 |
filePointer isNil ifTrue:[ |
|
4738 |
^ self errorNotOpen |
|
4739 |
]. |
|
4740 |
mode == #readonly ifTrue:[ |
|
4741 |
^ self errorReadOnly |
|
4742 |
]. |
|
4743 |
||
4744 |
fd := self fileDescriptor. |
|
4745 |
(OperatingSystem writeCheck:fd) ifTrue:[^ false]. |
|
4746 |
||
4747 |
wasBlocked := OperatingSystem blockInterrupts. |
|
4748 |
canWrite := OperatingSystem writeCheck:fd. |
|
4749 |
canWrite ifFalse:[ |
|
4750 |
outputSema := Semaphore new name:'writeWait'. |
|
4751 |
[ |
|
4752 |
timeout notNil ifTrue:[ |
|
4753 |
Processor signal:outputSema afterMilliseconds:timeout |
|
4754 |
]. |
|
4755 |
Processor signal:outputSema onOutput:fd. |
|
4756 |
Processor activeProcess state:#ioWait. |
|
4757 |
outputSema wait. |
|
4758 |
Processor disableSemaphore:outputSema. |
|
4759 |
canWrite := OperatingSystem writeCheck:fd |
|
4760 |
] valueOnUnwindDo:[ |
|
4761 |
Processor disableSemaphore:outputSema. |
|
4762 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4763 |
] |
|
4764 |
]. |
|
4765 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4766 |
^ canWrite not |
|
4767 |
! ! |
|
4768 |
||
4769 |
!ExternalStream methodsFor:'writing'! |
|
4770 |
||
4771 |
cr |
|
4772 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
4773 |
reimplemented for speed" |
|
4774 |
||
4775 |
%{ |
|
4776 |
FILEPOINTER f; |
|
4777 |
int len, cnt, _buffered; |
|
4778 |
OBJ fp; |
|
4779 |
char *cp; |
|
4780 |
||
4781 |
__INST(lastErrorNumber) = nil; |
|
4782 |
if (((fp = __INST(filePointer)) != nil) |
|
4783 |
&& (__INST(mode) != @symbol(readonly)) |
|
4784 |
&& (__INST(binary) != true) |
|
4785 |
) { |
|
4786 |
f = __FILEVal(fp); |
|
4787 |
||
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4788 |
if (_buffered = (__INST(buffered) == true)) { |
3746 | 4789 |
__WRITING__(f) |
4790 |
} |
|
4791 |
{ |
|
4792 |
OBJ mode = __INST(eolMode); |
|
4793 |
||
3823 | 4794 |
len = 1; |
3746 | 4795 |
if (mode == @symbol(cr)) { |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4796 |
cp = "\r"; |
3746 | 4797 |
} else if (mode == @symbol(crlf)) { |
4798 |
cp = "\r\n"; len = 2; |
|
4799 |
} else { |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4800 |
cp = "\n"; |
3746 | 4801 |
} |
4802 |
} |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4803 |
if ((f == stdout) || (f == stderr)) { |
4206 | 4804 |
cnt = fwrite(cp, 1, len, f); |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4805 |
} else { |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4806 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4807 |
} |
3746 | 4808 |
|
4809 |
if (cnt == len) { |
|
4810 |
if (__INST(position) != nil) { |
|
4811 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + len); |
|
4812 |
} |
|
4813 |
RETURN ( self ); |
|
4814 |
} |
|
4815 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4816 |
} |
|
4817 |
%}. |
|
4818 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
4819 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4820 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4821 |
self errorBinary |
|
4822 |
! |
|
4823 |
||
4824 |
flush |
|
4825 |
"write all buffered data - ignored if unbuffered" |
|
4826 |
||
4827 |
%{ |
|
4828 |
OBJ fp; |
|
4829 |
||
4830 |
__INST(lastErrorNumber) = nil; |
|
4831 |
if ((fp = __INST(filePointer)) != nil) { |
|
4832 |
if (__INST(mode) != @symbol(readonly)) { |
|
4833 |
if (__INST(buffered) == true) { |
|
3823 | 4834 |
FILE *f = __FILEVal(fp); |
4835 |
||
4836 |
if ((f == stdout) || (f == stderr)) { |
|
4206 | 4837 |
fflush(f); |
3823 | 4838 |
} else { |
3746 | 4839 |
#ifdef NO_STDIO |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4840 |
FFLUSH(f); |
3746 | 4841 |
#else |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4842 |
__BEGIN_INTERRUPTABLE__ |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4843 |
FFLUSH(f); |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4844 |
__END_INTERRUPTABLE__ |
3746 | 4845 |
#endif |
3823 | 4846 |
} |
3746 | 4847 |
} |
4848 |
} |
|
4849 |
} |
|
4850 |
%} |
|
4851 |
! |
|
4852 |
||
4853 |
nextPut:aCharacter |
|
4854 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
4855 |
Only single-byte characters are currently supported" |
|
4856 |
||
4857 |
%{ |
|
4858 |
FILEPOINTER f; |
|
4859 |
char c; |
|
4860 |
int cnt, _buffered; |
|
4861 |
OBJ pos, fp; |
|
4862 |
||
4863 |
__INST(lastErrorNumber) = nil; |
|
4864 |
if (((fp = __INST(filePointer)) != nil) |
|
4865 |
&& (__INST(mode) != @symbol(readonly)) |
|
4866 |
) { |
|
4867 |
if (__INST(binary) != true) { |
|
4868 |
if (__isCharacter(aCharacter)) { |
|
4869 |
c = __intVal(__characterVal(aCharacter)) & 0xFF; |
|
4870 |
doWrite: |
|
4871 |
f = __FILEVal(fp); |
|
4872 |
||
4873 |
||
4874 |
if (_buffered = (__INST(buffered) == true)) { |
|
4875 |
__WRITING__(f) |
|
4876 |
} |
|
3823 | 4877 |
if ((f == stdout) || (f == stderr)) { |
4206 | 4878 |
cnt = fwrite(&c, 1, 1, f); |
3823 | 4879 |
} else { |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4880 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
3823 | 4881 |
} |
3746 | 4882 |
|
4883 |
if (cnt == 1) { |
|
4884 |
pos = __INST(position); |
|
4885 |
if (pos != nil) { |
|
4886 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
4887 |
} |
|
4888 |
RETURN ( self ); |
|
4889 |
} |
|
4890 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4891 |
} |
|
4892 |
} else { |
|
4893 |
if (__isSmallInteger(aCharacter)) { |
|
4894 |
c = __intVal(aCharacter); |
|
4895 |
goto doWrite; |
|
4896 |
} |
|
4897 |
} |
|
4898 |
} |
|
4899 |
%}. |
|
4900 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
4901 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4902 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4903 |
binary ifFalse:[self argumentMustBeCharacter. ^ self]. |
|
4904 |
self argumentMustBeInteger. |
|
4905 |
! |
|
4906 |
||
4907 |
nextPutAll:aCollection |
|
4908 |
"write all elements of the argument, aCollection. |
|
4909 |
Reimplemented for speed when writing strings or byteArrays. |
|
4910 |
For others, falls back to general method in superclass." |
|
4911 |
||
4912 |
%{ |
|
4913 |
FILEPOINTER f; |
|
4914 |
int len, cnt; |
|
4915 |
OBJ pos, fp; |
|
4916 |
int _buffered; |
|
4917 |
int o_offs; |
|
4918 |
||
4919 |
__INST(lastErrorNumber) = nil; |
|
4920 |
||
4921 |
if (((fp = __INST(filePointer)) != nil) |
|
4922 |
&& (__INST(mode) != @symbol(readonly)) |
|
4923 |
) { |
|
4924 |
f = __FILEVal(fp); |
|
4925 |
if (_buffered = (__INST(buffered) == true)) { |
|
4926 |
__WRITING__(f) |
|
4927 |
} |
|
4928 |
||
4929 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
4930 |
len = __stringSize(aCollection); |
|
3823 | 4931 |
if ((f == stdout) || (f == stderr)) { |
4206 | 4932 |
cnt = fwrite(__stringVal(aCollection), 1, len, f); |
3823 | 4933 |
} else { |
4934 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
4935 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
|
4936 |
} |
|
3746 | 4937 |
} else { |
4938 |
if (__INST(binary) == true) { |
|
4939 |
int offs; |
|
4940 |
||
4941 |
if (__isByteArray(aCollection)) { |
|
4942 |
offs = 0; |
|
4943 |
len = __byteArraySize(aCollection); |
|
4944 |
} else if (__isBytes(aCollection)) { |
|
4945 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
4946 |
len = __byteArraySize(aCollection) - offs; |
|
4947 |
} else |
|
4948 |
goto out; |
|
4949 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection); |
|
4950 |
o_offs += offs; |
|
4951 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
|
4952 |
} else |
|
4953 |
goto out; |
|
4954 |
} |
|
4955 |
||
4956 |
if (cnt == len) { |
|
4957 |
pos = __INST(position); |
|
4958 |
if (pos != nil) { |
|
4959 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
4960 |
} |
|
4961 |
RETURN (self); |
|
4962 |
} |
|
4963 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
4964 |
} |
|
4965 |
out: ; |
|
4966 |
%}. |
|
4967 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
4968 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4969 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4970 |
||
4971 |
^ super nextPutAll:aCollection |
|
4972 |
! |
|
4973 |
||
4974 |
nextPutAll:aCollection startingAt:start to:stop |
|
4975 |
"write a range of elements of the argument, aCollection. |
|
4976 |
Reimplemented for speed when writing strings or byteArrays. |
|
4977 |
For others, falls back to general method in superclass." |
|
4978 |
||
4979 |
%{ |
|
4980 |
FILEPOINTER f; |
|
4981 |
int offs, len, cnt, iStart, iStop, _buffered; |
|
4982 |
OBJ fp, pos; |
|
4983 |
int o_offs; |
|
4984 |
||
4985 |
__INST(lastErrorNumber) = nil; |
|
4986 |
if (((fp = __INST(filePointer)) != nil) |
|
4987 |
&& (__INST(mode) != @symbol(readonly)) |
|
4988 |
&& __bothSmallInteger(start, stop) |
|
4989 |
) { |
|
4990 |
f = __FILEVal(fp); |
|
4991 |
if (_buffered = (__INST(buffered) == true)) { |
|
4992 |
__WRITING__(f) |
|
4993 |
} |
|
4994 |
iStart = __intVal(start); |
|
4995 |
iStop = __intVal(stop); |
|
4996 |
if ((iStart < 1) || (iStop < iStart)) { |
|
4997 |
RETURN(self); |
|
4998 |
} |
|
4999 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
5000 |
len = __stringSize(aCollection); |
|
5001 |
if (iStop > len) { |
|
5002 |
RETURN(self); |
|
5003 |
} |
|
5004 |
if (iStop > len) |
|
5005 |
iStop = len; |
|
5006 |
len = iStop - iStart + 1; |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
5007 |
|
3823 | 5008 |
if ((f == stdout) || (f == stderr)) { |
4206 | 5009 |
cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f); |
3823 | 5010 |
} else { |
5011 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
5012 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered); |
|
5013 |
} |
|
3746 | 5014 |
} else { |
5015 |
if (__INST(binary) == true) { |
|
5016 |
int offs; |
|
5017 |
||
5018 |
if (__isByteArray(aCollection)) { |
|
5019 |
offs = 0; |
|
5020 |
len = __byteArraySize(aCollection); |
|
5021 |
} else if (__isBytes(aCollection)) { |
|
5022 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
5023 |
len = __byteArraySize(aCollection) - offs; |
|
5024 |
} else |
|
5025 |
goto out; |
|
5026 |
||
5027 |
if (iStop > len) { |
|
5028 |
RETURN(self); |
|
5029 |
} |
|
5030 |
if (iStop > len) |
|
5031 |
iStop = len; |
|
5032 |
len = iStop - iStart + 1; |
|
5033 |
offs += iStart - 1; |
|
5034 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection); |
|
5035 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+offs, len, _buffered); |
|
5036 |
} else |
|
5037 |
goto out; |
|
5038 |
} |
|
5039 |
if (cnt == len) { |
|
5040 |
pos = __INST(position); |
|
5041 |
if (pos != nil) { |
|
5042 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
5043 |
} |
|
5044 |
RETURN (self); |
|
5045 |
} |
|
5046 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
5047 |
} |
|
5048 |
out: ; |
|
5049 |
%}. |
|
5050 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
5051 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
5052 |
! ! |
|
5053 |
||
5054 |
!ExternalStream class methodsFor:'documentation'! |
|
5055 |
||
5056 |
version |
|
4597
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
5057 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/ExternalStream_win32.st,v 1.14 1999-08-17 10:15:45 cg Exp $' |
3746 | 5058 |
! ! |
5059 |
ExternalStream initialize! |