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