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