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