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