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