author | Claus Gittinger <cg@exept.de> |
Thu, 19 Feb 2004 15:13:00 +0100 | |
changeset 7970 | 2ae69eb663eb |
parent 7720 | 677d3292cf08 |
child 8035 | 8bd596f4f90e |
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 |
|
1373 |
"reopen all files (if possible) after a snapShot load" |
|
1374 |
||
1375 |
Lobby do:[:aFileStream | |
|
1376 |
aFileStream reOpen |
|
1377 |
]. |
|
1378 |
! |
|
1379 |
||
1380 |
update:something with:aParameter from:changedObject |
|
1381 |
"have to reopen files when returning from snapshot" |
|
1382 |
||
1383 |
something == #returnFromSnapshot ifTrue:[ |
|
1384 |
self reOpenFiles. |
|
1385 |
self initDefaultEOLMode |
|
1386 |
] |
|
1387 |
||
1388 |
"Created: 15.6.1996 / 15:19:59 / cg" |
|
1389 |
! ! |
|
1390 |
||
1391 |
!ExternalStream class methodsFor:'instance creation'! |
|
1392 |
||
1393 |
forFileDescriptor:aFileDescriptor mode:modeString |
|
1394 |
"given a fileDescriptor, create an ExternalStream object |
|
1395 |
to operate on this fd. |
|
1396 |
The mode-argument is passed to the fdopen call. |
|
1397 |
This may be used to wrap fd's as returned by user |
|
1398 |
primitive code, or to wrap pipe-fds into externalStreams." |
|
1399 |
||
1400 |
|newStream| |
|
1401 |
||
1402 |
newStream := self basicNew. |
|
1403 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
|
1404 |
^ newStream connectTo:aFileDescriptor withMode:modeString |
|
1405 |
||
1406 |
" |
|
1407 |
the example below will probably fail (15 is a random FD): |
|
1408 |
||
1409 |
|s| |
|
1410 |
||
1411 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
|
1412 |
s next. |
|
1413 |
" |
|
1414 |
||
1415 |
"Created: 29.2.1996 / 18:05:00 / cg" |
|
1416 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
|
1417 |
! |
|
1418 |
||
1419 |
forReadWriteToFileDescriptor:aFileDescriptor |
|
1420 |
"given a fileDescriptor, create an ExternalStream object |
|
1421 |
to read/write from/to this fd. This may be used to wrap fd's |
|
1422 |
as returned by user primitive code, or to wrap pipe- |
|
1423 |
filedescriptors into externalStreams." |
|
1424 |
||
1425 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadWriteMode) readwrite |
|
1426 |
||
1427 |
" |
|
1428 |
the example below will probably fail (15 is a random FD): |
|
1429 |
||
1430 |
|s| |
|
1431 |
||
1432 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
|
1433 |
s next. |
|
1434 |
" |
|
1435 |
||
1436 |
"Created: 29.2.1996 / 18:15:08 / cg" |
|
1437 |
"Modified: 29.2.1996 / 18:16:25 / cg" |
|
1438 |
! |
|
1439 |
||
1440 |
forReadingFromFileDescriptor:aFileDescriptor |
|
1441 |
"given a fileDescriptor, create an ExternalStream object |
|
1442 |
to read from this fd. This may be used to wrap fd's |
|
1443 |
as returned by user primitive code, or to wrap pipe- |
|
1444 |
filedescriptors into externalStreams." |
|
1445 |
||
1446 |
^ (self forFileDescriptor:aFileDescriptor mode:ReadMode) readonly |
|
1447 |
||
1448 |
" |
|
1449 |
the example below will probably fail (15 is a random FD): |
|
1450 |
||
1451 |
|s| |
|
1452 |
||
1453 |
s := ExternalStream forReadingFromFileDescriptor:15. |
|
1454 |
s next. |
|
1455 |
" |
|
1456 |
||
1457 |
" |
|
1458 |
|pipe readFd writeFd rs ws| |
|
1459 |
||
1460 |
'create OS pipe ...'. |
|
1461 |
||
1462 |
pipe := OperatingSystem makePipe. |
|
1463 |
readFd := pipe at:1. |
|
1464 |
writeFd := pipe at:2. |
|
1465 |
||
1466 |
'connect Smalltalk streams ...'. |
|
1467 |
||
1468 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
|
1469 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
|
1470 |
||
1471 |
'read ...'. |
|
1472 |
[ |
|
1473 |
1 to:10 do:[:i | |
|
1474 |
Transcript showCR:rs nextLine |
|
1475 |
]. |
|
1476 |
rs close. |
|
1477 |
] forkAt:7. |
|
1478 |
||
1479 |
'write ...'. |
|
1480 |
[ |
|
1481 |
1 to:10 do:[:i | |
|
1482 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1483 |
]. |
|
1484 |
ws close. |
|
1485 |
] fork. |
|
1486 |
||
1487 |
" |
|
1488 |
||
1489 |
"Created: 29.2.1996 / 18:14:24 / cg" |
|
1490 |
"Modified: 29.2.1996 / 18:25:02 / cg" |
|
1491 |
! |
|
1492 |
||
1493 |
forWritingToFileDescriptor:aFileDescriptor |
|
1494 |
"given a fileDescriptor, create an ExternalStream object |
|
1495 |
to write to this fd. This may be used to wrap fd's |
|
1496 |
as returned by user primitive code, or to wrap pipe- |
|
1497 |
filedescriptors into externalStreams." |
|
1498 |
||
1499 |
^ (self forFileDescriptor:aFileDescriptor mode:WriteMode) writeonly |
|
1500 |
||
1501 |
" |
|
1502 |
the example below will probably fail (15 is a random FD): |
|
1503 |
||
1504 |
|s| |
|
1505 |
||
1506 |
s := ExternalStream forWritingToFileDescriptor:15. |
|
1507 |
s binary. |
|
1508 |
s nextPut:1. |
|
1509 |
" |
|
1510 |
||
1511 |
"Created: 29.2.1996 / 18:14:43 / cg" |
|
1512 |
"Modified: 29.2.1996 / 18:15:54 / cg" |
|
1513 |
! |
|
1514 |
||
1515 |
makePTYPair |
|
1516 |
"return an array with two streams - the first one is the master, |
|
1517 |
the second the slave of a ptym/ptys pair. |
|
1518 |
This is much like a bidirectional pipe, but allows signals & |
|
1519 |
control chars to be passed through the connection. |
|
1520 |
This is needed to execute a shell in a view. |
|
1521 |
This is the higher level equivalent of OperatingSystem>>makePTYPair |
|
1522 |
(which returns an array of file-descriptors)." |
|
1523 |
||
1524 |
|ptyPair m s| |
|
1525 |
||
1526 |
ptyPair := OperatingSystem makePTYPair. |
|
1527 |
ptyPair notNil ifTrue:[ |
|
1528 |
m := self forReadWriteToFileDescriptor:(ptyPair at:1). |
|
1529 |
s := self forReadWriteToFileDescriptor:(ptyPair at:2). |
|
1530 |
ptyPair at:1 put:m. |
|
1531 |
ptyPair at:2 put:s. |
|
1532 |
^ ptyPair |
|
1533 |
]. |
|
1534 |
^ nil |
|
1535 |
||
1536 |
" |
|
1537 |
ExternalStream makePTYPair. |
|
1538 |
" |
|
1539 |
||
1540 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
|
1541 |
! |
|
1542 |
||
1543 |
makePipe |
|
1544 |
"return an array with two streams - the first one for reading, |
|
1545 |
the second for writing. |
|
1546 |
This is the higher level equivalent of OperatingSystem>>makePipe |
|
1547 |
(which returns an array of file-descriptors)." |
|
1548 |
||
1549 |
|pipe rs ws| |
|
1550 |
||
1551 |
pipe := OperatingSystem makePipe. |
|
1552 |
||
1553 |
pipe notNil ifTrue:[ |
|
1554 |
rs := self forReadingFromFileDescriptor:(pipe at:1). |
|
1555 |
ws := self forWritingToFileDescriptor:(pipe at:2). |
|
1556 |
^ Array with:rs with:ws |
|
1557 |
]. |
|
1558 |
^ nil |
|
1559 |
||
1560 |
" |
|
1561 |
|pipe rs ws| |
|
1562 |
||
1563 |
pipe := ExternalStream makePipe. |
|
1564 |
rs := pipe at:1. |
|
1565 |
ws := pipe at:2. |
|
1566 |
||
1567 |
'read ...'. |
|
1568 |
[ |
|
1569 |
1 to:10 do:[:i | |
|
1570 |
Transcript showCR:rs nextLine |
|
1571 |
]. |
|
1572 |
rs close. |
|
1573 |
] forkAt:7. |
|
1574 |
||
1575 |
'write ...'. |
|
1576 |
[ |
|
1577 |
1 to:10 do:[:i | |
|
1578 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
1579 |
]. |
|
1580 |
ws close. |
|
1581 |
] fork. |
|
1582 |
" |
|
1583 |
||
1584 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
|
1585 |
! |
|
1586 |
||
1587 |
new |
|
1588 |
|newStream| |
|
1589 |
||
1590 |
newStream := self basicNew. |
|
1591 |
newStream text; buffered:true; eolMode:DefaultEOLMode; clearEOF. |
|
1592 |
^ newStream |
|
1593 |
! ! |
|
1594 |
||
1595 |
!ExternalStream class methodsFor:'Signal constants'! |
|
1596 |
||
1597 |
inaccessibleSignal |
|
1598 |
"ST-80 compatibility: return openErrorSignal" |
|
1599 |
||
1600 |
^ self openErrorSignal |
|
1601 |
||
1602 |
"Created: 2.7.1996 / 12:27:16 / stefan" |
|
1603 |
! |
|
1604 |
||
1605 |
invalidModeSignal |
|
1606 |
"return the signal raised when doing text-I/O with a binary stream |
|
1607 |
or binary-I/O with a text stream" |
|
1608 |
||
1609 |
^ InvalidModeSignal |
|
1610 |
! |
|
1611 |
||
1612 |
invalidOperationSignal |
|
1613 |
"return the signal raised when an unsupported or invalid |
|
1614 |
I/O operation is attempted" |
|
1615 |
||
1616 |
^ InvalidOperationSignal |
|
1617 |
! |
|
1618 |
||
1619 |
invalidReadSignal |
|
1620 |
"return the signal raised when reading from writeonly streams" |
|
1621 |
||
1622 |
^ InvalidReadSignal |
|
1623 |
! |
|
1624 |
||
1625 |
invalidWriteSignal |
|
1626 |
"return the signal raised when writing to readonly streams" |
|
1627 |
||
1628 |
^ InvalidWriteSignal |
|
1629 |
! |
|
1630 |
||
1631 |
openErrorSignal |
|
1632 |
"return the signal raised when a file open failed" |
|
1633 |
||
1634 |
^ OpenErrorSignal |
|
1635 |
! |
|
1636 |
||
1637 |
streamIOErrorSignal |
|
1638 |
"return the signal raised when an I/O error occurs. |
|
1639 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
1640 |
which is no longer available and has been mounted soft)" |
|
1641 |
||
1642 |
^ StreamIOErrorSignal |
|
1643 |
||
1644 |
"Created: / 21.5.1998 / 16:32:55 / cg" |
|
1645 |
! |
|
1646 |
||
1647 |
streamNotOpenSignal |
|
1648 |
"return the signal raised on I/O with closed streams" |
|
1649 |
||
1650 |
^ StreamNotOpenSignal |
|
1651 |
! ! |
|
1652 |
||
1653 |
!ExternalStream class methodsFor:'error handling'! |
|
1654 |
||
1655 |
lastErrorNumber |
|
1656 |
"return the errno of the last error" |
|
1657 |
||
1658 |
^ LastErrorNumber |
|
1659 |
||
1660 |
" |
|
1661 |
ExternalStream lastErrorNumber |
|
1662 |
" |
|
1663 |
! |
|
1664 |
||
1665 |
lastErrorString |
|
1666 |
"return a message string describing the last error" |
|
1667 |
||
1668 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
1669 |
||
1670 |
" |
|
1671 |
ExternalStream lastErrorString |
|
1672 |
" |
|
1673 |
! ! |
|
1674 |
||
6622 | 1675 |
!ExternalStream methodsFor:'Compatibility - Squeak'! |
3746 | 1676 |
|
1677 |
readOnly |
|
1678 |
"Squeak compatibility: make the stream readOnly" |
|
1679 |
||
1680 |
mode := #readonly |
|
1681 |
||
1682 |
"Modified: 20.10.1997 / 19:23:04 / cg" |
|
1683 |
"Created: 20.10.1997 / 19:23:19 / cg" |
|
1684 |
! ! |
|
1685 |
||
1686 |
!ExternalStream methodsFor:'accessing'! |
|
1687 |
||
1688 |
binary |
|
1689 |
"switch to binary mode - default is text" |
|
1690 |
||
1691 |
binary := true |
|
1692 |
! |
|
1693 |
||
1694 |
buffered:aBoolean |
|
1695 |
"turn buffering on or off - default is on" |
|
1696 |
||
1697 |
buffered := aBoolean |
|
1698 |
! |
|
1699 |
||
1700 |
contents |
|
1701 |
"return the contents of the file from the current position up-to |
|
1702 |
the end. If the stream is in binary mode, a ByteArray containing |
|
1703 |
the byte values is returned. |
|
1704 |
In text-mode, a collection of strings, each representing one line, |
|
1705 |
is returned." |
|
1706 |
||
1707 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
1708 |
||
1709 |
binary ifTrue:[ |
|
1710 |
"adding to a ByteArray produces quadratic time-space |
|
1711 |
behavior - therefore we allocate chunks, and concatenate them |
|
1712 |
at the end." |
|
1713 |
||
1714 |
chunks := OrderedCollection new. |
|
1715 |
sizes := OrderedCollection new. |
|
1716 |
byteCount := 0. |
|
1717 |
[self atEnd] whileFalse:[ |
|
1718 |
chunk := ByteArray uninitializedNew:4096. |
|
1719 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
1720 |
cnt notNil ifTrue:[ |
|
1721 |
chunks add:chunk. |
|
1722 |
sizes add:cnt. |
|
1723 |
byteCount := byteCount + cnt |
|
1724 |
] |
|
1725 |
]. |
|
1726 |
||
1727 |
"now, create one big ByteArray" |
|
1728 |
bytes := ByteArray uninitializedNew:byteCount. |
|
1729 |
offset := 1. |
|
1730 |
1 to:chunks size do:[:index | |
|
1731 |
chunk := chunks at:index. |
|
1732 |
cnt := sizes at:index. |
|
1733 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
1734 |
offset := offset + cnt |
|
1735 |
]. |
|
1736 |
^ bytes |
|
1737 |
]. |
|
1738 |
||
1739 |
text := StringCollection new. |
|
1740 |
[self atEnd] whileFalse:[ |
|
1741 |
l := self nextLine. |
|
1742 |
l isNil ifTrue:[ |
|
1743 |
^ text |
|
1744 |
]. |
|
1745 |
text add:l |
|
1746 |
]. |
|
1747 |
^ text |
|
1748 |
! |
|
1749 |
||
1750 |
contentsOfEntireFile |
|
1751 |
"ST-80 compatibility: return contents as String" |
|
1752 |
||
6281 | 1753 |
binary ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6622
diff
changeset
|
1754 |
^ self contents. |
6281 | 1755 |
]. |
6303
ef753226450d
contentsOfEntireFile fixed (no cr at the end)
Claus Gittinger <cg@exept.de>
parents:
6281
diff
changeset
|
1756 |
^ self upToEnd |
3746 | 1757 |
|
1758 |
"Modified: 3.7.1996 / 13:22:16 / stefan" |
|
1759 |
! |
|
1760 |
||
1761 |
contentsSpecies |
|
1762 |
"return the kind of object to be returned by sub-collection builders |
|
1763 |
(such as upTo)" |
|
1764 |
||
1765 |
binary ifTrue:[ |
|
1766 |
^ ByteArray |
|
1767 |
]. |
|
1768 |
^ String |
|
1769 |
! |
|
1770 |
||
1771 |
eolMode:aSymbolOrNil |
|
1772 |
"specify how end-of-line (EOL) is to be marked. |
|
1773 |
The argument may be one of: |
|
1774 |
#crlf -> add a CR-NL, as in MSDOS |
|
1775 |
#cr -> add a CR, as in VMS |
|
1776 |
#nl -> add a NL, as in Unix |
|
1777 |
anyOther -> like #nl |
|
1778 |
" |
|
1779 |
||
1780 |
eolMode := aSymbolOrNil |
|
1781 |
! |
|
1782 |
||
1783 |
fileDescriptor |
|
1784 |
"return the fileDescriptor of the receiver - |
|
1785 |
notice: this one returns the underlying OSs fileDescriptor - |
|
1786 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1787 |
||
1788 |
%{ /* NOCONTEXT */ |
|
1789 |
OBJ fp; |
|
1790 |
||
1791 |
if ((fp = __INST(filePointer)) != nil) { |
|
4206 | 1792 |
FILEPOINTER f; |
1793 |
||
3746 | 1794 |
f = __FILEVal(fp); |
1795 |
RETURN ( __MKINT(fileno(f)) ); |
|
1796 |
} |
|
1797 |
%}. |
|
1798 |
^ self errorNotOpen |
|
1799 |
! |
|
1800 |
||
1801 |
filePointer |
|
1802 |
"return the filePointer of the receiver - |
|
1803 |
notice: for portability stdio is used; this means you will get |
|
1804 |
a FILE * - not a fileDescriptor. |
|
1805 |
(what you really get is a corresponding integer). |
|
1806 |
You cannot do much with the returned value |
|
1807 |
- except passing it to a primitive, for example." |
|
1808 |
||
1809 |
^ filePointer |
|
1810 |
! |
|
1811 |
||
1812 |
lineEndTransparent |
|
1813 |
eolMode := #cr |
|
1814 |
! |
|
1815 |
||
1816 |
readonly |
|
1817 |
"set access mode to readonly" |
|
1818 |
||
1819 |
mode := #readonly |
|
1820 |
! |
|
1821 |
||
1822 |
readwrite |
|
1823 |
"set access mode to readwrite" |
|
1824 |
||
1825 |
mode := #readwrite |
|
1826 |
! |
|
1827 |
||
1828 |
text |
|
1829 |
"switch to text mode - default is text" |
|
1830 |
||
1831 |
binary := false |
|
1832 |
! |
|
1833 |
||
1834 |
useCRLF:aBoolean |
|
1835 |
"turn on or off CRLF sending (instead of LF only) - default is off. |
|
1836 |
This method is provided for backward compatibility - see #eolMode: |
|
1837 |
which offers another choice." |
|
1838 |
||
1839 |
aBoolean ifTrue:[ |
|
1840 |
eolMode := #crlf |
|
1841 |
] ifFalse:[ |
|
1842 |
eolMode := #nl |
|
1843 |
]. |
|
1844 |
! |
|
1845 |
||
1846 |
writeonly |
|
1847 |
"set access mode to writeonly" |
|
1848 |
||
1849 |
mode := #writeonly |
|
1850 |
! ! |
|
1851 |
||
1852 |
!ExternalStream methodsFor:'error handling'! |
|
1853 |
||
1854 |
argumentMustBeCharacter |
|
1855 |
"{ Pragma: +optSpace }" |
|
1856 |
||
1857 |
"report an error, that the argument must be a character" |
|
1858 |
||
1859 |
^ self error:'argument must be a character' |
|
1860 |
! |
|
1861 |
||
1862 |
argumentMustBeInteger |
|
1863 |
"{ Pragma: +optSpace }" |
|
1864 |
||
1865 |
"report an error, that the argument must be an integer" |
|
1866 |
||
1867 |
^ self error:'argument must be an integer' |
|
1868 |
! |
|
1869 |
||
1870 |
argumentMustBeString |
|
1871 |
"{ Pragma: +optSpace }" |
|
1872 |
||
1873 |
"report an error, that the argument must be a string" |
|
1874 |
||
1875 |
^ self error:'argument must be a string' |
|
1876 |
! |
|
1877 |
||
1878 |
errorBinary |
|
1879 |
"{ Pragma: +optSpace }" |
|
1880 |
||
1881 |
"report an error, that the stream is in binary mode" |
|
1882 |
||
1883 |
^ self invalidModeSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1884 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1885 |
errorString:(self class name , ' is in binary mode') |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1886 |
in:thisContext sender |
5106 | 1887 |
|
1888 |
"Modified: / 14.12.1999 / 21:17:55 / cg" |
|
3746 | 1889 |
! |
1890 |
||
1891 |
errorNotBinary |
|
1892 |
"{ Pragma: +optSpace }" |
|
1893 |
||
1894 |
"report an error, that the stream is not in binary mode" |
|
1895 |
||
1896 |
^ self invalidModeSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1897 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1898 |
errorString:(self class name , ' is not in binary mode') |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1899 |
in:thisContext sender |
5106 | 1900 |
|
1901 |
"Modified: / 14.12.1999 / 21:18:09 / cg" |
|
3746 | 1902 |
! |
1903 |
||
1904 |
errorNotBuffered |
|
1905 |
"{ Pragma: +optSpace }" |
|
1906 |
||
1907 |
"report an error, that the stream is not in buffered mode" |
|
1908 |
||
1909 |
^ StreamErrorSignal |
|
1910 |
raiseRequestWith:self |
|
1911 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
1912 |
in:thisContext sender |
|
1913 |
! |
|
1914 |
||
1915 |
errorNotOpen |
|
1916 |
"{ Pragma: +optSpace }" |
|
1917 |
||
1918 |
"report an error, that the stream has not been opened" |
|
1919 |
||
1920 |
^ StreamNotOpenSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1921 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1922 |
errorString:(self class name , ' not open') |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1923 |
in:thisContext sender |
5105 | 1924 |
|
1925 |
"Modified: / 14.12.1999 / 20:38:30 / cg" |
|
3746 | 1926 |
! |
1927 |
||
1928 |
errorOpen |
|
1929 |
"{ Pragma: +optSpace }" |
|
1930 |
||
1931 |
"report an error, that the stream is already opened" |
|
1932 |
||
1933 |
^ self openErrorSignal |
|
1934 |
raiseRequestWith:self |
|
1935 |
errorString:(self class name , ' is already open') |
|
1936 |
in:thisContext sender |
|
1937 |
! |
|
1938 |
||
1939 |
errorReadOnly |
|
1940 |
"{ Pragma: +optSpace }" |
|
1941 |
||
1942 |
"report an error, that the stream is a readOnly stream" |
|
1943 |
||
1944 |
^ self invalidWriteSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1945 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1946 |
errorString:(self class name , ' is readonly') |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1947 |
in:thisContext sender |
5106 | 1948 |
|
1949 |
"Modified: / 14.12.1999 / 21:18:28 / cg" |
|
3746 | 1950 |
! |
1951 |
||
1952 |
errorUnsupportedOperation |
|
1953 |
"{ Pragma: +optSpace }" |
|
1954 |
||
1955 |
"report an error, that some unsupported operation was attempted" |
|
1956 |
||
1957 |
^ self invalidOperationSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1958 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1959 |
errorString:'unsupported operation' |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1960 |
in:thisContext sender |
5106 | 1961 |
|
1962 |
"Modified: / 14.12.1999 / 21:18:36 / cg" |
|
3746 | 1963 |
! |
1964 |
||
1965 |
errorWriteOnly |
|
1966 |
"{ Pragma: +optSpace }" |
|
1967 |
||
1968 |
"report an error, that the stream is a writeOnly stream" |
|
1969 |
||
1970 |
^ self invalidReadSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1971 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1972 |
errorString:(self class name , ' is writeonly') |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1973 |
in:thisContext sender |
5106 | 1974 |
|
1975 |
"Modified: / 14.12.1999 / 21:18:45 / cg" |
|
3746 | 1976 |
! |
1977 |
||
1978 |
ioError |
|
1979 |
"{ Pragma: +optSpace }" |
|
1980 |
||
1981 |
"report an error, that some I/O error occured. |
|
1982 |
(for example, a device-IO-error, or reading an NFS-dir, |
|
1983 |
which is no longer available and has been mounted soft)" |
|
1984 |
||
1985 |
^ StreamIOErrorSignal |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1986 |
raiseWith:self |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1987 |
errorString:('I/O error: ' , self lastErrorString) |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
1988 |
in:thisContext sender |
5106 | 1989 |
|
1990 |
"Modified: / 14.12.1999 / 21:18:53 / cg" |
|
3746 | 1991 |
! |
1992 |
||
1993 |
lastErrorNumber |
|
1994 |
"return the last error" |
|
1995 |
||
1996 |
^ lastErrorNumber |
|
1997 |
! |
|
1998 |
||
1999 |
lastErrorString |
|
2000 |
"return a message string describing the last error" |
|
2001 |
||
2002 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
2003 |
^ 'I/O error' |
|
2004 |
]. |
|
2005 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2006 |
! |
|
2007 |
||
2008 |
openError |
|
2009 |
"{ Pragma: +optSpace }" |
|
2010 |
||
2011 |
"report an error, that the open failed" |
|
2012 |
||
2013 |
^ self class openErrorSignal |
|
2014 |
raiseRequestWith:self |
|
2015 |
errorString:('error on open: ' , self lastErrorString) |
|
2016 |
in:thisContext sender |
|
2017 |
||
2018 |
"Modified: / 28.1.1998 / 14:37:42 / stefan" |
|
2019 |
! |
|
2020 |
||
2021 |
readError |
|
2022 |
"{ Pragma: +optSpace }" |
|
2023 |
||
2024 |
"report an error, that some read error occured" |
|
2025 |
||
2026 |
^ ReadErrorSignal |
|
2027 |
raiseRequestWith:self |
|
2028 |
errorString:('read error: ' , self lastErrorString) |
|
2029 |
in:thisContext sender |
|
2030 |
! |
|
2031 |
||
2032 |
writeError |
|
2033 |
"{ Pragma: +optSpace }" |
|
2034 |
||
2035 |
"report an error, that some write error occured" |
|
2036 |
||
2037 |
^ WriteErrorSignal |
|
2038 |
raiseRequestWith:self |
|
2039 |
errorString:('write error: ' , self lastErrorString) |
|
2040 |
in:thisContext sender |
|
2041 |
! ! |
|
2042 |
||
2043 |
!ExternalStream methodsFor:'instance release'! |
|
2044 |
||
2045 |
closeFile |
|
2046 |
"low level close - may be redefined in subclasses" |
|
2047 |
||
2048 |
%{ |
|
2049 |
OBJ fp; |
|
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2050 |
int rslt; |
3746 | 2051 |
|
2052 |
if ((fp = __INST(filePointer)) != nil) { |
|
4206 | 2053 |
FILEPOINTER f; |
2054 |
||
3746 | 2055 |
__INST(filePointer) = nil; |
4206 | 2056 |
f = __FILEVal(fp); |
2057 |
||
3746 | 2058 |
#ifdef NO_STDIO |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2059 |
do { |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2060 |
__threadErrno = 0; |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2061 |
rslt = __STX_C_CALL1("fclose", fclose, f); |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2062 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
3746 | 2063 |
#else |
2064 |
__BEGIN_INTERRUPTABLE__ |
|
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2065 |
do { |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2066 |
__threadErrno = 0; |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2067 |
rslt = __STX_C_CALL1("fclose", fclose, f); |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2068 |
} while((rslt < 0) && (__threadErrno == EINTR)); |
3746 | 2069 |
__END_INTERRUPTABLE__ |
2070 |
#endif |
|
2071 |
} |
|
2072 |
%} |
|
2073 |
! |
|
2074 |
||
2075 |
disposed |
|
2076 |
"some Stream has been collected - close the file if not already done" |
|
2077 |
||
2078 |
self closeFile |
|
2079 |
! |
|
2080 |
||
2081 |
setFileDescriptor:anInteger mode:openMode |
|
2082 |
"set the filePointer based upon a given fileDescriptor - |
|
2083 |
notice: this one is based on the underlying OSs fileDescriptor - |
|
2084 |
this may not be available on all platforms (i.e. non unix systems)." |
|
2085 |
||
2086 |
%{ /* NOCONTEXT */ |
|
2087 |
||
2088 |
FILEPOINTER f; |
|
2089 |
OBJ fp; |
|
2090 |
FILE *fdopen(); |
|
2091 |
||
2092 |
if (__isSmallInteger(anInteger) && |
|
2093 |
__isString(openMode) && |
|
2094 |
(f = fdopen(__intVal(anInteger), __stringVal(openMode))) != 0 |
|
2095 |
) { |
|
2096 |
__PROTECT__(self); |
|
2097 |
fp = __MKFILEPOINTER(f); |
|
2098 |
__UNPROTECT__(self); |
|
2099 |
__INST(filePointer) = fp; __STORE(self, fp); |
|
2100 |
RETURN (self); |
|
2101 |
} |
|
2102 |
%}. |
|
2103 |
^ self primitiveFailed |
|
2104 |
! |
|
2105 |
||
2106 |
setFilePointer:something |
|
2107 |
"set the filePointer to the given one; |
|
2108 |
low level private & friend interface; may also be used to connect to some |
|
2109 |
externally provided file handle." |
|
2110 |
||
2111 |
filePointer := something |
|
2112 |
! |
|
2113 |
||
2114 |
shallowCopyForFinalization |
|
2115 |
"return a copy for finalization-registration; |
|
2116 |
since all we need at finalization time is the fileDescriptor, |
|
2117 |
a cheaper copy is possible." |
|
2118 |
||
2119 |
^ self class basicNew setFilePointer:filePointer |
|
2120 |
! |
|
2121 |
||
2122 |
shutDown |
|
2123 |
"close the stream - added for protocol compatibility with PipeStream. |
|
2124 |
see comment there" |
|
2125 |
||
3949 | 2126 |
self isOpen ifTrue:[ |
4206 | 2127 |
Lobby unregister:self. |
2128 |
self closeFile |
|
3949 | 2129 |
] |
3746 | 2130 |
|
2131 |
"Modified: 30.8.1996 / 00:39:21 / cg" |
|
2132 |
! ! |
|
2133 |
||
2134 |
!ExternalStream methodsFor:'line reading/writing'! |
|
2135 |
||
2136 |
nextLine |
|
2137 |
"read the next line (characters up to newline). |
|
2138 |
Return a string containing those characters excluding the newline. |
|
2139 |
If the previous-to-last character is a cr, this is also removed, |
|
2140 |
so its possible to read alien (i.e. ms-dos) text as well. |
|
2141 |
The line must be shorter than 16K characters - otherwise an error is signalled." |
|
2142 |
||
2143 |
|line| |
|
2144 |
||
2145 |
%{ /* STACK:34000 */ |
|
2146 |
||
2147 |
FILEPOINTER f; |
|
2148 |
int len, ret; |
|
2149 |
char buffer[32*1024]; |
|
2150 |
char *rslt, *nextPtr, *limit; |
|
2151 |
int fd, ch; |
|
2152 |
int _buffered; |
|
2153 |
OBJ fp; |
|
2154 |
int lineTooLong = 0; |
|
2155 |
int cutOff = 0; |
|
2156 |
||
2157 |
__INST(lastErrorNumber) = nil; |
|
2158 |
if (((fp = __INST(filePointer)) != nil) |
|
2159 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2160 |
&& (__INST(binary) != true) |
|
2161 |
) { |
|
2162 |
f = __FILEVal(fp); |
|
2163 |
buffer[0] = '\0'; |
|
2164 |
||
2165 |
_buffered = (__INST(buffered) == true); |
|
2166 |
if (_buffered) { |
|
2167 |
__READING__(f); |
|
2168 |
} |
|
2169 |
||
2170 |
rslt = nextPtr = buffer; |
|
2171 |
limit = buffer + sizeof(buffer) - 2; |
|
2172 |
||
2173 |
for (;;) { |
|
2174 |
__READBYTE__(ret, f, nextPtr, _buffered); |
|
2175 |
if (ret <= 0) { |
|
2176 |
if (nextPtr == buffer) |
|
2177 |
rslt = NULL; |
|
2178 |
if (ret == 0) { |
|
2179 |
__INST(hitEOF) = true; |
|
2180 |
break; |
|
2181 |
} else { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2182 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2183 |
goto err; |
2184 |
} |
|
2185 |
} |
|
2186 |
||
2187 |
if (*nextPtr == '\n') { |
|
2188 |
cutOff = 1; |
|
2189 |
*nextPtr = '\0'; |
|
2190 |
break; |
|
2191 |
} |
|
2192 |
if (*nextPtr == '\r') { |
|
2193 |
char peekChar; |
|
2194 |
||
2195 |
/* |
|
2196 |
* peek ahead for a newLine ... |
|
2197 |
*/ |
|
2198 |
__READBYTE__(ret, f, &peekChar, _buffered); |
|
2199 |
if (ret <= 0) { |
|
2200 |
cutOff = 1; |
|
2201 |
*nextPtr = '\0'; |
|
2202 |
if (ret == 0) { |
|
2203 |
__INST(hitEOF) = true; |
|
2204 |
break; |
|
2205 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2206 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2207 |
goto err; |
2208 |
} |
|
2209 |
||
2210 |
if (peekChar == '\n') { |
|
2211 |
cutOff = 2; |
|
2212 |
*nextPtr = '\0'; |
|
2213 |
break; |
|
2214 |
} |
|
2215 |
||
2216 |
__UNGETC__(peekChar, f, _buffered); |
|
2217 |
||
2218 |
cutOff = 1; |
|
2219 |
*nextPtr = '\0'; |
|
2220 |
break; |
|
2221 |
} |
|
2222 |
||
2223 |
nextPtr++; |
|
2224 |
if (nextPtr >= limit) { |
|
2225 |
*nextPtr = '\0'; |
|
2226 |
lineTooLong = 1; |
|
2227 |
if (@global(InfoPrinting) == true) { |
|
2228 |
fprintf(stderr, "ExtStream [warning]: line truncated in nextLine\n"); |
|
2229 |
} |
|
2230 |
break; |
|
2231 |
} |
|
2232 |
} |
|
2233 |
||
2234 |
if (rslt != NULL) { |
|
2235 |
len = nextPtr-buffer; |
|
2236 |
||
2237 |
if (__INST(position) != nil) { |
|
2238 |
int __p; |
|
2239 |
||
2240 |
__p = __intVal(__INST(position)) + len + cutOff; |
|
2241 |
__INST(position) = __MKSMALLINT(__p); |
|
2242 |
} |
|
2243 |
/* remove any EOL character */ |
|
2244 |
if (len != 0) { |
|
2245 |
if (buffer[len-1] == '\n') { |
|
2246 |
buffer[--len] = '\0'; |
|
2247 |
} |
|
2248 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
|
2249 |
buffer[--len] = '\0'; |
|
2250 |
} |
|
2251 |
} |
|
2252 |
line = __MKSTRING_L(buffer, len); |
|
2253 |
if (! lineTooLong) { |
|
2254 |
RETURN ( line ); |
|
2255 |
} |
|
2256 |
} |
|
2257 |
} |
|
2258 |
err: ; |
|
2259 |
%}. |
|
2260 |
line notNil ifTrue:[ |
|
2261 |
"/ the line as read is longer than 32k characters (boy - what a line) |
|
2262 |
"/ The exception could be handled by reading more and returning the |
|
2263 |
"/ concatenation in your exception handler (the receiver and the partial |
|
2264 |
"/ line are passed as parameter) |
|
2265 |
||
4597
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2266 |
LineTooLongErrorSignal isHandled ifTrue:[ |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2267 |
^ LineTooLongErrorSignal |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2268 |
raiseRequestWith:(Array with:self with:line) |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2269 |
errorString:('line too long read error') |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2270 |
]. |
9628a80e23ec
only raise LineTooLongError if handled; otherwise silently continue
Claus Gittinger <cg@exept.de>
parents:
4362
diff
changeset
|
2271 |
^ line , self nextLine |
3746 | 2272 |
]. |
2273 |
||
2274 |
hitEOF ifTrue:[^ self pastEnd]. |
|
2275 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2276 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2277 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2278 |
^ self errorBinary |
|
2279 |
! |
|
2280 |
||
2281 |
nextPutLine:aString |
|
2282 |
"write the characters in aString and append an end-of-Line marker |
|
2283 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
2284 |
||
2285 |
%{ |
|
2286 |
FILEPOINTER f; |
|
2287 |
int len, cnt, len1, _buffered; |
|
2288 |
OBJ pos, fp; |
|
2289 |
char *cp; |
|
2290 |
int o_offs; |
|
2291 |
||
2292 |
__INST(lastErrorNumber) = nil; |
|
2293 |
if (((fp = __INST(filePointer)) != nil) |
|
2294 |
&& (__INST(mode) != @symbol(readonly)) |
|
2295 |
&& (__INST(binary) != true) |
|
2296 |
&& __isString(aString) |
|
2297 |
) { |
|
2298 |
f = __FILEVal(fp); |
|
2299 |
len = __stringSize(aString); |
|
2300 |
||
2301 |
if (_buffered = (__INST(buffered) == true)) { |
|
2302 |
__WRITING__(f) |
|
2303 |
} |
|
2304 |
#ifdef BUGGY |
|
2305 |
__WRITEBYTES__(cnt, f, __stringVal(aString), len, _buffered); |
|
2306 |
#else |
|
2307 |
o_offs = (char *)__stringVal(aString)-(char *)aString; |
|
2308 |
__WRITEBYTES_OBJ__(cnt, f, aString, o_offs, len, _buffered); |
|
2309 |
#endif |
|
2310 |
if (cnt == len) { |
|
2311 |
OBJ mode = __INST(eolMode); |
|
2312 |
||
2313 |
len1 = len; |
|
2314 |
||
2315 |
if (mode == @symbol(cr)) { |
|
2316 |
cp = "\r"; len = 1; |
|
2317 |
} else if (mode == @symbol(crlf)) { |
|
2318 |
cp = "\r\n"; len = 2; |
|
2319 |
} else { |
|
2320 |
cp = "\n"; len = 1; |
|
2321 |
} |
|
2322 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
|
2323 |
||
2324 |
if (cnt > 0) { |
|
2325 |
pos = __INST(position); |
|
2326 |
if (pos != nil) { |
|
2327 |
int __p; |
|
2328 |
||
2329 |
__p = __intVal(pos) + len1 + cnt; |
|
2330 |
__INST(position) = __MKSMALLINT(__p); |
|
2331 |
} |
|
2332 |
RETURN ( self ); |
|
2333 |
} |
|
2334 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2335 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2336 |
} |
2337 |
%}. |
|
2338 |
super nextPutLine:aString. |
|
2339 |
! |
|
2340 |
||
2341 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
2342 |
"read from aStream up to and including a line starting with aStringOrNil |
|
2343 |
and append all lines to self. |
|
2344 |
Can be used to copy/create large files or copy from a pipe/socket. |
|
2345 |
||
2346 |
If aStringOrNil is nil or not matched, copy preceeds to the end. |
|
2347 |
(this allows for example to read a Socket and transfer the data quickly |
|
2348 |
into a file - without creating zillions of temporary strings)" |
|
2349 |
||
2350 |
|srcFilePointer readError line| |
|
2351 |
||
2352 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
2353 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
2354 |
srcFilePointer := aStream filePointer. |
|
2355 |
srcFilePointer isNil ifTrue:[aStream errorNotOpen. ^ self]. |
|
2356 |
||
2357 |
%{ /* STACK:2000 */ |
|
2358 |
||
2359 |
#ifndef NO_STDIO |
|
2360 |
FILEPOINTER dst; |
|
2361 |
FILEPOINTER src; |
|
2362 |
char *matchString; |
|
2363 |
int matchLen = 0; |
|
2364 |
char buffer[1024]; |
|
2365 |
||
2366 |
__INST(lastErrorNumber) = nil; |
|
2367 |
if (__isSmallInteger(srcFilePointer)) { |
|
2368 |
if ((aStringOrNil == nil) || __isString(aStringOrNil)) { |
|
2369 |
if (aStringOrNil != nil) { |
|
2370 |
matchString = (char *) __stringVal(aStringOrNil); |
|
2371 |
matchLen = __stringSize(aStringOrNil); |
|
2372 |
} |
|
2373 |
dst = __FILEVal(__INST(filePointer)); |
|
2374 |
src = __FILEVal(srcFilePointer); |
|
2375 |
__BEGIN_INTERRUPTABLE__ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2376 |
__threadErrno = 0; |
3746 | 2377 |
|
2378 |
__WRITING__(dst) |
|
2379 |
||
2380 |
for (;;) { |
|
2381 |
if (fgets(buffer, sizeof(buffer)-1, src) == NULL) { |
|
2382 |
if (ferror(src)) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2383 |
readError = __MKSMALLINT(__threadErrno); |
3746 | 2384 |
__END_INTERRUPTABLE__ |
2385 |
goto err; |
|
2386 |
} |
|
2387 |
break; |
|
2388 |
} |
|
2389 |
if (fputs(buffer, dst) == EOF) { |
|
2390 |
if (ferror(dst)) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2391 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2392 |
__END_INTERRUPTABLE__ |
2393 |
goto err; |
|
2394 |
} |
|
2395 |
break; |
|
2396 |
} |
|
2397 |
#ifndef OLD |
|
2398 |
if (__INST(buffered) == false) { |
|
2399 |
FFLUSH(dst); |
|
2400 |
} |
|
2401 |
#endif |
|
2402 |
if (matchLen) { |
|
2403 |
if (strncmp(matchString, buffer, matchLen) == 0) |
|
2404 |
break; |
|
2405 |
} |
|
2406 |
} |
|
2407 |
__END_INTERRUPTABLE__ |
|
2408 |
__INST(position) = nil; |
|
2409 |
RETURN (self); |
|
2410 |
} |
|
2411 |
} |
|
2412 |
#endif /* ! NO_STDIO */ |
|
2413 |
err: ; |
|
2414 |
%}. |
|
2415 |
readError ifTrue:[ |
|
2416 |
aStream setLastErrorNumber:readError. |
|
2417 |
aStream readError. |
|
2418 |
^ self |
|
2419 |
]. |
|
2420 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
2421 |
buffered ifFalse:[self errorNotBuffered. ^ self]. |
|
2422 |
||
2423 |
" |
|
2424 |
argument error or unimplemented; try it linewise |
|
2425 |
" |
|
2426 |
[aStream atEnd] whileFalse:[ |
|
2427 |
line := aStream nextLine. |
|
2428 |
line isNil ifTrue:[ |
|
2429 |
^ self. |
|
2430 |
]. |
|
2431 |
self nextPutLine:line. |
|
2432 |
(aStringOrNil notNil and:[line startsWith:aStringOrNil]) ifTrue:[ |
|
2433 |
^ self |
|
2434 |
] |
|
2435 |
]. |
|
2436 |
! |
|
2437 |
||
2438 |
peekForLineStartingWith:aString |
|
2439 |
"read ahead for next line starting with aString; |
|
2440 |
return the line-string if found, or nil if EOF is encountered. |
|
7118 | 2441 |
If matched, not advance position beyond that line |
3746 | 2442 |
i.e. nextLine will read the matched line. |
2443 |
If not matched, reposition to original position for firther reading." |
|
2444 |
||
2445 |
|firstPos lastPos line| |
|
2446 |
||
2447 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2448 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2449 |
binary ifTrue:[^ self errorBinary]. |
|
2450 |
buffered ifFalse:[^ self errorNotBuffered]. |
|
2451 |
||
2452 |
%{ /* STACK: 2000 */ |
|
2453 |
#ifndef NO_STDIO |
|
2454 |
||
2455 |
FILEPOINTER f; |
|
2456 |
int l; |
|
2457 |
char buffer[1024]; |
|
2458 |
char *cp; |
|
2459 |
char *matchString; |
|
2460 |
int gotFirst = 0; |
|
2461 |
long firstpos, lastpos; |
|
2462 |
||
2463 |
__INST(lastErrorNumber) = nil; |
|
2464 |
if (__isString(aString)) { |
|
2465 |
matchString = (char *) __stringVal(aString); |
|
2466 |
l = __stringSize(aString); |
|
2467 |
||
2468 |
f = __FILEVal(__INST(filePointer)); |
|
2469 |
__READING__(f) |
|
2470 |
||
2471 |
for (;;) { |
|
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2472 |
lastpos = ftell(f); |
3746 | 2473 |
if (!gotFirst) { |
2474 |
firstpos = lastpos; |
|
2475 |
gotFirst = 1; |
|
2476 |
} |
|
2477 |
||
2478 |
__BEGIN_INTERRUPTABLE__ |
|
2479 |
do { |
|
2480 |
cp = fgets(buffer, sizeof(buffer)-1, f); |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2481 |
} while ((cp == NULL) && ferror(f) && (__threadErrno == EINTR) && (clearerr(f), 1)); |
3746 | 2482 |
buffer[sizeof(buffer)-1] = '\0'; |
2483 |
__END_INTERRUPTABLE__ |
|
2484 |
||
2485 |
if (cp == NULL) { |
|
2486 |
if (ferror(f)) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2487 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2488 |
goto err; |
2489 |
} else { |
|
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2490 |
fseek(f, firstpos, SEEK_SET); |
3746 | 2491 |
RETURN (nil); |
2492 |
} |
|
2493 |
} |
|
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2494 |
if (strncmp(cp, matchString, l) == 0) { |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
2495 |
fseek(f, lastpos, SEEK_SET); |
3746 | 2496 |
break; |
2497 |
} |
|
2498 |
} |
|
2499 |
/* remove EOL character */ |
|
2500 |
cp = buffer; |
|
2501 |
while (*cp && (*cp != '\n') && (*cp != '\r')) cp++; |
|
2502 |
*cp = '\0'; |
|
2503 |
RETURN ( __MKSTRING(buffer) ); |
|
2504 |
} |
|
2505 |
err: ; |
|
2506 |
||
2507 |
# undef FTELL |
|
2508 |
# undef FSEEK |
|
2509 |
||
2510 |
#endif |
|
2511 |
%}. |
|
2512 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2513 |
||
2514 |
"/ try using generic code ... |
|
2515 |
||
2516 |
firstPos := self position. |
|
2517 |
[self atEnd] whileFalse:[ |
|
2518 |
lastPos := self position. |
|
2519 |
line := self nextLine. |
|
2520 |
line isNil ifTrue:[ |
|
2521 |
self position:firstPos. |
|
2522 |
^ nil |
|
2523 |
]. |
|
2524 |
(line startsWith:aString) ifTrue:[ |
|
2525 |
self position:lastPos. |
|
2526 |
^ line |
|
2527 |
] |
|
2528 |
]. |
|
2529 |
self position:firstPos. |
|
2530 |
^ nil |
|
2531 |
! |
|
2532 |
||
2533 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
2534 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
2535 |
return the index in aCollection if found, nil otherwise.. |
|
2536 |
If no match, do not change position; otherwise advance right before the |
|
2537 |
matched line so that nextLine will return this line." |
|
2538 |
||
2539 |
|line startPos linePos index| |
|
2540 |
||
2541 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2542 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2543 |
binary ifTrue:[^ self errorBinary]. |
|
2544 |
||
2545 |
startPos := self position. |
|
2546 |
[self atEnd] whileFalse:[ |
|
2547 |
linePos := self position. |
|
2548 |
line := self nextLine. |
|
2549 |
line notNil ifTrue:[ |
|
2550 |
index := 1. |
|
2551 |
aCollectionOfStrings do:[:prefix | |
|
2552 |
(line startsWith:prefix) ifTrue:[ |
|
2553 |
self position:linePos. |
|
2554 |
^ index |
|
2555 |
]. |
|
2556 |
index := index + 1 |
|
2557 |
] |
|
2558 |
] |
|
2559 |
]. |
|
2560 |
self position:startPos. |
|
2561 |
^ nil |
|
2562 |
! ! |
|
2563 |
||
2564 |
!ExternalStream methodsFor:'misc functions'! |
|
2565 |
||
2566 |
async:aBoolean |
|
2567 |
"set/clear the async attribute - if set, the availability of data on |
|
2568 |
the receiver will trigger an ioInterrupt. |
|
2569 |
If cleared (which is the default) no special notification is made. |
|
2570 |
Notice: |
|
2571 |
not every OS supports this |
|
2572 |
- check with OS>>supportsIOInterrupts before using" |
|
2573 |
||
2574 |
|fd| |
|
2575 |
||
2576 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2577 |
||
2578 |
OperatingSystem supportsIOInterrupts ifFalse:[ |
|
2579 |
^ self errorUnsupportedOperation |
|
2580 |
]. |
|
2581 |
||
2582 |
fd := self fileDescriptor. |
|
2583 |
aBoolean ifTrue:[ |
|
2584 |
^ OperatingSystem enableIOInterruptsOn:fd |
|
2585 |
]. |
|
2586 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
2587 |
||
2588 |
"Modified: 11.1.1997 / 17:50:21 / cg" |
|
2589 |
! |
|
2590 |
||
2591 |
blocking:aBoolean |
|
2592 |
"set/clear the blocking attribute - if set (which is the default) |
|
2593 |
a read (using next) on the receiver will block until data is available. |
|
2594 |
If cleared, a read operation will immediately return with a value of |
|
2595 |
nil. |
|
2596 |
Turning off blocking is useful when reading from PipeStreams |
|
2597 |
or Sockets, and the amount of data to be read is not known |
|
2598 |
in advance. However, the data must then be read using #nextBytes: |
|
2599 |
methods, in order to avoid other (pastEnd) exceptions." |
|
2600 |
||
2601 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2602 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor) |
|
2603 |
||
2604 |
"Modified: / 11.1.1997 / 17:48:01 / cg" |
|
2605 |
"Modified: / 15.1.1998 / 11:49:48 / stefan" |
|
2606 |
! |
|
2607 |
||
2608 |
close |
|
2609 |
"close the stream - tell operating system" |
|
2610 |
||
3948 | 2611 |
self isOpen ifTrue:[ |
4206 | 2612 |
Lobby unregister:self. |
2613 |
self closeFile. |
|
3948 | 2614 |
] |
2615 |
||
2616 |
"Modified: / 21.12.1998 / 11:19:54 / cg" |
|
3746 | 2617 |
! |
2618 |
||
2619 |
create |
|
2620 |
"create the stream |
|
2621 |
- this must be redefined in subclass" |
|
2622 |
||
2623 |
^ self subclassResponsibility |
|
2624 |
! |
|
2625 |
||
2626 |
ioctl:ioctlNumber |
|
2627 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2628 |
on the underlying file; no arguments are passed." |
|
2629 |
||
2630 |
^ self ioctl:ioctlNumber with:nil |
|
2631 |
! |
|
2632 |
||
2633 |
ioctl:ioctlNumber with:arg |
|
2634 |
"to provide a simple ioctl facility - an ioctl is performed |
|
2635 |
on the underlying file; the argument is passed as argument. |
|
2636 |
This is not used by ST/X, but provided for special situations |
|
2637 |
- for example, to control proprietrary I/O devices. |
|
2638 |
||
2639 |
Since the type of the argument depends on the ioctl being |
|
2640 |
performed, different arg types are allowed here. |
|
2641 |
If the argument is nil, an ioctl without argument is performed. |
|
2642 |
If the argument is an integral number, its directly passed; |
|
2643 |
if its a kind of ByteArray (ByteArray, String or Structure), |
|
2644 |
or external data (ExternalBytes or ExternalAddress), |
|
2645 |
a pointer to the data is passed. |
|
2646 |
This allows performing most ioctls |
|
2647 |
- however, it might be tricky to setup the buffer. |
|
2648 |
Be careful in what you pass - ST/X cannot validate its correctness." |
|
2649 |
||
2650 |
%{ |
|
2651 |
#if !defined(MSDOS_LIKE) |
|
2652 |
FILEPOINTER f; |
|
2653 |
int ret, ioNum; |
|
2654 |
OBJ fp; |
|
2655 |
INT ioArg; |
|
2656 |
||
2657 |
__INST(lastErrorNumber) = nil; |
|
2658 |
if ((fp = __INST(filePointer)) != nil) { |
|
2659 |
if (__isSmallInteger(ioctlNumber) |
|
2660 |
&& (__isSmallInteger(arg) |
|
2661 |
|| (arg == nil) |
|
2662 |
|| __isBytes(arg) |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2663 |
|| __isExternalBytesLike(arg) |
3746 | 2664 |
|| __isExternalAddress(arg))) { |
2665 |
f = __FILEVal(fp); |
|
2666 |
ioNum = __intVal(ioctlNumber); |
|
2667 |
||
2668 |
__BEGIN_INTERRUPTABLE__ |
|
2669 |
do { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2670 |
__threadErrno = 0; |
3746 | 2671 |
if (arg == nil) { |
2672 |
ioArg = 0; |
|
2673 |
} else if (__isSmallInteger(arg)) { |
|
2674 |
ioArg = __intVal(arg); |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2675 |
} else if (__isExternalBytesLike(arg)) { |
3746 | 2676 |
ioArg = (INT)(__externalBytesAddress(arg)); |
2677 |
} else if (__isExternalAddress(arg)) { |
|
2678 |
ioArg = (INT)(__externalAddressVal(arg)); |
|
2679 |
} else { |
|
2680 |
ioArg = (INT)(__ByteArrayInstPtr(arg)->ba_element); |
|
2681 |
} |
|
2682 |
ret = ioctl(fileno(f), ioNum, ioArg); |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2683 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3746 | 2684 |
__END_INTERRUPTABLE__ |
2685 |
||
2686 |
if (ret >= 0) { |
|
2687 |
RETURN ( __MKSMALLINT(ret) ); |
|
2688 |
} |
|
2689 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2690 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2691 |
} |
2692 |
} |
|
2693 |
#endif |
|
2694 |
%}. |
|
2695 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
2696 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2697 |
" |
|
2698 |
argument is not an integer |
|
2699 |
" |
|
2700 |
(ioctlNumber isMemberOf:SmallInteger) ifFalse:[^ self primitiveFailed]. |
|
2701 |
" |
|
2702 |
the system does not support ioctl (MSDOS or VMS) |
|
2703 |
" |
|
2704 |
^ self errorUnsupportedOperation |
|
2705 |
! |
|
2706 |
||
2707 |
open |
|
2708 |
"open the stream |
|
2709 |
- this must be redefined in subclass" |
|
2710 |
||
2711 |
^ self subclassResponsibility |
|
2712 |
! |
|
2713 |
||
7057 | 2714 |
position0Based |
3746 | 2715 |
"return the position |
2716 |
- this must be redefined in subclass" |
|
2717 |
||
2718 |
^ self subclassResponsibility |
|
2719 |
! |
|
2720 |
||
7057 | 2721 |
position0Based:anInteger |
3746 | 2722 |
"set the position |
2723 |
- this must be redefined in subclass" |
|
2724 |
||
2725 |
^ self subclassResponsibility |
|
2726 |
! |
|
2727 |
||
2728 |
setToEnd |
|
2729 |
"redefined since it must be implemented differently" |
|
2730 |
||
2731 |
^ self subclassResponsibility |
|
2732 |
! |
|
2733 |
||
2734 |
truncateTo:newSize |
|
2735 |
"truncate the underlying OS file to newSize. |
|
2736 |
Warning: this may not be implemented on all platforms." |
|
2737 |
||
2738 |
%{ |
|
2739 |
#ifdef HAS_FTRUNCATE |
|
2740 |
FILEPOINTER f; |
|
2741 |
OBJ fp; |
|
2742 |
||
2743 |
if (((fp = __INST(filePointer)) != nil) |
|
2744 |
&& (__INST(mode) != @symbol(readonly))) { |
|
2745 |
if (__isSmallInteger(newSize)) { |
|
2746 |
f = __FILEVal(fp); |
|
2747 |
||
2748 |
if (__INST(buffered) == true) { |
|
2749 |
__READING__(f) |
|
2750 |
FFLUSH(f); |
|
2751 |
OPT_FSEEK(f, 0L, SEEK_END); /* needed in stdio */ |
|
2752 |
} |
|
2753 |
ftruncate(fileno(f), __intVal(newSize)); |
|
2754 |
RETURN (self); |
|
2755 |
} |
|
2756 |
} |
|
2757 |
#endif |
|
2758 |
%}. |
|
2759 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
2760 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
2761 |
self errorUnsupportedOperation |
|
2762 |
||
2763 |
" |
|
2764 |
|s| |
|
2765 |
||
2766 |
s := 'test' asFilename writeStream. |
|
2767 |
s next:1000 put:$a. |
|
2768 |
s truncateTo:100. |
|
2769 |
s close. |
|
2770 |
||
2771 |
('test' asFilename fileSize) printNL |
|
2772 |
" |
|
2773 |
! ! |
|
2774 |
||
2775 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
2776 |
||
2777 |
nextAvailableBytes:count into:anObject startingAt:start |
|
2778 |
"read the next count bytes into an object and return the number of |
|
2779 |
bytes read or the number of bytes read, if EOF is encountered before, |
|
2780 |
or no more bytes are available for reading (from the pipe/socket). |
|
2781 |
||
2782 |
If the receiver is some socket/pipe-like stream, an exception |
|
2783 |
is raised if the connection is broken. |
|
2784 |
||
2785 |
Notice, that in contrast to other methods, |
|
2786 |
this does NOT return nil on EOF, but the actual count. |
|
2787 |
Thus allowing read of partial blocks. |
|
2788 |
||
2789 |
The object must have non-pointer indexed instvars |
|
2790 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
2791 |
If anObject is a string or byteArray and reused, this provides the |
|
2792 |
fastest possible physical I/O (since no new objects are allocated). |
|
2793 |
||
2794 |
Use with care - non object oriented I/O. |
|
2795 |
Warning: in general, you cannot use this method to pass data from other |
|
2796 |
architectures since it does not care for byte order or float representation." |
|
2797 |
||
2798 |
%{ |
|
2799 |
FILEPOINTER f; |
|
2800 |
int cnt, offs, ret, _buffered; |
|
2801 |
int objSize, nInstVars, nInstBytes; |
|
2802 |
char *cp; |
|
2803 |
char *extPtr; |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2804 |
OBJ pos, fp; |
3746 | 2805 |
int o_offs; |
2806 |
||
2807 |
__INST(lastErrorNumber) = nil; |
|
2808 |
if (((fp = __INST(filePointer)) != nil) |
|
2809 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2810 |
&& __bothSmallInteger(count, start) |
|
2811 |
) { |
|
2812 |
f = __FILEVal(fp); |
|
2813 |
||
2814 |
cnt = __intVal(count); |
|
2815 |
offs = __intVal(start) - 1; |
|
2816 |
||
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2817 |
if (__isExternalBytesLike(anObject)) { |
3746 | 2818 |
OBJ sz; |
2819 |
||
2820 |
nInstBytes = 0; |
|
2821 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
2822 |
sz = __externalBytesSize(anObject); |
|
2823 |
if (__isSmallInteger(sz)) { |
|
2824 |
objSize = __intVal(sz); |
|
2825 |
} else { |
|
2826 |
objSize = 0; /* unknown */ |
|
2827 |
} |
|
2828 |
} else { |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2829 |
OBJ oClass; |
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2830 |
|
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2831 |
oClass = __Class(anObject); |
3746 | 2832 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
2833 |
case BYTEARRAY: |
|
2834 |
case WORDARRAY: |
|
2835 |
case LONGARRAY: |
|
2836 |
case SWORDARRAY: |
|
2837 |
case SLONGARRAY: |
|
2838 |
case FLOATARRAY: |
|
2839 |
case DOUBLEARRAY: |
|
2840 |
break; |
|
2841 |
default: |
|
2842 |
goto bad; |
|
2843 |
} |
|
2844 |
extPtr = (char *)0; |
|
2845 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2846 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
2847 |
objSize = __Size(anObject) - nInstBytes; |
|
2848 |
} |
|
2849 |
||
2850 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
2851 |
_buffered = (__INST(buffered) == true); |
|
2852 |
if (_buffered) { |
|
2853 |
__READING__(f); |
|
2854 |
} |
|
2855 |
||
2856 |
if (extPtr) { |
|
2857 |
__READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
|
2858 |
} else { |
|
2859 |
/* |
|
2860 |
* on interrupt, anObject may be moved to another location. |
|
2861 |
* So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__, |
|
2862 |
* to get a new address. |
|
2863 |
*/ |
|
2864 |
offs += nInstBytes; |
|
2865 |
__READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
|
2866 |
} |
|
2867 |
if (ret > 0) { |
|
2868 |
pos = __INST(position); |
|
2869 |
if (pos != nil) { |
|
2870 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
|
2871 |
} |
|
2872 |
RETURN (__MKSMALLINT(ret)); |
|
2873 |
} |
|
2874 |
if (ret == 0) { |
|
2875 |
__INST(hitEOF) = true; |
|
2876 |
} else /* ret < 0 */ { |
|
2877 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2878 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2879 |
} |
2880 |
} |
|
2881 |
} |
|
2882 |
bad: ; |
|
2883 |
%}. |
|
2884 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
|
2885 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2886 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2887 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
2888 |
" |
|
2889 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
2890 |
" |
|
2891 |
^ self primitiveFailed |
|
2892 |
! |
|
2893 |
||
2894 |
nextByte |
|
2895 |
"read the next byte and return it as an Integer; return nil on error. |
|
2896 |
This is allowed in both text and binary modes, always returning the |
|
2897 |
bytes binary value as an integer in 0..255." |
|
2898 |
||
2899 |
%{ |
|
2900 |
FILEPOINTER f; |
|
2901 |
unsigned char byte; |
|
2902 |
int ret, _buffered; |
|
2903 |
OBJ fp; |
|
2904 |
||
2905 |
__INST(lastErrorNumber) = nil; |
|
2906 |
if (((fp = __INST(filePointer)) != nil) |
|
2907 |
&& (__INST(mode) != @symbol(writeonly))) { |
|
2908 |
f = __FILEVal(fp); |
|
2909 |
||
2910 |
_buffered = (__INST(buffered) == true); |
|
2911 |
if (_buffered) { |
|
2912 |
__READING__(f) |
|
2913 |
} |
|
2914 |
__READBYTE__(ret, f, &byte, _buffered); |
|
2915 |
if (ret > 0) { |
|
2916 |
if (__INST(position) != nil) |
|
2917 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
|
2918 |
RETURN (__MKSMALLINT(byte)); |
|
2919 |
} |
|
2920 |
||
2921 |
if (ret == 0) { |
|
2922 |
__INST(hitEOF) = true; |
|
2923 |
} else /* ret < 0 */ { |
|
2924 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
2925 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 2926 |
} |
2927 |
} |
|
2928 |
%}. |
|
2929 |
hitEOF ifTrue:[^ self pastEnd]. |
|
2930 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2931 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2932 |
^ self errorWriteOnly |
|
2933 |
! |
|
2934 |
||
2935 |
nextBytes:count into:anObject startingAt:start |
|
2936 |
"read the next count bytes into an object and return the number of |
|
2937 |
bytes read or the number of bytes read, if EOF is encountered before. |
|
2938 |
If the receiver is some socket/pipe-like stream, an exception |
|
2939 |
is raised if the connection is broken. |
|
2940 |
||
2941 |
Warning: if used with a pipe/socket, this blocks until the requested number |
|
2942 |
of bytes have been read. See #nextAvailableBytes:into:startingAt: |
|
2943 |
to only read whats there. |
|
2944 |
||
2945 |
Notice, that in contrast to other methods, |
|
2946 |
this does NOT return nil on EOF, but the actual count. |
|
2947 |
Thus allowing read of partial blocks. |
|
2948 |
||
2949 |
The object must have non-pointer indexed instvars |
|
2950 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray), |
|
2951 |
or an externalBytes object (with known size). |
|
2952 |
If anObject is a string or byteArray and reused, this provides the |
|
2953 |
fastest possible physical I/O (since no new objects are allocated). |
|
2954 |
||
2955 |
Use with care - non object oriented I/O. |
|
2956 |
Warning: in general, you cannot use this method to pass data from other |
|
2957 |
architectures (unless you prepared the buffer with care), |
|
2958 |
since it does not care for byte order or float representation." |
|
2959 |
||
2960 |
%{ |
|
2961 |
FILEPOINTER f; |
|
2962 |
int cnt, offs, ret, _buffered; |
|
2963 |
int objSize, nInstVars, nInstBytes; |
|
2964 |
char *extPtr; |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2965 |
OBJ pos, fp; |
3746 | 2966 |
|
2967 |
__INST(lastErrorNumber) = nil; |
|
2968 |
if (((fp = __INST(filePointer)) != nil) |
|
2969 |
&& (__INST(mode) != @symbol(writeonly)) |
|
2970 |
&& __bothSmallInteger(count, start) |
|
2971 |
) { |
|
2972 |
f = __FILEVal(fp); |
|
2973 |
||
2974 |
cnt = __intVal(count); |
|
2975 |
offs = __intVal(start) - 1; |
|
2976 |
||
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2977 |
if (__isExternalBytesLike(anObject)) { |
3746 | 2978 |
OBJ sz; |
2979 |
||
2980 |
nInstBytes = 0; |
|
2981 |
extPtr = (char *)(__externalBytesAddress(anObject)); |
|
2982 |
sz = __externalBytesSize(anObject); |
|
2983 |
if (__isSmallInteger(sz)) { |
|
2984 |
objSize = __intVal(sz); |
|
2985 |
} else { |
|
2986 |
objSize = 0; /* unknown */ |
|
2987 |
} |
|
2988 |
} else { |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2989 |
OBJ oClass; |
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2990 |
|
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
2991 |
oClass = __Class(anObject); |
3746 | 2992 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
2993 |
case BYTEARRAY: |
|
2994 |
case WORDARRAY: |
|
2995 |
case LONGARRAY: |
|
2996 |
case SWORDARRAY: |
|
2997 |
case SLONGARRAY: |
|
2998 |
case FLOATARRAY: |
|
2999 |
case DOUBLEARRAY: |
|
3000 |
break; |
|
3001 |
default: |
|
3002 |
goto bad; |
|
3003 |
} |
|
3004 |
extPtr = (char *)0; |
|
3005 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3006 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3007 |
objSize = __Size(anObject) - nInstBytes; |
|
3008 |
} |
|
3009 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3010 |
_buffered = (__INST(buffered) == true); |
|
3011 |
if (_buffered) { |
|
3012 |
__READING__(f); |
|
3013 |
} |
|
3014 |
||
3015 |
if (extPtr) { |
|
3016 |
__READBYTES__(ret, f, extPtr+offs, cnt, _buffered); |
|
3017 |
} else { |
|
3018 |
/* |
|
3019 |
* on interrupt, anObject may be moved to another location. |
|
3020 |
* So we pass anObject, and the offset to the __READBYTES_OBJ__ macro. |
|
3021 |
*/ |
|
3022 |
offs += nInstBytes; |
|
3023 |
__READBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered); |
|
3024 |
} |
|
3025 |
||
3026 |
if (ret > 0) { |
|
3027 |
pos = __INST(position); |
|
3028 |
if (pos != nil) { |
|
3029 |
__INST(position) = __MKSMALLINT(__intVal(pos) + ret); |
|
3030 |
} |
|
3031 |
RETURN (__MKSMALLINT(ret)); |
|
3032 |
} |
|
3033 |
if (ret == 0) { |
|
3034 |
__INST(hitEOF) = true; |
|
3035 |
} else /* ret < 0 */ { |
|
3036 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3037 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3038 |
} |
3039 |
} |
|
3040 |
} |
|
3041 |
bad: ; |
|
3042 |
%}. |
|
3043 |
(hitEOF and:[self pastEnd isNil]) ifTrue:[^ 0]. |
|
3044 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3045 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3046 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3047 |
" |
|
3048 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
3049 |
" |
|
3050 |
^ self primitiveFailed |
|
3051 |
! |
|
3052 |
||
3053 |
nextLong |
|
3054 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
3055 |
The returned value may be a LargeInteger. |
|
3056 |
(msb-first for compatibility with other smalltalks)" |
|
3057 |
||
3058 |
^ self nextUnsignedLongMSB:true |
|
3059 |
! |
|
3060 |
||
3061 |
nextLongMSB:msbFlag |
|
3062 |
"Read four bytes and return the value as a 32-bit signed Integer, |
|
3063 |
which may be a LargeInteger. |
|
3064 |
If msbFlag is true, value is read with most-significant byte first, |
|
3065 |
otherwise least-significant byte comes first. |
|
3066 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
3067 |
Works in both binary and text modes." |
|
3068 |
||
3069 |
%{ |
|
3070 |
OBJ fp; |
|
3071 |
||
3072 |
__INST(lastErrorNumber) = nil; |
|
3073 |
if (((fp = __INST(filePointer)) != nil) |
|
3074 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3075 |
) { |
|
3076 |
FILEPOINTER f; |
|
3077 |
int ret; |
|
3078 |
int value; |
|
3079 |
char buffer[4]; |
|
3080 |
||
3081 |
f = __FILEVal(fp); |
|
3082 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
|
3083 |
||
3084 |
if (ret == 4) { |
|
3085 |
if (__INST(position) != nil) { |
|
3086 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
|
3087 |
} |
|
3088 |
if (msbFlag == true) { |
|
3089 |
value = (buffer[0] & 0xFF); |
|
3090 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3091 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3092 |
value = (value << 8) | (buffer[3] & 0xFF); |
|
3093 |
} else { |
|
3094 |
value = (buffer[3] & 0xFF); |
|
3095 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3096 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3097 |
value = (value << 8) | (buffer[0] & 0xFF); |
|
3098 |
} |
|
3099 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
|
3100 |
RETURN ( __MKSMALLINT(value)); |
|
3101 |
} |
|
3102 |
RETURN ( __MKLARGEINT(value) ); |
|
3103 |
} |
|
3104 |
||
3105 |
if (ret < 0) { |
|
3106 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3107 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3108 |
} else /* ret == 0 */ { |
3109 |
__INST(hitEOF) = true; |
|
3110 |
} |
|
3111 |
} |
|
3112 |
%}. |
|
3113 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3114 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3115 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3116 |
^ self readError. |
|
3117 |
||
3118 |
! |
|
3119 |
||
3120 |
nextShortMSB:msbFlag |
|
3121 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
3122 |
If msbFlag is true, value is read with most-significant byte first, |
|
3123 |
otherwise least-significant byte comes first. |
|
3124 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3125 |
Works in both binary and text modes." |
|
3126 |
||
3127 |
%{ |
|
3128 |
OBJ fp; |
|
3129 |
||
3130 |
__INST(lastErrorNumber) = nil; |
|
3131 |
if (((fp = __INST(filePointer)) != nil) |
|
3132 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3133 |
) { |
|
3134 |
FILEPOINTER f; |
|
3135 |
int ret; |
|
3136 |
short value; |
|
3137 |
char buffer[2]; |
|
3138 |
||
3139 |
f = __FILEVal(fp); |
|
3140 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
3141 |
||
3142 |
if (ret == 2) { |
|
3143 |
if (__INST(position) != nil) { |
|
3144 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
3145 |
} |
|
3146 |
if (msbFlag == true) { |
|
3147 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
3148 |
} else { |
|
3149 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
3150 |
} |
|
3151 |
RETURN (__MKSMALLINT(value)); |
|
3152 |
} |
|
3153 |
||
3154 |
if (ret < 0) { |
|
3155 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3156 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3157 |
} else /* ret == 0 */ { |
3158 |
__INST(hitEOF) = true; |
|
3159 |
} |
|
3160 |
} |
|
3161 |
%}. |
|
3162 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3163 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3164 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3165 |
^ self readError. |
|
3166 |
! |
|
3167 |
||
3168 |
nextUnsignedLongMSB:msbFlag |
|
3169 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
3170 |
a LargeInteger. |
|
3171 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
3172 |
least-significant byte comes first. |
|
3173 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
3174 |
Works in both binary and text modes." |
|
3175 |
||
3176 |
%{ |
|
3177 |
OBJ fp; |
|
3178 |
||
3179 |
__INST(lastErrorNumber) = nil; |
|
3180 |
if (((fp = __INST(filePointer)) != nil) |
|
3181 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3182 |
) { |
|
3183 |
FILEPOINTER f; |
|
3184 |
int ret; |
|
3185 |
unsigned INT value; |
|
3186 |
unsigned char buffer[4]; |
|
3187 |
||
3188 |
f = __FILEVal(fp); |
|
3189 |
__READBYTES__(ret, f, buffer, 4, __INST(buffered) == true); |
|
3190 |
||
3191 |
if (ret == 4) { |
|
3192 |
if (__INST(position) != nil) { |
|
3193 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 4); |
|
3194 |
} |
|
3195 |
if (msbFlag == true) { |
|
3196 |
value = (buffer[0] & 0xFF); |
|
3197 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3198 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3199 |
value = (value << 8) | (buffer[3] & 0xFF); |
|
3200 |
} else { |
|
3201 |
value = (buffer[3] & 0xFF); |
|
3202 |
value = (value << 8) | (buffer[2] & 0xFF); |
|
3203 |
value = (value << 8) | (buffer[1] & 0xFF); |
|
3204 |
value = (value << 8) | (buffer[0] & 0xFF); |
|
3205 |
} |
|
3206 |
#ifdef alpha64 |
|
3207 |
value &= 0xFFFFFFFF; |
|
3208 |
#endif |
|
3209 |
if (value <= _MAX_INT) { |
|
3210 |
RETURN ( __MKSMALLINT(value)); |
|
3211 |
} |
|
3212 |
RETURN ( __MKULARGEINT(value) ); |
|
3213 |
} |
|
3214 |
||
3215 |
if (ret < 0) { |
|
3216 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3217 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3218 |
} else /* ret == 0 */ { |
3219 |
__INST(hitEOF) = true; |
|
3220 |
} |
|
3221 |
} |
|
3222 |
%}. |
|
3223 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3224 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3225 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3226 |
^ self readError. |
|
3227 |
! |
|
3228 |
||
3229 |
nextUnsignedShortMSB:msbFlag |
|
3230 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
3231 |
If msbFlag is true, value is read with most-significant byte first, |
|
3232 |
otherwise least-significant byte comes first. |
|
3233 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
3234 |
Works in both binary and text modes." |
|
3235 |
||
3236 |
%{ |
|
3237 |
OBJ fp; |
|
3238 |
||
3239 |
__INST(lastErrorNumber) = nil; |
|
3240 |
if (((fp = __INST(filePointer)) != nil) |
|
3241 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3242 |
) { |
|
3243 |
FILEPOINTER f; |
|
3244 |
int ret; |
|
3245 |
unsigned int value; |
|
3246 |
char buffer[2]; |
|
3247 |
||
3248 |
f = __FILEVal(fp); |
|
3249 |
__READBYTES__(ret, f, buffer, 2, __INST(buffered) == true); |
|
3250 |
||
3251 |
if (ret == 2) { |
|
3252 |
if (__INST(position) != nil) { |
|
3253 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 2); |
|
3254 |
} |
|
3255 |
if (msbFlag == true) { |
|
3256 |
value = ((buffer[0] & 0xFF) << 8) | (buffer[1] & 0xFF); |
|
3257 |
} else { |
|
3258 |
value = ((buffer[1] & 0xFF) << 8) | (buffer[0] & 0xFF); |
|
3259 |
} |
|
3260 |
RETURN (__MKSMALLINT(value)); |
|
3261 |
} |
|
3262 |
||
3263 |
if (ret < 0) { |
|
3264 |
__INST(position) = nil; |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3265 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3266 |
} else /* ret == 0 */ { |
3267 |
__INST(hitEOF) = true; |
|
3268 |
} |
|
3269 |
} |
|
3270 |
%}. |
|
3271 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3272 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3273 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3274 |
^ self readError. |
|
3275 |
! |
|
3276 |
||
3277 |
nextWord |
|
3278 |
"in text-mode: |
|
3279 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
|
3280 |
return a string containing those characters. |
|
3281 |
in binary-mode: |
|
3282 |
read two bytes (msb-first) and return the value as a 16-bit |
|
3283 |
unsigned Integer (for compatibility with other smalltalks)" |
|
3284 |
||
3285 |
binary ifTrue:[ |
|
3286 |
^ self nextUnsignedShortMSB:true |
|
3287 |
]. |
|
3288 |
^ self nextAlphaNumericWord |
|
3289 |
! ! |
|
3290 |
||
3291 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
3292 |
||
3293 |
nextPutByte:aByteValue |
|
3294 |
"write a byte. |
|
3295 |
Works in both binary and text modes." |
|
3296 |
||
3297 |
%{ |
|
3298 |
FILEPOINTER f; |
|
3299 |
char c; |
|
3300 |
OBJ pos, fp; |
|
3301 |
int cnt, _buffered; |
|
3302 |
||
3303 |
__INST(lastErrorNumber) = nil; |
|
3304 |
if (((fp = __INST(filePointer)) != nil) |
|
3305 |
&& (__INST(mode) != @symbol(readonly)) |
|
3306 |
&& __isSmallInteger(aByteValue) |
|
3307 |
||
3308 |
) { |
|
3309 |
c = __intVal(aByteValue); |
|
3310 |
f = __FILEVal(fp); |
|
3311 |
if (_buffered = (__INST(buffered) == true)) { |
|
3312 |
__WRITING__(f) |
|
3313 |
} |
|
3314 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
|
3315 |
if (cnt == 1) { |
|
3316 |
pos = __INST(position); |
|
3317 |
if (pos != nil) |
|
3318 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3319 |
RETURN (self); |
|
3320 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3321 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3322 |
} |
3323 |
%}. |
|
3324 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3325 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3326 |
self writeError. |
|
3327 |
! |
|
3328 |
||
3329 |
nextPutBytes:count from:anObject startingAt:start |
|
3330 |
"write count bytes from an object starting at index start. |
|
3331 |
return the number of bytes written - which could be 0. |
|
3332 |
The object must have non-pointer indexed instvars |
|
3333 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
|
3334 |
or an externalBytes object (with known size). |
|
3335 |
||
3336 |
Use with care - non object oriented i/o. |
|
3337 |
Warning: in general, you cannot use this method to pass data to other |
|
3338 |
architectures (unless you prepared the buffer with care), |
|
3339 |
since it does not care for byte order or float representation." |
|
3340 |
||
3341 |
%{ |
|
3342 |
FILEPOINTER f; |
|
3343 |
int cnt, len, offs, ret; |
|
3344 |
int objSize, nInstVars, nInstBytes, _buffered; |
|
3345 |
char *extPtr; |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3346 |
OBJ pos, fp; |
3746 | 3347 |
|
3348 |
__INST(lastErrorNumber) = nil; |
|
3349 |
if (((fp = __INST(filePointer)) != nil) |
|
3350 |
&& (__INST(mode) != @symbol(readonly)) |
|
3351 |
&& __bothSmallInteger(count, start) |
|
3352 |
) { |
|
3353 |
len = __intVal(count); |
|
3354 |
offs = __intVal(start) - 1; |
|
3355 |
f = __FILEVal(fp); |
|
3356 |
||
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3357 |
if (__isExternalBytesLike(anObject)) { |
3746 | 3358 |
OBJ sz; |
3359 |
||
3360 |
nInstBytes = 0; |
|
3361 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
3362 |
sz = __externalBytesSize(anObject); |
|
3363 |
if (__isSmallInteger(sz)) { |
|
3364 |
objSize = __intVal(sz); |
|
3365 |
} else { |
|
3366 |
objSize = 0; /* unknown */ |
|
3367 |
} |
|
3368 |
} else { |
|
7196
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3369 |
OBJ oClass; |
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3370 |
|
e86edc0af9c8
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
7118
diff
changeset
|
3371 |
oClass = __Class(anObject); |
3746 | 3372 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
3373 |
case BYTEARRAY: |
|
3374 |
case WORDARRAY: |
|
3375 |
case LONGARRAY: |
|
3376 |
case SWORDARRAY: |
|
3377 |
case SLONGARRAY: |
|
3378 |
case FLOATARRAY: |
|
3379 |
case DOUBLEARRAY: |
|
3380 |
break; |
|
3381 |
default: |
|
3382 |
goto bad; |
|
3383 |
} |
|
3384 |
extPtr = (char *)0; |
|
3385 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
3386 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
3387 |
objSize = __Size(anObject) - nInstBytes; |
|
3388 |
} |
|
3389 |
if ( (offs >= 0) && (len >= 0) && (objSize >= (len + offs)) ) { |
|
3390 |
if (_buffered = (__INST(buffered) == true)) { |
|
3391 |
__WRITING__(f) |
|
3392 |
} |
|
3393 |
||
3394 |
if (extPtr) { |
|
3395 |
__WRITEBYTES__(cnt, f, extPtr+offs, len, _buffered); |
|
3396 |
} else { |
|
3397 |
/* |
|
3398 |
* on interrupt, anObject may be moved to another location. |
|
3399 |
* So we pass anObject, and the offset to the __WRITEBYTES_OBJ__ macro. |
|
3400 |
*/ |
|
3401 |
offs += nInstBytes; |
|
3402 |
__WRITEBYTES_OBJ__(cnt, f, anObject, offs, len, _buffered); |
|
3403 |
} |
|
3404 |
||
3405 |
if (cnt >= 0) { |
|
3406 |
pos = __INST(position); |
|
3407 |
if (pos != nil) |
|
3408 |
__INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
3409 |
RETURN ( __MKSMALLINT(cnt) ); |
|
3410 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3411 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3412 |
} |
3413 |
} |
|
3414 |
bad: ; |
|
3415 |
%}. |
|
3416 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3417 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3418 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3419 |
^ self primitiveFailed |
|
3420 |
! |
|
3421 |
||
3422 |
nextPutLong:aNumber MSB:msbFlag |
|
3423 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
3424 |
true, data is written most-significant byte first; otherwise least |
|
3425 |
first. |
|
3426 |
Works in both binary and text modes." |
|
3427 |
||
3428 |
%{ |
|
3429 |
int num; |
|
3430 |
char bytes[4]; |
|
3431 |
FILEPOINTER f; |
|
3432 |
int cnt, _buffered; |
|
3433 |
OBJ fp, pos; |
|
3434 |
||
3435 |
__INST(lastErrorNumber) = nil; |
|
3436 |
if (((fp = __INST(filePointer)) != nil) |
|
3437 |
&& (__INST(mode) != @symbol(readonly)) |
|
3438 |
&& __isSmallInteger(aNumber) |
|
3439 |
) { |
|
3440 |
num = __intVal(aNumber); |
|
3441 |
if (msbFlag == true) { |
|
3442 |
bytes[0] = (num >> 24) & 0xFF; |
|
3443 |
bytes[1] = (num >> 16) & 0xFF; |
|
3444 |
bytes[2] = (num >> 8) & 0xFF; |
|
3445 |
bytes[3] = num & 0xFF; |
|
3446 |
} else { |
|
3447 |
bytes[3] = (num >> 24) & 0xFF; |
|
3448 |
bytes[2] = (num >> 16) & 0xFF; |
|
3449 |
bytes[1] = (num >> 8) & 0xFF; |
|
3450 |
bytes[0] = num & 0xFF; |
|
3451 |
} |
|
3452 |
||
3453 |
f = __FILEVal(fp); |
|
3454 |
if (_buffered = (__INST(buffered) == true)) { |
|
3455 |
__WRITING__(f) |
|
3456 |
} |
|
3457 |
__WRITEBYTES__(cnt, f, bytes, 4, _buffered); |
|
3458 |
||
3459 |
if (cnt == 4) { |
|
3460 |
if ((pos = __INST(position)) != nil) { |
|
3461 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 4); |
|
3462 |
} |
|
3463 |
RETURN ( self ); |
|
3464 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3465 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3466 |
} |
3467 |
%}. |
|
3468 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3469 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3470 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
3471 |
||
3472 |
aNumber isInteger ifTrue:[ |
|
3473 |
^ super nextPutLong:aNumber MSB:msbFlag |
|
3474 |
]. |
|
3475 |
self argumentMustBeInteger |
|
3476 |
! |
|
3477 |
||
3478 |
nextPutShort:aNumber MSB:msbFlag |
|
3479 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
3480 |
true, data is written most-significant byte first; otherwise least |
|
3481 |
first. |
|
3482 |
Works in both binary and text modes." |
|
3483 |
||
3484 |
%{ |
|
3485 |
int num; |
|
3486 |
char bytes[2]; |
|
3487 |
FILEPOINTER f; |
|
3488 |
int cnt, _buffered; |
|
3489 |
OBJ fp, pos; |
|
3490 |
||
3491 |
__INST(lastErrorNumber) = nil; |
|
3492 |
if (((fp = __INST(filePointer)) != nil) |
|
3493 |
&& (__INST(mode) != @symbol(readonly)) |
|
3494 |
&& __isSmallInteger(aNumber) |
|
3495 |
) { |
|
3496 |
num = __intVal(aNumber); |
|
3497 |
if (msbFlag == true) { |
|
3498 |
bytes[0] = (num >> 8) & 0xFF; |
|
3499 |
bytes[1] = num & 0xFF; |
|
3500 |
} else { |
|
3501 |
bytes[1] = (num >> 8) & 0xFF; |
|
3502 |
bytes[0] = num & 0xFF; |
|
3503 |
} |
|
3504 |
||
3505 |
f = __FILEVal(fp); |
|
3506 |
if (_buffered = (__INST(buffered) == true)) { |
|
3507 |
__WRITING__(f) |
|
3508 |
} |
|
3509 |
__WRITEBYTES__(cnt, f, bytes, 2, _buffered); |
|
3510 |
||
3511 |
if (cnt == 2) { |
|
3512 |
if ((pos = __INST(position)) != nil) { |
|
3513 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 2); |
|
3514 |
} |
|
3515 |
RETURN ( self ); |
|
3516 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3517 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3518 |
} |
3519 |
%}. |
|
3520 |
lastErrorNumber notNil ifTrue:[^ self writeError. ^ self]. |
|
3521 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
3522 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
3523 |
self argumentMustBeInteger |
|
3524 |
! ! |
|
3525 |
||
3526 |
!ExternalStream methodsFor:'private'! |
|
3527 |
||
3528 |
clearEOF |
|
3529 |
hitEOF := false |
|
3530 |
! |
|
3531 |
||
3532 |
connectTo:aFileDescriptor withMode:openmode |
|
3533 |
"connect a fileDescriptor; openmode is the string defining the way to open. |
|
3534 |
This can be used to connect an extrnally provided fileDescriptor (from |
|
3535 |
primitive code) or a pipeFileDescriptor (as returned by makePipe) to |
|
3536 |
a Stream object. |
|
3537 |
The openMode ('r', 'w' etc.) must match the mode in which |
|
3538 |
the fileDescriptor was originally opened (otherwise i/o errors will be reported later)." |
|
3539 |
||
3540 |
|retVal| |
|
3541 |
||
3542 |
filePointer notNil ifTrue:[^ self errorOpen]. |
|
3543 |
%{ |
|
3544 |
FILEPOINTER f; |
|
3545 |
OBJ fp; |
|
3546 |
FILE *fdopen(); |
|
3547 |
||
3548 |
if (__isSmallInteger(aFileDescriptor) |
|
3549 |
&& (__qClass(openmode)== @global(String))) { |
|
7099 | 3550 |
f = (FILEPOINTER) fdopen(__intVal(aFileDescriptor), (char *)__stringVal(openmode)); |
3551 |
if (f == NULL) { |
|
3552 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
|
3553 |
__INST(position) = nil; |
|
3554 |
} else { |
|
3555 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
|
3556 |
__INST(position) = @global(ZeroPosition); |
|
3557 |
retVal = self; |
|
3558 |
} |
|
3746 | 3559 |
} |
3560 |
%}. |
|
3561 |
retVal notNil ifTrue:[ |
|
7099 | 3562 |
self buffered:true. "default is buffered" |
3563 |
Lobby register:self |
|
3746 | 3564 |
]. |
3565 |
lastErrorNumber notNil ifTrue:[ |
|
7099 | 3566 |
" |
3567 |
the open failed for some reason ... |
|
3568 |
" |
|
3569 |
^ self openError |
|
3746 | 3570 |
]. |
3571 |
^ retVal |
|
3572 |
! |
|
3573 |
||
3574 |
open:aPath withMode:mode |
|
3575 |
"low level open; opens the file/device and sets the filePointer instance |
|
3576 |
variable. Careful: this does not care for any other state." |
|
3577 |
||
3578 |
|ok| |
|
3579 |
||
3580 |
ok := false. |
|
3581 |
%{ |
|
3582 |
FILE *f; |
|
3583 |
OBJ fp; |
|
3584 |
||
3585 |
if (__isString(aPath)) { |
|
3586 |
#ifdef NO_STDIO |
|
3587 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(mode)); |
|
3588 |
#else |
|
3589 |
__BEGIN_INTERRUPTABLE__ |
|
3590 |
do { |
|
3591 |
f = fopen((char *) __stringVal(aPath), (char *) __stringVal(mode)); |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3592 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
3746 | 3593 |
__END_INTERRUPTABLE__ |
3594 |
#endif |
|
3595 |
if (f == NULL) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3596 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3597 |
} else { |
3598 |
__INST(filePointer) = fp = __MKFILEPOINTER(f); __STORE(self, fp); |
|
3599 |
ok = true; |
|
3600 |
} |
|
3601 |
} |
|
3602 |
%}. |
|
3603 |
ok ifFalse:[ |
|
3604 |
" |
|
3605 |
the open failed for some reason ... |
|
3606 |
" |
|
3607 |
^ self openError |
|
3608 |
]. |
|
3609 |
Lobby register:self. |
|
3610 |
! |
|
3611 |
||
3612 |
reOpen |
|
3613 |
"sent after snapin to reopen streams. |
|
3614 |
cannot reopen here since I am abstract and have no device knowledge" |
|
3615 |
||
3616 |
self class name errorPrint. ' [warning]: automatic reOpen not supported - stream closed' errorPrintCR. |
|
3617 |
filePointer := nil. |
|
3618 |
Lobby unregister:self. |
|
3619 |
||
3620 |
"Modified: 10.1.1997 / 17:50:44 / cg" |
|
3621 |
! |
|
3622 |
||
3623 |
setLastError:aNumber |
|
3624 |
lastErrorNumber := aNumber |
|
3625 |
! ! |
|
3626 |
||
3627 |
!ExternalStream methodsFor:'queries'! |
|
3628 |
||
3629 |
isBinary |
|
3630 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
3631 |
The default when created is false." |
|
3632 |
||
3633 |
^ binary |
|
3634 |
! |
|
3635 |
||
3636 |
isExternalStream |
|
3637 |
"return true, if the receiver is some kind of externalStream; |
|
3638 |
true is returned here - the method redefined from Object." |
|
3639 |
||
3640 |
^ true |
|
3641 |
! |
|
3642 |
||
3948 | 3643 |
isOpen |
3644 |
"return true, if the stream is open" |
|
3645 |
||
3646 |
^ filePointer notNil |
|
3647 |
||
3648 |
"Created: / 21.12.1998 / 11:19:10 / cg" |
|
3649 |
! |
|
3650 |
||
3746 | 3651 |
isReadable |
3652 |
"return true, if this stream can be read from" |
|
3653 |
||
3654 |
^ (mode ~~ #writeonly) |
|
3655 |
! |
|
3656 |
||
3657 |
isWritable |
|
3658 |
"return true, if this stream can be written to" |
|
3659 |
||
3660 |
^ (mode ~~ #readonly) |
|
3661 |
! ! |
|
3662 |
||
3663 |
!ExternalStream methodsFor:'reading'! |
|
3664 |
||
3665 |
next |
|
3666 |
"return the next element; advance read position. |
|
3667 |
In binary mode, an integer is returned, otherwise a character. |
|
3668 |
If there are no more elements, nil is returned." |
|
3669 |
||
3670 |
%{ |
|
3671 |
FILEPOINTER f; |
|
3672 |
int ret, _buffered; |
|
3673 |
OBJ pos, fp; |
|
3674 |
unsigned char ch; |
|
3675 |
||
3676 |
__INST(lastErrorNumber) = nil; |
|
3677 |
if (((fp = __INST(filePointer)) != nil) |
|
3678 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3679 |
) { |
|
3680 |
f = __FILEVal(fp); |
|
3681 |
||
3682 |
_buffered = (__INST(buffered) == true); |
|
3683 |
if (_buffered) { |
|
3684 |
__READING__(f) |
|
3685 |
} |
|
3686 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3687 |
||
3688 |
if (ret > 0) { |
|
3689 |
pos = __INST(position); |
|
3690 |
if (__isSmallInteger(pos)) { |
|
3691 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3692 |
} else { |
|
3693 |
__INST(position) = nil; |
|
3694 |
} |
|
3695 |
if (__INST(binary) == true) { |
|
3696 |
RETURN ( __MKSMALLINT(ch) ); |
|
3697 |
} |
|
3698 |
RETURN ( __MKCHARACTER(ch) ); |
|
3699 |
} |
|
3700 |
||
3701 |
__INST(position) = nil; |
|
3702 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3703 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3704 |
} else /* ret == 0 */ { |
3705 |
__INST(hitEOF) = true; |
|
3706 |
} |
|
3707 |
} |
|
3708 |
%}. |
|
3709 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3710 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3711 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3712 |
^ self errorWriteOnly |
|
3713 |
! |
|
3714 |
||
3715 |
next:count |
|
3716 |
"return the next count elements of the stream as a collection. |
|
3717 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
3718 |
||
3719 |
|coll nRead| |
|
3720 |
||
3721 |
binary ifTrue:[ |
|
3722 |
coll := ByteArray uninitializedNew:count |
|
3723 |
] ifFalse:[ |
|
3724 |
coll := String new:count |
|
3725 |
]. |
|
3726 |
nRead := self nextBytes:count into:coll startingAt:1. |
|
3727 |
||
3728 |
"/ for readStream protocol compatibility, |
|
3729 |
"/ we must raise an exception here. |
|
3730 |
||
3731 |
nRead < count ifTrue:[ |
|
3732 |
^ self pastEnd |
|
3733 |
]. |
|
3734 |
^ coll |
|
3735 |
||
3736 |
"Modified: 11.1.1997 / 17:44:17 / cg" |
|
3737 |
! |
|
3738 |
||
3739 |
peek |
|
3740 |
"return the element to be read next without advancing read position. |
|
3741 |
In binary mode, an integer is returned, otherwise a character. |
|
3742 |
If there are no more elements, nil is returned." |
|
3743 |
||
3744 |
%{ |
|
3745 |
FILEPOINTER f; |
|
3746 |
unsigned char c; |
|
3747 |
int ret, _buffered; |
|
3748 |
OBJ fp; |
|
3749 |
||
3750 |
#if 0 |
|
3751 |
if (__INST(readAhead) != nil) { |
|
3752 |
RETURN (__INST(readAhead)); |
|
3753 |
} |
|
3754 |
#endif |
|
3755 |
__INST(lastErrorNumber) = nil; |
|
3756 |
||
3757 |
if (((fp = __INST(filePointer)) != nil) |
|
3758 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3759 |
) { |
|
3760 |
f = __FILEVal(fp); |
|
3761 |
_buffered = (__INST(buffered) == true); |
|
3762 |
if (_buffered) { |
|
3763 |
__READING__(f) |
|
3764 |
} |
|
3765 |
__READBYTE__(ret, f, &c, _buffered); |
|
3766 |
||
3767 |
if (ret > 0) { |
|
3768 |
__UNGETC__(c, f, _buffered); |
|
3769 |
||
3770 |
if (__INST(binary) == true) { |
|
3771 |
RETURN ( __MKSMALLINT(c) ); |
|
3772 |
} |
|
3773 |
RETURN ( __MKCHARACTER(c) ); |
|
3774 |
} |
|
3775 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3776 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3777 |
} else /* ret == 0 */ { |
3778 |
__INST(hitEOF) = true; |
|
3779 |
} |
|
3780 |
} |
|
3781 |
%}. |
|
3782 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3783 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3784 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3785 |
^ self errorWriteOnly |
|
3786 |
! |
|
3787 |
||
3788 |
upToEnd |
|
3789 |
"return a collection of the elements up-to the end. |
|
3790 |
Return nil if the stream-end is reached before." |
|
3791 |
||
3792 |
|answerStream ch| |
|
3793 |
||
3794 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
3795 |
[self atEnd] whileFalse:[ |
|
3796 |
ch := self next. |
|
3797 |
ch notNil ifTrue:[ |
|
3798 |
answerStream nextPut:ch |
|
3799 |
] |
|
3800 |
]. |
|
3801 |
^ answerStream contents |
|
3802 |
||
3803 |
"Created: 25.4.1996 / 14:40:31 / cg" |
|
3804 |
! ! |
|
3805 |
||
3806 |
!ExternalStream methodsFor:'reimplemented for speed'! |
|
3807 |
||
3808 |
nextAlphaNumericWord |
|
3809 |
"read the next word (i.e. up to non letter-or-digit) after first |
|
3810 |
skipping any whiteSpace. |
|
3811 |
Return a string containing those characters. |
|
3812 |
There is a limit of 1023 characters in the word - if longer, |
|
3813 |
it is truncated." |
|
3814 |
||
3815 |
%{ /* STACK: 2000 */ |
|
3816 |
FILEPOINTER f; |
|
3817 |
int len; |
|
3818 |
char buffer[1024]; |
|
3819 |
unsigned char ch; |
|
3820 |
int cnt = 0; |
|
3821 |
int ret, _buffered; |
|
3822 |
OBJ fp; |
|
3823 |
||
3824 |
if (((fp = __INST(filePointer)) != nil) |
|
3825 |
&& (__INST(mode) != @symbol(writeonly)) |
|
3826 |
) { |
|
3827 |
f = __FILEVal(fp); |
|
3828 |
_buffered = (__INST(buffered) == true); |
|
3829 |
if (_buffered) { |
|
3830 |
__READING__(f) |
|
3831 |
} |
|
3832 |
||
3833 |
/* |
|
3834 |
* skip whiteSpace first ... |
|
3835 |
*/ |
|
3836 |
do { |
|
3837 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3838 |
if (ret > 0) |
|
3839 |
cnt++; |
|
3840 |
else |
|
3841 |
break; |
|
3842 |
} while ( |
|
3843 |
#ifndef NON_ASCII |
|
3844 |
(ch < ' ') || |
|
3845 |
#endif |
|
3846 |
(ch == ' ' ) || (ch == '\t') || (ch == '\r') |
|
3847 |
|| (ch == '\n') || (ch == 0x0b)); |
|
3848 |
||
3849 |
len = 0; |
|
3850 |
while (ret > 0 && |
|
3851 |
(((ch >= 'a') && (ch <= 'z')) || |
|
3852 |
((ch >= 'A') && (ch <= 'Z')) || |
|
3853 |
((ch >= '0') && (ch <= '9'))) |
|
3854 |
) { |
|
3855 |
buffer[len++] = ch; |
|
3856 |
if (len >= (sizeof(buffer)-1)) { |
|
3857 |
/* emergency */ |
|
3858 |
break; |
|
3859 |
} |
|
3860 |
__READBYTE__(ret, f, &ch, _buffered); |
|
3861 |
if (ret > 0) |
|
3862 |
cnt++; |
|
3863 |
else |
|
3864 |
break; |
|
3865 |
} |
|
3866 |
||
3867 |
if (ret <= 0) { |
|
3868 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
3869 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 3870 |
goto err; |
3871 |
} |
|
3872 |
else if (ret == 0) |
|
3873 |
__INST(hitEOF) = true; |
|
3874 |
} else { |
|
3875 |
__UNGETC__(ch, f, _buffered); |
|
3876 |
cnt--; |
|
3877 |
} |
|
3878 |
||
3879 |
if (__INST(position) != nil) { |
|
3880 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + cnt); |
|
3881 |
} |
|
3882 |
if (len != 0) { |
|
3883 |
buffer[len] = '\0'; |
|
3884 |
RETURN ( __MKSTRING_L(buffer, len) ); |
|
3885 |
} |
|
3886 |
} |
|
3887 |
err: ; |
|
3888 |
%}. |
|
3889 |
hitEOF ifTrue:[^ self pastEnd]. |
|
3890 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3891 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3892 |
^ self errorWriteOnly |
|
3893 |
! |
|
3894 |
||
3895 |
nextChunk |
|
3896 |
"return the next chunk, i.e. all characters up to the next |
|
3897 |
exclamation mark. Within the chunk, exclamation marks have to be doubled - |
|
3898 |
except within primitive code (this exception was added to make it easier |
|
3899 |
to edit primitive code with external editors). This means, that other |
|
3900 |
Smalltalks cannot always read chunks containing primitive code - but that |
|
3901 |
doesnt really matter, since C-primitives are an ST/X feature anyway. |
|
3902 |
Reimplemented here for more speed." |
|
3903 |
||
3904 |
|retVal outOfMemory buffer| |
|
3905 |
||
3906 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3907 |
binary ifTrue:[^ self errorBinary]. |
|
3908 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3909 |
||
3910 |
%{ /* STACK: 12000 */ |
|
3911 |
/* |
|
3912 |
* the main trick (and a bit of complication) here |
|
3913 |
* is to read the first 8k into a stack-buffer. |
|
3914 |
* Since most chunks fit into that, |
|
3915 |
* this avoids creating lots of garbage for thos small chunks. |
|
3916 |
*/ |
|
3917 |
FILEPOINTER f; |
|
3918 |
int nread = 0, done = 0; |
|
3919 |
unsigned char c, lastC, peekC; |
|
3920 |
int ret; |
|
3921 |
unsigned char *bufferPtr = (unsigned char *)0; |
|
3922 |
unsigned char fastBuffer[8000]; |
|
3923 |
REGISTER int index; |
|
3924 |
int currSize, fastFlag, _buffered; |
|
3925 |
int atBeginOfLine = 1, inPrimitive = 0; |
|
3926 |
int haveCR = 0, haveNL = 0; |
|
3927 |
||
3928 |
__INST(lastErrorNumber) = nil; |
|
3929 |
f = __FILEVal(__INST(filePointer)); |
|
3930 |
||
3931 |
_buffered = (__INST(buffered) == true); |
|
3932 |
if (_buffered) { |
|
3933 |
__READING__(f) |
|
3934 |
} |
|
3935 |
||
3936 |
/* |
|
3937 |
* skip spaces |
|
3938 |
*/ |
|
3939 |
c = '\n'; |
|
3940 |
while (! done) { |
|
3941 |
lastC = c; |
|
3942 |
__READBYTE__(ret, f, &c, _buffered); |
|
3943 |
if (ret <= 0) { |
|
3944 |
goto err; |
|
3945 |
} |
|
3946 |
nread++; |
|
3947 |
atBeginOfLine = 0; |
|
3948 |
switch (c) { |
|
3949 |
case '\n': |
|
3950 |
case ' ': |
|
3951 |
case '\t': |
|
3952 |
case '\r': |
|
3953 |
case '\b': |
|
3954 |
case '\014': |
|
3955 |
break; |
|
3956 |
||
3957 |
default: |
|
3958 |
atBeginOfLine = (lastC == '\n'); |
|
3959 |
done = 1; |
|
3960 |
break; |
|
3961 |
} |
|
3962 |
} |
|
3963 |
||
3964 |
/* |
|
3965 |
* read chunk into a buffer |
|
3966 |
*/ |
|
3967 |
bufferPtr = fastBuffer; fastFlag = 1; |
|
3968 |
currSize = sizeof(fastBuffer); |
|
3969 |
||
3970 |
index = 0; |
|
3971 |
for (;;) { |
|
3972 |
__READBYTE__(ret, f, &peekC, _buffered); |
|
3973 |
if ((c == '%') && atBeginOfLine && ret > 0) { |
|
3974 |
if (peekC == '{') { |
|
3975 |
inPrimitive = 1; |
|
3976 |
} else if (peekC == '}') { |
|
3977 |
inPrimitive = 0; |
|
3978 |
} |
|
3979 |
} else if (c == '!' && !inPrimitive) { |
|
3980 |
if (ret > 0 && peekC == '!') { |
|
3981 |
/* |
|
3982 |
* convert double-! to a single ! |
|
3983 |
*/ |
|
3984 |
__READBYTE__(ret, f, &peekC, _buffered); |
|
3985 |
if (ret > 0) |
|
3986 |
nread++; |
|
3987 |
} else { |
|
3988 |
/* |
|
3989 |
* End of chunk, push back lookahead character |
|
3990 |
* and leave loop. |
|
3991 |
*/ |
|
3992 |
if (ret > 0) { |
|
3993 |
__UNGETC__(peekC, f, _buffered); |
|
3994 |
} else |
|
3995 |
ret = 1; |
|
3996 |
break; |
|
3997 |
} |
|
3998 |
} |
|
3999 |
||
4000 |
/* |
|
4001 |
* do we have to resize the buffer ? |
|
4002 |
*/ |
|
4003 |
if ((index+2) >= currSize) { |
|
4004 |
OBJ newBuffer; |
|
4005 |
unsigned char *nbp; |
|
4006 |
||
4007 |
newBuffer = __MKEMPTYSTRING(currSize * 2); |
|
4008 |
if (!fastFlag) { |
|
4009 |
/* |
|
4010 |
* refetch - buffer could be moved by GC |
|
4011 |
*/ |
|
4012 |
bufferPtr = __stringVal(buffer); |
|
4013 |
} |
|
4014 |
if (newBuffer == nil) { |
|
4015 |
/* |
|
4016 |
* mhmh - chunk seems to be very big .... |
|
4017 |
*/ |
|
4018 |
outOfMemory = true; |
|
4019 |
goto err; |
|
4020 |
} |
|
4021 |
nbp = __stringVal(newBuffer); |
|
4022 |
bcopy(bufferPtr, nbp, index); |
|
4023 |
bufferPtr = nbp; |
|
4024 |
bufferPtr[index] = '\0'; |
|
4025 |
buffer = newBuffer; |
|
4026 |
fastFlag = 0; |
|
4027 |
currSize = currSize * 2; |
|
4028 |
} |
|
4029 |
||
4030 |
if (!fastFlag) { |
|
4031 |
/* |
|
4032 |
* old buffer may have moved (interrupt while reading) |
|
4033 |
* - refetch pointer |
|
4034 |
*/ |
|
4035 |
bufferPtr = __stringVal(buffer); |
|
4036 |
} |
|
4037 |
||
4038 |
/* |
|
4039 |
* filter out cr-nl; make it nl |
|
4040 |
*/ |
|
4041 |
if (c == '\n') { |
|
4042 |
haveNL = 1; |
|
4043 |
if (index > 0) { |
|
4044 |
if (bufferPtr[index-1] == '\r') { |
|
4045 |
index--; |
|
4046 |
} |
|
4047 |
} |
|
4048 |
} else { |
|
4049 |
if (c == '\r') { |
|
4050 |
haveCR = 1; |
|
4051 |
} |
|
4052 |
} |
|
4053 |
bufferPtr[index++] = c; |
|
4054 |
if (ret <= 0) { |
|
4055 |
if (ret == 0) |
|
4056 |
break; /* End of chunk reached */ |
|
4057 |
goto err; |
|
4058 |
} |
|
4059 |
nread++; |
|
4060 |
atBeginOfLine = (c == '\n'); |
|
4061 |
c = peekC; |
|
4062 |
} |
|
4063 |
||
4064 |
/* |
|
4065 |
* care for CR-limited files. |
|
4066 |
* Replace CR's by NL's |
|
4067 |
*/ |
|
4068 |
if (!haveNL && haveCR) { |
|
4069 |
int i; |
|
4070 |
||
4071 |
for (i=index-1; i>=0; i--) { |
|
4072 |
if (bufferPtr[i] == '\r') { |
|
4073 |
bufferPtr[i] = '\n'; |
|
4074 |
} |
|
4075 |
} |
|
4076 |
} |
|
4077 |
||
4078 |
/* |
|
4079 |
* make it a string |
|
4080 |
* be careful here - allocating a new string may make the |
|
4081 |
* existing buffer move around. Need to check if copying from |
|
4082 |
* fast (C) buffer or from real (ST) buffer. |
|
4083 |
*/ |
|
4084 |
if (fastFlag) { |
|
4085 |
retVal = __MKSTRING_L(bufferPtr, index); |
|
4086 |
} else { |
|
4087 |
retVal = __MKSTRING_ST_L(buffer, index); |
|
4088 |
} |
|
4089 |
||
4090 |
err: ; |
|
4091 |
if (ret <= 0) { |
|
4092 |
if (ret == 0) |
|
4093 |
__INST(hitEOF) = true; |
|
4094 |
else /* ret < 0 */ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4095 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4096 |
} |
4097 |
if (__INST(position) != nil) { |
|
4098 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + nread); |
|
4099 |
} |
|
4100 |
%}. |
|
4101 |
retVal isNil ifTrue:[ |
|
4102 |
"/ |
|
4103 |
"/ arrive here with retVal==nil either on error or premature EOF |
|
4104 |
"/ or if running out of malloc-memory |
|
4105 |
"/ |
|
4106 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4107 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4108 |
outOfMemory == true ifTrue:[ |
|
4109 |
" |
|
4110 |
buffer memory allocation failed. |
|
4111 |
When we arrive here, there was no memory available for the |
|
4112 |
chunk. (seems to be too big of a chunk ...) |
|
4113 |
Bad luck - you should increase the ulimit and/or swap space on your machine. |
|
4114 |
" |
|
4115 |
^ ObjectMemory allocationFailureSignal raise. |
|
4116 |
] |
|
4117 |
]. |
|
4118 |
^ retVal |
|
4119 |
! |
|
4120 |
||
4121 |
nextMatchFor:anObject |
|
4122 |
"skip all objects up-to and including anObject, return anObject on success, |
|
4123 |
nil if end-of-file is reached before. The next read operation will return |
|
4124 |
the element after anObject. |
|
4125 |
Only single byte characters are currently supported." |
|
4126 |
||
4127 |
%{ |
|
4128 |
FILEPOINTER f; |
|
4129 |
int ret, peekValue; |
|
4130 |
char c; |
|
4131 |
OBJ fp; |
|
4132 |
||
4133 |
__INST(lastErrorNumber) = nil; |
|
4134 |
if (((fp = __INST(filePointer)) != nil) |
|
4135 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4136 |
) { |
|
4137 |
if ((__INST(binary) == true) && __isSmallInteger(anObject)) { |
|
4138 |
peekValue = __intVal(anObject) & 0xFF; |
|
4139 |
} else { |
|
4140 |
if ((__INST(binary) != true) && __isCharacter(anObject)) { |
|
4141 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
4142 |
} else { |
|
4143 |
peekValue = -1; |
|
4144 |
} |
|
4145 |
} |
|
4146 |
||
4147 |
if (peekValue >= 0) { |
|
4148 |
int _buffered; |
|
4149 |
||
4150 |
__INST(position) = nil; |
|
4151 |
f = __FILEVal(fp); |
|
4152 |
||
4153 |
if (_buffered = (__INST(buffered) == true)) { |
|
4154 |
__READING__(f) |
|
4155 |
} |
|
4156 |
||
4157 |
for (;;) { |
|
4158 |
__READBYTE__(ret, f, &c, _buffered); |
|
4159 |
if (ret <= 0) { |
|
4160 |
if (ret < 0) |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4161 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4162 |
else |
4163 |
__INST(hitEOF) = true; |
|
4164 |
break; |
|
4165 |
} |
|
4166 |
if (c == peekValue) { |
|
4167 |
RETURN (anObject); |
|
4168 |
} |
|
4169 |
} |
|
4170 |
} |
|
4171 |
} |
|
4172 |
%}. |
|
4173 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4174 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4175 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4176 |
^ super nextMatchFor:anObject |
|
4177 |
! |
|
4178 |
||
4179 |
peekFor:anObject |
|
4180 |
"return true and move past next element, if next == something. |
|
4181 |
Otherwise, stay and return false. False is also returned |
|
4182 |
when EOF is encountered. |
|
4183 |
The argument must be an integer if in binary, a character if in |
|
4184 |
text mode; only single byte characters are currently supported." |
|
4185 |
||
4186 |
%{ |
|
4187 |
FILEPOINTER f; |
|
4188 |
unsigned char c; |
|
4189 |
int ret, peekValue, _buffered; |
|
4190 |
OBJ fp; |
|
4191 |
||
4192 |
__INST(lastErrorNumber) = nil; |
|
4193 |
if (((fp = __INST(filePointer)) != nil) |
|
4194 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4195 |
) { |
|
4196 |
if (__INST(binary) == true) { |
|
4197 |
if (__isSmallInteger(anObject)) { |
|
4198 |
peekValue = __intVal(anObject) & 0xFF; |
|
4199 |
} else { |
|
4200 |
goto bad; |
|
4201 |
} |
|
4202 |
} else { |
|
4203 |
if (__isCharacter(anObject)) { |
|
4204 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
4205 |
} else { |
|
4206 |
goto bad; |
|
4207 |
} |
|
4208 |
} |
|
4209 |
||
4210 |
f = __FILEVal(fp); |
|
4211 |
||
4212 |
_buffered = (__INST(buffered) == true); |
|
4213 |
if (_buffered) { |
|
4214 |
__READING__(f) |
|
4215 |
} |
|
4216 |
__READBYTE__(ret, f, &c, _buffered); |
|
4217 |
if (ret > 0) { |
|
4218 |
if (c == peekValue) { |
|
4219 |
OBJ pos; |
|
4220 |
||
4221 |
if ((pos = __INST(position)) != nil) { |
|
4222 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
4223 |
} |
|
4224 |
RETURN (true); |
|
4225 |
} |
|
4226 |
__UNGETC__(c, f, _buffered); |
|
4227 |
RETURN (false); |
|
4228 |
} |
|
4229 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4230 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4231 |
} else /* ret == 0 */ |
4232 |
__INST(hitEOF) = true; |
|
4233 |
} |
|
4234 |
bad: ; |
|
4235 |
%}. |
|
4236 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4237 |
hitEOF ifTrue:[self pastEnd. ^ false]. |
|
4238 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4239 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4240 |
^ super peekFor:anObject |
|
4241 |
! |
|
4242 |
||
4243 |
skipLine |
|
4244 |
"read the next line (characters up to newline) skip only; |
|
4245 |
return nil if EOF reached, self otherwise. |
|
4246 |
Not allowed in binary mode." |
|
4247 |
||
4248 |
%{ /* STACK:2000 */ |
|
4249 |
||
4250 |
FILEPOINTER f; |
|
4251 |
OBJ fp; |
|
4252 |
int ret, _buffered; |
|
4253 |
char c; |
|
4254 |
||
4255 |
__INST(lastErrorNumber) = nil; |
|
4256 |
if (((fp = __INST(filePointer)) != nil) |
|
4257 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4258 |
&& (__INST(binary) != true) |
|
4259 |
) { |
|
4260 |
f = __FILEVal(fp); |
|
4261 |
if (_buffered = (__INST(buffered) == true)) { |
|
4262 |
__READING__(f) |
|
4263 |
} |
|
4264 |
for (;;) { |
|
4265 |
__READBYTE__(ret, f, &c, _buffered); |
|
4266 |
if (ret > 0) { |
|
4267 |
if (c == '\n') |
|
4268 |
RETURN(self) |
|
4269 |
} else |
|
4270 |
break; |
|
4271 |
} |
|
4272 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4273 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4274 |
} else /* ret == 0 */ { |
4275 |
__INST(hitEOF) = true; |
|
4276 |
} |
|
4277 |
} |
|
4278 |
%}. |
|
4279 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4280 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
|
4281 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4282 |
binary ifTrue:[self errorBinary. ^ self]. |
|
4283 |
self errorWriteOnly |
|
4284 |
! |
|
4285 |
||
4286 |
skipSeparators |
|
4287 |
"skip all whitespace; next will return next non-white-space character |
|
4288 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
4289 |
- reimplemented for speed" |
|
4290 |
||
4291 |
%{ |
|
4292 |
FILEPOINTER f; |
|
4293 |
char c; |
|
4294 |
int ret, _buffered; |
|
4295 |
OBJ fp; |
|
4296 |
||
4297 |
__INST(lastErrorNumber) = nil; |
|
4298 |
if (((fp = __INST(filePointer)) != nil) |
|
4299 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4300 |
&& (__INST(binary) != true) |
|
4301 |
) { |
|
4302 |
f = __FILEVal(fp); |
|
4303 |
_buffered = (__INST(buffered) == true); |
|
4304 |
if (_buffered) { |
|
4305 |
__READING__(f) |
|
4306 |
} |
|
4307 |
||
4308 |
while (1) { |
|
4309 |
__READBYTE__(ret, f, &c, _buffered); |
|
4310 |
if (ret <= 0) { |
|
4311 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4312 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4313 |
} else /* ret == 0 */ { |
4314 |
__INST(hitEOF) = true; |
|
4315 |
} |
|
4316 |
break; |
|
4317 |
} |
|
4318 |
switch (c) { |
|
4319 |
case ' ': |
|
4320 |
case '\t': |
|
4321 |
case '\n': |
|
4322 |
case '\r': |
|
4323 |
case '\b': |
|
4324 |
case '\f': |
|
4325 |
break; |
|
4326 |
||
4327 |
default: |
|
4328 |
__UNGETC__(c, f, _buffered); |
|
4329 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4330 |
} |
|
4331 |
} |
|
4332 |
} |
|
4333 |
%}. |
|
4334 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4335 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4336 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4337 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
4338 |
^ self errorBinary. |
|
4339 |
! |
|
4340 |
||
4341 |
skipSeparatorsExceptCR |
|
4342 |
"skip all whitespace but no newlines; |
|
4343 |
next will return next non-white-space character |
|
4344 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
4345 |
- reimplemented for speed" |
|
4346 |
||
4347 |
%{ |
|
4348 |
FILEPOINTER f; |
|
4349 |
char c; |
|
4350 |
int ret, _buffered; |
|
4351 |
OBJ fp; |
|
4352 |
||
4353 |
__INST(lastErrorNumber) = nil; |
|
4354 |
if (((fp = __INST(filePointer)) != nil) |
|
4355 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4356 |
&& (__INST(binary) != true) |
|
4357 |
) { |
|
4358 |
f = __FILEVal(fp); |
|
4359 |
_buffered = (__INST(buffered) == true); |
|
4360 |
if (_buffered) { |
|
4361 |
__READING__(f) |
|
4362 |
} |
|
4363 |
while (1) { |
|
4364 |
__READBYTE__(ret, f, &c, _buffered); |
|
4365 |
if (ret <= 0) { |
|
4366 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4367 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4368 |
} else /* ret == 0 */ { |
4369 |
__INST(hitEOF) = true; |
|
4370 |
} |
|
4371 |
break; |
|
4372 |
} |
|
4373 |
switch (c) { |
|
4374 |
case ' ': |
|
4375 |
case '\t': |
|
4376 |
case '\b': |
|
4377 |
case '\f': |
|
4378 |
break; |
|
4379 |
||
4380 |
default: |
|
4381 |
__UNGETC__(c, f, _buffered); |
|
4382 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
|
4383 |
} |
|
4384 |
} |
|
4385 |
} |
|
4386 |
%}. |
|
4387 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4388 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4389 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4390 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
4391 |
^ self errorBinary. |
|
4392 |
! |
|
4393 |
||
4394 |
skipThrough:aCharacter |
|
4395 |
"skip all characters up-to and including aCharacter. Return the receiver if |
|
4396 |
skip was successful, otherwise (i.e. if not found) return nil. |
|
4397 |
The next read operation will return the character after aCharacter. |
|
4398 |
The argument, aCharacter must be character, or integer when in binary mode. |
|
4399 |
Only single byte characters are currently supported." |
|
4400 |
||
4401 |
%{ |
|
4402 |
FILEPOINTER f; |
|
4403 |
REGISTER int cSearch; |
|
4404 |
unsigned char c; |
|
4405 |
int ret, _buffered; |
|
4406 |
OBJ fp; |
|
4407 |
||
4408 |
__INST(lastErrorNumber) = nil; |
|
4409 |
if (((fp = __INST(filePointer)) != nil) |
|
4410 |
&& (__INST(mode) != @symbol(writeonly)) |
|
4411 |
) { |
|
4412 |
if (__INST(binary) == true) { |
|
4413 |
/* searched for object must be a smallInteger */ |
|
4414 |
if (! __isSmallInteger(aCharacter)) goto badArgument; |
|
4415 |
cSearch = __intVal(aCharacter); |
|
4416 |
} else { |
|
4417 |
/* searched for object must be a character */ |
|
4418 |
if (! __isCharacter(aCharacter)) goto badArgument; |
|
4419 |
cSearch = __intVal(_characterVal(aCharacter)); |
|
4420 |
} |
|
4421 |
/* Q: should we just say: "not found" ? */ |
|
4422 |
if ((cSearch < 0) || (cSearch > 255)) goto badArgument; |
|
4423 |
||
4424 |
f = __FILEVal(fp); |
|
4425 |
if (_buffered = (__INST(buffered) == true)) { |
|
4426 |
__READING__(f) |
|
4427 |
} |
|
4428 |
||
4429 |
while (1) { |
|
4430 |
__READBYTE__(ret, f, &c, _buffered); |
|
4431 |
if (ret <= 0) { |
|
4432 |
if (ret < 0) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4433 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4434 |
} else /* ret == 0 */ { |
4435 |
__INST(hitEOF) = true; |
|
4436 |
} |
|
4437 |
break; |
|
4438 |
} |
|
4439 |
if (c == cSearch) { |
|
4440 |
RETURN (self); |
|
4441 |
} |
|
4442 |
} |
|
4443 |
} |
|
4444 |
badArgument: ; |
|
4445 |
%}. |
|
4446 |
hitEOF ifTrue:[^ self pastEnd]. |
|
4447 |
lastErrorNumber notNil ifTrue:[self readError. ^ self]. |
|
4448 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4449 |
(mode == #writeonly) ifTrue:[self errorWriteOnly. ^ self]. |
|
4450 |
" |
|
4451 |
argument must be integer/character in binary mode, |
|
4452 |
character in text mode |
|
4453 |
" |
|
4454 |
self error:'invalid argument'. |
|
4455 |
||
4456 |
" |
|
4457 |
|s| |
|
4458 |
s := 'Makefile' asFilename readStream. |
|
4459 |
s skipThrough:$=. |
|
4460 |
s next:10 |
|
4461 |
" |
|
4462 |
! |
|
4463 |
||
4464 |
skipToAll:aString |
|
4465 |
"skip for the sequence given by the argument, aCollection; |
|
4466 |
return nil if not found, self otherwise. |
|
4467 |
On a successful match, the next read will return characters of aString." |
|
4468 |
||
4469 |
|oldPos| |
|
4470 |
||
4471 |
oldPos := self position. |
|
4472 |
(self skipThroughAll:aString) isNil ifTrue:[ |
|
4473 |
" |
|
4474 |
restore position |
|
4475 |
" |
|
4476 |
self position:oldPos. |
|
4477 |
^ nil |
|
4478 |
]. |
|
4479 |
" |
|
4480 |
position before match-string |
|
4481 |
" |
|
4482 |
self position:(self position - aString size). |
|
4483 |
^ self |
|
4484 |
||
4485 |
" |
|
4486 |
|s| |
|
4487 |
s := 'Makefile' asFilename readStream. |
|
4488 |
s skipToAll:'are'. |
|
4489 |
s next:10 |
|
4490 |
" |
|
4491 |
||
4492 |
"Modified: 26.6.1996 / 09:22:05 / cg" |
|
4493 |
! ! |
|
4494 |
||
4495 |
!ExternalStream methodsFor:'testing'! |
|
4496 |
||
4497 |
atEnd |
|
4498 |
"return true, if position is at end" |
|
4499 |
||
4500 |
%{ |
|
4501 |
FILEPOINTER f; |
|
7099 | 4502 |
OBJ fp, pos; |
3746 | 4503 |
char c; |
7099 | 4504 |
int p, ret, _buffered; |
3746 | 4505 |
|
4506 |
if (__INST(hitEOF) == true) { |
|
7099 | 4507 |
RETURN (true); |
3746 | 4508 |
} |
4509 |
||
7099 | 4510 |
pos = __INST(position); |
4511 |
if (__bothSmallInteger(pos, __INST(readLimit))) { |
|
4512 |
int p = __intVal(pos); |
|
4513 |
||
4514 |
/* make 1-based */ |
|
4515 |
p = p - __intVal( @global(PositionableStream:ZeroPosition)) + 1; |
|
4516 |
if (p > __intVal(__INST(readLimit))) { |
|
4517 |
RETURN (true); |
|
4518 |
} |
|
3907 | 4519 |
} |
3746 | 4520 |
__INST(lastErrorNumber) = nil; |
4521 |
||
4522 |
if ((fp = __INST(filePointer)) != nil) { |
|
7099 | 4523 |
f = __FILEVal(fp); |
4524 |
||
4525 |
if (_buffered = (__INST(buffered) == true)) { |
|
4526 |
__READING__(f); |
|
4527 |
} else { |
|
4528 |
if (__INST(readAhead) != nil) { |
|
4529 |
RETURN (false); |
|
4530 |
} |
|
4531 |
} |
|
3746 | 4532 |
|
5121 | 4533 |
#ifdef DOES_NOT_WORK |
7099 | 4534 |
{ |
4535 |
int res; |
|
4536 |
||
4537 |
/* |
|
4538 |
* cannot read a pipe - returns EIO instead of EPIPE on w95 |
|
4539 |
*/ |
|
4540 |
if (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)), 0, 0, 0, &res, 0)) { |
|
4541 |
if (res > 0) { |
|
4542 |
RETURN (false); |
|
4543 |
} |
|
4544 |
RETURN (true); |
|
4545 |
} |
|
4546 |
} |
|
5121 | 4547 |
#endif |
7099 | 4548 |
/* |
4549 |
* read ahead ... |
|
4550 |
*/ |
|
4551 |
do { |
|
3746 | 4552 |
#ifdef NO_STDIO |
7099 | 4553 |
__READBYTE__(ret, f, &c, _buffered); |
5121 | 4554 |
|
3746 | 4555 |
#else |
7099 | 4556 |
__BEGIN_INTERRUPTABLE__ |
4557 |
__READBYTE__(ret, f, &c, _buffered); |
|
4558 |
__END_INTERRUPTABLE__ |
|
3746 | 4559 |
#endif |
7099 | 4560 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3746 | 4561 |
|
4562 |
#if defined(WIN32) && defined(EPIPE) |
|
7099 | 4563 |
if (ret < 0) { |
4564 |
if (__threadErrno == EPIPE) { |
|
4565 |
ret = 0; |
|
4566 |
__threadErrno = 0; |
|
4567 |
} |
|
4568 |
if (__threadErrno == 0) { |
|
4569 |
ret = 0; /* how comes ? */ |
|
4570 |
} |
|
4571 |
} |
|
3746 | 4572 |
#endif |
7099 | 4573 |
if (ret > 0) { |
4574 |
__UNGETC__(c&0xff, f, _buffered); |
|
4575 |
RETURN (false); |
|
4576 |
} |
|
4577 |
||
4578 |
if (ret == 0) { |
|
4579 |
__INST(hitEOF) = true; |
|
4580 |
RETURN (true); |
|
4581 |
} |
|
4582 |
||
4583 |
/* ret < 0 */ |
|
4584 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
|
3746 | 4585 |
} |
4586 |
%}. |
|
4587 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
4588 |
^ self errorNotOpen |
|
4589 |
! |
|
4590 |
||
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4591 |
canBeSelected |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4592 |
"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
|
4593 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4594 |
^ OperatingSystem supportsSelect |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4595 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4596 |
"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
|
4597 |
"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
|
4598 |
! |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4599 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4600 |
canReadOrWriteWithoutBlocking |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4601 |
"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
|
4602 |
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
|
4603 |
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
|
4604 |
fReturn alse otherwise." |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4605 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4606 |
readAhead notNil ifTrue:[^ true]. |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4607 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4608 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4609 |
^ OperatingSystem readWriteCheck:(self fileDescriptor) |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4610 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4611 |
"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
|
4612 |
! |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4613 |
|
3746 | 4614 |
canReadWithoutBlocking |
4615 |
"return true, if any data is available for reading (i.e. |
|
4616 |
a read operation will not block the smalltalk process), false otherwise." |
|
4617 |
||
4618 |
readAhead notNil ifTrue:[^ true]. |
|
4619 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4620 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4621 |
||
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4622 |
^ OperatingSystem readCheck:(self fileDescriptor) |
3746 | 4623 |
|
4624 |
" |
|
4625 |
|pipe| |
|
4626 |
||
4627 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
4628 |
pipe canReadWithoutBlocking ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4629 |
Transcript showCR:'data available' |
3746 | 4630 |
] ifFalse:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4631 |
Transcript showCR:'no data available' |
3746 | 4632 |
]. |
4633 |
pipe close |
|
4634 |
" |
|
4635 |
||
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4636 |
"Modified: / 25.9.1997 / 13:08:45 / stefan" |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4637 |
"Modified: / 14.12.1999 / 19:57:08 / cg" |
3746 | 4638 |
! |
4639 |
||
4640 |
canWriteWithoutBlocking |
|
4641 |
"return true, if data can be written into the stream |
|
4642 |
(i.e. a write operation will not block the smalltalk process)." |
|
4643 |
||
4644 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4645 |
mode == #readonly ifTrue:[^ self errorReadOnly]. |
|
4646 |
||
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4647 |
^ OperatingSystem writeCheck:(self fileDescriptor) |
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4648 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4649 |
"Modified: / 14.12.1999 / 19:56:58 / cg" |
3746 | 4650 |
! |
4651 |
||
4652 |
numAvailable |
|
4653 |
"return the number of bytes available for reading" |
|
4654 |
||
4655 |
|fd| |
|
4656 |
||
4657 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4658 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4659 |
||
4660 |
fd := self fileDescriptor. |
|
4661 |
^ OperatingSystem numAvailableForReadOn:fd. |
|
4662 |
||
4663 |
"/ self atEnd ifTrue:[^ 0]. |
|
4664 |
"/ self canReadWithoutBlocking ifTrue:[ |
|
4665 |
"/ ^ 1 |
|
4666 |
"/ ]. |
|
4667 |
"/ ^ 0 |
|
4668 |
||
4669 |
"Created: / 4.2.1998 / 16:56:01 / cg" |
|
4670 |
"Modified: / 4.2.1998 / 17:31:11 / cg" |
|
4671 |
! ! |
|
4672 |
||
4673 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
4674 |
||
4675 |
readWait |
|
4676 |
"suspend the current process, until the receiver |
|
4677 |
becomes ready for reading. If data is already available, |
|
4678 |
return immediate. |
|
4679 |
The other threads are not affected by the wait." |
|
4680 |
||
4681 |
self readWaitWithTimeoutMs:nil |
|
4682 |
! |
|
4683 |
||
4684 |
readWaitTimeoutMs:timeout |
|
4685 |
"ST-80 compatibility" |
|
4686 |
^ self readWaitWithTimeoutMs:timeout |
|
4687 |
! |
|
4688 |
||
4689 |
readWaitWithTimeout:timeout |
|
4690 |
"suspend the current process, until the receiver |
|
4691 |
becomes ready for reading or a timeout (in seconds) expired. |
|
4692 |
If data is already available, return immediate. |
|
4693 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4694 |
The other threads are not affected by the wait." |
|
4695 |
||
4696 |
^ self readWaitWithTimeoutMs:timeout * 1000 |
|
4697 |
! |
|
4698 |
||
4699 |
readWaitWithTimeoutMs:timeout |
|
4700 |
"suspend the current process, until the receiver |
|
4701 |
becomes ready for reading or a timeout (in milliseconds) expired. |
|
4702 |
If data is already available, return immediate. |
|
4703 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4704 |
The other threads are not affected by the wait." |
|
4705 |
||
4706 |
|fd inputSema hasData wasBlocked| |
|
4707 |
||
4708 |
readAhead notNil ifTrue:[^ false]. |
|
4709 |
||
4710 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
4711 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
4712 |
||
4713 |
fd := self fileDescriptor. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4714 |
(self canReadWithoutBlocking) ifTrue:[^ false]. |
3746 | 4715 |
|
4716 |
wasBlocked := OperatingSystem blockInterrupts. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4717 |
hasData := self canReadWithoutBlocking. |
3746 | 4718 |
hasData ifFalse:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4719 |
inputSema := Semaphore new name:'readWait'. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4720 |
[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4721 |
timeout notNil ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4722 |
Processor signal:inputSema afterMilliseconds:timeout |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4723 |
]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4724 |
Processor signal:inputSema onInputStream:self. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4725 |
Processor activeProcess state:#ioWait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4726 |
inputSema wait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4727 |
Processor disableSemaphore:inputSema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4728 |
hasData := self canReadWithoutBlocking |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4729 |
] valueOnUnwindDo:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4730 |
Processor disableSemaphore:inputSema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4731 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4732 |
] |
3746 | 4733 |
]. |
4734 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4735 |
^ hasData not |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4736 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4737 |
"Modified: / 14.12.1999 / 19:52:28 / cg" |
3746 | 4738 |
! |
4739 |
||
4740 |
readWriteWait |
|
4741 |
"suspend the current process, until the receiver |
|
4742 |
becomes ready for writing or reading. |
|
4743 |
Return immediate if the receiver is already ready. |
|
4744 |
The other threads are not affected by the wait. |
|
4745 |
This is actually only used with sockets, to wait for a connect to |
|
4746 |
be finished." |
|
4747 |
||
4748 |
self readWriteWaitWithTimeoutMs:nil |
|
4749 |
! |
|
4750 |
||
4751 |
readWriteWaitWithTimeoutMs:timeout |
|
4752 |
"suspend the current process, until the receiver |
|
4753 |
becomes ready for reading or writing or a timeout (in seconds) expired. |
|
4754 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4755 |
Return immediate if the receiver is already ready. |
|
4756 |
The other threads are not affected by the wait. |
|
4757 |
This is actually only used with sockets, to wait for a connect to |
|
4758 |
be finished." |
|
4759 |
||
4760 |
|fd sema canread canReadWrite wasBlocked| |
|
4761 |
||
4762 |
filePointer isNil ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4763 |
^ self errorNotOpen |
3746 | 4764 |
]. |
4765 |
||
4766 |
fd := self fileDescriptor. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4767 |
(self canReadOrWriteWithoutBlocking) ifTrue:[^ false]. |
3746 | 4768 |
|
4769 |
wasBlocked := OperatingSystem blockInterrupts. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4770 |
canReadWrite := self canReadOrWriteWithoutBlocking. |
3746 | 4771 |
canReadWrite ifFalse:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4772 |
sema := Semaphore new name:'readWriteWait'. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4773 |
[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4774 |
timeout notNil ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4775 |
Processor signal:sema afterMilliseconds:timeout |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4776 |
]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4777 |
Processor signal:sema onOutputStream:self. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4778 |
Processor signal:sema onInputStream:self. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4779 |
Processor activeProcess state:#ioWait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4780 |
sema wait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4781 |
Processor disableSemaphore:sema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4782 |
canReadWrite := self canReadOrWriteWithoutBlocking |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4783 |
] valueOnUnwindDo:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4784 |
Processor disableSemaphore:sema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4785 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4786 |
] |
3746 | 4787 |
]. |
4788 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4789 |
^ canReadWrite not |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4790 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4791 |
"Modified: / 14.12.1999 / 19:58:03 / cg" |
3746 | 4792 |
! |
4793 |
||
4794 |
writeWait |
|
4795 |
"suspend the current process, until the receiver |
|
4796 |
becomes ready for writing. |
|
4797 |
Return immediate if the receiver is already ready. |
|
4798 |
The other threads are not affected by the wait." |
|
4799 |
||
4800 |
self writeWaitWithTimeoutMs:nil |
|
4801 |
! |
|
4802 |
||
4803 |
writeWaitTimeoutMs:timeout |
|
4804 |
"ST-80 compatibility" |
|
4805 |
^ self writeWaitWithTimeoutMs:timeout |
|
4806 |
! |
|
4807 |
||
4808 |
writeWaitWithTimeout:timeout |
|
4809 |
"suspend the current process, until the receiver |
|
4810 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4811 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4812 |
Return immediate if the receiver is already ready. |
|
4813 |
The other threads are not affected by the wait." |
|
4814 |
||
4815 |
^ self writeWaitWithTimeoutMs:timeout * 1000 |
|
4816 |
! |
|
4817 |
||
4818 |
writeWaitWithTimeoutMs:timeout |
|
4819 |
"suspend the current process, until the receiver |
|
4820 |
becomes ready for writing or a timeout (in seconds) expired. |
|
4821 |
Return true if a timeout occured (i.e. false, if data is available). |
|
4822 |
Return immediate if the receiver is already ready. |
|
4823 |
The other threads are not affected by the wait." |
|
4824 |
||
4825 |
|fd outputSema canWrite wasBlocked| |
|
4826 |
||
4827 |
filePointer isNil ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4828 |
^ self errorNotOpen |
3746 | 4829 |
]. |
4830 |
mode == #readonly ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4831 |
^ self errorReadOnly |
3746 | 4832 |
]. |
4833 |
||
4834 |
fd := self fileDescriptor. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4835 |
(self canWriteWithoutBlocking) ifTrue:[^ false]. |
3746 | 4836 |
|
4837 |
wasBlocked := OperatingSystem blockInterrupts. |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4838 |
canWrite := self canWriteWithoutBlocking. |
3746 | 4839 |
canWrite ifFalse:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4840 |
outputSema := Semaphore new name:'writeWait'. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4841 |
[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4842 |
timeout notNil ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4843 |
Processor signal:outputSema afterMilliseconds:timeout |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4844 |
]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4845 |
Processor signal:outputSema onOutputStream:self. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4846 |
Processor activeProcess state:#ioWait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4847 |
outputSema wait. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4848 |
Processor disableSemaphore:outputSema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4849 |
canWrite := self canWriteWithoutBlocking |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4850 |
] valueOnUnwindDo:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4851 |
Processor disableSemaphore:outputSema. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4852 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4853 |
] |
3746 | 4854 |
]. |
4855 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4856 |
^ canWrite not |
|
5102
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4857 |
|
f5ae1f96efb5
use new signal:onStream: interface to scheduler.
Claus Gittinger <cg@exept.de>
parents:
5100
diff
changeset
|
4858 |
"Modified: / 14.12.1999 / 19:54:31 / cg" |
3746 | 4859 |
! ! |
4860 |
||
4861 |
!ExternalStream methodsFor:'writing'! |
|
4862 |
||
4863 |
cr |
|
4864 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
4865 |
reimplemented for speed" |
|
4866 |
||
4867 |
%{ |
|
4868 |
FILEPOINTER f; |
|
4869 |
int len, cnt, _buffered; |
|
4870 |
OBJ fp; |
|
4871 |
char *cp; |
|
4872 |
||
4873 |
__INST(lastErrorNumber) = nil; |
|
4874 |
if (((fp = __INST(filePointer)) != nil) |
|
4875 |
&& (__INST(mode) != @symbol(readonly)) |
|
4876 |
&& (__INST(binary) != true) |
|
4877 |
) { |
|
4878 |
f = __FILEVal(fp); |
|
4879 |
||
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4880 |
if (_buffered = (__INST(buffered) == true)) { |
3746 | 4881 |
__WRITING__(f) |
4882 |
} |
|
4883 |
{ |
|
4884 |
OBJ mode = __INST(eolMode); |
|
4885 |
||
3823 | 4886 |
len = 1; |
3746 | 4887 |
if (mode == @symbol(cr)) { |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4888 |
cp = "\r"; |
3746 | 4889 |
} else if (mode == @symbol(crlf)) { |
4890 |
cp = "\r\n"; len = 2; |
|
4891 |
} else { |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4892 |
cp = "\n"; |
3746 | 4893 |
} |
4894 |
} |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4895 |
if ((f == stdout) || (f == stderr)) { |
4206 | 4896 |
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
|
4897 |
} else { |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4898 |
__WRITEBYTES__(cnt, f, cp, len, _buffered); |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4899 |
} |
3746 | 4900 |
|
4901 |
if (cnt == len) { |
|
4902 |
if (__INST(position) != nil) { |
|
4903 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + len); |
|
4904 |
} |
|
4905 |
RETURN ( self ); |
|
4906 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4907 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4908 |
} |
4909 |
%}. |
|
4910 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
4911 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4912 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4913 |
self errorBinary |
|
4914 |
! |
|
4915 |
||
4916 |
flush |
|
4917 |
"write all buffered data - ignored if unbuffered" |
|
4918 |
||
4919 |
%{ |
|
4920 |
OBJ fp; |
|
4921 |
||
4922 |
__INST(lastErrorNumber) = nil; |
|
4923 |
if ((fp = __INST(filePointer)) != nil) { |
|
4924 |
if (__INST(mode) != @symbol(readonly)) { |
|
4925 |
if (__INST(buffered) == true) { |
|
3823 | 4926 |
FILE *f = __FILEVal(fp); |
4927 |
||
4928 |
if ((f == stdout) || (f == stderr)) { |
|
4206 | 4929 |
fflush(f); |
3823 | 4930 |
} else { |
3746 | 4931 |
#ifdef NO_STDIO |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4932 |
FFLUSH(f); |
3746 | 4933 |
#else |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4934 |
__BEGIN_INTERRUPTABLE__ |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4935 |
FFLUSH(f); |
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4936 |
__END_INTERRUPTABLE__ |
3746 | 4937 |
#endif |
3823 | 4938 |
} |
3746 | 4939 |
} |
4940 |
} |
|
4941 |
} |
|
4942 |
%} |
|
4943 |
! |
|
4944 |
||
4945 |
nextPut:aCharacter |
|
4946 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
4947 |
Only single-byte characters are currently supported" |
|
4948 |
||
4949 |
%{ |
|
4950 |
FILEPOINTER f; |
|
4951 |
char c; |
|
4952 |
int cnt, _buffered; |
|
4953 |
OBJ pos, fp; |
|
4954 |
||
4955 |
__INST(lastErrorNumber) = nil; |
|
4956 |
if (((fp = __INST(filePointer)) != nil) |
|
4957 |
&& (__INST(mode) != @symbol(readonly)) |
|
4958 |
) { |
|
4959 |
if (__INST(binary) != true) { |
|
4960 |
if (__isCharacter(aCharacter)) { |
|
4961 |
c = __intVal(__characterVal(aCharacter)) & 0xFF; |
|
4962 |
doWrite: |
|
4963 |
f = __FILEVal(fp); |
|
4964 |
||
4965 |
||
4966 |
if (_buffered = (__INST(buffered) == true)) { |
|
4967 |
__WRITING__(f) |
|
4968 |
} |
|
3823 | 4969 |
if ((f == stdout) || (f == stderr)) { |
4206 | 4970 |
cnt = fwrite(&c, 1, 1, f); |
3823 | 4971 |
} else { |
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
4972 |
__WRITEBYTE__(cnt, f, &c, _buffered); |
3823 | 4973 |
} |
3746 | 4974 |
|
4975 |
if (cnt == 1) { |
|
4976 |
pos = __INST(position); |
|
4977 |
if (pos != nil) { |
|
4978 |
__INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
4979 |
} |
|
4980 |
RETURN ( self ); |
|
4981 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
4982 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 4983 |
} |
4984 |
} else { |
|
4985 |
if (__isSmallInteger(aCharacter)) { |
|
4986 |
c = __intVal(aCharacter); |
|
4987 |
goto doWrite; |
|
4988 |
} |
|
4989 |
} |
|
4990 |
} |
|
4991 |
%}. |
|
4992 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
4993 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
4994 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
4995 |
binary ifFalse:[self argumentMustBeCharacter. ^ self]. |
|
4996 |
self argumentMustBeInteger. |
|
4997 |
! |
|
4998 |
||
4999 |
nextPutAll:aCollection |
|
5000 |
"write all elements of the argument, aCollection. |
|
5001 |
Reimplemented for speed when writing strings or byteArrays. |
|
5002 |
For others, falls back to general method in superclass." |
|
5003 |
||
5004 |
%{ |
|
5005 |
FILEPOINTER f; |
|
5006 |
int len, cnt; |
|
5007 |
OBJ pos, fp; |
|
5008 |
int _buffered; |
|
5009 |
int o_offs; |
|
5010 |
||
5011 |
__INST(lastErrorNumber) = nil; |
|
5012 |
||
5013 |
if (((fp = __INST(filePointer)) != nil) |
|
5014 |
&& (__INST(mode) != @symbol(readonly)) |
|
5015 |
) { |
|
5016 |
f = __FILEVal(fp); |
|
5017 |
if (_buffered = (__INST(buffered) == true)) { |
|
5018 |
__WRITING__(f) |
|
5019 |
} |
|
5020 |
||
5021 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
5022 |
len = __stringSize(aCollection); |
|
3823 | 5023 |
if ((f == stdout) || (f == stderr)) { |
4206 | 5024 |
cnt = fwrite(__stringVal(aCollection), 1, len, f); |
3823 | 5025 |
} else { |
5026 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
5027 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
|
5028 |
} |
|
3746 | 5029 |
} else { |
5030 |
if (__INST(binary) == true) { |
|
5031 |
int offs; |
|
5032 |
||
5033 |
if (__isByteArray(aCollection)) { |
|
5034 |
offs = 0; |
|
5035 |
len = __byteArraySize(aCollection); |
|
5036 |
} else if (__isBytes(aCollection)) { |
|
5037 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
5038 |
len = __byteArraySize(aCollection) - offs; |
|
5039 |
} else |
|
5040 |
goto out; |
|
5041 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element) - (char *)__InstPtr(aCollection); |
|
5042 |
o_offs += offs; |
|
5043 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs, len, _buffered); |
|
5044 |
} else |
|
5045 |
goto out; |
|
5046 |
} |
|
5047 |
||
5048 |
if (cnt == len) { |
|
5049 |
pos = __INST(position); |
|
5050 |
if (pos != nil) { |
|
5051 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
5052 |
} |
|
5053 |
RETURN (self); |
|
5054 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
5055 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 5056 |
} |
5057 |
out: ; |
|
5058 |
%}. |
|
5059 |
lastErrorNumber notNil ifTrue:[self writeError. ^ self]. |
|
5060 |
filePointer isNil ifTrue:[self errorNotOpen. ^ self]. |
|
5061 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
5062 |
||
5063 |
^ super nextPutAll:aCollection |
|
5064 |
! |
|
5065 |
||
5066 |
nextPutAll:aCollection startingAt:start to:stop |
|
5067 |
"write a range of elements of the argument, aCollection. |
|
5068 |
Reimplemented for speed when writing strings or byteArrays. |
|
5069 |
For others, falls back to general method in superclass." |
|
5070 |
||
5071 |
%{ |
|
5072 |
FILEPOINTER f; |
|
5073 |
int offs, len, cnt, iStart, iStop, _buffered; |
|
5074 |
OBJ fp, pos; |
|
5075 |
int o_offs; |
|
5076 |
||
5077 |
__INST(lastErrorNumber) = nil; |
|
5078 |
if (((fp = __INST(filePointer)) != nil) |
|
5079 |
&& (__INST(mode) != @symbol(readonly)) |
|
5080 |
&& __bothSmallInteger(start, stop) |
|
5081 |
) { |
|
5082 |
f = __FILEVal(fp); |
|
5083 |
if (_buffered = (__INST(buffered) == true)) { |
|
5084 |
__WRITING__(f) |
|
5085 |
} |
|
5086 |
iStart = __intVal(start); |
|
5087 |
iStop = __intVal(stop); |
|
5088 |
if ((iStart < 1) || (iStop < iStart)) { |
|
5089 |
RETURN(self); |
|
5090 |
} |
|
5091 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
5092 |
len = __stringSize(aCollection); |
|
5093 |
if (iStop > len) { |
|
5094 |
RETURN(self); |
|
5095 |
} |
|
5096 |
if (iStop > len) |
|
5097 |
iStop = len; |
|
5098 |
len = iStop - iStart + 1; |
|
3807
59b3b3518ad5
do wrapCall for fopen; removed unneeded VMS code;
Claus Gittinger <cg@exept.de>
parents:
3787
diff
changeset
|
5099 |
|
3823 | 5100 |
if ((f == stdout) || (f == stderr)) { |
4206 | 5101 |
cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f); |
3823 | 5102 |
} else { |
5103 |
o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection); |
|
5104 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered); |
|
5105 |
} |
|
3746 | 5106 |
} else { |
5107 |
if (__INST(binary) == true) { |
|
5108 |
int offs; |
|
5109 |
||
5110 |
if (__isByteArray(aCollection)) { |
|
5111 |
offs = 0; |
|
5112 |
len = __byteArraySize(aCollection); |
|
5113 |
} else if (__isBytes(aCollection)) { |
|
5114 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars)); |
|
5115 |
len = __byteArraySize(aCollection) - offs; |
|
5116 |
} else |
|
5117 |
goto out; |
|
5118 |
||
5119 |
if (iStop > len) { |
|
5120 |
RETURN(self); |
|
5121 |
} |
|
5122 |
if (iStop > len) |
|
5123 |
iStop = len; |
|
5124 |
len = iStop - iStart + 1; |
|
5125 |
offs += iStart - 1; |
|
5126 |
o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection); |
|
5127 |
__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+offs, len, _buffered); |
|
5128 |
} else |
|
5129 |
goto out; |
|
5130 |
} |
|
5131 |
if (cnt == len) { |
|
5132 |
pos = __INST(position); |
|
5133 |
if (pos != nil) { |
|
5134 |
__INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
5135 |
} |
|
5136 |
RETURN (self); |
|
5137 |
} |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
5106
diff
changeset
|
5138 |
__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); |
3746 | 5139 |
} |
5140 |
out: ; |
|
5141 |
%}. |
|
5142 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
5143 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
5144 |
! ! |
|
5145 |
||
5146 |
!ExternalStream class methodsFor:'documentation'! |
|
5147 |
||
5148 |
version |
|
7720 | 5149 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/ExternalStream_win32.st,v 1.32 2003-10-29 09:52:28 ca Exp $' |
3746 | 5150 |
! ! |
7099 | 5151 |
|
3746 | 5152 |
ExternalStream initialize! |