author | Patrik Svestka <patrik.svestka@gmail.com> |
Tue, 09 Apr 2019 11:34:04 +0200 | |
branch | jv |
changeset 24093 | 0f94f6c8c9d4 |
parent 23107 | 40173e082cbc |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
23107 | 3 |
COPYRIGHT (c) 2015 Jan Vrany |
4 |
COPYRIGHT (c) 2017 Jan Vrany |
|
7872 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5439 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17309 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
ExternalStream subclass:#FileStream |
7872 | 19 |
instanceVariableNames:'pathName canPosition removeOnClose' |
20 |
classVariableNames:'UserInitiatedFileSaveQuerySignal' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Streams-External' |
|
1 | 23 |
! |
24 |
||
216 | 25 |
!FileStream primitiveDefinitions! |
1 | 26 |
%{ |
17078 | 27 |
#include "stxOSDefs.h" |
28 |
||
14632 | 29 |
#undef String |
30 |
#undef Character |
|
31 |
||
1 | 32 |
#include <stdio.h> |
437 | 33 |
#define _STDIO_H_INCLUDED_ |
34 |
||
10 | 35 |
#include <errno.h> |
437 | 36 |
#define _ERRNO_H_INCLUDED_ |
1 | 37 |
|
38 |
#ifdef transputer |
|
39 |
# include <iocntrl.h> |
|
40 |
# ifndef fileno |
|
41 |
/* kludge: inmos forgot fileno */ |
|
42 |
# define fileno(f) ((f)->__file) |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
43 |
# endif |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
44 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
45 |
#else /* not transputer */ |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
46 |
|
1 | 47 |
# include <sys/types.h> |
48 |
# include <sys/stat.h> |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
49 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
50 |
#endif /* not transputer */ |
10 | 51 |
|
42 | 52 |
#ifdef hpux |
68 | 53 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
42 | 54 |
#endif |
55 |
||
2921 | 56 |
#ifdef __VMS__ |
57 |
# include <rms.h> |
|
58 |
#endif |
|
59 |
||
14635
aa0d74813208
Add __ before STX_String, STX_Character
Stefan Vogel <sv@exept.de>
parents:
14633
diff
changeset
|
60 |
#define String __STX_String |
aa0d74813208
Add __ before STX_String, STX_Character
Stefan Vogel <sv@exept.de>
parents:
14633
diff
changeset
|
61 |
#define Character __STX_Character |
14632 | 62 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
63 |
#ifdef __win32__ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
64 |
# define NO_STDIO |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
65 |
# if defined(__i386__) || defined(__x86__) |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
66 |
# define _X86_ |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
67 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
68 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
69 |
# undef INT |
10345 | 70 |
# undef UINT |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
71 |
# undef Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
72 |
# undef Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
73 |
# undef Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
74 |
# undef Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
75 |
# undef Rectangle |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
76 |
# undef Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
77 |
# undef Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
78 |
# undef Date |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
79 |
# undef Set |
5910 | 80 |
# undef Signal |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
81 |
# undef Delay |
5910 | 82 |
# undef Context |
9025 | 83 |
# undef Message |
84 |
# undef Process |
|
10342 | 85 |
# undef Processor |
14632 | 86 |
# undef String |
87 |
# undef Character |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
88 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
89 |
# define NOATOM |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
90 |
# define NOGDICAPMASKS |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
91 |
# define NOMETAFILE |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
92 |
# define NOMINMAX |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
93 |
//# define NOOPENFILE |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
94 |
# define NOSOUND |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
95 |
# define NOWH |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
96 |
//# define NOCOMM |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
97 |
# define NOKANJI |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
98 |
# define NOCRYPT |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
99 |
//# define NOMCX |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
100 |
# define WIN32_LEAN_AND_MEAN |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
101 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
102 |
# include <windows.h> |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
103 |
|
14633 | 104 |
# if 1 // defined(__MINGW64__) |
14632 | 105 |
# include <fcntl.h> |
106 |
# endif |
|
107 |
||
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
108 |
# if defined(__BORLANDC__) |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
109 |
# include <io.h> |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
110 |
# else |
10342 | 111 |
# define lseek _lseek |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
112 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
113 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
114 |
# ifdef __DEF_Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
115 |
# define Array __DEF_Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
116 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
117 |
# ifdef __DEF_Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
118 |
# define Number __DEF_Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
119 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
120 |
# ifdef __DEF_Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
121 |
# define Method __DEF_Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
122 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
123 |
# ifdef __DEF_Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
124 |
# define Point __DEF_Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
125 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
126 |
# ifdef __DEF_Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
127 |
# define Block __DEF_Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
128 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
129 |
# ifdef __DEF_Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
130 |
# define Time __DEF_Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
131 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
132 |
# ifdef __DEF_Date |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
133 |
# define Date __DEF_Date |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
134 |
# endif |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
135 |
# ifdef __DEF_Set |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
136 |
# define Set __DEF_Set |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
137 |
# endif |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
138 |
# ifdef __DEF_Signal |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
139 |
# define Signal __DEF_Signal |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
140 |
# endif |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
141 |
# ifdef __DEF_Delay |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
142 |
# define Delay __DEF_Delay |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
143 |
# endif |
5910 | 144 |
# ifdef __DEF_Context |
145 |
# define Context __DEF_Context |
|
146 |
# endif |
|
9025 | 147 |
# ifdef __DEF_Message |
148 |
# define Message __DEF_Message |
|
149 |
# endif |
|
150 |
# ifdef __DEF_Process |
|
151 |
# define Process __DEF_Process |
|
152 |
# endif |
|
10342 | 153 |
# ifdef __DEF_Processor |
154 |
# define Processor __DEF_Processor |
|
155 |
# endif |
|
14632 | 156 |
# ifdef __DEF_String |
157 |
# define String __DEF_String |
|
158 |
# endif |
|
159 |
# ifdef __DEF_Character |
|
160 |
# define Character __DEF_Character |
|
161 |
# endif |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
162 |
|
14644 | 163 |
# define INT STX_INT |
164 |
# define UINT STX_UINT |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
165 |
|
14632 | 166 |
# if 0 |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
167 |
# ifndef DO_WRAP_CALLS |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
168 |
# define STX_C_CALL0(__nm__, __f__) __f__((__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
169 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __f__((__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
170 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __f__((__a1__), (__a2__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
171 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __f__((__a1__), (__a2__), (__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
172 |
# else |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
173 |
# define STX_C_CALL0(__nm__, __f__) __STX_C_CALL0(__nm__, (void*)__f__) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
174 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __STX_C_CALL1(__nm__, (void*)__f__, (void*)(__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
175 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __STX_C_CALL2(__nm__, (void*)__f__, (void*)(__a1__), (void*)(__a2__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
176 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __STX_C_CALL3(__nm__, (void*)__f__, (void*)(__a1__), (void*)(__a2__), (void*)(__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
177 |
# endif |
14632 | 178 |
# endif |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
179 |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
180 |
/* # define HFILE HANDLE */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
181 |
# define HFILE FILE * |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
182 |
/* # define fileno(f) f */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
183 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
184 |
#else /* not __win32__ */ |
7970 | 185 |
# define HFILE FILE * |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
186 |
#endif /* ! __win32__ */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
187 |
|
16298 | 188 |
//#define DO_WRAP_CALL_FSEEK |
14853
63ffca691137
fseek does not need a WRAP_CALL (I hope)
Claus Gittinger <cg@exept.de>
parents:
14848
diff
changeset
|
189 |
|
7570 | 190 |
#include "stxOSDefs.h" |
191 |
||
10342 | 192 |
#ifndef SEEK_SET |
193 |
# define SEEK_SET 0 |
|
194 |
#endif |
|
195 |
#ifndef SEEK_CUR |
|
196 |
# define SEEK_CUR 1 |
|
197 |
#endif |
|
198 |
#ifndef SEEK_END |
|
199 |
# define SEEK_END 2 |
|
200 |
#endif |
|
1 | 201 |
%} |
173 | 202 |
! ! |
1 | 203 |
|
613 | 204 |
!FileStream class methodsFor:'documentation'! |
205 |
||
206 |
copyright |
|
207 |
" |
|
208 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
23107 | 209 |
COPYRIGHT (c) 2015 Jan Vrany |
210 |
COPYRIGHT (c) 2017 Jan Vrany |
|
7872 | 211 |
All Rights Reserved |
613 | 212 |
|
213 |
This software is furnished under a license and may be used |
|
214 |
only in accordance with the terms of that license and with the |
|
215 |
inclusion of the above copyright notice. This software may not |
|
216 |
be provided or otherwise made available to, or used by, any |
|
217 |
other person. No title to or ownership of the software is |
|
218 |
hereby transferred. |
|
219 |
" |
|
220 |
! |
|
221 |
||
222 |
documentation |
|
223 |
" |
|
224 |
This class provides access to the operating systems underlying file |
|
225 |
system (i.e. its an interface to the stdio library). |
|
226 |
||
227 |
Notice, that on some systems, the standard I/O library has performance |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
228 |
problems when a file is opened for readwrite. |
613 | 229 |
For best results, open files either readonly or writeonly. |
1295 | 230 |
|
2921 | 231 |
Also notice, that some OperatingSystems do not fully support |
232 |
positioning a file stream. |
|
233 |
For example, poor VMS does not allow positioning onto arbitrary |
|
234 |
byte boundaries if the file is a variable-record-RMS file. |
|
15480 | 235 |
(stupid enough, this is the default for textfiles as created by some tools ...) |
2921 | 236 |
Therefore, the instance variable canPosition is set according to |
237 |
this and an error is raised, if a position: is attemted. |
|
238 |
I know, this is ugly, but what else could we do ? |
|
15542 | 239 |
Late note: who cares for VMS these days? |
18651 | 240 |
(and how much useless effort has been put in the past, |
241 |
to support lousy operating systems?) |
|
2921 | 242 |
|
243 |
[instance variables:] |
|
18651 | 244 |
pathName <String> the file's path (if known) |
245 |
canPosition <Boolean> positionable - read above comment |
|
2921 | 246 |
|
1295 | 247 |
[author:] |
18651 | 248 |
Claus Gittinger |
1295 | 249 |
|
250 |
[see also:] |
|
18651 | 251 |
Filename DirectoryStream PipeStream Socket |
613 | 252 |
" |
2921 | 253 |
! |
254 |
||
255 |
examples |
|
256 |
" |
|
257 |
for VMS users only: |
|
258 |
||
259 |
The #openWithMode:attributes: entry allows additional RMS attributes |
|
260 |
to be passed in the second argument, which must be an array of strings |
|
261 |
as described in the 'creat' RTL Library documentation. |
|
262 |
||
263 |
For example, to create a file with fixed records and recordLength of 100, |
|
264 |
use: |
|
265 |
||
7872 | 266 |
|newFile| |
2921 | 267 |
|
7872 | 268 |
newFile := FileStream new pathName:'<nameOfFile>'. |
269 |
newFile setMode:#writeonly. |
|
270 |
newFile openWithMode:'w' attributes:#('rfm=fix' 'fsz=100'). |
|
2921 | 271 |
|
272 |
since all of the above is private protocol, and it is considered bad style to |
|
273 |
access these from user programs, we recommend subclassing FileStream as |
|
274 |
something like VMSFixedRecordFileStream, and redefine the instance creation |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
275 |
method(s) there as appropriate. |
2921 | 276 |
This will retain VMS specifics in one place and enhance maintanability. |
277 |
" |
|
278 |
! ! |
|
279 |
||
280 |
!FileStream class methodsFor:'initialization'! |
|
281 |
||
282 |
initialize |
|
7379 | 283 |
UserInitiatedFileSaveQuerySignal isNil ifTrue:[ |
7965 | 284 |
"/ |
285 |
"/ this is temporary - for now allow an openError to |
|
286 |
"/ be unhandled and proceed by returning a nil from the |
|
287 |
"/ stream creation method. |
|
288 |
"/ In the future, this will be a hard signal. |
|
289 |
"/ THE FUTURE IS NOW!!!! |
|
290 |
"/ |
|
7379 | 291 |
"/ OpenErrorSignal := QuerySignal new. |
292 |
"/ OpenErrorSignal parent:(super openErrorSignal) mayProceed:true. |
|
293 |
"/ OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
294 |
"/ OpenErrorSignal notifierString:'open error'. |
|
4907 | 295 |
|
7965 | 296 |
UserInitiatedFileSaveQuerySignal := QuerySignal new defaultAnswer:true. |
297 |
UserInitiatedFileSaveQuerySignal nameClass:self message:#userInitiatedFileSaveQuerySignal. |
|
2921 | 298 |
] |
3006
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
299 |
|
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
300 |
"Modified: 8.10.1997 / 11:56:39 / cg" |
2921 | 301 |
! ! |
302 |
||
1 | 303 |
!FileStream class methodsFor:'instance creation'! |
304 |
||
613 | 305 |
appendingOldFileNamed:filename |
306 |
"return a FileStream for existing file named filename, aString. |
|
307 |
The file is opened for writeonly access." |
|
1 | 308 |
|
7827 | 309 |
|
1 | 310 |
|newStream| |
7827 | 311 |
|
2 | 312 |
newStream := self new pathName:filename. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
313 |
^ newStream openForAppending. |
7827 | 314 |
|
315 |
" |
|
316 |
FileStream appendingOldFileNamed:'adasdasasd' |
|
317 |
" |
|
613 | 318 |
! |
319 |
||
320 |
appendingOldFileNamed:filename in:aDirectory |
|
321 |
"return a FileStream for existing file named filename, aString |
|
322 |
in aDirectory, a FileDirectory. |
|
323 |
The file is opened for writeonly access." |
|
324 |
||
325 |
|newStream| |
|
326 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
327 |
^ newStream openForAppending. |
1 | 328 |
! |
329 |
||
613 | 330 |
fileNamed:filename |
331 |
"return a stream on file filename - if the file does not |
|
332 |
already exist, create it. |
|
92 | 333 |
The file is opened for read/write access." |
1 | 334 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
335 |
[ |
7965 | 336 |
^ self oldFileNamed:filename. |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
337 |
] on:self openErrorSignal do:[:ex| ]. |
613 | 338 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
339 |
^ self newFileNamed:filename |
613 | 340 |
! |
341 |
||
342 |
fileNamed:filename in:aDirectory |
|
343 |
"return a stream on file filename - if the file does not |
|
344 |
already exist, create it. |
|
345 |
The file is opened for read/write access." |
|
346 |
||
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
347 |
[ |
7965 | 348 |
^ self oldFileNamed:filename in:aDirectory. |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
349 |
] on:self openErrorSignal do:[:ex| ]. |
613 | 350 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
351 |
^ self newFileNamed:filename in:aDirectory |
1 | 352 |
! |
353 |
||
92 | 354 |
newFileForWritingNamed:filename |
355 |
"return a FileStream for new file named filename, aString. |
|
356 |
If the file exists, it is truncated, otherwise created. |
|
357 |
The file is opened for writeonly access." |
|
358 |
||
359 |
|newStream| |
|
360 |
newStream := self new pathName:filename. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
361 |
^ newStream createForWriting. |
92 | 362 |
! |
363 |
||
364 |
newFileForWritingNamed:filename in:aDirectory |
|
365 |
"return a FileStream for new file named filename, aString |
|
366 |
in aDirectory, a FileDirectory. |
|
367 |
If the file exists, it is truncated, otherwise created. |
|
368 |
The file is opened for writeonly access." |
|
369 |
||
370 |
|newStream| |
|
371 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
372 |
^ newStream createForWriting. |
92 | 373 |
! |
374 |
||
613 | 375 |
newFileNamed:filename |
376 |
"return a FileStream for new file named filename, aString. |
|
377 |
If the file exists, it is truncated, otherwise created. |
|
378 |
The file is opened for read/write access." |
|
379 |
||
380 |
|newStream| |
|
381 |
newStream := self new pathName:filename. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
382 |
^ newStream createForReadWrite. |
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
383 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
384 |
"Modified: / 28.10.1997 / 14:28:08 / cg" |
613 | 385 |
! |
386 |
||
387 |
newFileNamed:filename in:aDirectory |
|
388 |
"return a FileStream for new file named filename, aString |
|
389 |
in aDirectory, a FileDirectory. |
|
390 |
If the file exists, it is truncated, otherwise created. |
|
391 |
The file is opened for read/write access." |
|
392 |
||
393 |
|newStream| |
|
394 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
395 |
^ newStream createForReadWrite. |
613 | 396 |
! |
397 |
||
15462 | 398 |
newTemporary |
399 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
400 |
The created file has the name '/tmp/stxtmp_xx_nn' where xx is our |
|
401 |
unix process id, and nn is a unique number, incremented with every call to this method. |
|
15542 | 402 |
If any of the environment variables ST_TMPDIR or TMPDIR is set, |
15462 | 403 |
its value defines the temp directory." |
404 |
||
405 |
^ self newTemporaryIn:Filename tempDirectory |
|
406 |
||
407 |
" |
|
15542 | 408 |
FileStream newTemporary |
409 |
FileStream newTemporary |
|
15462 | 410 |
" |
411 |
! |
|
412 |
||
413 |
newTemporaryIn:aDirectoryOrNil |
|
414 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
415 |
The created file is in aDirectoryPrefix and named 'stxtmp_xx_nn', |
|
15542 | 416 |
where xx is our unix process id, and nn is a unique number, incremented |
15462 | 417 |
with every call to this method." |
418 |
||
419 |
^ self newTemporaryIn:aDirectoryOrNil nameTemplate:Filename tempFileNameTemplate |
|
420 |
||
421 |
"temp files in '/tmp': |
|
422 |
||
15542 | 423 |
FileStream newTemporary |
15462 | 424 |
" |
425 |
||
15542 | 426 |
"temp files somewhere |
15462 | 427 |
(not recommended - use above since it can be controlled via shell variables): |
428 |
||
15542 | 429 |
FileStream newTemporaryIn:'/tmp' |
430 |
FileStream newTemporaryIn:'/tmp' |
|
431 |
FileStream newTemporaryIn:'/usr/tmp' |
|
432 |
FileStream newTemporaryIn:'/' |
|
15462 | 433 |
" |
434 |
||
435 |
"a local temp file: |
|
436 |
||
15542 | 437 |
FileStream newTemporaryIn:'' |
438 |
FileStream newTemporaryIn:nil |
|
439 |
FileStream newTemporaryIn:'.' |
|
440 |
FileStream newTemporaryIn:('source' asFilename) |
|
15462 | 441 |
" |
442 |
! |
|
443 |
||
444 |
newTemporaryIn:aDirectoryOrNil nameTemplate:template |
|
445 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
446 |
The created file is in aDirectoryOrNil and named after the given template, |
|
447 |
in which %1 and %2 are expanded to the unix process id, and a unique number, incremented |
|
448 |
with every call to this method respectively. |
|
449 |
See also: #newTemporary which looks for a good temp directory." |
|
450 |
||
451 |
|nameString random prevRandom prevNameString newTempFilename stream| |
|
452 |
||
453 |
[ |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
454 |
prevRandom := random. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
455 |
prevNameString := nameString. |
15462 | 456 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
457 |
"Use random numbers in order to improve the security |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
458 |
by making the generated names less predictable" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
459 |
[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
460 |
random := RandomGenerator nextLettersOrDigits:4. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
461 |
] doWhile:[random = prevRandom]. |
15462 | 462 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
463 |
nameString := template bindWith:(OperatingSystem getProcessId) with:random. |
15462 | 464 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
465 |
aDirectoryOrNil isNil ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
466 |
newTempFilename := nameString. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
467 |
] ifFalse:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
468 |
newTempFilename := aDirectoryOrNil asFilename constructString:nameString. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
469 |
]. |
15462 | 470 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
471 |
[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
472 |
stream := self open:newTempFilename withMode:#(CREATE_NEW GENERIC_READ_WRITE). |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
473 |
] on:OpenError do:[:ex| |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
474 |
ex errorCategory ~~ #existingReferentSignal ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
475 |
"some fundamental error, raise exception" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
476 |
ex reject. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
477 |
]. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
478 |
prevNameString = nameString ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
479 |
"no more names - probably a bad template" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
480 |
ex reject. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
481 |
]. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
482 |
"file exists, retry another one" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
483 |
]. |
15462 | 484 |
] doWhile:[ |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
485 |
stream isNil and:[prevNameString ~= nameString] "/ if namestring didn't change, the template is bad |
15462 | 486 |
]. |
487 |
^ stream |
|
488 |
||
489 |
"temp files in '/tmp': |
|
490 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
491 |
FileStream newTemporaryIn:'/tmp' asFilename nameTemplate:'foo%1_%2' |
15462 | 492 |
|
493 |
This must fail on the second try: |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
494 |
FileStream newTemporaryIn:'/tmp' asFilename nameTemplate:'foo' |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
495 |
FileStream newTemporaryIn:'c:\temp' asFilename nameTemplate:'foo' |
15462 | 496 |
" |
497 |
||
498 |
"temp files somewhere |
|
499 |
(not recommended - use above since it can be controlled via shell variables): |
|
500 |
||
501 |
FileStream newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' |
|
502 |
FileStream newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' |
|
503 |
FileStream newTemporaryIn:'/usr/tmp' nameTemplate:'foo%1_%2' |
|
504 |
FileStream newTemporaryIn:'/' nameTemplate:'foo%1_%2' |
|
505 |
" |
|
506 |
||
507 |
"a local temp file: |
|
508 |
||
509 |
FileStream newTemporaryIn:'' nameTemplate:'foo%1_%2' |
|
510 |
FileStream newTemporaryIn:nil nameTemplate:'foo%1_%2' |
|
511 |
FileStream newTemporaryIn:'.' nameTemplate:'foo%1_%2' |
|
512 |
FileStream newTemporaryIn:('source' asFilename) nameTemplate:'foo%1_%2' |
|
513 |
" |
|
514 |
! |
|
515 |
||
15542 | 516 |
newTemporaryIn:aDirectoryOrNil withSuffix:aSuffixString |
15469 | 517 |
"create atomically a new file and return the file stream - use this for temporary files. |
518 |
The created file is in aDirectoryPrefix and named 'stxtmp_xx_nn', |
|
519 |
where xx is our unix process id, and nn is a unique number, incremented |
|
520 |
with every call to this method." |
|
15542 | 521 |
|
522 |
^ self |
|
523 |
newTemporaryIn:aDirectoryOrNil |
|
524 |
nameTemplate:(Filename tempFileNameTemplate asFilename |
|
525 |
withSuffix:aSuffixString) asString |
|
15469 | 526 |
|
527 |
" |
|
528 |
FileStream newTemporaryWithSuffix:'txt' |
|
529 |
FileStream newTemporaryIn:'/tmp' withSuffix:'txt' |
|
530 |
" |
|
531 |
! |
|
532 |
||
533 |
newTemporaryWithSuffix:aString |
|
534 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
535 |
The created file has the name '/tmp/stxtmp_xx_nn' where xx is our |
|
536 |
unix process id, and nn is a unique number, incremented with every call to this method. |
|
537 |
If any of the environment variables ST_TMPDIR or TMPDIR is set, |
|
538 |
its value defines the temp directory." |
|
539 |
||
540 |
^ self newTemporaryIn:Filename tempDirectory withSuffix:aString |
|
541 |
||
542 |
" |
|
543 |
FileStream newTemporaryWithSuffix:'txt' |
|
544 |
" |
|
545 |
! |
|
546 |
||
1 | 547 |
oldFileNamed:filename |
548 |
"return a FileStream for existing file named filename, aString. |
|
7827 | 549 |
The file is opened for read/write access. |
550 |
Raises an error if the file does not exist." |
|
1 | 551 |
|
552 |
|newStream| |
|
553 |
||
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
554 |
"/ We can do the following, but is is an extra OS-systemcall: |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
555 |
"/ (OperatingSystem isReadable:filename) ifFalse:[^ self new openError]. |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
556 |
|
2 | 557 |
newStream := self new pathName:filename. |
1 | 558 |
newStream readwrite. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
559 |
^ newStream openForReadWrite. |
7827 | 560 |
|
561 |
" |
|
15489 | 562 |
'/tmp/dAsGiBtEsNiChT' asFilename remove. |
563 |
FileStream oldFileNamed:'/tmp/dAsGiBtEsNiChT' |
|
7827 | 564 |
" |
1 | 565 |
! |
566 |
||
567 |
oldFileNamed:filename in:aDirectory |
|
568 |
"return a FileStream for existing file named filename, aString |
|
569 |
in aDirectory, a FileDirectory. |
|
7827 | 570 |
The file is opened for read/write access. |
571 |
Raises an error if the file does not exist." |
|
1 | 572 |
|
573 |
|newStream| |
|
2 | 574 |
newStream := self new pathName:filename in:aDirectory. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
575 |
^ newStream openForReadWrite. |
7827 | 576 |
|
577 |
" |
|
578 |
FileStream oldFileNamed:'dAsGiBtEsNiChT' in:'/' |
|
579 |
" |
|
1 | 580 |
! |
581 |
||
15462 | 582 |
open:aFilenameString withMode:anArrayOrString |
583 |
"The argument de |
|
584 |
The file is opened for read/write access." |
|
585 |
||
586 |
|stream| |
|
587 |
||
588 |
stream := self new pathName:aFilenameString. |
|
15542 | 589 |
stream |
590 |
readwrite; "/ assume read/write mode, but this depends on the args |
|
591 |
openWithMode:anArrayOrString attributes:nil. |
|
15462 | 592 |
|
593 |
^ stream |
|
594 |
! |
|
595 |
||
1 | 596 |
readonlyFileNamed:filename |
20157 | 597 |
"Return a readonly FileStream for existing file named filename, a String. |
7827 | 598 |
Raises an error if the file does not exist." |
1 | 599 |
|
600 |
|newStream| |
|
601 |
||
20157 | 602 |
"/ We can do the following, but it is an extra system call: |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
603 |
"/ (OperatingSystem isReadable:filename) ifFalse:[^ self new openError]. |
1 | 604 |
|
2 | 605 |
newStream := self new pathName:filename. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
606 |
^ newStream openForReading. |
7827 | 607 |
|
608 |
" |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
609 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' |
7827 | 610 |
" |
1 | 611 |
! |
612 |
||
613 |
readonlyFileNamed:filename in:aDirectory |
|
614 |
"return a readonly FileStream for existing file named filename, aString |
|
7827 | 615 |
in aDirectory, a fileName or string instance representing a directory. |
616 |
Raises an error if the file does not exist." |
|
1 | 617 |
|
618 |
|newStream| |
|
2 | 619 |
newStream := self new pathName:filename in:aDirectory. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
620 |
^ newStream openForReading. |
7827 | 621 |
|
622 |
" |
|
623 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' in:'/' |
|
624 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' in:'/' asFilename |
|
625 |
" |
|
159 | 626 |
! ! |
627 |
||
7298 | 628 |
!FileStream class methodsFor:'Compatibility-ANSI'! |
6586 | 629 |
|
630 |
write:filename |
|
631 |
"return a FileStream for new file named filename, aString. |
|
632 |
If the file exists, it is truncated, otherwise created. |
|
633 |
The file is opened for read/write access. |
|
634 |
Same as newFileNamed: for ANSI compatibilily" |
|
635 |
||
636 |
^ self newFileNamed:filename |
|
637 |
! ! |
|
638 |
||
7298 | 639 |
!FileStream class methodsFor:'Compatibility-Dolphin'! |
6244 | 640 |
|
641 |
read:filename text:text |
|
642 |
"return a readonly FileStream for the existing file named filename, aString. |
|
643 |
If the argument, text is false, the stream is setup to read binary bytes, |
|
644 |
if false, it reads characters." |
|
645 |
||
646 |
|s| |
|
647 |
||
648 |
s := self readOnlyFileNamed:filename. |
|
649 |
text ifTrue:[ |
|
7872 | 650 |
s text |
6244 | 651 |
] ifFalse:[ |
7872 | 652 |
s binary |
6244 | 653 |
]. |
654 |
^ s |
|
6474 | 655 |
! |
656 |
||
657 |
write:filename mode:modeSymbol |
|
658 |
"return a writable FileStream for the file named filename, aString. |
|
659 |
The modeSymbol controls how the file is opened; currently supported are: |
|
7965 | 660 |
#append |
6474 | 661 |
" |
662 |
||
663 |
modeSymbol == #append ifTrue:[ |
|
7965 | 664 |
^ self appendingOldFileNamed:filename |
6474 | 665 |
]. |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
666 |
"/ self openErrorSignal is a Notification. This will change.. |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
667 |
"/ We want to raise a real error here |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
668 |
super openErrorSignal raiseRequestErrorString:' - unsupported mode'. |
6474 | 669 |
^ nil |
670 |
! |
|
671 |
||
672 |
write:filename text:textModeBoolean |
|
673 |
"return a writable FileStream for the file named filename, aString. |
|
674 |
If the argument, text is false, the stream is setup to write binary bytes, |
|
675 |
if false, it writes characters." |
|
676 |
||
677 |
|s| |
|
678 |
||
6475 | 679 |
s := self newFileNamed:filename. |
6474 | 680 |
s notNil ifTrue:[ |
7872 | 681 |
textModeBoolean ifTrue:[ |
682 |
s text |
|
683 |
] ifFalse:[ |
|
684 |
s binary |
|
685 |
] |
|
6474 | 686 |
]. |
687 |
^ s |
|
6244 | 688 |
! ! |
689 |
||
7298 | 690 |
!FileStream class methodsFor:'Compatibility-Squeak'! |
5186 | 691 |
|
19638 | 692 |
fileIn:aPath |
693 |
aPath asFilename fileIn |
|
694 |
! |
|
695 |
||
16109 | 696 |
forceNewFileNamed:filename |
697 |
"return a writing FileStream for new file named filename, aString. |
|
16113 | 698 |
If it already exists, it is overwritten silently." |
16109 | 699 |
|
700 |
^ self newFileNamed:filename |
|
701 |
! |
|
702 |
||
5186 | 703 |
readOnlyFileNamed:filename |
6244 | 704 |
"return a readonly FileStream for the existing file named filename, aString." |
705 |
||
5186 | 706 |
^ self readonlyFileNamed:filename |
707 |
! ! |
|
708 |
||
7298 | 709 |
!FileStream class methodsFor:'Compatibility-VW'! |
6170
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
710 |
|
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
711 |
badArgumentsSignal |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
712 |
^ Error |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
713 |
! ! |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
714 |
|
3233
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
715 |
!FileStream class methodsFor:'Signal constants'! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
716 |
|
4907 | 717 |
userInitiatedFileSaveQuerySignal |
718 |
"return the query signal, which is raised before a user-initiated |
|
719 |
file-save / file-saveAs operation is performed. |
|
720 |
The query will be invoked with the fileName which is about to be |
|
721 |
written to. |
|
722 |
The default signal here returnes true, which will grant the save. |
|
723 |
End-user applications may want to catch this signal, |
|
724 |
and only return true for certain directories." |
|
725 |
||
726 |
^ UserInitiatedFileSaveQuerySignal |
|
727 |
||
3233
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
728 |
! ! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
729 |
|
7261 | 730 |
!FileStream methodsFor:'Compatibility-Squeak'! |
3044 | 731 |
|
732 |
fullName |
|
733 |
"Squeak compatibility: return the full pathname" |
|
734 |
||
735 |
^ pathName asFilename pathName |
|
736 |
||
737 |
"Created: 17.10.1997 / 17:04:12 / cg" |
|
738 |
"Modified: 20.10.1997 / 19:22:44 / cg" |
|
739 |
! ! |
|
740 |
||
15489 | 741 |
!FileStream methodsFor:'access rights'! |
742 |
||
743 |
accessRights |
|
744 |
"return the access rights of the file as opaque data |
|
745 |
(SmallInteger in unix/linux)" |
|
746 |
||
747 |
^ OperatingSystem accessModeOfFd:self fileDescriptor. |
|
748 |
||
749 |
" |
|
750 |
'Make.proto' asFilename readingFileDo:[:s| |
|
15542 | 751 |
s accessRights |
15489 | 752 |
] |
753 |
" |
|
754 |
! |
|
755 |
||
756 |
accessRights:opaqueData |
|
757 |
"set the access rights of the file to opaqueData, |
|
758 |
which is normally retrieved by Filename>>#accessRights |
|
759 |
or FileStreamm>>#accessRights." |
|
760 |
||
21237 | 761 |
(OperatingSystem changeAccessModeOfFd:self fileHandle to:opaqueData) ifFalse:[ |
762 |
OperatingSystem accessDeniedErrorSignal |
|
763 |
raiseRequestWith:self |
|
764 |
errorString:(' - cannot change access rights: ' , self pathName). |
|
15489 | 765 |
]. |
766 |
||
767 |
" |
|
768 |
'Make.proto' asFilename readingFileDo:[:s| |
|
21237 | 769 |
s accessRights:s accessRights |
15489 | 770 |
] |
771 |
" |
|
772 |
||
773 |
" |
|
774 |
'/' asFilename readingFileDo:[:s| |
|
21237 | 775 |
s accessRights:s accessRights |
15489 | 776 |
] |
777 |
" |
|
778 |
! ! |
|
779 |
||
1 | 780 |
!FileStream methodsFor:'accessing'! |
781 |
||
17309 | 782 |
asFilename |
783 |
"return the file name" |
|
784 |
||
785 |
^ pathName asFilename |
|
786 |
! |
|
787 |
||
12950
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
788 |
contentsOfEntireFile |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
789 |
"ST-80 compatibility: return contents as a String (or byteArray, if in binary mode). |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
790 |
See also #contents, which returns the lines as stringCollection for text files." |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
791 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
792 |
position == 0 ifTrue:[ |
13778 | 793 |
^ self next:(self fileSize) |
12950
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
794 |
]. |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
795 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
796 |
^ super contentsOfEntireFile |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
797 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
798 |
"Created: / 18-07-2010 / 22:17:49 / cg" |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
799 |
! |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
800 |
|
1 | 801 |
directoryName |
2903 | 802 |
"return the name of the directory I'm in as a string" |
1 | 803 |
|
2903 | 804 |
^ pathName asFilename directoryName |
1 | 805 |
! |
806 |
||
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
807 |
fileName |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
808 |
"return the file name - same as pathName for compatibility with |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
809 |
other smalltalks" |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
810 |
|
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
811 |
^ pathName asFilename |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
812 |
! |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
813 |
|
1 | 814 |
name |
2903 | 815 |
"return my name without leading direcory-path (i.e. the plain fileName) |
816 |
as a string" |
|
1 | 817 |
|
2903 | 818 |
^ pathName asFilename baseName |
1 | 819 |
! |
820 |
||
821 |
pathName |
|
822 |
"return the pathname" |
|
823 |
||
824 |
^ pathName |
|
613 | 825 |
! |
826 |
||
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
827 |
removeOnClose:aBoolean |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
828 |
"set/clear the removeOnClose flag. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
829 |
If set, the file will be removed when closed. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
830 |
Provided mostly for OS's which do not allow an |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
831 |
open file to be removed (i.e. non unixes), |
12207
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
832 |
when a fileStream for a tempFile is used. |
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
833 |
Especially, the CVS-SourceCodeManager returns |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
834 |
this kind of file-handles occasionally. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
835 |
This is an ST/X special feature which is not portable |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
836 |
to other systems." |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
837 |
|
15475 | 838 |
removeOnClose := aBoolean. |
18781 | 839 |
self reRegisterForFinalization. |
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
840 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
841 |
"Modified: / 13.8.1998 / 12:10:07 / cg" |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
842 |
! |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
843 |
|
613 | 844 |
store:something |
845 |
"what really should this do" |
|
846 |
||
847 |
self nextPutAll:something storeString |
|
848 |
! ! |
|
849 |
||
850 |
!FileStream methodsFor:'error handling'! |
|
851 |
||
12687 | 852 |
openError:errorNumber |
853 |
"report an error, that file open failed" |
|
854 |
||
855 |
"This has changed from previous releases: |
|
856 |
in earlier versions of ST/X, failed file open operations |
|
857 |
returned nil - now, an error is raised. |
|
858 |
The old behavior is simulated by providing an exception |
|
859 |
handler, which proceeds with nil: |
|
860 |
||
13778 | 861 |
OpenError ignoreIn:[ |
862 |
'old code expecting nil return values on errors when doing an open'. |
|
12687 | 863 |
|
13778 | 864 |
('/dasGIBtEsbeStimmtNiChT' asFilename readStream) isNil ifTrue:[ |
865 |
self warn:'open failed'. |
|
866 |
]. |
|
867 |
] |
|
12687 | 868 |
" |
869 |
||
870 |
"{ Pragma: +optSpace }" |
|
871 |
||
872 |
LastErrorNumber := errorNumber. |
|
873 |
^ super openError:errorNumber. |
|
874 |
||
875 |
" |
|
876 |
'/dasGIBtEsbeStimmtNiChT' asFilename readStream |
|
877 |
" |
|
613 | 878 |
! ! |
879 |
||
15469 | 880 |
!FileStream methodsFor:'finalization'! |
881 |
||
882 |
executor |
|
883 |
|executor| |
|
884 |
||
885 |
executor := super executor. |
|
18985 | 886 |
"keep the pathname for any FileStream" |
887 |
executor setPathName:pathName removeOnClose:removeOnClose. |
|
15469 | 888 |
^ executor |
889 |
! ! |
|
890 |
||
20367 | 891 |
|
8362 | 892 |
!FileStream methodsFor:'misc functions'! |
893 |
||
8768 | 894 |
copyToEndInto:outStream bufferSize:bufferSize |
8362 | 895 |
"copy the data into another stream." |
896 |
||
897 |
|pos n nWritten| |
|
898 |
||
899 |
"the sendfile() system call currently (2004-05-07) handles only descriptors that |
|
900 |
support mmap-like operations as from-fd. |
|
901 |
Therefore, this method is reimplemented here (from ExternalStream)" |
|
902 |
||
903 |
outStream isExternalStream ifTrue:[ |
|
21237 | 904 |
pos := self position. |
905 |
n := self size - pos. |
|
906 |
nWritten := OperatingSystem |
|
907 |
copyFromFd:self fileHandle |
|
908 |
toFd:outStream fileHandle |
|
909 |
startIndex:pos |
|
910 |
count:n. |
|
911 |
nWritten > 0 ifTrue:[ |
|
912 |
self position:pos+nWritten. |
|
913 |
]. |
|
914 |
nWritten = n ifTrue:[ |
|
915 |
^ self |
|
916 |
]. |
|
8362 | 917 |
]. |
8768 | 918 |
^ super copyToEndInto:outStream bufferSize:bufferSize. |
8362 | 919 |
|
920 |
" |
|
921 |
|in out| |
|
922 |
||
923 |
in := 'Makefile' asFilename readStream. |
|
924 |
out := Stdout. |
|
925 |
in copyToEndInto:out. |
|
926 |
in close. |
|
927 |
" |
|
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
928 |
! |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
929 |
|
17151 | 930 |
syncFileSystem |
931 |
"sync the filesystem containing this FileStream" |
|
932 |
||
933 |
OperatingSystem syncFileSystem:handle. |
|
934 |
! |
|
935 |
||
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
936 |
truncateTo:newSize |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
937 |
"truncate the underlying OS file to newSize. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
938 |
Warning: this may not be implemented on all platforms." |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
939 |
|
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
940 |
%{ |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
941 |
#ifdef HAS_FTRUNCATE |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
942 |
OBJ fp = __INST(handle); |
16373
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16369
diff
changeset
|
943 |
FILE *f; |
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
944 |
off_t truncateSize; |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
945 |
|
16373
9b9827530bf4
Adapt previous change for linux
Stefan Vogel <sv@exept.de>
parents:
16369
diff
changeset
|
946 |
if ((fp != nil) && (__INST(mode) != @symbol(readonly))) { |
18651 | 947 |
if (__isSmallInteger(newSize)) { |
948 |
truncateSize = __intVal(newSize); |
|
949 |
if (truncateSize < 0) { |
|
950 |
goto getOutOfHere; |
|
951 |
} |
|
952 |
} else { |
|
953 |
truncateSize = __signedLongIntVal(newSize); |
|
954 |
if (truncateSize < 0) { |
|
955 |
goto getOutOfHere; |
|
956 |
} |
|
957 |
if (truncateSize == 0) { |
|
958 |
if (sizeof(truncateSize) == 8) { |
|
959 |
if (__signedLong64IntVal(newSize, &truncateSize) == 0 || truncateSize < 0) { |
|
960 |
goto getOutOfHere; |
|
961 |
} |
|
962 |
} else { |
|
963 |
goto getOutOfHere; |
|
964 |
} |
|
965 |
} |
|
966 |
} |
|
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
967 |
|
18651 | 968 |
f = __FILEVal(fp); |
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
969 |
|
18651 | 970 |
if (__INST(buffered) == true) { |
971 |
fflush(f); |
|
972 |
fseek(f, 0L, SEEK_END); /* needed in stdio */ |
|
973 |
} |
|
974 |
ftruncate(fileno(f), truncateSize); |
|
975 |
RETURN (self); |
|
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
976 |
} |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
977 |
getOutOfHere: ; |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
978 |
#endif |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
979 |
%}. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
980 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
981 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
982 |
newSize < 0 ifTrue:[ |
18651 | 983 |
self error:'wrong arg'. |
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
984 |
]. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
985 |
self errorUnsupportedOperation |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
986 |
|
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
987 |
" |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
988 |
|f s| |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
989 |
|
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
990 |
f := 'testTTTT' asFilename. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
991 |
s := f writeStream. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
992 |
s next:1000 put:$a. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
993 |
s truncateTo:100. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
994 |
s close. |
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
995 |
|
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
996 |
Transcript showCR:(f fileSize). |
18651 | 997 |
f remove. |
16369
a938f5cbc31d
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
16298
diff
changeset
|
998 |
" |
8362 | 999 |
! ! |
1000 |
||
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1001 |
!FileStream methodsFor:'positioning'! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1002 |
|
15620 | 1003 |
position |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
1004 |
"return the read/write position in the file" |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1005 |
|
16298 | 1006 |
|error| |
3971 | 1007 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1008 |
HFILE f; |
7872 | 1009 |
off_t currentPosition; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1010 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1011 |
if (__INST(handle) != nil) { |
18651 | 1012 |
do { |
1013 |
f = __FILEVal(__INST(handle)); |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1014 |
#ifdef __win32__ |
18651 | 1015 |
__threadErrno = 0; |
1016 |
if (__INST(buffered) == true) { |
|
6333 | 1017 |
# if 0 |
18651 | 1018 |
currentPosition = STX_C_CALL1( "ftell", ftell, f); |
6333 | 1019 |
# else |
18651 | 1020 |
currentPosition = ftell(f); |
1021 |
__threadErrno = errno; |
|
6333 | 1022 |
# endif |
18651 | 1023 |
} else { |
1024 |
OBJ rA = __INST(readAhead); |
|
1025 |
off_t offs = 0; |
|
6333 | 1026 |
|
18651 | 1027 |
if (rA != nil) { |
1028 |
__INST(readAhead) = nil; |
|
1029 |
offs = -1; |
|
1030 |
} |
|
6333 | 1031 |
# if 0 |
18651 | 1032 |
currentPosition = STX_C_CALL3( "lseek", lseek, fileno(f), offs, SEEK_CUR); |
6333 | 1033 |
# else |
18651 | 1034 |
currentPosition = lseek(fileno(f), offs, SEEK_CUR); |
1035 |
__threadErrno = errno; |
|
6333 | 1036 |
# endif |
18651 | 1037 |
} |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1038 |
#else /* !__win32__ */ |
18651 | 1039 |
if (__INST(buffered) == true) { |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1040 |
#ifdef _LFS_LARGEFILE |
18651 | 1041 |
currentPosition = ftello(f); |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1042 |
#else |
18651 | 1043 |
currentPosition = ftell(f); |
8039
6ef0379296cc
#position0Based did not work on unbuffered streams (with 64 bit off_t).
Stefan Vogel <sv@exept.de>
parents:
8034
diff
changeset
|
1044 |
#endif /* ! _LFS_LARGEFILE */ |
18651 | 1045 |
} else { |
18421
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1046 |
if (sizeof(off_t) == 8) { |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1047 |
currentPosition = lseek64(fileno(f), (off_t)0, SEEK_CUR); |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1048 |
} else { |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1049 |
currentPosition = lseek(fileno(f), (off_t)0, SEEK_CUR); |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1050 |
} |
15620 | 1051 |
} |
8039
6ef0379296cc
#position0Based did not work on unbuffered streams (with 64 bit off_t).
Stefan Vogel <sv@exept.de>
parents:
8034
diff
changeset
|
1052 |
#endif /* !WIN32 */ |
18651 | 1053 |
} while ((currentPosition < 0) && (__threadErrno == EINTR)); |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
1054 |
|
18651 | 1055 |
if (currentPosition >= 0) { |
1056 |
OBJ rslt; |
|
7872 | 1057 |
|
18651 | 1058 |
if (sizeof(currentPosition) == 8) { |
1059 |
rslt = __MKINT64 (¤tPosition); |
|
1060 |
} else { |
|
1061 |
rslt = __MKINT(currentPosition); |
|
1062 |
} |
|
1063 |
RETURN ( rslt ); |
|
1064 |
} |
|
1065 |
error = __mkSmallInteger(__threadErrno); |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1066 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1067 |
%}. |
16298 | 1068 |
error notNil ifTrue:[ |
18651 | 1069 |
lastErrorNumber := error. |
1070 |
^ self ioError:error. |
|
16298 | 1071 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1072 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1073 |
^ self primitiveFailed |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1074 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1075 |
|
15620 | 1076 |
position:newPos |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1077 |
"set the read/write position in the file" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1078 |
|
16298 | 1079 |
|rslt error| |
3971 | 1080 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1081 |
HFILE f; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1082 |
long ret; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1083 |
OBJ fp; |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1084 |
|
8913 | 1085 |
if ((__INST(canPosition) != false) || (newPos == __mkSmallInteger(0))) { |
18651 | 1086 |
if ((fp = __INST(handle)) != nil) { |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1087 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1088 |
#if defined(_LFS_LARGE_FILE) && !defined(__win32__) |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1089 |
# define FSEEK fseeko |
18651 | 1090 |
off_t nP; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1091 |
#else |
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1092 |
#define FSEEK fseek |
18651 | 1093 |
long nP; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1094 |
#endif |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1095 |
|
18651 | 1096 |
if (__isSmallInteger(newPos)) { |
1097 |
nP = __intVal(newPos); |
|
1098 |
if (nP < 0) { |
|
1099 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1100 |
goto getOutOfHere; |
|
1101 |
} |
|
1102 |
} else { |
|
1103 |
nP = __signedLongIntVal(newPos); |
|
1104 |
if (nP < 0) { |
|
1105 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1106 |
goto getOutOfHere; |
|
1107 |
} |
|
1108 |
if (nP == 0) { |
|
1109 |
if (sizeof(nP) == 8) { |
|
1110 |
if (__signedLong64IntVal(newPos, &nP) == 0 || nP < 0) { |
|
1111 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1112 |
goto getOutOfHere; |
|
1113 |
} |
|
1114 |
} else { |
|
1115 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1116 |
goto getOutOfHere; |
|
1117 |
} |
|
1118 |
} |
|
1119 |
} |
|
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1120 |
|
18651 | 1121 |
f = __FILEVal(fp); |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1122 |
|
18651 | 1123 |
do { |
14853
63ffca691137
fseek does not need a WRAP_CALL (I hope)
Claus Gittinger <cg@exept.de>
parents:
14848
diff
changeset
|
1124 |
#if defined(DO_WRAP_CALL_FSEEK) |
18651 | 1125 |
__threadErrno = 0; |
1126 |
if (__INST(buffered) == true) { |
|
1127 |
ret = STX_C_CALL3( "fseek", fseek, f, nP, SEEK_SET); |
|
1128 |
} else { |
|
1129 |
__INST(readAhead) = nil; |
|
1130 |
ret = STX_C_CALL3( "lseek", lseek, fileno(f), nP, SEEK_SET); |
|
1131 |
} |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1132 |
#else |
18651 | 1133 |
if (__INST(buffered) == true) { |
1134 |
ret = FSEEK(f, nP, SEEK_SET); |
|
1135 |
} else { |
|
18421
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1136 |
#if !defined(__BORLANDC__) |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1137 |
if (sizeof(nP) == 8) |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1138 |
ret = lseek64(fileno(f), nP, SEEK_SET); |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1139 |
else |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1140 |
#endif |
18651 | 1141 |
ret = lseek(fileno(f), nP, SEEK_SET); |
1142 |
} |
|
1143 |
__threadErrno = errno; |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1144 |
#endif |
18651 | 1145 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1146 |
if (ret >= 0) { |
|
1147 |
__INST(position) = newPos; __STORE(self, newPos); |
|
1148 |
/* |
|
1149 |
* just to make certain ... |
|
1150 |
*/ |
|
1151 |
__INST(hitEOF) = false; |
|
1152 |
RETURN ( self ); |
|
1153 |
} |
|
1154 |
error = __mkSmallInteger(__threadErrno); |
|
1155 |
} |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1156 |
} |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1157 |
getOutOfHere: ; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1158 |
#undef FSEEK |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1159 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1160 |
canPosition == false ifTrue:[ |
18651 | 1161 |
"/ position by rewinding & re-reading everything up-to |
1162 |
"/ that point. |
|
1163 |
^ self slowPosition:newPos |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1164 |
]. |
16298 | 1165 |
error notNil ifTrue:[ |
18651 | 1166 |
lastErrorNumber := error. |
1167 |
(OperatingSystem errorSymbolForNumber:error) == #EINVAL ifTrue:[ |
|
1168 |
"/ invalid position |
|
1169 |
^ self positionError. |
|
1170 |
]. |
|
1171 |
"/ assume I/O error |
|
1172 |
^ self ioError:error. |
|
2963 | 1173 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1174 |
handle isNil ifTrue:[^ self errorNotOpen]. |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1175 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1176 |
rslt := self positionFile:handle position:newPos. |
5493 | 1177 |
rslt >= 0 ifTrue:[ |
18651 | 1178 |
position := newPos. |
5493 | 1179 |
] ifFalse:[ |
18651 | 1180 |
hitEOF := true. |
5493 | 1181 |
] |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1182 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1183 |
|
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1184 |
reset |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1185 |
"additionaly to setting the position to the beginning of the file, |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1186 |
re-open a previously closed file. This behavior is compatible |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1187 |
with other Smalltalk dialects" |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1188 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1189 |
handle isNil ifTrue:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1190 |
"reopen the file" |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1191 |
mode == #readonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1192 |
self openForReading |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1193 |
] ifFalse:[mode == #writeonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1194 |
self openForWriting. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1195 |
] ifFalse:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1196 |
self openForReadWrite. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1197 |
]]. |
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1198 |
] ifFalse:[ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1199 |
super reset. |
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1200 |
]. |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1201 |
! |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1202 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1203 |
setToEnd |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1204 |
"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
|
1205 |
|
16298 | 1206 |
|error| |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1207 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1208 |
HFILE f; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1209 |
off_t ret; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1210 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1211 |
if (__INST(handle) != nil) { |
18651 | 1212 |
f = __FILEVal(__INST(handle)); |
1213 |
__INST(position) = nil; /* i.e. unknown */ |
|
1214 |
do { |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1215 |
#ifdef __win32__ |
18651 | 1216 |
__threadErrno = 0; |
1217 |
if (__INST(buffered) == true) { |
|
1218 |
ret = STX_C_CALL3( "fseek", fseek, f, 0L, SEEK_END); |
|
1219 |
} else { |
|
1220 |
__INST(readAhead) = nil; |
|
1221 |
ret = STX_C_CALL3( "lseek", lseek, fileno(f), 0L, SEEK_END); |
|
1222 |
} |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1223 |
#else |
18651 | 1224 |
if (__INST(buffered) == true) { |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1225 |
# ifdef _LFS_LARGE_FILE |
18651 | 1226 |
ret = fseeko(f, (off_t)0, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1227 |
# else |
18651 | 1228 |
ret = fseek(f, 0L, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1229 |
# endif |
18651 | 1230 |
} else { |
18421
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1231 |
if (sizeof(off_t) == 8) { |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1232 |
ret = lseek64(fileno(f), (off_t)0, SEEK_END); |
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1233 |
} else { |
18651 | 1234 |
ret = lseek(fileno(f), (off_t)0, SEEK_END); |
18421
5e7e40d367f2
Use lseek64() instead of lseek() when off_t is 64bit signed integer.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1235 |
} |
18651 | 1236 |
} |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1237 |
#endif |
18651 | 1238 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1239 |
if (ret >= 0) { |
|
1240 |
RETURN ( self ); |
|
1241 |
} |
|
1242 |
error = __mkSmallInteger(__threadErrno); |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1243 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1244 |
%}. |
16298 | 1245 |
error notNil ifTrue:[ |
18651 | 1246 |
lastErrorNumber := error. |
1247 |
self ioError:error. |
|
1248 |
^ self. |
|
16298 | 1249 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1250 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1251 |
^ self primitiveFailed |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1252 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1253 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1254 |
slowPosition:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1255 |
"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
|
1256 |
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
|
1257 |
This is required to reposition nonPositionable streams, such |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1258 |
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
|
1259 |
Caveat: |
18651 | 1260 |
This should really be done transparently by the stdio library." |
15620 | 1261 |
|
1262 |
|buffer amount pos0Based| |
|
1263 |
||
1264 |
self isReadable ifFalse:[ |
|
18651 | 1265 |
"/ sorry |
1266 |
^ self positionError |
|
15620 | 1267 |
]. |
1268 |
||
1269 |
buffer := ByteArray new:8*1024. |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1270 |
|
15620 | 1271 |
(position isNil "/ i.e. unknown |
1272 |
or:[newPos < (pos0Based := self position)]) ifTrue:[ |
|
18651 | 1273 |
self reset. |
1274 |
pos0Based := self position. |
|
15620 | 1275 |
]. |
1276 |
[pos0Based < newPos] whileTrue:[ |
|
18651 | 1277 |
amount := (buffer size) min:(newPos-pos0Based). |
1278 |
(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[ |
|
1279 |
^ self positionError |
|
1280 |
]. |
|
1281 |
pos0Based := self position. |
|
15620 | 1282 |
]. |
1283 |
"/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR. |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1284 |
! ! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1285 |
|
613 | 1286 |
!FileStream methodsFor:'printing & storing'! |
1287 |
||
1288 |
printOn:aStream |
|
4392 | 1289 |
"append a user printed representation of the receiver to aStream. |
1290 |
The format is suitable for a human - not meant to be read back." |
|
2903 | 1291 |
|
10354 | 1292 |
aStream nextPutAll:self className,'(for: '. |
1293 |
pathName printOn:aStream. |
|
1294 |
aStream nextPut:$). |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1295 |
|
10354 | 1296 |
" |
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
1297 |
'/' asFilename readStream printString |
10354 | 1298 |
" |
613 | 1299 |
! |
1300 |
||
1301 |
storeOn:aStream |
|
2903 | 1302 |
"append a representation of the receiver to aStream, |
1303 |
from which a copy can be reconstructed later." |
|
1304 |
||
613 | 1305 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1306 |
aStream nextPutAll:pathName storeString. |
15360 | 1307 |
(self position ~~ 0) ifTrue:[ |
15542 | 1308 |
aStream nextPutAll:'; position:'. |
1309 |
self position storeOn:aStream |
|
613 | 1310 |
]. |
1311 |
aStream nextPut:$) |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1312 |
|
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1313 |
"Modified: 30.7.1997 / 16:43:50 / cg" |
1 | 1314 |
! ! |
1315 |
||
18784 | 1316 |
!FileStream protectedMethodsFor:'private'! |
1 | 1317 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1318 |
closeFile |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1319 |
"low level close - may be redefined in subclasses. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1320 |
Don't send this message, send #close instead" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1321 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1322 |
super closeFile. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1323 |
removeOnClose == true ifTrue:[ |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1324 |
pathName asFilename remove. |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1325 |
] |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1326 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1327 |
"Created: / 13.8.1998 / 12:11:22 / cg" |
18784 | 1328 |
! ! |
1329 |
||
1330 |
!FileStream methodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1331 |
|
613 | 1332 |
createForReadWrite |
1333 |
"create/truncate the file for read/write. |
|
20921 | 1334 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1335 |
|
613 | 1336 |
mode := #readwrite. |
2921 | 1337 |
^ self openWithMode:CreateReadWriteMode |
1 | 1338 |
! |
1339 |
||
613 | 1340 |
createForWriting |
1341 |
"create/truncate the file for writeonly. |
|
20921 | 1342 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1343 |
|
613 | 1344 |
mode := #writeonly. |
1345 |
didWrite := true. |
|
8604 | 1346 |
^ self openWithMode:WriteMode |
1 | 1347 |
! |
1348 |
||
5439 | 1349 |
openFile:pathName withMode:openmode attributes:attributeSpec |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1350 |
"open the file; |
5439 | 1351 |
openmode is the string defining the way to open as defined by the stdio library |
1352 |
(i.e. the 2nd fopen argument). |
|
1353 |
||
1354 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1355 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1356 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1357 |
attributes (see fopen description). |
|
1358 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1359 |
This argument is ignored in UNIX & MSDOS systems. |
|
1360 |
||
1361 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1362 |
which is proprietrary to the operatingSystem." |
|
1363 |
||
16298 | 1364 |
|wasBlocked encodedPathName error| |
14578 | 1365 |
|
1366 |
encodedPathName := OperatingSystem encodePath:pathName. |
|
5439 | 1367 |
|
1368 |
%{ |
|
15462 | 1369 |
HFILE f = NULL; |
21604
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1370 |
enum { |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1371 |
PASS_INITIAL = 1, |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1372 |
PASS_RETRY_AFTER_SCAVENGE = 2, |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1373 |
PASS_RETRY_AFTER_GARBAGE_COLLECT = 3, |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1374 |
PASS_FINAL = PASS_RETRY_AFTER_GARBAGE_COLLECT |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1375 |
} pass = PASS_INITIAL; |
5439 | 1376 |
|
15462 | 1377 |
if (!__isNonNilObject(encodedPathName) |
20096 | 1378 |
|| !(__isStringLike(openmode) || __isArrayLike(openmode))) |
1379 |
goto badArgument; |
|
15462 | 1380 |
|
5439 | 1381 |
retry: |
1382 |
#ifdef __VMS__ |
|
15462 | 1383 |
if (__isStringLike(encodedPathName)) { |
20096 | 1384 |
do { |
1385 |
/* |
|
1386 |
* allow passing additional RMS arguments. |
|
1387 |
* stupid: DEC does not seem to offer an interface for passing a char **. |
|
1388 |
*/ |
|
1389 |
__threadErrno = 0; |
|
5439 | 1390 |
|
20096 | 1391 |
{ |
1392 |
if (__isArray(attributeSpec)) { |
|
1393 |
OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; |
|
1394 |
int numAttrib = 0; |
|
1395 |
int i; |
|
5439 | 1396 |
|
20096 | 1397 |
numAttrib = __arraySize(attributeSpec); |
1398 |
for (i=0; i<numAttrib;i++) { |
|
1399 |
if (! __isStringLike(ap[i])) { |
|
1400 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1401 |
goto getOutOfHere; |
|
1402 |
} |
|
1403 |
} |
|
1404 |
switch (numAttrib) { |
|
1405 |
case 0: |
|
1406 |
__BEGIN_INTERRUPTABLE__ |
|
1407 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode)); |
|
1408 |
__END_INTERRUPTABLE__ |
|
1409 |
break; |
|
1410 |
case 1: |
|
1411 |
__BEGIN_INTERRUPTABLE__ |
|
1412 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1413 |
__stringVal(ap[0])); |
|
1414 |
__END_INTERRUPTABLE__ |
|
1415 |
break; |
|
1416 |
case 2: |
|
1417 |
__BEGIN_INTERRUPTABLE__ |
|
1418 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1419 |
__stringVal(ap[0]), __stringVal(ap[1])); |
|
1420 |
__END_INTERRUPTABLE__ |
|
1421 |
break; |
|
1422 |
case 3: |
|
1423 |
__BEGIN_INTERRUPTABLE__ |
|
1424 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1425 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2])); |
|
1426 |
__END_INTERRUPTABLE__ |
|
1427 |
break; |
|
1428 |
case 4: |
|
1429 |
__BEGIN_INTERRUPTABLE__ |
|
1430 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1431 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1432 |
__stringVal(ap[3])); |
|
1433 |
__END_INTERRUPTABLE__ |
|
1434 |
break; |
|
1435 |
case 5: |
|
1436 |
__BEGIN_INTERRUPTABLE__ |
|
1437 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1438 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1439 |
__stringVal(ap[3]), __stringVal(ap[4])); |
|
1440 |
__END_INTERRUPTABLE__ |
|
1441 |
break; |
|
1442 |
case 6: |
|
1443 |
__BEGIN_INTERRUPTABLE__ |
|
1444 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1445 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1446 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5])); |
|
1447 |
__END_INTERRUPTABLE__ |
|
1448 |
break; |
|
1449 |
case 7: |
|
1450 |
__BEGIN_INTERRUPTABLE__ |
|
1451 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1452 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1453 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1454 |
__stringVal(ap[6])); |
|
1455 |
__END_INTERRUPTABLE__ |
|
1456 |
break; |
|
1457 |
case 8: |
|
1458 |
__BEGIN_INTERRUPTABLE__ |
|
1459 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1460 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1461 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1462 |
__stringVal(ap[6]), __stringVal(ap[7])); |
|
1463 |
__END_INTERRUPTABLE__ |
|
1464 |
break; |
|
1465 |
case 9: |
|
1466 |
__BEGIN_INTERRUPTABLE__ |
|
1467 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1468 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1469 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1470 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8])); |
|
1471 |
__END_INTERRUPTABLE__ |
|
1472 |
break; |
|
1473 |
case 10: |
|
1474 |
__BEGIN_INTERRUPTABLE__ |
|
1475 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1476 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1477 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1478 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8]), |
|
1479 |
__stringVal(ap[9])); |
|
1480 |
__END_INTERRUPTABLE__ |
|
1481 |
break; |
|
1482 |
default: |
|
1483 |
__threadErrno = E2BIG; /* too many args */ |
|
1484 |
goto getOutOfHere; |
|
1485 |
} |
|
1486 |
} else if (attributeSpec != nil) { |
|
1487 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1488 |
goto getOutOfHere; |
|
1489 |
} else { |
|
1490 |
/* |
|
1491 |
* create file as sequential streamLF by default. |
|
1492 |
*/ |
|
1493 |
__BEGIN_INTERRUPTABLE__ |
|
1494 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), "rfm=stmlf"); |
|
1495 |
__END_INTERRUPTABLE__ |
|
1496 |
} |
|
1497 |
} |
|
1498 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
11290 | 1499 |
} |
5439 | 1500 |
#else /* not VMS */ |
1501 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1502 |
# ifdef __win32__ |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1503 |
{ |
20096 | 1504 |
DWORD share = 0, access = 0, create = 0, attr = 0; |
1505 |
char * __openmode; |
|
1506 |
HANDLE handle; |
|
1507 |
SECURITY_ATTRIBUTES sa; |
|
11290 | 1508 |
|
20096 | 1509 |
if (__isStringLike(openmode)) { |
1510 |
share = FILE_SHARE_READ|FILE_SHARE_WRITE; |
|
1511 |
__openmode = __stringVal(openmode); |
|
1512 |
if (strcmp(__openmode, "rb") == 0) { |
|
1513 |
access = GENERIC_READ; |
|
1514 |
create = OPEN_EXISTING; |
|
1515 |
} else if (strcmp(__openmode, "rb+") == 0) { |
|
1516 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1517 |
create = OPEN_EXISTING; |
|
1518 |
} else if (strcmp(__openmode, "wb") == 0) { |
|
1519 |
access = GENERIC_WRITE; |
|
1520 |
create = CREATE_ALWAYS; |
|
1521 |
} else if (strcmp(__openmode, "wb+") == 0) { |
|
1522 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1523 |
create = CREATE_ALWAYS; |
|
1524 |
} else if (strcmp(__openmode, "ab") == 0) { |
|
1525 |
access = FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1526 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1527 |
create = OPEN_ALWAYS; |
|
1528 |
} else if (strcmp(__openmode, "ab+") == 0) { |
|
1529 |
access = GENERIC_READ |FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1530 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1531 |
create = OPEN_ALWAYS; |
|
1532 |
} else { |
|
1533 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1534 |
} |
|
1535 |
} else if (__isArrayLike(openmode)) { |
|
1536 |
OBJ *ap = __arrayVal(openmode); |
|
1537 |
int numAttrib = __arraySize(openmode); |
|
1538 |
int i; |
|
15462 | 1539 |
|
20096 | 1540 |
__openmode = "rb+"; |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1541 |
|
20096 | 1542 |
for (i=0; i<numAttrib; i++) { |
1543 |
OBJ attrSym = ap[i]; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1544 |
|
20096 | 1545 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1546 |
share |= FILE_SHARE_READ; |
|
1547 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1548 |
share |= FILE_SHARE_WRITE; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1549 |
|
20096 | 1550 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
1551 |
access |= GENERIC_READ; |
|
1552 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
|
1553 |
access |= GENERIC_WRITE; |
|
1554 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
|
1555 |
access |= GENERIC_READ|GENERIC_WRITE; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1556 |
|
20096 | 1557 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
1558 |
create |= CREATE_NEW; |
|
1559 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1560 |
create |= CREATE_ALWAYS; |
|
1561 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1562 |
create |= OPEN_EXISTING; |
|
1563 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1564 |
create |= OPEN_ALWAYS; |
|
1565 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1566 |
create |= TRUNCATE_EXISTING; |
|
5439 | 1567 |
|
20096 | 1568 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1569 |
attr |= FILE_ATTRIBUTE_HIDDEN; |
|
1570 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1571 |
attr |= FILE_ATTRIBUTE_READONLY; |
|
1572 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
1573 |
attr |= FILE_FLAG_WRITE_THROUGH; |
|
1574 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
|
1575 |
attr |= FILE_FLAG_SEQUENTIAL_SCAN; |
|
1576 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1577 |
attr |= FILE_FLAG_DELETE_ON_CLOSE; |
|
1578 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1579 |
__openmode = __stringVal(attrSym); |
|
1580 |
} else { |
|
1581 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1582 |
} |
|
1583 |
} |
|
1584 |
} |
|
1585 |
if (create == 0) { |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1586 |
// argumentError = @symbol(missingCreateMode); |
20096 | 1587 |
goto badArgument; |
1588 |
} |
|
1589 |
if (attr == 0) { |
|
1590 |
attr = FILE_ATTRIBUTE_NORMAL; |
|
1591 |
} |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1592 |
|
20096 | 1593 |
/* |
1594 |
* create security attributes - make handle inheritable by subprocesses |
|
1595 |
*/ |
|
1596 |
memset(&sa, 0, sizeof (sa)); |
|
1597 |
sa.nLength = sizeof( sa ); |
|
1598 |
// sa.bInheritHandle = TRUE; |
|
1599 |
sa.bInheritHandle = FALSE; |
|
12630
c6151f8f6854
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12467
diff
changeset
|
1600 |
|
20096 | 1601 |
if (__isStringLike(pathName)) { |
1602 |
char _aPathName[MAXPATHLEN]; |
|
11290 | 1603 |
|
20096 | 1604 |
strncpy(_aPathName, __stringVal(pathName), MAXPATHLEN-1); _aPathName[MAXPATHLEN-1] = '\0'; |
1605 |
do { |
|
1606 |
__threadErrno = 0; |
|
1607 |
handle = STX_API_NOINT_CALL7( "CreateFileA", CreateFileA, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1608 |
if (__threadErrno == EINTR) { |
|
1609 |
handle = INVALID_HANDLE_VALUE; |
|
1610 |
} |
|
1611 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1612 |
} else if (__isUnicode16String(pathName)) { |
|
1613 |
wchar_t _aPathName[MAXPATHLEN+1]; |
|
1614 |
int i, l; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1615 |
|
20096 | 1616 |
l = __unicode16StringSize(pathName); |
1617 |
if (l > MAXPATHLEN) l = MAXPATHLEN; |
|
1618 |
for (i=0; i<l; i++) { |
|
1619 |
_aPathName[i] = __unicode16StringVal(pathName)[i]; |
|
1620 |
} |
|
1621 |
_aPathName[i] = 0; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1622 |
|
20096 | 1623 |
do { |
1624 |
__threadErrno = 0; |
|
1625 |
handle = STX_API_NOINT_CALL7( "CreateFileW", CreateFileW, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1626 |
if (__threadErrno == EINTR) { |
|
1627 |
handle = INVALID_HANDLE_VALUE; |
|
1628 |
} |
|
1629 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1630 |
} |
|
1631 |
if (handle != INVALID_HANDLE_VALUE) { |
|
1632 |
int fd; |
|
1633 |
extern void __stxWrapApiEnterCritical(), __stxWrapApiLeaveCritical(); |
|
12692
6150a8d7c87e
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12687
diff
changeset
|
1634 |
|
20096 | 1635 |
__stxWrapApiEnterCritical(); |
1636 |
fd = _open_osfhandle((long)handle, O_BINARY); |
|
1637 |
if (fd < 0) { |
|
1638 |
if (__threadErrno == 0) { |
|
1639 |
// no more file descriptors |
|
1640 |
__threadErrno = EMFILE; |
|
1641 |
} |
|
1642 |
CloseHandle(handle); |
|
1643 |
} else { |
|
1644 |
f = fdopen(fd, __openmode); |
|
1645 |
} |
|
1646 |
__stxWrapApiLeaveCritical(); |
|
1647 |
} else { |
|
1648 |
__threadErrno = __WIN32_ERR(GetLastError()); |
|
1649 |
} |
|
11290 | 1650 |
} |
5439 | 1651 |
# else /* not WIN32 */ |
1652 |
||
14578 | 1653 |
if (__isStringLike(encodedPathName)) { |
20096 | 1654 |
int accessMode = 0666; // default access mode of fopen(), relies on umask() |
1655 |
int flags = 0; |
|
1656 |
int fd; |
|
1657 |
char * __openmode; |
|
15462 | 1658 |
|
20096 | 1659 |
if (__isStringLike(openmode)) { |
1660 |
__openmode = __stringVal(openmode); |
|
1661 |
if (strcmp(__openmode, "r") == 0) { |
|
1662 |
flags = O_RDONLY; |
|
1663 |
} else if (strcmp(__openmode, "r+") == 0) { |
|
1664 |
flags = O_RDWR; |
|
1665 |
} else if (strcmp(__openmode, "w") == 0) { |
|
1666 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
|
1667 |
} else if (strcmp(__openmode, "w+") == 0) { |
|
1668 |
flags = O_RDWR | O_CREAT | O_TRUNC; |
|
1669 |
} else if (strcmp(__openmode, "a") == 0) { |
|
1670 |
flags = O_WRONLY | O_CREAT | O_APPEND; |
|
1671 |
} else if (strcmp(__openmode, "a+") == 0) { |
|
1672 |
flags = O_RDWR | O_CREAT| O_APPEND; |
|
1673 |
} else { |
|
1674 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1675 |
} |
|
1676 |
} else if (__isArrayLike(openmode)) { |
|
1677 |
OBJ *ap = __arrayVal(openmode); |
|
1678 |
int numAttrib = __arraySize(openmode); |
|
1679 |
int i; |
|
15462 | 1680 |
|
20096 | 1681 |
__openmode = "r+"; |
15462 | 1682 |
|
20096 | 1683 |
for (i=0; i<numAttrib; i++) { |
1684 |
OBJ attrSym = ap[i]; |
|
15462 | 1685 |
|
20096 | 1686 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1687 |
// ignore |
|
1688 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1689 |
// ignore |
|
1690 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
|
1691 |
flags |= O_RDONLY; |
|
21004 | 1692 |
__openmode = "r"; |
20096 | 1693 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
1694 |
flags |= O_WRONLY; |
|
21004 | 1695 |
__openmode = "w"; |
20096 | 1696 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
1697 |
flags |= O_RDWR; |
|
21004 | 1698 |
__openmode = "r+"; |
20096 | 1699 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
1700 |
flags |= O_CREAT|O_EXCL; |
|
1701 |
accessMode = 0600; // simulate mkstemp() |
|
1702 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1703 |
flags |= O_CREAT|O_TRUNC; |
|
1704 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1705 |
// nothing to be set |
|
1706 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1707 |
flags |= O_CREAT; |
|
1708 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1709 |
flags |= O_TRUNC; |
|
15462 | 1710 |
|
20096 | 1711 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1712 |
// ignore |
|
1713 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1714 |
accessMode &= 0444; |
|
1715 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
15462 | 1716 |
#ifdef O_DIRECT |
20096 | 1717 |
flags |= O_DIRECT; |
15462 | 1718 |
#endif |
20096 | 1719 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
1720 |
// ignore |
|
1721 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1722 |
// ignore; |
|
1723 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1724 |
__openmode = __stringVal(attrSym); |
|
1725 |
} else { |
|
1726 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1727 |
} |
|
1728 |
} |
|
1729 |
} |
|
1730 |
do { |
|
1731 |
__BEGIN_INTERRUPTABLE__ |
|
1732 |
fd = open((char *) __stringVal(encodedPathName), flags, accessMode); |
|
1733 |
__END_INTERRUPTABLE__ |
|
1734 |
} while ((fd < 0) && (__threadErrno == EINTR)); |
|
15462 | 1735 |
|
20096 | 1736 |
if (fd >= 0) { |
1737 |
__threadErrno = 0; |
|
1738 |
f = fdopen(fd, __openmode); |
|
1739 |
if (f == NULL) { |
|
1740 |
close(fd); // fdopen failed, close before retry. |
|
1741 |
} |
|
1742 |
} |
|
11290 | 1743 |
} |
15462 | 1744 |
|
5439 | 1745 |
# endif /* not WIN32 */ |
1746 |
#endif /* not VMS */ |
|
1747 |
||
15462 | 1748 |
|
11290 | 1749 |
if (f == NULL) { |
20096 | 1750 |
/* |
1751 |
* If no filedescriptors available, try to finalize |
|
1752 |
* possibly collected fd's and try again. |
|
1753 |
*/ |
|
21604
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1754 |
if ((__threadErrno == ENFILE || __threadErrno == EMFILE) && pass < PASS_FINAL) { |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1755 |
switch (pass) { |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1756 |
case PASS_INITIAL: |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1757 |
{ |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1758 |
pass = PASS_RETRY_AFTER_SCAVENGE; |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1759 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1760 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1761 |
} |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1762 |
goto retry; |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1763 |
case PASS_RETRY_AFTER_SCAVENGE: |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1764 |
{ |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1765 |
pass = PASS_RETRY_AFTER_GARBAGE_COLLECT; |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1766 |
__SSEND0(@global(ObjectMemory), @symbol(garbageCollect), 0); |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1767 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1768 |
} |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1769 |
goto retry; |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1770 |
case PASS_RETRY_AFTER_GARBAGE_COLLECT: |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1771 |
/* Should never be reached */ |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1772 |
goto getOutOfHere; |
10256dd38558
Improved handling of `EMFILE` / `ENFILE` when opening files / directories
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21285
diff
changeset
|
1773 |
} |
20096 | 1774 |
} |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1775 |
badArgument: |
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1776 |
getOutOfHere: |
20096 | 1777 |
error = __mkSmallInteger(__threadErrno); |
1778 |
__INST(position) = nil; |
|
11290 | 1779 |
} else { |
5439 | 1780 |
#ifdef __VMS__ |
20096 | 1781 |
/* |
1782 |
* check to see if this is positionable ... |
|
1783 |
*/ |
|
1784 |
__INST(canPosition) = false; |
|
5439 | 1785 |
# ifndef _POSIX_C_SOURCE |
20096 | 1786 |
{ |
1787 |
struct stat statBuffer; |
|
5439 | 1788 |
|
20096 | 1789 |
if (fstat(fileno(f), &statBuffer) >= 0) { |
1790 |
switch (statBuffer.st_fab_rfm) { |
|
1791 |
case FAB$C_UDF: /* undefined (also stream binary) */ |
|
1792 |
case FAB$C_VAR: /* variable length records */ |
|
1793 |
case FAB$C_VFC: /* variable fixed control */ |
|
1794 |
case FAB$C_STM: /* RMS-11 stream (valid only for sequen) */ |
|
1795 |
default: |
|
1796 |
__INST(canPosition) = false; |
|
1797 |
break; |
|
5439 | 1798 |
|
20096 | 1799 |
case FAB$C_FIX: /* fixed length records */ |
1800 |
case FAB$C_STMLF: /* LF stream (valid only for sequential) */ |
|
1801 |
case FAB$C_STMCR: /* CR stream (valid only for sequential) */ |
|
1802 |
__INST(canPosition) = true; |
|
1803 |
break; |
|
1804 |
} |
|
1805 |
} |
|
1806 |
} |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1807 |
# endif |
5439 | 1808 |
#else /* not VMS */ |
20096 | 1809 |
__INST(canPosition) = true; |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1810 |
#endif /* not VMS */ |
5439 | 1811 |
|
20096 | 1812 |
if (@global(FileOpenTrace) == true) { |
1813 |
console_fprintf(stderr, "fopen %s [FileStream] -> %"_lx_"\n", __stringVal(pathName), (INT)f); |
|
1814 |
} |
|
1815 |
if (f != NULL) { |
|
1816 |
OBJ fp; |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1817 |
|
20096 | 1818 |
wasBlocked = __BLOCKINTERRUPTS(); |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1819 |
#if 0 |
20096 | 1820 |
// The original code was: |
1821 |
__INST(handle) = fp = __MKEXTERNALADDRESS(f); __STORE(self, fp); |
|
1822 |
// but for that, gcc generates wrong code, which loads self (volatile) into |
|
1823 |
// a register (bp), then calls __MKEXTERNALADDRESS, then stores indirect bp. |
|
1824 |
// That is wrong if a scavenge occurs in __MKEXTERNALADDRESS, as bp is now still pointing to the old |
|
1825 |
// object. |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1826 |
#else |
20096 | 1827 |
fp = __MKEXTERNALADDRESS(f); |
1828 |
__INST(handle) = fp; |
|
1829 |
__STORE(self, fp); |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1830 |
#endif |
20096 | 1831 |
} |
5439 | 1832 |
} |
1833 |
%}. |
|
16298 | 1834 |
error notNil ifTrue:[ |
20096 | 1835 |
lastErrorNumber := error. |
1836 |
^ self openError:error. |
|
8034
716b32d832bf
raise condition interrupt: interrupt between creation and lobby
ca
parents:
7970
diff
changeset
|
1837 |
]. |
16298 | 1838 |
handle isNil ifTrue:[ |
20096 | 1839 |
^ self openError:0. |
16298 | 1840 |
]. |
1841 |
||
20096 | 1842 |
buffered := true. "default is buffered" |
16298 | 1843 |
position := 0. |
1844 |
handleType := #filePointer. |
|
18781 | 1845 |
self registerForFinalization. |
16298 | 1846 |
wasBlocked == false ifTrue:[OperatingSystem unblockInterrupts]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1847 |
^ handle |
5439 | 1848 |
! |
1849 |
||
613 | 1850 |
openForAppending |
1851 |
"open the file for writeonly appending to the end. |
|
20921 | 1852 |
If the file does not exist, raise OpenError; |
613 | 1853 |
otherwise return the receiver." |
1854 |
||
1855 |
mode := #writeonly. |
|
1856 |
didWrite := true. |
|
2921 | 1857 |
^ self openWithMode:AppendMode |
613 | 1858 |
! |
1859 |
||
1860 |
openForReadWrite |
|
1861 |
"open the file for read/write. |
|
20921 | 1862 |
If the file does not exist, raise OpenError; |
613 | 1863 |
otherwise return the receiver." |
1864 |
||
1865 |
mode := #readwrite. |
|
2921 | 1866 |
^ self openWithMode:ReadWriteMode |
613 | 1867 |
! |
1868 |
||
1869 |
openForReading |
|
1870 |
"open the file for readonly. |
|
20921 | 1871 |
If the file does not exist, raise OpenError; |
613 | 1872 |
otherwise return the receiver." |
1873 |
||
1874 |
mode := #readonly. |
|
1875 |
didWrite := false. |
|
2921 | 1876 |
^ self openWithMode:ReadMode |
613 | 1877 |
! |
1878 |
||
1879 |
openForWriting |
|
8604 | 1880 |
"open the file writeonly. The contents of the file is preserved. |
20921 | 1881 |
If the file does not exist, raise OpenError; |
613 | 1882 |
otherwise return the receiver." |
1883 |
||
1884 |
mode := #writeonly. |
|
1885 |
didWrite := true. |
|
8604 | 1886 |
"we must not truncate the file!! So do not use WriteMode" |
1887 |
^ self openWithMode:ReadWriteMode |
|
613 | 1888 |
! |
1889 |
||
1 | 1890 |
openWithMode:openmode |
2921 | 1891 |
"open the file; |
1892 |
openmode is the string defining the way to open as defined by the stdio library |
|
1893 |
(i.e. the 2nd fopen argument). |
|
1894 |
||
1895 |
This is a private entry, but maybe useful to open a file in a special mode, |
|
1896 |
which is proprietrary to the operatingSystem." |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1897 |
|
2921 | 1898 |
^ self openWithMode:openmode attributes:nil |
1899 |
! |
|
1900 |
||
1901 |
openWithMode:openmode attributes:attributeSpec |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1902 |
"open the file; |
2921 | 1903 |
openmode is the string defining the way to open as defined by the stdio library |
1904 |
(i.e. the 2nd fopen argument). |
|
1905 |
||
1906 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1907 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1908 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1909 |
attributes (see fopen description). |
|
1910 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1911 |
This argument is ignored in UNIX & MSDOS systems. |
|
1912 |
||
1913 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1914 |
which is proprietrary to the operatingSystem." |
|
1 | 1915 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1916 |
handle notNil ifTrue:[^ self errorAlreadyOpen]. |
2921 | 1917 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1918 |
handle := self openFile:pathName withMode:openmode attributes:attributeSpec. |
17144 | 1919 |
handle isNil ifTrue:[ |
20096 | 1920 |
"this happens, if proceeding from an OpenError raised in #openFile..." |
1921 |
^ nil. |
|
8604 | 1922 |
]. |
1 | 1923 |
! |
1924 |
||
613 | 1925 |
pathName:filename |
1926 |
"set the pathname" |
|
1 | 1927 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1928 |
pathName := filename asString |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1929 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1930 |
"Modified: / 28.10.1997 / 14:29:01 / cg" |
1 | 1931 |
! |
1932 |
||
613 | 1933 |
pathName:filename in:aDirectory |
1934 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 1935 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1936 |
pathName := aDirectory asFilename constructString:filename |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1937 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1938 |
"Modified: / 28.10.1997 / 14:28:54 / cg" |
1 | 1939 |
! |
1940 |
||
1941 |
reOpen |
|
8604 | 1942 |
"USERS WILL NEVER INVOKE THIS METHOD |
1943 |
sent after snapin to reopen streams." |
|
1 | 1944 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1945 |
handle notNil ifTrue:[ |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1946 |
"it was open, when snapped-out" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1947 |
handle := nil. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1948 |
self unregisterForFinalization. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1949 |
pathName isNil ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1950 |
^ self. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1951 |
]. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1952 |
[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1953 |
|oldPos| |
173 | 1954 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1955 |
"should take care of appending files and open them for |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1956 |
append / position them to the end" |
8604 | 1957 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1958 |
oldPos := position. |
8604 | 1959 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1960 |
mode == #readonly ifTrue: [ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1961 |
self openForReading |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1962 |
] ifFalse:[mode == #writeonly ifTrue: [ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1963 |
self openForWriting. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1964 |
] ifFalse:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1965 |
self openForReadWrite. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1966 |
]]. |
8604 | 1967 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1968 |
oldPos notNil ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1969 |
self position:oldPos. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1970 |
] |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1971 |
] on:OpenError do:[:ex| |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1972 |
"this happens, if after a restart, |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1973 |
the file is no longer present or accessible ..." |
13 | 1974 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1975 |
(self class name , ' [warning]: could not reopen file: ', pathName) errorPrintCR. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1976 |
]. |
1 | 1977 |
] |
1088 | 1978 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
1979 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
2921 | 1980 |
! |
1981 |
||
1982 |
setMode:aModeSymbol |
|
1983 |
mode := aModeSymbol |
|
15479 | 1984 |
! |
1985 |
||
1986 |
setPathName:pathNameString removeOnClose:aBoolean |
|
1987 |
pathName := pathNameString. |
|
1988 |
removeOnClose := aBoolean. |
|
31 | 1989 |
! ! |
1990 |
||
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1991 |
!FileStream methodsFor:'private fileIn'! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1992 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1993 |
fileInNotifying:notifiedLoader passChunk:passChunk |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1994 |
"central method to file in from the receiver, i.e. read chunks and evaluate them - |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1995 |
return the value of the last chunk. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1996 |
Someone (which is usually some codeView) is notified of errors." |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1997 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1998 |
^ self fileInNotifying:notifiedLoader passChunk:passChunk inDirectory:(self pathName asFilename directory). |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
1999 |
! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2000 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2001 |
fileInNotifying:notifiedLoader passChunk:passChunk inDirectory:aDirectory |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2002 |
"central method to file in from the receiver, i.e. read chunks and evaluate them - |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2003 |
return the value of the last chunk. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2004 |
Someone (which is usually some codeView) is notified of errors. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2005 |
Add aDirectory to the search path for classes, while performing the fileIn." |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2006 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2007 |
|oldPath val thisDirectory thisDirectoryPathName| |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2008 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2009 |
thisDirectory := aDirectory asFilename. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2010 |
thisDirectoryPathName := thisDirectory pathName. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2011 |
oldPath := Smalltalk systemPath. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2012 |
|
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2013 |
[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2014 |
Smalltalk systemPath:(oldPath copy addFirst:thisDirectoryPathName; yourself). |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2015 |
self class currentFileInDirectoryQuerySignal answer:thisDirectory do:[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2016 |
self class currentSourceContainerQuery answer:self do:[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2017 |
val := self basicFileInNotifying:notifiedLoader passChunk:passChunk. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2018 |
]. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2019 |
] |
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2020 |
] ensure:[ |
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2021 |
"take care, someone could have changed SystemPath during fileIn!!" |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2022 |
(Smalltalk systemPath copyFrom:2) = oldPath ifTrue:[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2023 |
Smalltalk systemPath:oldPath. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2024 |
] ifFalse:[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2025 |
(oldPath includes:thisDirectoryPathName) ifFalse:[ |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2026 |
Smalltalk systemPath remove:thisDirectoryPathName ifAbsent:[]. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2027 |
Smalltalk flushPathCaches. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2028 |
]. |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
2029 |
]. |
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2030 |
]. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2031 |
^ val |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2032 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2033 |
"Modified: / 23-10-2006 / 16:35:10 / cg" |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2034 |
! ! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2035 |
|
2921 | 2036 |
!FileStream methodsFor:'queries'! |
613 | 2037 |
|
17603 | 2038 |
collectionSize |
2039 |
"common stream protocol: return the size of the stream; |
|
20412 | 2040 |
that's the number of bytes of the file." |
17603 | 2041 |
|
2042 |
^ self fileSize. |
|
2043 |
! |
|
2044 |
||
2903 | 2045 |
fileSize |
613 | 2046 |
"return the size in bytes of the file" |
2047 |
||
16298 | 2048 |
|error| |
3971 | 2049 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2050 |
HFILE f; |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2051 |
|
613 | 2052 |
#ifdef transputer |
7874
5e0319ca5e60
stats filesize can be up-to 32bit now.
Claus Gittinger <cg@exept.de>
parents:
7872
diff
changeset
|
2053 |
unsigned int size; |
613 | 2054 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2055 |
if (__INST(handle) != nil) { |
18651 | 2056 |
f = __FILEVal(__INST(handle)); |
2057 |
if ((size = filesize(fileno(f))) >= 0) { |
|
2058 |
RETURN ( __MKUINT(size) ); |
|
2059 |
} |
|
613 | 2060 |
} |
2061 |
#else |
|
2062 |
struct stat buf; |
|
2063 |
int ret; |
|
2064 |
int fd; |
|
2065 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2066 |
if (__INST(handle) != nil) { |
18651 | 2067 |
f = __FILEVal(__INST(handle)); |
2068 |
fd = fileno(f); |
|
2069 |
do { |
|
2070 |
ret = fstat(fd, &buf); |
|
2071 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
2072 |
if (ret >= 0) { |
|
2073 |
OBJ rslt; |
|
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
2074 |
|
18651 | 2075 |
if (sizeof(buf.st_size) == 8) { |
2076 |
rslt = __MKINT64(&buf.st_size); |
|
2077 |
} else { |
|
2078 |
rslt = __MKINT(buf.st_size); |
|
2079 |
} |
|
2080 |
RETURN(rslt); |
|
2081 |
} |
|
2082 |
error = __mkSmallInteger(__threadErrno); |
|
613 | 2083 |
} |
2084 |
#endif |
|
2085 |
%}. |
|
2086 |
||
2087 |
"could add a fall-back here: |
|
2088 |
||
18651 | 2089 |
oldPosition := self position. |
2090 |
self setToEnd. |
|
2091 |
sz := self position. |
|
2092 |
self position:oldPosition. |
|
2093 |
^ sz |
|
613 | 2094 |
" |
16298 | 2095 |
error notNil ifTrue:[ |
18651 | 2096 |
lastErrorNumber := error. |
2097 |
self ioError:error. |
|
2098 |
^ self. |
|
16298 | 2099 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2100 |
handle isNil ifTrue:[^ self errorNotOpen]. |
613 | 2101 |
^ self primitiveFailed |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2102 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2103 |
|
13051 | 2104 |
isDirectory |
2105 |
^ OperatingSystem isDirectory:pathName |
|
2106 |
! |
|
2107 |
||
14093 | 2108 |
isEmpty |
2109 |
"common stream protocol: are there no bytes in the file?" |
|
2110 |
||
2111 |
^ self fileSize == 0. |
|
2112 |
! |
|
2113 |
||
17603 | 2114 |
remainingSize |
2115 |
"return the number of remaining elements in the streamed collection." |
|
2116 |
||
2117 |
^ self fileSize - self position. |
|
2118 |
! |
|
2119 |
||
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2120 |
size |
11454 | 2121 |
"common stream protocol: return the size of the stream; |
20412 | 2122 |
that's the number of bytes of the file." |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2123 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2124 |
^ self fileSize. |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2125 |
|
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2126 |
"Modified: 4.10.1997 / 18:01:09 / cg" |
1 | 2127 |
! ! |
2128 |
||
5493 | 2129 |
!FileStream methodsFor:'rel5 protocol'! |
2130 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2131 |
positionFile:handle position:newPos |
5493 | 2132 |
"for migration to rel5 only" |
2133 |
||
2134 |
self primitiveFailed |
|
2135 |
! ! |
|
2136 |
||
2 | 2137 |
!FileStream methodsFor:'testing'! |
2138 |
||
2139 |
isFileStream |
|
2140 |
"return true, if the receiver is some kind of fileStream. |
|
2141 |
redefined from Object" |
|
2142 |
||
2143 |
^ true |
|
2144 |
! ! |
|
2145 |
||
1088 | 2146 |
!FileStream class methodsFor:'documentation'! |
2147 |
||
2148 |
version |
|
18651 | 2149 |
^ '$Header$' |
12207
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2150 |
! |
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2151 |
|
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2152 |
version_CVS |
18651 | 2153 |
^ '$Header$' |
1088 | 2154 |
! ! |
6798 | 2155 |
|
15360 | 2156 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2157 |
FileStream initialize! |