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