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