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