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