author | Claus Gittinger <cg@exept.de> |
Tue, 14 Jan 1997 12:49:01 +0100 | |
changeset 2161 | 0472a226a714 |
parent 2134 | 246a3bdab8b4 |
child 2763 | cea404472e7c |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 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 |
ExternalStream subclass:#FileStream |
|
1088 | 14 |
instanceVariableNames:'pathName' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams-External' |
|
1 | 18 |
! |
19 |
||
216 | 20 |
!FileStream primitiveDefinitions! |
1 | 21 |
%{ |
22 |
#include <stdio.h> |
|
437 | 23 |
#define _STDIO_H_INCLUDED_ |
24 |
||
10 | 25 |
#include <errno.h> |
437 | 26 |
#define _ERRNO_H_INCLUDED_ |
1 | 27 |
|
28 |
#ifdef transputer |
|
29 |
# include <iocntrl.h> |
|
30 |
# ifndef fileno |
|
31 |
/* kludge: inmos forgot fileno */ |
|
32 |
# define fileno(f) ((f)->__file) |
|
33 |
# endif |
|
34 |
#else |
|
35 |
# include <sys/types.h> |
|
36 |
# include <sys/stat.h> |
|
37 |
#endif |
|
10 | 38 |
|
42 | 39 |
#ifdef hpux |
68 | 40 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
42 | 41 |
#endif |
42 |
||
10 | 43 |
#ifndef SEEK_SET |
13 | 44 |
# define SEEK_SET 0 |
10 | 45 |
#endif |
46 |
#ifndef SEEK_CUR |
|
13 | 47 |
# define SEEK_CUR 1 |
10 | 48 |
#endif |
49 |
#ifndef SEEK_END |
|
13 | 50 |
# define SEEK_END 2 |
10 | 51 |
#endif |
52 |
||
223 | 53 |
/* |
54 |
* on some systems errno is a macro ... check for it here |
|
55 |
*/ |
|
56 |
#ifndef errno |
|
57 |
extern errno; |
|
58 |
#endif |
|
59 |
||
1 | 60 |
%} |
173 | 61 |
! ! |
1 | 62 |
|
613 | 63 |
!FileStream class methodsFor:'documentation'! |
64 |
||
65 |
copyright |
|
66 |
" |
|
67 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
68 |
All Rights Reserved |
|
69 |
||
70 |
This software is furnished under a license and may be used |
|
71 |
only in accordance with the terms of that license and with the |
|
72 |
inclusion of the above copyright notice. This software may not |
|
73 |
be provided or otherwise made available to, or used by, any |
|
74 |
other person. No title to or ownership of the software is |
|
75 |
hereby transferred. |
|
76 |
" |
|
77 |
! |
|
78 |
||
79 |
documentation |
|
80 |
" |
|
81 |
This class provides access to the operating systems underlying file |
|
82 |
system (i.e. its an interface to the stdio library). |
|
83 |
||
84 |
Notice, that on some systems, the standard I/O library has performance |
|
85 |
problems when a file is opened for readwrite. |
|
86 |
For best results, open files either readonly or writeonly. |
|
1295 | 87 |
|
88 |
[author:] |
|
2161 | 89 |
Claus Gittinger |
1295 | 90 |
|
91 |
[see also:] |
|
2161 | 92 |
Filename DirectoryStream PipeStream Socket |
613 | 93 |
" |
94 |
! ! |
|
95 |
||
1 | 96 |
!FileStream class methodsFor:'instance creation'! |
97 |
||
613 | 98 |
appendingOldFileNamed:filename |
99 |
"return a FileStream for existing file named filename, aString. |
|
100 |
The file is opened for writeonly access." |
|
1 | 101 |
|
102 |
|newStream| |
|
2 | 103 |
newStream := self new pathName:filename. |
613 | 104 |
newStream openForAppending isNil ifTrue:[^nil]. |
105 |
" |
|
106 |
this is not a good idea; I might like to read the written stuff ... |
|
107 |
||
108 |
newStream readLimit:(newStream size). |
|
109 |
" |
|
110 |
^ newStream |
|
111 |
! |
|
112 |
||
113 |
appendingOldFileNamed:filename in:aDirectory |
|
114 |
"return a FileStream for existing file named filename, aString |
|
115 |
in aDirectory, a FileDirectory. |
|
116 |
The file is opened for writeonly access." |
|
117 |
||
118 |
|newStream| |
|
119 |
newStream := self new pathName:filename in:aDirectory. |
|
120 |
newStream openForAppending isNil ifTrue:[^nil]. |
|
121 |
" |
|
122 |
this is not a good idea; I might like to read the written stuff ... |
|
123 |
||
124 |
newStream readLimit:(newStream size). |
|
125 |
" |
|
1 | 126 |
^ newStream |
127 |
! |
|
128 |
||
613 | 129 |
fileNamed:filename |
130 |
"return a stream on file filename - if the file does not |
|
131 |
already exist, create it. |
|
92 | 132 |
The file is opened for read/write access." |
1 | 133 |
|
613 | 134 |
|stream| |
135 |
||
136 |
stream := self oldFileNamed:filename. |
|
137 |
stream isNil ifTrue:[ |
|
138 |
stream := self newFileNamed:filename |
|
139 |
]. |
|
140 |
^ stream |
|
141 |
! |
|
142 |
||
143 |
fileNamed:filename in:aDirectory |
|
144 |
"return a stream on file filename - if the file does not |
|
145 |
already exist, create it. |
|
146 |
The file is opened for read/write access." |
|
147 |
||
148 |
|stream| |
|
149 |
||
150 |
stream := self oldFileNamed:filename in:aDirectory. |
|
151 |
stream isNil ifTrue:[ |
|
152 |
stream := self newFileNamed:filename in:aDirectory |
|
153 |
]. |
|
154 |
^ stream |
|
1 | 155 |
! |
156 |
||
92 | 157 |
newFileForWritingNamed:filename |
158 |
"return a FileStream for new file named filename, aString. |
|
159 |
If the file exists, it is truncated, otherwise created. |
|
160 |
The file is opened for writeonly access." |
|
161 |
||
162 |
|newStream| |
|
163 |
newStream := self new pathName:filename. |
|
164 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
165 |
^ newStream |
|
166 |
! |
|
167 |
||
168 |
newFileForWritingNamed:filename in:aDirectory |
|
169 |
"return a FileStream for new file named filename, aString |
|
170 |
in aDirectory, a FileDirectory. |
|
171 |
If the file exists, it is truncated, otherwise created. |
|
172 |
The file is opened for writeonly access." |
|
173 |
||
174 |
|newStream| |
|
175 |
newStream := self new pathName:filename in:aDirectory. |
|
176 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
177 |
^ newStream |
|
178 |
! |
|
179 |
||
613 | 180 |
newFileNamed:filename |
181 |
"return a FileStream for new file named filename, aString. |
|
182 |
If the file exists, it is truncated, otherwise created. |
|
183 |
The file is opened for read/write access." |
|
184 |
||
185 |
|newStream| |
|
186 |
newStream := self new pathName:filename. |
|
187 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
188 |
^ newStream |
|
189 |
! |
|
190 |
||
191 |
newFileNamed:filename in:aDirectory |
|
192 |
"return a FileStream for new file named filename, aString |
|
193 |
in aDirectory, a FileDirectory. |
|
194 |
If the file exists, it is truncated, otherwise created. |
|
195 |
The file is opened for read/write access." |
|
196 |
||
197 |
|newStream| |
|
198 |
newStream := self new pathName:filename in:aDirectory. |
|
199 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
200 |
^ newStream |
|
201 |
! |
|
202 |
||
1 | 203 |
oldFileNamed:filename |
204 |
"return a FileStream for existing file named filename, aString. |
|
205 |
The file is opened for read/write access." |
|
206 |
||
207 |
|newStream| |
|
208 |
||
209 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
2 | 210 |
newStream := self new pathName:filename. |
1 | 211 |
newStream readwrite. |
212 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
|
42 | 213 |
" |
214 |
this is not a good idea; someone else might be appending ... |
|
215 |
||
1 | 216 |
newStream readLimit:(newStream size). |
42 | 217 |
" |
1 | 218 |
^ newStream |
219 |
! |
|
220 |
||
221 |
oldFileNamed:filename in:aDirectory |
|
222 |
"return a FileStream for existing file named filename, aString |
|
223 |
in aDirectory, a FileDirectory. |
|
224 |
The file is opened for read/write access." |
|
225 |
||
226 |
|newStream| |
|
2 | 227 |
newStream := self new pathName:filename in:aDirectory. |
1 | 228 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
42 | 229 |
" |
230 |
this is not a good idea; someone else might be appending ... |
|
231 |
||
1 | 232 |
newStream readLimit:(newStream size). |
42 | 233 |
" |
1 | 234 |
^ newStream |
235 |
! |
|
236 |
||
237 |
readonlyFileNamed:filename |
|
238 |
"return a readonly FileStream for existing file named filename, aString" |
|
239 |
||
240 |
|newStream| |
|
241 |
||
242 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
243 |
||
2 | 244 |
newStream := self new pathName:filename. |
1 | 245 |
newStream openForReading isNil ifTrue:[^nil]. |
42 | 246 |
" |
247 |
this is not a good idea; someone else might be appending ... |
|
248 |
||
1 | 249 |
newStream readLimit:(newStream size). |
42 | 250 |
" |
1 | 251 |
^ newStream |
252 |
! |
|
253 |
||
254 |
readonlyFileNamed:filename in:aDirectory |
|
255 |
"return a readonly FileStream for existing file named filename, aString |
|
256 |
in aDirectory, a FileDirectory" |
|
257 |
||
258 |
|newStream| |
|
2 | 259 |
newStream := self new pathName:filename in:aDirectory. |
1 | 260 |
newStream openForReading isNil ifTrue:[^nil]. |
42 | 261 |
" |
262 |
this is not a good idea; someone else might be appending ... |
|
263 |
||
1 | 264 |
newStream readLimit:(newStream size). |
42 | 265 |
" |
1 | 266 |
^ newStream |
159 | 267 |
! ! |
268 |
||
1 | 269 |
!FileStream methodsFor:'accessing'! |
270 |
||
271 |
directoryName |
|
272 |
"return the name of the directory I'm in" |
|
273 |
||
274 |
|path lastIndex index| |
|
275 |
||
276 |
path := pathName. |
|
277 |
lastIndex := 0. |
|
278 |
index := path indexOf:$/. |
|
279 |
[index ~~ 0] whileTrue:[ |
|
159 | 280 |
lastIndex := index. |
281 |
index := path indexOf:$/ startingAt:(index + 1) |
|
1 | 282 |
]. |
283 |
(lastIndex == 0) ifTrue:[^ '.']. |
|
284 |
(lastIndex == 1) ifTrue:[^ '/']. |
|
31 | 285 |
^ path copyTo:(lastIndex - 1) |
1 | 286 |
! |
287 |
||
288 |
name |
|
289 |
"return my name without leading direcory-path" |
|
290 |
||
291 |
|lastIndex index| |
|
292 |
||
293 |
lastIndex := 1. |
|
294 |
[true] whileTrue:[ |
|
159 | 295 |
index := pathName indexOf:$/ startingAt:lastIndex. |
296 |
(index == 0) ifTrue:[ |
|
297 |
^ pathName copyFrom:lastIndex |
|
298 |
]. |
|
299 |
lastIndex := index + 1 |
|
1 | 300 |
] |
301 |
! |
|
302 |
||
303 |
pathName |
|
304 |
"return the pathname" |
|
305 |
||
306 |
^ pathName |
|
613 | 307 |
! |
308 |
||
309 |
store:something |
|
310 |
"what really should this do" |
|
311 |
||
312 |
self nextPutAll:something storeString |
|
313 |
! ! |
|
314 |
||
315 |
!FileStream methodsFor:'error handling'! |
|
316 |
||
317 |
openError |
|
318 |
"report an error, that file open failed" |
|
319 |
||
320 |
"/ |
|
321 |
"/ for now, do not raise any signal (see super>>openError). |
|
322 |
"/ Its not yet handled anywhere. Instead, senders of open |
|
323 |
"/ check for nil return value (which is a historic leftover) |
|
324 |
"/ |
|
325 |
LastErrorNumber := lastErrorNumber. |
|
326 |
^ nil. |
|
327 |
! ! |
|
328 |
||
329 |
!FileStream methodsFor:'printing & storing'! |
|
330 |
||
331 |
printOn:aStream |
|
332 |
aStream nextPutAll:'(a FileStream for:'. |
|
333 |
aStream nextPutAll:pathName. |
|
334 |
aStream nextPut:$) |
|
335 |
! |
|
336 |
||
337 |
storeOn:aStream |
|
338 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
|
339 |
aStream nextPutAll:pathName. |
|
340 |
(self position ~~ 1) ifTrue:[ |
|
341 |
aStream nextPutAll:'; position:'. |
|
342 |
self position storeOn:aStream |
|
343 |
]. |
|
344 |
aStream nextPut:$) |
|
1 | 345 |
! ! |
346 |
||
347 |
!FileStream methodsFor:'private'! |
|
348 |
||
613 | 349 |
createForReadWrite |
350 |
"create/truncate the file for read/write. |
|
351 |
If the file existed, its truncated; otherwise its created." |
|
1 | 352 |
|
613 | 353 |
|
354 |
mode := #readwrite. |
|
355 |
^ self openWithMode:'w+' |
|
1 | 356 |
! |
357 |
||
613 | 358 |
createForWriting |
359 |
"create/truncate the file for writeonly. |
|
360 |
If the file existed, its truncated; otherwise its created." |
|
1 | 361 |
|
613 | 362 |
mode := #writeonly. |
363 |
didWrite := true. |
|
364 |
^ self openWithMode:'w' |
|
1 | 365 |
! |
366 |
||
367 |
open |
|
368 |
"open the file" |
|
369 |
||
370 |
pathName isNil ifTrue:[^nil]. |
|
371 |
(mode == #readonly) ifTrue: [ |
|
159 | 372 |
didWrite := false. |
373 |
^ self openWithMode:'r' |
|
1 | 374 |
]. |
375 |
(mode == #writeonly) ifTrue: [ |
|
159 | 376 |
didWrite := true. |
377 |
^ self openWithMode:'w' |
|
1 | 378 |
]. |
77 | 379 |
^ self openWithMode:'r+' |
1 | 380 |
! |
381 |
||
613 | 382 |
openForAppending |
383 |
"open the file for writeonly appending to the end. |
|
384 |
If the file does not exist its an error, return nil; |
|
385 |
otherwise return the receiver." |
|
386 |
||
387 |
mode := #writeonly. |
|
388 |
didWrite := true. |
|
389 |
^ self openWithMode:'a+' |
|
390 |
! |
|
391 |
||
392 |
openForReadWrite |
|
393 |
"open the file for read/write. |
|
394 |
If the file does not exist its an error, return nil; |
|
395 |
otherwise return the receiver." |
|
396 |
||
397 |
mode := #readwrite. |
|
398 |
^ self openWithMode:'r+' |
|
399 |
! |
|
400 |
||
401 |
openForReading |
|
402 |
"open the file for readonly. |
|
403 |
If the file does not exist its an error, return nil; |
|
404 |
otherwise return the receiver." |
|
405 |
||
406 |
mode := #readonly. |
|
407 |
didWrite := false. |
|
408 |
^ self openWithMode:'r' |
|
409 |
! |
|
410 |
||
411 |
openForWriting |
|
412 |
"open the file writeonly. |
|
413 |
If the file does not exist its an error, return nil; |
|
414 |
otherwise return the receiver." |
|
415 |
||
416 |
mode := #writeonly. |
|
417 |
didWrite := true. |
|
418 |
^ self openWithMode:'r+' "unix-io does not allow this; open for update here" |
|
419 |
! |
|
420 |
||
1 | 421 |
openWithMode:openmode |
422 |
"open the file; openmode is the string defining the way to open" |
|
423 |
||
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
424 |
|retVal laspass| |
159 | 425 |
|
426 |
filePointer notNil ifTrue:[^ self errorOpen]. |
|
1 | 427 |
%{ |
428 |
FILE *f; |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
429 |
OBJ path, fp; |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
430 |
int pass = 0; |
1 | 431 |
|
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
432 |
retry: |
1133 | 433 |
path = __INST(pathName); |
328 | 434 |
if (__isNonNilObject(path) && (__qClass(path)==String)) { |
2161 | 435 |
__BEGIN_INTERRUPTABLE__ |
436 |
do { |
|
13 | 437 |
#ifdef LINUX |
2161 | 438 |
/* LINUX returns a non-NULL f even when interrupted */ |
439 |
errno = 0; |
|
440 |
f = (FILE *) fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
|
441 |
if (errno == EINTR) |
|
442 |
f = NULL; |
|
13 | 443 |
#else |
444 |
||
2161 | 445 |
f = (FILE *) fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
13 | 446 |
#endif |
2161 | 447 |
} while ((f == NULL) && (errno == EINTR)); |
448 |
__END_INTERRUPTABLE__ |
|
449 |
if (f == NULL) { |
|
450 |
/* |
|
451 |
* If no filedescriptors available, try to finalize |
|
452 |
* possibly collected fd's and try again. |
|
453 |
*/ |
|
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
454 |
|
2161 | 455 |
if (pass == 0 && (errno == ENFILE || errno == EMFILE)) { |
456 |
pass = 1; |
|
457 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
|
458 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
|
459 |
goto retry; |
|
460 |
} |
|
461 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
462 |
__INST(position) = nil; |
|
463 |
} else { |
|
464 |
__INST(filePointer) = fp = __MKOBJ((int)f); __STORE(self, fp); |
|
465 |
__INST(position) = __MKSMALLINT(1); |
|
466 |
retVal = self; |
|
467 |
} |
|
1 | 468 |
} |
159 | 469 |
%}. |
1 | 470 |
retVal notNil ifTrue:[ |
2161 | 471 |
buffered := true. "default is buffered" |
472 |
Lobby register:self. |
|
473 |
^ retVal |
|
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
474 |
] ifFalse:[ |
2161 | 475 |
" |
476 |
the open failed for some reason ... |
|
477 |
" |
|
478 |
^ self openError |
|
1 | 479 |
]. |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
480 |
|
1 | 481 |
! |
482 |
||
613 | 483 |
pathName:filename |
484 |
"set the pathname" |
|
1 | 485 |
|
613 | 486 |
pathName := filename |
1 | 487 |
! |
488 |
||
613 | 489 |
pathName:filename in:aDirectory |
490 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 491 |
|
613 | 492 |
((filename at:1) == $/) ifTrue:[ |
493 |
"filename may not start with a '/'" |
|
494 |
pathName := nil |
|
495 |
] ifFalse:[ |
|
496 |
pathName := aDirectory pathName. |
|
497 |
(pathName endsWith:'/') ifFalse:[ |
|
498 |
pathName := pathName , '/' |
|
499 |
]. |
|
500 |
pathName := pathName , filename |
|
501 |
] |
|
1 | 502 |
! |
503 |
||
504 |
reOpen |
|
505 |
"sent after snapin to reopen streams" |
|
506 |
||
173 | 507 |
|oldPos| |
508 |
||
1 | 509 |
filePointer notNil ifTrue:[ |
2161 | 510 |
"it was open, when snapped-out" |
511 |
filePointer := nil. |
|
512 |
Lobby unregister:self. |
|
513 |
oldPos := position. |
|
514 |
self open. |
|
515 |
filePointer isNil ifTrue:[ |
|
516 |
"this happens, if after a restart, the file is no longer accessable ..." |
|
13 | 517 |
|
2161 | 518 |
(self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. |
519 |
] ifFalse:[ |
|
520 |
self position:oldPos. |
|
521 |
] |
|
1 | 522 |
] |
1088 | 523 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
524 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
31 | 525 |
! ! |
526 |
||
527 |
!FileStream methodsFor:'queries'! |
|
1 | 528 |
|
529 |
position |
|
530 |
"return the read/write position in the file - |
|
31 | 531 |
notice, in smalltalk indices start at 1 so begin of file is 1" |
1 | 532 |
|
533 |
%{ /* NOCONTEXT */ |
|
534 |
||
535 |
FILE *f; |
|
536 |
long currentPosition; |
|
537 |
||
1133 | 538 |
if (__INST(filePointer) != nil) { |
539 |
f = __FILEVal(__INST(filePointer)); |
|
159 | 540 |
do { |
1133 | 541 |
if (__INST(buffered) == true) { |
159 | 542 |
currentPosition = (long) ftell(f); |
543 |
} else { |
|
544 |
currentPosition = (long) lseek(fileno(f), 0L, SEEK_CUR); |
|
545 |
} |
|
546 |
} while ((currentPosition < 0) && (errno == EINTR)); |
|
547 |
if (currentPosition >= 0) { |
|
548 |
/* |
|
549 |
* notice: Smalltalk index starts at 1 |
|
550 |
*/ |
|
1133 | 551 |
RETURN ( __MKSMALLINT(currentPosition + 1) ); |
159 | 552 |
} |
1133 | 553 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
1 | 554 |
} |
159 | 555 |
%}. |
556 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1 | 557 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
558 |
^ self primitiveFailed |
|
559 |
! |
|
560 |
||
561 |
position:newPos |
|
562 |
"set the read/write position in the file" |
|
563 |
||
564 |
%{ /* NOCONTEXT */ |
|
565 |
||
566 |
FILE *f; |
|
10 | 567 |
int ret; |
249 | 568 |
OBJ fp; |
1 | 569 |
|
1133 | 570 |
if ((fp = __INST(filePointer)) != nil) { |
249 | 571 |
if (__isSmallInteger(newPos)) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
437
diff
changeset
|
572 |
f = __FILEVal(fp); |
159 | 573 |
/* |
574 |
* notice: Smalltalk index starts at 1 |
|
575 |
*/ |
|
576 |
do { |
|
1133 | 577 |
if (__INST(buffered) == true) { |
578 |
ret = fseek(f, (long) (__intVal(newPos) - 1), SEEK_SET); |
|
159 | 579 |
} else { |
1133 | 580 |
ret = (long) lseek(fileno(f), (long)(__intVal(newPos) - 1), SEEK_SET); |
159 | 581 |
} |
582 |
} while ((ret < 0) && (errno == EINTR)); |
|
583 |
if (ret >= 0) { |
|
1133 | 584 |
__INST(position) = newPos; |
159 | 585 |
/* |
586 |
* just to make certain ... |
|
587 |
*/ |
|
1133 | 588 |
__INST(hitEOF) = false; |
159 | 589 |
RETURN ( self ); |
590 |
} |
|
1133 | 591 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 592 |
} |
1 | 593 |
} |
159 | 594 |
%}. |
595 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1 | 596 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
597 |
^ self primitiveFailed |
|
598 |
! |
|
599 |
||
600 |
setToEnd |
|
601 |
"set the read/write position in the file to be at the end of the file" |
|
602 |
||
603 |
%{ |
|
604 |
FILE *f; |
|
10 | 605 |
int ret; |
1 | 606 |
|
1133 | 607 |
if (__INST(filePointer) != nil) { |
608 |
f = __FILEVal(__INST(filePointer)); |
|
609 |
__INST(position) = nil; |
|
159 | 610 |
do { |
1133 | 611 |
if (__INST(buffered) == true) { |
159 | 612 |
ret = fseek(f, 0L, SEEK_END); |
613 |
} else { |
|
614 |
ret = (long)lseek(fileno(f), 0L, SEEK_END); |
|
615 |
} |
|
616 |
} while ((ret < 0) && (errno == EINTR)); |
|
617 |
if (ret >= 0) { |
|
618 |
RETURN ( self ); |
|
619 |
} |
|
1133 | 620 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
1 | 621 |
} |
159 | 622 |
%}. |
623 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
49 | 624 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
1 | 625 |
^ self primitiveFailed |
613 | 626 |
! |
627 |
||
628 |
size |
|
629 |
"return the size in bytes of the file" |
|
630 |
||
631 |
%{ /* NOCONTEXT */ |
|
632 |
||
633 |
#ifdef transputer |
|
634 |
FILE *f; |
|
635 |
int size; |
|
636 |
||
1133 | 637 |
if (__INST(filePointer) != nil) { |
638 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 639 |
if ((size = filesize(fileno(f))) >= 0) { |
1133 | 640 |
RETURN ( __MKSMALLINT(size) ); |
613 | 641 |
} |
642 |
} |
|
643 |
#else |
|
644 |
FILE *f; |
|
645 |
struct stat buf; |
|
646 |
int ret; |
|
647 |
int fd; |
|
648 |
||
1133 | 649 |
if (__INST(filePointer) != nil) { |
650 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 651 |
fd = fileno(f); |
652 |
do { |
|
653 |
ret = fstat(fd, &buf); |
|
654 |
} while ((ret < 0) && (errno == EINTR)); |
|
655 |
if (ret >= 0) { |
|
1133 | 656 |
RETURN ( __MKSMALLINT(buf.st_size) ); |
613 | 657 |
} |
1133 | 658 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
613 | 659 |
} |
660 |
#endif |
|
661 |
%}. |
|
662 |
||
663 |
"could add a fall-back here: |
|
664 |
||
665 |
oldPosition := self position. |
|
666 |
self setToEnd. |
|
667 |
sz := self position. |
|
668 |
self position:oldPosition. |
|
669 |
^ sz |
|
670 |
" |
|
671 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
672 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
673 |
^ self primitiveFailed |
|
1 | 674 |
! ! |
675 |
||
2 | 676 |
!FileStream methodsFor:'testing'! |
677 |
||
678 |
isFileStream |
|
679 |
"return true, if the receiver is some kind of fileStream. |
|
680 |
redefined from Object" |
|
681 |
||
682 |
^ true |
|
683 |
! ! |
|
684 |
||
1088 | 685 |
!FileStream class methodsFor:'documentation'! |
686 |
||
687 |
version |
|
2161 | 688 |
^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.36 1997-01-14 11:49:01 cg Exp $' |
1088 | 689 |
! ! |