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