1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'pathName'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
FileStream comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
22 |
All Rights Reserved
|
|
23 |
|
|
24 |
This class provides access to the operating systems underlying file
|
|
25 |
system (i.e. its an interface to the stdio library).
|
|
26 |
|
31
|
27 |
$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.7 1994-01-08 16:18:37 claus Exp $
|
1
|
28 |
'!
|
|
29 |
|
|
30 |
%{
|
|
31 |
#include <stdio.h>
|
10
|
32 |
#include <errno.h>
|
1
|
33 |
|
|
34 |
#ifdef transputer
|
|
35 |
# include <iocntrl.h>
|
|
36 |
# ifndef fileno
|
|
37 |
/* kludge: inmos forgot fileno */
|
|
38 |
# define fileno(f) ((f)->__file)
|
|
39 |
# endif
|
|
40 |
#else
|
|
41 |
# include <sys/types.h>
|
|
42 |
# include <sys/stat.h>
|
|
43 |
#endif
|
10
|
44 |
|
|
45 |
#ifndef SEEK_SET
|
13
|
46 |
# define SEEK_SET 0
|
10
|
47 |
#endif
|
|
48 |
#ifndef SEEK_CUR
|
13
|
49 |
# define SEEK_CUR 1
|
10
|
50 |
#endif
|
|
51 |
#ifndef SEEK_END
|
13
|
52 |
# define SEEK_END 2
|
10
|
53 |
#endif
|
|
54 |
|
1
|
55 |
%}
|
|
56 |
|
|
57 |
!FileStream class methodsFor:'instance creation'!
|
|
58 |
|
|
59 |
newFileNamed:filename
|
|
60 |
"return a FileStream for new file named filename, aString.
|
|
61 |
If the file exists, it is truncated, otherwise created.
|
|
62 |
The file is opened for write access only."
|
|
63 |
|
|
64 |
|newStream|
|
2
|
65 |
newStream := self new pathName:filename.
|
1
|
66 |
newStream createForWriting isNil ifTrue:[^nil].
|
|
67 |
^ newStream
|
|
68 |
!
|
|
69 |
|
|
70 |
newFileNamed:filename in:aDirectory
|
|
71 |
"return a FileStream for new file named filename, aString
|
|
72 |
in aDirectory, a FileDirectory.
|
|
73 |
If the file exists, it is truncated, otherwise created.
|
|
74 |
The file is opened for write access only."
|
|
75 |
|
|
76 |
|newStream|
|
2
|
77 |
newStream := self new pathName:filename in:aDirectory.
|
1
|
78 |
newStream createForWriting isNil ifTrue:[^nil].
|
|
79 |
^ newStream
|
|
80 |
!
|
|
81 |
|
|
82 |
oldFileNamed:filename
|
|
83 |
"return a FileStream for existing file named filename, aString.
|
|
84 |
The file is opened for read/write access."
|
|
85 |
|
|
86 |
|newStream|
|
|
87 |
|
|
88 |
(OperatingSystem isReadable:filename) ifFalse:[^nil].
|
2
|
89 |
newStream := self new pathName:filename.
|
1
|
90 |
newStream readwrite.
|
|
91 |
newStream openForReadWrite isNil ifTrue:[^nil].
|
|
92 |
newStream readLimit:(newStream size).
|
|
93 |
^ newStream
|
|
94 |
!
|
|
95 |
|
|
96 |
oldFileNamed:filename in:aDirectory
|
|
97 |
"return a FileStream for existing file named filename, aString
|
|
98 |
in aDirectory, a FileDirectory.
|
|
99 |
The file is opened for read/write access."
|
|
100 |
|
|
101 |
|newStream|
|
2
|
102 |
newStream := self new pathName:filename in:aDirectory.
|
1
|
103 |
newStream openForReadWrite isNil ifTrue:[^nil].
|
|
104 |
newStream readLimit:(newStream size).
|
|
105 |
^ newStream
|
|
106 |
!
|
|
107 |
|
|
108 |
fileNamed:filename
|
|
109 |
"return a stream on file filename - if the file does not
|
|
110 |
already exist, create it."
|
|
111 |
|
|
112 |
|stream|
|
|
113 |
|
|
114 |
stream := self oldFileNamed:filename.
|
|
115 |
stream isNil ifTrue:[
|
|
116 |
stream := self newFileNamed:filename
|
|
117 |
].
|
|
118 |
^ stream
|
|
119 |
!
|
|
120 |
|
|
121 |
fileNamed:filename in:aDirectory
|
|
122 |
"return a stream on file filename - if the file does not
|
|
123 |
already exist, create it."
|
|
124 |
|
|
125 |
|stream|
|
|
126 |
|
|
127 |
stream := self oldFileNamed:filename in:aDirectory.
|
|
128 |
stream isNil ifTrue:[
|
|
129 |
stream := self newFileNamed:filename in:aDirectory
|
|
130 |
].
|
|
131 |
^ stream
|
|
132 |
!
|
|
133 |
|
|
134 |
readonlyFileNamed:filename
|
|
135 |
"return a readonly FileStream for existing file named filename, aString"
|
|
136 |
|
|
137 |
|newStream|
|
|
138 |
|
|
139 |
(OperatingSystem isReadable:filename) ifFalse:[^nil].
|
|
140 |
|
2
|
141 |
newStream := self new pathName:filename.
|
1
|
142 |
newStream openForReading isNil ifTrue:[^nil].
|
|
143 |
newStream readLimit:(newStream size).
|
|
144 |
^ newStream
|
|
145 |
!
|
|
146 |
|
|
147 |
readonlyFileNamed:filename in:aDirectory
|
|
148 |
"return a readonly FileStream for existing file named filename, aString
|
|
149 |
in aDirectory, a FileDirectory"
|
|
150 |
|
|
151 |
|newStream|
|
2
|
152 |
newStream := self new pathName:filename in:aDirectory.
|
1
|
153 |
newStream openForReading isNil ifTrue:[^nil].
|
|
154 |
newStream readLimit:(newStream size).
|
|
155 |
^ newStream
|
|
156 |
!
|
|
157 |
|
|
158 |
appendingOldFileNamed:filename
|
|
159 |
"return a FileStream for existing file named filename, aString"
|
|
160 |
|
|
161 |
|newStream|
|
2
|
162 |
newStream := self new pathName:filename.
|
1
|
163 |
newStream openForAppending isNil ifTrue:[^nil].
|
|
164 |
newStream readLimit:(newStream size).
|
|
165 |
^ newStream
|
|
166 |
!
|
|
167 |
|
|
168 |
appendingOldFileNamed:filename in:aDirectory
|
|
169 |
"return a FileStream for existing file named filename, aString
|
|
170 |
in aDirectory, a FileDirectory"
|
|
171 |
|
|
172 |
|newStream|
|
2
|
173 |
newStream := self new pathName:filename in:aDirectory.
|
1
|
174 |
newStream openForAppending isNil ifTrue:[^nil].
|
|
175 |
newStream readLimit:(newStream size).
|
|
176 |
^ newStream
|
|
177 |
! !
|
|
178 |
|
|
179 |
!FileStream methodsFor:'accessing'!
|
|
180 |
|
|
181 |
store:something
|
|
182 |
"what really should this do"
|
|
183 |
|
31
|
184 |
self nextPutAll:something storeString
|
1
|
185 |
!
|
|
186 |
|
|
187 |
directoryName
|
|
188 |
"return the name of the directory I'm in"
|
|
189 |
|
|
190 |
|path lastIndex index|
|
|
191 |
|
|
192 |
path := pathName.
|
|
193 |
lastIndex := 0.
|
|
194 |
index := path indexOf:$/.
|
|
195 |
[index ~~ 0] whileTrue:[
|
|
196 |
lastIndex := index.
|
|
197 |
index := path indexOf:$/ startingAt:(index + 1)
|
|
198 |
].
|
|
199 |
(lastIndex == 0) ifTrue:[^ '.'].
|
|
200 |
(lastIndex == 1) ifTrue:[^ '/'].
|
31
|
201 |
^ path copyTo:(lastIndex - 1)
|
1
|
202 |
!
|
|
203 |
|
|
204 |
name
|
|
205 |
"return my name without leading direcory-path"
|
|
206 |
|
|
207 |
|lastIndex index|
|
|
208 |
|
|
209 |
lastIndex := 1.
|
|
210 |
[true] whileTrue:[
|
|
211 |
index := pathName indexOf:$/ startingAt:lastIndex.
|
|
212 |
(index == 0) ifTrue:[
|
|
213 |
^ pathName copyFrom:lastIndex
|
|
214 |
].
|
|
215 |
lastIndex := index + 1
|
|
216 |
]
|
|
217 |
!
|
|
218 |
|
|
219 |
pathName
|
|
220 |
"return the pathname"
|
|
221 |
|
|
222 |
^ pathName
|
|
223 |
! !
|
|
224 |
|
|
225 |
!FileStream methodsFor:'private'!
|
|
226 |
|
|
227 |
pathName:filename
|
|
228 |
"set the pathname"
|
|
229 |
|
|
230 |
pathName := filename
|
|
231 |
!
|
|
232 |
|
|
233 |
pathName:filename in:aDirectory
|
|
234 |
"set the pathname starting at aDirectory, a FileDirectory"
|
|
235 |
|
|
236 |
((filename at:1) == $/) ifTrue:[
|
|
237 |
"filename may not start with a '/'"
|
|
238 |
pathName := nil
|
|
239 |
] ifFalse:[
|
|
240 |
pathName := aDirectory pathName.
|
|
241 |
(pathName endsWith:'/') ifFalse:[
|
|
242 |
pathName := pathName , '/'
|
|
243 |
].
|
|
244 |
pathName := pathName , filename
|
|
245 |
]
|
|
246 |
!
|
|
247 |
|
|
248 |
open
|
|
249 |
"open the file"
|
|
250 |
|
|
251 |
pathName isNil ifTrue:[^nil].
|
|
252 |
(mode == #readonly) ifTrue: [
|
|
253 |
^ self openForReading
|
|
254 |
].
|
|
255 |
(mode == #writeonly) ifTrue: [
|
|
256 |
^ self openForWriting
|
|
257 |
].
|
|
258 |
^ self openForReadWrite
|
|
259 |
!
|
|
260 |
|
|
261 |
openWithMode:openmode
|
|
262 |
"open the file; openmode is the string defining the way to open"
|
|
263 |
|
|
264 |
|retVal|
|
|
265 |
%{
|
|
266 |
FILE *f;
|
|
267 |
OBJ path;
|
|
268 |
extern OBJ ErrorNumber, Filename;
|
|
269 |
extern errno;
|
|
270 |
|
|
271 |
if (_INST(filePointer) == nil) {
|
|
272 |
path = _INST(pathName);
|
10
|
273 |
if (path != nil
|
13
|
274 |
&& ((_qClass(path)==String) || (_qClass(path) == Filename))) {
|
|
275 |
do {
|
|
276 |
#ifdef LINUX
|
|
277 |
/* LINUX returns a non-NULL f even when interrupted */
|
|
278 |
errno = 0;
|
10
|
279 |
f = (FILE *)fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
|
13
|
280 |
if (errno == EINTR)
|
|
281 |
f = NULL;
|
|
282 |
#else
|
|
283 |
|
|
284 |
f = (FILE *)fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
|
|
285 |
#endif
|
|
286 |
} while ((f == NULL) && (errno == EINTR));
|
1
|
287 |
if (f == NULL) {
|
|
288 |
ErrorNumber = _MKSMALLINT(errno);
|
|
289 |
_INST(position) = nil;
|
|
290 |
} else {
|
|
291 |
_INST(filePointer) = _MKSMALLINT((int)f);
|
|
292 |
_INST(position) = _MKSMALLINT(1);
|
|
293 |
retVal = self;
|
|
294 |
}
|
|
295 |
}
|
|
296 |
}
|
|
297 |
%}
|
|
298 |
.
|
|
299 |
retVal notNil ifTrue:[
|
13
|
300 |
buffered := true. "default is buffered"
|
31
|
301 |
Lobby register:self
|
1
|
302 |
].
|
|
303 |
^ retVal
|
|
304 |
!
|
|
305 |
|
|
306 |
openForReading
|
|
307 |
"open the file for readonly"
|
|
308 |
|
|
309 |
mode := #readonly.
|
|
310 |
^ self openWithMode:'r'
|
|
311 |
!
|
|
312 |
|
|
313 |
openForWriting
|
|
314 |
"open the file for writeonly"
|
|
315 |
|
|
316 |
mode := #writeonly.
|
|
317 |
^ self openWithMode:'w'
|
|
318 |
!
|
|
319 |
|
|
320 |
openForAppending
|
|
321 |
"open the file for writeonly appending to the end"
|
|
322 |
|
|
323 |
mode := #writeonly.
|
|
324 |
^ self openWithMode:'a+'
|
|
325 |
!
|
|
326 |
|
|
327 |
createForWriting
|
|
328 |
"create/truncate the file for writeonly"
|
|
329 |
|
|
330 |
mode := #writeonly.
|
|
331 |
^ self openWithMode:'w+'
|
|
332 |
!
|
|
333 |
|
|
334 |
openForReadWrite
|
|
335 |
"open the file for read/write"
|
|
336 |
|
|
337 |
mode := #readwrite.
|
|
338 |
^ self openWithMode:'r+w'
|
|
339 |
!
|
|
340 |
|
|
341 |
createForReadWrite
|
|
342 |
"create/truncate the file for read/write"
|
|
343 |
|
|
344 |
mode := #readwrite.
|
|
345 |
^ self openWithMode:'rw+'
|
|
346 |
!
|
|
347 |
|
|
348 |
reOpen
|
|
349 |
"sent after snapin to reopen streams"
|
|
350 |
|
|
351 |
filePointer notNil ifTrue:[
|
|
352 |
"it was open, when snapped-out"
|
|
353 |
filePointer := nil.
|
31
|
354 |
Lobby unregister:self.
|
1
|
355 |
self open.
|
|
356 |
filePointer isNil ifTrue:[
|
13
|
357 |
"this happens, if after a restart, the file is no longer accessable ..."
|
|
358 |
|
10
|
359 |
('could not reopen file: ', pathName) printNewline.
|
1
|
360 |
] ifFalse:[
|
2
|
361 |
self position:position.
|
1
|
362 |
]
|
|
363 |
]
|
31
|
364 |
! !
|
|
365 |
|
|
366 |
!FileStream methodsFor:'queries'!
|
1
|
367 |
|
|
368 |
size
|
|
369 |
"return the size in bytes of the file"
|
|
370 |
|
|
371 |
%{ /* NOCONTEXT */
|
|
372 |
|
|
373 |
#ifdef transputer
|
|
374 |
FILE *f;
|
|
375 |
int size;
|
|
376 |
|
|
377 |
if (_INST(filePointer) != nil) {
|
|
378 |
f = (FILE *)_intVal(_INST(filePointer));
|
|
379 |
if ((size = filesize(fileno(f))) >= 0) {
|
|
380 |
RETURN ( _MKSMALLINT(size) );
|
|
381 |
}
|
|
382 |
}
|
|
383 |
#else
|
|
384 |
FILE *f;
|
|
385 |
struct stat buf;
|
10
|
386 |
int ret;
|
|
387 |
extern errno;
|
|
388 |
extern OBJ ErrorNumber;
|
1
|
389 |
|
|
390 |
if (_INST(filePointer) != nil) {
|
|
391 |
f = (FILE *)_intVal(_INST(filePointer));
|
13
|
392 |
do {
|
10
|
393 |
ret = fstat(fileno(f), &buf);
|
13
|
394 |
} while ((ret < 0) && (errno == EINTR));
|
10
|
395 |
if (ret >= 0) {
|
1
|
396 |
RETURN ( _MKSMALLINT(buf.st_size) );
|
|
397 |
}
|
13
|
398 |
ErrorNumber = _MKSMALLINT(errno);
|
1
|
399 |
}
|
|
400 |
#endif
|
|
401 |
%}
|
|
402 |
.
|
|
403 |
"could add a fall-back here:
|
|
404 |
|
|
405 |
oldPosition := self position.
|
|
406 |
self setToEnd.
|
|
407 |
sz := self position.
|
|
408 |
self position:oldPosition.
|
|
409 |
^ sz
|
|
410 |
"
|
|
411 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
412 |
^ self primitiveFailed
|
|
413 |
!
|
|
414 |
|
|
415 |
position
|
|
416 |
"return the read/write position in the file -
|
31
|
417 |
notice, in smalltalk indices start at 1 so begin of file is 1"
|
1
|
418 |
|
|
419 |
%{ /* NOCONTEXT */
|
|
420 |
|
|
421 |
FILE *f;
|
|
422 |
long currentPosition;
|
10
|
423 |
extern errno;
|
|
424 |
extern OBJ ErrorNumber;
|
1
|
425 |
|
|
426 |
if (_INST(filePointer) != nil) {
|
|
427 |
f = (FILE *)_intVal(_INST(filePointer));
|
13
|
428 |
do {
|
|
429 |
if (_INST(buffered) == true) {
|
10
|
430 |
currentPosition = ftell(f);
|
13
|
431 |
} else {
|
|
432 |
currentPosition = (long)lseek(fileno(f), SEEK_CUR, 0);
|
|
433 |
}
|
|
434 |
} while ((currentPosition < 0) && (errno == EINTR));
|
1
|
435 |
if (currentPosition >= 0) {
|
|
436 |
/*
|
|
437 |
* notice: Smalltalk index starts at 1
|
|
438 |
*/
|
|
439 |
RETURN ( _MKSMALLINT(currentPosition + 1) );
|
|
440 |
}
|
13
|
441 |
ErrorNumber = _MKSMALLINT(errno);
|
1
|
442 |
}
|
|
443 |
%}
|
|
444 |
.
|
|
445 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
446 |
^ self primitiveFailed
|
|
447 |
!
|
|
448 |
|
|
449 |
position:newPos
|
|
450 |
"set the read/write position in the file"
|
|
451 |
|
|
452 |
%{ /* NOCONTEXT */
|
|
453 |
|
|
454 |
FILE *f;
|
10
|
455 |
int ret;
|
1
|
456 |
extern OBJ ErrorNumber;
|
|
457 |
extern errno;
|
|
458 |
|
|
459 |
if (_INST(filePointer) != nil) {
|
|
460 |
if (_isSmallInteger(newPos)) {
|
|
461 |
f = (FILE *)_intVal(_INST(filePointer));
|
|
462 |
/*
|
|
463 |
* notice: Smalltalk index starts at 1
|
|
464 |
*/
|
13
|
465 |
do {
|
|
466 |
if (_INST(buffered) == true) {
|
10
|
467 |
ret = fseek(f, _intVal(newPos) - 1, 0);
|
|
468 |
} else {
|
|
469 |
ret = lseek(fileno(f), (long)_intVal(newPos) - 1, SEEK_SET);
|
13
|
470 |
}
|
|
471 |
} while ((ret < 0) && (errno == EINTR));
|
10
|
472 |
if (ret >= 0) {
|
1
|
473 |
_INST(position) = newPos;
|
|
474 |
RETURN ( self );
|
|
475 |
}
|
|
476 |
ErrorNumber = _MKSMALLINT(errno);
|
|
477 |
}
|
|
478 |
}
|
|
479 |
%}
|
|
480 |
.
|
|
481 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
482 |
^ self primitiveFailed
|
|
483 |
!
|
|
484 |
|
|
485 |
setToEnd
|
|
486 |
"set the read/write position in the file to be at the end of the file"
|
|
487 |
|
|
488 |
filePointer isNil ifTrue:[^ self errorNotOpen].
|
|
489 |
%{
|
|
490 |
FILE *f;
|
10
|
491 |
int ret;
|
1
|
492 |
extern OBJ ErrorNumber;
|
|
493 |
extern errno;
|
|
494 |
|
|
495 |
f = (FILE *)_intVal(_INST(filePointer));
|
|
496 |
_INST(position) = nil;
|
10
|
497 |
do {
|
|
498 |
if (_INST(buffered) == true) {
|
|
499 |
ret = fseek(f, 0, 2);
|
|
500 |
} else {
|
|
501 |
ret = lseek(fileno(f), 0, SEEK_END);
|
|
502 |
}
|
|
503 |
} while ((ret < 0) && (errno == EINTR));
|
|
504 |
if (ret >= 0) {
|
1
|
505 |
RETURN ( self );
|
|
506 |
}
|
|
507 |
ErrorNumber = _MKSMALLINT(errno);
|
|
508 |
%}
|
|
509 |
.
|
|
510 |
^ self primitiveFailed
|
|
511 |
! !
|
|
512 |
|
2
|
513 |
!FileStream methodsFor:'testing'!
|
|
514 |
|
|
515 |
isFileStream
|
|
516 |
"return true, if the receiver is some kind of fileStream.
|
|
517 |
redefined from Object"
|
|
518 |
|
|
519 |
^ true
|
|
520 |
! !
|
|
521 |
|
1
|
522 |
!FileStream methodsFor:'printing & storing'!
|
|
523 |
|
|
524 |
printOn:aStream
|
|
525 |
aStream nextPutAll:'(a FileStream for:'.
|
|
526 |
aStream nextPutAll:pathName.
|
|
527 |
aStream nextPut:$)
|
|
528 |
!
|
|
529 |
|
|
530 |
storeOn:aStream
|
|
531 |
aStream nextPutAll:'(FileStream oldFileNamed:'.
|
|
532 |
aStream nextPutAll:pathName.
|
|
533 |
(self position ~~ 1) ifTrue:[
|
|
534 |
aStream nextPutAll:'; position:'.
|
|
535 |
self position storeOn:aStream
|
|
536 |
].
|
|
537 |
aStream nextPut:$)
|
|
538 |
! !
|