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