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