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