author | Claus Gittinger <cg@exept.de> |
Tue, 05 Mar 1996 21:02:56 +0100 | |
changeset 1066 | cffbbdea7e01 |
parent 1048 | c535c5a73cba |
child 1067 | 1e7959af0821 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ReadWriteStream subclass:#ExternalStream |
|
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
14 |
instanceVariableNames:'filePointer mode buffered binary useCRLF hitEOF didWrite |
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
791
diff
changeset
|
15 |
lastErrorNumber' |
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
16 |
classVariableNames:'Lobby LastErrorNumber InvalidReadSignal InvalidWriteSignal |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
17 |
InvalidModeSignal OpenErrorSignal StreamNotOpenSignal |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
18 |
InvalidOperationSignal' |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
19 |
poolDictionaries:'' |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
20 |
category:'Streams-External' |
1 | 21 |
! |
22 |
||
216 | 23 |
!ExternalStream primitiveDefinitions! |
1 | 24 |
%{ |
25 |
#include <stdio.h> |
|
437 | 26 |
#define _STDIO_H_INCLUDED_ |
27 |
||
10 | 28 |
#include <fcntl.h> |
437 | 29 |
#define _FCNTL_H_INCLUDED_ |
30 |
||
10 | 31 |
#include <errno.h> |
437 | 32 |
#define _ERRNO_H_INCLUDED_ |
42 | 33 |
|
34 |
#ifdef hpux |
|
35 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
|
36 |
#endif |
|
216 | 37 |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
38 |
#ifndef SEEK_SET |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
39 |
# define SEEK_SET 0 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
40 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
41 |
#ifndef SEEK_CUR |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
42 |
# define SEEK_CUR 1 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
43 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
44 |
#ifndef SEEK_END |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
45 |
# define SEEK_END 2 |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
46 |
#endif |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
47 |
|
216 | 48 |
/* |
49 |
* stdio library requires an fseek before reading whenever a file |
|
50 |
* is open for read/write and the last operation was a write. |
|
51 |
* (also vice-versa). |
|
52 |
* All code should use the following macro before doing reads: |
|
53 |
*/ |
|
54 |
#define __READING__(f) \ |
|
55 |
if ((_INST(didWrite) != false) \ |
|
56 |
&& (_INST(mode) == @symbol(readwrite))) { \ |
|
57 |
_INST(didWrite) = false; \ |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
58 |
fseek(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
216 | 59 |
} |
60 |
||
61 |
#define __WRITING__(f) \ |
|
62 |
if ((_INST(didWrite) != true) \ |
|
63 |
&& (_INST(mode) == @symbol(readwrite))) { \ |
|
64 |
_INST(didWrite) = true; \ |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
65 |
fseek(f, 0L, SEEK_CUR); /* needed in stdio */ \ |
216 | 66 |
} |
67 |
||
223 | 68 |
/* |
69 |
* on some systems errno is a macro ... check for it here |
|
70 |
*/ |
|
71 |
#ifndef errno |
|
72 |
extern errno; |
|
73 |
#endif |
|
74 |
||
1 | 75 |
%} |
188 | 76 |
! ! |
1 | 77 |
|
22 | 78 |
!ExternalStream class methodsFor:'documentation'! |
79 |
||
88 | 80 |
copyright |
81 |
" |
|
82 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 83 |
All Rights Reserved |
88 | 84 |
|
85 |
This software is furnished under a license and may be used |
|
86 |
only in accordance with the terms of that license and with the |
|
87 |
inclusion of the above copyright notice. This software may not |
|
88 |
be provided or otherwise made available to, or used by, any |
|
89 |
other person. No title to or ownership of the software is |
|
90 |
hereby transferred. |
|
91 |
" |
|
92 |
! |
|
93 |
||
22 | 94 |
documentation |
95 |
" |
|
68 | 96 |
ExternalStream defines protocol common to Streams which have a file-descriptor and |
97 |
represent some file or communicationChannel of the underlying OperatingSystem. |
|
98 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc. |
|
77 | 99 |
|
159 | 100 |
ExternalStreams can be in two modes: text- (the default) and binary-mode. |
68 | 101 |
In text-mode, the elements read/written are characters; |
102 |
while in binary-mode the basic elements are bytes which read/write as SmallIntegers |
|
103 |
in the range 0..255. |
|
22 | 104 |
|
68 | 105 |
Also, the stream can be either in buffered or unbuffered mode. In buffered mode, |
106 |
data is not written until either a cr is written (in text mode) or a synchronizeOutput |
|
107 |
is sent (in both modes). |
|
22 | 108 |
|
77 | 109 |
The underlying OperatingSystem streams may either be closed explicitely (sending a close) |
68 | 110 |
or just forgotten - in this case, the garbage collector will eventually collect the |
77 | 111 |
object AND a close will be performed automatically (but you will NOT know when this |
112 |
happens - so it is recommended, that you close your files when no longer needed). |
|
159 | 113 |
Closing is also suggested, since if smalltalk is finished (be it by purpose, or due to |
77 | 114 |
some crash) the data will not be in the file, if unclosed. |
115 |
All streams understand the close message, so it never hurts to use it (it is defined as |
|
116 |
a noop in one of the superclasses). |
|
22 | 117 |
|
68 | 118 |
Most of the methods found here redefine inherited methods for better performance, |
119 |
since I/O from/to files should be fast. |
|
22 | 120 |
|
68 | 121 |
Recovering a snapshot: |
122 |
not all streams can be restored to the state they had before - see the implementation of |
|
123 |
reOpen in subclasses for more information. |
|
77 | 124 |
For streams sitting on some communication channel (i.e. Pipes and Sockets) you should |
125 |
reestablish the stream upon image restart (make someone dependent on ObjectMemory). |
|
216 | 126 |
FileStreams are reopened and positioned to their offset they had at snapshot time. |
127 |
This may fail, if the file was removed or renamed - or lead to confusion |
|
128 |
if the contents changed in the meantime. |
|
129 |
Therefore, it is a good idea to reopen files and check for these things at restart time. |
|
85 | 130 |
|
131 |
Instance variables: |
|
132 |
||
159 | 133 |
filePointer <Integer> the unix FILE*; somehow mapped to an integer |
134 |
(notice: not the fd) |
|
135 |
mode <Symbol> #readwrite, #readonly or #writeonly |
|
136 |
buffered <Boolean> true, if buffered (i.e. collects characters - does |
|
137 |
not output immediately) |
|
138 |
binary <Boolean> true if in binary mode (reads bytes instead of chars) |
|
139 |
useCRLF <Boolean> true, if lines should be terminated with crlf instead |
|
140 |
of lf. (i.e. if file is an MSDOS-type file) |
|
141 |
hitEOF <Boolean> true, if EOF was reached |
|
142 |
||
143 |
lastErrorNumber <Integer> the value of errno (only valid right after the error - |
|
144 |
updated with next i/o operation) |
|
85 | 145 |
|
146 |
Class variables: |
|
159 | 147 |
Lobby <Registry> keeps track of used ext-streams (to free up FILE*'s) |
148 |
||
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
149 |
StreamErrorSignal <Signal> parent of all stream errors (see Stream class) |
159 | 150 |
InvalidReadSignal <Signal> raised on read from writeonly stream |
151 |
InvalidWriteSignal <Signal> raised on write to readonly stream |
|
152 |
InvalidModeSignal <Signal> raised on text I/O with binary-stream |
|
153 |
or binary I/O with text-stream |
|
255 | 154 |
OpenErrorSignal <Signal> raised if open fails |
159 | 155 |
StreamNotOpenSignal <Signal> raised on I/O with non-open stream |
156 |
||
157 |
Additional notes: |
|
158 |
This class is implemented using the underlying stdio-c library package, which |
|
159 |
has both advantages and disadvantages: since it is portable (posix defined), porting |
|
160 |
ST/X to non-Unix machines is simplified. The disadvantage is that the stdio library |
|
161 |
has big problems handling unbounded Streams, since the EOF handling in stdio is |
|
162 |
not prepared for data to arrive after EOF has been reached - time will show, if we need |
|
163 |
a complete rewrite for UnboundedStream ... |
|
325 | 164 |
|
188 | 165 |
Also, depending on the system, the stdio library behaves infriendly when signals |
166 |
occur while reading (for example, timer interrupts) - on real unixes (i.e. BSD) the signal |
|
167 |
is handled transparently - on SYS5.3 (i.e. non unixes :-) the read operation returns |
|
168 |
an error and errno is set to EINTR. Thats what the ugly code around all getc-calls is for. |
|
325 | 169 |
|
170 |
Notice that typical stdio's use a single errno global variable to return an error code, |
|
171 |
this was bad design in the stdio lib (right from the very beginning), since its much |
|
172 |
harder to deal with this in the presence of lightweight processes, where errno gets |
|
173 |
overwritten by an I/O operation done in another thread. (stdio should have been written |
|
174 |
to return errno as a negative number ...). |
|
175 |
To deal with this, the scheduler treats errno like a per-thread private variable, |
|
176 |
and saves/restores the errno setting when switching to another thread. |
|
177 |
(Notice that some thread packages do this also, but ST/X's thread implementation |
|
178 |
does not depend on those, but instead uses a portable private package). |
|
179 |
||
216 | 180 |
Finally, if an stdio-stream is open for both reading and writing, we have to call |
181 |
fseek whenever we are about to read after write and vice versa. |
|
182 |
Two macros (__READING__ and __WRITING__) have been defined to be used before every |
|
183 |
fread/fgetc and fwrite/putc respectively. |
|
22 | 184 |
" |
185 |
! ! |
|
186 |
||
1 | 187 |
!ExternalStream class methodsFor:'initialization'! |
188 |
||
189 |
initialize |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
190 |
OpenErrorSignal isNil ifTrue:[ |
255 | 191 |
OpenErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
192 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
193 |
OpenErrorSignal notifierString:'open error'. |
|
194 |
||
159 | 195 |
InvalidReadSignal := ReadErrorSignal newSignalMayProceed:false. |
196 |
InvalidReadSignal nameClass:self message:#invalidReadSignal. |
|
583 | 197 |
InvalidReadSignal notifierString:'read error'. |
159 | 198 |
|
199 |
InvalidWriteSignal := WriteErrorSignal newSignalMayProceed:false. |
|
200 |
InvalidWriteSignal nameClass:self message:#invalidWriteSignal. |
|
201 |
InvalidWriteSignal notifierString:'write error'. |
|
202 |
||
203 |
InvalidModeSignal := StreamErrorSignal newSignalMayProceed:false. |
|
204 |
InvalidModeSignal nameClass:self message:#invalidModeSignal. |
|
205 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
|
206 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
207 |
InvalidOperationSignal := StreamErrorSignal newSignalMayProceed:false. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
208 |
InvalidOperationSignal nameClass:self message:#invalidOperationSignal. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
209 |
InvalidOperationSignal notifierString:'unsupported file operation'. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
210 |
|
159 | 211 |
StreamNotOpenSignal := StreamErrorSignal newSignalMayProceed:false. |
212 |
StreamNotOpenSignal nameClass:self message:#streamNotOpenSignal. |
|
213 |
StreamNotOpenSignal notifierString:'stream is not open'. |
|
214 |
]. |
|
215 |
||
31 | 216 |
Lobby isNil ifTrue:[ |
159 | 217 |
Lobby := Registry new. |
218 |
||
219 |
"want to get informed when returning from snapshot" |
|
220 |
ObjectMemory addDependent:self |
|
1 | 221 |
] |
222 |
! |
|
223 |
||
224 |
reOpenFiles |
|
225 |
"reopen all files (if possible) after a snapShot load" |
|
226 |
||
301 | 227 |
Lobby do:[:aFileStream | |
159 | 228 |
aFileStream reOpen |
1 | 229 |
] |
230 |
! |
|
231 |
||
232 |
update:something |
|
233 |
"have to reopen files when returning from snapshot" |
|
234 |
||
235 |
something == #returnFromSnapshot ifTrue:[ |
|
159 | 236 |
self reOpenFiles |
1 | 237 |
] |
238 |
! ! |
|
239 |
||
612 | 240 |
!ExternalStream class methodsFor:'instance creation'! |
241 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
242 |
forFileDescriptor:aFileDescriptor mode:mode |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
243 |
|newStream| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
244 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
245 |
newStream := self basicNew. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
246 |
newStream text; buffered:true; useCRLF:false; clearEOF. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
247 |
^ newStream connectTo:aFileDescriptor withMode:mode |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
248 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
249 |
"this will probably fail (15 is a random FD): |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
250 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
251 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
252 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
253 |
s := ExternalStream forFileDescriptor:15 mode:'r'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
254 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
255 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
256 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
257 |
"Created: 29.2.1996 / 18:05:00 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
258 |
"Modified: 29.2.1996 / 18:17:07 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
259 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
260 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
261 |
forReadWriteToFileDescriptor:aFileDescriptor |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
262 |
^ self forFileDescriptor:aFileDescriptor mode:'r+' |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
263 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
264 |
"this will probably fail (15 is a random FD): |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
265 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
266 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
267 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
268 |
s := ExternalStream forReadWriteToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
269 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
270 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
271 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
272 |
"Created: 29.2.1996 / 18:15:08 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
273 |
"Modified: 29.2.1996 / 18:16:25 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
274 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
275 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
276 |
forReadingFromFileDescriptor:aFileDescriptor |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
277 |
^ self forFileDescriptor:aFileDescriptor mode:'r' |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
278 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
279 |
"this will probably fail (15 is a random FD): |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
280 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
281 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
282 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
283 |
s := ExternalStream forReadingFromFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
284 |
s next. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
285 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
286 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
287 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
288 |
|pipe readFd writeFd rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
289 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
290 |
'create OS pipe ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
291 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
292 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
293 |
readFd := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
294 |
writeFd := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
295 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
296 |
'connect Smalltalk streams ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
297 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
298 |
rs := ExternalStream forReadingFromFileDescriptor:readFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
299 |
ws := ExternalStream forWritingToFileDescriptor:writeFd. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
300 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
301 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
302 |
[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
303 |
1 to:10 do:[:i | |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
304 |
Transcript showCr:rs nextLine |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
305 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
306 |
rs close. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
307 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
308 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
309 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
310 |
[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
311 |
1 to:10 do:[:i | |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
312 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
313 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
314 |
ws close. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
315 |
] fork. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
316 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
317 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
318 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
319 |
"Created: 29.2.1996 / 18:14:24 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
320 |
"Modified: 29.2.1996 / 18:25:02 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
321 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
322 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
323 |
forWritingToFileDescriptor:aFileDescriptor |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
324 |
^ self forFileDescriptor:aFileDescriptor mode:'w' |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
325 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
326 |
"this will probably fail (15 is a random FD): |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
327 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
328 |
|s| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
329 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
330 |
s := ExternalStream forWritingToFileDescriptor:15. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
331 |
s binary. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
332 |
s nextPut:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
333 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
334 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
335 |
"Created: 29.2.1996 / 18:14:43 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
336 |
"Modified: 29.2.1996 / 18:15:54 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
337 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
338 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
339 |
makePipe |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
340 |
"return an array with two streams - the first one for reading, the second |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
341 |
for writing. This is the higher level equivalent of OperatingSystem>>makePipe." |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
342 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
343 |
|pipe rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
344 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
345 |
pipe := OperatingSystem makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
346 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
347 |
pipe notNil ifTrue:[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
348 |
rs := self forReadingFromFileDescriptor:(pipe at:1). |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
349 |
ws := self forWritingToFileDescriptor:(pipe at:2). |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
350 |
^ Array with:rs with:ws |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
351 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
352 |
^ nil |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
353 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
354 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
355 |
|pipe rs ws| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
356 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
357 |
pipe := ExternalStream makePipe. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
358 |
rs := pipe at:1. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
359 |
ws := pipe at:2. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
360 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
361 |
'read ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
362 |
[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
363 |
1 to:10 do:[:i | |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
364 |
Transcript showCr:rs nextLine |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
365 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
366 |
rs close. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
367 |
] forkAt:7. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
368 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
369 |
'write ...'. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
370 |
[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
371 |
1 to:10 do:[:i | |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
372 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
373 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
374 |
ws close. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
375 |
] fork. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
376 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
377 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
378 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
379 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
380 |
|
612 | 381 |
new |
382 |
|newStream| |
|
383 |
||
384 |
newStream := self basicNew. |
|
385 |
newStream text; buffered:true; useCRLF:false; clearEOF. |
|
386 |
^ newStream |
|
387 |
! ! |
|
388 |
||
345 | 389 |
!ExternalStream class methodsFor:'Signal constants'! |
159 | 390 |
|
612 | 391 |
invalidModeSignal |
392 |
"return the signal raised when doing text-I/O with a binary stream |
|
393 |
or binary-I/O with a text stream" |
|
394 |
||
395 |
^ InvalidModeSignal |
|
159 | 396 |
! |
397 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
398 |
invalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
399 |
"return the signal raised when an unsupported or invalid |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
400 |
I/O operation is attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
401 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
402 |
^ InvalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
403 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
404 |
|
159 | 405 |
invalidReadSignal |
406 |
"return the signal raised when reading from writeonly streams" |
|
407 |
||
408 |
^ InvalidReadSignal |
|
409 |
! |
|
410 |
||
411 |
invalidWriteSignal |
|
412 |
"return the signal raised when writing to readonly streams" |
|
413 |
||
414 |
^ InvalidWriteSignal |
|
415 |
! |
|
416 |
||
612 | 417 |
openErrorSignal |
418 |
"return the signal raised when a file open failed" |
|
419 |
||
420 |
^ OpenErrorSignal |
|
421 |
! |
|
422 |
||
159 | 423 |
streamNotOpenSignal |
424 |
"return the signal raised on I/O with closed streams" |
|
425 |
||
426 |
^ StreamNotOpenSignal |
|
2 | 427 |
! ! |
428 |
||
92 | 429 |
!ExternalStream class methodsFor:'error handling'! |
430 |
||
431 |
lastErrorNumber |
|
159 | 432 |
"return the errno of the last error" |
92 | 433 |
|
434 |
^ LastErrorNumber |
|
159 | 435 |
|
436 |
" |
|
437 |
ExternalStream lastErrorNumber |
|
438 |
" |
|
92 | 439 |
! |
440 |
||
441 |
lastErrorString |
|
442 |
"return a message string describing the last error" |
|
443 |
||
444 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
445 |
||
446 |
" |
|
447 |
ExternalStream lastErrorString |
|
448 |
" |
|
449 |
! ! |
|
450 |
||
1 | 451 |
!ExternalStream methodsFor:'accessing'! |
452 |
||
612 | 453 |
binary |
454 |
"switch to binary mode - default is text" |
|
455 |
||
456 |
binary := true |
|
1 | 457 |
! |
458 |
||
459 |
buffered:aBoolean |
|
10 | 460 |
"turn buffering on or off - default is on" |
1 | 461 |
|
10 | 462 |
buffered := aBoolean |
1 | 463 |
! |
464 |
||
465 |
contents |
|
2 | 466 |
"return the contents of the file from the current position up-to |
467 |
the end. If the stream is in binary mode, a ByteArray containing |
|
468 |
the byte values is returned. |
|
469 |
In text-mode, a collection of strings, each representing one line, |
|
470 |
is returned." |
|
471 |
||
472 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
473 |
||
474 |
binary ifTrue:[ |
|
159 | 475 |
"adding to a ByteArray produces quadratic time-space |
476 |
behavior - therefore we allocate chunks, and concatenate them |
|
477 |
at the end." |
|
478 |
||
479 |
chunks := OrderedCollection new. |
|
480 |
sizes := OrderedCollection new. |
|
481 |
byteCount := 0. |
|
482 |
[self atEnd] whileFalse:[ |
|
483 |
chunk := ByteArray uninitializedNew:4096. |
|
484 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
485 |
cnt notNil ifTrue:[ |
|
486 |
chunks add:chunk. |
|
487 |
sizes add:cnt. |
|
488 |
byteCount := byteCount + cnt |
|
489 |
] |
|
490 |
]. |
|
491 |
||
492 |
"now, create one big ByteArray" |
|
493 |
bytes := ByteArray uninitializedNew:byteCount. |
|
494 |
offset := 1. |
|
495 |
1 to:chunks size do:[:index | |
|
496 |
chunk := chunks at:index. |
|
497 |
cnt := sizes at:index. |
|
498 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
499 |
offset := offset + cnt |
|
500 |
]. |
|
501 |
^ bytes |
|
2 | 502 |
]. |
1 | 503 |
|
244 | 504 |
text := StringCollection new. |
1 | 505 |
[self atEnd] whileFalse:[ |
159 | 506 |
l := self nextLine. |
406 | 507 |
l isNil ifTrue:[ |
508 |
^ text |
|
509 |
]. |
|
510 |
text add:l |
|
1 | 511 |
]. |
512 |
^ text |
|
513 |
! |
|
514 |
||
612 | 515 |
contentsOfEntireFile |
516 |
"ST-80 compatibility" |
|
517 |
||
518 |
^ self contents |
|
2 | 519 |
! |
520 |
||
612 | 521 |
contentsSpecies |
522 |
"return the kind of object to be returned by sub-collection builders |
|
523 |
(such as upTo)" |
|
159 | 524 |
|
525 |
binary ifTrue:[ |
|
612 | 526 |
^ ByteArray |
159 | 527 |
]. |
612 | 528 |
^ String |
2 | 529 |
! |
530 |
||
612 | 531 |
fileDescriptor |
532 |
"return the fileDescriptor of the receiver - |
|
533 |
notice: this one returns the underlying OSs fileDescriptor - |
|
534 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1 | 535 |
|
536 |
%{ /* NOCONTEXT */ |
|
537 |
||
538 |
FILE *f; |
|
612 | 539 |
OBJ fp; |
540 |
||
541 |
if ((fp = _INST(filePointer)) != nil) { |
|
542 |
f = __FILEVal(fp); |
|
543 |
RETURN ( __MKSMALLINT(fileno(f)) ); |
|
1 | 544 |
} |
612 | 545 |
%} |
546 |
. |
|
547 |
^ self errorNotOpen |
|
548 |
! |
|
549 |
||
826
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
550 |
fileDescriptor:anInteger withMode:openMode |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
551 |
"set the filePointer from the receiver based on the fileDescriptor- |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
552 |
notice: this one is based on the underlying OSs fileDescriptor - |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
553 |
this may not be available on all platforms (i.e. non unix systems)." |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
554 |
|
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
555 |
%{ /* NOCONTEXT */ |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
556 |
|
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
557 |
FILE *f; |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
558 |
OBJ fp; |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
559 |
|
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
560 |
if (__isSmallInteger(anInteger) && |
872 | 561 |
__isString(openMode) && |
562 |
(f = fdopen(__intVal(anInteger), __stringVal(openMode))) != 0 |
|
826
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
563 |
) { |
872 | 564 |
_INST(filePointer) = fp = __MKOBJ((int)f); __STORE(self, fp); |
565 |
RETURN (self); |
|
826
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
566 |
} |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
567 |
%} |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
568 |
. |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
569 |
^ self primitiveFailed |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
570 |
! |
b271ea453f6a
New method to set the filedescriptor of a stream.
Stefan Vogel <sv@exept.de>
parents:
820
diff
changeset
|
571 |
|
612 | 572 |
filePointer |
573 |
"return the filePointer of the receiver - |
|
574 |
notice: for portability stdio is used; this means you will get |
|
575 |
a FILE * - not a fileDescriptor. |
|
576 |
(what you really get is a corresponding integer). |
|
577 |
You cannot do much with the returned value |
|
578 |
- except passing it to a primitive, for example." |
|
579 |
||
580 |
^ filePointer |
|
581 |
! |
|
582 |
||
583 |
readonly |
|
584 |
"set access mode to readonly" |
|
585 |
||
586 |
mode := #readonly |
|
587 |
! |
|
588 |
||
589 |
readwrite |
|
590 |
"set access mode to readwrite" |
|
591 |
||
592 |
mode := #readwrite |
|
593 |
! |
|
594 |
||
595 |
text |
|
596 |
"switch to text mode - default is text" |
|
597 |
||
598 |
binary := false |
|
599 |
! |
|
600 |
||
601 |
useCRLF:aBoolean |
|
602 |
"turn on or off CRLF sending (instead of LF only) - default is off" |
|
603 |
||
604 |
useCRLF := aBoolean |
|
1 | 605 |
! |
606 |
||
612 | 607 |
writeonly |
608 |
"set access mode to writeonly" |
|
609 |
||
610 |
mode := #writeonly |
|
611 |
! ! |
|
612 |
||
613 |
!ExternalStream methodsFor:'error handling'! |
|
614 |
||
615 |
argumentMustBeCharacter |
|
616 |
"report an error, that the argument must be a character" |
|
617 |
||
618 |
^ self error:'argument must be a character' |
|
619 |
! |
|
620 |
||
621 |
argumentMustBeInteger |
|
622 |
"report an error, that the argument must be an integer" |
|
623 |
||
624 |
^ self error:'argument must be an integer' |
|
77 | 625 |
! |
626 |
||
612 | 627 |
argumentMustBeString |
628 |
"report an error, that the argument must be a string" |
|
629 |
||
630 |
^ self error:'argument must be a string' |
|
631 |
! |
|
632 |
||
633 |
errorBinary |
|
634 |
"report an error, that the stream is in binary mode" |
|
635 |
||
636 |
^ InvalidModeSignal |
|
637 |
raiseRequestWith:self |
|
638 |
errorString:(self class name , ' is in binary mode') |
|
639 |
in:thisContext sender |
|
640 |
! |
|
641 |
||
642 |
errorNotBinary |
|
643 |
"report an error, that the stream is not in binary mode" |
|
644 |
||
645 |
^ InvalidModeSignal |
|
646 |
raiseRequestWith:self |
|
647 |
errorString:(self class name , ' is not in binary mode') |
|
648 |
in:thisContext sender |
|
1 | 649 |
! |
650 |
||
612 | 651 |
errorNotBuffered |
652 |
"report an error, that the stream is not in buffered mode" |
|
653 |
||
654 |
^ StreamErrorSignal |
|
655 |
raiseRequestWith:self |
|
656 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
657 |
in:thisContext sender |
|
658 |
! |
|
659 |
||
660 |
errorNotOpen |
|
661 |
"report an error, that the stream has not been opened" |
|
662 |
||
663 |
^ StreamNotOpenSignal |
|
664 |
raiseRequestWith:self |
|
665 |
errorString:(self class name , ' not open') |
|
666 |
in:thisContext sender |
|
667 |
! |
|
668 |
||
669 |
errorOpen |
|
670 |
"report an error, that the stream is already opened" |
|
671 |
||
672 |
^ OpenErrorSignal |
|
673 |
raiseRequestWith:self |
|
674 |
errorString:(self class name , ' is already open') |
|
675 |
in:thisContext sender |
|
676 |
! |
|
677 |
||
678 |
errorReadOnly |
|
679 |
"report an error, that the stream is a readOnly stream" |
|
680 |
||
681 |
^ InvalidWriteSignal |
|
682 |
raiseRequestWith:self |
|
683 |
errorString:(self class name , ' is readonly') |
|
684 |
in:thisContext sender |
|
685 |
! |
|
686 |
||
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
687 |
errorUnsupportedOperation |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
688 |
"report an error, that some unsupported operation was attempted" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
689 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
690 |
^ InvalidOperationSignal |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
691 |
raiseRequestWith:self |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
692 |
errorString:'unsupported operation' |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
693 |
in:thisContext sender |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
694 |
! |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
695 |
|
612 | 696 |
errorWriteOnly |
697 |
"report an error, that the stream is a writeOnly stream" |
|
698 |
||
699 |
^ InvalidReadSignal |
|
700 |
raiseRequestWith:self |
|
701 |
errorString:(self class name , ' is writeonly') |
|
702 |
in:thisContext sender |
|
2 | 703 |
! |
704 |
||
612 | 705 |
ioError |
706 |
"report an error, that some I/O error occured" |
|
707 |
||
708 |
^ StreamErrorSignal |
|
709 |
raiseRequestWith:self |
|
710 |
errorString:('I/O error: ' , self lastErrorString) |
|
711 |
in:thisContext sender |
|
712 |
! |
|
713 |
||
714 |
lastErrorNumber |
|
715 |
"return the last error" |
|
716 |
||
717 |
^ lastErrorNumber |
|
718 |
! |
|
719 |
||
720 |
lastErrorString |
|
721 |
"return a message string describing the last error" |
|
722 |
||
723 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
724 |
^ 'I/O error' |
|
725 |
]. |
|
726 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2 | 727 |
! |
728 |
||
612 | 729 |
openError |
730 |
"report an error, that the open failed" |
|
731 |
||
732 |
^ OpenErrorSignal |
|
733 |
raiseRequestWith:self |
|
734 |
errorString:('error on open: ' , self lastErrorString) |
|
735 |
in:thisContext sender |
|
736 |
! |
|
737 |
||
738 |
readError |
|
739 |
"report an error, that some read error occured" |
|
740 |
||
741 |
^ ReadErrorSignal |
|
742 |
raiseRequestWith:self |
|
743 |
errorString:('read error: ' , self lastErrorString) |
|
744 |
in:thisContext sender |
|
745 |
! |
|
746 |
||
747 |
writeError |
|
748 |
"report an error, that some write error occured" |
|
749 |
||
750 |
^ WriteErrorSignal |
|
751 |
raiseRequestWith:self |
|
752 |
errorString:('write error: ' , self lastErrorString) |
|
753 |
in:thisContext sender |
|
754 |
! ! |
|
755 |
||
756 |
!ExternalStream methodsFor:'instance release'! |
|
757 |
||
758 |
closeFile |
|
759 |
"low level close - may be redefined in subclasses" |
|
2 | 760 |
|
761 |
%{ /* NOCONTEXT */ |
|
762 |
||
216 | 763 |
OBJ fp; |
2 | 764 |
|
612 | 765 |
if ((fp = _INST(filePointer)) != nil) { |
766 |
_INST(filePointer) = nil; |
|
362 | 767 |
__BEGIN_INTERRUPTABLE__ |
612 | 768 |
fclose(__FILEVal(fp)); |
362 | 769 |
__END_INTERRUPTABLE__ |
10 | 770 |
} |
771 |
%} |
|
772 |
! |
|
773 |
||
612 | 774 |
disposed |
775 |
"some Stream has been collected - close the file if not already done" |
|
776 |
||
777 |
self closeFile |
|
778 |
! |
|
779 |
||
780 |
setFilePointer:anInteger |
|
781 |
filePointer := anInteger |
|
1 | 782 |
! |
783 |
||
612 | 784 |
shallowCopyForFinalization |
785 |
"return a copy for finalization-registration; |
|
786 |
since all we need at finalization time is the fileDescriptor, |
|
787 |
a cheaper copy is possible." |
|
788 |
||
789 |
^ self class basicNew setFilePointer:filePointer |
|
1 | 790 |
! |
791 |
||
612 | 792 |
shutDown |
793 |
"close the stream - added for protocol compatibility with PipeStream. |
|
794 |
see comment there" |
|
795 |
||
796 |
self closeFile |
|
1 | 797 |
! ! |
798 |
||
2 | 799 |
!ExternalStream methodsFor:'line reading/writing'! |
1 | 800 |
|
801 |
nextLine |
|
802 |
"read the next line (characters up to newline). |
|
803 |
Return a string containing those characters excluding the newline. |
|
804 |
If the previous-to-last character is a cr, this is also removed, |
|
68 | 805 |
so its possible to read alien (i.e. ms-dos) text as well. |
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
806 |
The line must be shorter than 16K characters - otherwise its truncated." |
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
807 |
|
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
808 |
%{ /* STACK:17000 */ |
1 | 809 |
|
810 |
FILE *f; |
|
811 |
int len; |
|
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
812 |
char buffer[16*1024]; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
813 |
char *rslt, *nextPtr, *limit; |
22 | 814 |
int fd, ch; |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
815 |
int _buffered, _isReadWrite; |
216 | 816 |
OBJ fp; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
817 |
OBJ __MKSTRING_L(); |
1 | 818 |
|
159 | 819 |
_INST(lastErrorNumber) = nil; |
216 | 820 |
if (((fp = _INST(filePointer)) != nil) |
159 | 821 |
&& (_INST(mode) != @symbol(writeonly))) { |
822 |
if (_INST(binary) != true) { |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
823 |
f = __FILEVal(fp); |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
824 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
825 |
_isReadWrite = (_INST(mode) == @symbol(readwrite)); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
826 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
827 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
828 |
|
362 | 829 |
__BEGIN_INTERRUPTABLE__ |
159 | 830 |
buffer[0] = 0; |
831 |
||
249 | 832 |
if (_buffered) { |
255 | 833 |
__READING__(f); |
249 | 834 |
} else { |
255 | 835 |
fd = fileno(f); |
249 | 836 |
} |
159 | 837 |
|
838 |
/* |
|
839 |
* mhmh - the following code looks ok to me, |
|
840 |
* but seems not to work for sockets |
|
841 |
*/ |
|
22 | 842 |
#ifdef DOES_NOT_WORK |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
843 |
if (_isReadWrite) |
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
844 |
fseek(f, 0L, SEEK_CUR); /* needed in stdio */ |
159 | 845 |
do { |
846 |
rslt = fgets(buffer, sizeof(buffer), f); |
|
847 |
} while ((rslt == NULL) && (errno == EINTR)); |
|
22 | 848 |
#else |
249 | 849 |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
850 |
rslt = nextPtr = buffer; |
370 | 851 |
limit = buffer + sizeof(buffer) - 2; |
852 |
||
159 | 853 |
for (;;) { |
854 |
if (_buffered) { |
|
379 | 855 |
errno = 0; |
856 |
do { |
|
441 | 857 |
if (feof(f)) { |
858 |
ch = EOF; |
|
859 |
break; |
|
860 |
} |
|
379 | 861 |
ch = getc(f); |
862 |
} while ((ch < 0) && (errno == EINTR)); |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
863 |
|
159 | 864 |
if (ch == EOF) { |
865 |
if (ferror(f)) { |
|
866 |
if (errno == EINTR) { |
|
867 |
clearerr(f); |
|
440 | 868 |
if (! feof(f)) { |
869 |
continue; |
|
870 |
} |
|
159 | 871 |
} |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
872 |
__FETCHPROTECT__(self, 1); |
359 | 873 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 874 |
} |
875 |
len = 0; |
|
876 |
} else { |
|
877 |
len = 1; |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
878 |
*nextPtr = ch; |
159 | 879 |
} |
880 |
} else { |
|
881 |
do { |
|
447 | 882 |
errno = 0; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
883 |
len = read(fd, nextPtr, 1); |
159 | 884 |
} while ((len < 0) && (errno == EINTR)); |
885 |
} |
|
886 |
if (len <= 0) { |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
887 |
if (nextPtr == buffer) { |
159 | 888 |
rslt = NULL; |
889 |
} else { |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
890 |
*nextPtr = '\0'; |
159 | 891 |
} |
892 |
break; |
|
893 |
} |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
894 |
if (*nextPtr == '\n') { |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
895 |
*nextPtr = '\0'; |
159 | 896 |
break; |
897 |
} |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
898 |
nextPtr++; |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
899 |
if (nextPtr >= limit) { |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
900 |
*nextPtr = '\0'; |
971
eb70f5674303
max lineLength (in nextLine) increased to 16k
Claus Gittinger <cg@exept.de>
parents:
933
diff
changeset
|
901 |
fprintf(stderr, "EXTSTREAM: line truncated in nextLine\n"); |
159 | 902 |
break; |
903 |
} |
|
904 |
} |
|
12 | 905 |
#endif |
362 | 906 |
__END_INTERRUPTABLE__ |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
907 |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
908 |
__UNPROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
909 |
|
159 | 910 |
if (rslt != NULL) { |
370 | 911 |
/* |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
912 |
* that strlen can be avoided and replaced by (nextPtr - buffer) |
370 | 913 |
*/ |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
914 |
/* len = strlen(buffer); */ |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
915 |
len = nextPtr-buffer; |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
916 |
|
159 | 917 |
if (_INST(position) != nil) { |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
918 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len); |
159 | 919 |
} |
920 |
/* remove EOL character */ |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
921 |
if (len != 0) { |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
922 |
if (buffer[len-1] == '\n') { |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
521
diff
changeset
|
923 |
buffer[--len] = '\0'; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
924 |
} |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
925 |
if ((len != 0) && (buffer[len-1] == '\r')) { |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
521
diff
changeset
|
926 |
buffer[--len] = '\0'; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
927 |
} |
159 | 928 |
} |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
929 |
RETURN ( __MKSTRING_L(buffer, len COMMA_CON) ); |
159 | 930 |
} |
216 | 931 |
if (ferror(f) && (errno != 0)) { |
359 | 932 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 933 |
} else { |
934 |
_INST(hitEOF) = true; |
|
935 |
RETURN ( nil ); |
|
936 |
} |
|
937 |
} |
|
1 | 938 |
} |
159 | 939 |
%}. |
940 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2 | 941 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
1 | 942 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
2 | 943 |
self errorBinary |
1 | 944 |
! |
945 |
||
946 |
nextPutLine:aString |
|
947 |
"write the characters in aString and append a newline" |
|
948 |
||
949 |
%{ /* NOCONTEXT */ |
|
950 |
||
951 |
FILE *f; |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
952 |
int len, cnt, len1; |
216 | 953 |
OBJ pos, fp; |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
954 |
char *cp; |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
955 |
int _buffered, _useCRLF; |
1 | 956 |
|
159 | 957 |
_INST(lastErrorNumber) = nil; |
216 | 958 |
if (((fp = _INST(filePointer)) != nil) |
159 | 959 |
&& (_INST(mode) != @symbol(readonly))) { |
960 |
if (_INST(binary) != true) { |
|
961 |
if (__isString(aString)) { |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
962 |
f = __FILEVal(fp); |
159 | 963 |
len = _stringSize(aString); |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
964 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
965 |
_useCRLF = (_INST(useCRLF) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
966 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
967 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
968 |
__PROTECT__(aString); |
159 | 969 |
|
362 | 970 |
__BEGIN_INTERRUPTABLE__ |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
971 |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
972 |
if (_buffered) { |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
973 |
__WRITING__(f) |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
974 |
cnt = fwrite(_stringVal(aString), 1, len, f); |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
975 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
976 |
do { |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
977 |
cnt = write(fileno(f), _stringVal(aString), len); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
978 |
__FETCHPROTECT__(aString, 1); |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
979 |
} while ((cnt < 0) && (errno == EINTR)); |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
980 |
} |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
981 |
|
159 | 982 |
if (cnt == len) { |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
983 |
len1 = len; |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
984 |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
985 |
if (_useCRLF) { |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
986 |
cp = "\r\n"; len = 2; |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
987 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
988 |
cp = "\n"; len = 1; |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
989 |
} |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
990 |
if (_buffered) { |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
991 |
cnt = fwrite(cp, 1, len, f); |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
992 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
993 |
do { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
994 |
cnt = write(fileno(f), cp, len); |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
995 |
} while ((cnt < 0) && (errno == EINTR)); |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
996 |
} |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
997 |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
998 |
__END_INTERRUPTABLE__ |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
999 |
__UNPROTECT__(aString); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1000 |
__UNPROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1001 |
|
454
344ccfa720d9
Add useCRLF handling in nextPutLine:
Stefan Vogel <sv@exept.de>
parents:
447
diff
changeset
|
1002 |
if (cnt > 0) { |
159 | 1003 |
pos = _INST(position); |
1004 |
if (pos != nil) { |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1005 |
_INST(position) = __MKSMALLINT(__intVal(pos)+len1+cnt); |
159 | 1006 |
} |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1007 |
|
159 | 1008 |
RETURN ( self ); |
1009 |
} |
|
1010 |
} |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1011 |
|
362 | 1012 |
__END_INTERRUPTABLE__ |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1013 |
__UNPROTECT__(aString); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1014 |
__UNPROTECT__(self); |
359 | 1015 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 1016 |
} |
1017 |
} |
|
1 | 1018 |
} |
159 | 1019 |
%}. |
1020 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
1021 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
1022 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
1023 |
binary ifTrue:[^ self errorBinary]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
1024 |
|
68 | 1025 |
super nextPutAll:aString. |
1026 |
self cr. |
|
1 | 1027 |
! |
1028 |
||
1029 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
68 | 1030 |
"read from aStream up to and including a line starting with aStringOrNil |
22 | 1031 |
and append it to self. |
159 | 1032 |
Can be used to copy/create large files or copy from a pipe/socket. |
68 | 1033 |
|
22 | 1034 |
If aStringOrNil is nil or not matched, copy preceeds to the end. |
1035 |
(this allows for example to read a Socket and transfer the data quickly |
|
1036 |
into a file - without creating zillions of temporary strings)" |
|
1 | 1037 |
|
159 | 1038 |
|srcFilePointer readError| |
1 | 1039 |
|
1040 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
1041 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1042 |
srcFilePointer := aStream filePointer. |
|
1043 |
srcFilePointer isNil ifTrue:[^ aStream errorNotOpen]. |
|
12 | 1044 |
|
1045 |
%{ /* STACK:2000 */ |
|
22 | 1046 |
|
1 | 1047 |
FILE *dst, *src; |
1048 |
char *matchString; |
|
1049 |
int matchLen = 0; |
|
12 | 1050 |
char buffer[1024]; |
1 | 1051 |
|
159 | 1052 |
_INST(lastErrorNumber) = nil; |
249 | 1053 |
if (__isSmallInteger(srcFilePointer)) { |
159 | 1054 |
if ((aStringOrNil == nil) || __isString(aStringOrNil)) { |
1055 |
if (aStringOrNil != nil) { |
|
1056 |
matchString = (char *) _stringVal(aStringOrNil); |
|
1057 |
matchLen = _stringSize(aStringOrNil); |
|
1058 |
} |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
1059 |
dst = __FILEVal(_INST(filePointer)); |
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
1060 |
src = __FILEVal(srcFilePointer); |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1061 |
|
362 | 1062 |
__BEGIN_INTERRUPTABLE__ |
159 | 1063 |
errno = 0; |
1064 |
||
216 | 1065 |
__WRITING__(dst) |
159 | 1066 |
|
1067 |
for (;;) { |
|
370 | 1068 |
if (fgets(buffer, sizeof(buffer)-1, src) == NULL) { |
159 | 1069 |
if (ferror(src)) { |
359 | 1070 |
readError = __MKSMALLINT(errno); |
362 | 1071 |
__END_INTERRUPTABLE__ |
159 | 1072 |
goto err; |
1073 |
} |
|
1074 |
break; |
|
1075 |
} |
|
1076 |
if (fputs(buffer, dst) == EOF) { |
|
1077 |
if (ferror(dst)) { |
|
359 | 1078 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 1079 |
__END_INTERRUPTABLE__ |
159 | 1080 |
goto err; |
1081 |
} |
|
1082 |
break; |
|
1083 |
} |
|
12 | 1084 |
#ifndef OLD |
159 | 1085 |
if (_INST(buffered) == false) { |
1086 |
fflush(dst); |
|
1087 |
} |
|
12 | 1088 |
#endif |
159 | 1089 |
if (matchLen) { |
1090 |
if (strncmp(matchString, buffer, matchLen) == 0) |
|
1091 |
break; |
|
1092 |
} |
|
1093 |
} |
|
362 | 1094 |
__END_INTERRUPTABLE__ |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1095 |
|
159 | 1096 |
_INST(position) = nil; |
1097 |
RETURN (self); |
|
1098 |
} |
|
1 | 1099 |
} |
159 | 1100 |
err: ; |
1101 |
%}. |
|
1102 |
readError ifTrue:[ |
|
1103 |
aStream setLastErrorNumber:readError. |
|
1104 |
^ aStream readError |
|
1105 |
]. |
|
1106 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
10 | 1107 |
buffered ifFalse:[^ self errorNotBuffered]. |
159 | 1108 |
" |
1109 |
argument error |
|
1110 |
" |
|
1 | 1111 |
^ self primitiveFailed |
1112 |
! |
|
1113 |
||
1114 |
peekForLineStartingWith:aString |
|
1115 |
"read ahead for next line starting with aString; |
|
159 | 1116 |
return the line-string if found, or nil if EOF is encountered. |
1117 |
If matched, not advance position behond that line |
|
1118 |
i.e. nextLine will read the matched line. |
|
1119 |
If not matched, reposition to original position for firther reading." |
|
1 | 1120 |
|
1121 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1122 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 1123 |
binary ifTrue:[^ self errorBinary]. |
12 | 1124 |
|
1125 |
%{ /* STACK: 2000 */ |
|
1 | 1126 |
FILE *f; |
1127 |
int l; |
|
12 | 1128 |
char buffer[1024]; |
1 | 1129 |
char *cp; |
1130 |
char *matchString; |
|
92 | 1131 |
int firstpos = -1, lastpos; |
1 | 1132 |
|
159 | 1133 |
_INST(lastErrorNumber) = nil; |
54 | 1134 |
if (__isString(aString)) { |
159 | 1135 |
matchString = (char *) _stringVal(aString); |
1136 |
l = _stringSize(aString); |
|
1137 |
||
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
1138 |
f = __FILEVal(_INST(filePointer)); |
216 | 1139 |
__READING__(f) |
159 | 1140 |
|
1141 |
for (;;) { |
|
1142 |
lastpos = ftell(f); |
|
1143 |
if (firstpos == -1) firstpos = lastpos; |
|
1144 |
||
362 | 1145 |
__BEGIN_INTERRUPTABLE__ |
159 | 1146 |
do { |
370 | 1147 |
cp = fgets(buffer, sizeof(buffer)-1, f); |
159 | 1148 |
} while ((cp == NULL) && (errno == EINTR)); |
370 | 1149 |
buffer[sizeof(buffer)-1] = '\0'; |
362 | 1150 |
__END_INTERRUPTABLE__ |
159 | 1151 |
|
1152 |
if (cp == NULL) { |
|
1153 |
if (ferror(f)) { |
|
359 | 1154 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 1155 |
goto err; |
1156 |
} else { |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1157 |
fseek(f, firstpos, SEEK_SET); |
159 | 1158 |
RETURN (nil); |
1159 |
} |
|
1160 |
} |
|
1161 |
if (strncmp(cp, matchString, l) == 0) { |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1162 |
fseek(f, lastpos, SEEK_SET); |
159 | 1163 |
break; |
1164 |
} |
|
1165 |
} |
|
1166 |
/* remove EOL character */ |
|
1167 |
cp = buffer; |
|
1168 |
while (*cp && (*cp != '\n')) cp++; |
|
1169 |
*cp = '\0'; |
|
1170 |
RETURN ( _MKSTRING(buffer COMMA_CON) ); |
|
1 | 1171 |
} |
159 | 1172 |
err: ; |
1173 |
%}. |
|
1174 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
1175 |
^ self argumentMustBeString |
|
1 | 1176 |
! |
1177 |
||
1178 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
1179 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
1180 |
return the index in aCollection if found, nil otherwise.. |
|
1181 |
If no match, do not change position; otherwise advance right before the |
|
1182 |
matched line so that nextLine will return this line." |
|
1183 |
||
1184 |
|line startPos linePos index| |
|
1185 |
||
1186 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1187 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 1188 |
binary ifTrue:[^ self errorBinary]. |
1189 |
||
1 | 1190 |
startPos := self position. |
1191 |
[self atEnd] whileFalse:[ |
|
159 | 1192 |
linePos := self position. |
1193 |
line := self nextLine. |
|
1194 |
line notNil ifTrue:[ |
|
1195 |
index := 1. |
|
1196 |
aCollectionOfStrings do:[:prefix | |
|
1197 |
(line startsWith:prefix) ifTrue:[ |
|
1198 |
self position:linePos. |
|
1199 |
^ index |
|
1200 |
]. |
|
1201 |
index := index + 1 |
|
1202 |
] |
|
1203 |
] |
|
1 | 1204 |
]. |
1205 |
self position:startPos. |
|
1206 |
^ nil |
|
1207 |
! ! |
|
1208 |
||
612 | 1209 |
!ExternalStream methodsFor:'misc functions'! |
1210 |
||
1211 |
async:aBoolean |
|
779 | 1212 |
"set/clear the async attribute - if set, the availability of data on |
612 | 1213 |
the receiver will trigger an ioInterrupt. |
779 | 1214 |
If cleared (which is the default) no special notification is made. |
1215 |
Notice: not every OS supports this - check with OS>>supportsIOInterrupts before" |
|
92 | 1216 |
|
1217 |
|fd| |
|
1218 |
||
159 | 1219 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
92 | 1220 |
fd := self fileDescriptor. |
612 | 1221 |
aBoolean ifTrue:[ |
1222 |
^ OperatingSystem enableIOInterruptsOn:fd |
|
1223 |
]. |
|
1224 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
92 | 1225 |
! |
1226 |
||
612 | 1227 |
backStep |
1228 |
"step back one element - |
|
1229 |
redefined, since position is redefined here" |
|
1230 |
||
1231 |
self position:(self position - 1) |
|
362 | 1232 |
! |
1233 |
||
612 | 1234 |
blocking:aBoolean |
1235 |
"set/clear the blocking attribute - if set (which is the default) |
|
1236 |
a read (using next) on the receiver will block until data is available. |
|
1237 |
If cleared, a read operation will immediately return with a value of |
|
1238 |
nil." |
|
92 | 1239 |
|
159 | 1240 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
612 | 1241 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor) |
1242 |
! |
|
1243 |
||
1244 |
close |
|
1245 |
"close the stream - tell operating system" |
|
1246 |
||
1247 |
filePointer isNil ifTrue:[^ self]. |
|
1248 |
Lobby unregister:self. |
|
1249 |
self closeFile. |
|
1250 |
filePointer := nil |
|
1251 |
! |
|
1252 |
||
1253 |
create |
|
1254 |
"create the stream |
|
1255 |
- this must be redefined in subclass" |
|
1256 |
||
1257 |
^ self subclassResponsibility |
|
1258 |
! |
|
1259 |
||
1260 |
ioctl:ioctlNumber |
|
1261 |
"to provide a simple ioctl facility - an ioctl is performed |
|
1262 |
on the underlying file; no arguments are passed." |
|
1263 |
||
1264 |
%{ /* NOCONTEXT */ |
|
791 | 1265 |
#ifndef MSDOS_LIKE |
612 | 1266 |
FILE *f; |
1267 |
int ret, ioNum, ioArg; |
|
1268 |
OBJ fp; |
|
1269 |
||
1270 |
_INST(lastErrorNumber) = nil; |
|
1271 |
if ((fp = _INST(filePointer)) != nil) { |
|
1272 |
if (__isSmallInteger(ioctlNumber)) { |
|
1273 |
ioNum = __intVal(ioctlNumber); |
|
1274 |
f = __FILEVal(fp); |
|
1275 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1276 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1277 |
|
612 | 1278 |
__BEGIN_INTERRUPTABLE__ |
1279 |
do { |
|
1280 |
ret = ioctl(fileno(f), ioNum); |
|
1281 |
} while ((ret < 0) && (errno == EINTR)); |
|
1282 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1283 |
__UNPROTECT__(self); |
612 | 1284 |
|
1285 |
if (ret >= 0) { |
|
1286 |
RETURN ( __MKSMALLINT(ret) ); |
|
1287 |
} |
|
1288 |
_INST(position) = nil; |
|
1289 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1290 |
} |
|
1291 |
} |
|
791 | 1292 |
#endif |
612 | 1293 |
%}. |
1294 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1295 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1296 |
" |
|
1297 |
ioctl-number is not an integer |
|
791 | 1298 |
or the system does not support ioctl (MSDOS) |
612 | 1299 |
" |
1300 |
^ self primitiveFailed |
|
1301 |
! |
|
1302 |
||
1303 |
ioctl:ioctlNumber with:arg |
|
1304 |
"to provide a simple ioctl facility - an ioctl is performed |
|
1305 |
on the underlying file; the argument is passed as argument. |
|
1306 |
If the argument is a number, its directly passed; if its a |
|
1307 |
kind of ByteArray (ByteArray, String or Structure) a pointer to |
|
1308 |
the data is passed. This allows performing most ioctls - however, |
|
1309 |
it might be tricky to setup the buffer." |
|
1310 |
||
1311 |
%{ /* NOCONTEXT */ |
|
791 | 1312 |
#ifndef MSDOS_LIKE |
612 | 1313 |
FILE *f; |
1314 |
int ret, ioNum; |
|
1315 |
OBJ fp; |
|
1316 |
||
1317 |
_INST(lastErrorNumber) = nil; |
|
1318 |
if ((fp = _INST(filePointer)) != nil) { |
|
1319 |
if (__isSmallInteger(ioctlNumber) |
|
1320 |
&& (__isSmallInteger(arg) || __isBytes(arg))) { |
|
1321 |
f = __FILEVal(fp); |
|
1322 |
ioNum = __intVal(ioctlNumber); |
|
1323 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1324 |
__PROTECT__(self); |
612 | 1325 |
__BEGIN_INTERRUPTABLE__ |
1326 |
do { |
|
1327 |
if (__isSmallInteger(arg)) { |
|
1328 |
ret = ioctl(fileno(f), ioNum, __intVal(arg)); |
|
1329 |
} else { |
|
1330 |
ret = ioctl(fileno(f), ioNum, __ByteArrayInstPtr(arg)->ba_element); |
|
1331 |
} |
|
1332 |
} while ((ret < 0) && (errno == EINTR)); |
|
1333 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1334 |
__UNPROTECT__(self); |
612 | 1335 |
|
1336 |
if (ret >= 0) { |
|
1337 |
RETURN ( __MKSMALLINT(ret) ); |
|
1338 |
} |
|
1339 |
_INST(position) = nil; |
|
1340 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1341 |
} |
|
1342 |
} |
|
791 | 1343 |
#endif |
612 | 1344 |
%}. |
1345 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1346 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1347 |
" |
|
1348 |
ioctl-number is not an integer or argument is not byteArray-like |
|
791 | 1349 |
or the system does not support ioctl (MSDOS) |
612 | 1350 |
" |
1351 |
^ self primitiveFailed |
|
1352 |
! |
|
1353 |
||
1354 |
open |
|
1355 |
"open the stream |
|
1356 |
- this must be redefined in subclass" |
|
1357 |
||
1358 |
^ self subclassResponsibility |
|
1359 |
! |
|
1360 |
||
1361 |
position |
|
1362 |
"return the position |
|
1363 |
- this must be redefined in subclass" |
|
1364 |
||
1365 |
^ self subclassResponsibility |
|
1366 |
! |
|
1367 |
||
1368 |
position:anInteger |
|
1369 |
"set the position |
|
1370 |
- this must be redefined in subclass" |
|
1371 |
||
1372 |
^ self subclassResponsibility |
|
1373 |
! |
|
1374 |
||
1375 |
reset |
|
1376 |
"set the read position to the beginning of the collection" |
|
1377 |
||
1378 |
self position:"0" 1 |
|
1379 |
! |
|
1380 |
||
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1381 |
setToEnd |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1382 |
"redefined since it must be implemented differently" |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1383 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1384 |
^ self subclassResponsibility |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1385 |
! |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1386 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1387 |
truncateTo:newSize |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1388 |
"truncate the underlying OS file to newSize. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1389 |
Warning: this may not be implemented on all platforms." |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1390 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1391 |
%{ /* NOCONTEXT */ |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1392 |
|
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1393 |
#ifdef HAS_FTRUNCATE |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1394 |
FILE *f; |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1395 |
OBJ fp; |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1396 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1397 |
if (((fp = _INST(filePointer)) != nil) |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1398 |
&& (_INST(mode) != @symbol(readonly))) { |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1399 |
if (__isSmallInteger(newSize)) { |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1400 |
f = __FILEVal(fp); |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1401 |
|
933
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1402 |
if (_INST(buffered) == true) { |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1403 |
__READING__(f) |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1404 |
fflush(f); |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1405 |
fseek(f, 0L, SEEK_END); /* needed in stdio */ |
2eebae059045
oops - when truncating buffered streams, we must flush & seek first (stdio trouble)
Claus Gittinger <cg@exept.de>
parents:
932
diff
changeset
|
1406 |
} |
932
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1407 |
ftruncate(fileno(f), __intVal(newSize)); |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1408 |
RETURN (self); |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1409 |
} |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1410 |
} |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1411 |
#endif |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1412 |
%}. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1413 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1414 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1415 |
^ self errorUnsupportedOperation |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1416 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1417 |
" |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1418 |
|s| |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1419 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1420 |
s := 'test' asFilename writeStream. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1421 |
s next:1000 put:$a. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1422 |
s truncateTo:100. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1423 |
s close. |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1424 |
|
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1425 |
('test' asFilename fileSize) printNL |
f57ec42ceb44
added interface to truncate & ftruncate (req. by Mr. Olberding)
Claus Gittinger <cg@exept.de>
parents:
872
diff
changeset
|
1426 |
" |
612 | 1427 |
! ! |
1428 |
||
1429 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
1430 |
||
1431 |
nextByte |
|
1432 |
"read the next byte and return it as an Integer; return nil on error. |
|
1433 |
This is allowed in both text and binary modes, always returning the |
|
1434 |
bytes binary value as an integer in 0..255." |
|
1435 |
||
1436 |
%{ /* NOCONTEXT */ |
|
1437 |
||
1438 |
FILE *f; |
|
1439 |
unsigned char byte; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1440 |
int cnt, _buffered; |
612 | 1441 |
OBJ fp; |
1442 |
||
1443 |
_INST(lastErrorNumber) = nil; |
|
1444 |
if (((fp = _INST(filePointer)) != nil) |
|
1445 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1446 |
f = __FILEVal(fp); |
|
1447 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1448 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1449 |
if (_buffered) { |
612 | 1450 |
__READING__(f) |
1451 |
} |
|
1452 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1453 |
__PROTECT__(self); |
612 | 1454 |
__BEGIN_INTERRUPTABLE__ |
1455 |
do { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1456 |
if (_buffered) { |
612 | 1457 |
cnt = read(fileno(f), &byte, 1); |
1458 |
} else { |
|
1459 |
cnt = fread(&byte, 1, 1, f); |
|
1460 |
} |
|
1461 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1462 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1463 |
__UNPROTECT__(self); |
612 | 1464 |
|
1465 |
if (cnt == 1) { |
|
1466 |
if (_INST(position) != nil) |
|
1467 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 1); |
|
1468 |
RETURN ( __MKSMALLINT(byte) ); |
|
1469 |
} |
|
1470 |
if (cnt == 0) { |
|
1471 |
_INST(hitEOF) = true; |
|
1472 |
/* |
|
1473 |
if (errno == EWOULDBLOCK) { |
|
1474 |
RETURN (nil); |
|
1475 |
} |
|
1476 |
*/ |
|
1477 |
} |
|
1478 |
_INST(position) = nil; |
|
1479 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1480 |
} |
|
1481 |
%}. |
|
1482 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
1483 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1484 |
^ self errorWriteOnly |
|
1485 |
! |
|
1486 |
||
1487 |
nextBytes:count into:anObject |
|
1488 |
"read the next count bytes into an object and return the number of |
|
1489 |
bytes read. On EOF, 0 is returned. |
|
1490 |
If the receiver is some socket/pipe-like stream, an exception |
|
1491 |
is raised if the connection is broken. |
|
1492 |
||
1493 |
The object must have non-pointer indexed instvars (i.e. it must be |
|
1494 |
a ByteArray, String, Float- or DoubleArray). |
|
1495 |
If anObject is a string or byteArray and reused, this provides the |
|
1496 |
fastest possible physical I/O (since no new objects are allocated). |
|
1497 |
||
1498 |
Use with care - non object oriented i/o. |
|
1499 |
Warning: in general, you cannot use this method to pass data from other |
|
1500 |
architectures since it does not care for byte order or float representation." |
|
1501 |
||
1502 |
^ self nextBytes:count into:anObject startingAt:1 |
|
10 | 1503 |
! |
1504 |
||
612 | 1505 |
nextBytes:count into:anObject startingAt:start |
1506 |
"read the next count bytes into an object and return the number of |
|
1507 |
bytes read or 0 on EOF. |
|
1508 |
If the receiver is some socket/pipe-like stream, an exception |
|
1509 |
is raised if the connection is broken. |
|
1510 |
Notice, that in contrast to other methods |
|
1511 |
here, this does NOT return nil on EOF, but the actual count. |
|
1512 |
Thus allowing read of partial blocks. |
|
1513 |
||
1514 |
The object must have non-pointer indexed instvars |
|
1515 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
1516 |
If anObject is a string or byteArray and reused, this provides the |
|
1517 |
fastest possible physical I/O (since no new objects are allocated). |
|
1518 |
||
1519 |
Use with care - non object oriented I/O. |
|
1520 |
Warning: in general, you cannot use this method to pass data from other |
|
1521 |
architectures since it does not care for byte order or float representation." |
|
1522 |
||
1523 |
%{ |
|
1524 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1525 |
int cnt, offs, ret, _buffered; |
612 | 1526 |
int objSize, nInstVars, nInstBytes; |
1527 |
char *cp; |
|
1528 |
OBJ pos, fp, oClass; |
|
1529 |
||
1530 |
_INST(lastErrorNumber) = nil; |
|
1531 |
if (((fp = _INST(filePointer)) != nil) |
|
1532 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1533 |
if (__bothSmallInteger(count, start)) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1534 |
f = __FILEVal(fp); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1535 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1536 |
oClass = __Class(anObject); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1537 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1538 |
case BYTEARRAY: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1539 |
case WORDARRAY: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1540 |
case LONGARRAY: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1541 |
case FLOATARRAY: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1542 |
case DOUBLEARRAY: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1543 |
break; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1544 |
default: |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1545 |
goto bad; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1546 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1547 |
cnt = __intVal(count); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1548 |
offs = __intVal(start) - 1; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1549 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1550 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1551 |
objSize = __Size(anObject) - nInstBytes; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1552 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1553 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1554 |
|
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1555 |
/* |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1556 |
* mhmh - since we are interruptable, anObject may move. |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1557 |
* therefore, fetch the cp-pointer within the loop |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1558 |
*/ |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1559 |
if (_buffered) { |
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1560 |
__READING__(f) |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1561 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1562 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1563 |
__BEGIN_INTERRUPTABLE__ |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1564 |
for (;;) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1565 |
errno = 0; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1566 |
/* |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1567 |
* because we are interruptable, refetch pointer |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1568 |
*/ |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1569 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1570 |
if (_buffered) { |
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1571 |
ret = read(fileno(f), cp, cnt); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1572 |
} else { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1573 |
if (feof(f)) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1574 |
ret = 0; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1575 |
break; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1576 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1577 |
ret = fread(cp, 1, cnt, f); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1578 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1579 |
if (ret < 0) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1580 |
if (errno == EINTR) |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1581 |
continue; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1582 |
else |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1583 |
break; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1584 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1585 |
cnt -= ret; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1586 |
if (cnt == 0 || ret == 0) |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1587 |
break; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1588 |
offs += ret; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1589 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1590 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1591 |
__END_INTERRUPTABLE__ |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1592 |
|
1044
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1593 |
cnt = __intVal(count) - cnt; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1594 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1595 |
if (cnt > 0) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1596 |
pos = _INST(position); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1597 |
if (pos != nil) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1598 |
_INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1599 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1600 |
RETURN (__MKSMALLINT(cnt)); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1601 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1602 |
if (cnt == 0) { |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1603 |
_INST(hitEOF) = true; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1604 |
RETURN (__MKSMALLINT(cnt)); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1605 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1606 |
|
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1607 |
_INST(position) = nil; |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1608 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1609 |
} |
c2fb3818e097
ExtSTream>>next: did not work correct if interrupted.
Stefan Vogel <sv@exept.de>
parents:
971
diff
changeset
|
1610 |
} |
612 | 1611 |
} |
1612 |
bad: ; |
|
1613 |
%}. |
|
1614 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
1615 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1616 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1617 |
" |
|
1618 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
1619 |
" |
|
1620 |
^ self primitiveFailed |
|
92 | 1621 |
! |
1622 |
||
612 | 1623 |
nextBytesInto:anObject |
1624 |
"read bytes into an object, regardless of binary/text mode. |
|
1625 |
The number of bytes to read is defined by the objects size. |
|
1626 |
Return the number of bytes read. On EOF, 0 is returned. |
|
1627 |
If the receiver is some socket/pipe-like stream, an exception |
|
1628 |
is raised if the connection is broken. |
|
1629 |
||
1630 |
The object to read into must have non-pointer indexed instvars |
|
1631 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
1632 |
If anObject is a string or byteArray and reused, this provides the |
|
1633 |
fastest possible physical I/O (since no new objects are allocated). |
|
1634 |
||
1635 |
Use with care - non object oriented i/o. |
|
1636 |
Warning: in general, you cannot use this method to pass data from other |
|
1637 |
architectures since it does not care for byte order or float representation." |
|
1638 |
||
1639 |
^ self nextBytes:(anObject size) into:anObject startingAt:1 |
|
1640 |
||
1641 |
" to read 100 bytes from a stream: |
|
1642 |
||
1643 |
|b aStream| |
|
1644 |
||
1645 |
aStream := 'smalltalk.rc' asFilename readStream. |
|
1646 |
b := ByteArray new:100. |
|
1647 |
aStream nextBytesInto:b. |
|
1648 |
aStream close. |
|
1649 |
b inspect |
|
1650 |
" |
|
1651 |
||
1652 |
" |
|
1653 |
|s aStream| |
|
1654 |
aStream := 'smalltalk.rc' asFilename readStream. |
|
1655 |
s := String new:100. |
|
1656 |
aStream nextBytesInto:s. |
|
1657 |
aStream close. |
|
1658 |
s inspect |
|
1659 |
" |
|
1660 |
! |
|
1661 |
||
1662 |
nextLong |
|
1663 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
1664 |
The returned value may be a LargeInteger. |
|
1665 |
(msb-first for compatibility with other smalltalks)" |
|
1666 |
||
1667 |
^ self nextUnsignedLongMSB:true |
|
1668 |
! |
|
1669 |
||
1670 |
nextLongMSB:msbFlag |
|
1671 |
"Read four bytes and return the value as a 32-bit signed Integer, |
|
1672 |
which may be a LargeInteger. |
|
1673 |
If msbFlag is true, value is read with most-significant byte first, |
|
1674 |
otherwise least-significant byte comes first. |
|
1675 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
1676 |
Works in both binary and text modes." |
|
1677 |
||
1678 |
%{ /* NOCONTEXT */ |
|
1679 |
OBJ fp; |
|
1680 |
||
1681 |
_INST(lastErrorNumber) = nil; |
|
1682 |
if (((fp = _INST(filePointer)) != nil) |
|
1683 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1684 |
FILE *f; |
|
1685 |
int first, second, third, fourth; |
|
1686 |
int value; |
|
1687 |
||
1688 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1689 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1690 |
__PROTECT__(self); |
612 | 1691 |
__BEGIN_INTERRUPTABLE__ |
1692 |
__READING__(f) |
|
1693 |
||
1694 |
do { |
|
1695 |
first = getc(f); |
|
1696 |
} while ((first < 0) && (errno == EINTR)); |
|
1697 |
if (first != EOF) { |
|
1698 |
do { |
|
1699 |
second = getc(f); |
|
1700 |
} while ((second < 0) && (errno == EINTR)); |
|
1701 |
if (second != EOF) { |
|
1702 |
do { |
|
1703 |
third = getc(f); |
|
1704 |
} while ((third < 0) && (errno == EINTR)); |
|
1705 |
if (third != EOF) { |
|
1706 |
do { |
|
1707 |
fourth = getc(f); |
|
1708 |
} while ((fourth < 0) && (errno == EINTR)); |
|
1709 |
if (fourth != EOF) { |
|
1710 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1711 |
__UNPROTECT__(self); |
612 | 1712 |
|
1713 |
if (msbFlag == true) { |
|
1714 |
value = (first & 0xFF); |
|
1715 |
value = (value<<8) | (second & 0xFF); |
|
1716 |
value = (value<<8) | (third & 0xFF); |
|
1717 |
value = (value<<8) | (fourth & 0xFF); |
|
1718 |
} else { |
|
1719 |
value = (fourth & 0xFF); |
|
1720 |
value = (value<<8) | (third & 0xFF); |
|
1721 |
value = (value<<8) | (second & 0xFF); |
|
1722 |
value = (value<<8) | (first & 0xFF); |
|
1723 |
} |
|
1724 |
if (_INST(position) != nil) { |
|
1725 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
1726 |
} |
|
1727 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
|
1728 |
RETURN ( __MKSMALLINT(value)); |
|
1729 |
} |
|
1730 |
RETURN ( _MKLARGEINT(value) ); |
|
1731 |
} |
|
1732 |
} |
|
1733 |
} |
|
1734 |
} |
|
1735 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1736 |
__UNPROTECT__(self); |
612 | 1737 |
|
1738 |
if (ferror(f) && (errno != 0)) { |
|
1739 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1740 |
_INST(position) = nil; |
|
1741 |
} else { |
|
1742 |
_INST(hitEOF) = true; |
|
1743 |
RETURN (nil); |
|
1744 |
} |
|
1745 |
} |
|
1746 |
%}. |
|
1747 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1748 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1749 |
^ self readError. |
|
362 | 1750 |
! |
1751 |
||
612 | 1752 |
nextShortMSB:msbFlag |
1753 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
1754 |
If msbFlag is true, value is read with most-significant byte first, |
|
1755 |
otherwise least-significant byte comes first. |
|
1756 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
1757 |
Works in both binary and text modes." |
|
1758 |
||
1759 |
%{ /* NOCONTEXT */ |
|
1760 |
OBJ fp; |
|
1761 |
||
1762 |
_INST(lastErrorNumber) = nil; |
|
1763 |
if (((fp = _INST(filePointer)) != nil) |
|
1764 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1765 |
FILE *f; |
|
1766 |
int first, second, err; |
|
1767 |
short value; |
|
1768 |
||
1769 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1770 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1771 |
__PROTECT__(self); |
612 | 1772 |
__BEGIN_INTERRUPTABLE__ |
1773 |
__READING__(f) |
|
1774 |
||
1775 |
do { |
|
1776 |
first = getc(f); |
|
1777 |
} while ((first < 0) && (errno == EINTR)); |
|
1778 |
||
1779 |
if (first != EOF) { |
|
1780 |
do { |
|
1781 |
second = getc(f); |
|
1782 |
} while ((second < 0) && (errno == EINTR)); |
|
1783 |
||
1784 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1785 |
__UNPROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1786 |
|
612 | 1787 |
if (second != EOF) { |
1788 |
if (_INST(position) != nil) { |
|
1789 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
1790 |
} |
|
1791 |
if (msbFlag == true) { |
|
1792 |
value = ((first & 0xFF) << 8) | (second & 0xFF); |
|
1793 |
} else { |
|
1794 |
value = ((second & 0xFF) << 8) | (first & 0xFF); |
|
1795 |
} |
|
1796 |
RETURN (__MKSMALLINT(value)); |
|
1797 |
} |
|
1798 |
} |
|
1799 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1800 |
__UNPROTECT__(self); |
612 | 1801 |
|
1802 |
if (ferror(f) && (errno != 0)) { |
|
1803 |
_INST(position) = nil; |
|
1804 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1805 |
} else { |
|
1806 |
_INST(hitEOF) = true; |
|
1807 |
RETURN (nil); |
|
1808 |
} |
|
1809 |
} |
|
1810 |
%}. |
|
1811 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1812 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1813 |
^ self readError. |
|
1814 |
! |
|
1815 |
||
1816 |
nextUnsignedLongMSB:msbFlag |
|
1817 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
1818 |
a LargeInteger. |
|
1819 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
1820 |
least-significant byte comes first. |
|
1821 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
1822 |
Works in both binary and text modes." |
|
1823 |
||
1824 |
%{ /* NOCONTEXT */ |
|
1825 |
OBJ fp; |
|
1826 |
||
1827 |
_INST(lastErrorNumber) = nil; |
|
1828 |
if (((fp = _INST(filePointer)) != nil) |
|
1829 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1830 |
FILE *f; |
|
1831 |
int first, second, third, fourth; |
|
1832 |
unsigned int value; |
|
1833 |
||
1834 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1835 |
__PROTECT__(self); |
612 | 1836 |
__BEGIN_INTERRUPTABLE__ |
1837 |
__READING__(f) |
|
1838 |
||
1839 |
do { |
|
1840 |
first = getc(f); |
|
1841 |
} while ((first < 0) && (errno == EINTR)); |
|
1842 |
if (first != EOF) { |
|
1843 |
do { |
|
1844 |
second = getc(f); |
|
1845 |
} while ((second < 0) && (errno == EINTR)); |
|
1846 |
if (second != EOF) { |
|
1847 |
do { |
|
1848 |
third = getc(f); |
|
1849 |
} while ((third < 0) && (errno == EINTR)); |
|
1850 |
if (third != EOF) { |
|
1851 |
do { |
|
1852 |
fourth = getc(f); |
|
1853 |
} while ((fourth < 0) && (errno == EINTR)); |
|
1854 |
if (fourth != EOF) { |
|
1855 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1856 |
__UNPROTECT__(self); |
612 | 1857 |
|
1858 |
if (msbFlag == true) { |
|
1859 |
value = (first & 0xFF); |
|
1860 |
value = (value<<8) | (second & 0xFF); |
|
1861 |
value = (value<<8) | (third & 0xFF); |
|
1862 |
value = (value<<8) | (fourth & 0xFF); |
|
1863 |
} else { |
|
1864 |
value = (fourth & 0xFF); |
|
1865 |
value = (value<<8) | (third & 0xFF); |
|
1866 |
value = (value<<8) | (second & 0xFF); |
|
1867 |
value = (value<<8) | (first & 0xFF); |
|
1868 |
} |
|
1869 |
if (_INST(position) != nil) { |
|
1870 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
1871 |
} |
|
1872 |
if (value <= _MAX_INT) { |
|
1873 |
RETURN ( __MKSMALLINT(value)); |
|
1874 |
} |
|
1875 |
RETURN ( _MKULARGEINT(value) ); |
|
1876 |
} |
|
1877 |
} |
|
1878 |
} |
|
1879 |
} |
|
1880 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1881 |
__UNPROTECT__(self); |
612 | 1882 |
|
1883 |
if (ferror(f) && (errno != 0)) { |
|
1884 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1885 |
_INST(position) = nil; |
|
1886 |
} else { |
|
1887 |
_INST(hitEOF) = true; |
|
1888 |
RETURN (nil); |
|
1889 |
} |
|
1890 |
} |
|
1891 |
%}. |
|
1892 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1893 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1894 |
^ self readError. |
|
1895 |
! |
|
1896 |
||
1897 |
nextUnsignedShortMSB:msbFlag |
|
1898 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
1899 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
1900 |
least-significant byte comes first. |
|
1901 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
1902 |
Works in both binary and text modes." |
|
1903 |
||
1904 |
%{ /* NOCONTEXT */ |
|
1905 |
OBJ fp; |
|
1906 |
||
1907 |
_INST(lastErrorNumber) = nil; |
|
1908 |
if (((fp = _INST(filePointer)) != nil) |
|
1909 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1910 |
FILE *f; |
|
1911 |
int first, second; |
|
1912 |
unsigned value; |
|
1913 |
||
1914 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1915 |
__PROTECT__(self); |
612 | 1916 |
__BEGIN_INTERRUPTABLE__ |
1917 |
__READING__(f) |
|
1918 |
||
1919 |
do { |
|
1920 |
first = getc(f); |
|
1921 |
} while ((first < 0) && (errno == EINTR)); |
|
1922 |
||
1923 |
if (first != EOF) { |
|
1924 |
do { |
|
1925 |
second = getc(f); |
|
1926 |
} while ((second < 0) && (errno == EINTR)); |
|
1927 |
||
1928 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1929 |
__UNPROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1930 |
|
612 | 1931 |
if (second != EOF) { |
1932 |
if (_INST(position) != nil) { |
|
1933 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
1934 |
} |
|
1935 |
if (msbFlag == true) { |
|
1936 |
value = ((first & 0xFF) << 8) | (second & 0xFF); |
|
1937 |
} else { |
|
1938 |
value = ((second & 0xFF) << 8) | (first & 0xFF); |
|
1939 |
} |
|
1940 |
RETURN (__MKSMALLINT(value)); |
|
1941 |
} |
|
1942 |
} |
|
1943 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1944 |
__UNPROTECT__(self); |
612 | 1945 |
|
1946 |
if (ferror(f) && (errno != 0)) { |
|
1947 |
_INST(position) = nil; |
|
1948 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1949 |
} else { |
|
1950 |
_INST(hitEOF) = true; |
|
1951 |
RETURN (nil); |
|
1952 |
} |
|
1953 |
} |
|
1954 |
%}. |
|
1955 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1956 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1957 |
^ self readError. |
|
1958 |
! |
|
1959 |
||
1960 |
nextWord |
|
1961 |
"in text-mode: |
|
1962 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
|
1963 |
return a string containing those characters. |
|
1964 |
in binary-mode: |
|
1965 |
read two bytes (msb-first) and return the value as a 16-bit |
|
1966 |
unsigned Integer (for compatibility with other smalltalks)" |
|
1967 |
||
1968 |
binary ifTrue:[ |
|
1969 |
^ self nextUnsignedShortMSB:true |
|
92 | 1970 |
]. |
612 | 1971 |
^ self nextAlphaNumericWord |
1972 |
! ! |
|
1973 |
||
1974 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
1975 |
||
1976 |
nextPutByte:aByteValue |
|
1977 |
"write a byte. |
|
1978 |
Works in both binary and text modes." |
|
1979 |
||
1980 |
%{ /* NOCONTEXT */ |
|
1981 |
||
1982 |
FILE *f; |
|
1983 |
char c; |
|
1984 |
OBJ pos, fp; |
|
1985 |
int cnt; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1986 |
int _buffered; |
612 | 1987 |
|
1988 |
_INST(lastErrorNumber) = nil; |
|
1989 |
if (((fp = _INST(filePointer)) != nil) |
|
1990 |
&& (_INST(mode) != @symbol(readonly))) { |
|
1991 |
if (__isSmallInteger(aByteValue)) { |
|
1992 |
c = __intVal(aByteValue); |
|
1993 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1994 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1995 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
1996 |
__PROTECT__(self); |
612 | 1997 |
__BEGIN_INTERRUPTABLE__ |
1998 |
/* |
|
1999 |
*#ifdef OLD |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2000 |
* if (_buffered) { |
612 | 2001 |
* cnt = write(fileno(f), &c, 1); |
2002 |
* } else |
|
2003 |
*#endif |
|
2004 |
* { |
|
2005 |
* __WRITING__(f) |
|
2006 |
* cnt = fwrite(&c, 1, 1, f); |
|
2007 |
*#ifndef OLD |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2008 |
* if (_buffered) { |
612 | 2009 |
* fflush(f); |
2010 |
* } |
|
2011 |
*#endif |
|
2012 |
* } |
|
2013 |
*/ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2014 |
if (_buffered) { |
612 | 2015 |
__WRITING__(f) |
2016 |
cnt = fwrite(&c, 1, 1, f); |
|
2017 |
} else { |
|
2018 |
do { |
|
2019 |
cnt = write(fileno(f), &c, 1); |
|
2020 |
} while ((cnt < 0) && (errno == EINTR)); |
|
2021 |
} |
|
2022 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2023 |
__UNPROTECT__(self); |
612 | 2024 |
|
2025 |
if (cnt == 1) { |
|
2026 |
pos = _INST(position); |
|
2027 |
if (pos != nil) |
|
2028 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
2029 |
RETURN (self); |
|
2030 |
} |
|
2031 |
if (cnt < 0) { |
|
2032 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2033 |
} |
|
2034 |
} |
|
2035 |
} |
|
2036 |
%}. |
|
2037 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2038 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
2039 |
^ self writeError. |
|
2040 |
! |
|
2041 |
||
2042 |
nextPutBytes:count from:anObject |
|
2043 |
"write count bytes from an object. |
|
2044 |
Return the number of bytes written or nil on error. |
|
2045 |
The object must have non-pointer indexed instvars |
|
2046 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
2047 |
Use with care - non object oriented i/o. |
|
2048 |
Warning: in general, you cannot use this method to pass data to other |
|
2049 |
architectures since it does not care for byte order or float representation." |
|
2050 |
||
2051 |
^ self nextPutBytes:count from:anObject startingAt:1 |
|
2052 |
! |
|
2053 |
||
2054 |
nextPutBytes:count from:anObject startingAt:start |
|
2055 |
"write count bytes from an object starting at index start. |
|
2056 |
return the number of bytes written - which could be 0. |
|
2057 |
The object must have non-pointer indexed instvars |
|
2058 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
2059 |
Use with care - non object oriented i/o. |
|
2060 |
Warning: in general, you cannot use this method to pass data to other |
|
2061 |
architectures since it does not care for byte order or float representation." |
|
2062 |
||
2063 |
%{ /* NOCONTEXT */ |
|
2064 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2065 |
int cnt, offs, _buffered; |
612 | 2066 |
int objSize, nInstVars, nInstBytes; |
2067 |
char *cp; |
|
2068 |
OBJ oClass, pos, fp; |
|
2069 |
||
2070 |
_INST(lastErrorNumber) = nil; |
|
2071 |
if (((fp = _INST(filePointer)) != nil) |
|
2072 |
&& (_INST(mode) != @symbol(readonly))) { |
|
2073 |
if (__bothSmallInteger(count, start)) { |
|
2074 |
oClass = __Class(anObject); |
|
2075 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
2076 |
case BYTEARRAY: |
|
2077 |
case WORDARRAY: |
|
2078 |
case LONGARRAY: |
|
2079 |
case FLOATARRAY: |
|
2080 |
case DOUBLEARRAY: |
|
2081 |
break; |
|
2082 |
default: |
|
2083 |
goto bad; |
|
2084 |
} |
|
2085 |
cnt = __intVal(count); |
|
2086 |
offs = __intVal(start) - 1; |
|
2087 |
f = __FILEVal(fp); |
|
2088 |
||
2089 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2090 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
2091 |
objSize = __Size(anObject) - nInstBytes; |
|
2092 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
2093 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2094 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2095 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2096 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2097 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2098 |
__PROTECT__(anObject); |
612 | 2099 |
__BEGIN_INTERRUPTABLE__ |
2100 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2101 |
if (_buffered) { |
612 | 2102 |
__WRITING__(f) |
2103 |
cnt = fwrite(cp, 1, cnt, f); |
|
2104 |
} else { |
|
2105 |
do { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2106 |
__FETCHPROTECT__(anObject, 1); |
612 | 2107 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
2108 |
cnt = write(fileno(f), cp, cnt); |
|
2109 |
} while ((cnt < 0) && (errno == EINTR)); |
|
2110 |
} |
|
2111 |
||
2112 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2113 |
__UNPROTECT__(anObject); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2114 |
__UNPROTECT__(self); |
612 | 2115 |
|
2116 |
if (cnt >= 0) { |
|
2117 |
pos = _INST(position); |
|
2118 |
if (pos != nil) |
|
2119 |
_INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
2120 |
RETURN ( __MKSMALLINT(cnt) ); |
|
2121 |
} |
|
2122 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2123 |
} |
|
2124 |
} |
|
2125 |
} |
|
2126 |
bad: ; |
|
2127 |
%}. |
|
2128 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
2129 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2130 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
2131 |
^ self primitiveFailed |
|
2132 |
! |
|
2133 |
||
2134 |
nextPutBytesFrom:anObject |
|
2135 |
"write bytes from an object; the number of bytes is defined by |
|
2136 |
the objects size. |
|
2137 |
Return the number of bytes written or nil on error. |
|
2138 |
The object must have non-pointer indexed instvars |
|
2139 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
2140 |
Use with care - non object oriented i/o. |
|
2141 |
Warning: in general, you cannot use this method to pass data to other |
|
2142 |
architectures since it does not care for byte order or float representation." |
|
2143 |
||
2144 |
^ self nextPutBytes:(anObject size) from:anObject startingAt:1 |
|
2145 |
! |
|
2146 |
||
2147 |
nextPutLong:aNumber MSB:msbFlag |
|
2148 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
2149 |
true, data is written most-significant byte first; otherwise least |
|
2150 |
first. |
|
2151 |
Works in both binary and text modes." |
|
2152 |
||
2153 |
%{ /* NOCONTEXT */ |
|
2154 |
||
2155 |
int num; |
|
2156 |
char bytes[4]; |
|
2157 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2158 |
int cnt, _buffered; |
612 | 2159 |
OBJ fp; |
2160 |
||
2161 |
_INST(lastErrorNumber) = nil; |
|
2162 |
if (((fp = _INST(filePointer)) != nil) |
|
2163 |
&& (_INST(mode) != @symbol(readonly)) |
|
2164 |
&& __isSmallInteger(aNumber)) { |
|
2165 |
num = __intVal(aNumber); |
|
2166 |
if (msbFlag == true) { |
|
2167 |
bytes[0] = (num >> 24) & 0xFF; |
|
2168 |
bytes[1] = (num >> 16) & 0xFF; |
|
2169 |
bytes[2] = (num >> 8) & 0xFF; |
|
2170 |
bytes[3] = num & 0xFF; |
|
2171 |
} else { |
|
2172 |
bytes[3] = (num >> 24) & 0xFF; |
|
2173 |
bytes[2] = (num >> 16) & 0xFF; |
|
2174 |
bytes[1] = (num >> 8) & 0xFF; |
|
2175 |
bytes[0] = num & 0xFF; |
|
2176 |
} |
|
2177 |
||
2178 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2179 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2180 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2181 |
__PROTECT__(self); |
612 | 2182 |
__BEGIN_INTERRUPTABLE__ |
2183 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2184 |
if (_buffered) { |
612 | 2185 |
__WRITING__(f) |
2186 |
cnt = fwrite(bytes, 1, 4, f); |
|
2187 |
} else { |
|
2188 |
do { |
|
2189 |
cnt = write(fileno(f), bytes, 4); |
|
2190 |
} while ((cnt < 0) && (errno == EINTR)); |
|
2191 |
} |
|
2192 |
||
2193 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2194 |
__UNPROTECT__(self); |
612 | 2195 |
|
2196 |
if (cnt == 4) { |
|
2197 |
if (_INST(position) != nil) { |
|
2198 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
2199 |
} |
|
2200 |
RETURN ( self ); |
|
2201 |
} |
|
2202 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2203 |
} |
|
2204 |
%}. |
|
2205 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2206 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
2207 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
2208 |
||
2209 |
aNumber isInteger ifTrue:[ |
|
2210 |
msbFlag ifTrue:[ |
|
2211 |
"high word first" |
|
2212 |
(self nextShortPut:(aNumber // 16r10000) MSB:true) isNil ifTrue:[^ nil]. |
|
2213 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:true |
|
2214 |
]. |
|
2215 |
"low word first" |
|
2216 |
(self nextShortPut:(aNumber \\ 16r10000) MSB:false) isNil ifTrue:[^ nil]. |
|
2217 |
^ self nextShortPut:(aNumber // 16r10000) MSB:false. |
|
92 | 2218 |
]. |
612 | 2219 |
self argumentMustBeInteger |
2220 |
! |
|
2221 |
||
2222 |
nextPutShort:aNumber MSB:msbFlag |
|
2223 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
2224 |
true, data is written most-significant byte first; otherwise least |
|
2225 |
first. |
|
2226 |
Works in both binary and text modes." |
|
2227 |
||
2228 |
%{ /* NOCONTEXT */ |
|
2229 |
||
2230 |
int num; |
|
2231 |
char bytes[2]; |
|
2232 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2233 |
int cnt, _buffered; |
612 | 2234 |
OBJ fp; |
2235 |
||
2236 |
_INST(lastErrorNumber) = nil; |
|
2237 |
if (((fp = _INST(filePointer)) != nil) |
|
2238 |
&& (_INST(mode) != @symbol(readonly)) |
|
2239 |
&& __isSmallInteger(aNumber)) { |
|
2240 |
num = __intVal(aNumber); |
|
2241 |
if (msbFlag == true) { |
|
2242 |
bytes[0] = (num >> 8) & 0xFF; |
|
2243 |
bytes[1] = num & 0xFF; |
|
2244 |
} else { |
|
2245 |
bytes[1] = (num >> 8) & 0xFF; |
|
2246 |
bytes[0] = num & 0xFF; |
|
2247 |
} |
|
2248 |
||
2249 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2250 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2251 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2252 |
__PROTECT__(self); |
612 | 2253 |
__BEGIN_INTERRUPTABLE__ |
2254 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2255 |
if (_buffered) { |
612 | 2256 |
__WRITING__(f) |
2257 |
cnt = fwrite(bytes, 1, 2, f); |
|
2258 |
} else { |
|
2259 |
do { |
|
2260 |
cnt = write(fileno(f), bytes, 2); |
|
2261 |
} while ((cnt < 0) && (errno == EINTR)); |
|
2262 |
} |
|
2263 |
||
2264 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2265 |
__UNPROTECT__(self); |
612 | 2266 |
|
2267 |
if (cnt == 2) { |
|
2268 |
if (_INST(position) != nil) { |
|
2269 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
2270 |
} |
|
2271 |
RETURN ( self ); |
|
2272 |
} |
|
2273 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2274 |
} |
|
2275 |
%}. |
|
2276 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
2277 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2278 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
2279 |
self argumentMustBeInteger |
|
10 | 2280 |
! ! |
612 | 2281 |
|
2282 |
!ExternalStream methodsFor:'private'! |
|
2283 |
||
2284 |
clearEOF |
|
2285 |
hitEOF := false |
|
2286 |
! |
|
2287 |
||
1048
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2288 |
connectTo:aFileDescriptor withMode:openmode |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2289 |
"connect a fileDescriptor; openmode is the string defining the way to open. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2290 |
This can be used to connect an extrnally provided fileDescriptor (from |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2291 |
primitive code) or a pipeFileDescriptor (as returned by makePipe) to |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2292 |
a Stream object. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2293 |
The openMode ('r', 'w' etc.) must match the mode in which |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2294 |
the fileDescriptor was originally opened (otherwise i/o errors will be reported later)." |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2295 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2296 |
|retVal| |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2297 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2298 |
filePointer notNil ifTrue:[^ self errorOpen]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2299 |
%{ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2300 |
FILE *f; |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2301 |
OBJ fp; |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2302 |
|
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2303 |
if (__isSmallInteger(aFileDescriptor) |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2304 |
&& (__qClass(openmode)== @global(String))) { |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2305 |
f = (FILE *) fdopen(__intVal(aFileDescriptor), (char *)__stringVal(openmode)); |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2306 |
if (f == NULL) { |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2307 |
_INST(lastErrorNumber) = _MKSMALLINT(errno); |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2308 |
_INST(position) = nil; |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2309 |
} else { |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2310 |
_INST(filePointer) = fp = __MKOBJ((int)f); __STORE(self, fp); |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2311 |
_INST(position) = _MKSMALLINT(1); |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2312 |
retVal = self; |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2313 |
} |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2314 |
} |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2315 |
%}. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2316 |
retVal notNil ifTrue:[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2317 |
buffered := true. "default is buffered" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2318 |
Lobby register:self |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2319 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2320 |
lastErrorNumber notNil ifTrue:[ |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2321 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2322 |
the open failed for some reason ... |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2323 |
" |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2324 |
^ self openError |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2325 |
]. |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2326 |
^ retVal |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2327 |
! |
c535c5a73cba
added facilities to connect to given Fd's and #makePipe
Claus Gittinger <cg@exept.de>
parents:
1044
diff
changeset
|
2328 |
|
612 | 2329 |
reOpen |
2330 |
"sent after snapin to reopen streams. |
|
2331 |
cannot reopen here since I am abstract and have no device knowledge" |
|
2332 |
||
2333 |
self class name errorPrint. ': cannot reOpen stream - stream closed' errorPrintNL. |
|
2334 |
filePointer := nil. |
|
2335 |
Lobby unregister:self. |
|
2336 |
! |
|
2337 |
||
2338 |
setLastError:aNumber |
|
2339 |
lastErrorNumber := aNumber |
|
2340 |
! ! |
|
2341 |
||
2342 |
!ExternalStream methodsFor:'queries'! |
|
2343 |
||
2344 |
isBinary |
|
2345 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
2346 |
The default when created is false." |
|
2347 |
||
2348 |
^ binary |
|
2349 |
! |
|
2350 |
||
2351 |
isExternalStream |
|
2352 |
"return true, if the receiver is some kind of externalStream; |
|
2353 |
true is returned here - the method redefined from Object." |
|
2354 |
||
2355 |
^ true |
|
2356 |
! |
|
2357 |
||
2358 |
isReadable |
|
2359 |
"return true, if this stream can be read from" |
|
2360 |
||
2361 |
^ (mode ~~ #writeonly) |
|
2362 |
! |
|
2363 |
||
2364 |
isWritable |
|
2365 |
"return true, if this stream can be written to" |
|
2366 |
||
2367 |
^ (mode ~~ #readonly) |
|
2368 |
! ! |
|
2369 |
||
2370 |
!ExternalStream methodsFor:'reading'! |
|
2371 |
||
2372 |
next |
|
2373 |
"return the next element; advance read position. |
|
2374 |
In binary mode, an integer is returned, otherwise a character. |
|
2375 |
If there are no more elements, nil is returned." |
|
1 | 2376 |
|
2377 |
%{ /* NOCONTEXT */ |
|
2378 |
||
2379 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2380 |
int c, _buffered; |
612 | 2381 |
OBJ pos, fp; |
2382 |
unsigned char ch; |
|
2383 |
||
2384 |
_INST(lastErrorNumber) = nil; |
|
2385 |
if (((fp = _INST(filePointer)) != nil) |
|
2386 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
2387 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2388 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2389 |
_buffered = (_INST(buffered) == false); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2390 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2391 |
__PROTECT__(self); |
612 | 2392 |
__BEGIN_INTERRUPTABLE__ |
2393 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2394 |
if (_buffered) { |
612 | 2395 |
__READING__(f) |
2396 |
} |
|
2397 |
||
2398 |
do { |
|
2399 |
errno = 0; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2400 |
if (_buffered) { |
612 | 2401 |
if (read(fileno(f), &ch, 1) != 1) |
2402 |
c = EOF; |
|
2403 |
else |
|
2404 |
c = ch; |
|
2405 |
} else |
|
2406 |
{ |
|
2407 |
c = getc(f); |
|
2408 |
} |
|
2409 |
} while ((c < 0) && (errno == EINTR)); |
|
2410 |
||
2411 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2412 |
__UNPROTECT__(self); |
612 | 2413 |
|
2414 |
if (c != EOF) { |
|
2415 |
pos = _INST(position); |
|
2416 |
if (__isSmallInteger(pos)) { |
|
2417 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
2418 |
} else { |
|
2419 |
_INST(position) = nil; |
|
2420 |
} |
|
2421 |
if (_INST(binary) == true) { |
|
2422 |
RETURN ( __MKSMALLINT(c & 0xFF) ); |
|
2423 |
} |
|
2424 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
|
2425 |
} |
|
2426 |
/* |
|
2427 |
if (errno == EWOULDBLOCK) { |
|
2428 |
_INST(hitEOF) = true; |
|
2429 |
RETURN(nil); |
|
2430 |
} |
|
2431 |
*/ |
|
2432 |
_INST(position) = nil; |
|
2433 |
||
2434 |
if ((_INST(buffered) == false) |
|
2435 |
|| (ferror(f) && (errno != 0))) { |
|
2436 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2437 |
} else { |
|
2438 |
_INST(hitEOF) = true; |
|
2439 |
RETURN ( nil ); |
|
2440 |
} |
|
2441 |
} |
|
2442 |
%}. |
|
2443 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2444 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2445 |
self errorWriteOnly |
|
2446 |
! |
|
2447 |
||
2448 |
next:count |
|
2449 |
"return the next count elements of the stream as a collection. |
|
2450 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
2451 |
||
2452 |
|coll| |
|
2453 |
||
2454 |
binary ifTrue:[ |
|
2455 |
coll := ByteArray uninitializedNew:count |
|
2456 |
] ifFalse:[ |
|
2457 |
coll := String new:count |
|
2458 |
]. |
|
872 | 2459 |
self nextBytes:count into:coll startingAt:1. |
612 | 2460 |
^ coll |
2461 |
! |
|
2462 |
||
2463 |
peek |
|
2464 |
"return the element to be read next without advancing read position. |
|
2465 |
In binary mode, an integer is returned, otherwise a character. |
|
2466 |
If there are no more elements, nil is returned. |
|
2467 |
Not allowed in unbuffered mode." |
|
2468 |
||
2469 |
%{ /* NOCONTEXT */ |
|
2470 |
||
2471 |
FILE *f; |
|
2472 |
REGISTER int c; |
|
216 | 2473 |
OBJ fp; |
92 | 2474 |
|
159 | 2475 |
_INST(lastErrorNumber) = nil; |
216 | 2476 |
if (((fp = _INST(filePointer)) != nil) |
159 | 2477 |
&& (_INST(mode) != @symbol(writeonly))) { |
612 | 2478 |
#ifdef OLD |
2479 |
if (_INST(buffered) == true) |
|
2480 |
#endif |
|
2481 |
{ |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2482 |
f = __FILEVal(fp); |
612 | 2483 |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2484 |
__PROTECT__(self); |
362 | 2485 |
__BEGIN_INTERRUPTABLE__ |
216 | 2486 |
__READING__(f) |
159 | 2487 |
|
612 | 2488 |
do { |
2489 |
c = getc(f); |
|
2490 |
} while ((c < 0) && (errno == EINTR)); |
|
2491 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2492 |
__UNPROTECT__(self); |
612 | 2493 |
|
2494 |
if (c != EOF) { |
|
2495 |
ungetc(c, f); |
|
2496 |
if (_INST(binary) == true) { |
|
2497 |
RETURN ( __MKSMALLINT(c & 0xFF) ); |
|
159 | 2498 |
} |
612 | 2499 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
159 | 2500 |
} |
216 | 2501 |
if (ferror(f) && (errno != 0)) { |
359 | 2502 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 2503 |
} else { |
2504 |
_INST(hitEOF) = true; |
|
2505 |
RETURN ( nil ); |
|
2506 |
} |
|
2507 |
} |
|
1 | 2508 |
} |
159 | 2509 |
%}. |
216 | 2510 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
1 | 2511 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
612 | 2512 |
buffered ifFalse:[^ self errorNotBuffered]. |
159 | 2513 |
^ self errorWriteOnly |
612 | 2514 |
! ! |
2515 |
||
2516 |
!ExternalStream methodsFor:'reimplemented for speed'! |
|
159 | 2517 |
|
2518 |
nextAlphaNumericWord |
|
379 | 2519 |
"read the next word (i.e. up to non letter-or-digit) after first |
2520 |
skipping any whiteSpace. |
|
2521 |
Return a string containing those characters. |
|
2522 |
There is a limit of 1023 characters in the word - if longer, |
|
2523 |
it is truncated." |
|
159 | 2524 |
|
2525 |
%{ /* STACK: 2000 */ |
|
2526 |
FILE *f; |
|
2527 |
int len; |
|
2528 |
char buffer[1024]; |
|
2529 |
int ch; |
|
2530 |
int cnt = 0; |
|
216 | 2531 |
OBJ fp; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
2532 |
OBJ __MKSTRING_L(); |
216 | 2533 |
|
2534 |
if (((fp = _INST(filePointer)) != nil) |
|
159 | 2535 |
&& (_INST(mode) != @symbol(writeonly))) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2536 |
f = __FILEVal(fp); |
362 | 2537 |
__BEGIN_INTERRUPTABLE__ |
216 | 2538 |
__READING__(f) |
159 | 2539 |
|
370 | 2540 |
/* |
2541 |
* skip whiteSpace first ... |
|
2542 |
*/ |
|
159 | 2543 |
for (;;) { |
213 | 2544 |
do { |
2545 |
ch = getc(f); |
|
2546 |
} while ((ch < 0) && (errno == EINTR)); |
|
362 | 2547 |
|
159 | 2548 |
if (ch < 0) { |
216 | 2549 |
if (ferror(f) && (errno != 0)) { |
359 | 2550 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 2551 |
__END_INTERRUPTABLE__ |
159 | 2552 |
goto err; |
2553 |
} |
|
2554 |
_INST(hitEOF) = true; |
|
2555 |
break; |
|
2556 |
} |
|
2557 |
cnt++; |
|
2558 |
||
379 | 2559 |
#ifndef NON_ASCII |
159 | 2560 |
if (ch >= ' ') break; |
379 | 2561 |
#endif |
159 | 2562 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') |
2563 |
&& (ch != '\n') && (ch != 0x0b)) break; |
|
2564 |
} |
|
2565 |
ungetc(ch, f); |
|
2566 |
cnt--; |
|
2567 |
||
2568 |
len = 0; |
|
2569 |
for (;;) { |
|
213 | 2570 |
do { |
2571 |
ch = getc(f); |
|
2572 |
} while ((ch < 0) && (errno == EINTR)); |
|
159 | 2573 |
if (ch < 0) { |
216 | 2574 |
if (ferror(f) && (errno != 0)) { |
359 | 2575 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 2576 |
__END_INTERRUPTABLE__ |
159 | 2577 |
goto err; |
2578 |
} |
|
2579 |
_INST(hitEOF) = true; |
|
2580 |
break; |
|
2581 |
} |
|
2582 |
||
2583 |
ch &= 0xFF; |
|
2584 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
|
2585 |
((ch >= 'A') && (ch <= 'Z')) || |
|
2586 |
((ch >= '0') && (ch <= '9')))) { |
|
2587 |
ungetc(ch, f); |
|
2588 |
break; |
|
2589 |
} |
|
2590 |
cnt++; |
|
2591 |
buffer[len++] = ch; |
|
370 | 2592 |
if (len >= (sizeof(buffer)-1)) { |
159 | 2593 |
/* emergency */ |
2594 |
break; |
|
2595 |
} |
|
2596 |
} |
|
362 | 2597 |
__END_INTERRUPTABLE__ |
159 | 2598 |
|
2599 |
if (_INST(position) != nil) { |
|
359 | 2600 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + cnt); |
159 | 2601 |
} |
2602 |
if (len != 0) { |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
2603 |
buffer[len] = '\0'; |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
2604 |
RETURN ( __MKSTRING_L(buffer, len COMMA_CON) ); |
159 | 2605 |
} |
2606 |
RETURN ( nil ); |
|
2607 |
} |
|
2608 |
err: ; |
|
2609 |
%}. |
|
2610 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2611 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2612 |
^ self errorWriteOnly |
|
1 | 2613 |
! |
2614 |
||
2615 |
nextChunk |
|
2616 |
"return the next chunk, i.e. all characters up to the next |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2617 |
exclamation mark. Within the chunk, exclamation marks have to be doubled - |
216 | 2618 |
except within primitive code (this exception was added to make it easier |
2619 |
to edit primitive code with external editors). This means, that other |
|
2620 |
Smalltalks cannot always read chunks containing primitive code - but that |
|
2621 |
doesnt really matter, since C-primitives are an ST/X feature anyway. |
|
2622 |
Reimplemented here for more speed." |
|
2623 |
||
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2624 |
|retVal outOfMemory buffer| |
1 | 2625 |
|
159 | 2626 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2627 |
binary ifTrue:[^ self errorBinary]. |
|
2628 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
216 | 2629 |
|
443 | 2630 |
%{ /* STACK: 8000 */ |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2631 |
/* |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2632 |
* the main trick (and a bit of complication) here |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2633 |
* is to read the first 4k into a stack-buffer. |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2634 |
* Since most chunks fit into that, |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2635 |
* this avoids creating lots of garbage for thos small chunks. |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2636 |
*/ |
1 | 2637 |
FILE *f; |
2638 |
int done = 0; |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2639 |
REGISTER int c, lastC; |
216 | 2640 |
int peekC; |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2641 |
char *bufferPtr = (char *)0; |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2642 |
char fastBuffer[7000]; |
1 | 2643 |
REGISTER int index; |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2644 |
int currSize, fastFlag; |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2645 |
int atBeginOfLine = 1, inComment, inString, inPrimitive = 0; |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2646 |
OBJ __MKSTRING_L(), __MKSTRING_ST_L(), __MKEMPTYSTRING(); |
216 | 2647 |
|
159 | 2648 |
_INST(lastErrorNumber) = nil; |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2649 |
f = __FILEVal(_INST(filePointer)); |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2650 |
|
216 | 2651 |
__READING__(f) |
159 | 2652 |
|
2653 |
if (feof(f)) { |
|
422 | 2654 |
_INST(hitEOF) = true; |
159 | 2655 |
RETURN (nil); |
92 | 2656 |
} |
436 | 2657 |
|
1 | 2658 |
/* |
2659 |
* skip spaces |
|
2660 |
*/ |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2661 |
c = '\n'; |
1 | 2662 |
while (! done) { |
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2663 |
lastC = c; |
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2664 |
|
362 | 2665 |
__BEGIN_INTERRUPTABLE__ |
159 | 2666 |
do { |
2667 |
c = getc(f); |
|
2668 |
} while ((c < 0) && (errno == EINTR)); |
|
362 | 2669 |
__END_INTERRUPTABLE__ |
159 | 2670 |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2671 |
atBeginOfLine = 0; |
159 | 2672 |
switch (c) { |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2673 |
case '\n': |
159 | 2674 |
case ' ': |
2675 |
case '\t': |
|
2676 |
case '\r': |
|
2677 |
case '\b': |
|
2678 |
case '\014': |
|
2679 |
break; |
|
2680 |
||
2681 |
case EOF: |
|
216 | 2682 |
if (ferror(f) && (errno != 0)) { |
359 | 2683 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 2684 |
goto err; |
2685 |
} |
|
2686 |
_INST(hitEOF) = true; |
|
2687 |
RETURN (nil); |
|
2688 |
||
2689 |
default: |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2690 |
atBeginOfLine = (lastC == '\n'); |
159 | 2691 |
ungetc(c, f); |
2692 |
done = 1; |
|
2693 |
break; |
|
2694 |
} |
|
1 | 2695 |
} |
2696 |
||
2697 |
/* |
|
2698 |
* read chunk into a buffer |
|
2699 |
*/ |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2700 |
bufferPtr = fastBuffer; fastFlag = 1; |
216 | 2701 |
currSize = sizeof(fastBuffer); |
362 | 2702 |
|
1 | 2703 |
index = 0; |
2704 |
while (! feof(f)) { |
|
216 | 2705 |
/* |
2706 |
* do we have to resize the buffer ? |
|
2707 |
*/ |
|
159 | 2708 |
if ((index+2) >= currSize) { |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2709 |
OBJ newBuffer; |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2710 |
char *nbp; |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2711 |
|
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2712 |
newBuffer = __MKEMPTYSTRING(currSize * 2 COMMA_CON); |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2713 |
if (newBuffer == nil) { |
216 | 2714 |
/* |
2715 |
* mhmh - chunk seems to be very big .... |
|
2716 |
*/ |
|
2717 |
outOfMemory = true; |
|
2718 |
goto err; |
|
2719 |
} |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2720 |
nbp = __stringVal(newBuffer); |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2721 |
if (!fastFlag) { |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2722 |
/* |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2723 |
* old buffer may have moved - refetch pointer |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2724 |
*/ |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2725 |
bufferPtr = __stringVal(buffer); |
216 | 2726 |
} |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2727 |
bcopy(bufferPtr, nbp, index); |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2728 |
bufferPtr = nbp; |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2729 |
bufferPtr[index] = '\0'; |
159 | 2730 |
buffer = newBuffer; |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2731 |
fastFlag = 0; |
159 | 2732 |
currSize = currSize * 2; |
2733 |
} |
|
362 | 2734 |
|
2735 |
__BEGIN_INTERRUPTABLE__ |
|
159 | 2736 |
do { |
2737 |
c = getc(f); |
|
2738 |
} while (c < 0 && (errno == EINTR)); |
|
362 | 2739 |
__END_INTERRUPTABLE__ |
159 | 2740 |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2741 |
if (atBeginOfLine && (c == '%')) { |
362 | 2742 |
__BEGIN_INTERRUPTABLE__ |
188 | 2743 |
do { |
213 | 2744 |
peekC = getc(f); |
188 | 2745 |
} while (peekC < 0 && (errno == EINTR)); |
362 | 2746 |
__END_INTERRUPTABLE__ |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2747 |
|
159 | 2748 |
ungetc(peekC, f); |
2749 |
if (peekC == '{') { |
|
2750 |
inPrimitive++; |
|
2751 |
} else if (peekC == '}') { |
|
687
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2752 |
if (inPrimitive > 0) { |
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2753 |
inPrimitive--; |
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2754 |
} |
159 | 2755 |
} |
2756 |
} else { |
|
688 | 2757 |
if (! inPrimitive) { |
159 | 2758 |
if (c == '!') { |
362 | 2759 |
__BEGIN_INTERRUPTABLE__ |
159 | 2760 |
do { |
2761 |
c = getc(f); |
|
2762 |
} while (c < 0 && (errno == EINTR)); |
|
362 | 2763 |
__END_INTERRUPTABLE__ |
2764 |
||
159 | 2765 |
if (c != '!') { |
2766 |
ungetc(c, f); |
|
2767 |
break; |
|
2768 |
} |
|
2769 |
} |
|
2770 |
} |
|
2771 |
} |
|
2772 |
||
2773 |
if (c < 0) { |
|
436 | 2774 |
_INST(hitEOF) = true; |
216 | 2775 |
if (ferror(f) && (errno != 0)) { |
359 | 2776 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 2777 |
goto err; |
2778 |
} |
|
2779 |
break; |
|
2780 |
} |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2781 |
bufferPtr[index++] = c; |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2782 |
atBeginOfLine = (c == '\n'); |
1 | 2783 |
} |
92 | 2784 |
|
1 | 2785 |
/* |
2786 |
* make it a string |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2787 |
* be careful here - allocating a new string may make the |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2788 |
* existing buffer move around. Need to check if copying from |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2789 |
* fast (C) buffer or from real (ST) buffer. |
1 | 2790 |
*/ |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2791 |
if (fastFlag) { |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2792 |
retVal = __MKSTRING_L(bufferPtr, index COMMA_CON); |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2793 |
} else { |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2794 |
retVal = __MKSTRING_ST_L(buffer, index COMMA_CON); |
216 | 2795 |
} |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2796 |
err: ; |
159 | 2797 |
%}. |
370 | 2798 |
retVal isNil ifTrue:[ |
379 | 2799 |
"/ |
2800 |
"/ arrive here with retVal==nil either on error or premature EOF |
|
2801 |
"/ or if running out of malloc-memory |
|
2802 |
"/ |
|
2803 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2804 |
outOfMemory == true ifTrue:[ |
|
370 | 2805 |
" |
711 | 2806 |
buffer memory allocation failed. |
2807 |
When we arrive here, there was no memory available for the |
|
370 | 2808 |
chunk. (seems to be too big of a chunk ...) |
466
6e88139088a1
raise new mallocFailureSignal when running out of buffer memory
Claus Gittinger <cg@exept.de>
parents:
454
diff
changeset
|
2809 |
Bad luck - you should increase the ulimit and/or swap space on your machine. |
370 | 2810 |
" |
711 | 2811 |
^ ObjectMemory allocationFailureSignal raise. |
370 | 2812 |
] |
216 | 2813 |
]. |
1 | 2814 |
^ retVal |
612 | 2815 |
! |
2816 |
||
2817 |
nextMatchFor:anObject |
|
2818 |
"skip all objects up-to and including anObject, return anObject on success, |
|
2819 |
nil if end-of-file is reached before. The next read operation will return |
|
2820 |
the element after anObject. |
|
2821 |
Only single byte characters are currently supported." |
|
2822 |
||
2823 |
%{ /* NOCONTEXT */ |
|
2824 |
||
2825 |
FILE *f; |
|
2826 |
int peekValue, c; |
|
2827 |
OBJ fp; |
|
2828 |
||
2829 |
_INST(lastErrorNumber) = nil; |
|
2830 |
if (((fp = _INST(filePointer)) != nil) |
|
2831 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
2832 |
if ((_INST(binary) == true) && __isSmallInteger(anObject)) { |
|
2833 |
peekValue = __intVal(anObject) & 0xFF; |
|
2834 |
} else { |
|
2835 |
if ((_INST(binary) != true) && __isCharacter(anObject)) { |
|
2836 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
2837 |
} else { |
|
2838 |
peekValue = -1; |
|
2839 |
} |
|
2840 |
} |
|
2841 |
||
2842 |
if (peekValue >= 0) { |
|
2843 |
_INST(position) = nil; |
|
2844 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2845 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2846 |
__PROTECT__(self); |
612 | 2847 |
__BEGIN_INTERRUPTABLE__ |
2848 |
__READING__(f) |
|
2849 |
||
2850 |
for (;;) { |
|
2851 |
do { |
|
2852 |
c = getc(f); |
|
2853 |
} while ((c < 0) && (errno == EINTR)); |
|
2854 |
||
2855 |
if (c == EOF) { |
|
2856 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2857 |
__UNPROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2858 |
|
612 | 2859 |
if (ferror(f) && (errno != 0)) { |
2860 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2861 |
break; |
|
2862 |
} |
|
2863 |
_INST(hitEOF) = true; |
|
2864 |
RETURN (nil); |
|
2865 |
} |
|
2866 |
if (c == peekValue) { |
|
2867 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2868 |
__UNPROTECT__(self); |
612 | 2869 |
RETURN (anObject); |
2870 |
} |
|
2871 |
} |
|
2872 |
} |
|
2873 |
} |
|
2874 |
%}. |
|
2875 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2876 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2877 |
^ super nextMatchFor:anObject |
|
2878 |
! |
|
2879 |
||
2880 |
peekFor:anObject |
|
2881 |
"return true and move past next element, if next == something. |
|
2882 |
Otherwise, stay and return false. False is also returned |
|
2883 |
when EOF is encountered. |
|
2884 |
The argument must be an integer if in binary, a character if in |
|
2885 |
text mode; only single byte characters are currently supported." |
|
2886 |
||
2887 |
%{ /* NOCONTEXT */ |
|
2888 |
||
2889 |
FILE *f; |
|
2890 |
int c; |
|
2891 |
int peekValue; |
|
2892 |
OBJ fp; |
|
2893 |
||
2894 |
_INST(lastErrorNumber) = nil; |
|
2895 |
if (((fp = _INST(filePointer)) != nil) |
|
2896 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
2897 |
if (_INST(binary) == true) { |
|
2898 |
if (__isSmallInteger(anObject)) { |
|
2899 |
peekValue = __intVal(anObject) & 0xFF; |
|
2900 |
} else { |
|
2901 |
goto bad; |
|
2902 |
} |
|
2903 |
} else { |
|
2904 |
if (__isCharacter(anObject)) { |
|
2905 |
peekValue = __intVal(_characterVal(anObject)) & 0xFF; |
|
2906 |
} else { |
|
2907 |
goto bad; |
|
2908 |
} |
|
2909 |
} |
|
2910 |
||
2911 |
f = __FILEVal(fp); |
|
2912 |
||
2913 |
if (feof(f)) { |
|
2914 |
_INST(hitEOF) = true; |
|
2915 |
RETURN (false); |
|
2916 |
} |
|
2917 |
||
2918 |
__READING__(f) |
|
2919 |
||
2920 |
errno = 0; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2921 |
__PROTECT__(self); |
612 | 2922 |
__BEGIN_INTERRUPTABLE__ |
2923 |
do { |
|
2924 |
if (feof(f)) { |
|
2925 |
break; |
|
2926 |
} |
|
2927 |
c = getc(f); |
|
2928 |
} while ((c < 0) && (errno == EINTR)); |
|
2929 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
2930 |
__UNPROTECT__(self); |
612 | 2931 |
|
2932 |
if (feof(f)) { |
|
2933 |
_INST(hitEOF) = true; |
|
2934 |
} |
|
2935 |
||
2936 |
if (c == peekValue) { |
|
2937 |
OBJ pos; |
|
2938 |
||
2939 |
if ((pos = _INST(position)) != nil) { |
|
2940 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
2941 |
} |
|
2942 |
RETURN (true); |
|
2943 |
} |
|
2944 |
||
2945 |
if (c != EOF) { |
|
2946 |
ungetc(c, f); |
|
2947 |
RETURN (false); |
|
2948 |
} |
|
2949 |
||
2950 |
_INST(hitEOF) = true; |
|
2951 |
if (ferror(f) && (errno != 0)) { |
|
2952 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2953 |
} else { |
|
2954 |
RETURN (false); |
|
2955 |
} |
|
2956 |
} |
|
2957 |
bad: ; |
|
2958 |
%}. |
|
2959 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
2960 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2961 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2962 |
^ super peekFor:anObject |
|
2963 |
! |
|
2964 |
||
2965 |
skipLine |
|
2966 |
"read the next line (characters up to newline) skip only; |
|
2967 |
return nil if EOF reached, self otherwise. |
|
2968 |
Not allowed in binary mode." |
|
2969 |
||
2970 |
%{ /* STACK:2000 */ |
|
2971 |
||
2972 |
FILE *f; |
|
2973 |
char buffer[1024]; |
|
2974 |
OBJ fp; |
|
2975 |
||
2976 |
_INST(lastErrorNumber) = nil; |
|
2977 |
if (((fp = _INST(filePointer)) != nil) |
|
2978 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
2979 |
if (_INST(binary) != true) { |
|
2980 |
f = __FILEVal(fp); |
|
2981 |
||
2982 |
__READING__(f) |
|
2983 |
||
2984 |
__BEGIN_INTERRUPTABLE__ |
|
2985 |
if (fgets(buffer, sizeof(buffer)-1, f) != NULL) { |
|
2986 |
__END_INTERRUPTABLE__ |
|
2987 |
RETURN ( self ); |
|
2988 |
} |
|
2989 |
__END_INTERRUPTABLE__ |
|
2990 |
||
2991 |
if (ferror(f) && (errno != 0)) { |
|
2992 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2993 |
} else { |
|
2994 |
_INST(hitEOF) = true; |
|
2995 |
RETURN ( nil ); |
|
2996 |
} |
|
2997 |
} |
|
2998 |
} |
|
2999 |
%}. |
|
3000 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3001 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3002 |
binary ifTrue:[^ self errorBinary]. |
|
3003 |
^ self errorWriteOnly |
|
3004 |
! |
|
3005 |
||
3006 |
skipSeparators |
|
3007 |
"skip all whitespace; next will return next non-white-space character |
|
3008 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
3009 |
- reimplemented for speed" |
|
3010 |
||
3011 |
%{ /* NOCONTEXT */ |
|
3012 |
||
3013 |
FILE *f; |
|
3014 |
REGISTER int c; |
|
3015 |
OBJ fp; |
|
3016 |
||
3017 |
_INST(lastErrorNumber) = nil; |
|
3018 |
if (((fp = _INST(filePointer)) != nil) |
|
3019 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
3020 |
if (_INST(binary) != true) { |
|
3021 |
f = __FILEVal(fp); |
|
3022 |
||
3023 |
if (feof(f)) { |
|
3024 |
_INST(hitEOF) = true; |
|
3025 |
RETURN ( nil ); |
|
3026 |
} |
|
3027 |
||
3028 |
__READING__(f) |
|
3029 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3030 |
__PROTECT__(self); |
612 | 3031 |
__BEGIN_INTERRUPTABLE__ |
3032 |
while (1) { |
|
3033 |
do { |
|
3034 |
if (feof(f)) { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3035 |
__UNPROTECT__(self); |
612 | 3036 |
__END_INTERRUPTABLE__ |
3037 |
_INST(hitEOF) = true; |
|
3038 |
RETURN ( nil ); |
|
3039 |
} |
|
3040 |
c = getc(f); |
|
3041 |
} while ((c < 0) && (errno == EINTR)); |
|
3042 |
||
3043 |
switch (c) { |
|
3044 |
case ' ': |
|
3045 |
case '\t': |
|
3046 |
case '\n': |
|
3047 |
case '\r': |
|
3048 |
case '\b': |
|
3049 |
case '\014': |
|
3050 |
break; |
|
3051 |
||
3052 |
default: |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3053 |
__UNPROTECT__(self); |
612 | 3054 |
__END_INTERRUPTABLE__ |
3055 |
if (c < 0) { |
|
3056 |
_INST(hitEOF) = true; |
|
3057 |
if (ferror(f) && (errno != 0)) { |
|
3058 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3059 |
goto err; |
|
3060 |
} |
|
3061 |
RETURN ( nil ); |
|
3062 |
} |
|
3063 |
ungetc(c, f); |
|
3064 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
|
3065 |
} |
|
3066 |
} |
|
3067 |
} |
|
3068 |
} |
|
3069 |
err: ; |
|
3070 |
%}. |
|
3071 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3072 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3073 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3074 |
^ self errorBinary. |
|
3075 |
! |
|
3076 |
||
3077 |
skipSeparatorsExceptCR |
|
3078 |
"skip all whitespace but no newlines; |
|
3079 |
next will return next non-white-space character |
|
3080 |
or nil if endOfFile reached. Not allowed in binary mode. |
|
3081 |
- reimplemented for speed" |
|
3082 |
||
3083 |
%{ /* NOCONTEXT */ |
|
3084 |
||
3085 |
FILE *f; |
|
3086 |
int c; |
|
3087 |
OBJ fp; |
|
3088 |
||
3089 |
_INST(lastErrorNumber) = nil; |
|
3090 |
if (((fp = _INST(filePointer)) != nil) |
|
3091 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
3092 |
if (_INST(binary) != true) { |
|
3093 |
f = __FILEVal(fp); |
|
3094 |
__READING__(f) |
|
3095 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3096 |
__PROTECT__(self); |
612 | 3097 |
__BEGIN_INTERRUPTABLE__ |
3098 |
while (1) { |
|
3099 |
||
3100 |
if (feof(f)) { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3101 |
__UNPROTECT__(self); |
612 | 3102 |
__END_INTERRUPTABLE__ |
3103 |
_INST(hitEOF) = true; |
|
3104 |
RETURN ( nil ); |
|
3105 |
} |
|
3106 |
||
3107 |
do { |
|
3108 |
c = getc(f); |
|
3109 |
} while ((c < 0) && (errno == EINTR)); |
|
3110 |
||
3111 |
switch (c) { |
|
3112 |
case ' ': |
|
3113 |
case '\t': |
|
3114 |
case '\b': |
|
3115 |
break; |
|
3116 |
||
3117 |
default: |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3118 |
__UNPROTECT__(self); |
612 | 3119 |
__END_INTERRUPTABLE__ |
3120 |
if (c < 0) { |
|
3121 |
if (ferror(f) && (errno != 0)) { |
|
3122 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3123 |
goto err; |
|
3124 |
} |
|
3125 |
_INST(hitEOF) = true; |
|
3126 |
RETURN ( nil ); |
|
3127 |
} |
|
3128 |
ungetc(c, f); |
|
3129 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
|
3130 |
} |
|
3131 |
} |
|
3132 |
} |
|
3133 |
} |
|
3134 |
err: ; |
|
3135 |
%}. |
|
3136 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3137 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3138 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3139 |
^ self errorBinary |
|
3140 |
! |
|
3141 |
||
3142 |
skipThrough:aCharacter |
|
3143 |
"skip all characters up-to and including aCharacter. Return the receiver if |
|
671 | 3144 |
skip was successful, otherwise (i.e. if not found) return nil. |
612 | 3145 |
The next read operation will return the character after aCharacter. |
3146 |
The argument, aCharacter must be character, or integer when in binary mode. |
|
3147 |
Only single byte characters are currently supported." |
|
3148 |
||
3149 |
%{ /* NOCONTEXT */ |
|
3150 |
||
3151 |
FILE *f; |
|
3152 |
REGISTER int c, cSearch; |
|
3153 |
OBJ fp; |
|
3154 |
||
3155 |
_INST(lastErrorNumber) = nil; |
|
3156 |
if (((fp = _INST(filePointer)) != nil) |
|
3157 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
3158 |
if (_INST(binary) == true) { |
|
3159 |
/* searched for object must be a smallInteger */ |
|
3160 |
if (! __isSmallInteger(aCharacter)) goto badArgument; |
|
3161 |
cSearch = __intVal(aCharacter); |
|
3162 |
} else { |
|
3163 |
/* searched for object must be a character */ |
|
3164 |
if (! __isCharacter(aCharacter)) goto badArgument; |
|
3165 |
cSearch = __intVal(_characterVal(aCharacter)); |
|
3166 |
} |
|
3167 |
/* Q: should we just say: "not found" ? */ |
|
3168 |
if ((cSearch < 0) || (cSearch > 255)) goto badArgument; |
|
3169 |
||
3170 |
f = __FILEVal(fp); |
|
3171 |
__READING__(f) |
|
3172 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3173 |
__PROTECT__(self); |
612 | 3174 |
__BEGIN_INTERRUPTABLE__ |
3175 |
while (1) { |
|
3176 |
#ifdef NOTNEEDED |
|
3177 |
if (feof(f)) { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3178 |
__UNPROTECT__(self); |
612 | 3179 |
__END_INTERRUPTABLE__ |
3180 |
RETURN ( nil ); |
|
3181 |
} |
|
3182 |
#endif |
|
3183 |
do { |
|
3184 |
c = getc(f); |
|
3185 |
} while ((c < 0) && (errno == EINTR)); |
|
3186 |
||
3187 |
if (c == cSearch) { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3188 |
__UNPROTECT__(self); |
612 | 3189 |
__END_INTERRUPTABLE__ |
3190 |
RETURN (self); |
|
3191 |
} |
|
3192 |
if (c < 0) { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3193 |
__UNPROTECT__(self); |
612 | 3194 |
__END_INTERRUPTABLE__ |
3195 |
if (ferror(f) && (errno != 0)) { |
|
3196 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3197 |
break; |
|
3198 |
} else { |
|
3199 |
_INST(hitEOF) = true; |
|
3200 |
RETURN (nil); |
|
3201 |
} |
|
3202 |
} |
|
3203 |
} |
|
3204 |
} |
|
3205 |
badArgument: ; |
|
3206 |
%}. |
|
3207 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3208 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3209 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
3210 |
" |
|
3211 |
argument must be integer/character in binary mode, |
|
3212 |
character in text mode |
|
3213 |
" |
|
3214 |
^ self error:'invalid argument'. |
|
3215 |
||
3216 |
" |
|
3217 |
|s| |
|
3218 |
s := 'Makefile' asFilename readStream. |
|
3219 |
s skipThrough:$=. |
|
3220 |
s next:10 |
|
3221 |
" |
|
3222 |
! |
|
3223 |
||
3224 |
skipThroughAll:aString |
|
3225 |
"search & skip for the sequence given by the argument, aCollection; |
|
671 | 3226 |
return nil if not found, self otherwise. If successful, the next read |
612 | 3227 |
will return the character after the searchstring." |
3228 |
||
3229 |
|buffer len first| |
|
3230 |
||
3231 |
(aString isString and:[binary not]) ifTrue:[ |
|
3232 |
len := aString size. |
|
3233 |
first := aString at:1. |
|
3234 |
buffer := String new:len. |
|
3235 |
buffer at:1 put:first. |
|
3236 |
len := len - 1. |
|
3237 |
[true] whileTrue:[ |
|
3238 |
(self skipThrough:first) isNil ifTrue:[ |
|
3239 |
^ nil. |
|
3240 |
]. |
|
3241 |
(self nextBytes:len into:buffer startingAt:2) == len ifFalse:[ |
|
3242 |
^ nil |
|
3243 |
]. |
|
3244 |
buffer = aString ifTrue:[ |
|
3245 |
" |
|
3246 |
position back, before string |
|
3247 |
" |
|
3248 |
^ self |
|
3249 |
]. |
|
3250 |
]. |
|
3251 |
"NOT REACHED" |
|
3252 |
]. |
|
3253 |
^ super skipThroughAll:aString |
|
3254 |
||
3255 |
" |
|
3256 |
|s| |
|
3257 |
s := 'Makefile' asFilename readStream. |
|
3258 |
s skipThroughAll:'are'. |
|
3259 |
s next:10 |
|
3260 |
" |
|
3261 |
! |
|
3262 |
||
3263 |
skipToAll:aString |
|
3264 |
"skip for the sequence given by the argument, aCollection; |
|
3265 |
return nil if not found, self otherwise. On a successful match, next read |
|
3266 |
will return characters of aString." |
|
3267 |
||
3268 |
|oldPos| |
|
3269 |
||
3270 |
oldPos := self position. |
|
3271 |
(self skipThroughAll:aString) isNil ifTrue:[ |
|
3272 |
" |
|
3273 |
restore position |
|
3274 |
" |
|
3275 |
self position:oldPos. |
|
3276 |
^ nil |
|
3277 |
]. |
|
3278 |
" |
|
3279 |
position before match-string |
|
3280 |
" |
|
3281 |
self position:(self position - aString size). |
|
3282 |
^ self |
|
3283 |
||
3284 |
" |
|
3285 |
|s| |
|
3286 |
s := 'Makefile' asFilename readStream. |
|
3287 |
s skipToAll:'are'. |
|
3288 |
s next:10 |
|
3289 |
" |
|
1 | 3290 |
! ! |
612 | 3291 |
|
3292 |
!ExternalStream methodsFor:'testing'! |
|
3293 |
||
3294 |
atEnd |
|
3295 |
"return true, if position is at end" |
|
3296 |
||
3297 |
%{ /* NOCONTEXT */ |
|
3298 |
||
3299 |
FILE *f; |
|
3300 |
OBJ fp; |
|
3301 |
int c; |
|
3302 |
||
3303 |
if (_INST(hitEOF) == true) { |
|
3304 |
RETURN (true); |
|
3305 |
} |
|
3306 |
if (_INST(buffered) == false) { |
|
3307 |
RETURN (false); |
|
3308 |
} |
|
3309 |
||
3310 |
_INST(lastErrorNumber) = nil; |
|
3311 |
||
3312 |
if ((fp = _INST(filePointer)) != nil) { |
|
3313 |
f = __FILEVal(fp); |
|
3314 |
#ifdef OLD |
|
3315 |
RETURN ( feof(f) ? true : false ); |
|
3316 |
#else |
|
3317 |
__READING__(f) |
|
3318 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3319 |
__PROTECT__(self); |
612 | 3320 |
__BEGIN_INTERRUPTABLE__ |
3321 |
do { |
|
3322 |
c = getc(f); |
|
3323 |
} while ((c < 0) && (errno == EINTR) && (clearerr(f), 1)); |
|
3324 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3325 |
__UNPROTECT__(self); |
612 | 3326 |
|
3327 |
if (c != EOF) { |
|
3328 |
ungetc(c, f); |
|
3329 |
RETURN (false); |
|
3330 |
} |
|
3331 |
if (ferror(f) && (errno != 0)) { |
|
3332 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3333 |
} else { |
|
3334 |
_INST(hitEOF) = true; |
|
3335 |
RETURN (true); |
|
3336 |
} |
|
3337 |
#endif |
|
3338 |
} |
|
3339 |
%}. |
|
3340 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
3341 |
^ self errorNotOpen |
|
3342 |
! |
|
3343 |
||
3344 |
canReadWithoutBlocking |
|
3345 |
"return true, if any data is available for reading (i.e. |
|
3346 |
a read operation will not block the smalltalk process), false otherwise." |
|
3347 |
||
3348 |
|fd| |
|
3349 |
||
3350 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3351 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
3352 |
||
3353 |
fd := self fileDescriptor. |
|
3354 |
^ OperatingSystem readCheck:fd |
|
3355 |
||
3356 |
" |
|
3357 |
|pipe| |
|
3358 |
||
3359 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
3360 |
pipe canReadWithoutBlocking ifTrue:[ |
|
3361 |
Transcript showCr:'data available' |
|
3362 |
] ifFalse:[ |
|
3363 |
Transcript showCr:'no data available' |
|
3364 |
]. |
|
3365 |
pipe close |
|
3366 |
" |
|
3367 |
! |
|
3368 |
||
3369 |
canWriteWithoutBlocking |
|
3370 |
"return true, if data can be written into the stream |
|
3371 |
(i.e. a write operation will not block the smalltalk process)." |
|
3372 |
||
3373 |
|fd| |
|
3374 |
||
3375 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3376 |
mode == #readonly ifTrue:[^ self errorReadOnly]. |
|
3377 |
||
3378 |
fd := self fileDescriptor. |
|
3379 |
^ OperatingSystem writeCheck:fd |
|
3380 |
! ! |
|
3381 |
||
3382 |
!ExternalStream methodsFor:'waiting for I/O'! |
|
3383 |
||
3384 |
readWait |
|
3385 |
"suspend the current process, until the receiver |
|
3386 |
becomes ready for reading. If data is already available, |
|
3387 |
return immediate. |
|
3388 |
The other threads are not affected by the wait." |
|
3389 |
||
3390 |
self readWaitWithTimeoutMs:nil |
|
3391 |
! |
|
3392 |
||
3393 |
readWaitWithTimeout:timeout |
|
3394 |
"suspend the current process, until the receiver |
|
3395 |
becomes ready for reading or a timeout (in seconds) expired. |
|
3396 |
If data is already available, return immediate. |
|
3397 |
Return true if a timeout occured (i.e. false, if data is available). |
|
3398 |
The other threads are not affected by the wait." |
|
3399 |
||
3400 |
^ self readWaitWithTimeoutMs:timeout * 1000 |
|
3401 |
! |
|
3402 |
||
3403 |
readWaitWithTimeoutMs:timeout |
|
3404 |
"suspend the current process, until the receiver |
|
3405 |
becomes ready for reading or a timeout (in milliseconds) expired. |
|
3406 |
If data is already available, return immediate. |
|
3407 |
Return true if a timeout occured (i.e. false, if data is available). |
|
3408 |
The other threads are not affected by the wait." |
|
3409 |
||
3410 |
|fd inputSema hasData wasBlocked| |
|
3411 |
||
3412 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3413 |
mode == #writeonly ifTrue:[^ self errorWriteOnly]. |
|
3414 |
||
3415 |
fd := self fileDescriptor. |
|
3416 |
(OperatingSystem readCheck:fd) ifTrue:[^ false]. |
|
3417 |
||
3418 |
wasBlocked := OperatingSystem blockInterrupts. |
|
3419 |
hasData := OperatingSystem readCheck:fd. |
|
3420 |
hasData ifFalse:[ |
|
3421 |
inputSema := Semaphore new. |
|
3422 |
[ |
|
3423 |
timeout notNil ifTrue:[ |
|
3424 |
Processor signal:inputSema afterMilliseconds:timeout |
|
3425 |
]. |
|
3426 |
Processor signal:inputSema onInput:fd. |
|
3427 |
Processor activeProcess state:#ioWait. |
|
3428 |
inputSema wait. |
|
3429 |
Processor disableSemaphore:inputSema. |
|
3430 |
hasData := OperatingSystem readCheck:fd |
|
3431 |
] valueOnUnwindDo:[ |
|
3432 |
Processor disableSemaphore:inputSema. |
|
3433 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3434 |
] |
|
3435 |
]. |
|
3436 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3437 |
^ hasData not |
|
3438 |
! |
|
3439 |
||
3440 |
writeWait |
|
3441 |
"suspend the current process, until the receiver |
|
3442 |
becomes ready for writing. |
|
3443 |
Return immediate if the receiver is already ready. |
|
3444 |
The other threads are not affected by the wait." |
|
3445 |
||
3446 |
self writeWaitWithTimeoutMs:nil |
|
3447 |
! |
|
3448 |
||
3449 |
writeWaitWithTimeout:timeout |
|
3450 |
"suspend the current process, until the receiver |
|
3451 |
becomes ready for writing or a timeout (in seconds) expired. |
|
3452 |
Return true if a timeout occured (i.e. false, if data is available). |
|
3453 |
Return immediate if the receiver is already ready. |
|
3454 |
The other threads are not affected by the wait." |
|
3455 |
||
3456 |
^ self writeWaitWithTimeoutMs:timeout * 1000 |
|
3457 |
! |
|
3458 |
||
3459 |
writeWaitWithTimeoutMs:timeout |
|
3460 |
"suspend the current process, until the receiver |
|
3461 |
becomes ready for writing or a timeout (in seconds) expired. |
|
3462 |
Return true if a timeout occured (i.e. false, if data is available). |
|
3463 |
Return immediate if the receiver is already ready. |
|
3464 |
The other threads are not affected by the wait." |
|
3465 |
||
3466 |
|fd outputSema canWrite wasBlocked| |
|
3467 |
||
3468 |
filePointer isNil ifTrue:[ |
|
3469 |
^ self errorNotOpen |
|
3470 |
]. |
|
3471 |
mode == #readonly ifTrue:[ |
|
3472 |
^ self errorReadOnly |
|
3473 |
]. |
|
3474 |
||
3475 |
fd := self fileDescriptor. |
|
3476 |
(OperatingSystem writeCheck:fd) ifTrue:[^ false]. |
|
3477 |
||
3478 |
wasBlocked := OperatingSystem blockInterrupts. |
|
3479 |
canWrite := OperatingSystem writeCheck:fd. |
|
3480 |
canWrite ifFalse:[ |
|
3481 |
outputSema := Semaphore new. |
|
3482 |
[ |
|
3483 |
timeout notNil ifTrue:[ |
|
3484 |
Processor signal:outputSema afterMilliseconds:timeout |
|
3485 |
]. |
|
3486 |
Processor signal:outputSema onOutput:fd. |
|
3487 |
Processor activeProcess state:#ioWait. |
|
3488 |
outputSema wait. |
|
3489 |
Processor disableSemaphore:outputSema. |
|
3490 |
canWrite := OperatingSystem writeCheck:fd |
|
3491 |
] valueOnUnwindDo:[ |
|
3492 |
Processor disableSemaphore:outputSema. |
|
3493 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3494 |
] |
|
3495 |
]. |
|
3496 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3497 |
^ canWrite not |
|
3498 |
! ! |
|
3499 |
||
3500 |
!ExternalStream methodsFor:'writing'! |
|
3501 |
||
3502 |
commit |
|
3503 |
"write all buffered date - ignored if unbuffered" |
|
3504 |
||
3505 |
self synchronizeOutput |
|
3506 |
! |
|
3507 |
||
3508 |
cr |
|
3509 |
"append an end-of-line character (or CRLF if in crlf mode). |
|
3510 |
reimplemented for speed" |
|
3511 |
||
3512 |
%{ /* NOCONTEXT */ |
|
3513 |
||
3514 |
FILE *f; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3515 |
int len, cnt, _buffered; |
612 | 3516 |
OBJ fp; |
3517 |
char *cp; |
|
3518 |
||
3519 |
_INST(lastErrorNumber) = nil; |
|
3520 |
if (((fp = _INST(filePointer)) != nil) |
|
3521 |
&& (_INST(mode) != @symbol(readonly))) { |
|
3522 |
if (_INST(binary) != true) { |
|
3523 |
f = __FILEVal(fp); |
|
3524 |
||
3525 |
if (_INST(useCRLF) == true) { |
|
3526 |
cp = "\r\n"; len = 2; |
|
3527 |
} else { |
|
3528 |
cp = "\n"; len = 1; |
|
3529 |
} |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3530 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3531 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3532 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3533 |
__PROTECT__(self); |
612 | 3534 |
__BEGIN_INTERRUPTABLE__ |
3535 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3536 |
if (_buffered) { |
612 | 3537 |
__WRITING__(f) |
3538 |
cnt = fwrite(cp, 1, len, f); |
|
3539 |
} else { |
|
3540 |
do { |
|
3541 |
cnt = write(fileno(f), cp, len); |
|
3542 |
} while ((cnt < 0) && (errno == EINTR)); |
|
3543 |
} |
|
3544 |
||
3545 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3546 |
__UNPROTECT__(self); |
612 | 3547 |
|
3548 |
if (cnt == len) { |
|
3549 |
if (_INST(position) != nil) { |
|
3550 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len); |
|
3551 |
} |
|
3552 |
RETURN ( self ); |
|
3553 |
} |
|
3554 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3555 |
} |
|
3556 |
} |
|
3557 |
%}. |
|
3558 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3559 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3560 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3561 |
self errorBinary |
|
3562 |
! |
|
3563 |
||
3564 |
nextPut:aCharacter |
|
3565 |
"write the argument, aCharacter - return nil if failed, self if ok. |
|
3566 |
Only single-byte characters are currently supported" |
|
3567 |
||
3568 |
%{ /* NOCONTEXT */ |
|
3569 |
||
3570 |
FILE *f; |
|
3571 |
char c; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3572 |
int cnt, _buffered; |
612 | 3573 |
OBJ pos, fp; |
3574 |
||
3575 |
_INST(lastErrorNumber) = nil; |
|
3576 |
if (((fp = _INST(filePointer)) != nil) |
|
3577 |
&& (_INST(mode) != @symbol(readonly))) { |
|
3578 |
if (_INST(binary) != true) { |
|
3579 |
if (__isCharacter(aCharacter)) { |
|
3580 |
c = __intVal(__characterVal(aCharacter)) & 0xFF; |
|
3581 |
doWrite: |
|
3582 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3583 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3584 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3585 |
__PROTECT__(self); |
612 | 3586 |
__BEGIN_INTERRUPTABLE__ |
3587 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3588 |
if (_buffered) { |
612 | 3589 |
__WRITING__(f) |
3590 |
||
3591 |
do { |
|
3592 |
cnt = fwrite(&c, 1, 1, f); |
|
3593 |
} while ((cnt != 1) && (errno == EINTR)); |
|
3594 |
} else { |
|
3595 |
do { |
|
3596 |
cnt = write(fileno(f), &c, 1); |
|
3597 |
} while ((cnt != 1) && (errno == EINTR)); |
|
3598 |
} |
|
3599 |
||
3600 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3601 |
__UNPROTECT__(self); |
612 | 3602 |
|
3603 |
if (cnt == 1) { |
|
3604 |
pos = _INST(position); |
|
3605 |
if (pos != nil) { |
|
3606 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
3607 |
} |
|
3608 |
RETURN ( self ); |
|
3609 |
} |
|
3610 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3611 |
} |
|
3612 |
} else { |
|
3613 |
if (__isSmallInteger(aCharacter)) { |
|
3614 |
c = __intVal(aCharacter); |
|
3615 |
goto doWrite; |
|
3616 |
} |
|
3617 |
} |
|
3618 |
} |
|
3619 |
%}. |
|
3620 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3621 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3622 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3623 |
binary ifFalse:[^ self argumentMustBeCharacter]. |
|
3624 |
^ self argumentMustBeInteger. |
|
3625 |
! |
|
3626 |
||
3627 |
nextPutAll:aCollection |
|
3628 |
"write all elements of the argument, aCollection. |
|
3629 |
Reimplemented for speed when writing strings or byteArrays. |
|
3630 |
For others, falls back to general method in superclass." |
|
3631 |
||
3632 |
%{ /* NOCONTEXT */ |
|
3633 |
||
3634 |
FILE *f; |
|
3635 |
unsigned char *cp; |
|
3636 |
int len, cnt; |
|
3637 |
OBJ pos, fp; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3638 |
int offs, _buffered; |
612 | 3639 |
|
3640 |
_INST(lastErrorNumber) = nil; |
|
3641 |
||
3642 |
if (((fp = _INST(filePointer)) != nil) |
|
3643 |
&& (_INST(mode) != @symbol(readonly))) { |
|
3644 |
cp = NULL; |
|
3645 |
offs = 0; |
|
3646 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
3647 |
cp = _stringVal(aCollection); |
|
3648 |
len = _stringSize(aCollection); |
|
3649 |
} else { |
|
3650 |
if (_INST(binary) == true) { |
|
3651 |
if (__isByteArray(aCollection)) { |
|
3652 |
cp = __ByteArrayInstPtr(aCollection)->ba_element; |
|
3653 |
len = _byteArraySize(aCollection); |
|
3654 |
} else { |
|
3655 |
if (__isBytes(aCollection)) { |
|
3656 |
int nInst; |
|
3657 |
||
3658 |
cp = __ByteArrayInstPtr(aCollection)->ba_element; |
|
3659 |
len = _byteArraySize(aCollection); |
|
3660 |
nInst = __intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars); |
|
3661 |
offs = __OBJS2BYTES__(nInst); |
|
3662 |
cp += offs; |
|
3663 |
len -= offs; |
|
3664 |
} |
|
3665 |
} |
|
3666 |
} |
|
3667 |
} |
|
3668 |
if (cp != NULL) { |
|
3669 |
f = __FILEVal(fp); |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3670 |
_buffered = (_INST(buffered) == false); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3671 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3672 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3673 |
__PROTECT__(aCollection); |
612 | 3674 |
__BEGIN_INTERRUPTABLE__ |
3675 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3676 |
if (_buffered) { |
612 | 3677 |
int cc, rest; |
3678 |
||
3679 |
cnt = 0; |
|
849
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3680 |
#ifdef LINUX |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3681 |
rest = len; |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3682 |
do { |
872 | 3683 |
/* |
3684 |
* in linux, partial writes seem to be possible |
|
3685 |
* with ttys when interrupted ... |
|
849
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3686 |
* I do not like this |
872 | 3687 |
*/ |
849
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3688 |
do { |
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3689 |
__FETCHPROTECT__(aCollection, 1); |
872 | 3690 |
cp = __ByteArrayInstPtr(aCollection)->ba_element + offs; |
3691 |
cc = write(fileno(f), cp, rest); |
|
849
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3692 |
if (cc > 0) { |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3693 |
cnt += cc; |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3694 |
offs += cc; |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3695 |
rest -= cc; |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3696 |
} else { |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3697 |
if (cc < 0) { |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3698 |
cnt = cc; |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3699 |
} |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3700 |
} |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3701 |
} while ((cc >= 0) && rest); |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3702 |
} while ((cnt < 0) && (errno == EINTR)); |
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3703 |
|
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3704 |
#else |
612 | 3705 |
do { |
3706 |
/* |
|
3707 |
* refetch pointer in case of an interrupt |
|
3708 |
*/ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3709 |
__FETCHPROTECT__(aCollection, 1); |
612 | 3710 |
cp = __ByteArrayInstPtr(aCollection)->ba_element + offs; |
3711 |
cnt = write(fileno(f), cp, len); |
|
3712 |
} while ((cnt < 0) && (errno == EINTR)); |
|
849
033900f9b217
LINUX seems to interrupt writes & return a positive parial count
Claus Gittinger <cg@exept.de>
parents:
826
diff
changeset
|
3713 |
#endif |
612 | 3714 |
} else { |
3715 |
__WRITING__(f) |
|
3716 |
cnt = fwrite(cp, 1, len, f); |
|
3717 |
if (errno == EINTR) errno = 0; |
|
3718 |
} |
|
3719 |
||
3720 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3721 |
__UNPROTECT__(aCollection); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3722 |
__UNPROTECT__(self); |
612 | 3723 |
|
3724 |
if (cnt == len) { |
|
3725 |
pos = _INST(position); |
|
3726 |
if (pos != nil) { |
|
3727 |
_INST(position) = __MKSMALLINT(__intVal(pos) + len); |
|
3728 |
} |
|
3729 |
RETURN ( self ); |
|
3730 |
} |
|
3731 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3732 |
} |
|
3733 |
} |
|
3734 |
%}. |
|
3735 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3736 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
3737 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
3738 |
||
3739 |
^ super nextPutAll:aCollection |
|
3740 |
! |
|
3741 |
||
3742 |
nextPutAll:aCollection startingAt:start to:stop |
|
3743 |
"write a range of elements of the argument, aCollection. |
|
3744 |
Reimplemented for speed when writing strings or byteArrays. |
|
3745 |
For others, falls back to general method in superclass." |
|
3746 |
||
3747 |
%{ /* NOCONTEXT */ |
|
3748 |
||
3749 |
FILE *f; |
|
3750 |
unsigned char *cp; |
|
3751 |
int offs, len, cnt, index1, index2; |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3752 |
int _buffered; |
612 | 3753 |
OBJ fp; |
3754 |
||
3755 |
_INST(lastErrorNumber) = nil; |
|
3756 |
if (((fp = _INST(filePointer)) != nil) |
|
3757 |
&& (_INST(mode) != @symbol(readonly))) { |
|
3758 |
if (__bothSmallInteger(start, stop)) { |
|
3759 |
cp = NULL; |
|
3760 |
offs = 0; |
|
3761 |
if (_INST(binary) != true) { |
|
3762 |
if (__isString(aCollection) || __isSymbol(aCollection)) { |
|
3763 |
cp = _stringVal(aCollection); |
|
3764 |
len = _stringSize(aCollection); |
|
3765 |
} |
|
3766 |
} else { |
|
3767 |
if (__isByteArray(aCollection)) { |
|
3768 |
cp = __ByteArrayInstPtr(aCollection)->ba_element; |
|
3769 |
len = _byteArraySize(aCollection); |
|
3770 |
} else { |
|
3771 |
if (__isBytes(aCollection)) { |
|
3772 |
int nInst; |
|
3773 |
||
3774 |
cp = __ByteArrayInstPtr(aCollection)->ba_element; |
|
3775 |
len = _byteArraySize(aCollection); |
|
3776 |
nInst = __intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars); |
|
3777 |
offs = __OBJS2BYTES__(nInst); |
|
3778 |
cp += offs; |
|
3779 |
len -= offs; |
|
3780 |
} |
|
3781 |
} |
|
3782 |
} |
|
3783 |
||
3784 |
if (cp != NULL) { |
|
3785 |
f = __FILEVal(fp); |
|
3786 |
||
3787 |
index1 = __intVal(start); |
|
3788 |
index2 = __intVal(stop); |
|
3789 |
if ((index1 < 1) || (index2 > len) || (index2 < index1)) { |
|
3790 |
RETURN ( self ); |
|
3791 |
} |
|
3792 |
if (index2 > len) |
|
3793 |
index2 = len; |
|
3794 |
||
3795 |
len = index2 - index1 + 1; |
|
3796 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3797 |
_buffered = (_INST(buffered) == true); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3798 |
|
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3799 |
__PROTECT__(self); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3800 |
__PROTECT__(aCollection); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3801 |
|
612 | 3802 |
__BEGIN_INTERRUPTABLE__ |
3803 |
||
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3804 |
if (_buffered) { |
612 | 3805 |
__WRITING__(f) |
3806 |
cnt = fwrite(cp+index1-1, 1, len, f); |
|
3807 |
} else { |
|
3808 |
do { |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3809 |
__FETCHPROTECT__(aCollection, 1); |
612 | 3810 |
cp = __ByteArrayInstPtr(aCollection)->ba_element + offs; |
3811 |
cnt = write(fileno(f), cp+offs, len); |
|
3812 |
} while ((cnt < 0) && (errno == EINTR)); |
|
3813 |
} |
|
3814 |
||
3815 |
__END_INTERRUPTABLE__ |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3816 |
__PROTECT__(aCollection); |
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3817 |
__PROTECT__(self); |
612 | 3818 |
|
3819 |
if (cnt == len) { |
|
3820 |
if (_INST(position) != nil) { |
|
3821 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len); |
|
3822 |
} |
|
3823 |
RETURN ( self ); |
|
3824 |
} |
|
3825 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
3826 |
} |
|
3827 |
} |
|
3828 |
} |
|
3829 |
%}. |
|
3830 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
3831 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
3832 |
! |
|
3833 |
||
3834 |
synchronizeOutput |
|
3835 |
"write all buffered data - ignored if unbuffered" |
|
3836 |
||
3837 |
%{ /* NOCONTEXT */ |
|
3838 |
OBJ fp; |
|
3839 |
||
3840 |
_INST(lastErrorNumber) = nil; |
|
3841 |
if ((fp = _INST(filePointer)) != nil) { |
|
3842 |
if (_INST(mode) != @symbol(readonly)) { |
|
3843 |
if (_INST(buffered) == true) { |
|
3844 |
__BEGIN_INTERRUPTABLE__ |
|
3845 |
fflush( __FILEVal(fp) ); |
|
3846 |
__END_INTERRUPTABLE__ |
|
3847 |
} |
|
3848 |
} |
|
3849 |
} |
|
3850 |
%} |
|
3851 |
! ! |
|
3852 |
||
730 | 3853 |
!ExternalStream class methodsFor:'documentation'! |
3854 |
||
3855 |
version |
|
1066
cffbbdea7e01
care for GC while being interruptable
Claus Gittinger <cg@exept.de>
parents:
1048
diff
changeset
|
3856 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.90 1996-03-05 20:02:56 cg Exp $' |
730 | 3857 |
! ! |
612 | 3858 |
ExternalStream initialize! |