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