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