1 /*
2 Copyright (c) 2014-2021 Martin Cejp
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dlib.filesystem.windows.file;
30 
31 version(Windows)
32 {
33     import dlib.filesystem.filesystem;
34     import dlib.filesystem.windows.common;
35     import dlib.core.stream;
36     import dlib.core.memory;
37 
38     class WindowsFile: IOStream
39     {
40         HANDLE handle;
41         uint accessFlags;
42         bool eof = false;
43 
44         this(HANDLE handle, uint accessFlags)
45         {
46             this.handle = handle;
47             this.accessFlags = accessFlags;
48         }
49 
50         ~this()
51         {
52             close();
53         }
54 
55         override void close()
56         {
57             if (handle != INVALID_HANDLE_VALUE)
58             {
59                 CloseHandle(handle);
60                 handle = INVALID_HANDLE_VALUE;
61             }
62         }
63 
64         override bool seekable()
65         {
66             return true;
67         }
68 
69         override StreamPos getPosition()
70         {
71             LONG pos_high = 0;
72             LONG pos_low = SetFilePointer(handle, 0, &pos_high, FILE_CURRENT);
73 
74             wenforce(pos_low != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR);
75 
76             return cast(StreamPos) pos_high << 32 | pos_low;
77         }
78 
79         override bool setPosition(StreamPos pos)
80         {
81             LONG pos_high = cast(LONG)(pos >> 32);
82 
83             if (SetFilePointer(handle, cast(LONG) pos, &pos_high, FILE_BEGIN) == INVALID_SET_FILE_POINTER
84                 && GetLastError() != NO_ERROR)
85                 return false;
86             else
87                 return true;
88         }
89 
90         override StreamSize size()
91         {
92             DWORD size_high;
93             DWORD size_low = GetFileSize(handle, &size_high);
94 
95             wenforce(size_low != INVALID_FILE_SIZE || GetLastError() == NO_ERROR);
96 
97             return cast(StreamPos) size_high << 32 | size_low;
98         }
99 
100         override bool readable()
101         {
102             return handle != INVALID_HANDLE_VALUE && (accessFlags & FileSystem.read) && !eof;
103         }
104 
105         override size_t readBytes(void* buffer, size_t count)
106         {
107             // TODO: make sure that count fits in a DWORD
108             DWORD dwCount = cast(DWORD) count;
109 
110             DWORD dwGot = void;
111             wenforce(ReadFile(handle, buffer, dwCount, &dwGot, null));
112 
113             if (dwCount > dwGot)
114                 eof = true;
115 
116             return dwGot;
117         }
118 
119         override bool writeable()
120         {
121             return handle != INVALID_HANDLE_VALUE && (accessFlags & FileSystem.write);
122         }
123 
124         override size_t writeBytes(const void* buffer, size_t count)
125         {
126             // TODO: make sure that count fits in a DWORD
127             DWORD dwCount = cast(DWORD) count;
128 
129             DWORD dwGot = void;
130             wenforce(WriteFile(handle, buffer, dwCount, &dwGot, null));
131 
132             return dwGot;
133         }
134 
135         override void flush()
136         {
137         }
138     }
139 }