author | Claus Gittinger <cg@exept.de> |
Fri, 10 Jan 1997 19:01:32 +0100 | |
changeset 2134 | 246a3bdab8b4 |
parent 2078 | 0a5a557b5194 |
child 2161 | 0472a226a714 |
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:] |
|
89 |
Claus Gittinger |
|
90 |
||
91 |
[see also:] |
|
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)) { |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
435 |
__BEGIN_INTERRUPTABLE__ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
436 |
do { |
13 | 437 |
#ifdef LINUX |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
438 |
/* LINUX returns a non-NULL f even when interrupted */ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
439 |
errno = 0; |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
440 |
f = (FILE *) fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
441 |
if (errno == EINTR) |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
442 |
f = NULL; |
13 | 443 |
#else |
444 |
||
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
445 |
f = (FILE *) fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
13 | 446 |
#endif |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
447 |
} while ((f == NULL) && (errno == EINTR)); |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
448 |
__END_INTERRUPTABLE__ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
449 |
if (f == NULL) { |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
450 |
/* |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
451 |
* If no filedescriptors available, try to finalize |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
452 |
* possibly collected fd's and try again. |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
453 |
*/ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
454 |
|
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
455 |
if (pass == 0 && (errno == ENFILE || errno == EMFILE)) { |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
456 |
pass = 1; |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
457 |
%}. |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
458 |
ObjectMemory scavenge; finalize. |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
459 |
%{ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
460 |
goto retry; |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
461 |
} |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
462 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
463 |
__INST(position) = nil; |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
464 |
} else { |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
465 |
__INST(filePointer) = fp = __MKOBJ((int)f); __STORE(self, fp); |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
466 |
__INST(position) = __MKSMALLINT(1); |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
467 |
retVal = self; |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
468 |
} |
1 | 469 |
} |
159 | 470 |
%}. |
1 | 471 |
retVal notNil ifTrue:[ |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
472 |
buffered := true. "default is buffered" |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
473 |
Lobby register:self. |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
474 |
^ retVal |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
475 |
] ifFalse:[ |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
476 |
" |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
477 |
the open failed for some reason ... |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
478 |
" |
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
479 |
^ self openError |
1 | 480 |
]. |
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
481 |
|
1 | 482 |
! |
483 |
||
613 | 484 |
pathName:filename |
485 |
"set the pathname" |
|
1 | 486 |
|
613 | 487 |
pathName := filename |
1 | 488 |
! |
489 |
||
613 | 490 |
pathName:filename in:aDirectory |
491 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 492 |
|
613 | 493 |
((filename at:1) == $/) ifTrue:[ |
494 |
"filename may not start with a '/'" |
|
495 |
pathName := nil |
|
496 |
] ifFalse:[ |
|
497 |
pathName := aDirectory pathName. |
|
498 |
(pathName endsWith:'/') ifFalse:[ |
|
499 |
pathName := pathName , '/' |
|
500 |
]. |
|
501 |
pathName := pathName , filename |
|
502 |
] |
|
1 | 503 |
! |
504 |
||
505 |
reOpen |
|
506 |
"sent after snapin to reopen streams" |
|
507 |
||
173 | 508 |
|oldPos| |
509 |
||
1 | 510 |
filePointer notNil ifTrue:[ |
1088 | 511 |
"it was open, when snapped-out" |
512 |
filePointer := nil. |
|
513 |
Lobby unregister:self. |
|
514 |
oldPos := position. |
|
515 |
self open. |
|
516 |
filePointer isNil ifTrue:[ |
|
517 |
"this happens, if after a restart, the file is no longer accessable ..." |
|
13 | 518 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
519 |
(self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. |
1088 | 520 |
] ifFalse:[ |
521 |
self position:oldPos. |
|
522 |
] |
|
1 | 523 |
] |
1088 | 524 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
525 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
31 | 526 |
! ! |
527 |
||
528 |
!FileStream methodsFor:'queries'! |
|
1 | 529 |
|
530 |
position |
|
531 |
"return the read/write position in the file - |
|
31 | 532 |
notice, in smalltalk indices start at 1 so begin of file is 1" |
1 | 533 |
|
534 |
%{ /* NOCONTEXT */ |
|
535 |
||
536 |
FILE *f; |
|
537 |
long currentPosition; |
|
538 |
||
1133 | 539 |
if (__INST(filePointer) != nil) { |
540 |
f = __FILEVal(__INST(filePointer)); |
|
159 | 541 |
do { |
1133 | 542 |
if (__INST(buffered) == true) { |
159 | 543 |
currentPosition = (long) ftell(f); |
544 |
} else { |
|
545 |
currentPosition = (long) lseek(fileno(f), 0L, SEEK_CUR); |
|
546 |
} |
|
547 |
} while ((currentPosition < 0) && (errno == EINTR)); |
|
548 |
if (currentPosition >= 0) { |
|
549 |
/* |
|
550 |
* notice: Smalltalk index starts at 1 |
|
551 |
*/ |
|
1133 | 552 |
RETURN ( __MKSMALLINT(currentPosition + 1) ); |
159 | 553 |
} |
1133 | 554 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
1 | 555 |
} |
159 | 556 |
%}. |
557 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1 | 558 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
559 |
^ self primitiveFailed |
|
560 |
! |
|
561 |
||
562 |
position:newPos |
|
563 |
"set the read/write position in the file" |
|
564 |
||
565 |
%{ /* NOCONTEXT */ |
|
566 |
||
567 |
FILE *f; |
|
10 | 568 |
int ret; |
249 | 569 |
OBJ fp; |
1 | 570 |
|
1133 | 571 |
if ((fp = __INST(filePointer)) != nil) { |
249 | 572 |
if (__isSmallInteger(newPos)) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
437
diff
changeset
|
573 |
f = __FILEVal(fp); |
159 | 574 |
/* |
575 |
* notice: Smalltalk index starts at 1 |
|
576 |
*/ |
|
577 |
do { |
|
1133 | 578 |
if (__INST(buffered) == true) { |
579 |
ret = fseek(f, (long) (__intVal(newPos) - 1), SEEK_SET); |
|
159 | 580 |
} else { |
1133 | 581 |
ret = (long) lseek(fileno(f), (long)(__intVal(newPos) - 1), SEEK_SET); |
159 | 582 |
} |
583 |
} while ((ret < 0) && (errno == EINTR)); |
|
584 |
if (ret >= 0) { |
|
1133 | 585 |
__INST(position) = newPos; |
159 | 586 |
/* |
587 |
* just to make certain ... |
|
588 |
*/ |
|
1133 | 589 |
__INST(hitEOF) = false; |
159 | 590 |
RETURN ( self ); |
591 |
} |
|
1133 | 592 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
159 | 593 |
} |
1 | 594 |
} |
159 | 595 |
%}. |
596 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1 | 597 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
598 |
^ self primitiveFailed |
|
599 |
! |
|
600 |
||
601 |
setToEnd |
|
602 |
"set the read/write position in the file to be at the end of the file" |
|
603 |
||
604 |
%{ |
|
605 |
FILE *f; |
|
10 | 606 |
int ret; |
1 | 607 |
|
1133 | 608 |
if (__INST(filePointer) != nil) { |
609 |
f = __FILEVal(__INST(filePointer)); |
|
610 |
__INST(position) = nil; |
|
159 | 611 |
do { |
1133 | 612 |
if (__INST(buffered) == true) { |
159 | 613 |
ret = fseek(f, 0L, SEEK_END); |
614 |
} else { |
|
615 |
ret = (long)lseek(fileno(f), 0L, SEEK_END); |
|
616 |
} |
|
617 |
} while ((ret < 0) && (errno == EINTR)); |
|
618 |
if (ret >= 0) { |
|
619 |
RETURN ( self ); |
|
620 |
} |
|
1133 | 621 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
1 | 622 |
} |
159 | 623 |
%}. |
624 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
49 | 625 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
1 | 626 |
^ self primitiveFailed |
613 | 627 |
! |
628 |
||
629 |
size |
|
630 |
"return the size in bytes of the file" |
|
631 |
||
632 |
%{ /* NOCONTEXT */ |
|
633 |
||
634 |
#ifdef transputer |
|
635 |
FILE *f; |
|
636 |
int size; |
|
637 |
||
1133 | 638 |
if (__INST(filePointer) != nil) { |
639 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 640 |
if ((size = filesize(fileno(f))) >= 0) { |
1133 | 641 |
RETURN ( __MKSMALLINT(size) ); |
613 | 642 |
} |
643 |
} |
|
644 |
#else |
|
645 |
FILE *f; |
|
646 |
struct stat buf; |
|
647 |
int ret; |
|
648 |
int fd; |
|
649 |
||
1133 | 650 |
if (__INST(filePointer) != nil) { |
651 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 652 |
fd = fileno(f); |
653 |
do { |
|
654 |
ret = fstat(fd, &buf); |
|
655 |
} while ((ret < 0) && (errno == EINTR)); |
|
656 |
if (ret >= 0) { |
|
1133 | 657 |
RETURN ( __MKSMALLINT(buf.st_size) ); |
613 | 658 |
} |
1133 | 659 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
613 | 660 |
} |
661 |
#endif |
|
662 |
%}. |
|
663 |
||
664 |
"could add a fall-back here: |
|
665 |
||
666 |
oldPosition := self position. |
|
667 |
self setToEnd. |
|
668 |
sz := self position. |
|
669 |
self position:oldPosition. |
|
670 |
^ sz |
|
671 |
" |
|
672 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
673 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
674 |
^ self primitiveFailed |
|
1 | 675 |
! ! |
676 |
||
2 | 677 |
!FileStream methodsFor:'testing'! |
678 |
||
679 |
isFileStream |
|
680 |
"return true, if the receiver is some kind of fileStream. |
|
681 |
redefined from Object" |
|
682 |
||
683 |
^ true |
|
684 |
! ! |
|
685 |
||
1088 | 686 |
!FileStream class methodsFor:'documentation'! |
687 |
||
688 |
version |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
689 |
^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.35 1997-01-10 18:00:57 cg Exp $' |
1088 | 690 |
! ! |