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