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