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