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