source: Include/Classes/System/IO/FileStream.ab@ 260

Last change on this file since 260 was 260, checked in by NoWest, 17 years ago

コンストラクタを整備

File size: 9.5 KB
Line 
1
2/* ほんとはmiscに入れるかかファイルを分けたほうがいいかもしれないが一先ず実装 */
3Enum FileOptions
4 Asynchronous
5 DeleteOnClose
6 Encrypted
7 None
8 RandomAccess
9 SequentialScan
10 WriteThrough
11End Enum
12
13Class FileStream
14 Inherits Stream
15
16 handle As HANDLE
17
18 /*
19 ファイルハンドルからこれらを取得できれば、これらは入らないが
20 今のところは不明なので自前で実装するしかない
21 */
22 filePath As String
23 fileMode As DWord
24 fileAccess As DWord
25 fileShare As DWord
26 fileOptions As DWord
27
28 /* 現在は未使用 */
29 /*Overlapped As OVERLAPPED*/
30
31Public
32 /* コンストラクタ.NETと同じように実装は難しい、一先ず動くものを実装したが変更が必要だと思う */
33 Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare, options As FileOptions)
34 Dim ac As DWord
35 Dim mo As DWord
36 Dim sh As DWord
37 Dim op As DWord
38
39 Select Case access
40 Case FileAccess.Read
41 ac=GENERIC_READ
42 Case FileAccess.ReadWrite
43 ac=GENERIC_READ or GENERIC_WRITE
44 Case FileAccess.Write
45 ac=GENERIC_WRITE
46 End Select
47
48 Select Case share
49 Case FileShare.DeleteFile
50 sh=FILE_SHARE_DELETE
51 Case FileShare.None
52 sh=0
53 Case FileShare.Read
54 sh=FILE_SHARE_READ
55 Case FileShare.ReadWrite
56 sh=FILE_SHARE_READ or FILE_SHARE_WRITE
57 Case FileShare.Write
58 sh=FILE_SHARE_WRITE
59 End Select
60
61 Select Case mode
62 Case FileMode.Append
63 mo=OPEN_ALWAYS
64 Case FileMode.Create
65 mo=CREATE_ALWAYS
66 Case FileMode.CreateNew
67 mo=CREATE_NEW
68 Case FileMode.Open
69 mo=OPEN_EXISTING
70 Case FileMode.OpenOrCreate
71 mo=OPEN_ALWAYS
72 Case FileMode.Truncate
73 mo=TRUNCATE_EXISTING
74 End Select
75
76 Select Case options
77 Case FileOptions.Asynchronous
78 op=FILE_FLAG_OVERLAPPED
79 Case FileOptions.DeleteOnClose
80 op=FILE_FLAG_DELETE_ON_CLOSE
81 Case FileOptions.Encrypted
82 Case FileOptions.None
83 op=0
84 Case FileOptions.RandomAccess
85 op=FILE_FLAG_RANDOM_ACCESS
86 Case FileOptions.SequentialScan
87 op=FILE_FLAG_SEQUENTIAL_SCAN
88 Case FileOptions.WriteThrough
89 op=FILE_FLAG_WRITE_THROUGH
90 End Select
91
92 This.handle=CreateFile(path.Chars As PSTR,ac,sh,ByVal NULL,mo,op,0)
93 If This.handle=INVALID_HANDLE_VALUE Then
94 'エラー処理
95 'Throw ArgumentException
96 'Throw IOException
97 'Throw System.IO.FileNotFoundException
98 This.handle=0
99 Exit Sub
100 End If
101
102 filePath = path
103 fileMode = mo
104 fileAccess = ac
105 fileShare = sh
106 fileOptions = op
107 End Sub
108 Sub FileStream(path As String, mode As FileMode, access As FileAccess, share As FileShare)
109 FileStream(path,mode,access,share,FileOptions.None)
110 End Sub
111 Sub FileStream(path As String, mode As FileMode, access As FileAccess)
112 FileStream(path,mode,access,FileShare.None,FileOptions.None)
113 End Sub
114 Sub FileStream(path As String, mode As FileMode)
115 Dim access As FileAccess
116 Select Case mode
117 Case FileMode.Append
118 access=FileAccess.Write
119 Case FileMode.Create
120 access=FileAccess.ReadWrite
121 Case FileMode.CreateNew
122 access=FileAccess.ReadWrite
123 Case FileMode.Open
124 access=FileAccess.ReadWrite
125 Case FileMode.OpenOrCreate
126 access=FileAccess.ReadWrite
127 Case FileMode.Truncate
128 access=FileAccess.Write
129 End Select
130 FileStream(path,mode,access,FileShare.None,FileOptions.None)
131 End Sub
132
133 Sub ~FileStream()
134 This.Flush()
135 This.Close()
136 End Sub
137
138Public
139 Override Function CanRead() As Boolean
140 /* ファイルが読み込みに対応しているかを返す */
141 If fileAccess=GENERIC_READ/*FileAccess.Read*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
142 Return True
143 Else
144 Return False
145 End If
146 End Function
147
148 Override Function CanSeek() As Boolean
149 /* ファイルがシークに対応しているかを返す */
150 Return True
151 End Function
152
153 Override Function CanTimeout() As Boolean
154 /* ファイルがタイムアウトに対応しているかを返す */
155 Return False /*今のところ対応していないのでFalse*/
156 End Function
157
158 Override Function CanWrite() As Boolean
159 /* ファイルが書き込みに対応しているかを返す */
160 If fileAccess=GENERIC_WRITE/*FileAccess.Write*/ or fileAccess=GENERIC_READ or GENERIC_WRITE/*FileAccess.ReadWrite*/ Then
161 Return True
162 Else
163 Return False
164 End If
165 End Function
166
167 /*Handle*/
168
169 Function IsAsync() As Boolean
170 /* ファイルが非同期操作に対応しているかを返す */
171 If fileOptions=FILE_FLAG_OVERLAPPED/*FileOptions.Asynchronous*/ Then
172 Return True
173 Else
174 Return False
175 End If
176 End Function
177
178 Override Function Length() As Int64
179 /* ファイルのサイズの取得 */
180 Dim s As LARGE_INTEGER
181 s.LowPart=GetFileSize(This.handle,VarPtr(s.HighPart) As *DWord)
182 memcpy(VarPtr(Length),VarPtr(s),SizeOf(LARGE_INTEGER))
183 End Function
184
185 Function Name() As String
186 /* 多分文字列をコピーして返す方がいいのと思うが。。。*/
187 Return filePath
188 End Function
189
190 Override Sub Position(value As Int64)
191 /* ファイルポインタの位置の設定 */
192 Dim o As LARGE_INTEGER
193 memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER))
194 If o.HighPart=0 And value<0 Then
195 o.LowPart=-Abs(o.LowPart) As Long
196 End If
197 Dim ret As LARGE_INTEGER
198 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
199 End Sub
200 Override Function Position() As Int64
201 /* ファイルポインタの位置の取得 */
202 Dim o As LARGE_INTEGER
203 o.HighPart=0
204 o.LowPart=0
205 o.LowPart=SetFilePointer(This.handle,0,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
206 memcpy(VarPtr(Position),VarPtr(o),SizeOf(LARGE_INTEGER))
207 End Function
208
209 Override Sub ReadTimeout(value As Long)
210 'TODO
211 End Sub
212 Override Function ReadTimeout() As Long
213 'TODO
214 End Function
215
216 /* Safe~Handle系の実装は要相談!! */
217/* Function SafeFileHandle() As SafeFileHandle
218 End Function*/
219
220 Override Sub WriteTimeout(value As Long)
221 'TODO
222 End Sub
223 Override Function WriteTimeout() As Long
224 'TODO
225 End Function
226
227
228Public
229 Override Function BeginRead(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As IAsyncResult
230 'TODO
231 End Function
232
233 Override Function BeginWrite(ByRef buffer[] As Byte, offset As Long, count As Long, callback As AsyncCallback, state As Object) As IAsyncResult
234 'TODO
235 End Function
236
237 Override Sub Close()
238 CloseHandle(This.handle)
239 End Sub
240
241/* CreateObjRef*/
242/* Dispose*/
243
244 Override Sub EndRead(ByRef asyncResult As IAsyncResult)
245 'TODO
246 End Sub
247
248 Override Sub EndWrite(ByRef asyncResult As IAsyncResult)
249 'TODO
250 End Sub
251
252/* Equals*/
253
254 Override Sub Flush()
255 FlushFileBuffers(This.handle)
256 End Sub
257
258/* Function GetAccessControl() As FileSecurity
259 FileSecurityの実装がまだできてない。
260 End Function*/
261
262 Override Function GetHashCode() As Long
263 Return ObjPtr(This) As Long
264 End Function
265
266/* GetLifetimeService*/
267
268/* Override Function GetType() As TypeInfo
269 Return Super.GetType()
270 End Function*/
271
272/* InitializeLifetimeService*/
273
274 Sub Lock(position As Int64, length As Int64)
275 Dim p As LARGE_INTEGER
276 Dim l As LARGE_INTEGER
277 memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))
278 memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))
279 LockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)
280 End Sub
281
282 Override Function Read(ByRef buffer[] As Byte, offset As Long, count As Long) As Long
283 If offset Then
284 SetFilePointer(This.handle,offset,NULL,FILE_CURRENT)
285 End If
286 ReadFile(This.handle,buffer,count,VarPtr(Read) As *DWord,ByVal NULL)
287 End Function
288
289 Override Function ReadByte() As Long
290 Dim b As Byte
291 Dim ret = Read(b, 0, 1)
292 If ret <> 0 Then
293 Return b As Long
294 Else
295 Return -1
296 End If
297 End Function
298/* ReferenceEquals*/
299
300 Override Function Seek(offset As Int64, origin As SeekOrigin) As Long
301 Dim o As LARGE_INTEGER
302 memcpy(VarPtr(o),VarPtr(offset),SizeOf(LARGE_INTEGER))
303 If o.HighPart=0 And offset<0 Then
304 o.LowPart=-o.LowPart
305 End If
306 Select Case origin
307 Case SeekOrigin.Begin
308 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN)
309 Case SeekOrigin.Current
310 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_CURRENT)
311 Case SeekOrigin.End
312 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_END)
313 End Select
314 End Function
315
316/* Sub SetAccessControl(fileSecurity As FileSecurity)
317 FileSecurityの実装がまだできてない。
318 End Sub*/
319
320 Override Sub SetLength(value As Int64)
321 Dim o As LARGE_INTEGER
322 memcpy(VarPtr(o),VarPtr(value),SizeOf(LARGE_INTEGER))
323 If o.HighPart=0 And value<0 Then
324 o.LowPart=-o.LowPart
325 End If
326 If Length()=>value Then
327 SetFilePointer(This.handle,o.LowPart,VarPtr(o.HighPart) As *DWord,FILE_BEGIN)
328 SetEndOfFile(This.handle)
329 End If
330 End Sub
331
332/* Synchronized*/
333
334 Override Function ToString() As String
335 Return Name()
336 End Function
337
338 Sub Unlock(position As Int64, length As Int64)
339 Dim p As LARGE_INTEGER
340 Dim l As LARGE_INTEGER
341 memcpy(VarPtr(p),VarPtr(position),SizeOf(LARGE_INTEGER))
342 memcpy(VarPtr(l),VarPtr(length),SizeOf(LARGE_INTEGER))
343 UnlockFile(This.handle,p.LowPart,p.HighPart,l.LowPart,l.HighPart)
344 End Sub
345
346 Override Sub Write(ByRef buffer[] As Byte, offset As Long, count As Long)
347 If offset Then
348 SetFilePointer(This.handle,offset,NULL,FILE_CURRENT)
349 End If
350 Dim ret As DWord
351 WriteFile(This.handle,buffer,count,VarPtr(ret),ByVal NULL)
352 End Sub
353
354 Override Sub WriteByte(b As Byte)
355 Write(b, 0, 1)
356 End Sub
357
358Protected
359 /*非同期ファイル操作に必須だがどうやって実装しようか検討中*/
360 Override Function CreateWaitHandle() As WaitHandle
361' Return Nothing
362 End Function
363
364/* Dispose
365 Finalize
366 MemberwiseClone*/
367End Class
Note: See TracBrowser for help on using the repository browser.