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