author | Stefan Vogel <sv@exept.de> |
Wed, 08 May 2019 15:04:54 +0200 | |
changeset 24121 | d0148c842873 |
parent 24102 | e14d73f12120 |
child 24149 | e5e20aba8e1f |
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 |
[ |
|
24102 | 452 |
prevRandom := random. |
453 |
prevNameString := nameString. |
|
15462 | 454 |
|
24102 | 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 |
|
24102 | 461 |
nameString := template bindWith:(OperatingSystem getProcessId) with:random. |
15462 | 462 |
|
24102 | 463 |
aDirectoryOrNil isNil ifTrue:[ |
464 |
newTempFilename := nameString. |
|
465 |
] ifFalse:[ |
|
466 |
newTempFilename := aDirectoryOrNil asFilename constructString:nameString. |
|
467 |
]. |
|
15462 | 468 |
|
24102 | 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:[ |
24102 | 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 |
||
24102 | 489 |
FileStream newTemporaryIn:'/tmp' asFilename nameTemplate:'foo%1_%2' |
15462 | 490 |
|
491 |
This must fail on the second try: |
|
24102 | 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. |
|
24102 | 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:[ |
|
24102 | 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." |
|
23898 | 925 |
|
24102 | 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 |
]. |
|
23898 | 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))) { |
24102 | 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 |
|
24102 | 1005 |
f = __FILEVal(fp); |
22151 | 1006 |
|
24102 | 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 |
|
24102 | 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:[ |
24102 | 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) { |
24102 | 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__ |
24102 | 1063 |
__threadErrno = 0; |
1064 |
if (__INST(buffered) == true) { |
|
6333 | 1065 |
# if 0 |
24102 | 1066 |
currentPosition = STX_C_CALL1( "ftell", ftell, f); |
6333 | 1067 |
# else |
24102 | 1068 |
currentPosition = ftell(f); |
1069 |
__threadErrno = errno; |
|
6333 | 1070 |
# endif |
24102 | 1071 |
} else { |
1072 |
OBJ rA = __INST(readAhead); |
|
1073 |
off_t offs = 0; |
|
6333 | 1074 |
|
24102 | 1075 |
if (rA != nil) { |
1076 |
__INST(readAhead) = nil; |
|
1077 |
offs = -1; |
|
1078 |
} |
|
6333 | 1079 |
# if 0 |
24102 | 1080 |
currentPosition = STX_C_CALL3( "lseek", lseek, fileno(f), offs, SEEK_CUR); |
6333 | 1081 |
# else |
24102 | 1082 |
currentPosition = lseek(fileno(f), offs, SEEK_CUR); |
1083 |
__threadErrno = errno; |
|
6333 | 1084 |
# endif |
24102 | 1085 |
} |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1086 |
#else /* !__win32__ */ |
24102 | 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 |
24102 | 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 |
24102 | 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 */ |
24102 | 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 */ |
24102 | 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 |
|
24102 | 1099 |
if (currentPosition >= 0) { |
1100 |
OBJ rslt; |
|
7872 | 1101 |
|
24102 | 1102 |
if (sizeof(currentPosition) == 8) { |
1103 |
rslt = __MKINT64 (¤tPosition); |
|
1104 |
} else { |
|
1105 |
rslt = __MKINT(currentPosition); |
|
1106 |
} |
|
1107 |
RETURN ( rslt ); |
|
1108 |
} |
|
23539 | 1109 |
# ifdef __win32__ |
24102 | 1110 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1111 |
# endif |
24102 | 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:[ |
24102 | 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))) { |
24102 | 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 |
24102 | 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 |
24102 | 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 |
|
24102 | 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 |
|
24102 | 1170 |
f = __FILEVal(fp); |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1171 |
|
24102 | 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) |
24102 | 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 |
24102 | 1182 |
errno = 0; |
1183 |
if (__INST(buffered) == true) { |
|
1184 |
ret = FSEEK(f, nP, SEEK_SET); |
|
1185 |
} else { |
|
1186 |
ret = lseek(fileno(f), nP, SEEK_SET); |
|
1187 |
} |
|
1188 |
__threadErrno = errno; |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1189 |
#endif |
24102 | 1190 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1191 |
if (ret >= 0) { |
|
1192 |
__INST(position) = newPos; __STORE(self, newPos); |
|
1193 |
/* |
|
1194 |
* just to make certain ... |
|
1195 |
*/ |
|
1196 |
__INST(hitEOF) = false; |
|
1197 |
RETURN ( self ); |
|
1198 |
} |
|
23539 | 1199 |
# ifdef __win32__ |
24102 | 1200 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1201 |
# endif |
24102 | 1202 |
error = __mkSmallInteger(__threadErrno); |
1203 |
} |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1204 |
} |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1205 |
getOutOfHere: ; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1206 |
#undef FSEEK |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1207 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1208 |
canPosition == false ifTrue:[ |
24102 | 1209 |
"/ position by rewinding & re-reading everything up-to |
1210 |
"/ that point. |
|
1211 |
^ self slowPosition:newPos |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1212 |
]. |
16298 | 1213 |
error notNil ifTrue:[ |
24102 | 1214 |
lastErrorNumber := error. |
1215 |
(OperatingSystem errorSymbolForNumber:error) == #EINVAL ifTrue:[ |
|
1216 |
"/ invalid position |
|
1217 |
^ self positionError. |
|
1218 |
]. |
|
1219 |
"/ assume I/O error |
|
1220 |
^ self ioError:error. |
|
2963 | 1221 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1222 |
handle isNil ifTrue:[^ self errorNotOpen]. |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1223 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1224 |
rslt := self positionFile:handle position:newPos. |
5493 | 1225 |
rslt >= 0 ifTrue:[ |
24102 | 1226 |
position := newPos. |
5493 | 1227 |
] ifFalse:[ |
24102 | 1228 |
hitEOF := true. |
5493 | 1229 |
] |
23539 | 1230 |
|
1231 |
"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
|
1232 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1233 |
|
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1234 |
reset |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1235 |
"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
|
1236 |
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
|
1237 |
with other Smalltalk dialects" |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1238 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1239 |
handle isNil ifTrue:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1240 |
"reopen the file" |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1241 |
mode == #readonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1242 |
self openForReading |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1243 |
] ifFalse:[mode == #writeonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1244 |
self openForWriting. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1245 |
] ifFalse:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1246 |
self openForReadWrite. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1247 |
]]. |
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1248 |
] ifFalse:[ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1249 |
super reset. |
11437
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 |
! |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1252 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1253 |
setToEnd |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1254 |
"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
|
1255 |
|
16298 | 1256 |
|error| |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1257 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1258 |
HFILE f; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1259 |
off_t ret; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1260 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1261 |
if (__INST(handle) != nil) { |
24102 | 1262 |
f = __FILEVal(__INST(handle)); |
1263 |
__INST(position) = nil; /* i.e. unknown */ |
|
1264 |
do { |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1265 |
#ifdef __win32__ |
24102 | 1266 |
__threadErrno = 0; |
1267 |
if (__INST(buffered) == true) { |
|
1268 |
ret = STX_C_CALL3( "fseek", fseek, f, 0L, SEEK_END); |
|
1269 |
} else { |
|
1270 |
__INST(readAhead) = nil; |
|
1271 |
ret = STX_C_CALL3( "lseek", lseek, fileno(f), 0L, SEEK_END); |
|
1272 |
} |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1273 |
#else |
24102 | 1274 |
if (__INST(buffered) == true) { |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1275 |
# ifdef _LFS_LARGE_FILE |
24102 | 1276 |
ret = fseeko(f, (off_t)0, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1277 |
# else |
24102 | 1278 |
ret = fseek(f, 0L, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1279 |
# endif |
24102 | 1280 |
} else { |
1281 |
ret = lseek(fileno(f), (off_t)0, SEEK_END); |
|
1282 |
} |
|
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1283 |
#endif |
24102 | 1284 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1285 |
if (ret >= 0) { |
|
1286 |
RETURN ( self ); |
|
1287 |
} |
|
23539 | 1288 |
# ifdef __win32__ |
24102 | 1289 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1290 |
# endif |
24102 | 1291 |
error = __mkSmallInteger(__threadErrno); |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1292 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1293 |
%}. |
16298 | 1294 |
error notNil ifTrue:[ |
24102 | 1295 |
lastErrorNumber := error. |
1296 |
self ioError:error. |
|
1297 |
^ self. |
|
16298 | 1298 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1299 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1300 |
^ self primitiveFailed |
23539 | 1301 |
|
1302 |
"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
|
1303 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1304 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1305 |
slowPosition:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1306 |
"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
|
1307 |
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
|
1308 |
This is required to reposition nonPositionable streams, such |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1309 |
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
|
1310 |
Caveat: |
18651 | 1311 |
This should really be done transparently by the stdio library." |
15620 | 1312 |
|
1313 |
|buffer amount pos0Based| |
|
1314 |
||
1315 |
self isReadable ifFalse:[ |
|
18651 | 1316 |
"/ sorry |
1317 |
^ self positionError |
|
15620 | 1318 |
]. |
1319 |
||
1320 |
buffer := ByteArray new:8*1024. |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1321 |
|
15620 | 1322 |
(position isNil "/ i.e. unknown |
1323 |
or:[newPos < (pos0Based := self position)]) ifTrue:[ |
|
18651 | 1324 |
self reset. |
1325 |
pos0Based := self position. |
|
15620 | 1326 |
]. |
1327 |
[pos0Based < newPos] whileTrue:[ |
|
18651 | 1328 |
amount := (buffer size) min:(newPos-pos0Based). |
1329 |
(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[ |
|
1330 |
^ self positionError |
|
1331 |
]. |
|
1332 |
pos0Based := self position. |
|
15620 | 1333 |
]. |
1334 |
"/ ('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
|
1335 |
! ! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1336 |
|
613 | 1337 |
!FileStream methodsFor:'printing & storing'! |
1338 |
||
1339 |
printOn:aStream |
|
4392 | 1340 |
"append a user printed representation of the receiver to aStream. |
1341 |
The format is suitable for a human - not meant to be read back." |
|
2903 | 1342 |
|
10354 | 1343 |
aStream nextPutAll:self className,'(for: '. |
1344 |
pathName printOn:aStream. |
|
1345 |
aStream nextPut:$). |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1346 |
|
10354 | 1347 |
" |
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
1348 |
'/' asFilename readStream printString |
10354 | 1349 |
" |
613 | 1350 |
! |
1351 |
||
1352 |
storeOn:aStream |
|
2903 | 1353 |
"append a representation of the receiver to aStream, |
1354 |
from which a copy can be reconstructed later." |
|
1355 |
||
613 | 1356 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1357 |
aStream nextPutAll:pathName storeString. |
15360 | 1358 |
(self position ~~ 0) ifTrue:[ |
15542 | 1359 |
aStream nextPutAll:'; position:'. |
1360 |
self position storeOn:aStream |
|
613 | 1361 |
]. |
1362 |
aStream nextPut:$) |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1363 |
|
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1364 |
"Modified: 30.7.1997 / 16:43:50 / cg" |
1 | 1365 |
! ! |
1366 |
||
18784 | 1367 |
!FileStream protectedMethodsFor:'private'! |
1 | 1368 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1369 |
closeFile |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1370 |
"low level close - may be redefined in subclasses. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1371 |
Don't send this message, send #close instead" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1372 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1373 |
super closeFile. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1374 |
removeOnClose == true ifTrue:[ |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1375 |
pathName asFilename remove. |
8471
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 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1378 |
"Created: / 13.8.1998 / 12:11:22 / cg" |
18784 | 1379 |
! ! |
1380 |
||
1381 |
!FileStream methodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1382 |
|
613 | 1383 |
createForReadWrite |
1384 |
"create/truncate the file for read/write. |
|
20921 | 1385 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1386 |
|
613 | 1387 |
mode := #readwrite. |
2921 | 1388 |
^ self openWithMode:CreateReadWriteMode |
1 | 1389 |
! |
1390 |
||
613 | 1391 |
createForWriting |
1392 |
"create/truncate the file for writeonly. |
|
20921 | 1393 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1394 |
|
613 | 1395 |
mode := #writeonly. |
1396 |
didWrite := true. |
|
8604 | 1397 |
^ self openWithMode:WriteMode |
1 | 1398 |
! |
1399 |
||
5439 | 1400 |
openFile:pathName withMode:openmode attributes:attributeSpec |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1401 |
"open the file; |
5439 | 1402 |
openmode is the string defining the way to open as defined by the stdio library |
1403 |
(i.e. the 2nd fopen argument). |
|
1404 |
||
1405 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1406 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1407 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1408 |
attributes (see fopen description). |
|
1409 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1410 |
This argument is ignored in UNIX & MSDOS systems. |
|
1411 |
||
1412 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1413 |
which is proprietrary to the operatingSystem." |
|
1414 |
||
16298 | 1415 |
|wasBlocked encodedPathName error| |
14578 | 1416 |
|
1417 |
encodedPathName := OperatingSystem encodePath:pathName. |
|
5439 | 1418 |
|
1419 |
%{ |
|
15462 | 1420 |
HFILE f = NULL; |
5439 | 1421 |
int pass = 0; |
1422 |
||
15462 | 1423 |
if (!__isNonNilObject(encodedPathName) |
24102 | 1424 |
|| !(__isStringLike(openmode) || __isArrayLike(openmode))) |
1425 |
goto badArgument; |
|
15462 | 1426 |
|
5439 | 1427 |
retry: |
1428 |
#ifdef __VMS__ |
|
15462 | 1429 |
if (__isStringLike(encodedPathName)) { |
24102 | 1430 |
do { |
1431 |
/* |
|
1432 |
* allow passing additional RMS arguments. |
|
1433 |
* stupid: DEC does not seem to offer an interface for passing a char **. |
|
1434 |
*/ |
|
1435 |
__threadErrno = 0; |
|
5439 | 1436 |
|
24102 | 1437 |
{ |
1438 |
if (__isArrayLike(attributeSpec)) { |
|
1439 |
OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; |
|
1440 |
int numAttrib = 0; |
|
1441 |
int i; |
|
5439 | 1442 |
|
24102 | 1443 |
numAttrib = __arraySize(attributeSpec); |
1444 |
for (i=0; i<numAttrib;i++) { |
|
1445 |
if (! __isStringLike(ap[i])) { |
|
1446 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1447 |
goto getOutOfHere; |
|
1448 |
} |
|
1449 |
} |
|
1450 |
switch (numAttrib) { |
|
1451 |
case 0: |
|
1452 |
__BEGIN_INTERRUPTABLE__ |
|
1453 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode)); |
|
1454 |
__END_INTERRUPTABLE__ |
|
1455 |
break; |
|
1456 |
case 1: |
|
1457 |
__BEGIN_INTERRUPTABLE__ |
|
1458 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1459 |
__stringVal(ap[0])); |
|
1460 |
__END_INTERRUPTABLE__ |
|
1461 |
break; |
|
1462 |
case 2: |
|
1463 |
__BEGIN_INTERRUPTABLE__ |
|
1464 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1465 |
__stringVal(ap[0]), __stringVal(ap[1])); |
|
1466 |
__END_INTERRUPTABLE__ |
|
1467 |
break; |
|
1468 |
case 3: |
|
1469 |
__BEGIN_INTERRUPTABLE__ |
|
1470 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1471 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2])); |
|
1472 |
__END_INTERRUPTABLE__ |
|
1473 |
break; |
|
1474 |
case 4: |
|
1475 |
__BEGIN_INTERRUPTABLE__ |
|
1476 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1477 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1478 |
__stringVal(ap[3])); |
|
1479 |
__END_INTERRUPTABLE__ |
|
1480 |
break; |
|
1481 |
case 5: |
|
1482 |
__BEGIN_INTERRUPTABLE__ |
|
1483 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1484 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1485 |
__stringVal(ap[3]), __stringVal(ap[4])); |
|
1486 |
__END_INTERRUPTABLE__ |
|
1487 |
break; |
|
1488 |
case 6: |
|
1489 |
__BEGIN_INTERRUPTABLE__ |
|
1490 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1491 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1492 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5])); |
|
1493 |
__END_INTERRUPTABLE__ |
|
1494 |
break; |
|
1495 |
case 7: |
|
1496 |
__BEGIN_INTERRUPTABLE__ |
|
1497 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1498 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1499 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1500 |
__stringVal(ap[6])); |
|
1501 |
__END_INTERRUPTABLE__ |
|
1502 |
break; |
|
1503 |
case 8: |
|
1504 |
__BEGIN_INTERRUPTABLE__ |
|
1505 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1506 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1507 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1508 |
__stringVal(ap[6]), __stringVal(ap[7])); |
|
1509 |
__END_INTERRUPTABLE__ |
|
1510 |
break; |
|
1511 |
case 9: |
|
1512 |
__BEGIN_INTERRUPTABLE__ |
|
1513 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1514 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1515 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1516 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8])); |
|
1517 |
__END_INTERRUPTABLE__ |
|
1518 |
break; |
|
1519 |
case 10: |
|
1520 |
__BEGIN_INTERRUPTABLE__ |
|
1521 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1522 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1523 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1524 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8]), |
|
1525 |
__stringVal(ap[9])); |
|
1526 |
__END_INTERRUPTABLE__ |
|
1527 |
break; |
|
1528 |
default: |
|
1529 |
__threadErrno = E2BIG; /* too many args */ |
|
1530 |
goto getOutOfHere; |
|
1531 |
} |
|
1532 |
} else if (attributeSpec != nil) { |
|
1533 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1534 |
goto getOutOfHere; |
|
1535 |
} else { |
|
1536 |
/* |
|
1537 |
* create file as sequential streamLF by default. |
|
1538 |
*/ |
|
1539 |
__BEGIN_INTERRUPTABLE__ |
|
1540 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), "rfm=stmlf"); |
|
1541 |
__END_INTERRUPTABLE__ |
|
1542 |
} |
|
1543 |
} |
|
1544 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
11290 | 1545 |
} |
5439 | 1546 |
#else /* not VMS */ |
1547 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1548 |
# 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
|
1549 |
{ |
24102 | 1550 |
DWORD share = 0, access = 0, create = 0, attr = 0; |
1551 |
char * __openmode; |
|
1552 |
HANDLE handle; |
|
1553 |
SECURITY_ATTRIBUTES sa; |
|
11290 | 1554 |
|
24102 | 1555 |
if (__isStringLike(openmode)) { |
1556 |
share = FILE_SHARE_READ|FILE_SHARE_WRITE; |
|
1557 |
__openmode = __stringVal(openmode); |
|
1558 |
if (strcmp(__openmode, "rb") == 0) { |
|
1559 |
access = GENERIC_READ; |
|
1560 |
create = OPEN_EXISTING; |
|
1561 |
} else if (strcmp(__openmode, "rb+") == 0) { |
|
1562 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1563 |
create = OPEN_EXISTING; |
|
1564 |
} else if (strcmp(__openmode, "wb") == 0) { |
|
1565 |
access = GENERIC_WRITE; |
|
1566 |
create = CREATE_ALWAYS; |
|
1567 |
} else if (strcmp(__openmode, "wb+") == 0) { |
|
1568 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1569 |
create = CREATE_ALWAYS; |
|
1570 |
} else if (strcmp(__openmode, "ab") == 0) { |
|
1571 |
access = FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1572 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1573 |
create = OPEN_ALWAYS; |
|
1574 |
} else if (strcmp(__openmode, "ab+") == 0) { |
|
1575 |
access = GENERIC_READ |FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1576 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1577 |
create = OPEN_ALWAYS; |
|
1578 |
} else { |
|
1579 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1580 |
} |
|
1581 |
} else if (__isArrayLike(openmode)) { |
|
1582 |
OBJ *ap = __arrayVal(openmode); |
|
1583 |
int numAttrib = __arraySize(openmode); |
|
1584 |
int i; |
|
15462 | 1585 |
|
24102 | 1586 |
__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
|
1587 |
|
24102 | 1588 |
for (i=0; i<numAttrib; i++) { |
1589 |
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
|
1590 |
|
24102 | 1591 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1592 |
share |= FILE_SHARE_READ; |
|
1593 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1594 |
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
|
1595 |
|
24102 | 1596 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
1597 |
access |= GENERIC_READ; |
|
1598 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
|
1599 |
access |= GENERIC_WRITE; |
|
1600 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
|
1601 |
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
|
1602 |
|
24102 | 1603 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
1604 |
create |= CREATE_NEW; |
|
1605 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1606 |
create |= CREATE_ALWAYS; |
|
1607 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1608 |
create |= OPEN_EXISTING; |
|
1609 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1610 |
create |= OPEN_ALWAYS; |
|
1611 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1612 |
create |= TRUNCATE_EXISTING; |
|
5439 | 1613 |
|
24102 | 1614 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1615 |
attr |= FILE_ATTRIBUTE_HIDDEN; |
|
1616 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1617 |
attr |= FILE_ATTRIBUTE_READONLY; |
|
1618 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
1619 |
attr |= FILE_FLAG_WRITE_THROUGH; |
|
1620 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
|
1621 |
attr |= FILE_FLAG_SEQUENTIAL_SCAN; |
|
1622 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1623 |
attr |= FILE_FLAG_DELETE_ON_CLOSE; |
|
1624 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1625 |
__openmode = __stringVal(attrSym); |
|
1626 |
} else { |
|
1627 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1628 |
} |
|
1629 |
} |
|
1630 |
} |
|
1631 |
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
|
1632 |
// argumentError = @symbol(missingCreateMode); |
24102 | 1633 |
goto badArgument; |
1634 |
} |
|
1635 |
if (attr == 0) { |
|
1636 |
attr = FILE_ATTRIBUTE_NORMAL; |
|
1637 |
} |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1638 |
|
24102 | 1639 |
/* |
1640 |
* create security attributes - make handle inheritable by subprocesses |
|
1641 |
*/ |
|
1642 |
memset(&sa, 0, sizeof (sa)); |
|
1643 |
sa.nLength = sizeof( sa ); |
|
1644 |
// sa.bInheritHandle = TRUE; |
|
1645 |
sa.bInheritHandle = FALSE; |
|
12630
c6151f8f6854
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12467
diff
changeset
|
1646 |
|
24102 | 1647 |
if (__isStringLike(pathName)) { |
1648 |
char _aPathName[MAXPATHLEN]; |
|
11290 | 1649 |
|
24102 | 1650 |
strncpy(_aPathName, __stringVal(pathName), MAXPATHLEN-1); _aPathName[MAXPATHLEN-1] = '\0'; |
1651 |
do { |
|
1652 |
__threadErrno = 0; |
|
1653 |
handle = STX_API_NOINT_CALL7( "CreateFileA", CreateFileA, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1654 |
if (__threadErrno == EINTR) { |
|
1655 |
handle = INVALID_HANDLE_VALUE; |
|
1656 |
} |
|
1657 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1658 |
} else if (__isUnicode16String(pathName)) { |
|
1659 |
wchar_t _aPathName[MAXPATHLEN+1]; |
|
1660 |
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
|
1661 |
|
24102 | 1662 |
l = __unicode16StringSize(pathName); |
1663 |
if (l > MAXPATHLEN) l = MAXPATHLEN; |
|
1664 |
for (i=0; i<l; i++) { |
|
1665 |
_aPathName[i] = __unicode16StringVal(pathName)[i]; |
|
1666 |
} |
|
1667 |
_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
|
1668 |
|
24102 | 1669 |
do { |
1670 |
__threadErrno = 0; |
|
1671 |
handle = STX_API_NOINT_CALL7( "CreateFileW", CreateFileW, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1672 |
if (__threadErrno == EINTR) { |
|
1673 |
handle = INVALID_HANDLE_VALUE; |
|
1674 |
} |
|
1675 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1676 |
} |
|
1677 |
if (handle != INVALID_HANDLE_VALUE) { |
|
1678 |
int fd; |
|
1679 |
extern void __stxWrapApiEnterCritical(), __stxWrapApiLeaveCritical(); |
|
12692
6150a8d7c87e
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12687
diff
changeset
|
1680 |
|
24102 | 1681 |
__stxWrapApiEnterCritical(); |
1682 |
fd = _open_osfhandle((long)handle, O_BINARY); |
|
1683 |
if (fd < 0) { |
|
1684 |
if (__threadErrno == 0) { |
|
1685 |
// no more file descriptors |
|
1686 |
__threadErrno = EMFILE; |
|
1687 |
} |
|
1688 |
CloseHandle(handle); |
|
1689 |
} else { |
|
1690 |
f = fdopen(fd, __openmode); |
|
1691 |
} |
|
1692 |
__stxWrapApiLeaveCritical(); |
|
1693 |
} else { |
|
1694 |
__threadErrno = __WIN32_ERR(GetLastError()); |
|
1695 |
} |
|
11290 | 1696 |
} |
5439 | 1697 |
# else /* not WIN32 */ |
1698 |
||
14578 | 1699 |
if (__isStringLike(encodedPathName)) { |
24102 | 1700 |
int accessMode = 0666; // default access mode of fopen(), relies on umask() |
1701 |
int flags = 0; |
|
1702 |
int fd; |
|
1703 |
char * __openmode; |
|
15462 | 1704 |
|
24102 | 1705 |
if (__isStringLike(openmode)) { |
1706 |
__openmode = __stringVal(openmode); |
|
1707 |
if (strcmp(__openmode, "r") == 0) { |
|
1708 |
flags = O_RDONLY; |
|
1709 |
} else if (strcmp(__openmode, "r+") == 0) { |
|
1710 |
flags = O_RDWR; |
|
1711 |
} else if (strcmp(__openmode, "w") == 0) { |
|
1712 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
|
1713 |
} else if (strcmp(__openmode, "w+") == 0) { |
|
1714 |
flags = O_RDWR | O_CREAT | O_TRUNC; |
|
1715 |
} else if (strcmp(__openmode, "a") == 0) { |
|
1716 |
flags = O_WRONLY | O_CREAT | O_APPEND; |
|
1717 |
} else if (strcmp(__openmode, "a+") == 0) { |
|
1718 |
flags = O_RDWR | O_CREAT| O_APPEND; |
|
1719 |
} else { |
|
1720 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1721 |
} |
|
1722 |
} else if (__isArrayLike(openmode)) { |
|
1723 |
OBJ *ap = __arrayVal(openmode); |
|
1724 |
int numAttrib = __arraySize(openmode); |
|
1725 |
int i; |
|
15462 | 1726 |
|
24102 | 1727 |
__openmode = "r+"; |
15462 | 1728 |
|
24102 | 1729 |
for (i=0; i<numAttrib; i++) { |
1730 |
OBJ attrSym = ap[i]; |
|
15462 | 1731 |
|
24102 | 1732 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1733 |
// ignore |
|
1734 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1735 |
// ignore |
|
1736 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
|
1737 |
flags |= O_RDONLY; |
|
1738 |
__openmode = "r"; |
|
1739 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
|
1740 |
flags |= O_WRONLY; |
|
1741 |
__openmode = "w"; |
|
1742 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
|
1743 |
flags |= O_RDWR; |
|
1744 |
__openmode = "r+"; |
|
1745 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
|
1746 |
flags |= O_CREAT|O_EXCL; |
|
1747 |
accessMode = 0600; // simulate mkstemp() |
|
1748 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1749 |
flags |= O_CREAT|O_TRUNC; |
|
1750 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1751 |
// nothing to be set |
|
1752 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1753 |
flags |= O_CREAT; |
|
1754 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1755 |
flags |= O_TRUNC; |
|
15462 | 1756 |
|
24102 | 1757 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1758 |
// ignore |
|
1759 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1760 |
accessMode &= 0444; |
|
1761 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
15462 | 1762 |
#ifdef O_DIRECT |
24102 | 1763 |
flags |= O_DIRECT; |
15462 | 1764 |
#endif |
24102 | 1765 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
1766 |
// ignore |
|
1767 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1768 |
// ignore; |
|
1769 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1770 |
__openmode = __stringVal(attrSym); |
|
1771 |
} else { |
|
1772 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1773 |
} |
|
1774 |
} |
|
1775 |
} |
|
1776 |
do { |
|
1777 |
__BEGIN_INTERRUPTABLE__ |
|
1778 |
fd = open((char *) __stringVal(encodedPathName), flags, accessMode); |
|
1779 |
__END_INTERRUPTABLE__ |
|
1780 |
} while ((fd < 0) && (__threadErrno == EINTR)); |
|
15462 | 1781 |
|
24102 | 1782 |
if (fd >= 0) { |
1783 |
__threadErrno = 0; |
|
1784 |
f = fdopen(fd, __openmode); |
|
1785 |
if (f == NULL) { |
|
1786 |
close(fd); // fdopen failed, close before retry. |
|
1787 |
} |
|
1788 |
} |
|
11290 | 1789 |
} |
15462 | 1790 |
|
5439 | 1791 |
# endif /* not WIN32 */ |
1792 |
#endif /* not VMS */ |
|
1793 |
||
15462 | 1794 |
|
11290 | 1795 |
if (f == NULL) { |
24102 | 1796 |
/* |
1797 |
* If no filedescriptors available, try to finalize |
|
1798 |
* possibly collected fd's and try again. |
|
1799 |
*/ |
|
1800 |
if ((__threadErrno == ENFILE || __threadErrno == EMFILE) && pass == 0) { |
|
1801 |
pass = 1; |
|
1802 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
|
1803 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
|
1804 |
goto retry; |
|
1805 |
} |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1806 |
badArgument: |
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1807 |
getOutOfHere: |
23539 | 1808 |
# ifdef __win32__ |
24102 | 1809 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1810 |
# endif |
24102 | 1811 |
error = __mkSmallInteger(__threadErrno); |
1812 |
__INST(position) = nil; |
|
11290 | 1813 |
} else { |
5439 | 1814 |
#ifdef __VMS__ |
24102 | 1815 |
/* |
1816 |
* check to see if this is positionable ... |
|
1817 |
*/ |
|
1818 |
__INST(canPosition) = false; |
|
5439 | 1819 |
# ifndef _POSIX_C_SOURCE |
24102 | 1820 |
{ |
1821 |
struct stat statBuffer; |
|
5439 | 1822 |
|
24102 | 1823 |
if (fstat(fileno(f), &statBuffer) >= 0) { |
1824 |
switch (statBuffer.st_fab_rfm) { |
|
1825 |
case FAB$C_UDF: /* undefined (also stream binary) */ |
|
1826 |
case FAB$C_VAR: /* variable length records */ |
|
1827 |
case FAB$C_VFC: /* variable fixed control */ |
|
1828 |
case FAB$C_STM: /* RMS-11 stream (valid only for sequen) */ |
|
1829 |
default: |
|
1830 |
__INST(canPosition) = false; |
|
1831 |
break; |
|
5439 | 1832 |
|
24102 | 1833 |
case FAB$C_FIX: /* fixed length records */ |
1834 |
case FAB$C_STMLF: /* LF stream (valid only for sequential) */ |
|
1835 |
case FAB$C_STMCR: /* CR stream (valid only for sequential) */ |
|
1836 |
__INST(canPosition) = true; |
|
1837 |
break; |
|
1838 |
} |
|
1839 |
} |
|
1840 |
} |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1841 |
# endif |
5439 | 1842 |
#else /* not VMS */ |
24102 | 1843 |
__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
|
1844 |
#endif /* not VMS */ |
5439 | 1845 |
|
24102 | 1846 |
if (@global(FileOpenTrace) == true) { |
1847 |
console_fprintf(stderr, "fopen %s [FileStream] -> %"_lx_"\n", __stringVal(pathName), (INT)f); |
|
1848 |
} |
|
1849 |
if (f != NULL) { |
|
1850 |
OBJ fp; |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1851 |
|
24102 | 1852 |
wasBlocked = __BLOCKINTERRUPTS(); |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1853 |
#if 0 |
24102 | 1854 |
// The original code was: |
1855 |
__INST(handle) = fp = __MKEXTERNALADDRESS(f); __STORE(self, fp); |
|
1856 |
// but for that, gcc generates wrong code, which loads self (volatile) into |
|
1857 |
// a register (bp), then calls __MKEXTERNALADDRESS, then stores indirect bp. |
|
1858 |
// That is wrong if a scavenge occurs in __MKEXTERNALADDRESS, as bp is now still pointing to the old |
|
1859 |
// object. |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1860 |
#else |
24102 | 1861 |
fp = __MKEXTERNALADDRESS(f); |
1862 |
__INST(handle) = fp; |
|
1863 |
__STORE(self, fp); |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1864 |
#endif |
24102 | 1865 |
} |
5439 | 1866 |
} |
1867 |
%}. |
|
16298 | 1868 |
error notNil ifTrue:[ |
24102 | 1869 |
lastErrorNumber := error. |
1870 |
^ self openError:error. |
|
8034
716b32d832bf
raise condition interrupt: interrupt between creation and lobby
ca
parents:
7970
diff
changeset
|
1871 |
]. |
16298 | 1872 |
handle isNil ifTrue:[ |
24102 | 1873 |
^ self openError:0. |
16298 | 1874 |
]. |
1875 |
||
20096 | 1876 |
buffered := true. "default is buffered" |
16298 | 1877 |
position := 0. |
1878 |
handleType := #filePointer. |
|
18781 | 1879 |
self registerForFinalization. |
16298 | 1880 |
wasBlocked == false ifTrue:[OperatingSystem unblockInterrupts]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1881 |
^ handle |
23539 | 1882 |
|
1883 |
"Modified: / 22-11-2018 / 14:53:51 / Stefan Vogel" |
|
5439 | 1884 |
! |
1885 |
||
613 | 1886 |
openForAppending |
1887 |
"open the file for writeonly appending to the end. |
|
20921 | 1888 |
If the file does not exist, raise OpenError; |
613 | 1889 |
otherwise return the receiver." |
1890 |
||
1891 |
mode := #writeonly. |
|
1892 |
didWrite := true. |
|
2921 | 1893 |
^ self openWithMode:AppendMode |
613 | 1894 |
! |
1895 |
||
1896 |
openForReadWrite |
|
1897 |
"open the file for read/write. |
|
20921 | 1898 |
If the file does not exist, raise OpenError; |
613 | 1899 |
otherwise return the receiver." |
1900 |
||
1901 |
mode := #readwrite. |
|
2921 | 1902 |
^ self openWithMode:ReadWriteMode |
613 | 1903 |
! |
1904 |
||
1905 |
openForReading |
|
1906 |
"open the file for readonly. |
|
20921 | 1907 |
If the file does not exist, raise OpenError; |
613 | 1908 |
otherwise return the receiver." |
1909 |
||
1910 |
mode := #readonly. |
|
1911 |
didWrite := false. |
|
2921 | 1912 |
^ self openWithMode:ReadMode |
613 | 1913 |
! |
1914 |
||
1915 |
openForWriting |
|
8604 | 1916 |
"open the file writeonly. The contents of the file is preserved. |
20921 | 1917 |
If the file does not exist, raise OpenError; |
613 | 1918 |
otherwise return the receiver." |
1919 |
||
1920 |
mode := #writeonly. |
|
1921 |
didWrite := true. |
|
8604 | 1922 |
"we must not truncate the file!! So do not use WriteMode" |
1923 |
^ self openWithMode:ReadWriteMode |
|
613 | 1924 |
! |
1925 |
||
1 | 1926 |
openWithMode:openmode |
2921 | 1927 |
"open the file; |
1928 |
openmode is the string defining the way to open as defined by the stdio library |
|
1929 |
(i.e. the 2nd fopen argument). |
|
1930 |
||
1931 |
This is a private entry, but maybe useful to open a file in a special mode, |
|
1932 |
which is proprietrary to the operatingSystem." |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1933 |
|
2921 | 1934 |
^ self openWithMode:openmode attributes:nil |
1935 |
! |
|
1936 |
||
1937 |
openWithMode:openmode attributes:attributeSpec |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1938 |
"open the file; |
2921 | 1939 |
openmode is the string defining the way to open as defined by the stdio library |
1940 |
(i.e. the 2nd fopen argument). |
|
1941 |
||
1942 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1943 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1944 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1945 |
attributes (see fopen description). |
|
1946 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1947 |
This argument is ignored in UNIX & MSDOS systems. |
|
1948 |
||
1949 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1950 |
which is proprietrary to the operatingSystem." |
|
1 | 1951 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1952 |
handle notNil ifTrue:[^ self errorAlreadyOpen]. |
2921 | 1953 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1954 |
handle := self openFile:pathName withMode:openmode attributes:attributeSpec. |
17144 | 1955 |
handle isNil ifTrue:[ |
21623 | 1956 |
"this happens, if proceeding from an OpenError raised in #openFile..." |
1957 |
^ nil. |
|
8604 | 1958 |
]. |
1 | 1959 |
! |
1960 |
||
613 | 1961 |
pathName:filename |
1962 |
"set the pathname" |
|
1 | 1963 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1964 |
pathName := filename asString |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1965 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1966 |
"Modified: / 28.10.1997 / 14:29:01 / cg" |
1 | 1967 |
! |
1968 |
||
613 | 1969 |
pathName:filename in:aDirectory |
1970 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 1971 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1972 |
pathName := aDirectory asFilename constructString:filename |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1973 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1974 |
"Modified: / 28.10.1997 / 14:28:54 / cg" |
1 | 1975 |
! |
1976 |
||
1977 |
reOpen |
|
8604 | 1978 |
"USERS WILL NEVER INVOKE THIS METHOD |
1979 |
sent after snapin to reopen streams." |
|
1 | 1980 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1981 |
handle notNil ifTrue:[ |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1982 |
"it was open, when snapped-out" |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1983 |
handle := nil. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1984 |
self unregisterForFinalization. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1985 |
pathName isNil ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1986 |
^ self. |
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 |
[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1989 |
|oldPos| |
173 | 1990 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1991 |
"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
|
1992 |
append / position them to the end" |
8604 | 1993 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1994 |
oldPos := position. |
8604 | 1995 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1996 |
mode == #readonly ifTrue: [ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1997 |
self openForReading |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1998 |
] ifFalse:[mode == #writeonly ifTrue: [ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1999 |
self openForWriting. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2000 |
] ifFalse:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2001 |
self openForReadWrite. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2002 |
]]. |
8604 | 2003 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2004 |
oldPos notNil ifTrue:[ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2005 |
self position:oldPos. |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2006 |
] |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2007 |
] on:OpenError do:[:ex| |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2008 |
"this happens, if after a restart, |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2009 |
the file is no longer present or accessible ..." |
13 | 2010 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
2011 |
(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
|
2012 |
]. |
1 | 2013 |
] |
1088 | 2014 |
|
2134
246a3bdab8b4
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
2078
diff
changeset
|
2015 |
"Modified: 10.1.1997 / 17:50:51 / cg" |
2921 | 2016 |
! |
2017 |
||
2018 |
setMode:aModeSymbol |
|
2019 |
mode := aModeSymbol |
|
15479 | 2020 |
! |
2021 |
||
2022 |
setPathName:pathNameString removeOnClose:aBoolean |
|
2023 |
pathName := pathNameString. |
|
2024 |
removeOnClose := aBoolean. |
|
31 | 2025 |
! ! |
2026 |
||
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2027 |
!FileStream methodsFor:'private fileIn'! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2028 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2029 |
fileInNotifying:notifiedLoader passChunk:passChunk |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2030 |
"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
|
2031 |
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
|
2032 |
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
|
2033 |
|
21459 | 2034 |
^ self fileInNotifying:notifiedLoader passChunk:passChunk inDirectory:(self fileName directory). |
2035 |
||
2036 |
"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
|
2037 |
! ! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2038 |
|
2921 | 2039 |
!FileStream methodsFor:'queries'! |
613 | 2040 |
|
17603 | 2041 |
collectionSize |
2042 |
"common stream protocol: return the size of the stream; |
|
20412 | 2043 |
that's the number of bytes of the file." |
17603 | 2044 |
|
2045 |
^ self fileSize. |
|
2046 |
! |
|
2047 |
||
2903 | 2048 |
fileSize |
613 | 2049 |
"return the size in bytes of the file" |
2050 |
||
16298 | 2051 |
|error| |
3971 | 2052 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2053 |
HFILE f; |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2054 |
|
613 | 2055 |
#ifdef transputer |
7874
5e0319ca5e60
stats filesize can be up-to 32bit now.
Claus Gittinger <cg@exept.de>
parents:
7872
diff
changeset
|
2056 |
unsigned int size; |
613 | 2057 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2058 |
if (__INST(handle) != nil) { |
24102 | 2059 |
f = __FILEVal(__INST(handle)); |
2060 |
if ((size = filesize(fileno(f))) >= 0) { |
|
2061 |
RETURN ( __MKUINT(size) ); |
|
2062 |
} |
|
613 | 2063 |
} |
2064 |
#else |
|
2065 |
struct stat buf; |
|
2066 |
int ret; |
|
2067 |
int fd; |
|
2068 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2069 |
if (__INST(handle) != nil) { |
24102 | 2070 |
f = __FILEVal(__INST(handle)); |
2071 |
fd = fileno(f); |
|
2072 |
do { |
|
2073 |
ret = fstat(fd, &buf); |
|
2074 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
2075 |
if (ret >= 0) { |
|
2076 |
OBJ rslt; |
|
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
2077 |
|
24102 | 2078 |
if (sizeof(buf.st_size) == 8) { |
2079 |
rslt = __MKINT64(&buf.st_size); |
|
2080 |
} else { |
|
2081 |
rslt = __MKINT(buf.st_size); |
|
2082 |
} |
|
2083 |
RETURN(rslt); |
|
2084 |
} |
|
23539 | 2085 |
# ifdef __win32__ |
24102 | 2086 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 2087 |
# endif |
24102 | 2088 |
error = __mkSmallInteger(__threadErrno); |
613 | 2089 |
} |
2090 |
#endif |
|
2091 |
%}. |
|
2092 |
||
2093 |
"could add a fall-back here: |
|
2094 |
||
24102 | 2095 |
oldPosition := self position. |
2096 |
self setToEnd. |
|
2097 |
sz := self position. |
|
2098 |
self position:oldPosition. |
|
2099 |
^ sz |
|
613 | 2100 |
" |
16298 | 2101 |
error notNil ifTrue:[ |
24102 | 2102 |
lastErrorNumber := error. |
2103 |
self ioError:error. |
|
2104 |
^ self. |
|
16298 | 2105 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2106 |
handle isNil ifTrue:[^ self errorNotOpen]. |
613 | 2107 |
^ self primitiveFailed |
23539 | 2108 |
|
2109 |
"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
|
2110 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2111 |
|
13051 | 2112 |
isDirectory |
2113 |
^ OperatingSystem isDirectory:pathName |
|
2114 |
! |
|
2115 |
||
14093 | 2116 |
isEmpty |
2117 |
"common stream protocol: are there no bytes in the file?" |
|
2118 |
||
2119 |
^ self fileSize == 0. |
|
2120 |
! |
|
2121 |
||
17603 | 2122 |
remainingSize |
2123 |
"return the number of remaining elements in the streamed collection." |
|
2124 |
||
2125 |
^ self fileSize - self position. |
|
2126 |
! |
|
2127 |
||
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2128 |
size |
11454 | 2129 |
"common stream protocol: return the size of the stream; |
20412 | 2130 |
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
|
2131 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2132 |
^ self fileSize. |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2133 |
|
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2134 |
"Modified: 4.10.1997 / 18:01:09 / cg" |
1 | 2135 |
! ! |
2136 |
||
5493 | 2137 |
!FileStream methodsFor:'rel5 protocol'! |
2138 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2139 |
positionFile:handle position:newPos |
5493 | 2140 |
"for migration to rel5 only" |
2141 |
||
2142 |
self primitiveFailed |
|
2143 |
! ! |
|
2144 |
||
2 | 2145 |
!FileStream methodsFor:'testing'! |
2146 |
||
2147 |
isFileStream |
|
2148 |
"return true, if the receiver is some kind of fileStream. |
|
2149 |
redefined from Object" |
|
2150 |
||
2151 |
^ true |
|
2152 |
! ! |
|
2153 |
||
1088 | 2154 |
!FileStream class methodsFor:'documentation'! |
2155 |
||
2156 |
version |
|
18651 | 2157 |
^ '$Header$' |
12207
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 |
|
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2160 |
version_CVS |
18651 | 2161 |
^ '$Header$' |
1088 | 2162 |
! ! |
6798 | 2163 |
|
15360 | 2164 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2165 |
FileStream initialize! |