author | Claus Gittinger <cg@exept.de> |
Sun, 10 Dec 1995 01:33:10 +0100 | |
changeset 730 | 62643519bf68 |
parent 711 | b535f22402a1 |
child 779 | 0e41a665038a |
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 |
730 | 15 |
lastErrorNumber' |
612 | 16 |
classVariableNames:'Lobby LastErrorNumber ReadErrorSignal WriteErrorSignal |
730 | 17 |
InvalidReadSignal InvalidWriteSignal InvalidModeSignal |
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 |
" |
177 |
! ! |
|
178 |
||
1 | 179 |
!ExternalStream class methodsFor:'initialization'! |
180 |
||
181 |
initialize |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
182 |
OpenErrorSignal isNil ifTrue:[ |
255 | 183 |
OpenErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
184 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
185 |
OpenErrorSignal notifierString:'open error'. |
|
186 |
||
159 | 187 |
ReadErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
188 |
ReadErrorSignal nameClass:self message:#readErrorSignal. |
|
189 |
ReadErrorSignal notifierString:'read error'. |
|
190 |
||
191 |
WriteErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
|
192 |
WriteErrorSignal nameClass:self message:#writeErrorSignal. |
|
193 |
WriteErrorSignal notifierString:'write error'. |
|
194 |
||
195 |
InvalidReadSignal := ReadErrorSignal newSignalMayProceed:false. |
|
196 |
InvalidReadSignal nameClass:self message:#invalidReadSignal. |
|
583 | 197 |
InvalidReadSignal notifierString:'read error'. |
159 | 198 |
|
199 |
InvalidWriteSignal := WriteErrorSignal newSignalMayProceed:false. |
|
200 |
InvalidWriteSignal nameClass:self message:#invalidWriteSignal. |
|
201 |
InvalidWriteSignal notifierString:'write error'. |
|
202 |
||
203 |
InvalidModeSignal := StreamErrorSignal newSignalMayProceed:false. |
|
204 |
InvalidModeSignal nameClass:self message:#invalidModeSignal. |
|
205 |
InvalidModeSignal notifierString:'binary/text mode mismatch'. |
|
206 |
||
207 |
StreamNotOpenSignal := StreamErrorSignal newSignalMayProceed:false. |
|
208 |
StreamNotOpenSignal nameClass:self message:#streamNotOpenSignal. |
|
209 |
StreamNotOpenSignal notifierString:'stream is not open'. |
|
210 |
]. |
|
211 |
||
31 | 212 |
Lobby isNil ifTrue:[ |
159 | 213 |
Lobby := Registry new. |
214 |
||
215 |
"want to get informed when returning from snapshot" |
|
216 |
ObjectMemory addDependent:self |
|
1 | 217 |
] |
218 |
! |
|
219 |
||
220 |
reOpenFiles |
|
221 |
"reopen all files (if possible) after a snapShot load" |
|
222 |
||
301 | 223 |
Lobby do:[:aFileStream | |
159 | 224 |
aFileStream reOpen |
1 | 225 |
] |
226 |
! |
|
227 |
||
228 |
update:something |
|
229 |
"have to reopen files when returning from snapshot" |
|
230 |
||
231 |
something == #returnFromSnapshot ifTrue:[ |
|
159 | 232 |
self reOpenFiles |
1 | 233 |
] |
234 |
! ! |
|
235 |
||
612 | 236 |
!ExternalStream class methodsFor:'instance creation'! |
237 |
||
238 |
new |
|
239 |
|newStream| |
|
240 |
||
241 |
newStream := self basicNew. |
|
242 |
newStream text; buffered:true; useCRLF:false; clearEOF. |
|
243 |
^ newStream |
|
244 |
! ! |
|
245 |
||
345 | 246 |
!ExternalStream class methodsFor:'Signal constants'! |
159 | 247 |
|
612 | 248 |
invalidModeSignal |
249 |
"return the signal raised when doing text-I/O with a binary stream |
|
250 |
or binary-I/O with a text stream" |
|
251 |
||
252 |
^ InvalidModeSignal |
|
159 | 253 |
! |
254 |
||
255 |
invalidReadSignal |
|
256 |
"return the signal raised when reading from writeonly streams" |
|
257 |
||
258 |
^ InvalidReadSignal |
|
259 |
! |
|
260 |
||
261 |
invalidWriteSignal |
|
262 |
"return the signal raised when writing to readonly streams" |
|
263 |
||
264 |
^ InvalidWriteSignal |
|
265 |
! |
|
266 |
||
612 | 267 |
openErrorSignal |
268 |
"return the signal raised when a file open failed" |
|
269 |
||
270 |
^ OpenErrorSignal |
|
271 |
! |
|
272 |
||
273 |
readErrorSignal |
|
274 |
"return the signal raised on read errors" |
|
275 |
||
276 |
^ ReadErrorSignal |
|
159 | 277 |
! |
278 |
||
279 |
streamNotOpenSignal |
|
280 |
"return the signal raised on I/O with closed streams" |
|
281 |
||
282 |
^ StreamNotOpenSignal |
|
308 | 283 |
! |
284 |
||
612 | 285 |
writeErrorSignal |
286 |
"return the signal raised on write errors" |
|
287 |
||
288 |
^ WriteErrorSignal |
|
2 | 289 |
! ! |
290 |
||
92 | 291 |
!ExternalStream class methodsFor:'error handling'! |
292 |
||
293 |
lastErrorNumber |
|
159 | 294 |
"return the errno of the last error" |
92 | 295 |
|
296 |
^ LastErrorNumber |
|
159 | 297 |
|
298 |
" |
|
299 |
ExternalStream lastErrorNumber |
|
300 |
" |
|
92 | 301 |
! |
302 |
||
303 |
lastErrorString |
|
304 |
"return a message string describing the last error" |
|
305 |
||
306 |
^ OperatingSystem errorTextForNumber:LastErrorNumber |
|
307 |
||
308 |
" |
|
309 |
ExternalStream lastErrorString |
|
310 |
" |
|
311 |
! ! |
|
312 |
||
1 | 313 |
!ExternalStream methodsFor:'accessing'! |
314 |
||
612 | 315 |
binary |
316 |
"switch to binary mode - default is text" |
|
317 |
||
318 |
binary := true |
|
1 | 319 |
! |
320 |
||
321 |
buffered:aBoolean |
|
10 | 322 |
"turn buffering on or off - default is on" |
1 | 323 |
|
10 | 324 |
buffered := aBoolean |
1 | 325 |
! |
326 |
||
327 |
contents |
|
2 | 328 |
"return the contents of the file from the current position up-to |
329 |
the end. If the stream is in binary mode, a ByteArray containing |
|
330 |
the byte values is returned. |
|
331 |
In text-mode, a collection of strings, each representing one line, |
|
332 |
is returned." |
|
333 |
||
334 |
|text l chunks sizes chunk byteCount cnt bytes offset| |
|
335 |
||
336 |
binary ifTrue:[ |
|
159 | 337 |
"adding to a ByteArray produces quadratic time-space |
338 |
behavior - therefore we allocate chunks, and concatenate them |
|
339 |
at the end." |
|
340 |
||
341 |
chunks := OrderedCollection new. |
|
342 |
sizes := OrderedCollection new. |
|
343 |
byteCount := 0. |
|
344 |
[self atEnd] whileFalse:[ |
|
345 |
chunk := ByteArray uninitializedNew:4096. |
|
346 |
cnt := self nextBytes:(chunk size) into:chunk. |
|
347 |
cnt notNil ifTrue:[ |
|
348 |
chunks add:chunk. |
|
349 |
sizes add:cnt. |
|
350 |
byteCount := byteCount + cnt |
|
351 |
] |
|
352 |
]. |
|
353 |
||
354 |
"now, create one big ByteArray" |
|
355 |
bytes := ByteArray uninitializedNew:byteCount. |
|
356 |
offset := 1. |
|
357 |
1 to:chunks size do:[:index | |
|
358 |
chunk := chunks at:index. |
|
359 |
cnt := sizes at:index. |
|
360 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk. |
|
361 |
offset := offset + cnt |
|
362 |
]. |
|
363 |
^ bytes |
|
2 | 364 |
]. |
1 | 365 |
|
244 | 366 |
text := StringCollection new. |
1 | 367 |
[self atEnd] whileFalse:[ |
159 | 368 |
l := self nextLine. |
406 | 369 |
l isNil ifTrue:[ |
370 |
^ text |
|
371 |
]. |
|
372 |
text add:l |
|
1 | 373 |
]. |
374 |
^ text |
|
375 |
! |
|
376 |
||
612 | 377 |
contentsOfEntireFile |
378 |
"ST-80 compatibility" |
|
379 |
||
380 |
^ self contents |
|
2 | 381 |
! |
382 |
||
612 | 383 |
contentsSpecies |
384 |
"return the kind of object to be returned by sub-collection builders |
|
385 |
(such as upTo)" |
|
159 | 386 |
|
387 |
binary ifTrue:[ |
|
612 | 388 |
^ ByteArray |
159 | 389 |
]. |
612 | 390 |
^ String |
2 | 391 |
! |
392 |
||
612 | 393 |
fileDescriptor |
394 |
"return the fileDescriptor of the receiver - |
|
395 |
notice: this one returns the underlying OSs fileDescriptor - |
|
396 |
this may not be available on all platforms (i.e. non unix systems)." |
|
1 | 397 |
|
398 |
%{ /* NOCONTEXT */ |
|
399 |
||
400 |
FILE *f; |
|
612 | 401 |
OBJ fp; |
402 |
||
403 |
if ((fp = _INST(filePointer)) != nil) { |
|
404 |
f = __FILEVal(fp); |
|
405 |
RETURN ( __MKSMALLINT(fileno(f)) ); |
|
1 | 406 |
} |
612 | 407 |
%} |
408 |
. |
|
409 |
^ self errorNotOpen |
|
410 |
! |
|
411 |
||
412 |
filePointer |
|
413 |
"return the filePointer of the receiver - |
|
414 |
notice: for portability stdio is used; this means you will get |
|
415 |
a FILE * - not a fileDescriptor. |
|
416 |
(what you really get is a corresponding integer). |
|
417 |
You cannot do much with the returned value |
|
418 |
- except passing it to a primitive, for example." |
|
419 |
||
420 |
^ filePointer |
|
421 |
! |
|
422 |
||
423 |
readonly |
|
424 |
"set access mode to readonly" |
|
425 |
||
426 |
mode := #readonly |
|
427 |
! |
|
428 |
||
429 |
readwrite |
|
430 |
"set access mode to readwrite" |
|
431 |
||
432 |
mode := #readwrite |
|
433 |
! |
|
434 |
||
435 |
text |
|
436 |
"switch to text mode - default is text" |
|
437 |
||
438 |
binary := false |
|
439 |
! |
|
440 |
||
441 |
useCRLF:aBoolean |
|
442 |
"turn on or off CRLF sending (instead of LF only) - default is off" |
|
443 |
||
444 |
useCRLF := aBoolean |
|
1 | 445 |
! |
446 |
||
612 | 447 |
writeonly |
448 |
"set access mode to writeonly" |
|
449 |
||
450 |
mode := #writeonly |
|
451 |
! ! |
|
452 |
||
453 |
!ExternalStream methodsFor:'error handling'! |
|
454 |
||
455 |
argumentMustBeCharacter |
|
456 |
"report an error, that the argument must be a character" |
|
457 |
||
458 |
^ self error:'argument must be a character' |
|
459 |
! |
|
460 |
||
461 |
argumentMustBeInteger |
|
462 |
"report an error, that the argument must be an integer" |
|
463 |
||
464 |
^ self error:'argument must be an integer' |
|
77 | 465 |
! |
466 |
||
612 | 467 |
argumentMustBeString |
468 |
"report an error, that the argument must be a string" |
|
469 |
||
470 |
^ self error:'argument must be a string' |
|
471 |
! |
|
472 |
||
473 |
errorBinary |
|
474 |
"report an error, that the stream is in binary mode" |
|
475 |
||
476 |
^ InvalidModeSignal |
|
477 |
raiseRequestWith:self |
|
478 |
errorString:(self class name , ' is in binary mode') |
|
479 |
in:thisContext sender |
|
480 |
! |
|
481 |
||
482 |
errorNotBinary |
|
483 |
"report an error, that the stream is not in binary mode" |
|
484 |
||
485 |
^ InvalidModeSignal |
|
486 |
raiseRequestWith:self |
|
487 |
errorString:(self class name , ' is not in binary mode') |
|
488 |
in:thisContext sender |
|
1 | 489 |
! |
490 |
||
612 | 491 |
errorNotBuffered |
492 |
"report an error, that the stream is not in buffered mode" |
|
493 |
||
494 |
^ StreamErrorSignal |
|
495 |
raiseRequestWith:self |
|
496 |
errorString:(self class name , ' is unbuffered - operation not allowed') |
|
497 |
in:thisContext sender |
|
498 |
! |
|
499 |
||
500 |
errorNotOpen |
|
501 |
"report an error, that the stream has not been opened" |
|
502 |
||
503 |
^ StreamNotOpenSignal |
|
504 |
raiseRequestWith:self |
|
505 |
errorString:(self class name , ' not open') |
|
506 |
in:thisContext sender |
|
507 |
! |
|
508 |
||
509 |
errorOpen |
|
510 |
"report an error, that the stream is already opened" |
|
511 |
||
512 |
^ OpenErrorSignal |
|
513 |
raiseRequestWith:self |
|
514 |
errorString:(self class name , ' is already open') |
|
515 |
in:thisContext sender |
|
516 |
! |
|
517 |
||
518 |
errorReadOnly |
|
519 |
"report an error, that the stream is a readOnly stream" |
|
520 |
||
521 |
^ InvalidWriteSignal |
|
522 |
raiseRequestWith:self |
|
523 |
errorString:(self class name , ' is readonly') |
|
524 |
in:thisContext sender |
|
525 |
! |
|
526 |
||
527 |
errorWriteOnly |
|
528 |
"report an error, that the stream is a writeOnly stream" |
|
529 |
||
530 |
^ InvalidReadSignal |
|
531 |
raiseRequestWith:self |
|
532 |
errorString:(self class name , ' is writeonly') |
|
533 |
in:thisContext sender |
|
2 | 534 |
! |
535 |
||
612 | 536 |
ioError |
537 |
"report an error, that some I/O error occured" |
|
538 |
||
539 |
^ StreamErrorSignal |
|
540 |
raiseRequestWith:self |
|
541 |
errorString:('I/O error: ' , self lastErrorString) |
|
542 |
in:thisContext sender |
|
543 |
! |
|
544 |
||
545 |
lastErrorNumber |
|
546 |
"return the last error" |
|
547 |
||
548 |
^ lastErrorNumber |
|
549 |
! |
|
550 |
||
551 |
lastErrorString |
|
552 |
"return a message string describing the last error" |
|
553 |
||
554 |
(lastErrorNumber isNil or:[lastErrorNumber == 0]) ifTrue:[ |
|
555 |
^ 'I/O error' |
|
556 |
]. |
|
557 |
^ OperatingSystem errorTextForNumber:lastErrorNumber |
|
2 | 558 |
! |
559 |
||
612 | 560 |
openError |
561 |
"report an error, that the open failed" |
|
562 |
||
563 |
^ OpenErrorSignal |
|
564 |
raiseRequestWith:self |
|
565 |
errorString:('error on open: ' , self lastErrorString) |
|
566 |
in:thisContext sender |
|
567 |
! |
|
568 |
||
569 |
readError |
|
570 |
"report an error, that some read error occured" |
|
571 |
||
572 |
^ ReadErrorSignal |
|
573 |
raiseRequestWith:self |
|
574 |
errorString:('read error: ' , self lastErrorString) |
|
575 |
in:thisContext sender |
|
576 |
! |
|
577 |
||
578 |
writeError |
|
579 |
"report an error, that some write error occured" |
|
580 |
||
581 |
^ WriteErrorSignal |
|
582 |
raiseRequestWith:self |
|
583 |
errorString:('write error: ' , self lastErrorString) |
|
584 |
in:thisContext sender |
|
585 |
! ! |
|
586 |
||
587 |
!ExternalStream methodsFor:'instance release'! |
|
588 |
||
589 |
closeFile |
|
590 |
"low level close - may be redefined in subclasses" |
|
2 | 591 |
|
592 |
%{ /* NOCONTEXT */ |
|
593 |
||
216 | 594 |
OBJ fp; |
2 | 595 |
|
612 | 596 |
if ((fp = _INST(filePointer)) != nil) { |
597 |
_INST(filePointer) = nil; |
|
362 | 598 |
__BEGIN_INTERRUPTABLE__ |
612 | 599 |
fclose(__FILEVal(fp)); |
362 | 600 |
__END_INTERRUPTABLE__ |
10 | 601 |
} |
602 |
%} |
|
603 |
! |
|
604 |
||
612 | 605 |
disposed |
606 |
"some Stream has been collected - close the file if not already done" |
|
607 |
||
608 |
self closeFile |
|
609 |
! |
|
610 |
||
611 |
setFilePointer:anInteger |
|
612 |
filePointer := anInteger |
|
1 | 613 |
! |
614 |
||
612 | 615 |
shallowCopyForFinalization |
616 |
"return a copy for finalization-registration; |
|
617 |
since all we need at finalization time is the fileDescriptor, |
|
618 |
a cheaper copy is possible." |
|
619 |
||
620 |
^ self class basicNew setFilePointer:filePointer |
|
1 | 621 |
! |
622 |
||
612 | 623 |
shutDown |
624 |
"close the stream - added for protocol compatibility with PipeStream. |
|
625 |
see comment there" |
|
626 |
||
627 |
self closeFile |
|
1 | 628 |
! ! |
629 |
||
2 | 630 |
!ExternalStream methodsFor:'line reading/writing'! |
1 | 631 |
|
632 |
nextLine |
|
633 |
"read the next line (characters up to newline). |
|
634 |
Return a string containing those characters excluding the newline. |
|
635 |
If the previous-to-last character is a cr, this is also removed, |
|
68 | 636 |
so its possible to read alien (i.e. ms-dos) text as well. |
637 |
The line must be shorter than 1K characters - otherwise its truncated." |
|
1 | 638 |
|
12 | 639 |
%{ /* STACK:2000 */ |
1 | 640 |
|
641 |
FILE *f; |
|
642 |
int len; |
|
12 | 643 |
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
|
644 |
char *rslt, *nextPtr, *limit; |
22 | 645 |
int fd, ch; |
92 | 646 |
int _buffered; |
216 | 647 |
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
|
648 |
OBJ __MKSTRING_L(); |
1 | 649 |
|
159 | 650 |
_INST(lastErrorNumber) = nil; |
216 | 651 |
if (((fp = _INST(filePointer)) != nil) |
159 | 652 |
&& (_INST(mode) != @symbol(writeonly))) { |
653 |
if (_INST(binary) != true) { |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
654 |
f = __FILEVal(fp); |
362 | 655 |
__BEGIN_INTERRUPTABLE__ |
159 | 656 |
buffer[0] = 0; |
657 |
||
658 |
_buffered = (_INST(buffered) == true); |
|
249 | 659 |
if (_buffered) { |
255 | 660 |
__READING__(f); |
249 | 661 |
} else { |
255 | 662 |
fd = fileno(f); |
249 | 663 |
} |
159 | 664 |
|
665 |
/* |
|
666 |
* mhmh - the following code looks ok to me, |
|
667 |
* but seems not to work for sockets |
|
668 |
*/ |
|
22 | 669 |
#ifdef DOES_NOT_WORK |
159 | 670 |
if (_INST(mode) == _readwrite) |
671 |
fseek(f, 0L, 1); /* needed in stdio */ |
|
672 |
do { |
|
673 |
rslt = fgets(buffer, sizeof(buffer), f); |
|
674 |
} while ((rslt == NULL) && (errno == EINTR)); |
|
22 | 675 |
#else |
249 | 676 |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
677 |
rslt = nextPtr = buffer; |
370 | 678 |
limit = buffer + sizeof(buffer) - 2; |
679 |
||
159 | 680 |
for (;;) { |
681 |
if (_buffered) { |
|
379 | 682 |
errno = 0; |
683 |
do { |
|
441 | 684 |
if (feof(f)) { |
685 |
ch = EOF; |
|
686 |
break; |
|
687 |
} |
|
379 | 688 |
ch = getc(f); |
689 |
} 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
|
690 |
|
159 | 691 |
if (ch == EOF) { |
692 |
if (ferror(f)) { |
|
693 |
if (errno == EINTR) { |
|
694 |
clearerr(f); |
|
440 | 695 |
if (! feof(f)) { |
696 |
continue; |
|
697 |
} |
|
159 | 698 |
} |
359 | 699 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 700 |
} |
701 |
len = 0; |
|
702 |
} else { |
|
703 |
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
|
704 |
*nextPtr = ch; |
159 | 705 |
} |
706 |
} else { |
|
707 |
do { |
|
447 | 708 |
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
|
709 |
len = read(fd, nextPtr, 1); |
159 | 710 |
} while ((len < 0) && (errno == EINTR)); |
711 |
} |
|
712 |
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
|
713 |
if (nextPtr == buffer) { |
159 | 714 |
rslt = NULL; |
715 |
} else { |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
716 |
*nextPtr = '\0'; |
159 | 717 |
} |
718 |
break; |
|
719 |
} |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
720 |
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
|
721 |
*nextPtr = '\0'; |
159 | 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 |
nextPtr++; |
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
725 |
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
|
726 |
*nextPtr = '\0'; |
159 | 727 |
break; |
728 |
} |
|
729 |
} |
|
12 | 730 |
#endif |
362 | 731 |
__END_INTERRUPTABLE__ |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
732 |
|
159 | 733 |
if (rslt != NULL) { |
370 | 734 |
/* |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
735 |
* that strlen can be avoided and replaced by (nextPtr - buffer) |
370 | 736 |
*/ |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
737 |
/* 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
|
738 |
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
|
739 |
|
159 | 740 |
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
|
741 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len); |
159 | 742 |
} |
743 |
/* 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
|
744 |
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
|
745 |
if (buffer[len-1] == '\n') { |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
521
diff
changeset
|
746 |
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
|
747 |
} |
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) && (buffer[len-1] == '\r')) { |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
521
diff
changeset
|
749 |
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
|
750 |
} |
159 | 751 |
} |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
481
diff
changeset
|
752 |
RETURN ( __MKSTRING_L(buffer, len COMMA_CON) ); |
159 | 753 |
} |
216 | 754 |
if (ferror(f) && (errno != 0)) { |
359 | 755 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 756 |
} else { |
757 |
_INST(hitEOF) = true; |
|
758 |
RETURN ( nil ); |
|
759 |
} |
|
760 |
} |
|
1 | 761 |
} |
159 | 762 |
%}. |
763 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2 | 764 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
1 | 765 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
2 | 766 |
self errorBinary |
1 | 767 |
! |
768 |
||
769 |
nextPutLine:aString |
|
770 |
"write the characters in aString and append a newline" |
|
771 |
||
772 |
%{ /* NOCONTEXT */ |
|
773 |
||
774 |
FILE *f; |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
775 |
int len, cnt, len1; |
216 | 776 |
OBJ pos, fp; |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
777 |
char *cp; |
1 | 778 |
|
159 | 779 |
_INST(lastErrorNumber) = nil; |
216 | 780 |
if (((fp = _INST(filePointer)) != nil) |
159 | 781 |
&& (_INST(mode) != @symbol(readonly))) { |
782 |
if (_INST(binary) != true) { |
|
783 |
if (__isString(aString)) { |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
784 |
f = __FILEVal(fp); |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
785 |
cp = (char *) _stringVal(aString); |
159 | 786 |
len = _stringSize(aString); |
787 |
||
362 | 788 |
__BEGIN_INTERRUPTABLE__ |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
789 |
|
561
e1059b1aa138
allow 0 return (at eof) without error
Claus Gittinger <cg@exept.de>
parents:
537
diff
changeset
|
790 |
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
|
791 |
__WRITING__(f) |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
792 |
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
|
793 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
794 |
do { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
795 |
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
|
796 |
} 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
|
797 |
} |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
798 |
|
159 | 799 |
if (cnt == len) { |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
800 |
len1 = len; |
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 |
if (_INST(useCRLF) == true) { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
803 |
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
|
804 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
805 |
cp = "\n"; len = 1; |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
806 |
} |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
807 |
if (_INST(buffered) == true) { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
808 |
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
|
809 |
} else { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
810 |
do { |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
811 |
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
|
812 |
} 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
|
813 |
} |
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
814 |
|
454
344ccfa720d9
Add useCRLF handling in nextPutLine:
Stefan Vogel <sv@exept.de>
parents:
447
diff
changeset
|
815 |
if (cnt > 0) { |
159 | 816 |
pos = _INST(position); |
817 |
if (pos != nil) { |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
818 |
_INST(position) = __MKSMALLINT(__intVal(pos)+len1+cnt); |
159 | 819 |
} |
362 | 820 |
__END_INTERRUPTABLE__ |
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
821 |
|
159 | 822 |
RETURN ( self ); |
823 |
} |
|
824 |
} |
|
537
dc4dad8a5ddd
many ExtStream rewrites - has to be reevaluated in all platforms
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
825 |
|
362 | 826 |
__END_INTERRUPTABLE__ |
359 | 827 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 828 |
} |
829 |
} |
|
1 | 830 |
} |
159 | 831 |
%}. |
832 |
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
|
833 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
834 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
835 |
binary ifTrue:[^ self errorBinary]. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
836 |
|
68 | 837 |
super nextPutAll:aString. |
838 |
self cr. |
|
1 | 839 |
! |
840 |
||
841 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil |
|
68 | 842 |
"read from aStream up to and including a line starting with aStringOrNil |
22 | 843 |
and append it to self. |
159 | 844 |
Can be used to copy/create large files or copy from a pipe/socket. |
68 | 845 |
|
22 | 846 |
If aStringOrNil is nil or not matched, copy preceeds to the end. |
847 |
(this allows for example to read a Socket and transfer the data quickly |
|
848 |
into a file - without creating zillions of temporary strings)" |
|
1 | 849 |
|
159 | 850 |
|srcFilePointer readError| |
1 | 851 |
|
852 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
853 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
854 |
srcFilePointer := aStream filePointer. |
|
855 |
srcFilePointer isNil ifTrue:[^ aStream errorNotOpen]. |
|
12 | 856 |
|
857 |
%{ /* STACK:2000 */ |
|
22 | 858 |
|
1 | 859 |
FILE *dst, *src; |
860 |
char *matchString; |
|
861 |
int matchLen = 0; |
|
12 | 862 |
char buffer[1024]; |
1 | 863 |
|
159 | 864 |
_INST(lastErrorNumber) = nil; |
249 | 865 |
if (__isSmallInteger(srcFilePointer)) { |
159 | 866 |
if ((aStringOrNil == nil) || __isString(aStringOrNil)) { |
867 |
if (aStringOrNil != nil) { |
|
868 |
matchString = (char *) _stringVal(aStringOrNil); |
|
869 |
matchLen = _stringSize(aStringOrNil); |
|
870 |
} |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
871 |
dst = __FILEVal(_INST(filePointer)); |
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
872 |
src = __FILEVal(srcFilePointer); |
362 | 873 |
__BEGIN_INTERRUPTABLE__ |
159 | 874 |
errno = 0; |
875 |
||
216 | 876 |
__WRITING__(dst) |
159 | 877 |
|
878 |
for (;;) { |
|
370 | 879 |
if (fgets(buffer, sizeof(buffer)-1, src) == NULL) { |
159 | 880 |
if (ferror(src)) { |
359 | 881 |
readError = __MKSMALLINT(errno); |
362 | 882 |
__END_INTERRUPTABLE__ |
159 | 883 |
goto err; |
884 |
} |
|
885 |
break; |
|
886 |
} |
|
887 |
if (fputs(buffer, dst) == EOF) { |
|
888 |
if (ferror(dst)) { |
|
359 | 889 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 890 |
__END_INTERRUPTABLE__ |
159 | 891 |
goto err; |
892 |
} |
|
893 |
break; |
|
894 |
} |
|
12 | 895 |
#ifndef OLD |
159 | 896 |
if (_INST(buffered) == false) { |
897 |
fflush(dst); |
|
898 |
} |
|
12 | 899 |
#endif |
159 | 900 |
if (matchLen) { |
901 |
if (strncmp(matchString, buffer, matchLen) == 0) |
|
902 |
break; |
|
903 |
} |
|
904 |
} |
|
362 | 905 |
__END_INTERRUPTABLE__ |
159 | 906 |
_INST(position) = nil; |
907 |
RETURN (self); |
|
908 |
} |
|
1 | 909 |
} |
159 | 910 |
err: ; |
911 |
%}. |
|
912 |
readError ifTrue:[ |
|
913 |
aStream setLastErrorNumber:readError. |
|
914 |
^ aStream readError |
|
915 |
]. |
|
916 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
10 | 917 |
buffered ifFalse:[^ self errorNotBuffered]. |
159 | 918 |
" |
919 |
argument error |
|
920 |
" |
|
1 | 921 |
^ self primitiveFailed |
922 |
! |
|
923 |
||
924 |
peekForLineStartingWith:aString |
|
925 |
"read ahead for next line starting with aString; |
|
159 | 926 |
return the line-string if found, or nil if EOF is encountered. |
927 |
If matched, not advance position behond that line |
|
928 |
i.e. nextLine will read the matched line. |
|
929 |
If not matched, reposition to original position for firther reading." |
|
1 | 930 |
|
931 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
932 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 933 |
binary ifTrue:[^ self errorBinary]. |
12 | 934 |
|
935 |
%{ /* STACK: 2000 */ |
|
1 | 936 |
FILE *f; |
937 |
int l; |
|
12 | 938 |
char buffer[1024]; |
1 | 939 |
char *cp; |
940 |
char *matchString; |
|
92 | 941 |
int firstpos = -1, lastpos; |
1 | 942 |
|
159 | 943 |
_INST(lastErrorNumber) = nil; |
54 | 944 |
if (__isString(aString)) { |
159 | 945 |
matchString = (char *) _stringVal(aString); |
946 |
l = _stringSize(aString); |
|
947 |
||
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
948 |
f = __FILEVal(_INST(filePointer)); |
216 | 949 |
__READING__(f) |
159 | 950 |
|
951 |
for (;;) { |
|
952 |
lastpos = ftell(f); |
|
953 |
if (firstpos == -1) firstpos = lastpos; |
|
954 |
||
362 | 955 |
__BEGIN_INTERRUPTABLE__ |
159 | 956 |
do { |
370 | 957 |
cp = fgets(buffer, sizeof(buffer)-1, f); |
159 | 958 |
} while ((cp == NULL) && (errno == EINTR)); |
370 | 959 |
buffer[sizeof(buffer)-1] = '\0'; |
362 | 960 |
__END_INTERRUPTABLE__ |
159 | 961 |
|
962 |
if (cp == NULL) { |
|
963 |
if (ferror(f)) { |
|
359 | 964 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 965 |
goto err; |
966 |
} else { |
|
967 |
fseek(f, firstpos, 0); |
|
968 |
RETURN (nil); |
|
969 |
} |
|
970 |
} |
|
971 |
if (strncmp(cp, matchString, l) == 0) { |
|
972 |
fseek(f, lastpos, 0); |
|
973 |
break; |
|
974 |
} |
|
975 |
} |
|
976 |
/* remove EOL character */ |
|
977 |
cp = buffer; |
|
978 |
while (*cp && (*cp != '\n')) cp++; |
|
979 |
*cp = '\0'; |
|
980 |
RETURN ( _MKSTRING(buffer COMMA_CON) ); |
|
1 | 981 |
} |
159 | 982 |
err: ; |
983 |
%}. |
|
984 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
985 |
^ self argumentMustBeString |
|
1 | 986 |
! |
987 |
||
988 |
peekForLineStartingWithAny:aCollectionOfStrings |
|
989 |
"read ahead for next line starting with any of aCollectionOfStrings; |
|
990 |
return the index in aCollection if found, nil otherwise.. |
|
991 |
If no match, do not change position; otherwise advance right before the |
|
992 |
matched line so that nextLine will return this line." |
|
993 |
||
994 |
|line startPos linePos index| |
|
995 |
||
996 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
997 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2 | 998 |
binary ifTrue:[^ self errorBinary]. |
999 |
||
1 | 1000 |
startPos := self position. |
1001 |
[self atEnd] whileFalse:[ |
|
159 | 1002 |
linePos := self position. |
1003 |
line := self nextLine. |
|
1004 |
line notNil ifTrue:[ |
|
1005 |
index := 1. |
|
1006 |
aCollectionOfStrings do:[:prefix | |
|
1007 |
(line startsWith:prefix) ifTrue:[ |
|
1008 |
self position:linePos. |
|
1009 |
^ index |
|
1010 |
]. |
|
1011 |
index := index + 1 |
|
1012 |
] |
|
1013 |
] |
|
1 | 1014 |
]. |
1015 |
self position:startPos. |
|
1016 |
^ nil |
|
1017 |
! ! |
|
1018 |
||
612 | 1019 |
!ExternalStream methodsFor:'misc functions'! |
1020 |
||
1021 |
async:aBoolean |
|
1022 |
"set/clear the async attribute - if set the availability of data on |
|
1023 |
the receiver will trigger an ioInterrupt. |
|
1024 |
If cleared (which is the default) no special notification is made." |
|
92 | 1025 |
|
1026 |
|fd| |
|
1027 |
||
159 | 1028 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
92 | 1029 |
fd := self fileDescriptor. |
612 | 1030 |
aBoolean ifTrue:[ |
1031 |
^ OperatingSystem enableIOInterruptsOn:fd |
|
1032 |
]. |
|
1033 |
^ OperatingSystem disableIOInterruptsOn:fd |
|
92 | 1034 |
! |
1035 |
||
612 | 1036 |
backStep |
1037 |
"step back one element - |
|
1038 |
redefined, since position is redefined here" |
|
1039 |
||
1040 |
self position:(self position - 1) |
|
362 | 1041 |
! |
1042 |
||
612 | 1043 |
blocking:aBoolean |
1044 |
"set/clear the blocking attribute - if set (which is the default) |
|
1045 |
a read (using next) on the receiver will block until data is available. |
|
1046 |
If cleared, a read operation will immediately return with a value of |
|
1047 |
nil." |
|
92 | 1048 |
|
159 | 1049 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
612 | 1050 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor) |
1051 |
! |
|
1052 |
||
1053 |
close |
|
1054 |
"close the stream - tell operating system" |
|
1055 |
||
1056 |
filePointer isNil ifTrue:[^ self]. |
|
1057 |
Lobby unregister:self. |
|
1058 |
self closeFile. |
|
1059 |
filePointer := nil |
|
1060 |
! |
|
1061 |
||
1062 |
create |
|
1063 |
"create the stream |
|
1064 |
- this must be redefined in subclass" |
|
1065 |
||
1066 |
^ self subclassResponsibility |
|
1067 |
! |
|
1068 |
||
1069 |
ioctl:ioctlNumber |
|
1070 |
"to provide a simple ioctl facility - an ioctl is performed |
|
1071 |
on the underlying file; no arguments are passed." |
|
1072 |
||
1073 |
%{ /* NOCONTEXT */ |
|
1074 |
||
1075 |
FILE *f; |
|
1076 |
int ret, ioNum, ioArg; |
|
1077 |
OBJ fp; |
|
1078 |
||
1079 |
_INST(lastErrorNumber) = nil; |
|
1080 |
if ((fp = _INST(filePointer)) != nil) { |
|
1081 |
if (__isSmallInteger(ioctlNumber)) { |
|
1082 |
ioNum = __intVal(ioctlNumber); |
|
1083 |
f = __FILEVal(fp); |
|
1084 |
||
1085 |
__BEGIN_INTERRUPTABLE__ |
|
1086 |
do { |
|
1087 |
ret = ioctl(fileno(f), ioNum); |
|
1088 |
} while ((ret < 0) && (errno == EINTR)); |
|
1089 |
__END_INTERRUPTABLE__ |
|
1090 |
||
1091 |
if (ret >= 0) { |
|
1092 |
RETURN ( __MKSMALLINT(ret) ); |
|
1093 |
} |
|
1094 |
_INST(position) = nil; |
|
1095 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1096 |
} |
|
1097 |
} |
|
1098 |
%}. |
|
1099 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1100 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1101 |
" |
|
1102 |
ioctl-number is not an integer |
|
1103 |
" |
|
1104 |
^ self primitiveFailed |
|
1105 |
! |
|
1106 |
||
1107 |
ioctl:ioctlNumber with:arg |
|
1108 |
"to provide a simple ioctl facility - an ioctl is performed |
|
1109 |
on the underlying file; the argument is passed as argument. |
|
1110 |
If the argument is a number, its directly passed; if its a |
|
1111 |
kind of ByteArray (ByteArray, String or Structure) a pointer to |
|
1112 |
the data is passed. This allows performing most ioctls - however, |
|
1113 |
it might be tricky to setup the buffer." |
|
1114 |
||
1115 |
%{ /* NOCONTEXT */ |
|
1116 |
FILE *f; |
|
1117 |
int ret, ioNum; |
|
1118 |
OBJ fp; |
|
1119 |
||
1120 |
_INST(lastErrorNumber) = nil; |
|
1121 |
if ((fp = _INST(filePointer)) != nil) { |
|
1122 |
if (__isSmallInteger(ioctlNumber) |
|
1123 |
&& (__isSmallInteger(arg) || __isBytes(arg))) { |
|
1124 |
f = __FILEVal(fp); |
|
1125 |
ioNum = __intVal(ioctlNumber); |
|
1126 |
||
1127 |
__BEGIN_INTERRUPTABLE__ |
|
1128 |
do { |
|
1129 |
if (__isSmallInteger(arg)) { |
|
1130 |
ret = ioctl(fileno(f), ioNum, __intVal(arg)); |
|
1131 |
} else { |
|
1132 |
ret = ioctl(fileno(f), ioNum, __ByteArrayInstPtr(arg)->ba_element); |
|
1133 |
} |
|
1134 |
} while ((ret < 0) && (errno == EINTR)); |
|
1135 |
__END_INTERRUPTABLE__ |
|
1136 |
||
1137 |
if (ret >= 0) { |
|
1138 |
RETURN ( __MKSMALLINT(ret) ); |
|
1139 |
} |
|
1140 |
_INST(position) = nil; |
|
1141 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1142 |
} |
|
1143 |
} |
|
1144 |
%}. |
|
1145 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1146 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1147 |
" |
|
1148 |
ioctl-number is not an integer or argument is not byteArray-like |
|
1149 |
" |
|
1150 |
^ self primitiveFailed |
|
1151 |
! |
|
1152 |
||
1153 |
open |
|
1154 |
"open the stream |
|
1155 |
- this must be redefined in subclass" |
|
1156 |
||
1157 |
^ self subclassResponsibility |
|
1158 |
! |
|
1159 |
||
1160 |
position |
|
1161 |
"return the position |
|
1162 |
- this must be redefined in subclass" |
|
1163 |
||
1164 |
^ self subclassResponsibility |
|
1165 |
! |
|
1166 |
||
1167 |
position:anInteger |
|
1168 |
"set the position |
|
1169 |
- this must be redefined in subclass" |
|
1170 |
||
1171 |
^ self subclassResponsibility |
|
1172 |
! |
|
1173 |
||
1174 |
reset |
|
1175 |
"set the read position to the beginning of the collection" |
|
1176 |
||
1177 |
self position:"0" 1 |
|
1178 |
! |
|
1179 |
||
1180 |
setToEnd |
|
1181 |
"redefined since it must be implemented differently" |
|
1182 |
||
1183 |
^ self subclassResponsibility |
|
1184 |
! ! |
|
1185 |
||
1186 |
!ExternalStream methodsFor:'non homogenous reading'! |
|
1187 |
||
1188 |
nextByte |
|
1189 |
"read the next byte and return it as an Integer; return nil on error. |
|
1190 |
This is allowed in both text and binary modes, always returning the |
|
1191 |
bytes binary value as an integer in 0..255." |
|
1192 |
||
1193 |
%{ /* NOCONTEXT */ |
|
1194 |
||
1195 |
FILE *f; |
|
1196 |
unsigned char byte; |
|
1197 |
int cnt; |
|
1198 |
OBJ fp; |
|
1199 |
||
1200 |
_INST(lastErrorNumber) = nil; |
|
1201 |
if (((fp = _INST(filePointer)) != nil) |
|
1202 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1203 |
f = __FILEVal(fp); |
|
1204 |
||
1205 |
if (_INST(buffered) == true) { |
|
1206 |
__READING__(f) |
|
1207 |
} |
|
1208 |
||
1209 |
__BEGIN_INTERRUPTABLE__ |
|
1210 |
do { |
|
1211 |
if (_INST(buffered) == false) { |
|
1212 |
cnt = read(fileno(f), &byte, 1); |
|
1213 |
} else { |
|
1214 |
cnt = fread(&byte, 1, 1, f); |
|
1215 |
} |
|
1216 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1217 |
__END_INTERRUPTABLE__ |
|
1218 |
||
1219 |
if (cnt == 1) { |
|
1220 |
if (_INST(position) != nil) |
|
1221 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 1); |
|
1222 |
RETURN ( __MKSMALLINT(byte) ); |
|
1223 |
} |
|
1224 |
if (cnt == 0) { |
|
1225 |
_INST(hitEOF) = true; |
|
1226 |
/* |
|
1227 |
if (errno == EWOULDBLOCK) { |
|
1228 |
RETURN (nil); |
|
1229 |
} |
|
1230 |
*/ |
|
1231 |
} |
|
1232 |
_INST(position) = nil; |
|
1233 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1234 |
} |
|
1235 |
%}. |
|
1236 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
1237 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1238 |
^ self errorWriteOnly |
|
1239 |
! |
|
1240 |
||
1241 |
nextBytes:count into:anObject |
|
1242 |
"read the next count bytes into an object and return the number of |
|
1243 |
bytes read. On EOF, 0 is returned. |
|
1244 |
If the receiver is some socket/pipe-like stream, an exception |
|
1245 |
is raised if the connection is broken. |
|
1246 |
||
1247 |
The object must have non-pointer indexed instvars (i.e. it must be |
|
1248 |
a ByteArray, String, Float- or DoubleArray). |
|
1249 |
If anObject is a string or byteArray and reused, this provides the |
|
1250 |
fastest possible physical I/O (since no new objects are allocated). |
|
1251 |
||
1252 |
Use with care - non object oriented i/o. |
|
1253 |
Warning: in general, you cannot use this method to pass data from other |
|
1254 |
architectures since it does not care for byte order or float representation." |
|
1255 |
||
1256 |
^ self nextBytes:count into:anObject startingAt:1 |
|
10 | 1257 |
! |
1258 |
||
612 | 1259 |
nextBytes:count into:anObject startingAt:start |
1260 |
"read the next count bytes into an object and return the number of |
|
1261 |
bytes read or 0 on EOF. |
|
1262 |
If the receiver is some socket/pipe-like stream, an exception |
|
1263 |
is raised if the connection is broken. |
|
1264 |
Notice, that in contrast to other methods |
|
1265 |
here, this does NOT return nil on EOF, but the actual count. |
|
1266 |
Thus allowing read of partial blocks. |
|
1267 |
||
1268 |
The object must have non-pointer indexed instvars |
|
1269 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
1270 |
If anObject is a string or byteArray and reused, this provides the |
|
1271 |
fastest possible physical I/O (since no new objects are allocated). |
|
1272 |
||
1273 |
Use with care - non object oriented I/O. |
|
1274 |
Warning: in general, you cannot use this method to pass data from other |
|
1275 |
architectures since it does not care for byte order or float representation." |
|
1276 |
||
1277 |
%{ |
|
1278 |
FILE *f; |
|
1279 |
int cnt, offs, ret; |
|
1280 |
int objSize, nInstVars, nInstBytes; |
|
1281 |
char *cp; |
|
1282 |
OBJ pos, fp, oClass; |
|
1283 |
||
1284 |
_INST(lastErrorNumber) = nil; |
|
1285 |
if (((fp = _INST(filePointer)) != nil) |
|
1286 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1287 |
if (__bothSmallInteger(count, start)) { |
|
1288 |
f = __FILEVal(fp); |
|
1289 |
||
1290 |
oClass = __Class(anObject); |
|
1291 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1292 |
case BYTEARRAY: |
|
1293 |
case WORDARRAY: |
|
1294 |
case LONGARRAY: |
|
1295 |
case FLOATARRAY: |
|
1296 |
case DOUBLEARRAY: |
|
1297 |
break; |
|
1298 |
default: |
|
1299 |
goto bad; |
|
1300 |
} |
|
1301 |
cnt = __intVal(count); |
|
1302 |
offs = __intVal(start) - 1; |
|
1303 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1304 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
1305 |
objSize = __Size(anObject) - nInstBytes; |
|
1306 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
1307 |
/* |
|
1308 |
* mhmh - since we are interruptable, anObject may move. |
|
1309 |
* therefore, fetch the cp-pointer within the loop |
|
1310 |
*/ |
|
1311 |
if (_INST(buffered) == true) { |
|
1312 |
__READING__(f) |
|
1313 |
} |
|
1314 |
||
1315 |
__BEGIN_INTERRUPTABLE__ |
|
1316 |
do { |
|
1317 |
errno = 0; |
|
1318 |
/* |
|
1319 |
* because we are interruptable, refetch pointer |
|
1320 |
*/ |
|
1321 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
|
1322 |
if (_INST(buffered) == false) { |
|
1323 |
ret = read(fileno(f), cp, cnt); |
|
1324 |
} else { |
|
1325 |
if (feof(f)) { |
|
1326 |
ret = 0; |
|
1327 |
break; |
|
1328 |
} |
|
1329 |
ret = fread(cp, 1, cnt, f); |
|
1330 |
} |
|
1331 |
} while ((ret < 0) && (errno == EINTR)); |
|
1332 |
||
1333 |
__END_INTERRUPTABLE__ |
|
1334 |
cnt = ret; |
|
1335 |
||
1336 |
if (cnt > 0) { |
|
1337 |
pos = _INST(position); |
|
1338 |
if (pos != nil) { |
|
1339 |
_INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
1340 |
} |
|
1341 |
RETURN (__MKSMALLINT(cnt)); |
|
1342 |
} |
|
1343 |
if (cnt == 0) { |
|
1344 |
_INST(hitEOF) = true; |
|
1345 |
RETURN (__MKSMALLINT(cnt)); |
|
1346 |
} |
|
1347 |
||
1348 |
_INST(position) = nil; |
|
1349 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1350 |
} |
|
1351 |
} |
|
1352 |
} |
|
1353 |
bad: ; |
|
1354 |
%}. |
|
1355 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
1356 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1357 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1358 |
" |
|
1359 |
count not integer or arg not bit-like (String, ByteArray etc) |
|
1360 |
" |
|
1361 |
^ self primitiveFailed |
|
92 | 1362 |
! |
1363 |
||
612 | 1364 |
nextBytesInto:anObject |
1365 |
"read bytes into an object, regardless of binary/text mode. |
|
1366 |
The number of bytes to read is defined by the objects size. |
|
1367 |
Return the number of bytes read. On EOF, 0 is returned. |
|
1368 |
If the receiver is some socket/pipe-like stream, an exception |
|
1369 |
is raised if the connection is broken. |
|
1370 |
||
1371 |
The object to read into must have non-pointer indexed instvars |
|
1372 |
(i.e. it must be a ByteArray, String, Float- or DoubleArray). |
|
1373 |
If anObject is a string or byteArray and reused, this provides the |
|
1374 |
fastest possible physical I/O (since no new objects are allocated). |
|
1375 |
||
1376 |
Use with care - non object oriented i/o. |
|
1377 |
Warning: in general, you cannot use this method to pass data from other |
|
1378 |
architectures since it does not care for byte order or float representation." |
|
1379 |
||
1380 |
^ self nextBytes:(anObject size) into:anObject startingAt:1 |
|
1381 |
||
1382 |
" to read 100 bytes from a stream: |
|
1383 |
||
1384 |
|b aStream| |
|
1385 |
||
1386 |
aStream := 'smalltalk.rc' asFilename readStream. |
|
1387 |
b := ByteArray new:100. |
|
1388 |
aStream nextBytesInto:b. |
|
1389 |
aStream close. |
|
1390 |
b inspect |
|
1391 |
" |
|
1392 |
||
1393 |
" |
|
1394 |
|s aStream| |
|
1395 |
aStream := 'smalltalk.rc' asFilename readStream. |
|
1396 |
s := String new:100. |
|
1397 |
aStream nextBytesInto:s. |
|
1398 |
aStream close. |
|
1399 |
s inspect |
|
1400 |
" |
|
1401 |
! |
|
1402 |
||
1403 |
nextLong |
|
1404 |
"Read four bytes (msb-first) and return the value as a 32-bit signed Integer. |
|
1405 |
The returned value may be a LargeInteger. |
|
1406 |
(msb-first for compatibility with other smalltalks)" |
|
1407 |
||
1408 |
^ self nextUnsignedLongMSB:true |
|
1409 |
! |
|
1410 |
||
1411 |
nextLongMSB:msbFlag |
|
1412 |
"Read four bytes and return the value as a 32-bit signed Integer, |
|
1413 |
which may be a LargeInteger. |
|
1414 |
If msbFlag is true, value is read with most-significant byte first, |
|
1415 |
otherwise least-significant byte comes first. |
|
1416 |
A nil is returned, if EOF is hit before all 4 bytes have been read. |
|
1417 |
Works in both binary and text modes." |
|
1418 |
||
1419 |
%{ /* NOCONTEXT */ |
|
1420 |
OBJ fp; |
|
1421 |
||
1422 |
_INST(lastErrorNumber) = nil; |
|
1423 |
if (((fp = _INST(filePointer)) != nil) |
|
1424 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1425 |
FILE *f; |
|
1426 |
int first, second, third, fourth; |
|
1427 |
int value; |
|
1428 |
||
1429 |
f = __FILEVal(fp); |
|
1430 |
__BEGIN_INTERRUPTABLE__ |
|
1431 |
__READING__(f) |
|
1432 |
||
1433 |
do { |
|
1434 |
first = getc(f); |
|
1435 |
} while ((first < 0) && (errno == EINTR)); |
|
1436 |
if (first != EOF) { |
|
1437 |
do { |
|
1438 |
second = getc(f); |
|
1439 |
} while ((second < 0) && (errno == EINTR)); |
|
1440 |
if (second != EOF) { |
|
1441 |
do { |
|
1442 |
third = getc(f); |
|
1443 |
} while ((third < 0) && (errno == EINTR)); |
|
1444 |
if (third != EOF) { |
|
1445 |
do { |
|
1446 |
fourth = getc(f); |
|
1447 |
} while ((fourth < 0) && (errno == EINTR)); |
|
1448 |
if (fourth != EOF) { |
|
1449 |
__END_INTERRUPTABLE__ |
|
1450 |
||
1451 |
if (msbFlag == true) { |
|
1452 |
value = (first & 0xFF); |
|
1453 |
value = (value<<8) | (second & 0xFF); |
|
1454 |
value = (value<<8) | (third & 0xFF); |
|
1455 |
value = (value<<8) | (fourth & 0xFF); |
|
1456 |
} else { |
|
1457 |
value = (fourth & 0xFF); |
|
1458 |
value = (value<<8) | (third & 0xFF); |
|
1459 |
value = (value<<8) | (second & 0xFF); |
|
1460 |
value = (value<<8) | (first & 0xFF); |
|
1461 |
} |
|
1462 |
if (_INST(position) != nil) { |
|
1463 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
1464 |
} |
|
1465 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) { |
|
1466 |
RETURN ( __MKSMALLINT(value)); |
|
1467 |
} |
|
1468 |
RETURN ( _MKLARGEINT(value) ); |
|
1469 |
} |
|
1470 |
} |
|
1471 |
} |
|
1472 |
} |
|
1473 |
__END_INTERRUPTABLE__ |
|
1474 |
||
1475 |
if (ferror(f) && (errno != 0)) { |
|
1476 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1477 |
_INST(position) = nil; |
|
1478 |
} else { |
|
1479 |
_INST(hitEOF) = true; |
|
1480 |
RETURN (nil); |
|
1481 |
} |
|
1482 |
} |
|
1483 |
%}. |
|
1484 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1485 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1486 |
^ self readError. |
|
362 | 1487 |
! |
1488 |
||
612 | 1489 |
nextShortMSB:msbFlag |
1490 |
"Read two bytes and return the value as a 16-bit signed Integer. |
|
1491 |
If msbFlag is true, value is read with most-significant byte first, |
|
1492 |
otherwise least-significant byte comes first. |
|
1493 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
1494 |
Works in both binary and text modes." |
|
1495 |
||
1496 |
%{ /* NOCONTEXT */ |
|
1497 |
OBJ fp; |
|
1498 |
||
1499 |
_INST(lastErrorNumber) = nil; |
|
1500 |
if (((fp = _INST(filePointer)) != nil) |
|
1501 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1502 |
FILE *f; |
|
1503 |
int first, second, err; |
|
1504 |
short value; |
|
1505 |
||
1506 |
f = __FILEVal(fp); |
|
1507 |
__BEGIN_INTERRUPTABLE__ |
|
1508 |
__READING__(f) |
|
1509 |
||
1510 |
do { |
|
1511 |
first = getc(f); |
|
1512 |
} while ((first < 0) && (errno == EINTR)); |
|
1513 |
||
1514 |
if (first != EOF) { |
|
1515 |
do { |
|
1516 |
second = getc(f); |
|
1517 |
} while ((second < 0) && (errno == EINTR)); |
|
1518 |
||
1519 |
__END_INTERRUPTABLE__ |
|
1520 |
if (second != EOF) { |
|
1521 |
if (_INST(position) != nil) { |
|
1522 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
1523 |
} |
|
1524 |
if (msbFlag == true) { |
|
1525 |
value = ((first & 0xFF) << 8) | (second & 0xFF); |
|
1526 |
} else { |
|
1527 |
value = ((second & 0xFF) << 8) | (first & 0xFF); |
|
1528 |
} |
|
1529 |
RETURN (__MKSMALLINT(value)); |
|
1530 |
} |
|
1531 |
} |
|
1532 |
__END_INTERRUPTABLE__ |
|
1533 |
||
1534 |
if (ferror(f) && (errno != 0)) { |
|
1535 |
_INST(position) = nil; |
|
1536 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1537 |
} else { |
|
1538 |
_INST(hitEOF) = true; |
|
1539 |
RETURN (nil); |
|
1540 |
} |
|
1541 |
} |
|
1542 |
%}. |
|
1543 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1544 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1545 |
^ self readError. |
|
1546 |
! |
|
1547 |
||
1548 |
nextUnsignedLongMSB:msbFlag |
|
1549 |
"Read four bytes and return the value as a 32-bit unsigned Integer, which may be |
|
1550 |
a LargeInteger. |
|
1551 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
1552 |
least-significant byte comes first. |
|
1553 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read. |
|
1554 |
Works in both binary and text modes." |
|
1555 |
||
1556 |
%{ /* NOCONTEXT */ |
|
1557 |
OBJ fp; |
|
1558 |
||
1559 |
_INST(lastErrorNumber) = nil; |
|
1560 |
if (((fp = _INST(filePointer)) != nil) |
|
1561 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1562 |
FILE *f; |
|
1563 |
int first, second, third, fourth; |
|
1564 |
unsigned int value; |
|
1565 |
||
1566 |
f = __FILEVal(fp); |
|
1567 |
__BEGIN_INTERRUPTABLE__ |
|
1568 |
__READING__(f) |
|
1569 |
||
1570 |
do { |
|
1571 |
first = getc(f); |
|
1572 |
} while ((first < 0) && (errno == EINTR)); |
|
1573 |
if (first != EOF) { |
|
1574 |
do { |
|
1575 |
second = getc(f); |
|
1576 |
} while ((second < 0) && (errno == EINTR)); |
|
1577 |
if (second != EOF) { |
|
1578 |
do { |
|
1579 |
third = getc(f); |
|
1580 |
} while ((third < 0) && (errno == EINTR)); |
|
1581 |
if (third != EOF) { |
|
1582 |
do { |
|
1583 |
fourth = getc(f); |
|
1584 |
} while ((fourth < 0) && (errno == EINTR)); |
|
1585 |
if (fourth != EOF) { |
|
1586 |
__END_INTERRUPTABLE__ |
|
1587 |
||
1588 |
if (msbFlag == true) { |
|
1589 |
value = (first & 0xFF); |
|
1590 |
value = (value<<8) | (second & 0xFF); |
|
1591 |
value = (value<<8) | (third & 0xFF); |
|
1592 |
value = (value<<8) | (fourth & 0xFF); |
|
1593 |
} else { |
|
1594 |
value = (fourth & 0xFF); |
|
1595 |
value = (value<<8) | (third & 0xFF); |
|
1596 |
value = (value<<8) | (second & 0xFF); |
|
1597 |
value = (value<<8) | (first & 0xFF); |
|
1598 |
} |
|
1599 |
if (_INST(position) != nil) { |
|
1600 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
1601 |
} |
|
1602 |
if (value <= _MAX_INT) { |
|
1603 |
RETURN ( __MKSMALLINT(value)); |
|
1604 |
} |
|
1605 |
RETURN ( _MKULARGEINT(value) ); |
|
1606 |
} |
|
1607 |
} |
|
1608 |
} |
|
1609 |
} |
|
1610 |
__END_INTERRUPTABLE__ |
|
1611 |
||
1612 |
if (ferror(f) && (errno != 0)) { |
|
1613 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1614 |
_INST(position) = nil; |
|
1615 |
} else { |
|
1616 |
_INST(hitEOF) = true; |
|
1617 |
RETURN (nil); |
|
1618 |
} |
|
1619 |
} |
|
1620 |
%}. |
|
1621 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1622 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1623 |
^ self readError. |
|
1624 |
! |
|
1625 |
||
1626 |
nextUnsignedShortMSB:msbFlag |
|
1627 |
"Read two bytes and return the value as a 16-bit unsigned Integer. |
|
1628 |
If msbFlag is true, value is read with most-significant byte first, otherwise |
|
1629 |
least-significant byte comes first. |
|
1630 |
A nil is returned if EOF is reached (also when EOF is hit after the first byte). |
|
1631 |
Works in both binary and text modes." |
|
1632 |
||
1633 |
%{ /* NOCONTEXT */ |
|
1634 |
OBJ fp; |
|
1635 |
||
1636 |
_INST(lastErrorNumber) = nil; |
|
1637 |
if (((fp = _INST(filePointer)) != nil) |
|
1638 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
1639 |
FILE *f; |
|
1640 |
int first, second; |
|
1641 |
unsigned value; |
|
1642 |
||
1643 |
f = __FILEVal(fp); |
|
1644 |
__BEGIN_INTERRUPTABLE__ |
|
1645 |
__READING__(f) |
|
1646 |
||
1647 |
do { |
|
1648 |
first = getc(f); |
|
1649 |
} while ((first < 0) && (errno == EINTR)); |
|
1650 |
||
1651 |
if (first != EOF) { |
|
1652 |
do { |
|
1653 |
second = getc(f); |
|
1654 |
} while ((second < 0) && (errno == EINTR)); |
|
1655 |
||
1656 |
__END_INTERRUPTABLE__ |
|
1657 |
if (second != EOF) { |
|
1658 |
if (_INST(position) != nil) { |
|
1659 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
1660 |
} |
|
1661 |
if (msbFlag == true) { |
|
1662 |
value = ((first & 0xFF) << 8) | (second & 0xFF); |
|
1663 |
} else { |
|
1664 |
value = ((second & 0xFF) << 8) | (first & 0xFF); |
|
1665 |
} |
|
1666 |
RETURN (__MKSMALLINT(value)); |
|
1667 |
} |
|
1668 |
} |
|
1669 |
__END_INTERRUPTABLE__ |
|
1670 |
||
1671 |
if (ferror(f) && (errno != 0)) { |
|
1672 |
_INST(position) = nil; |
|
1673 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1674 |
} else { |
|
1675 |
_INST(hitEOF) = true; |
|
1676 |
RETURN (nil); |
|
1677 |
} |
|
1678 |
} |
|
1679 |
%}. |
|
1680 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1681 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
1682 |
^ self readError. |
|
1683 |
! |
|
1684 |
||
1685 |
nextWord |
|
1686 |
"in text-mode: |
|
1687 |
read the alphaNumeric next word (i.e. up to non letter-or-digit). |
|
1688 |
return a string containing those characters. |
|
1689 |
in binary-mode: |
|
1690 |
read two bytes (msb-first) and return the value as a 16-bit |
|
1691 |
unsigned Integer (for compatibility with other smalltalks)" |
|
1692 |
||
1693 |
binary ifTrue:[ |
|
1694 |
^ self nextUnsignedShortMSB:true |
|
92 | 1695 |
]. |
612 | 1696 |
^ self nextAlphaNumericWord |
1697 |
! ! |
|
1698 |
||
1699 |
!ExternalStream methodsFor:'non homogenous writing'! |
|
1700 |
||
1701 |
nextPutByte:aByteValue |
|
1702 |
"write a byte. |
|
1703 |
Works in both binary and text modes." |
|
1704 |
||
1705 |
%{ /* NOCONTEXT */ |
|
1706 |
||
1707 |
FILE *f; |
|
1708 |
char c; |
|
1709 |
OBJ pos, fp; |
|
1710 |
int cnt; |
|
1711 |
||
1712 |
_INST(lastErrorNumber) = nil; |
|
1713 |
if (((fp = _INST(filePointer)) != nil) |
|
1714 |
&& (_INST(mode) != @symbol(readonly))) { |
|
1715 |
if (__isSmallInteger(aByteValue)) { |
|
1716 |
c = __intVal(aByteValue); |
|
1717 |
f = __FILEVal(fp); |
|
1718 |
__BEGIN_INTERRUPTABLE__ |
|
1719 |
/* |
|
1720 |
*#ifdef OLD |
|
1721 |
* if (_INST(buffered) == false) { |
|
1722 |
* cnt = write(fileno(f), &c, 1); |
|
1723 |
* } else |
|
1724 |
*#endif |
|
1725 |
* { |
|
1726 |
* __WRITING__(f) |
|
1727 |
* cnt = fwrite(&c, 1, 1, f); |
|
1728 |
*#ifndef OLD |
|
1729 |
* if (_INST(buffered) == false) { |
|
1730 |
* fflush(f); |
|
1731 |
* } |
|
1732 |
*#endif |
|
1733 |
* } |
|
1734 |
*/ |
|
1735 |
if (_INST(buffered) == true) { |
|
1736 |
__WRITING__(f) |
|
1737 |
cnt = fwrite(&c, 1, 1, f); |
|
1738 |
} else { |
|
1739 |
do { |
|
1740 |
cnt = write(fileno(f), &c, 1); |
|
1741 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1742 |
} |
|
1743 |
__END_INTERRUPTABLE__ |
|
1744 |
||
1745 |
if (cnt == 1) { |
|
1746 |
pos = _INST(position); |
|
1747 |
if (pos != nil) |
|
1748 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
1749 |
RETURN (self); |
|
1750 |
} |
|
1751 |
if (cnt < 0) { |
|
1752 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1753 |
} |
|
1754 |
} |
|
1755 |
} |
|
1756 |
%}. |
|
1757 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1758 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
1759 |
^ self writeError. |
|
1760 |
! |
|
1761 |
||
1762 |
nextPutBytes:count from:anObject |
|
1763 |
"write count bytes from an object. |
|
1764 |
Return the number of bytes written or nil on error. |
|
1765 |
The object must have non-pointer indexed instvars |
|
1766 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
1767 |
Use with care - non object oriented i/o. |
|
1768 |
Warning: in general, you cannot use this method to pass data to other |
|
1769 |
architectures since it does not care for byte order or float representation." |
|
1770 |
||
1771 |
^ self nextPutBytes:count from:anObject startingAt:1 |
|
1772 |
! |
|
1773 |
||
1774 |
nextPutBytes:count from:anObject startingAt:start |
|
1775 |
"write count bytes from an object starting at index start. |
|
1776 |
return the number of bytes written - which could be 0. |
|
1777 |
The object must have non-pointer indexed instvars |
|
1778 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
1779 |
Use with care - non object oriented i/o. |
|
1780 |
Warning: in general, you cannot use this method to pass data to other |
|
1781 |
architectures since it does not care for byte order or float representation." |
|
1782 |
||
1783 |
%{ /* NOCONTEXT */ |
|
1784 |
FILE *f; |
|
1785 |
int cnt, offs; |
|
1786 |
int objSize, nInstVars, nInstBytes; |
|
1787 |
char *cp; |
|
1788 |
OBJ oClass, pos, fp; |
|
1789 |
||
1790 |
_INST(lastErrorNumber) = nil; |
|
1791 |
if (((fp = _INST(filePointer)) != nil) |
|
1792 |
&& (_INST(mode) != @symbol(readonly))) { |
|
1793 |
if (__bothSmallInteger(count, start)) { |
|
1794 |
oClass = __Class(anObject); |
|
1795 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1796 |
case BYTEARRAY: |
|
1797 |
case WORDARRAY: |
|
1798 |
case LONGARRAY: |
|
1799 |
case FLOATARRAY: |
|
1800 |
case DOUBLEARRAY: |
|
1801 |
break; |
|
1802 |
default: |
|
1803 |
goto bad; |
|
1804 |
} |
|
1805 |
cnt = __intVal(count); |
|
1806 |
offs = __intVal(start) - 1; |
|
1807 |
f = __FILEVal(fp); |
|
1808 |
||
1809 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1810 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
1811 |
objSize = __Size(anObject) - nInstBytes; |
|
1812 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
1813 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
|
1814 |
__BEGIN_INTERRUPTABLE__ |
|
1815 |
||
1816 |
if (_INST(buffered) == true) { |
|
1817 |
__WRITING__(f) |
|
1818 |
cnt = fwrite(cp, 1, cnt, f); |
|
1819 |
} else { |
|
1820 |
do { |
|
1821 |
cp = (char *)__InstPtr(anObject) + nInstBytes + offs; |
|
1822 |
cnt = write(fileno(f), cp, cnt); |
|
1823 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1824 |
} |
|
1825 |
||
1826 |
__END_INTERRUPTABLE__ |
|
1827 |
||
1828 |
if (cnt >= 0) { |
|
1829 |
pos = _INST(position); |
|
1830 |
if (pos != nil) |
|
1831 |
_INST(position) = __MKSMALLINT(__intVal(pos) + cnt); |
|
1832 |
RETURN ( __MKSMALLINT(cnt) ); |
|
1833 |
} |
|
1834 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1835 |
} |
|
1836 |
} |
|
1837 |
} |
|
1838 |
bad: ; |
|
1839 |
%}. |
|
1840 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
1841 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1842 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
1843 |
^ self primitiveFailed |
|
1844 |
! |
|
1845 |
||
1846 |
nextPutBytesFrom:anObject |
|
1847 |
"write bytes from an object; the number of bytes is defined by |
|
1848 |
the objects size. |
|
1849 |
Return the number of bytes written or nil on error. |
|
1850 |
The object must have non-pointer indexed instvars |
|
1851 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
1852 |
Use with care - non object oriented i/o. |
|
1853 |
Warning: in general, you cannot use this method to pass data to other |
|
1854 |
architectures since it does not care for byte order or float representation." |
|
1855 |
||
1856 |
^ self nextPutBytes:(anObject size) from:anObject startingAt:1 |
|
1857 |
! |
|
1858 |
||
1859 |
nextPutLong:aNumber MSB:msbFlag |
|
1860 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
1861 |
true, data is written most-significant byte first; otherwise least |
|
1862 |
first. |
|
1863 |
Works in both binary and text modes." |
|
1864 |
||
1865 |
%{ /* NOCONTEXT */ |
|
1866 |
||
1867 |
int num; |
|
1868 |
char bytes[4]; |
|
1869 |
FILE *f; |
|
1870 |
int cnt; |
|
1871 |
OBJ fp; |
|
1872 |
||
1873 |
_INST(lastErrorNumber) = nil; |
|
1874 |
if (((fp = _INST(filePointer)) != nil) |
|
1875 |
&& (_INST(mode) != @symbol(readonly)) |
|
1876 |
&& __isSmallInteger(aNumber)) { |
|
1877 |
num = __intVal(aNumber); |
|
1878 |
if (msbFlag == true) { |
|
1879 |
bytes[0] = (num >> 24) & 0xFF; |
|
1880 |
bytes[1] = (num >> 16) & 0xFF; |
|
1881 |
bytes[2] = (num >> 8) & 0xFF; |
|
1882 |
bytes[3] = num & 0xFF; |
|
1883 |
} else { |
|
1884 |
bytes[3] = (num >> 24) & 0xFF; |
|
1885 |
bytes[2] = (num >> 16) & 0xFF; |
|
1886 |
bytes[1] = (num >> 8) & 0xFF; |
|
1887 |
bytes[0] = num & 0xFF; |
|
1888 |
} |
|
1889 |
||
1890 |
f = __FILEVal(fp); |
|
1891 |
__BEGIN_INTERRUPTABLE__ |
|
1892 |
||
1893 |
if (_INST(buffered) == true) { |
|
1894 |
__WRITING__(f) |
|
1895 |
cnt = fwrite(bytes, 1, 4, f); |
|
1896 |
} else { |
|
1897 |
do { |
|
1898 |
cnt = write(fileno(f), bytes, 4); |
|
1899 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1900 |
} |
|
1901 |
||
1902 |
__END_INTERRUPTABLE__ |
|
1903 |
||
1904 |
if (cnt == 4) { |
|
1905 |
if (_INST(position) != nil) { |
|
1906 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 4); |
|
1907 |
} |
|
1908 |
RETURN ( self ); |
|
1909 |
} |
|
1910 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1911 |
} |
|
1912 |
%}. |
|
1913 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1914 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
1915 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
1916 |
||
1917 |
aNumber isInteger ifTrue:[ |
|
1918 |
msbFlag ifTrue:[ |
|
1919 |
"high word first" |
|
1920 |
(self nextShortPut:(aNumber // 16r10000) MSB:true) isNil ifTrue:[^ nil]. |
|
1921 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:true |
|
1922 |
]. |
|
1923 |
"low word first" |
|
1924 |
(self nextShortPut:(aNumber \\ 16r10000) MSB:false) isNil ifTrue:[^ nil]. |
|
1925 |
^ self nextShortPut:(aNumber // 16r10000) MSB:false. |
|
92 | 1926 |
]. |
612 | 1927 |
self argumentMustBeInteger |
1928 |
! |
|
1929 |
||
1930 |
nextPutShort:aNumber MSB:msbFlag |
|
1931 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
1932 |
true, data is written most-significant byte first; otherwise least |
|
1933 |
first. |
|
1934 |
Works in both binary and text modes." |
|
1935 |
||
1936 |
%{ /* NOCONTEXT */ |
|
1937 |
||
1938 |
int num; |
|
1939 |
char bytes[2]; |
|
1940 |
FILE *f; |
|
1941 |
int cnt; |
|
1942 |
OBJ fp; |
|
1943 |
||
1944 |
_INST(lastErrorNumber) = nil; |
|
1945 |
if (((fp = _INST(filePointer)) != nil) |
|
1946 |
&& (_INST(mode) != @symbol(readonly)) |
|
1947 |
&& __isSmallInteger(aNumber)) { |
|
1948 |
num = __intVal(aNumber); |
|
1949 |
if (msbFlag == true) { |
|
1950 |
bytes[0] = (num >> 8) & 0xFF; |
|
1951 |
bytes[1] = num & 0xFF; |
|
1952 |
} else { |
|
1953 |
bytes[1] = (num >> 8) & 0xFF; |
|
1954 |
bytes[0] = num & 0xFF; |
|
1955 |
} |
|
1956 |
||
1957 |
f = __FILEVal(fp); |
|
1958 |
__BEGIN_INTERRUPTABLE__ |
|
1959 |
||
1960 |
if (_INST(buffered) == true) { |
|
1961 |
__WRITING__(f) |
|
1962 |
cnt = fwrite(bytes, 1, 2, f); |
|
1963 |
} else { |
|
1964 |
do { |
|
1965 |
cnt = write(fileno(f), bytes, 2); |
|
1966 |
} while ((cnt < 0) && (errno == EINTR)); |
|
1967 |
} |
|
1968 |
||
1969 |
__END_INTERRUPTABLE__ |
|
1970 |
||
1971 |
if (cnt == 2) { |
|
1972 |
if (_INST(position) != nil) { |
|
1973 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + 2); |
|
1974 |
} |
|
1975 |
RETURN ( self ); |
|
1976 |
} |
|
1977 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1978 |
} |
|
1979 |
%}. |
|
1980 |
lastErrorNumber notNil ifTrue:[^ self writeError]. |
|
1981 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1982 |
(mode == #readonly) ifTrue:[^ self errorReadOnly]. |
|
1983 |
self argumentMustBeInteger |
|
10 | 1984 |
! ! |
612 | 1985 |
|
1986 |
!ExternalStream methodsFor:'private'! |
|
1987 |
||
1988 |
clearEOF |
|
1989 |
hitEOF := false |
|
1990 |
! |
|
1991 |
||
1992 |
reOpen |
|
1993 |
"sent after snapin to reopen streams. |
|
1994 |
cannot reopen here since I am abstract and have no device knowledge" |
|
1995 |
||
1996 |
self class name errorPrint. ': cannot reOpen stream - stream closed' errorPrintNL. |
|
1997 |
filePointer := nil. |
|
1998 |
Lobby unregister:self. |
|
1999 |
! |
|
2000 |
||
2001 |
setLastError:aNumber |
|
2002 |
lastErrorNumber := aNumber |
|
2003 |
! ! |
|
2004 |
||
2005 |
!ExternalStream methodsFor:'queries'! |
|
2006 |
||
2007 |
isBinary |
|
2008 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
2009 |
The default when created is false." |
|
2010 |
||
2011 |
^ binary |
|
2012 |
! |
|
2013 |
||
2014 |
isExternalStream |
|
2015 |
"return true, if the receiver is some kind of externalStream; |
|
2016 |
true is returned here - the method redefined from Object." |
|
2017 |
||
2018 |
^ true |
|
2019 |
! |
|
2020 |
||
2021 |
isReadable |
|
2022 |
"return true, if this stream can be read from" |
|
2023 |
||
2024 |
^ (mode ~~ #writeonly) |
|
2025 |
! |
|
2026 |
||
2027 |
isWritable |
|
2028 |
"return true, if this stream can be written to" |
|
2029 |
||
2030 |
^ (mode ~~ #readonly) |
|
2031 |
! ! |
|
2032 |
||
2033 |
!ExternalStream methodsFor:'reading'! |
|
2034 |
||
2035 |
next |
|
2036 |
"return the next element; advance read position. |
|
2037 |
In binary mode, an integer is returned, otherwise a character. |
|
2038 |
If there are no more elements, nil is returned." |
|
1 | 2039 |
|
2040 |
%{ /* NOCONTEXT */ |
|
2041 |
||
2042 |
FILE *f; |
|
2043 |
int c; |
|
612 | 2044 |
OBJ pos, fp; |
2045 |
unsigned char ch; |
|
2046 |
||
2047 |
_INST(lastErrorNumber) = nil; |
|
2048 |
if (((fp = _INST(filePointer)) != nil) |
|
2049 |
&& (_INST(mode) != @symbol(writeonly))) { |
|
2050 |
f = __FILEVal(fp); |
|
2051 |
__BEGIN_INTERRUPTABLE__ |
|
2052 |
||
2053 |
if (_INST(buffered) == true) { |
|
2054 |
__READING__(f) |
|
2055 |
} |
|
2056 |
||
2057 |
do { |
|
2058 |
errno = 0; |
|
2059 |
if (_INST(buffered) == false) { |
|
2060 |
if (read(fileno(f), &ch, 1) != 1) |
|
2061 |
c = EOF; |
|
2062 |
else |
|
2063 |
c = ch; |
|
2064 |
} else |
|
2065 |
{ |
|
2066 |
c = getc(f); |
|
2067 |
} |
|
2068 |
} while ((c < 0) && (errno == EINTR)); |
|
2069 |
||
2070 |
__END_INTERRUPTABLE__ |
|
2071 |
||
2072 |
if (c != EOF) { |
|
2073 |
pos = _INST(position); |
|
2074 |
if (__isSmallInteger(pos)) { |
|
2075 |
_INST(position) = __MKSMALLINT(__intVal(pos) + 1); |
|
2076 |
} else { |
|
2077 |
_INST(position) = nil; |
|
2078 |
} |
|
2079 |
if (_INST(binary) == true) { |
|
2080 |
RETURN ( __MKSMALLINT(c & 0xFF) ); |
|
2081 |
} |
|
2082 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
|
2083 |
} |
|
2084 |
/* |
|
2085 |
if (errno == EWOULDBLOCK) { |
|
2086 |
_INST(hitEOF) = true; |
|
2087 |
RETURN(nil); |
|
2088 |
} |
|
2089 |
*/ |
|
2090 |
_INST(position) = nil; |
|
2091 |
||
2092 |
if ((_INST(buffered) == false) |
|
2093 |
|| (ferror(f) && (errno != 0))) { |
|
2094 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2095 |
} else { |
|
2096 |
_INST(hitEOF) = true; |
|
2097 |
RETURN ( nil ); |
|
2098 |
} |
|
2099 |
} |
|
2100 |
%}. |
|
2101 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2102 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2103 |
self errorWriteOnly |
|
2104 |
! |
|
2105 |
||
2106 |
next:count |
|
2107 |
"return the next count elements of the stream as a collection. |
|
2108 |
Redefined to return a String or ByteArray instead of the default: Array." |
|
2109 |
||
2110 |
|coll| |
|
2111 |
||
2112 |
binary ifTrue:[ |
|
2113 |
coll := ByteArray uninitializedNew:count |
|
2114 |
] ifFalse:[ |
|
2115 |
coll := String new:count |
|
2116 |
]. |
|
2117 |
"/ |
|
2118 |
"/ Q: should we use: |
|
2119 |
"/ self nextBytes:count into:coll startingAt:1 |
|
2120 |
"/ |
|
2121 |
1 to:count do: [:index | |
|
2122 |
coll at:index put:(self next) |
|
2123 |
]. |
|
2124 |
^ coll |
|
2125 |
! |
|
2126 |
||
2127 |
peek |
|
2128 |
"return the element to be read next without advancing read position. |
|
2129 |
In binary mode, an integer is returned, otherwise a character. |
|
2130 |
If there are no more elements, nil is returned. |
|
2131 |
Not allowed in unbuffered mode." |
|
2132 |
||
2133 |
%{ /* NOCONTEXT */ |
|
2134 |
||
2135 |
FILE *f; |
|
2136 |
REGISTER int c; |
|
216 | 2137 |
OBJ fp; |
92 | 2138 |
|
159 | 2139 |
_INST(lastErrorNumber) = nil; |
216 | 2140 |
if (((fp = _INST(filePointer)) != nil) |
159 | 2141 |
&& (_INST(mode) != @symbol(writeonly))) { |
612 | 2142 |
#ifdef OLD |
2143 |
if (_INST(buffered) == true) |
|
2144 |
#endif |
|
2145 |
{ |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2146 |
f = __FILEVal(fp); |
612 | 2147 |
|
362 | 2148 |
__BEGIN_INTERRUPTABLE__ |
216 | 2149 |
__READING__(f) |
159 | 2150 |
|
612 | 2151 |
do { |
2152 |
c = getc(f); |
|
2153 |
} while ((c < 0) && (errno == EINTR)); |
|
2154 |
__END_INTERRUPTABLE__ |
|
2155 |
||
2156 |
if (c != EOF) { |
|
2157 |
ungetc(c, f); |
|
2158 |
if (_INST(binary) == true) { |
|
2159 |
RETURN ( __MKSMALLINT(c & 0xFF) ); |
|
159 | 2160 |
} |
612 | 2161 |
RETURN ( _MKCHARACTER(c & 0xFF) ); |
159 | 2162 |
} |
216 | 2163 |
if (ferror(f) && (errno != 0)) { |
359 | 2164 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 2165 |
} else { |
2166 |
_INST(hitEOF) = true; |
|
2167 |
RETURN ( nil ); |
|
2168 |
} |
|
2169 |
} |
|
1 | 2170 |
} |
159 | 2171 |
%}. |
216 | 2172 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
1 | 2173 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
612 | 2174 |
buffered ifFalse:[^ self errorNotBuffered]. |
159 | 2175 |
^ self errorWriteOnly |
612 | 2176 |
! ! |
2177 |
||
2178 |
!ExternalStream methodsFor:'reimplemented for speed'! |
|
159 | 2179 |
|
2180 |
nextAlphaNumericWord |
|
379 | 2181 |
"read the next word (i.e. up to non letter-or-digit) after first |
2182 |
skipping any whiteSpace. |
|
2183 |
Return a string containing those characters. |
|
2184 |
There is a limit of 1023 characters in the word - if longer, |
|
2185 |
it is truncated." |
|
159 | 2186 |
|
2187 |
%{ /* STACK: 2000 */ |
|
2188 |
FILE *f; |
|
2189 |
int len; |
|
2190 |
char buffer[1024]; |
|
2191 |
int ch; |
|
2192 |
int cnt = 0; |
|
216 | 2193 |
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
|
2194 |
OBJ __MKSTRING_L(); |
216 | 2195 |
|
2196 |
if (((fp = _INST(filePointer)) != nil) |
|
159 | 2197 |
&& (_INST(mode) != @symbol(writeonly))) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2198 |
f = __FILEVal(fp); |
362 | 2199 |
__BEGIN_INTERRUPTABLE__ |
216 | 2200 |
__READING__(f) |
159 | 2201 |
|
370 | 2202 |
/* |
2203 |
* skip whiteSpace first ... |
|
2204 |
*/ |
|
159 | 2205 |
for (;;) { |
213 | 2206 |
do { |
2207 |
ch = getc(f); |
|
2208 |
} while ((ch < 0) && (errno == EINTR)); |
|
362 | 2209 |
|
159 | 2210 |
if (ch < 0) { |
216 | 2211 |
if (ferror(f) && (errno != 0)) { |
359 | 2212 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 2213 |
__END_INTERRUPTABLE__ |
159 | 2214 |
goto err; |
2215 |
} |
|
2216 |
_INST(hitEOF) = true; |
|
2217 |
break; |
|
2218 |
} |
|
2219 |
cnt++; |
|
2220 |
||
379 | 2221 |
#ifndef NON_ASCII |
159 | 2222 |
if (ch >= ' ') break; |
379 | 2223 |
#endif |
159 | 2224 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') |
2225 |
&& (ch != '\n') && (ch != 0x0b)) break; |
|
2226 |
} |
|
2227 |
ungetc(ch, f); |
|
2228 |
cnt--; |
|
2229 |
||
2230 |
len = 0; |
|
2231 |
for (;;) { |
|
213 | 2232 |
do { |
2233 |
ch = getc(f); |
|
2234 |
} while ((ch < 0) && (errno == EINTR)); |
|
159 | 2235 |
if (ch < 0) { |
216 | 2236 |
if (ferror(f) && (errno != 0)) { |
359 | 2237 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
362 | 2238 |
__END_INTERRUPTABLE__ |
159 | 2239 |
goto err; |
2240 |
} |
|
2241 |
_INST(hitEOF) = true; |
|
2242 |
break; |
|
2243 |
} |
|
2244 |
||
2245 |
ch &= 0xFF; |
|
2246 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
|
2247 |
((ch >= 'A') && (ch <= 'Z')) || |
|
2248 |
((ch >= '0') && (ch <= '9')))) { |
|
2249 |
ungetc(ch, f); |
|
2250 |
break; |
|
2251 |
} |
|
2252 |
cnt++; |
|
2253 |
buffer[len++] = ch; |
|
370 | 2254 |
if (len >= (sizeof(buffer)-1)) { |
159 | 2255 |
/* emergency */ |
2256 |
break; |
|
2257 |
} |
|
2258 |
} |
|
362 | 2259 |
__END_INTERRUPTABLE__ |
159 | 2260 |
|
2261 |
if (_INST(position) != nil) { |
|
359 | 2262 |
_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + cnt); |
159 | 2263 |
} |
2264 |
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
|
2265 |
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
|
2266 |
RETURN ( __MKSTRING_L(buffer, len COMMA_CON) ); |
159 | 2267 |
} |
2268 |
RETURN ( nil ); |
|
2269 |
} |
|
2270 |
err: ; |
|
2271 |
%}. |
|
2272 |
lastErrorNumber notNil ifTrue:[^ self readError]. |
|
2273 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
2274 |
^ self errorWriteOnly |
|
1 | 2275 |
! |
2276 |
||
2277 |
nextChunk |
|
2278 |
"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
|
2279 |
exclamation mark. Within the chunk, exclamation marks have to be doubled - |
216 | 2280 |
except within primitive code (this exception was added to make it easier |
2281 |
to edit primitive code with external editors). This means, that other |
|
2282 |
Smalltalks cannot always read chunks containing primitive code - but that |
|
2283 |
doesnt really matter, since C-primitives are an ST/X feature anyway. |
|
2284 |
Reimplemented here for more speed." |
|
2285 |
||
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2286 |
|retVal outOfMemory buffer| |
1 | 2287 |
|
159 | 2288 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
2289 |
binary ifTrue:[^ self errorBinary]. |
|
2290 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly]. |
|
216 | 2291 |
|
443 | 2292 |
%{ /* 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
|
2293 |
/* |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2294 |
* 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
|
2295 |
* 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
|
2296 |
* 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
|
2297 |
* 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
|
2298 |
*/ |
1 | 2299 |
FILE *f; |
2300 |
int done = 0; |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2301 |
REGISTER int c, lastC; |
216 | 2302 |
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
|
2303 |
char *bufferPtr = (char *)0; |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2304 |
char fastBuffer[7000]; |
1 | 2305 |
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
|
2306 |
int currSize, fastFlag; |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2307 |
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
|
2308 |
OBJ __MKSTRING_L(), __MKSTRING_ST_L(), __MKEMPTYSTRING(); |
216 | 2309 |
|
159 | 2310 |
_INST(lastErrorNumber) = nil; |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
2311 |
f = __FILEVal(_INST(filePointer)); |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2312 |
|
216 | 2313 |
__READING__(f) |
159 | 2314 |
|
2315 |
if (feof(f)) { |
|
422 | 2316 |
_INST(hitEOF) = true; |
159 | 2317 |
RETURN (nil); |
92 | 2318 |
} |
436 | 2319 |
|
1 | 2320 |
/* |
2321 |
* skip spaces |
|
2322 |
*/ |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2323 |
c = '\n'; |
1 | 2324 |
while (! done) { |
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2325 |
lastC = c; |
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2326 |
|
362 | 2327 |
__BEGIN_INTERRUPTABLE__ |
159 | 2328 |
do { |
2329 |
c = getc(f); |
|
2330 |
} while ((c < 0) && (errno == EINTR)); |
|
362 | 2331 |
__END_INTERRUPTABLE__ |
159 | 2332 |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2333 |
atBeginOfLine = 0; |
159 | 2334 |
switch (c) { |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2335 |
case '\n': |
159 | 2336 |
case ' ': |
2337 |
case '\t': |
|
2338 |
case '\r': |
|
2339 |
case '\b': |
|
2340 |
case '\014': |
|
2341 |
break; |
|
2342 |
||
2343 |
case EOF: |
|
216 | 2344 |
if (ferror(f) && (errno != 0)) { |
359 | 2345 |
_INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 2346 |
goto err; |
2347 |
} |
|
2348 |
_INST(hitEOF) = true; |
|
2349 |
RETURN (nil); |
|
2350 |
||
2351 |
default: |
|
673
2c3a4a536cd1
oops - handling of exclas within primitives was wrong
Claus Gittinger <cg@exept.de>
parents:
671
diff
changeset
|
2352 |
atBeginOfLine = (lastC == '\n'); |
159 | 2353 |
ungetc(c, f); |
2354 |
done = 1; |
|
2355 |
break; |
|
2356 |
} |
|
1 | 2357 |
} |
2358 |
||
2359 |
/* |
|
2360 |
* read chunk into a buffer |
|
2361 |
*/ |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2362 |
bufferPtr = fastBuffer; fastFlag = 1; |
216 | 2363 |
currSize = sizeof(fastBuffer); |
362 | 2364 |
|
1 | 2365 |
index = 0; |
2366 |
while (! feof(f)) { |
|
216 | 2367 |
/* |
2368 |
* do we have to resize the buffer ? |
|
2369 |
*/ |
|
159 | 2370 |
if ((index+2) >= currSize) { |
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2371 |
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
|
2372 |
char *nbp; |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2373 |
|
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2374 |
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
|
2375 |
if (newBuffer == nil) { |
216 | 2376 |
/* |
2377 |
* mhmh - chunk seems to be very big .... |
|
2378 |
*/ |
|
2379 |
outOfMemory = true; |
|
2380 |
goto err; |
|
2381 |
} |
|
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2382 |
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
|
2383 |
if (!fastFlag) { |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2384 |
/* |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2385 |
* 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
|
2386 |
*/ |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2387 |
bufferPtr = __stringVal(buffer); |
216 | 2388 |
} |
665
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2389 |
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
|
2390 |
bufferPtr = nbp; |
429936f8e6e7
fixed nextChunk; it was not reentrant due to freeing of the static
Claus Gittinger <cg@exept.de>
parents:
612
diff
changeset
|
2391 |
bufferPtr[index] = '\0'; |
159 | 2392 |
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
|
2393 |
fastFlag = 0; |
159 | 2394 |
currSize = currSize * 2; |
2395 |
} |
|
362 | 2396 |
|
2397 |
__BEGIN_INTERRUPTABLE__ |
|
159 | 2398 |
do { |
2399 |
c = getc(f); |
|
2400 |
} while (c < 0 && (errno == EINTR)); |
|
362 | 2401 |
__END_INTERRUPTABLE__ |
159 | 2402 |
|
668
73165201a9da
nextChunk: %{ is only recognized if at begin-of-line
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2403 |
if (atBeginOfLine && (c == '%')) { |
362 | 2404 |
__BEGIN_INTERRUPTABLE__ |
188 | 2405 |
do { |
213 | 2406 |
peekC = getc(f); |
188 | 2407 |
} while (peekC < 0 && (errno == EINTR)); |
362 | 2408 |
__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
|
2409 |
|
159 | 2410 |
ungetc(peekC, f); |
2411 |
if (peekC == '{') { |
|
2412 |
inPrimitive++; |
|
2413 |
} else if (peekC == '}') { |
|
687
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2414 |
if (inPrimitive > 0) { |
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2415 |
inPrimitive--; |
d92bee8e9f41
avoid getting out of sync, if too many %}'s are encountered
Claus Gittinger <cg@exept.de>
parents:
673
diff
changeset
|
2416 |
} |
159 | 2417 |
} |
2418 |
} else { |
|
688 | 2419 |
if (! inPrimitive) { |
159 | 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 |
" |
711 | 2468 |
buffer memory allocation failed. |
2469 |
When we arrive here, there was no memory available for the |
|
370 | 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 |
" |
711 | 2473 |
^ ObjectMemory allocationFailureSignal 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 |
|
671 | 2793 |
skip was successful, otherwise (i.e. if not found) return nil. |
612 | 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; |
|
671 | 2871 |
return nil if not found, self otherwise. If successful, the next read |
612 | 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 |
||
730 | 3447 |
!ExternalStream class methodsFor:'documentation'! |
3448 |
||
3449 |
version |
|
3450 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.78 1995-12-10 00:33:10 cg Exp $' |
|
3451 |
! ! |
|
612 | 3452 |
ExternalStream initialize! |