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