author | Claus Gittinger <cg@exept.de> |
Fri, 15 Jan 1999 21:53:59 +0100 | |
changeset 3956 | 51f1a9a4d63f |
parent 3940 | 2b5888b16c68 |
child 3971 | aad506cdc5d9 |
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 |
|
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
14 |
instanceVariableNames:'pathName canPosition removeOnClose' |
3231
538eee23688a
FileStream OpenErrorSignal is a querySignal, ExternalStream
Stefan Vogel <sv@exept.de>
parents:
3051
diff
changeset
|
15 |
classVariableNames:'OpenErrorSignal' |
1088 | 16 |
poolDictionaries:'' |
17 |
category:'Streams-External' |
|
1 | 18 |
! |
19 |
||
216 | 20 |
!FileStream primitiveDefinitions! |
1 | 21 |
%{ |
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
22 |
#if defined(__openVMS__) || defined(LINUX) |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
23 |
/* |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
24 |
* mhmh - used in stdio ... |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
25 |
* (new linuxes) |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
26 |
*/ |
2903 | 27 |
# undef __new |
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
28 |
# define __NEED_REDEF_new__ |
2903 | 29 |
#endif |
30 |
||
1 | 31 |
#include <stdio.h> |
437 | 32 |
#define _STDIO_H_INCLUDED_ |
33 |
||
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
34 |
#ifdef __NEED_REDEF_new__ |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
35 |
# define __new __STX___new |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
36 |
#endif |
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
37 |
|
10 | 38 |
#include <errno.h> |
437 | 39 |
#define _ERRNO_H_INCLUDED_ |
1 | 40 |
|
41 |
#ifdef transputer |
|
42 |
# include <iocntrl.h> |
|
43 |
# ifndef fileno |
|
44 |
/* kludge: inmos forgot fileno */ |
|
45 |
# define fileno(f) ((f)->__file) |
|
46 |
# endif |
|
47 |
#else |
|
48 |
# include <sys/types.h> |
|
49 |
# include <sys/stat.h> |
|
50 |
#endif |
|
10 | 51 |
|
42 | 52 |
#ifdef hpux |
68 | 53 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
42 | 54 |
#endif |
55 |
||
10 | 56 |
#ifndef SEEK_SET |
13 | 57 |
# define SEEK_SET 0 |
10 | 58 |
#endif |
59 |
#ifndef SEEK_CUR |
|
13 | 60 |
# define SEEK_CUR 1 |
10 | 61 |
#endif |
62 |
#ifndef SEEK_END |
|
13 | 63 |
# define SEEK_END 2 |
10 | 64 |
#endif |
65 |
||
223 | 66 |
/* |
2921 | 67 |
* not all systems have off_t |
2808 | 68 |
* explicit add of those we know to have ... |
69 |
*/ |
|
70 |
#ifdef __osf__ |
|
71 |
# define OFF_T off_t |
|
72 |
#endif |
|
73 |
||
74 |
#ifndef OFF_T |
|
75 |
# define OFF_T long |
|
76 |
#endif |
|
77 |
||
2921 | 78 |
#ifdef __VMS__ |
79 |
/* |
|
80 |
* get those VMS definitions ... |
|
81 |
*/ |
|
82 |
# include <rms.h> |
|
83 |
#endif |
|
84 |
||
2992 | 85 |
#ifndef WIN32 |
86 |
extern long ftell(), lseek(); |
|
87 |
#endif |
|
88 |
||
2808 | 89 |
/* |
223 | 90 |
* on some systems errno is a macro ... check for it here |
91 |
*/ |
|
92 |
#ifndef errno |
|
93 |
extern errno; |
|
94 |
#endif |
|
95 |
||
1 | 96 |
%} |
173 | 97 |
! ! |
1 | 98 |
|
613 | 99 |
!FileStream class methodsFor:'documentation'! |
100 |
||
101 |
copyright |
|
102 |
" |
|
103 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
104 |
All Rights Reserved |
|
105 |
||
106 |
This software is furnished under a license and may be used |
|
107 |
only in accordance with the terms of that license and with the |
|
108 |
inclusion of the above copyright notice. This software may not |
|
109 |
be provided or otherwise made available to, or used by, any |
|
110 |
other person. No title to or ownership of the software is |
|
111 |
hereby transferred. |
|
112 |
" |
|
113 |
! |
|
114 |
||
115 |
documentation |
|
116 |
" |
|
117 |
This class provides access to the operating systems underlying file |
|
118 |
system (i.e. its an interface to the stdio library). |
|
119 |
||
120 |
Notice, that on some systems, the standard I/O library has performance |
|
121 |
problems when a file is opened for readwrite. |
|
122 |
For best results, open files either readonly or writeonly. |
|
1295 | 123 |
|
2921 | 124 |
Also notice, that some OperatingSystems do not fully support |
125 |
positioning a file stream. |
|
126 |
For example, poor VMS does not allow positioning onto arbitrary |
|
127 |
byte boundaries if the file is a variable-record-RMS file. |
|
128 |
(stupid enough, this is the default for textfiles as created by |
|
129 |
some tools ...) |
|
130 |
Therefore, the instance variable canPosition is set according to |
|
131 |
this and an error is raised, if a position: is attemted. |
|
132 |
I know, this is ugly, but what else could we do ? |
|
133 |
||
134 |
[instance variables:] |
|
135 |
pathName <String> the files path (if known) |
|
136 |
canPosition <Boolean> positionable - read above comment |
|
137 |
||
1295 | 138 |
[author:] |
2161 | 139 |
Claus Gittinger |
1295 | 140 |
|
141 |
[see also:] |
|
2161 | 142 |
Filename DirectoryStream PipeStream Socket |
613 | 143 |
" |
2921 | 144 |
! |
145 |
||
146 |
examples |
|
147 |
" |
|
148 |
for VMS users only: |
|
149 |
||
150 |
The #openWithMode:attributes: entry allows additional RMS attributes |
|
151 |
to be passed in the second argument, which must be an array of strings |
|
152 |
as described in the 'creat' RTL Library documentation. |
|
153 |
||
154 |
For example, to create a file with fixed records and recordLength of 100, |
|
155 |
use: |
|
156 |
||
157 |
|newFile| |
|
158 |
||
159 |
newFile := FileStream new pathName:'<nameOfFile>'. |
|
160 |
newFile setMode:#writeonly. |
|
161 |
newFile openWithMode:'w' attributes:#('rfm=fix' 'fsz=100'). |
|
162 |
||
163 |
since all of the above is private protocol, and it is considered bad style to |
|
164 |
access these from user programs, we recommend subclassing FileStream as |
|
165 |
something like VMSFixedRecordFileStream, and redefine the instance creation |
|
166 |
method(s) there as appropriate. |
|
167 |
This will retain VMS specifics in one place and enhance maintanability. |
|
168 |
" |
|
169 |
! ! |
|
170 |
||
171 |
!FileStream class methodsFor:'initialization'! |
|
172 |
||
173 |
initialize |
|
174 |
OpenErrorSignal isNil ifTrue:[ |
|
3006
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
175 |
"/ |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
176 |
"/ this is temporary - for now allow an openError to |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
177 |
"/ be unhandled and proceed by returning a nil from the |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
178 |
"/ stream creation method. |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
179 |
"/ In the future, this will be a hard signal. |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
180 |
"/ |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
181 |
OpenErrorSignal := QuerySignal new. |
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
182 |
OpenErrorSignal parent:(super openErrorSignal) mayProceed:true. |
2921 | 183 |
OpenErrorSignal nameClass:self message:#openErrorSignal. |
184 |
OpenErrorSignal notifierString:'open error'. |
|
185 |
] |
|
3006
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
186 |
|
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
187 |
"Modified: 8.10.1997 / 11:56:39 / cg" |
2921 | 188 |
! ! |
189 |
||
1 | 190 |
!FileStream class methodsFor:'instance creation'! |
191 |
||
613 | 192 |
appendingOldFileNamed:filename |
193 |
"return a FileStream for existing file named filename, aString. |
|
194 |
The file is opened for writeonly access." |
|
1 | 195 |
|
196 |
|newStream| |
|
2 | 197 |
newStream := self new pathName:filename. |
613 | 198 |
newStream openForAppending isNil ifTrue:[^nil]. |
199 |
" |
|
200 |
this is not a good idea; I might like to read the written stuff ... |
|
201 |
||
202 |
newStream readLimit:(newStream size). |
|
203 |
" |
|
204 |
^ newStream |
|
205 |
! |
|
206 |
||
207 |
appendingOldFileNamed:filename in:aDirectory |
|
208 |
"return a FileStream for existing file named filename, aString |
|
209 |
in aDirectory, a FileDirectory. |
|
210 |
The file is opened for writeonly access." |
|
211 |
||
212 |
|newStream| |
|
213 |
newStream := self new pathName:filename in:aDirectory. |
|
214 |
newStream openForAppending isNil ifTrue:[^nil]. |
|
215 |
" |
|
216 |
this is not a good idea; I might like to read the written stuff ... |
|
217 |
||
218 |
newStream readLimit:(newStream size). |
|
219 |
" |
|
1 | 220 |
^ newStream |
221 |
! |
|
222 |
||
613 | 223 |
fileNamed:filename |
224 |
"return a stream on file filename - if the file does not |
|
225 |
already exist, create it. |
|
92 | 226 |
The file is opened for read/write access." |
1 | 227 |
|
613 | 228 |
|stream| |
229 |
||
230 |
stream := self oldFileNamed:filename. |
|
231 |
stream isNil ifTrue:[ |
|
232 |
stream := self newFileNamed:filename |
|
233 |
]. |
|
234 |
^ stream |
|
235 |
! |
|
236 |
||
237 |
fileNamed:filename in:aDirectory |
|
238 |
"return a stream on file filename - if the file does not |
|
239 |
already exist, create it. |
|
240 |
The file is opened for read/write access." |
|
241 |
||
242 |
|stream| |
|
243 |
||
244 |
stream := self oldFileNamed:filename in:aDirectory. |
|
245 |
stream isNil ifTrue:[ |
|
246 |
stream := self newFileNamed:filename in:aDirectory |
|
247 |
]. |
|
248 |
^ stream |
|
1 | 249 |
! |
250 |
||
92 | 251 |
newFileForWritingNamed:filename |
252 |
"return a FileStream for new file named filename, aString. |
|
253 |
If the file exists, it is truncated, otherwise created. |
|
254 |
The file is opened for writeonly access." |
|
255 |
||
256 |
|newStream| |
|
257 |
newStream := self new pathName:filename. |
|
258 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
259 |
^ newStream |
|
260 |
! |
|
261 |
||
262 |
newFileForWritingNamed:filename in:aDirectory |
|
263 |
"return a FileStream for new file named filename, aString |
|
264 |
in aDirectory, a FileDirectory. |
|
265 |
If the file exists, it is truncated, otherwise created. |
|
266 |
The file is opened for writeonly access." |
|
267 |
||
268 |
|newStream| |
|
269 |
newStream := self new pathName:filename in:aDirectory. |
|
270 |
newStream createForWriting isNil ifTrue:[^nil]. |
|
271 |
^ newStream |
|
272 |
! |
|
273 |
||
613 | 274 |
newFileNamed:filename |
275 |
"return a FileStream for new file named filename, aString. |
|
276 |
If the file exists, it is truncated, otherwise created. |
|
277 |
The file is opened for read/write access." |
|
278 |
||
279 |
|newStream| |
|
280 |
newStream := self new pathName:filename. |
|
281 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
282 |
^ newStream |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
283 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
284 |
"Modified: / 28.10.1997 / 14:28:08 / cg" |
613 | 285 |
! |
286 |
||
287 |
newFileNamed:filename in:aDirectory |
|
288 |
"return a FileStream for new file named filename, aString |
|
289 |
in aDirectory, a FileDirectory. |
|
290 |
If the file exists, it is truncated, otherwise created. |
|
291 |
The file is opened for read/write access." |
|
292 |
||
293 |
|newStream| |
|
294 |
newStream := self new pathName:filename in:aDirectory. |
|
295 |
newStream createForReadWrite isNil ifTrue:[^nil]. |
|
296 |
^ newStream |
|
297 |
! |
|
298 |
||
1 | 299 |
oldFileNamed:filename |
300 |
"return a FileStream for existing file named filename, aString. |
|
301 |
The file is opened for read/write access." |
|
302 |
||
303 |
|newStream| |
|
304 |
||
305 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
2 | 306 |
newStream := self new pathName:filename. |
1 | 307 |
newStream readwrite. |
308 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
|
42 | 309 |
" |
310 |
this is not a good idea; someone else might be appending ... |
|
311 |
||
1 | 312 |
newStream readLimit:(newStream size). |
42 | 313 |
" |
1 | 314 |
^ newStream |
315 |
! |
|
316 |
||
317 |
oldFileNamed:filename in:aDirectory |
|
318 |
"return a FileStream for existing file named filename, aString |
|
319 |
in aDirectory, a FileDirectory. |
|
320 |
The file is opened for read/write access." |
|
321 |
||
322 |
|newStream| |
|
2 | 323 |
newStream := self new pathName:filename in:aDirectory. |
1 | 324 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
42 | 325 |
" |
326 |
this is not a good idea; someone else might be appending ... |
|
327 |
||
1 | 328 |
newStream readLimit:(newStream size). |
42 | 329 |
" |
1 | 330 |
^ newStream |
331 |
! |
|
332 |
||
333 |
readonlyFileNamed:filename |
|
334 |
"return a readonly FileStream for existing file named filename, aString" |
|
335 |
||
336 |
|newStream| |
|
337 |
||
338 |
(OperatingSystem isReadable:filename) ifFalse:[^nil]. |
|
339 |
||
2 | 340 |
newStream := self new pathName:filename. |
1 | 341 |
newStream openForReading isNil ifTrue:[^nil]. |
42 | 342 |
" |
343 |
this is not a good idea; someone else might be appending ... |
|
344 |
||
1 | 345 |
newStream readLimit:(newStream size). |
42 | 346 |
" |
1 | 347 |
^ newStream |
348 |
! |
|
349 |
||
350 |
readonlyFileNamed:filename in:aDirectory |
|
351 |
"return a readonly FileStream for existing file named filename, aString |
|
352 |
in aDirectory, a FileDirectory" |
|
353 |
||
354 |
|newStream| |
|
2 | 355 |
newStream := self new pathName:filename in:aDirectory. |
1 | 356 |
newStream openForReading isNil ifTrue:[^nil]. |
42 | 357 |
" |
358 |
this is not a good idea; someone else might be appending ... |
|
359 |
||
1 | 360 |
newStream readLimit:(newStream size). |
42 | 361 |
" |
1 | 362 |
^ newStream |
159 | 363 |
! ! |
364 |
||
3233
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
365 |
!FileStream class methodsFor:'Signal constants'! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
366 |
|
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
367 |
openErrorSignal |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
368 |
"return the FileStream specific openErrorSignal. |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
369 |
This is different from ExternalStream>>OpenErrorSignal!!" |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
370 |
|
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
371 |
^ OpenErrorSignal |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
372 |
|
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
373 |
"Created: / 28.1.1998 / 14:41:18 / stefan" |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
374 |
! ! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
375 |
|
3044 | 376 |
!FileStream methodsFor:'Squeak compatibility'! |
377 |
||
378 |
fullName |
|
379 |
"Squeak compatibility: return the full pathname" |
|
380 |
||
381 |
^ pathName asFilename pathName |
|
382 |
||
383 |
"Created: 17.10.1997 / 17:04:12 / cg" |
|
384 |
"Modified: 20.10.1997 / 19:22:44 / cg" |
|
385 |
! ! |
|
386 |
||
1 | 387 |
!FileStream methodsFor:'accessing'! |
388 |
||
389 |
directoryName |
|
2903 | 390 |
"return the name of the directory I'm in as a string" |
1 | 391 |
|
2903 | 392 |
^ pathName asFilename directoryName |
1 | 393 |
! |
394 |
||
395 |
name |
|
2903 | 396 |
"return my name without leading direcory-path (i.e. the plain fileName) |
397 |
as a string" |
|
1 | 398 |
|
2903 | 399 |
^ pathName asFilename baseName |
1 | 400 |
! |
401 |
||
402 |
pathName |
|
403 |
"return the pathname" |
|
404 |
||
405 |
^ pathName |
|
613 | 406 |
! |
407 |
||
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
408 |
removeOnClose:aBoolean |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
409 |
"set/clear the removeOnClose flag. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
410 |
If set, the file will be removed when closed. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
411 |
Provided mostly for OS's which do not allow an |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
412 |
open file to be removed (i.e. non unixes), |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
413 |
and a fileStream for a tempFile is used. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
414 |
Especially, the CVS-SourceCodeManager returns |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
415 |
this kind of file-handles occasionally. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
416 |
This is an ST/X special feature which is not portable |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
417 |
to other systems." |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
418 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
419 |
removeOnClose := aBoolean |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
420 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
421 |
"Modified: / 13.8.1998 / 12:10:07 / cg" |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
422 |
! |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
423 |
|
613 | 424 |
store:something |
425 |
"what really should this do" |
|
426 |
||
427 |
self nextPutAll:something storeString |
|
428 |
! ! |
|
429 |
||
430 |
!FileStream methodsFor:'error handling'! |
|
431 |
||
432 |
openError |
|
2963 | 433 |
"{ Pragma: +optSpace }" |
434 |
||
613 | 435 |
"report an error, that file open failed" |
436 |
||
437 |
"/ |
|
2921 | 438 |
"/ for now, error on open only raises a querySignal. |
439 |
"/ If its not handled, a failing open will return nil. |
|
440 |
"/ Currently, all users of fileStream check for this |
|
441 |
"/ nil return value (which is a historic leftover) |
|
442 |
"/ |
|
443 |
"/ This will change in one of the next ST/X releases - for |
|
444 |
"/ now, it is best to provide an exceptionHandler AND check the |
|
445 |
"/ return value. |
|
446 |
"/ Sorry about that. |
|
613 | 447 |
"/ |
448 |
LastErrorNumber := lastErrorNumber. |
|
2921 | 449 |
super openError. |
450 |
^ nil |
|
613 | 451 |
! ! |
452 |
||
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
453 |
!FileStream methodsFor:'instance release'! |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
454 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
455 |
closeFile |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
456 |
"low level close - may be redefined in subclasses" |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
457 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
458 |
super closeFile. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
459 |
removeOnClose == true ifTrue:[ |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
460 |
pathName asFilename remove. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
461 |
] |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
462 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
463 |
"Created: / 13.8.1998 / 12:11:22 / cg" |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
464 |
! ! |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
465 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
466 |
!FileStream methodsFor:'positioning'! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
467 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
468 |
position |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
469 |
"return the read/write position in the file - |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
470 |
notice, in smalltalk indices start at 1 so begin of file is 1" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
471 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
472 |
%{ /* NOCONTEXT */ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
473 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
474 |
FILE *f; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
475 |
long currentPosition; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
476 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
477 |
if (__INST(filePointer) != nil) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
478 |
f = __FILEVal(__INST(filePointer)); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
479 |
do { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
480 |
if (__INST(buffered) == true) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
481 |
currentPosition = ftell(f); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
482 |
} else { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
483 |
currentPosition = lseek(fileno(f), 0L, SEEK_CUR); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
484 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
485 |
} while ((currentPosition < 0) && (errno == EINTR)); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
486 |
if (currentPosition >= 0) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
487 |
/* |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
488 |
* notice: Smalltalk index starts at 1 |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
489 |
*/ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
490 |
RETURN ( __MKSMALLINT(currentPosition + 1) ); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
491 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
492 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
493 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
494 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
495 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
496 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
497 |
^ self primitiveFailed |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
498 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
499 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
500 |
position:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
501 |
"set the read/write position in the file" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
502 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
503 |
%{ /* NOCONTEXT */ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
504 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
505 |
FILE *f; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
506 |
long ret; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
507 |
OBJ fp; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
508 |
long nP; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
509 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
510 |
if ((__INST(canPosition) != false) |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
511 |
|| (newPos == __MKSMALLINT(1))) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
512 |
if ((fp = __INST(filePointer)) != nil) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
513 |
if (__isSmallInteger(newPos)) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
514 |
f = __FILEVal(fp); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
515 |
nP = (long)__intVal(newPos); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
516 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
517 |
/* |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
518 |
* notice: Smalltalk index starts at 1 |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
519 |
*/ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
520 |
nP--; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
521 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
522 |
do { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
523 |
if (__INST(buffered) == true) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
524 |
ret = fseek(f, nP, SEEK_SET); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
525 |
} else { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
526 |
ret = lseek(fileno(f), nP, SEEK_SET); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
527 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
528 |
} while ((ret < 0) && (errno == EINTR)); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
529 |
if (ret >= 0) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
530 |
__INST(position) = newPos; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
531 |
/* |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
532 |
* just to make certain ... |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
533 |
*/ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
534 |
__INST(hitEOF) = false; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
535 |
RETURN ( self ); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
536 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
537 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
538 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
539 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
540 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
541 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
542 |
canPosition == false ifTrue:[ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
543 |
"/ position by rewinding & re-reading everything up-to |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
544 |
"/ that point. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
545 |
^ self slowPosition:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
546 |
]. |
2963 | 547 |
lastErrorNumber notNil ifTrue:[ |
548 |
(OperatingSystem errorSymbolForNumber:lastErrorNumber) == #EINVAL ifTrue:[ |
|
549 |
"/ invalid position |
|
550 |
^ self positionError |
|
551 |
]. |
|
552 |
"/ assume I/O error |
|
553 |
^ self ioError |
|
554 |
]. |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
555 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
556 |
^ self primitiveFailed |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
557 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
558 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
559 |
setToEnd |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
560 |
"set the read/write position in the file to be at the end of the file" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
561 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
562 |
%{ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
563 |
FILE *f; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
564 |
long ret; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
565 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
566 |
if (__INST(filePointer) != nil) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
567 |
f = __FILEVal(__INST(filePointer)); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
568 |
__INST(position) = nil; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
569 |
do { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
570 |
if (__INST(buffered) == true) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
571 |
ret = fseek(f, 0L, SEEK_END); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
572 |
} else { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
573 |
ret = lseek(fileno(f), 0L, SEEK_END); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
574 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
575 |
} while ((ret < 0) && (errno == EINTR)); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
576 |
if (ret >= 0) { |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
577 |
RETURN ( self ); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
578 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
579 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
580 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
581 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
582 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
583 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
584 |
^ self primitiveFailed |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
585 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
586 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
587 |
slowPosition:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
588 |
"position the file by re-reading everything up-to newPos. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
589 |
The effect is the same as that of #position:, but its much slower. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
590 |
This is required to reposition nonPositionable streams, such |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
591 |
as tape-streams or variable-record-RMS files under VMS. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
592 |
Caveat: |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
593 |
This should really be done transparently by the stdio library." |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
594 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
595 |
|buffer amount| |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
596 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
597 |
self isReadable ifFalse:[ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
598 |
"/ sorry |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
599 |
^ self positionError |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
600 |
]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
601 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
602 |
buffer := ByteArray new:8*1024. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
603 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
604 |
(position isNil "/ i.e. unknown |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
605 |
or:[newPos < position]) ifTrue:[ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
606 |
self reset. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
607 |
]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
608 |
[position < newPos] whileTrue:[ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
609 |
amount := (buffer size) min:(newPos-position). |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
610 |
(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[ |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
611 |
^ self positionError |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
612 |
]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
613 |
]. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
614 |
"/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
615 |
! ! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
616 |
|
613 | 617 |
!FileStream methodsFor:'printing & storing'! |
618 |
||
619 |
printOn:aStream |
|
2903 | 620 |
"append a human readable representation of the receiver to aStream" |
621 |
||
613 | 622 |
aStream nextPutAll:'(a FileStream for:'. |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
623 |
aStream nextPutAll:pathName printString. |
613 | 624 |
aStream nextPut:$) |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
625 |
|
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
626 |
"Modified: 30.7.1997 / 13:51:01 / cg" |
613 | 627 |
! |
628 |
||
629 |
storeOn:aStream |
|
2903 | 630 |
"append a representation of the receiver to aStream, |
631 |
from which a copy can be reconstructed later." |
|
632 |
||
613 | 633 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
634 |
aStream nextPutAll:pathName storeString. |
613 | 635 |
(self position ~~ 1) ifTrue:[ |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
636 |
aStream nextPutAll:'; position:'. |
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
637 |
self position storeOn:aStream |
613 | 638 |
]. |
639 |
aStream nextPut:$) |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
640 |
|
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
641 |
"Modified: 30.7.1997 / 16:43:50 / cg" |
1 | 642 |
! ! |
643 |
||
644 |
!FileStream methodsFor:'private'! |
|
645 |
||
613 | 646 |
createForReadWrite |
647 |
"create/truncate the file for read/write. |
|
648 |
If the file existed, its truncated; otherwise its created." |
|
1 | 649 |
|
613 | 650 |
mode := #readwrite. |
2921 | 651 |
^ self openWithMode:CreateReadWriteMode |
1 | 652 |
! |
653 |
||
613 | 654 |
createForWriting |
655 |
"create/truncate the file for writeonly. |
|
656 |
If the file existed, its truncated; otherwise its created." |
|
1 | 657 |
|
613 | 658 |
mode := #writeonly. |
659 |
didWrite := true. |
|
2921 | 660 |
^ self openWithMode:WriteMode |
1 | 661 |
! |
662 |
||
663 |
open |
|
664 |
"open the file" |
|
665 |
||
666 |
pathName isNil ifTrue:[^nil]. |
|
667 |
(mode == #readonly) ifTrue: [ |
|
159 | 668 |
didWrite := false. |
2921 | 669 |
^ self openWithMode:ReadMode |
1 | 670 |
]. |
671 |
(mode == #writeonly) ifTrue: [ |
|
159 | 672 |
didWrite := true. |
2921 | 673 |
^ self openWithMode:WriteMode |
1 | 674 |
]. |
2921 | 675 |
^ self openWithMode:ReadWriteMode |
1 | 676 |
! |
677 |
||
613 | 678 |
openForAppending |
679 |
"open the file for writeonly appending to the end. |
|
680 |
If the file does not exist its an error, return nil; |
|
681 |
otherwise return the receiver." |
|
682 |
||
683 |
mode := #writeonly. |
|
684 |
didWrite := true. |
|
2921 | 685 |
^ self openWithMode:AppendMode |
613 | 686 |
! |
687 |
||
688 |
openForReadWrite |
|
689 |
"open the file for read/write. |
|
690 |
If the file does not exist its an error, return nil; |
|
691 |
otherwise return the receiver." |
|
692 |
||
693 |
mode := #readwrite. |
|
2921 | 694 |
^ self openWithMode:ReadWriteMode |
613 | 695 |
! |
696 |
||
697 |
openForReading |
|
698 |
"open the file for readonly. |
|
699 |
If the file does not exist its an error, return nil; |
|
700 |
otherwise return the receiver." |
|
701 |
||
702 |
mode := #readonly. |
|
703 |
didWrite := false. |
|
2921 | 704 |
^ self openWithMode:ReadMode |
613 | 705 |
! |
706 |
||
707 |
openForWriting |
|
708 |
"open the file writeonly. |
|
709 |
If the file does not exist its an error, return nil; |
|
710 |
otherwise return the receiver." |
|
711 |
||
712 |
mode := #writeonly. |
|
713 |
didWrite := true. |
|
2921 | 714 |
^ self openWithMode:ReadWriteMode "unix-io does not allow this; open for update here" |
613 | 715 |
! |
716 |
||
1 | 717 |
openWithMode:openmode |
2921 | 718 |
"open the file; |
719 |
openmode is the string defining the way to open as defined by the stdio library |
|
720 |
(i.e. the 2nd fopen argument). |
|
721 |
||
722 |
This is a private entry, but maybe useful to open a file in a special mode, |
|
723 |
which is proprietrary to the operatingSystem." |
|
724 |
||
725 |
^ self openWithMode:openmode attributes:nil |
|
726 |
! |
|
727 |
||
728 |
openWithMode:openmode attributes:attributeSpec |
|
2908 | 729 |
"open the file; |
2921 | 730 |
openmode is the string defining the way to open as defined by the stdio library |
731 |
(i.e. the 2nd fopen argument). |
|
732 |
||
733 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
734 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
735 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
736 |
attributes (see fopen description). |
|
737 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
738 |
This argument is ignored in UNIX & MSDOS systems. |
|
739 |
||
740 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
741 |
which is proprietrary to the operatingSystem." |
|
1 | 742 |
|
2963 | 743 |
|ok| |
159 | 744 |
|
745 |
filePointer notNil ifTrue:[^ self errorOpen]. |
|
2963 | 746 |
ok := false. |
1 | 747 |
%{ |
748 |
FILE *f; |
|
2763 | 749 |
FILE *fopen(); |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
750 |
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
|
751 |
int pass = 0; |
1 | 752 |
|
2078
0a5a557b5194
Try to collect unref files if there are no filedescriptors available.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
753 |
retry: |
1133 | 754 |
path = __INST(pathName); |
328 | 755 |
if (__isNonNilObject(path) && (__qClass(path)==String)) { |
3034 | 756 |
#ifdef __VMS__ |
2161 | 757 |
do { |
2921 | 758 |
/* |
759 |
* allow passing additional RMS arguments. |
|
3034 | 760 |
* stupid: DEC does not seem to offer an interface for passing a char **. |
2921 | 761 |
*/ |
2974 | 762 |
errno = 0; |
763 |
||
2921 | 764 |
{ |
765 |
if (__isArray(attributeSpec)) { |
|
766 |
OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; |
|
767 |
int numAttrib = 0; |
|
768 |
int i; |
|
769 |
||
770 |
numAttrib = __arraySize(attributeSpec); |
|
771 |
for (i=0; i<numAttrib;i++) { |
|
772 |
if (! __isString(ap[i])) { |
|
773 |
f = NULL; |
|
774 |
errno = EINVAL; /* invalid argument */ |
|
775 |
goto getOutOfHere; |
|
776 |
} |
|
777 |
} |
|
778 |
switch (numAttrib) { |
|
779 |
case 0: |
|
3034 | 780 |
__BEGIN_INTERRUPTABLE__ |
2921 | 781 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode)); |
3034 | 782 |
__END_INTERRUPTABLE__ |
2921 | 783 |
break; |
784 |
case 1: |
|
3034 | 785 |
__BEGIN_INTERRUPTABLE__ |
2921 | 786 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
787 |
__stringVal(ap[0])); |
|
3034 | 788 |
__END_INTERRUPTABLE__ |
2921 | 789 |
break; |
790 |
case 2: |
|
3034 | 791 |
__BEGIN_INTERRUPTABLE__ |
2921 | 792 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
793 |
__stringVal(ap[0]), __stringVal(ap[1])); |
|
3034 | 794 |
__END_INTERRUPTABLE__ |
2921 | 795 |
break; |
796 |
case 3: |
|
3034 | 797 |
__BEGIN_INTERRUPTABLE__ |
2921 | 798 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
799 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2])); |
|
3034 | 800 |
__END_INTERRUPTABLE__ |
2921 | 801 |
break; |
802 |
case 4: |
|
3034 | 803 |
__BEGIN_INTERRUPTABLE__ |
2921 | 804 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
805 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
806 |
__stringVal(ap[3])); |
|
3034 | 807 |
__END_INTERRUPTABLE__ |
2921 | 808 |
break; |
809 |
case 5: |
|
3034 | 810 |
__BEGIN_INTERRUPTABLE__ |
2921 | 811 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
812 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
813 |
__stringVal(ap[3]), __stringVal(ap[4])); |
|
3034 | 814 |
__END_INTERRUPTABLE__ |
2921 | 815 |
break; |
816 |
case 6: |
|
3034 | 817 |
__BEGIN_INTERRUPTABLE__ |
2921 | 818 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
819 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
820 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5])); |
|
3034 | 821 |
__END_INTERRUPTABLE__ |
2921 | 822 |
break; |
823 |
case 7: |
|
3034 | 824 |
__BEGIN_INTERRUPTABLE__ |
2921 | 825 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
826 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
827 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
828 |
__stringVal(ap[6])); |
|
3034 | 829 |
__END_INTERRUPTABLE__ |
2921 | 830 |
break; |
831 |
case 8: |
|
3034 | 832 |
__BEGIN_INTERRUPTABLE__ |
2921 | 833 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
834 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
835 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
836 |
__stringVal(ap[6]), __stringVal(ap[7])); |
|
3034 | 837 |
__END_INTERRUPTABLE__ |
2921 | 838 |
break; |
839 |
case 9: |
|
3034 | 840 |
__BEGIN_INTERRUPTABLE__ |
2921 | 841 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
842 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
843 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
844 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8])); |
|
3034 | 845 |
__END_INTERRUPTABLE__ |
2921 | 846 |
break; |
847 |
case 10: |
|
3034 | 848 |
__BEGIN_INTERRUPTABLE__ |
2921 | 849 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), |
850 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
851 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
852 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8]), |
|
853 |
__stringVal(ap[9])); |
|
3034 | 854 |
__END_INTERRUPTABLE__ |
2921 | 855 |
break; |
856 |
default: |
|
857 |
f = NULL; |
|
858 |
errno = E2BIG; /* too many args */ |
|
859 |
goto getOutOfHere; |
|
860 |
} |
|
861 |
} else if (attributeSpec != nil) { |
|
862 |
f = NULL; |
|
863 |
errno = EINVAL; /* invalid argument */ |
|
864 |
goto getOutOfHere; |
|
865 |
} else { |
|
866 |
/* |
|
867 |
* create file as sequential streamLF by default. |
|
868 |
*/ |
|
3034 | 869 |
__BEGIN_INTERRUPTABLE__ |
2921 | 870 |
f = fopen((char *)__stringVal(path), (char *)__stringVal(openmode), "rfm=stmlf"); |
3034 | 871 |
__END_INTERRUPTABLE__ |
2921 | 872 |
} |
873 |
} |
|
3034 | 874 |
/* must refetch - could be GC'd */ |
875 |
path = __INST(pathName); |
|
876 |
} while ((f == NULL) && (errno == EINTR)); |
|
2921 | 877 |
#else |
3034 | 878 |
do { |
879 |
__BEGIN_INTERRUPTABLE__ |
|
3039 | 880 |
# ifdef LINUX |
2921 | 881 |
/* |
882 |
* LINUX may return a non-NULL f even when interrupted. |
|
883 |
* Therefore, check errno and fake a null-return. |
|
884 |
*/ |
|
2161 | 885 |
errno = 0; |
2763 | 886 |
f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
2161 | 887 |
if (errno == EINTR) |
888 |
f = NULL; |
|
2921 | 889 |
# else |
2763 | 890 |
f = fopen((char *) __stringVal(path), (char *) __stringVal(openmode)); |
2921 | 891 |
# endif |
3034 | 892 |
__END_INTERRUPTABLE__ |
2817
10f114dfe605
oops - GC while being in an interrupted file-open may lead to bad path-string access
Claus Gittinger <cg@exept.de>
parents:
2811
diff
changeset
|
893 |
/* must refetch - could be GC'd */ |
10f114dfe605
oops - GC while being in an interrupted file-open may lead to bad path-string access
Claus Gittinger <cg@exept.de>
parents:
2811
diff
changeset
|
894 |
path = __INST(pathName); |
2161 | 895 |
} while ((f == NULL) && (errno == EINTR)); |
3034 | 896 |
#endif |
2921 | 897 |
|
2161 | 898 |
if (f == NULL) { |
899 |
/* |
|
900 |
* If no filedescriptors available, try to finalize |
|
901 |
* possibly collected fd's and try again. |
|
902 |
*/ |
|
903 |
if (pass == 0 && (errno == ENFILE || errno == EMFILE)) { |
|
904 |
pass = 1; |
|
905 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
|
906 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
|
907 |
goto retry; |
|
908 |
} |
|
2921 | 909 |
getOutOfHere: ; |
2161 | 910 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
911 |
__INST(position) = nil; |
|
912 |
} else { |
|
2921 | 913 |
#ifdef __VMS__ |
914 |
/* |
|
915 |
* check to see if this is positionable ... |
|
916 |
*/ |
|
917 |
__INST(canPosition) = false; |
|
918 |
# ifndef _POSIX_C_SOURCE |
|
919 |
{ |
|
920 |
struct stat statBuffer; |
|
921 |
||
922 |
if (fstat(fileno(f), &statBuffer) >= 0) { |
|
923 |
switch (statBuffer.st_fab_rfm) { |
|
924 |
case FAB$C_UDF: /* undefined (also stream binary) */ |
|
925 |
case FAB$C_VAR: /* variable length records */ |
|
926 |
case FAB$C_VFC: /* variable fixed control */ |
|
927 |
case FAB$C_STM: /* RMS-11 stream (valid only for sequen> */ |
|
928 |
default: |
|
929 |
__INST(canPosition) = false; |
|
930 |
break; |
|
931 |
||
932 |
case FAB$C_FIX: /* fixed length records */ |
|
933 |
case FAB$C_STMLF: /* LF stream (valid only for sequential> */ |
|
934 |
case FAB$C_STMCR: /* CR stream (valid only for sequential> */ |
|
935 |
__INST(canPosition) = true; |
|
936 |
break; |
|
937 |
} |
|
938 |
} |
|
939 |
} |
|
940 |
# endif |
|
941 |
#else /* not VMS */ |
|
942 |
__INST(canPosition) = true; |
|
943 |
#endif /* poor VMS */ |
|
944 |
||
2766 | 945 |
__INST(filePointer) = fp = __MKOBJ((INT)f); __STORE(self, fp); |
2161 | 946 |
__INST(position) = __MKSMALLINT(1); |
2963 | 947 |
ok = true; |
2161 | 948 |
} |
1 | 949 |
} |
159 | 950 |
%}. |
2963 | 951 |
ok ifFalse:[ |
2161 | 952 |
" |
953 |
the open failed for some reason ... |
|
954 |
" |
|
955 |
^ self openError |
|
1 | 956 |
]. |
2963 | 957 |
buffered := true. "default is buffered" |
958 |
Lobby register:self. |
|
1 | 959 |
! |
960 |
||
613 | 961 |
pathName:filename |
962 |
"set the pathname" |
|
1 | 963 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
964 |
pathName := filename asString |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
965 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
966 |
"Modified: / 28.10.1997 / 14:29:01 / cg" |
1 | 967 |
! |
968 |
||
613 | 969 |
pathName:filename in:aDirectory |
970 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 971 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
972 |
pathName := aDirectory asFilename constructString:filename |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
973 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
974 |
"Modified: / 28.10.1997 / 14:28:54 / cg" |
1 | 975 |
! |
976 |
||
977 |
reOpen |
|
978 |
"sent after snapin to reopen streams" |
|
979 |
||
173 | 980 |
|oldPos| |
981 |
||
1 | 982 |
filePointer notNil ifTrue:[ |
2161 | 983 |
"it was open, when snapped-out" |
984 |
filePointer := nil. |
|
985 |
Lobby unregister:self. |
|
986 |
oldPos := position. |
|
987 |
self open. |
|
988 |
filePointer isNil ifTrue:[ |
|
2903 | 989 |
"/ this happens, if after a restart, |
990 |
"/ the file is no longer present or accessable ..." |
|
13 | 991 |
|
2161 | 992 |
(self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. |
993 |
] ifFalse:[ |
|
3940 | 994 |
oldPos notNil ifTrue:[ |
995 |
self position:oldPos. |
|
996 |
] |
|
2161 | 997 |
] |
1 | 998 |
] |
1088 | 999 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
1000 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
2921 | 1001 |
! |
1002 |
||
1003 |
setMode:aModeSymbol |
|
1004 |
mode := aModeSymbol |
|
31 | 1005 |
! ! |
1006 |
||
2921 | 1007 |
!FileStream methodsFor:'queries'! |
613 | 1008 |
|
2903 | 1009 |
fileSize |
613 | 1010 |
"return the size in bytes of the file" |
1011 |
||
1012 |
%{ /* NOCONTEXT */ |
|
1013 |
||
1014 |
#ifdef transputer |
|
1015 |
FILE *f; |
|
1016 |
int size; |
|
1017 |
||
1133 | 1018 |
if (__INST(filePointer) != nil) { |
1019 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 1020 |
if ((size = filesize(fileno(f))) >= 0) { |
1133 | 1021 |
RETURN ( __MKSMALLINT(size) ); |
613 | 1022 |
} |
1023 |
} |
|
1024 |
#else |
|
1025 |
FILE *f; |
|
1026 |
struct stat buf; |
|
1027 |
int ret; |
|
1028 |
int fd; |
|
1029 |
||
1133 | 1030 |
if (__INST(filePointer) != nil) { |
1031 |
f = __FILEVal(__INST(filePointer)); |
|
613 | 1032 |
fd = fileno(f); |
1033 |
do { |
|
1034 |
ret = fstat(fd, &buf); |
|
1035 |
} while ((ret < 0) && (errno == EINTR)); |
|
1036 |
if (ret >= 0) { |
|
1133 | 1037 |
RETURN ( __MKSMALLINT(buf.st_size) ); |
613 | 1038 |
} |
1133 | 1039 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
613 | 1040 |
} |
1041 |
#endif |
|
1042 |
%}. |
|
1043 |
||
1044 |
"could add a fall-back here: |
|
1045 |
||
1046 |
oldPosition := self position. |
|
1047 |
self setToEnd. |
|
1048 |
sz := self position. |
|
1049 |
self position:oldPosition. |
|
1050 |
^ sz |
|
1051 |
" |
|
1052 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1053 |
filePointer isNil ifTrue:[^ self errorNotOpen]. |
|
1054 |
^ self primitiveFailed |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1055 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1056 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1057 |
size |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1058 |
"return the size of the stream; |
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1059 |
thats the number of bytes of the file." |
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1060 |
|
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1061 |
"/ no obsoleteMethodWarning - size is ok for streams. |
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1062 |
"/ self obsoleteMethodWarning:'use #fileSize'. |
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1063 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1064 |
^ self fileSize. |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1065 |
|
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
1066 |
"Modified: 4.10.1997 / 18:01:09 / cg" |
1 | 1067 |
! ! |
1068 |
||
2 | 1069 |
!FileStream methodsFor:'testing'! |
1070 |
||
1071 |
isFileStream |
|
1072 |
"return true, if the receiver is some kind of fileStream. |
|
1073 |
redefined from Object" |
|
1074 |
||
1075 |
^ true |
|
1076 |
! ! |
|
1077 |
||
1088 | 1078 |
!FileStream class methodsFor:'documentation'! |
1079 |
||
1080 |
version |
|
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3940
diff
changeset
|
1081 |
^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.60 1999-01-15 20:53:35 cg Exp $' |
1088 | 1082 |
! ! |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1083 |
FileStream initialize! |