1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
12
|
14 |
instanceVariableNames:'filePointer mode buffered binary useCRLF'
|
1
|
15 |
classVariableNames:'lobby'
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
ExternalStream comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
ExternalStream defines protocol common to Streams which have a file-descriptor and
|
2
|
26 |
represent some file or communicationChannel of the underlying OperatingSystem.
|
1
|
27 |
ExternalStream is abstract; concrete classes are FileStream, PipeStream etc.
|
|
28 |
ExternalStreams can be in two modes: text (the default) and binary.
|
|
29 |
In text-mode, the elements read/written are characters; while in binary-mode the basic
|
|
30 |
elements are bytes which read/write as SmallIntegers in the range 0..255.
|
|
31 |
|
12
|
32 |
$Header: /cvs/stx/stx/libbasic/Attic/ExtStream.st,v 1.6 1993-12-11 00:46:55 claus Exp $
|
1
|
33 |
|
|
34 |
written 88 by claus
|
|
35 |
'!
|
|
36 |
|
|
37 |
%{
|
|
38 |
#include <stdio.h>
|
10
|
39 |
#include <fcntl.h>
|
|
40 |
#include <errno.h>
|
1
|
41 |
%}
|
|
42 |
|
|
43 |
!ExternalStream class methodsFor:'initialization'!
|
|
44 |
|
|
45 |
initialize
|
|
46 |
lobby isNil ifTrue:[
|
|
47 |
lobby := Registry new.
|
|
48 |
|
|
49 |
"want to get informed when returning from snapshot"
|
|
50 |
ObjectMemory addDependent:self
|
|
51 |
]
|
|
52 |
!
|
|
53 |
|
|
54 |
reOpenFiles
|
|
55 |
"reopen all files (if possible) after a snapShot load"
|
|
56 |
|
|
57 |
lobby contentsDo:[:aFileStream |
|
|
58 |
aFileStream reOpen
|
|
59 |
]
|
|
60 |
!
|
|
61 |
|
|
62 |
update:something
|
|
63 |
"have to reopen files when returning from snapshot"
|
|
64 |
|
|
65 |
something == #returnFromSnapshot ifTrue:[
|
|
66 |
self reOpenFiles
|
|
67 |
]
|
|
68 |
! !
|
|
69 |
|
2
|
70 |
!ExternalStream class methodsFor:'instance creation'!
|
|
71 |
|
|
72 |
new
|
|
73 |
|newStream|
|
|
74 |
|
|
75 |
newStream := self basicNew.
|
12
|
76 |
newStream text; buffered:true; useCRLF:false.
|
2
|
77 |
^ newStream
|
|
78 |
! !
|
|
79 |
|
1
|
80 |
!ExternalStream methodsFor:'instance release'!
|
|
81 |
|
|
82 |
disposed
|
|
83 |
"some Stream has been collected - close the file if not already done"
|
|
84 |
|
|
85 |
self closeFile
|
|
86 |
!
|
|
87 |
|
|
88 |
closeFile
|
|
89 |
"low level close - may be redefined in subclasses"
|
|
90 |
|
|
91 |
%{ /* NOCONTEXT */
|
|
92 |
|
12
|
93 |
if (_INST(filePointer) != nil) {
|
|
94 |
int savInt;
|
|
95 |
extern int _immediateInterrupt;
|
|
96 |
|
|
97 |
savInt = _immediateInterrupt;
|
|
98 |
_immediateInterrupt = 1;
|
|
99 |
fclose(MKFD(_INST(filePointer)));
|
|
100 |
_immediateInterrupt = savInt;
|
|
101 |
}
|
1
|
102 |
%}
|
|
103 |
! !
|
|
104 |
|
|
105 |
!ExternalStream methodsFor:'private'!
|
|
106 |
|
|
107 |
reOpen
|
|
108 |
"sent after snapin to reopen streams.
|
|
109 |
cannot reopen here since I am abstract and have no device knowledge"
|
|
110 |
|
|
111 |
self class name print. ': cannot reOpen stream - stream closed' printNewline.
|
|
112 |
filePointer := nil.
|
|
113 |
lobby unregister:self.
|
|
114 |
! !
|
|
115 |
|
|
116 |
!ExternalStream methodsFor:'error handling'!
|
|
117 |
|
|
118 |
errorNotOpen
|
|
119 |
"report an error, that the stream has not been opened"
|
|
120 |
|
|
121 |
^ self error:(self class name , ' not open')
|
|
122 |
!
|
|
123 |
|
|
124 |
errorReadOnly
|
|
125 |
"report an error, that the stream is a readOnly stream"
|
|
126 |
|
|
127 |
^ self error:(self class name , ' is readonly')
|
|
128 |
!
|
|
129 |
|
|
130 |
errorWriteOnly
|
|
131 |
"report an error, that the stream is a writeOnly stream"
|
|
132 |
|
|
133 |
^ self error:(self class name , ' is writeonly')
|
|
134 |
!
|
|
135 |
|
|
136 |
errorNotBinary
|
|
137 |
"report an error, that the stream is not in binary mode"
|
|
138 |
|
|
139 |
^ self error:(self class name , ' is not in binary mode')
|
|
140 |
!
|
|
141 |
|
2
|
142 |
errorBinary
|
|
143 |
"report an error, that the stream is in binary mode"
|
|
144 |
|
|
145 |
^ self error:(self class name , ' is in binary mode')
|
|
146 |
!
|
|
147 |
|
10
|
148 |
errorNotBuffered
|
|
149 |
"report an error, that the stream is not in buffered mode"
|
|
150 |
|
|
151 |
^ self error:(self class name , ' is unbuffered - operation not allowed')
|
|
152 |
!
|
|
153 |
|
1
|
154 |
argumentMustBeInteger
|
|
155 |
"report an error, that the argument must be an integer"
|
|
156 |
|
|
157 |
^ self error:'argument must be an integer'
|
|
158 |
!
|
|
159 |
|
|
160 |
argumentMustBeCharacter
|
|
161 |
"report an error, that the argument must be a character"
|
|
162 |
|
|
163 |
^ self error:'argument must be a character'
|
|
164 |
!
|
|
165 |
|
|
166 |
argumentMustBeString
|
|
167 |
"report an error, that the argument must be a string"
|
|
168 |
|
|
169 |
^ self error:'argument must be a string'
|
|
170 |
! !
|
|
171 |
|
|
172 |
!ExternalStream methodsFor:'accessing'!
|
|
173 |
|
|
174 |
readonly
|
|
175 |
"set access mode to readonly"
|
|
176 |
|
|
177 |
mode := #readonly
|
|
178 |
!
|
|
179 |
|
|
180 |
writeonly
|
|
181 |
"set access mode to writeonly"
|
|
182 |
|
|
183 |
mode := #writeonly
|
|
184 |
!
|
|
185 |
|
|
186 |
readwrite
|
|
187 |
"set access mode to readwrite"
|
|
188 |
|
|
189 |
mode := #readwrite
|
|
190 |
!
|
|
191 |
|
|
192 |
filePointer
|
|
193 |
"return the filePointer of the receiver -
|
|
194 |
notice: for portability stdio is used; this means you will get
|
10
|
195 |
a FILE * - not a fileDescriptor.
|
|
196 |
(what you really get is a corresponding integer)"
|
1
|
197 |
|
|
198 |
^ filePointer
|
|
199 |
!
|
|
200 |
|
|
201 |
fileDescriptor
|
|
202 |
"return the fileDescriptor of the receiver -
|
|
203 |
notice: this one returns the underlying OSs fileDescriptor -
|
|
204 |
this may not be available on all platforms (i.e. non unix systems)."
|
|
205 |
|
|
206 |
%{ /* NOCONTEXT */
|
|
207 |
|
|
208 |
FILE *f;
|
|
209 |
|
|
210 |
if (_INST(filePointer) != nil) {
|
|
211 |
f = MKFD(_INST(filePointer));
|
|
212 |
RETURN ( MKOBJ(fileno(f)) );
|
|
213 |
}
|
|
214 |
%}
|
|
215 |
.
|
|
216 |
^ self errorNotOpen
|
|
217 |
!
|
|
218 |
|
|
219 |
buffered:aBoolean
|
10
|
220 |
"turn buffering on or off - default is on"
|
1
|
221 |
|
10
|
222 |
buffered := aBoolean
|
1
|
223 |
!
|
|
224 |
|
12
|
225 |
useCRLF:aBoolean
|
|
226 |
"turn on or off CRLF sending (instead of LF only) - default is off"
|
|
227 |
|
|
228 |
useCRLF := aBoolean
|
|
229 |
!
|
|
230 |
|
1
|
231 |
binary
|
10
|
232 |
"switch to binary mode - default is text"
|
1
|
233 |
|
|
234 |
binary := true
|
|
235 |
!
|
|
236 |
|
|
237 |
text
|
10
|
238 |
"switch to text mode - default is text"
|
1
|
239 |
|
|
240 |
binary := false
|
|
241 |
!
|
|
242 |
|
|
243 |
contents
|
2
|
244 |
"return the contents of the file from the current position up-to
|
|
245 |
the end. If the stream is in binary mode, a ByteArray containing
|
|
246 |
the byte values is returned.
|
|
247 |
In text-mode, a collection of strings, each representing one line,
|
|
248 |
is returned."
|
|
249 |
|
|
250 |
|text l chunks sizes chunk byteCount cnt bytes offset|
|
|
251 |
|
|
252 |
binary ifTrue:[
|
|
253 |
"adding to a ByteArray produces quadratic time-space
|
|
254 |
behavior - therefore we allocate chunks, and concatenate them
|
|
255 |
at the end."
|
1
|
256 |
|
2
|
257 |
chunks := VariableArray new.
|
|
258 |
sizes := VariableArray new.
|
|
259 |
byteCount := 0.
|
|
260 |
[self atEnd] whileFalse:[
|
|
261 |
chunk := ByteArray uninitializedNew:4096.
|
|
262 |
cnt := self nextBytes:(chunk size) into:chunk.
|
|
263 |
cnt isNil ifFalse:[
|
|
264 |
chunks add:chunk.
|
|
265 |
sizes add:cnt.
|
|
266 |
byteCount := byteCount + cnt
|
|
267 |
]
|
|
268 |
].
|
|
269 |
"now, create one big ByteArray"
|
|
270 |
bytes := ByteArray uninitializedNew:byteCount.
|
|
271 |
offset := 1.
|
|
272 |
1 to:chunks size do:[:index |
|
|
273 |
chunk := chunks at:index.
|
|
274 |
cnt := sizes at:index.
|
|
275 |
bytes replaceFrom:offset to:(offset + cnt - 1) with:chunk.
|
|
276 |
offset := offset + cnt
|
|
277 |
].
|
|
278 |
^ bytes
|
|
279 |
].
|
1
|
280 |
|
|
281 |
text := Text new.
|
|
282 |
[self atEnd] whileFalse:[
|
2
|
283 |
l := self nextLine.
|
|
284 |
l notNil ifTrue:[
|
|
285 |
text add:l
|
|
286 |
]
|
1
|
287 |
].
|
|
288 |
^ text
|
|
289 |
! !
|
|
290 |
|
2
|
291 |
!ExternalStream methodsFor:'misc functions'!
|
1
|
292 |
|
|
293 |
open
|
|
294 |
"open the stream
|
|
295 |
- this must be redefined in subclass"
|
|
296 |
|
|
297 |
^ self subclassResponsibility
|
|
298 |
!
|
|
299 |
|
2
|
300 |
close
|
|
301 |
"close the stream - tell operating system"
|
|
302 |
|
|
303 |
filePointer isNil ifTrue:[^ self].
|
|
304 |
lobby unregister:self.
|
|
305 |
self closeFile.
|
|
306 |
filePointer := nil
|
|
307 |
!
|
|
308 |
|
1
|
309 |
create
|
|
310 |
"create the stream
|
|
311 |
- this must be redefined in subclass"
|
|
312 |
|
|
313 |
^ self subclassResponsibility
|
|
314 |
!
|
|
315 |
|
|
316 |
position
|
|
317 |
"return the position
|
|
318 |
- this must be redefined in subclass"
|
|
319 |
|
|
320 |
^ self subclassResponsibility
|
|
321 |
!
|
|
322 |
|
|
323 |
position:anInteger
|
|
324 |
"set the position
|
|
325 |
- this must be redefined in subclass"
|
|
326 |
|
|
327 |
^ self subclassResponsibility
|
2
|
328 |
!
|
1
|
329 |
|
10
|
330 |
backStep
|
|
331 |
"step back one element -
|
|
332 |
redefined, since position is redefined here"
|
|
333 |
|
|
334 |
self position:(self position - 1)
|
|
335 |
!
|
|
336 |
|
|
337 |
blocking:aBoolean
|
|
338 |
"set/clear the blocking attribute - if set (which is the default)
|
|
339 |
a read (using next) on the receiver will block until data is available.
|
|
340 |
If cleared, a read operation will immediately return with a value of
|
|
341 |
nil."
|
|
342 |
|
|
343 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
344 |
^ OperatingSystem setBlocking:aBoolean on:(self fileDescriptor)
|
|
345 |
!
|
|
346 |
|
|
347 |
async:aBoolean
|
|
348 |
"set/clear the async attribute - if set the availability of data on
|
|
349 |
the receiver will trigger an ioInterrupt.
|
|
350 |
If cleared (which is the default) no special notification is made."
|
|
351 |
|
|
352 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
353 |
aBoolean ifTrue:[
|
|
354 |
^ OperatingSystem enableIOInterruptsOn:(self fileDescriptor)
|
|
355 |
].
|
|
356 |
^ OperatingSystem disableIOInterruptsOn:(self fileDescriptor)
|
|
357 |
!
|
|
358 |
|
2
|
359 |
ioctl:ioctlNumber
|
|
360 |
"to provide a simple ioctl facility - an ioctl is performed
|
|
361 |
on the underlying file; no arguments are passed."
|
1
|
362 |
|
|
363 |
%{ /* NOCONTEXT */
|
|
364 |
|
|
365 |
FILE *f;
|
|
366 |
int ret, ioNum, ioArg;
|
10
|
367 |
int savInt;
|
2
|
368 |
extern int _immediateInterrupt;
|
1
|
369 |
extern OBJ ErrorNumber;
|
|
370 |
extern errno;
|
|
371 |
|
|
372 |
if (_INST(filePointer) != nil) {
|
2
|
373 |
if (_isSmallInteger(ioctlNumber)) {
|
1
|
374 |
ioNum = _intVal(ioctlNumber);
|
|
375 |
f = MKFD(_INST(filePointer));
|
10
|
376 |
savInt = _immediateInterrupt;
|
2
|
377 |
_immediateInterrupt = 1;
|
12
|
378 |
do {
|
10
|
379 |
ret = ioctl(fileno(f), ioNum);
|
12
|
380 |
} while ((ret < 0) && (errno == EINTR));
|
10
|
381 |
_immediateInterrupt = savInt;
|
1
|
382 |
if (ret >= 0) {
|
|
383 |
RETURN ( _MKSMALLINT(ret) );
|
|
384 |
}
|
|
385 |
ErrorNumber = _MKSMALLINT(errno);
|
|
386 |
RETURN ( nil );
|
|
387 |
}
|
|
388 |
}
|
|
389 |
%}
|
|
390 |
.
|
|
391 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
392 |
self primitiveFailed
|
|
393 |
!
|
|
394 |
|
2
|
395 |
ioctl:ioctlNumber with:arg
|
|
396 |
"to provide a simple ioctl facility - an ioctl is performed
|
|
397 |
on the underlying file; the argument is passed as argument.
|
|
398 |
If the argument is a number, its directly passed; if its a
|
|
399 |
kind of ByteArray (ByteArray, String or Structure) a pointer to
|
10
|
400 |
the data is passed. This allows performing most ioctls - however,
|
|
401 |
it might be tricky to setup the buffer."
|
2
|
402 |
|
|
403 |
|isStructure|
|
|
404 |
|
|
405 |
isStructure := arg isKindOf:ByteArray.
|
|
406 |
%{
|
|
407 |
FILE *f;
|
10
|
408 |
int ret, ioNum, savInt;
|
2
|
409 |
extern int _immediateInterrupt;
|
|
410 |
extern OBJ ErrorNumber;
|
|
411 |
extern errno;
|
|
412 |
|
|
413 |
if (_INST(filePointer) != nil) {
|
|
414 |
if (_isSmallInteger(ioctlNumber)
|
|
415 |
&& (_isSmallInteger(arg) || (isStructure == true))) {
|
|
416 |
f = MKFD(_INST(filePointer));
|
|
417 |
ioNum = _intVal(ioctlNumber);
|
10
|
418 |
savInt = _immediateInterrupt;
|
2
|
419 |
_immediateInterrupt = 1;
|
12
|
420 |
do {
|
10
|
421 |
if (isStructure == true) {
|
|
422 |
ret = ioctl(fileno(f), ioNum, _ByteArrayInstPtr(arg)->ba_element);
|
|
423 |
} else {
|
|
424 |
ret = ioctl(fileno(f), ioNum, _intVal(arg));
|
|
425 |
}
|
12
|
426 |
} while ((ret < 0) && (errno == EINTR));
|
10
|
427 |
_immediateInterrupt = savInt;
|
2
|
428 |
if (ret >= 0) {
|
|
429 |
RETURN ( _MKSMALLINT(ret) );
|
|
430 |
}
|
|
431 |
ErrorNumber = _MKSMALLINT(errno);
|
|
432 |
RETURN ( nil );
|
|
433 |
}
|
|
434 |
}
|
|
435 |
%}
|
|
436 |
.
|
|
437 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
438 |
self primitiveFailed
|
|
439 |
! !
|
|
440 |
|
|
441 |
!ExternalStream methodsFor:'non homogenous reading'!
|
|
442 |
|
1
|
443 |
nextByte
|
2
|
444 |
"read the next byte and return it as an Integer; return nil on error.
|
|
445 |
This is allowed in both text and binary modes."
|
1
|
446 |
|
|
447 |
%{ /* NOCONTEXT */
|
|
448 |
|
|
449 |
FILE *f;
|
|
450 |
unsigned char byte;
|
10
|
451 |
int cnt, savInt;
|
1
|
452 |
extern OBJ ErrorNumber;
|
|
453 |
extern errno;
|
|
454 |
extern int _immediateInterrupt;
|
|
455 |
|
12
|
456 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
457 |
f = MKFD(_INST(filePointer));
|
|
458 |
savInt = _immediateInterrupt;
|
|
459 |
_immediateInterrupt = 1;
|
|
460 |
do {
|
|
461 |
if (_INST(buffered) == false) {
|
|
462 |
cnt = read(fileno(f), &byte, 1);
|
|
463 |
} else {
|
|
464 |
if (_INST(mode) == _readwrite)
|
|
465 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
466 |
cnt = fread(&byte, 1, 1, f);
|
1
|
467 |
}
|
12
|
468 |
} while ((cnt < 0) && (errno == EINTR));
|
|
469 |
_immediateInterrupt = savInt;
|
|
470 |
if (cnt == 1) {
|
|
471 |
if (_INST(position) != nil)
|
|
472 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
|
|
473 |
RETURN ( _MKSMALLINT(byte) );
|
1
|
474 |
}
|
12
|
475 |
if (cnt < 0) {
|
|
476 |
ErrorNumber = _MKSMALLINT(errno);
|
|
477 |
}
|
|
478 |
RETURN ( nil );
|
1
|
479 |
}
|
|
480 |
%}
|
|
481 |
.
|
|
482 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
483 |
self errorWriteOnly
|
|
484 |
!
|
|
485 |
|
|
486 |
nextBytes:count into:anObject
|
|
487 |
"read the next count bytes into an object and return the number of
|
|
488 |
bytes read or nil on error.
|
2
|
489 |
Use with care - non object oriented i/o.
|
|
490 |
You can create bad crashes with this method."
|
1
|
491 |
|
|
492 |
^ self nextBytes:count into:anObject startingAt:1
|
|
493 |
!
|
|
494 |
|
|
495 |
nextBytes:count into:anObject startingAt:start
|
|
496 |
"read the next count bytes into an object and return the number of
|
|
497 |
bytes read or nil on error.
|
2
|
498 |
Use with care - non object oriented i/o.
|
|
499 |
You can create bad crashes with this method."
|
1
|
500 |
|
|
501 |
%{ /* NOCONTEXT */
|
|
502 |
|
|
503 |
FILE *f;
|
|
504 |
int cnt, offs;
|
10
|
505 |
int objSize, savInt;
|
1
|
506 |
char *cp;
|
|
507 |
extern OBJ ErrorNumber;
|
|
508 |
extern errno;
|
|
509 |
OBJ pos;
|
|
510 |
extern int _immediateInterrupt;
|
|
511 |
|
12
|
512 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
513 |
if (_isSmallInteger(count) && _isSmallInteger(start)) {
|
|
514 |
cnt = _intVal(count);
|
|
515 |
offs = _intVal(start) - 1;
|
|
516 |
f = MKFD(_INST(filePointer));
|
|
517 |
objSize = _Size(anObject) - OHDR_SIZE;
|
|
518 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
|
|
519 |
cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
|
|
520 |
savInt = _immediateInterrupt;
|
|
521 |
_immediateInterrupt = 1;
|
|
522 |
do {
|
|
523 |
if (_INST(buffered) == false) {
|
|
524 |
cnt = read(fileno(f), cp, cnt);
|
|
525 |
} else {
|
|
526 |
if (_INST(mode) == _readwrite)
|
|
527 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
528 |
cnt = fread(cp, 1, cnt, f);
|
1
|
529 |
}
|
12
|
530 |
} while ((cnt < 0) && (errno == EINTR));
|
|
531 |
_immediateInterrupt = savInt;
|
|
532 |
if (cnt >= 0) {
|
|
533 |
pos = _INST(position);
|
|
534 |
if (pos != nil)
|
|
535 |
_INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
|
|
536 |
RETURN ( _MKSMALLINT(cnt) );
|
1
|
537 |
}
|
12
|
538 |
ErrorNumber = _MKSMALLINT(errno);
|
|
539 |
RETURN ( nil );
|
1
|
540 |
}
|
|
541 |
}
|
|
542 |
}
|
|
543 |
%}
|
|
544 |
.
|
|
545 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
546 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
547 |
self primitiveFailed
|
|
548 |
!
|
|
549 |
|
2
|
550 |
nextWord
|
|
551 |
"in text-mode:
|
|
552 |
read the next word (i.e. up to non letter-or-digit).
|
|
553 |
return a string containing those characters.
|
|
554 |
in binary-mode:
|
|
555 |
read two bytes (msb-first) and return the value as a 16-bit unsigned Integer
|
|
556 |
(for compatibility with other smalltalks)"
|
|
557 |
|
|
558 |
%{ /* NOCONTEXT */
|
|
559 |
extern int _immediateInterrupt;
|
10
|
560 |
int savInt;
|
2
|
561 |
|
|
562 |
if (_INST(binary) == true) {
|
12
|
563 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
564 |
FILE *f;
|
|
565 |
unsigned char hi, low;
|
|
566 |
int cnt;
|
2
|
567 |
|
12
|
568 |
savInt = _immediateInterrupt;
|
|
569 |
_immediateInterrupt = 1;
|
|
570 |
f = MKFD(_INST(filePointer));
|
|
571 |
do {
|
|
572 |
if (_INST(buffered) == false) {
|
|
573 |
cnt = read(fileno(f), &hi, 1);
|
|
574 |
} else {
|
|
575 |
if (_INST(mode) == _readwrite)
|
|
576 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
577 |
cnt = fread(&hi, 1, 1, f);
|
2
|
578 |
}
|
12
|
579 |
} while ((cnt < 0) && (errno == EINTR));
|
2
|
580 |
|
12
|
581 |
if (cnt < 0) {
|
10
|
582 |
_immediateInterrupt = savInt;
|
12
|
583 |
RETURN ( nil );
|
|
584 |
}
|
|
585 |
do {
|
|
586 |
if (_INST(buffered) == false) {
|
|
587 |
cnt = read(fileno(f), &low, 1);
|
|
588 |
} else {
|
|
589 |
if (_INST(mode) == _readwrite)
|
|
590 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
591 |
cnt = fread(&low, 1, 1, f);
|
2
|
592 |
}
|
12
|
593 |
} while ((cnt < 0) && (errno == EINTR));
|
|
594 |
|
|
595 |
_immediateInterrupt = savInt;
|
|
596 |
if (cnt < 0) {
|
2
|
597 |
if (_INST(position) != nil) {
|
12
|
598 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
|
2
|
599 |
}
|
12
|
600 |
RETURN ( _MKSMALLINT(hi & 0xFF) );
|
2
|
601 |
}
|
12
|
602 |
if (_INST(position) != nil) {
|
|
603 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
|
|
604 |
}
|
|
605 |
RETURN ( _MKSMALLINT(((hi & 0xFF)<<8) | (low & 0xFF)) );
|
2
|
606 |
}
|
|
607 |
}
|
|
608 |
%}
|
|
609 |
.
|
12
|
610 |
%{ /* STACK: 2000 */
|
2
|
611 |
FILE *f;
|
|
612 |
int len;
|
|
613 |
char buffer[1024];
|
10
|
614 |
int ch, savInt;
|
2
|
615 |
int cnt = 0;
|
|
616 |
extern int _immediateInterrupt;
|
|
617 |
|
12
|
618 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
619 |
f = MKFD(_INST(filePointer));
|
|
620 |
savInt = _immediateInterrupt;
|
|
621 |
_immediateInterrupt = 1;
|
2
|
622 |
|
12
|
623 |
/* text-mode */
|
|
624 |
for (;;) {
|
|
625 |
ch = getc(f);
|
|
626 |
cnt++;
|
2
|
627 |
|
12
|
628 |
if (ch >= ' ') break;
|
|
629 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r')
|
|
630 |
&& (ch != '\n') && (ch != 0x0b)) break;
|
|
631 |
}
|
|
632 |
ungetc(ch, f);
|
|
633 |
cnt--;
|
2
|
634 |
|
12
|
635 |
len = 0;
|
|
636 |
for (;;) {
|
|
637 |
ch = getc(f);
|
|
638 |
if (ch == EOF)
|
|
639 |
break;
|
|
640 |
ch &= 0xFF;
|
|
641 |
if (! (((ch >= 'a') && (ch <= 'z')) ||
|
|
642 |
((ch >= 'A') && (ch <= 'Z')) ||
|
|
643 |
((ch >= '0') && (ch <= '9')))) {
|
|
644 |
ungetc(ch, f);
|
|
645 |
break;
|
2
|
646 |
}
|
12
|
647 |
cnt++;
|
|
648 |
buffer[len++] = ch;
|
|
649 |
if (len >= sizeof(buffer)-1) {
|
|
650 |
/* emergency */
|
|
651 |
break;
|
2
|
652 |
}
|
12
|
653 |
}
|
|
654 |
_immediateInterrupt = savInt;
|
|
655 |
|
|
656 |
if (_INST(position) != nil) {
|
|
657 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + cnt);
|
2
|
658 |
}
|
12
|
659 |
buffer[len] = '\0';
|
|
660 |
if (len != 0) {
|
|
661 |
RETURN ( _MKSTRING(buffer COMMA_CON) );
|
|
662 |
}
|
|
663 |
RETURN ( nil );
|
2
|
664 |
}
|
|
665 |
%}
|
|
666 |
.
|
|
667 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
668 |
self errorWriteOnly
|
|
669 |
!
|
|
670 |
|
|
671 |
nextShortMSB:msbFlag
|
|
672 |
"in binary-mode only:
|
|
673 |
read two bytes and return the value as a 16-bit signed Integer.
|
|
674 |
If msbFlag is true, value is read with most-significant byte first, otherwise
|
|
675 |
least-significant byte comes first.
|
|
676 |
A nil is also returned, if endOfFile occurs after the first byte."
|
|
677 |
|
|
678 |
%{ /* NOCONTEXT */
|
10
|
679 |
int savInt;
|
2
|
680 |
extern int _immediateInterrupt;
|
|
681 |
|
|
682 |
if (_INST(binary) == true) {
|
12
|
683 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
684 |
FILE *f;
|
|
685 |
int first, second;
|
|
686 |
short value;
|
2
|
687 |
|
12
|
688 |
savInt = _immediateInterrupt;
|
|
689 |
_immediateInterrupt = 1;
|
2
|
690 |
|
12
|
691 |
f = MKFD(_INST(filePointer));
|
|
692 |
first = getc(f);
|
|
693 |
if (first == EOF) {
|
10
|
694 |
_immediateInterrupt = savInt;
|
12
|
695 |
RETURN ( nil );
|
|
696 |
}
|
|
697 |
second = getc(f);
|
|
698 |
_immediateInterrupt = savInt;
|
2
|
699 |
|
12
|
700 |
if (second == EOF) {
|
|
701 |
RETURN ( nil );
|
2
|
702 |
}
|
12
|
703 |
if (_INST(position) != nil) {
|
|
704 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
|
|
705 |
}
|
|
706 |
if (msbFlag == true) {
|
|
707 |
RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
|
|
708 |
}
|
|
709 |
RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
|
2
|
710 |
}
|
|
711 |
}
|
|
712 |
%}
|
|
713 |
.
|
|
714 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
715 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
716 |
binary ifFalse:[^ self errorNotBinary].
|
|
717 |
self primitiveFailed
|
|
718 |
!
|
|
719 |
|
|
720 |
nextUnsignedShortMSB:msbFlag
|
|
721 |
"in binary-mode only:
|
|
722 |
read two bytes and return the value as a 16-bit unsigned Integer.
|
|
723 |
If msbFlag is true, value is read with most-significant byte first, otherwise
|
|
724 |
least-significant byte comes first.
|
|
725 |
A nil is also returned, if endOfFile occurs after the first byte."
|
|
726 |
|
|
727 |
%{ /* NOCONTEXT */
|
10
|
728 |
int savInt;
|
2
|
729 |
extern int _immediateInterrupt;
|
|
730 |
|
|
731 |
if (_INST(binary) == true) {
|
12
|
732 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
733 |
FILE *f;
|
|
734 |
int first, second;
|
2
|
735 |
|
12
|
736 |
savInt = _immediateInterrupt;
|
|
737 |
_immediateInterrupt = 1;
|
|
738 |
f = MKFD(_INST(filePointer));
|
|
739 |
first = getc(f);
|
|
740 |
if (first == EOF) {
|
10
|
741 |
_immediateInterrupt = savInt;
|
12
|
742 |
RETURN ( nil );
|
|
743 |
}
|
|
744 |
second = getc(f);
|
|
745 |
_immediateInterrupt = savInt;
|
2
|
746 |
|
12
|
747 |
if (second == EOF) {
|
|
748 |
RETURN ( nil );
|
2
|
749 |
}
|
12
|
750 |
if (_INST(position) != nil) {
|
|
751 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
|
|
752 |
}
|
|
753 |
if (msbFlag == true) {
|
|
754 |
RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
|
|
755 |
}
|
|
756 |
RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
|
2
|
757 |
}
|
|
758 |
}
|
|
759 |
%}
|
|
760 |
.
|
|
761 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
762 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
763 |
binary ifFalse:[^ self errorNotBinary].
|
|
764 |
self primitiveFailed
|
|
765 |
!
|
|
766 |
|
|
767 |
nextLongMSB:msbFlag
|
|
768 |
"in binary-mode only:
|
|
769 |
read four bytes and return the value as a 32-bit signed Integer, which may be
|
|
770 |
a LargeInteger.
|
|
771 |
If msbFlag is true, value is read with most-significant byte first, otherwise
|
|
772 |
least-significant byte comes first.
|
|
773 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read."
|
|
774 |
|
|
775 |
%{ /* NOCONTEXT */
|
10
|
776 |
int savInt;
|
2
|
777 |
extern int _immediateInterrupt;
|
|
778 |
|
|
779 |
if (_INST(binary) == true) {
|
12
|
780 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
781 |
FILE *f;
|
|
782 |
int first, second, third, fourth;
|
|
783 |
int value;
|
2
|
784 |
|
12
|
785 |
savInt = _immediateInterrupt;
|
|
786 |
_immediateInterrupt = 1;
|
|
787 |
f = MKFD(_INST(filePointer));
|
|
788 |
first = getc(f);
|
|
789 |
if (first == EOF) {
|
|
790 |
_immediateInterrupt = savInt;
|
|
791 |
RETURN ( nil );
|
|
792 |
}
|
|
793 |
second = getc(f);
|
|
794 |
if (second == EOF) {
|
|
795 |
_immediateInterrupt = savInt;
|
|
796 |
RETURN ( nil );
|
|
797 |
}
|
|
798 |
third = getc(f);
|
|
799 |
if (third == EOF) {
|
10
|
800 |
_immediateInterrupt = savInt;
|
12
|
801 |
RETURN ( nil );
|
|
802 |
}
|
|
803 |
fourth = getc(f);
|
|
804 |
_immediateInterrupt = savInt;
|
|
805 |
if (fourth == EOF) {
|
|
806 |
RETURN ( nil );
|
|
807 |
}
|
|
808 |
if (_INST(position) != nil) {
|
|
809 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
|
2
|
810 |
}
|
12
|
811 |
if (msbFlag == true) {
|
|
812 |
value = ((first & 0xFF) << 24)
|
|
813 |
| ((second & 0xFF) << 16)
|
|
814 |
| ((third & 0xFF) << 8)
|
|
815 |
| (fourth & 0xFF);
|
|
816 |
} else {
|
|
817 |
value = ((fourth & 0xFF) << 24)
|
|
818 |
| ((third & 0xFF) << 16)
|
|
819 |
| ((second & 0xFF) << 8)
|
|
820 |
| (first & 0xFF);
|
|
821 |
}
|
|
822 |
if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
|
|
823 |
RETURN ( _MKSMALLINT(value));
|
|
824 |
}
|
|
825 |
RETURN ( _makeLarge(value) );
|
2
|
826 |
}
|
|
827 |
}
|
|
828 |
%}
|
|
829 |
.
|
|
830 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
831 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
832 |
binary ifFalse:[^ self errorNotBinary].
|
|
833 |
self primitiveFailed
|
|
834 |
!
|
|
835 |
|
|
836 |
nextUnsignedLongMSB:msbFlag
|
|
837 |
"in binary-mode only:
|
|
838 |
read four bytes and return the value as a 32-bit unsigned Integer, which may be
|
|
839 |
a LargeInteger.
|
|
840 |
If msbFlag is true, value is read with most-significant byte first, otherwise
|
|
841 |
least-significant byte comes first.
|
|
842 |
A nil is returned, if endOfFile occurs before all 4 bytes have been read."
|
|
843 |
|
|
844 |
%{ /* NOCONTEXT */
|
10
|
845 |
int savInt;
|
2
|
846 |
extern int _immediateInterrupt;
|
|
847 |
extern OBJ _makeULarge();
|
|
848 |
|
|
849 |
if (_INST(binary) == true) {
|
12
|
850 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
851 |
FILE *f;
|
|
852 |
int first, second, third, fourth;
|
|
853 |
unsigned int value;
|
2
|
854 |
|
12
|
855 |
savInt = _immediateInterrupt;
|
|
856 |
_immediateInterrupt = 1;
|
|
857 |
f = MKFD(_INST(filePointer));
|
|
858 |
first = getc(f);
|
|
859 |
if (first == EOF) {
|
|
860 |
_immediateInterrupt = savInt;
|
|
861 |
RETURN ( nil );
|
|
862 |
}
|
|
863 |
second = getc(f);
|
|
864 |
if (second == EOF) {
|
|
865 |
_immediateInterrupt = savInt;
|
|
866 |
RETURN ( nil );
|
|
867 |
}
|
|
868 |
third = getc(f);
|
|
869 |
if (third == EOF) {
|
10
|
870 |
_immediateInterrupt = savInt;
|
12
|
871 |
RETURN ( nil );
|
|
872 |
}
|
|
873 |
fourth = getc(f);
|
|
874 |
_immediateInterrupt = savInt;
|
|
875 |
if (fourth == EOF) {
|
|
876 |
RETURN ( nil );
|
|
877 |
}
|
|
878 |
if (_INST(position) != nil) {
|
|
879 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
|
2
|
880 |
}
|
12
|
881 |
if (msbFlag == true) {
|
|
882 |
value = ((first & 0xFF) << 24)
|
|
883 |
| ((second & 0xFF) << 16)
|
|
884 |
| ((third & 0xFF) << 8)
|
|
885 |
| (fourth & 0xFF);
|
|
886 |
} else {
|
|
887 |
value = ((fourth & 0xFF) << 24)
|
|
888 |
| ((third & 0xFF) << 16)
|
|
889 |
| ((second & 0xFF) << 8)
|
|
890 |
| (first & 0xFF);
|
|
891 |
}
|
|
892 |
if (value <= _MAX_INT) {
|
|
893 |
RETURN ( _MKSMALLINT(value));
|
|
894 |
}
|
|
895 |
RETURN ( _makeULarge(value) );
|
2
|
896 |
}
|
|
897 |
}
|
|
898 |
%}
|
|
899 |
.
|
|
900 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
901 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
902 |
binary ifFalse:[^ self errorNotBinary].
|
|
903 |
self primitiveFailed
|
|
904 |
!
|
|
905 |
|
|
906 |
nextLong
|
|
907 |
"in binary-mode only:
|
|
908 |
read four bytes (msb-first) and return the value as a 32-bit unsigned Integer.
|
|
909 |
The returned value may be a LargeInteger.
|
|
910 |
(msb-first for compatibility with other smalltalks)"
|
|
911 |
|
|
912 |
^ self nextUnsignedLongMSB:true
|
|
913 |
! !
|
|
914 |
|
|
915 |
!ExternalStream methodsFor:'non homogenous writing'!
|
|
916 |
|
1
|
917 |
nextPutByte:aByteValue
|
|
918 |
"write a byte"
|
|
919 |
|
|
920 |
%{ /* NOCONTEXT */
|
|
921 |
|
|
922 |
FILE *f;
|
|
923 |
char c;
|
|
924 |
extern OBJ ErrorNumber;
|
|
925 |
extern errno;
|
|
926 |
OBJ pos;
|
10
|
927 |
int cnt, savInt;
|
1
|
928 |
extern int _immediateInterrupt;
|
|
929 |
|
12
|
930 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
|
|
931 |
if (_isSmallInteger(aByteValue)) {
|
|
932 |
c = _intVal(aByteValue);
|
|
933 |
f = MKFD(_INST(filePointer));
|
|
934 |
savInt = _immediateInterrupt;
|
|
935 |
_immediateInterrupt = 1;
|
|
936 |
#ifdef OLD
|
|
937 |
if (_INST(buffered) == false) {
|
|
938 |
cnt = write(fileno(f), &c, 1);
|
|
939 |
} else
|
|
940 |
#endif
|
|
941 |
{
|
|
942 |
if (_INST(mode) == _readwrite)
|
|
943 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
944 |
cnt = fwrite(&c, 1, 1, f);
|
|
945 |
#ifndef OLD
|
10
|
946 |
if (_INST(buffered) == false) {
|
12
|
947 |
fflush(f);
|
10
|
948 |
}
|
12
|
949 |
#endif
|
1
|
950 |
}
|
12
|
951 |
_immediateInterrupt = savInt;
|
|
952 |
if (cnt == 1) {
|
|
953 |
pos = _INST(position);
|
|
954 |
if (pos != nil)
|
|
955 |
_INST(position) = _MKSMALLINT(_intVal(pos) + 1);
|
|
956 |
RETURN ( self );
|
|
957 |
}
|
|
958 |
ErrorNumber = _MKSMALLINT(errno);
|
|
959 |
RETURN (nil);
|
1
|
960 |
}
|
|
961 |
}
|
|
962 |
%}
|
|
963 |
.
|
|
964 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
965 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
966 |
self primitiveFailed
|
|
967 |
!
|
|
968 |
|
|
969 |
nextPutBytes:count from:anObject
|
|
970 |
"write count bytes from an object starting at index start.
|
|
971 |
return the number of bytes written or nil on error.
|
|
972 |
Use with care - non object oriented i/o"
|
|
973 |
|
|
974 |
^ self nextPutBytes:count from:anObject startingAt:1
|
|
975 |
!
|
|
976 |
|
|
977 |
nextPutBytes:count from:anObject startingAt:start
|
|
978 |
"write count bytes from an object starting at index start.
|
|
979 |
return the number of bytes written or nil on error.
|
|
980 |
Use with care - non object oriented i/o"
|
|
981 |
|
|
982 |
%{ /* NOCONTEXT */
|
|
983 |
|
|
984 |
FILE *f;
|
|
985 |
int cnt, offs;
|
|
986 |
int objSize;
|
|
987 |
char *cp;
|
|
988 |
extern OBJ ErrorNumber;
|
|
989 |
extern errno;
|
|
990 |
OBJ pos;
|
10
|
991 |
int savInt;
|
1
|
992 |
extern int _immediateInterrupt;
|
|
993 |
|
|
994 |
if (_INST(filePointer) != nil) {
|
|
995 |
if (_INST(mode) != _readonly) {
|
|
996 |
if (_isSmallInteger(count) && _isSmallInteger(start)) {
|
|
997 |
cnt = _intVal(count);
|
|
998 |
offs = _intVal(start) - 1;
|
|
999 |
f = MKFD(_INST(filePointer));
|
|
1000 |
|
|
1001 |
objSize = _Size(anObject) - OHDR_SIZE;
|
|
1002 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
|
|
1003 |
cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
|
10
|
1004 |
savInt = _immediateInterrupt;
|
1
|
1005 |
_immediateInterrupt = 1;
|
12
|
1006 |
#ifdef OLD
|
10
|
1007 |
if (_INST(buffered) == false) {
|
1
|
1008 |
cnt = write(fileno(f), cp, cnt);
|
12
|
1009 |
} else
|
|
1010 |
#endif
|
|
1011 |
{
|
2
|
1012 |
if (_INST(mode) == _readwrite)
|
|
1013 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
1014 |
cnt = fwrite(cp, 1, cnt, f);
|
|
1015 |
}
|
12
|
1016 |
#ifndef OLD
|
|
1017 |
if (_INST(buffered) == false) {
|
|
1018 |
fflush(f);
|
|
1019 |
}
|
|
1020 |
#endif
|
10
|
1021 |
_immediateInterrupt = savInt;
|
1
|
1022 |
if (cnt >= 0) {
|
|
1023 |
pos = _INST(position);
|
|
1024 |
if (pos != nil)
|
|
1025 |
_INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
|
|
1026 |
RETURN ( _MKSMALLINT(cnt) );
|
|
1027 |
}
|
|
1028 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1029 |
RETURN ( nil );
|
|
1030 |
}
|
|
1031 |
}
|
|
1032 |
}
|
|
1033 |
}
|
|
1034 |
%}
|
|
1035 |
.
|
|
1036 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1037 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1038 |
self primitiveFailed
|
2
|
1039 |
!
|
|
1040 |
|
|
1041 |
nextWordPut:aNumber
|
|
1042 |
"only in binary-mode:
|
|
1043 |
write the argument, aNumber as a short (two bytes);
|
|
1044 |
write msb-first for compatibility with other smalltalks."
|
|
1045 |
|
|
1046 |
^ self nextShortPut:aNumber MSB:true
|
|
1047 |
!
|
|
1048 |
|
|
1049 |
nextShortPut:aNumber MSB:msbFlag
|
|
1050 |
"only in binary-mode:
|
|
1051 |
write the argument, aNumber as a short (two bytes). If msbFlag is
|
|
1052 |
true, data is written most-significant byte first; otherwise least
|
|
1053 |
first. Returns the receiver on ok, nil on error."
|
|
1054 |
|
|
1055 |
%{ /* NOCONTEXT */
|
|
1056 |
|
|
1057 |
int num;
|
|
1058 |
char bytes[2];
|
|
1059 |
FILE *f;
|
|
1060 |
extern OBJ ErrorNumber;
|
|
1061 |
extern errno;
|
10
|
1062 |
int savInt;
|
2
|
1063 |
extern int _immediateInterrupt;
|
10
|
1064 |
int cnt;
|
2
|
1065 |
|
|
1066 |
if (_INST(binary) == true) {
|
|
1067 |
if (_INST(filePointer) != nil) {
|
|
1068 |
if (_INST(mode) != _readonly) {
|
|
1069 |
if (_isSmallInteger(aNumber)) {
|
|
1070 |
num = _intVal(aNumber);
|
|
1071 |
if (msbFlag == true) {
|
|
1072 |
bytes[0] = (num >> 8) & 0xFF;
|
|
1073 |
bytes[1] = num & 0xFF;
|
|
1074 |
} else {
|
|
1075 |
bytes[1] = (num >> 8) & 0xFF;
|
|
1076 |
bytes[0] = num & 0xFF;
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
f = MKFD(_INST(filePointer));
|
10
|
1080 |
savInt = _immediateInterrupt;
|
2
|
1081 |
_immediateInterrupt = 1;
|
12
|
1082 |
#ifdef OLD
|
10
|
1083 |
if (_INST(buffered) == false) {
|
|
1084 |
cnt = write(fileno(f), bytes, 2);
|
12
|
1085 |
} else
|
|
1086 |
#endif
|
|
1087 |
{
|
10
|
1088 |
if (_INST(mode) == _readwrite)
|
|
1089 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1090 |
cnt = fwrite(bytes, 1, 2, f);
|
|
1091 |
}
|
12
|
1092 |
#ifndef OLD
|
|
1093 |
if (_INST(buffered) == false) {
|
|
1094 |
fflush(f);
|
|
1095 |
}
|
|
1096 |
#endif
|
10
|
1097 |
_immediateInterrupt = savInt;
|
|
1098 |
if (cnt == 2) {
|
2
|
1099 |
if (_INST(position) != nil) {
|
|
1100 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
|
|
1101 |
}
|
|
1102 |
RETURN ( self );
|
|
1103 |
}
|
|
1104 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1105 |
return ( nil );
|
|
1106 |
}
|
|
1107 |
}
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
%}
|
|
1111 |
.
|
|
1112 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1113 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1114 |
binary ifFalse:[^ self errorNotBinary].
|
|
1115 |
self argumentMustBeInteger
|
|
1116 |
!
|
|
1117 |
|
|
1118 |
nextLongPut:aNumber MSB:msbFlag
|
|
1119 |
"only in binary-mode:
|
|
1120 |
write the argument, aNumber as a long (four bytes). If msbFlag is
|
|
1121 |
true, data is written most-significant byte first; otherwise least
|
|
1122 |
first. Returns the receiver on ok, nil on error."
|
|
1123 |
|
|
1124 |
%{ /* NOCONTEXT */
|
|
1125 |
|
|
1126 |
int num;
|
|
1127 |
char bytes[4];
|
|
1128 |
FILE *f;
|
|
1129 |
extern OBJ ErrorNumber;
|
|
1130 |
extern errno;
|
10
|
1131 |
int cnt, savInt;
|
2
|
1132 |
extern int _immediateInterrupt;
|
|
1133 |
|
|
1134 |
if (_INST(binary) == true) {
|
|
1135 |
if (_INST(filePointer) != nil) {
|
|
1136 |
if (_INST(mode) != _readonly) {
|
|
1137 |
if (_isSmallInteger(aNumber)) {
|
|
1138 |
num = _intVal(aNumber);
|
|
1139 |
if (msbFlag == true) {
|
|
1140 |
bytes[0] = (num >> 24) & 0xFF;
|
|
1141 |
bytes[1] = (num >> 16) & 0xFF;
|
|
1142 |
bytes[2] = (num >> 8) & 0xFF;
|
|
1143 |
bytes[3] = num & 0xFF;
|
|
1144 |
} else {
|
|
1145 |
bytes[3] = (num >> 24) & 0xFF;
|
|
1146 |
bytes[2] = (num >> 16) & 0xFF;
|
|
1147 |
bytes[1] = (num >> 8) & 0xFF;
|
|
1148 |
bytes[0] = num & 0xFF;
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
f = MKFD(_INST(filePointer));
|
10
|
1152 |
savInt = _immediateInterrupt;
|
2
|
1153 |
_immediateInterrupt = 1;
|
12
|
1154 |
#ifdef OLD
|
10
|
1155 |
if (_INST(buffered) == false) {
|
|
1156 |
cnt = write(fileno(f), bytes, 4);
|
12
|
1157 |
} else
|
|
1158 |
#endif
|
|
1159 |
{
|
10
|
1160 |
cnt = fwrite(bytes, 1, 4, f);
|
|
1161 |
}
|
12
|
1162 |
#ifndef OLD
|
|
1163 |
if (_INST(buffered) == false) {
|
|
1164 |
fflush(f);
|
|
1165 |
}
|
|
1166 |
#endif
|
10
|
1167 |
_immediateInterrupt = savInt;
|
|
1168 |
if (cnt == 4) {
|
2
|
1169 |
if (_INST(position) != nil) {
|
|
1170 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
|
|
1171 |
}
|
|
1172 |
RETURN ( self );
|
|
1173 |
}
|
|
1174 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1175 |
return ( nil );
|
|
1176 |
}
|
|
1177 |
}
|
|
1178 |
}
|
|
1179 |
}
|
|
1180 |
%}
|
|
1181 |
.
|
|
1182 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1183 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1184 |
binary ifFalse:[^ self errorNotBinary].
|
|
1185 |
|
12
|
1186 |
aNumber isInteger ifTrue:[
|
2
|
1187 |
(self nextShortPut:(aNumber // 16r10000) MSB:msbFlag) isNil ifTrue:[^ nil].
|
|
1188 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:msbFlag.
|
|
1189 |
].
|
|
1190 |
self argumentMustBeInteger
|
1
|
1191 |
! !
|
|
1192 |
|
2
|
1193 |
!ExternalStream methodsFor:'reading'!
|
1
|
1194 |
|
|
1195 |
peek
|
2
|
1196 |
"return the element to be read next without advancing read position.
|
|
1197 |
In binary mode, an integer is returned, otherwise a character.
|
10
|
1198 |
If there are no more elements, nil is returned.
|
|
1199 |
Not allowed in unbuffered mode."
|
1
|
1200 |
|
|
1201 |
%{ /* NOCONTEXT */
|
|
1202 |
|
|
1203 |
FILE *f;
|
|
1204 |
REGISTER int c;
|
10
|
1205 |
int savInt;
|
1
|
1206 |
extern int _immediateInterrupt;
|
|
1207 |
|
|
1208 |
if (_INST(filePointer) != nil) {
|
|
1209 |
if (_INST(mode) != _writeonly) {
|
12
|
1210 |
#ifdef OLD
|
|
1211 |
if (_INST(buffered) == true)
|
|
1212 |
#endif
|
|
1213 |
{
|
10
|
1214 |
f = MKFD(_INST(filePointer));
|
|
1215 |
savInt = _immediateInterrupt;
|
|
1216 |
_immediateInterrupt = 1;
|
|
1217 |
c = getc(f);
|
|
1218 |
_immediateInterrupt = savInt;
|
|
1219 |
if (c != EOF) {
|
|
1220 |
ungetc(c, f);
|
|
1221 |
if (_INST(binary) == true) {
|
|
1222 |
RETURN ( _MKSMALLINT(c & 0xFF) );
|
|
1223 |
}
|
|
1224 |
RETURN ( _MKCHARACTER(c & 0xFF) );
|
1
|
1225 |
}
|
10
|
1226 |
RETURN ( nil );
|
1
|
1227 |
}
|
|
1228 |
}
|
|
1229 |
}
|
|
1230 |
%}
|
|
1231 |
.
|
|
1232 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
10
|
1233 |
buffered ifFalse:[^ self errorNotBuffered].
|
1
|
1234 |
self errorWriteOnly
|
|
1235 |
!
|
|
1236 |
|
|
1237 |
next
|
2
|
1238 |
"return the next element; advance read position.
|
|
1239 |
In binary mode, an integer is returned, otherwise a character.
|
|
1240 |
If there are no more elements, nil is returned."
|
1
|
1241 |
|
|
1242 |
%{ /* NOCONTEXT */
|
|
1243 |
|
|
1244 |
FILE *f;
|
|
1245 |
int c;
|
|
1246 |
OBJ pos;
|
10
|
1247 |
int savInt;
|
|
1248 |
unsigned char ch;
|
|
1249 |
|
1
|
1250 |
extern int _immediateInterrupt;
|
|
1251 |
|
|
1252 |
if (_INST(filePointer) != nil) {
|
|
1253 |
if (_INST(mode) != _writeonly) {
|
|
1254 |
f = MKFD(_INST(filePointer));
|
10
|
1255 |
savInt = _immediateInterrupt;
|
1
|
1256 |
_immediateInterrupt = 1;
|
12
|
1257 |
do {
|
|
1258 |
#ifdef OLD
|
10
|
1259 |
if (_INST(buffered) == false) {
|
|
1260 |
if (read(fileno(f), &ch, 1) != 1)
|
|
1261 |
c = EOF;
|
|
1262 |
else
|
|
1263 |
c = ch;
|
12
|
1264 |
} else
|
|
1265 |
#endif
|
|
1266 |
{
|
10
|
1267 |
if (_INST(mode) == _readwrite)
|
|
1268 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1269 |
c = getc(f);
|
|
1270 |
}
|
12
|
1271 |
} while ((c < 0) && (errno == EINTR));
|
10
|
1272 |
|
|
1273 |
_immediateInterrupt = savInt;
|
1
|
1274 |
if (c != EOF) {
|
|
1275 |
pos = _INST(position);
|
2
|
1276 |
if (_isSmallInteger(pos)) {
|
1
|
1277 |
_INST(position) = _MKSMALLINT(_intVal(pos) + 1);
|
2
|
1278 |
} else {
|
|
1279 |
_INST(position) = nil;
|
1
|
1280 |
}
|
|
1281 |
if (_INST(binary) == true) {
|
|
1282 |
RETURN ( _MKSMALLINT(c & 0xFF) );
|
|
1283 |
}
|
|
1284 |
RETURN ( _MKCHARACTER(c & 0xFF) );
|
|
1285 |
}
|
2
|
1286 |
_INST(position) = nil;
|
1
|
1287 |
RETURN ( nil );
|
|
1288 |
}
|
|
1289 |
}
|
|
1290 |
%}
|
|
1291 |
.
|
|
1292 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1293 |
self errorWriteOnly
|
|
1294 |
!
|
|
1295 |
|
2
|
1296 |
next:count
|
|
1297 |
"return the next count elements of the stream as a collection.
|
|
1298 |
Redefined to return a String or ByteArray instead of the default Array."
|
|
1299 |
|
|
1300 |
|coll|
|
|
1301 |
|
|
1302 |
binary ifTrue:[
|
|
1303 |
coll := ByteArray new:count
|
|
1304 |
] ifFalse:[
|
|
1305 |
coll := String new:count
|
|
1306 |
].
|
|
1307 |
1 to:count do: [:index |
|
|
1308 |
coll at:index put:(self next)
|
|
1309 |
].
|
|
1310 |
^ coll
|
|
1311 |
! !
|
|
1312 |
|
|
1313 |
!ExternalStream methodsFor:'writing'!
|
|
1314 |
|
10
|
1315 |
synchronizeOutput
|
|
1316 |
"write all buffered data - for buffered mode only"
|
|
1317 |
|
|
1318 |
%{ /* NOCONTEXT */
|
|
1319 |
|
|
1320 |
if (_INST(filePointer) != nil) {
|
|
1321 |
if (_INST(mode) != _readonly) {
|
|
1322 |
if (_INST(buffered) == true) {
|
12
|
1323 |
fflush( MKFD(_INST(filePointer)) );
|
10
|
1324 |
}
|
|
1325 |
}
|
|
1326 |
}
|
|
1327 |
%}
|
|
1328 |
!
|
|
1329 |
|
1
|
1330 |
nextPut:aCharacter
|
|
1331 |
"write the argument, aCharacter - return nil if failed, self if ok"
|
|
1332 |
|
|
1333 |
%{ /* NOCONTEXT */
|
|
1334 |
|
|
1335 |
FILE *f;
|
|
1336 |
char c;
|
|
1337 |
extern OBJ ErrorNumber;
|
|
1338 |
extern errno;
|
|
1339 |
int cnt;
|
|
1340 |
OBJ pos;
|
10
|
1341 |
int savInt;
|
1
|
1342 |
extern int _immediateInterrupt;
|
|
1343 |
|
|
1344 |
if (_INST(filePointer) != nil) {
|
|
1345 |
if (_INST(mode) != _readonly) {
|
2
|
1346 |
if (_INST(binary) != true) {
|
|
1347 |
if (_isCharacter(aCharacter)) {
|
|
1348 |
c = _intVal(_CharacterInstPtr(aCharacter)->c_asciivalue);
|
1
|
1349 |
doWrite:
|
2
|
1350 |
f = MKFD(_INST(filePointer));
|
1
|
1351 |
|
10
|
1352 |
savInt = _immediateInterrupt;
|
2
|
1353 |
_immediateInterrupt = 1;
|
12
|
1354 |
#ifdef OLD
|
10
|
1355 |
if (_INST(buffered) == false) {
|
2
|
1356 |
cnt = write(fileno(f), &c, 1);
|
12
|
1357 |
} else
|
|
1358 |
#endif
|
|
1359 |
{
|
2
|
1360 |
if (_INST(mode) == _readwrite)
|
|
1361 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1362 |
cnt = fwrite(&c, 1, 1, f);
|
1
|
1363 |
}
|
12
|
1364 |
#ifndef OLD
|
|
1365 |
if (_INST(buffered) == false) {
|
|
1366 |
fflush(f);
|
|
1367 |
}
|
|
1368 |
#endif
|
10
|
1369 |
_immediateInterrupt = savInt;
|
2
|
1370 |
if (cnt == 1) {
|
|
1371 |
pos = _INST(position);
|
|
1372 |
if (pos != nil) {
|
|
1373 |
_INST(position) = _MKSMALLINT(_intVal(pos) + 1);
|
|
1374 |
}
|
|
1375 |
RETURN ( self );
|
1
|
1376 |
}
|
2
|
1377 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1378 |
RETURN ( nil );
|
1
|
1379 |
}
|
|
1380 |
} else {
|
2
|
1381 |
if (_isSmallInteger(aCharacter)) {
|
|
1382 |
c = _intVal(aCharacter);
|
|
1383 |
goto doWrite;
|
1
|
1384 |
}
|
|
1385 |
}
|
|
1386 |
}
|
|
1387 |
}
|
|
1388 |
%}
|
|
1389 |
.
|
|
1390 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1391 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
2
|
1392 |
binary ifFalse:[^ self argumentMustBeCharacter].
|
|
1393 |
^ self argumentMustBeInteger.
|
1
|
1394 |
!
|
|
1395 |
|
|
1396 |
nextPutAll:aCollection
|
|
1397 |
"write all elements of the argument, aCollection"
|
|
1398 |
|
|
1399 |
%{ /* NOCONTEXT */
|
|
1400 |
|
|
1401 |
FILE *f;
|
|
1402 |
unsigned char *cp;
|
|
1403 |
int len, cnt;
|
|
1404 |
extern OBJ ErrorNumber;
|
|
1405 |
extern errno;
|
|
1406 |
OBJ pos;
|
10
|
1407 |
int savInt;
|
1
|
1408 |
extern int _immediateInterrupt;
|
|
1409 |
|
2
|
1410 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
|
|
1411 |
cp = NULL;
|
10
|
1412 |
if (_isString(aCollection) || _isSymbol(aCollection)) {
|
|
1413 |
cp = _stringVal(aCollection);
|
|
1414 |
len = _stringSize(aCollection);
|
2
|
1415 |
} else {
|
10
|
1416 |
if (_INST(binary) == true) {
|
|
1417 |
if (_isByteArray(aCollection)) {
|
|
1418 |
cp = _ByteArrayInstPtr(aCollection)->ba_element;
|
|
1419 |
len = _byteArraySize(aCollection);
|
|
1420 |
}
|
2
|
1421 |
}
|
|
1422 |
}
|
|
1423 |
if (cp != NULL) {
|
|
1424 |
f = MKFD(_INST(filePointer));
|
1
|
1425 |
|
10
|
1426 |
savInt = _immediateInterrupt;
|
2
|
1427 |
_immediateInterrupt = 1;
|
12
|
1428 |
#ifdef OLD
|
10
|
1429 |
if (_INST(buffered) == false) {
|
2
|
1430 |
cnt = write(fileno(f), cp, len);
|
12
|
1431 |
} else
|
|
1432 |
#endif
|
|
1433 |
{
|
2
|
1434 |
if (_INST(mode) == _readwrite)
|
|
1435 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1436 |
cnt = fwrite(cp, 1, len, f);
|
|
1437 |
}
|
12
|
1438 |
#ifndef OLD
|
|
1439 |
if (_INST(buffered) == false) {
|
|
1440 |
fflush(f);
|
|
1441 |
}
|
|
1442 |
#endif
|
10
|
1443 |
_immediateInterrupt = savInt;
|
2
|
1444 |
if (cnt == len) {
|
|
1445 |
pos = _INST(position);
|
|
1446 |
if (pos != nil) {
|
|
1447 |
_INST(position) = _MKSMALLINT(_intVal(pos) + len);
|
1
|
1448 |
}
|
2
|
1449 |
RETURN ( self );
|
1
|
1450 |
}
|
2
|
1451 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1452 |
RETURN ( nil );
|
1
|
1453 |
}
|
|
1454 |
}
|
|
1455 |
%}
|
|
1456 |
.
|
|
1457 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1458 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1459 |
|
|
1460 |
aCollection do:[:element |
|
|
1461 |
self nextPut:element
|
|
1462 |
]
|
|
1463 |
!
|
|
1464 |
|
|
1465 |
nextPut:aCollection from:start to:stop
|
|
1466 |
"write a range of elements of the argument, aCollection"
|
|
1467 |
|
|
1468 |
%{ /* NOCONTEXT */
|
|
1469 |
|
|
1470 |
FILE *f;
|
|
1471 |
unsigned char *cp;
|
|
1472 |
int len, cnt, index1, index2;
|
|
1473 |
extern OBJ ErrorNumber;
|
|
1474 |
extern errno;
|
10
|
1475 |
int savInt;
|
1
|
1476 |
extern int _immediateInterrupt;
|
|
1477 |
|
2
|
1478 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
|
|
1479 |
if (_isSmallInteger(start) && _isSmallInteger(stop)) {
|
|
1480 |
cp = NULL;
|
|
1481 |
if (_INST(binary) != true) {
|
|
1482 |
if (_isString(aCollection) || _isSymbol(aCollection)) {
|
|
1483 |
cp = _stringVal(aCollection);
|
|
1484 |
len = _stringSize(aCollection);
|
|
1485 |
}
|
|
1486 |
} else {
|
|
1487 |
if (_isByteArray(aCollection)) {
|
|
1488 |
cp = _ByteArrayInstPtr(aCollection)->ba_element;
|
|
1489 |
len = _byteArraySize(aCollection);
|
|
1490 |
}
|
1
|
1491 |
}
|
2
|
1492 |
if (cp != NULL) {
|
|
1493 |
f = MKFD(_INST(filePointer));
|
|
1494 |
index1 = _intVal(start);
|
|
1495 |
index2 = _intVal(stop);
|
|
1496 |
if ((index1 < 1) || (index2 > len) || (index2 < index1)) {
|
|
1497 |
RETURN ( self );
|
|
1498 |
}
|
|
1499 |
if (index2 > len)
|
|
1500 |
index2 = len;
|
1
|
1501 |
|
10
|
1502 |
savInt = _immediateInterrupt;
|
2
|
1503 |
_immediateInterrupt = 1;
|
|
1504 |
len = index2 - index1 + 1;
|
12
|
1505 |
#ifdef OLD
|
10
|
1506 |
if (_INST(buffered) == false) {
|
2
|
1507 |
cnt = write(fileno(f), cp + index1 - 1, len);
|
12
|
1508 |
} else
|
|
1509 |
#endif
|
|
1510 |
{
|
2
|
1511 |
if (_INST(mode) == _readwrite)
|
|
1512 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1513 |
cnt = fwrite(cp + index1 - 1, 1, len, f);
|
1
|
1514 |
}
|
12
|
1515 |
#ifndef OLD
|
|
1516 |
if (_INST(buffered) == false) {
|
|
1517 |
fflush(f);
|
|
1518 |
}
|
|
1519 |
#endif
|
10
|
1520 |
_immediateInterrupt = savInt;
|
2
|
1521 |
if (cnt == len) {
|
|
1522 |
if (_INST(position) != nil) {
|
|
1523 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + len);
|
|
1524 |
}
|
|
1525 |
RETURN ( self );
|
|
1526 |
}
|
|
1527 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1528 |
RETURN ( nil );
|
1
|
1529 |
}
|
|
1530 |
}
|
|
1531 |
}
|
|
1532 |
%}
|
|
1533 |
.
|
|
1534 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1535 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1536 |
|
|
1537 |
start to:stop do:[:index |
|
|
1538 |
self nextPut:(aCollection at:index)
|
|
1539 |
]
|
|
1540 |
!
|
|
1541 |
|
|
1542 |
cr
|
|
1543 |
"reimplemented for speed"
|
|
1544 |
|
|
1545 |
%{ /* NOCONTEXT */
|
|
1546 |
|
|
1547 |
FILE *f;
|
|
1548 |
extern OBJ ErrorNumber;
|
|
1549 |
extern errno;
|
|
1550 |
extern int _immediateInterrupt;
|
10
|
1551 |
int cnt, savInt;
|
1
|
1552 |
|
2
|
1553 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
|
|
1554 |
if (_INST(binary) != true) {
|
1
|
1555 |
f = MKFD(_INST(filePointer));
|
|
1556 |
|
10
|
1557 |
savInt = _immediateInterrupt;
|
2
|
1558 |
_immediateInterrupt = 1;
|
12
|
1559 |
#ifdef OLD
|
10
|
1560 |
if (_INST(buffered) == false) {
|
2
|
1561 |
cnt = write(fileno(f), "\n", 1);
|
12
|
1562 |
} else
|
|
1563 |
#endif
|
|
1564 |
{
|
2
|
1565 |
if (_INST(mode) == _readwrite)
|
|
1566 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
1567 |
cnt = fwrite("\n", 1, 1, f);
|
2
|
1568 |
}
|
12
|
1569 |
#ifndef OLD
|
|
1570 |
if (_INST(buffered) == false) {
|
|
1571 |
fflush(f);
|
|
1572 |
}
|
|
1573 |
#endif
|
10
|
1574 |
_immediateInterrupt = savInt;
|
1
|
1575 |
if (cnt == 1) {
|
|
1576 |
if (_INST(position) != nil) {
|
|
1577 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
|
|
1578 |
}
|
|
1579 |
RETURN ( self );
|
|
1580 |
}
|
|
1581 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1582 |
return ( nil );
|
|
1583 |
}
|
|
1584 |
}
|
|
1585 |
%}
|
|
1586 |
.
|
|
1587 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
2
|
1588 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1589 |
self errorBinary
|
1
|
1590 |
! !
|
|
1591 |
|
2
|
1592 |
!ExternalStream methodsFor:'line reading/writing'!
|
1
|
1593 |
|
|
1594 |
nextLine
|
|
1595 |
"read the next line (characters up to newline).
|
|
1596 |
Return a string containing those characters excluding the newline.
|
|
1597 |
If the previous-to-last character is a cr, this is also removed,
|
|
1598 |
so its possible to read alien (i.e. ms-dos) text as well."
|
|
1599 |
|
12
|
1600 |
%{ /* STACK:2000 */
|
1
|
1601 |
|
|
1602 |
FILE *f;
|
|
1603 |
int len;
|
12
|
1604 |
char buffer[1024];
|
1
|
1605 |
extern int _immediateInterrupt;
|
10
|
1606 |
int savInt;
|
1
|
1607 |
char *rslt;
|
10
|
1608 |
extern errno;
|
12
|
1609 |
int fd;
|
1
|
1610 |
|
12
|
1611 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
2
|
1612 |
if (_INST(binary) != true) {
|
1
|
1613 |
f = MKFD(_INST(filePointer));
|
10
|
1614 |
savInt = _immediateInterrupt;
|
1
|
1615 |
_immediateInterrupt = 1;
|
|
1616 |
buffer[0] = 0;
|
12
|
1617 |
|
|
1618 |
#ifndef OLD
|
|
1619 |
if (_INST(mode) == _readwrite)
|
|
1620 |
fseek(f, 0L, 1); /* needed in stdio */
|
|
1621 |
#endif
|
|
1622 |
#ifdef OLD
|
10
|
1623 |
if (_INST(buffered) == true) {
|
12
|
1624 |
#endif
|
|
1625 |
do {
|
10
|
1626 |
rslt = fgets(buffer, sizeof(buffer), f);
|
12
|
1627 |
} while ((rslt == NULL) && (errno == EINTR));
|
|
1628 |
#ifdef OLD
|
10
|
1629 |
} else {
|
|
1630 |
rslt = buffer;
|
12
|
1631 |
fd = fileno(f);
|
10
|
1632 |
for (;;) {
|
12
|
1633 |
do {
|
|
1634 |
len = read(fd, rslt, 1);
|
|
1635 |
} while ((len < 0) && (errno == EINTR));
|
10
|
1636 |
if (len <= 0) {
|
|
1637 |
if (rslt == buffer) {
|
|
1638 |
rslt = NULL;
|
|
1639 |
} else {
|
|
1640 |
*rslt = '\0';
|
|
1641 |
}
|
|
1642 |
break;
|
|
1643 |
}
|
|
1644 |
rslt++;
|
|
1645 |
if (*(rslt-1) == '\n') {
|
|
1646 |
*rslt = '\0';
|
|
1647 |
break;
|
|
1648 |
}
|
|
1649 |
if (rslt == (buffer + sizeof(buffer) - 1)) {
|
|
1650 |
*rslt = '\0';
|
|
1651 |
break;
|
|
1652 |
}
|
|
1653 |
}
|
|
1654 |
}
|
12
|
1655 |
#endif
|
10
|
1656 |
_immediateInterrupt = savInt;
|
1
|
1657 |
if (rslt != NULL) {
|
|
1658 |
len = strlen(buffer);
|
|
1659 |
if (_INST(position) != nil) {
|
|
1660 |
_INST(position) = _MKSMALLINT(_intVal(_INST(position)) + len);
|
|
1661 |
}
|
|
1662 |
/* remove EOL character */
|
|
1663 |
if ((len != 0) && (buffer[len-1] == '\n')) {
|
|
1664 |
buffer[--len] = '\0';
|
|
1665 |
}
|
|
1666 |
if ((len != 0) && (buffer[len-1] == '\r')) {
|
|
1667 |
buffer[--len] = '\0';
|
|
1668 |
}
|
|
1669 |
RETURN ( _MKSTRING(buffer COMMA_CON) );
|
|
1670 |
}
|
2
|
1671 |
RETURN ( nil );
|
1
|
1672 |
}
|
|
1673 |
}
|
|
1674 |
%}
|
|
1675 |
.
|
2
|
1676 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
1
|
1677 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
2
|
1678 |
self errorBinary
|
1
|
1679 |
!
|
|
1680 |
|
|
1681 |
nextPutLine:aString
|
|
1682 |
"write the characters in aString and append a newline"
|
|
1683 |
|
|
1684 |
%{ /* NOCONTEXT */
|
|
1685 |
|
|
1686 |
FILE *f;
|
|
1687 |
int len, cnt;
|
|
1688 |
OBJ pos;
|
|
1689 |
char *s;
|
|
1690 |
extern OBJ ErrorNumber;
|
|
1691 |
extern errno;
|
10
|
1692 |
int savInt;
|
1
|
1693 |
extern int _immediateInterrupt;
|
|
1694 |
|
2
|
1695 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
|
|
1696 |
if (_INST(binary) != true) {
|
1
|
1697 |
if (_isString(aString)) {
|
|
1698 |
f = MKFD(_INST(filePointer));
|
|
1699 |
s = (char *) _stringVal(aString);
|
|
1700 |
len = _stringSize(aString);
|
|
1701 |
|
10
|
1702 |
savInt = _immediateInterrupt;
|
2
|
1703 |
_immediateInterrupt = 1;
|
12
|
1704 |
#ifdef OLD
|
10
|
1705 |
if (_INST(buffered) == false) {
|
2
|
1706 |
cnt = write(fileno(f), s, len);
|
12
|
1707 |
} else
|
|
1708 |
#endif
|
|
1709 |
{
|
2
|
1710 |
if (_INST(mode) == _readwrite)
|
|
1711 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
1712 |
cnt = fwrite(s, 1, len, f);
|
2
|
1713 |
}
|
1
|
1714 |
if (cnt == len) {
|
12
|
1715 |
#ifdef OLD
|
10
|
1716 |
if (_INST(buffered) == false) {
|
2
|
1717 |
cnt = write(fileno(f), "\n", 1);
|
12
|
1718 |
} else
|
|
1719 |
#endif
|
|
1720 |
{
|
1
|
1721 |
cnt = fwrite("\n", 1, 1, f);
|
2
|
1722 |
}
|
12
|
1723 |
#ifndef OLD
|
|
1724 |
if (_INST(buffered) == false) {
|
|
1725 |
fflush(f);
|
|
1726 |
}
|
|
1727 |
#endif
|
1
|
1728 |
if (cnt == 1) {
|
|
1729 |
pos = _INST(position);
|
|
1730 |
if (pos != nil) {
|
|
1731 |
_INST(position) = _MKSMALLINT(_intVal(pos)+len+1);
|
|
1732 |
}
|
10
|
1733 |
_immediateInterrupt = savInt;
|
1
|
1734 |
RETURN ( self );
|
|
1735 |
}
|
|
1736 |
}
|
10
|
1737 |
_immediateInterrupt = savInt;
|
1
|
1738 |
ErrorNumber = _MKSMALLINT(errno);
|
|
1739 |
RETURN ( nil );
|
|
1740 |
}
|
|
1741 |
}
|
|
1742 |
}
|
|
1743 |
%}
|
|
1744 |
.
|
2
|
1745 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
1
|
1746 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
2
|
1747 |
binary ifTrue:[^ self errorBinary].
|
1
|
1748 |
self argumentMustBeString
|
|
1749 |
!
|
|
1750 |
|
|
1751 |
nextPutLinesFrom:aStream upToLineStartingWith:aStringOrNil
|
|
1752 |
"used to copy large files
|
|
1753 |
- read from aStream up to and including a line starting with aStringOrNil
|
|
1754 |
and append it to self. If aStringOrNil is nil or not matched,
|
|
1755 |
copy preceeds to the end"
|
|
1756 |
|
|
1757 |
|srcFilePointer|
|
|
1758 |
|
|
1759 |
(mode == #readonly) ifTrue:[^ self errorReadOnly].
|
|
1760 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
1761 |
srcFilePointer := aStream filePointer.
|
|
1762 |
srcFilePointer isNil ifTrue:[^ aStream errorNotOpen].
|
12
|
1763 |
|
|
1764 |
%{ /* STACK:2000 */
|
1
|
1765 |
FILE *dst, *src;
|
|
1766 |
char *matchString;
|
|
1767 |
int matchLen = 0;
|
12
|
1768 |
char buffer[1024];
|
1
|
1769 |
extern int _immediateInterrupt;
|
10
|
1770 |
int savInt;
|
1
|
1771 |
|
12
|
1772 |
if (_isSmallInteger(srcFilePointer)
|
|
1773 |
#ifdef OLD
|
|
1774 |
&& (_INST(buffered) == true)
|
|
1775 |
#endif
|
|
1776 |
) {
|
2
|
1777 |
if ((aStringOrNil == nil) || _isString(aStringOrNil)) {
|
1
|
1778 |
if (aStringOrNil != nil) {
|
|
1779 |
matchString = (char *) _stringVal(aStringOrNil);
|
|
1780 |
matchLen = _stringSize(aStringOrNil);
|
|
1781 |
}
|
|
1782 |
dst = MKFD(_INST(filePointer));
|
|
1783 |
src = (FILE *)_intVal(srcFilePointer);
|
10
|
1784 |
savInt = _immediateInterrupt;
|
2
|
1785 |
_immediateInterrupt = 1;
|
1
|
1786 |
for (;;) {
|
|
1787 |
if (fgets(buffer, sizeof(buffer), src) == NULL) break;
|
|
1788 |
if (fputs(buffer, dst) == EOF) break;
|
12
|
1789 |
#ifndef OLD
|
|
1790 |
if (_INST(buffered) == false) {
|
|
1791 |
fflush(dst);
|
|
1792 |
}
|
|
1793 |
#endif
|
1
|
1794 |
if (matchLen) {
|
|
1795 |
if (strncmp(matchString, buffer, matchLen) == 0)
|
|
1796 |
break;
|
|
1797 |
}
|
|
1798 |
}
|
10
|
1799 |
_immediateInterrupt = savInt;
|
1
|
1800 |
_INST(position) = nil;
|
|
1801 |
RETURN ( self );
|
|
1802 |
}
|
|
1803 |
}
|
|
1804 |
%}
|
|
1805 |
.
|
10
|
1806 |
buffered ifFalse:[^ self errorNotBuffered].
|
1
|
1807 |
^ self primitiveFailed
|
|
1808 |
!
|
|
1809 |
|
|
1810 |
peekForLineStartingWith:aString
|
|
1811 |
"read ahead for next line starting with aString;
|
|
1812 |
return the line-string if found, nil otherwise..
|
|
1813 |
do not advance position i.e. nextLine will reread this line"
|
|
1814 |
|
|
1815 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
1816 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
2
|
1817 |
binary ifTrue:[^ self errorBinary].
|
12
|
1818 |
|
|
1819 |
%{ /* STACK: 2000 */
|
1
|
1820 |
FILE *f;
|
|
1821 |
int l;
|
12
|
1822 |
char buffer[1024];
|
1
|
1823 |
char *cp;
|
|
1824 |
char *matchString;
|
|
1825 |
int firstpos, lastpos;
|
|
1826 |
extern int _immediateInterrupt;
|
10
|
1827 |
int savInt;
|
1
|
1828 |
|
|
1829 |
if (_isString(aString)) {
|
|
1830 |
matchString = (char *) _stringVal(aString);
|
|
1831 |
l = _stringSize(aString);
|
|
1832 |
|
|
1833 |
f = MKFD(_INST(filePointer));
|
|
1834 |
firstpos = ftell(f);
|
|
1835 |
for (;;) {
|
12
|
1836 |
if (_INST(mode) == _readwrite)
|
|
1837 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
1838 |
lastpos = ftell(f);
|
10
|
1839 |
savInt = _immediateInterrupt;
|
1
|
1840 |
_immediateInterrupt = 1;
|
12
|
1841 |
do {
|
10
|
1842 |
cp = fgets(buffer, sizeof(buffer), f);
|
12
|
1843 |
} while ((cp == NULL) && (errno == EINTR));
|
10
|
1844 |
_immediateInterrupt = savInt;
|
1
|
1845 |
if (cp == NULL) {
|
|
1846 |
fseek(f, firstpos, 0);
|
|
1847 |
RETURN ( nil );
|
|
1848 |
}
|
|
1849 |
if (strncmp(cp, matchString, l) == 0) {
|
|
1850 |
fseek(f, lastpos, 0);
|
|
1851 |
break;
|
|
1852 |
}
|
|
1853 |
}
|
|
1854 |
/* remove EOL character */
|
|
1855 |
cp = buffer;
|
|
1856 |
while (*cp && (*cp != '\n')) cp++;
|
|
1857 |
*cp = '\0';
|
|
1858 |
RETURN ( _MKSTRING(buffer COMMA_CON) );
|
|
1859 |
}
|
|
1860 |
%}
|
|
1861 |
.
|
|
1862 |
self argumentMustBeString
|
|
1863 |
!
|
|
1864 |
|
|
1865 |
peekForLineStartingWithAny:aCollectionOfStrings
|
|
1866 |
"read ahead for next line starting with any of aCollectionOfStrings;
|
|
1867 |
return the index in aCollection if found, nil otherwise..
|
|
1868 |
If no match, do not change position; otherwise advance right before the
|
|
1869 |
matched line so that nextLine will return this line."
|
|
1870 |
|
|
1871 |
|line startPos linePos index|
|
|
1872 |
|
|
1873 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
|
1874 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
2
|
1875 |
binary ifTrue:[^ self errorBinary].
|
|
1876 |
|
1
|
1877 |
startPos := self position.
|
|
1878 |
[self atEnd] whileFalse:[
|
|
1879 |
linePos := self position.
|
|
1880 |
line := self nextLine.
|
|
1881 |
index := 1.
|
|
1882 |
aCollectionOfStrings do:[:prefix |
|
|
1883 |
(line startsWith:prefix) ifTrue:[
|
|
1884 |
self position:linePos.
|
|
1885 |
^ index
|
|
1886 |
].
|
|
1887 |
index := index + 1
|
|
1888 |
]
|
|
1889 |
].
|
|
1890 |
self position:startPos.
|
|
1891 |
^ nil
|
|
1892 |
! !
|
|
1893 |
|
|
1894 |
!ExternalStream methodsFor:'testing'!
|
|
1895 |
|
|
1896 |
atEnd
|
|
1897 |
"return true, if position is at end"
|
|
1898 |
|
|
1899 |
%{ /* NOCONTEXT */
|
|
1900 |
|
|
1901 |
FILE *f;
|
|
1902 |
|
|
1903 |
if (_INST(filePointer) != nil) {
|
|
1904 |
f = MKFD(_INST(filePointer));
|
|
1905 |
RETURN ( feof(f) ? true : false );
|
|
1906 |
}
|
|
1907 |
%}
|
|
1908 |
.
|
|
1909 |
self errorNotOpen
|
|
1910 |
! !
|
|
1911 |
|
10
|
1912 |
!ExternalStream methodsFor:'waiting for I/O'!
|
|
1913 |
|
|
1914 |
readWait
|
|
1915 |
"suspend the current process, until the receiver
|
|
1916 |
becomes ready for reading. If data is already available,
|
|
1917 |
return immediate. The other threads are not affected by
|
|
1918 |
the wait."
|
|
1919 |
|
|
1920 |
|fd sema|
|
|
1921 |
|
|
1922 |
fd := self fileDescriptor.
|
|
1923 |
sema := Semaphore new.
|
|
1924 |
|
|
1925 |
[OperatingSystem readCheck:fd] whileFalse:[
|
12
|
1926 |
Processor enableSemaphore:sema onInput:fd.
|
|
1927 |
Processor currentProcess state:#ioWait.
|
10
|
1928 |
sema wait.
|
12
|
1929 |
Processor disableSemaphore:sema
|
10
|
1930 |
]
|
|
1931 |
!
|
|
1932 |
|
|
1933 |
writeWait
|
|
1934 |
"suspend the current process, until the receiver
|
|
1935 |
becomes ready for writing. Return immediate if the receiver
|
|
1936 |
is already ready. The other threads are not affected by
|
|
1937 |
the wait."
|
|
1938 |
|
|
1939 |
|fd sema|
|
|
1940 |
|
|
1941 |
fd := self fileDescriptor.
|
|
1942 |
sema := Semaphore new.
|
|
1943 |
|
|
1944 |
[OperatingSystem writeCheck:fd] whileFalse:[
|
12
|
1945 |
Processor enableSemaphore:sema onOutput:fd.
|
|
1946 |
Processor currentProcess state:#ioWait.
|
10
|
1947 |
sema wait.
|
12
|
1948 |
Processor disableSemaphore:sema
|
10
|
1949 |
]
|
|
1950 |
! !
|
|
1951 |
|
1
|
1952 |
!ExternalStream methodsFor:'reimplemented for speed'!
|
|
1953 |
|
2
|
1954 |
peekFor:anObject
|
|
1955 |
"return true and move past if next == something."
|
1
|
1956 |
|
|
1957 |
%{ /* NOCONTEXT */
|
|
1958 |
|
|
1959 |
FILE *f;
|
|
1960 |
int c;
|
2
|
1961 |
int peekValue;
|
1
|
1962 |
extern int _immediateInterrupt;
|
10
|
1963 |
int savInt;
|
1
|
1964 |
|
2
|
1965 |
if ((_INST(binary) == true) && _isSmallInteger(anObject)) {
|
|
1966 |
peekValue = _intVal(anObject) & 0xFF;
|
|
1967 |
} else {
|
|
1968 |
if ((_INST(binary) != true) && _isCharacter(anObject)) {
|
|
1969 |
peekValue = _intVal(_characterVal(anObject)) & 0xFF;
|
|
1970 |
} else
|
|
1971 |
peekValue = -1;
|
|
1972 |
}
|
|
1973 |
|
|
1974 |
if (peekValue >= 0) {
|
1
|
1975 |
if (_INST(filePointer) != nil) {
|
|
1976 |
f = MKFD(_INST(filePointer));
|
10
|
1977 |
savInt = _immediateInterrupt;
|
2
|
1978 |
_immediateInterrupt = 1;
|
1
|
1979 |
c = getc(f);
|
10
|
1980 |
_immediateInterrupt = savInt;
|
2
|
1981 |
if (c == peekValue) {
|
|
1982 |
_INST(position) = nil;
|
1
|
1983 |
RETURN ( true );
|
|
1984 |
}
|
|
1985 |
ungetc(c, f);
|
|
1986 |
RETURN ( false );
|
|
1987 |
}
|
|
1988 |
}
|
|
1989 |
%}
|
|
1990 |
.
|
|
1991 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
2
|
1992 |
^ super peekFor:anObject
|
|
1993 |
!
|
|
1994 |
|
|
1995 |
nextMatchFor:anObject
|
|
1996 |
"skip all objects up-to and including anObject, return anObject on success,
|
|
1997 |
nil if end-of-file is reached before. The next read operation will return
|
|
1998 |
the element after anObject."
|
|
1999 |
|
|
2000 |
%{ /* NOCONTEXT */
|
|
2001 |
|
|
2002 |
FILE *f;
|
|
2003 |
int peekValue, c;
|
|
2004 |
extern int _immediateInterrupt;
|
10
|
2005 |
int savInt;
|
2
|
2006 |
|
|
2007 |
if ((_INST(binary) == true) && _isSmallInteger(anObject)) {
|
|
2008 |
peekValue = _intVal(anObject) & 0xFF;
|
|
2009 |
} else {
|
|
2010 |
if ((_INST(binary) != true) && _isCharacter(anObject)) {
|
|
2011 |
peekValue = _intVal(_characterVal(anObject)) & 0xFF;
|
|
2012 |
} else
|
|
2013 |
peekValue = -1;
|
|
2014 |
}
|
|
2015 |
|
|
2016 |
if (peekValue >= 0) {
|
|
2017 |
if (_INST(filePointer) != nil) {
|
|
2018 |
if (_INST(mode) != _writeonly) {
|
|
2019 |
f = MKFD(_INST(filePointer));
|
10
|
2020 |
savInt = _immediateInterrupt;
|
2
|
2021 |
_immediateInterrupt = 1;
|
|
2022 |
_INST(position) = nil;
|
12
|
2023 |
if (_INST(mode) == _readwrite)
|
|
2024 |
fseek(f, 0L, 1); /* needed in stdio */
|
2
|
2025 |
for (;;) {
|
|
2026 |
c = getc(f);
|
|
2027 |
if (c == EOF) {
|
10
|
2028 |
_immediateInterrupt = savInt;
|
2
|
2029 |
RETURN (nil);
|
|
2030 |
}
|
|
2031 |
if (c == peekValue) {
|
10
|
2032 |
_immediateInterrupt = savInt;
|
2
|
2033 |
RETURN (anObject);
|
|
2034 |
}
|
|
2035 |
}
|
10
|
2036 |
_immediateInterrupt = savInt;
|
2
|
2037 |
}
|
|
2038 |
}
|
|
2039 |
}
|
|
2040 |
%}
|
|
2041 |
.
|
|
2042 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
2043 |
^ super nextMatchFor:anObject
|
1
|
2044 |
!
|
|
2045 |
|
|
2046 |
skipLine
|
|
2047 |
"read the next line (characters up to newline) skip only;
|
2
|
2048 |
return nil if EOF reached. Not allowed in binary mode."
|
1
|
2049 |
|
12
|
2050 |
%{ /* STACK:2000 */
|
1
|
2051 |
|
|
2052 |
FILE *f;
|
12
|
2053 |
char buffer[1024];
|
1
|
2054 |
extern int _immediateInterrupt;
|
10
|
2055 |
int savInt;
|
1
|
2056 |
|
2
|
2057 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
2058 |
if (_INST(binary) != true) {
|
1
|
2059 |
f = MKFD(_INST(filePointer));
|
10
|
2060 |
savInt = _immediateInterrupt;
|
2
|
2061 |
_immediateInterrupt = 1;
|
12
|
2062 |
if (_INST(mode) == _readwrite)
|
|
2063 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
2064 |
if (fgets(buffer, sizeof(buffer), f) != NULL) {
|
10
|
2065 |
_immediateInterrupt = savInt;
|
1
|
2066 |
RETURN ( self );
|
|
2067 |
}
|
10
|
2068 |
_immediateInterrupt = savInt;
|
1
|
2069 |
RETURN ( nil );
|
|
2070 |
}
|
|
2071 |
}
|
|
2072 |
%}
|
|
2073 |
.
|
|
2074 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
2
|
2075 |
binary ifTrue:[^ self errorBinary].
|
1
|
2076 |
self errorWriteOnly
|
|
2077 |
!
|
|
2078 |
|
|
2079 |
skipToAll:aString
|
|
2080 |
"skip for the sequence given by the argument, aCollection;
|
|
2081 |
return nil if not found, self otherwise. On a successful match, next read
|
|
2082 |
will return characters of aString."
|
|
2083 |
|
|
2084 |
|oldPos buffer l first idx|
|
|
2085 |
|
|
2086 |
(aString isKindOf:String) ifTrue:[
|
|
2087 |
oldPos := self position.
|
|
2088 |
l := aString size.
|
|
2089 |
first := aString at:1.
|
|
2090 |
buffer := String new:l.
|
|
2091 |
[true] whileTrue:[
|
|
2092 |
(self nextBytes:l into:buffer) == l ifFalse:[
|
|
2093 |
self position:oldPos.
|
|
2094 |
^ nil
|
|
2095 |
].
|
|
2096 |
buffer = aString ifTrue:[
|
|
2097 |
self position:(self position - l).
|
|
2098 |
^ self
|
|
2099 |
].
|
|
2100 |
idx := buffer indexOf:first startingAt:2.
|
|
2101 |
idx == 0 ifFalse:[
|
|
2102 |
self position:(self position - l + idx - 1)
|
|
2103 |
]
|
|
2104 |
]
|
|
2105 |
].
|
|
2106 |
^ super skipFor:aString
|
|
2107 |
!
|
|
2108 |
|
|
2109 |
skipSeparators
|
|
2110 |
"skip all whitespace; next will return next non-white-space character
|
2
|
2111 |
or nil if endOfFile reached. Not allowed in binary mode.
|
1
|
2112 |
- reimplemented for speed"
|
|
2113 |
|
|
2114 |
%{ /* NOCONTEXT */
|
|
2115 |
|
|
2116 |
FILE *f;
|
|
2117 |
REGISTER int c;
|
|
2118 |
extern int _immediateInterrupt;
|
10
|
2119 |
int savInt;
|
1
|
2120 |
|
2
|
2121 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
2122 |
if (_INST(binary) != true) {
|
1
|
2123 |
f = MKFD(_INST(filePointer));
|
12
|
2124 |
if (_INST(mode) == _readwrite)
|
|
2125 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
2126 |
while (1) {
|
|
2127 |
if (feof(f)) {
|
|
2128 |
RETURN ( nil );
|
|
2129 |
}
|
10
|
2130 |
savInt = _immediateInterrupt;
|
2
|
2131 |
_immediateInterrupt = 1;
|
1
|
2132 |
c = getc(f);
|
10
|
2133 |
_immediateInterrupt = savInt;
|
1
|
2134 |
if (c < 0) {
|
|
2135 |
RETURN ( nil );
|
|
2136 |
}
|
|
2137 |
switch (c) {
|
|
2138 |
case ' ':
|
|
2139 |
case '\t':
|
|
2140 |
case '\n':
|
|
2141 |
case '\r':
|
|
2142 |
case '\b':
|
|
2143 |
case '\014':
|
|
2144 |
break;
|
|
2145 |
default:
|
|
2146 |
ungetc(c, f);
|
|
2147 |
RETURN ( _MKCHARACTER(c & 0xFF) );
|
|
2148 |
}
|
|
2149 |
}
|
|
2150 |
}
|
|
2151 |
}
|
|
2152 |
%}
|
|
2153 |
.
|
2
|
2154 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
1
|
2155 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
2
|
2156 |
^ self errorBinary.
|
1
|
2157 |
!
|
|
2158 |
|
|
2159 |
skipSeparatorsExceptCR
|
|
2160 |
"skip all whitespace but no newlines;
|
|
2161 |
next will return next non-white-space character
|
2
|
2162 |
or nil if endOfFile reached. Not allowed in binary mode.
|
1
|
2163 |
- reimplemented for speed"
|
|
2164 |
|
|
2165 |
%{ /* NOCONTEXT */
|
|
2166 |
|
|
2167 |
FILE *f;
|
|
2168 |
int c;
|
|
2169 |
extern int _immediateInterrupt;
|
10
|
2170 |
int savInt;
|
1
|
2171 |
|
2
|
2172 |
if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
|
|
2173 |
if (_INST(binary) != true) {
|
1
|
2174 |
f = MKFD(_INST(filePointer));
|
|
2175 |
while (1) {
|
12
|
2176 |
if (_INST(mode) == _readwrite)
|
|
2177 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
2178 |
if (feof(f)) {
|
|
2179 |
RETURN ( nil );
|
|
2180 |
}
|
10
|
2181 |
savInt = _immediateInterrupt;
|
2
|
2182 |
_immediateInterrupt = 1;
|
1
|
2183 |
c = getc(f);
|
10
|
2184 |
_immediateInterrupt = savInt;
|
1
|
2185 |
if (c < 0) {
|
|
2186 |
RETURN ( nil );
|
|
2187 |
}
|
|
2188 |
switch (c) {
|
|
2189 |
case ' ':
|
|
2190 |
case '\t':
|
|
2191 |
case '\b':
|
|
2192 |
break;
|
|
2193 |
default:
|
|
2194 |
ungetc(c, f);
|
|
2195 |
RETURN ( _MKCHARACTER(c & 0xFF) );
|
|
2196 |
}
|
|
2197 |
}
|
|
2198 |
}
|
|
2199 |
}
|
|
2200 |
%}
|
|
2201 |
.
|
2
|
2202 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
1
|
2203 |
(mode == #writeonly) ifTrue:[^ self errorWriteOnly].
|
2
|
2204 |
self errorBinary
|
1
|
2205 |
!
|
|
2206 |
|
|
2207 |
nextChunk
|
|
2208 |
"return the next chunk, i.e. all characters up to the next
|
|
2209 |
non-doubled exclamation mark; undouble doubled exclamation marks.
|
|
2210 |
- reimplemented for speed"
|
|
2211 |
|retVal|
|
|
2212 |
|
|
2213 |
filePointer isNil ifTrue:[
|
|
2214 |
^ self errorNotOpen
|
|
2215 |
].
|
2
|
2216 |
binary ifTrue:[
|
|
2217 |
^ self errorBinary
|
|
2218 |
].
|
1
|
2219 |
%{
|
|
2220 |
FILE *f;
|
|
2221 |
int done = 0;
|
|
2222 |
REGISTER int c;
|
|
2223 |
unsigned char peekC;
|
|
2224 |
char *buffer, *newBuffer;
|
|
2225 |
REGISTER int index;
|
|
2226 |
int currSize;
|
|
2227 |
int inComment, inString, inPrimitive = 0;
|
|
2228 |
extern int _immediateInterrupt;
|
10
|
2229 |
int savInt;
|
1
|
2230 |
|
|
2231 |
f = MKFD(_INST(filePointer));
|
12
|
2232 |
if (_INST(mode) == _readwrite)
|
|
2233 |
fseek(f, 0L, 1); /* needed in stdio */
|
1
|
2234 |
/*
|
|
2235 |
* skip spaces
|
|
2236 |
*/
|
10
|
2237 |
savInt = _immediateInterrupt;
|
2
|
2238 |
_immediateInterrupt = 1;
|
1
|
2239 |
while (! done) {
|
|
2240 |
if (feof(f)) {
|
10
|
2241 |
_immediateInterrupt = savInt;
|
1
|
2242 |
RETURN ( nil );
|
|
2243 |
}
|
12
|
2244 |
do {
|
10
|
2245 |
c = getc(f);
|
12
|
2246 |
} while ((c < 0) && (errno == EINTR));
|
1
|
2247 |
switch (c) {
|
|
2248 |
case ' ':
|
|
2249 |
case '\t':
|
|
2250 |
case '\n':
|
|
2251 |
case '\r':
|
|
2252 |
case '\b':
|
|
2253 |
case '\014':
|
|
2254 |
break;
|
|
2255 |
|
|
2256 |
case EOF:
|
10
|
2257 |
_immediateInterrupt = savInt;
|
1
|
2258 |
RETURN ( nil );
|
|
2259 |
|
|
2260 |
default:
|
|
2261 |
ungetc(c, f);
|
|
2262 |
done = 1;
|
|
2263 |
break;
|
|
2264 |
}
|
|
2265 |
}
|
|
2266 |
|
|
2267 |
/*
|
|
2268 |
* read chunk into a buffer
|
|
2269 |
*/
|
|
2270 |
buffer = (char *)malloc(3000);
|
|
2271 |
currSize = 3000;
|
|
2272 |
index = 0;
|
|
2273 |
while (! feof(f)) {
|
|
2274 |
/* do we have to resize the buffer ? */
|
|
2275 |
if ((index+2) >= currSize) {
|
|
2276 |
newBuffer = (char *)malloc(currSize * 2);
|
|
2277 |
bcopy(buffer, newBuffer, index);
|
|
2278 |
free(buffer);
|
|
2279 |
buffer = newBuffer;
|
|
2280 |
currSize = currSize * 2;
|
|
2281 |
}
|
12
|
2282 |
do {
|
10
|
2283 |
c = getc(f);
|
12
|
2284 |
} while (c < 0 && (errno == EINTR));
|
1
|
2285 |
if (c == '%') {
|
|
2286 |
peekC = getc(f);
|
|
2287 |
ungetc(peekC, f);
|
|
2288 |
if (peekC == '{') {
|
|
2289 |
inPrimitive++;
|
|
2290 |
} else if (peekC == '}') {
|
|
2291 |
inPrimitive--;
|
|
2292 |
}
|
|
2293 |
} else {
|
|
2294 |
if (! inPrimitive) {
|
|
2295 |
if (c == '!') {
|
|
2296 |
c = getc(f);
|
|
2297 |
if (c != '!') {
|
|
2298 |
ungetc(c, f);
|
|
2299 |
break;
|
|
2300 |
}
|
|
2301 |
}
|
|
2302 |
}
|
|
2303 |
}
|
|
2304 |
if (c == EOF) break;
|
|
2305 |
buffer[index++] = c;
|
|
2306 |
}
|
10
|
2307 |
_immediateInterrupt = savInt;
|
1
|
2308 |
buffer[index] = '\0';
|
|
2309 |
/*
|
|
2310 |
* make it a string
|
|
2311 |
*/
|
|
2312 |
retVal = _MKSTRING(buffer COMMA_CON);
|
|
2313 |
free(buffer);
|
|
2314 |
%}
|
|
2315 |
.
|
|
2316 |
^ retVal
|
|
2317 |
! !
|