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