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