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