authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-19 16:17:04-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-01-19 16:17:04-05:00
log2eede35577fdd917e5680a2bf3c2cf64ff9339f2
tree060682b945efa97288b42d12d8b64020fd82dc4e
parent613c4dbf58a281eb70c63e5ae2834a8d1f7c7d7b
parentd8469e3c7ce9c0627aba65075ae85b98d2b60cbc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #710 from Hejsil/seekto-getpos-windows

Implemented windows versions of seekTo and getPos

2 files changed, 48 insertions(+), 1 deletions(-)

std/io.zig+41-1
......@@ -48,6 +48,7 @@ error PathNotFound;
4848error OutOfMemory;
4949error Unseekable;
5050error EndOfFile;
51error FilePosLargerThanPointerRange;
5152
5253pub fn getStdErr() -> %File {
5354 const handle = if (is_windows)
......@@ -204,6 +205,15 @@ pub const File = struct {
204205 };
205206 }
206207 },
208 Os.windows => {
209 if (system.SetFilePointerEx(self.handle, amount, null, system.FILE_CURRENT) == 0) {
210 const err = system.GetLastError();
211 return switch (err) {
212 system.ERROR.INVALID_PARAMETER => error.BadFd,
213 else => os.unexpectedErrorWindows(err),
214 };
215 }
216 },
207217 else => @compileError("unsupported OS"),
208218 }
209219 }
......@@ -211,7 +221,8 @@ pub const File = struct {
211221 pub fn seekTo(self: &File, pos: usize) -> %void {
212222 switch (builtin.os) {
213223 Os.linux, Os.macosx, Os.ios => {
214 const result = system.lseek(self.handle, @bitCast(isize, pos), system.SEEK_SET);
224 const ipos = try math.cast(isize, pos);
225 const result = system.lseek(self.handle, ipos, system.SEEK_SET);
215226 const err = system.getErrno(result);
216227 if (err > 0) {
217228 return switch (err) {
......@@ -224,6 +235,16 @@ pub const File = struct {
224235 };
225236 }
226237 },
238 Os.windows => {
239 const ipos = try math.cast(isize, pos);
240 if (system.SetFilePointerEx(self.handle, ipos, null, system.FILE_BEGIN) == 0) {
241 const err = system.GetLastError();
242 return switch (err) {
243 system.ERROR.INVALID_PARAMETER => error.BadFd,
244 else => os.unexpectedErrorWindows(err),
245 };
246 }
247 },
227248 else => @compileError("unsupported OS: " ++ @tagName(builtin.os)),
228249 }
229250 }
......@@ -245,6 +266,25 @@ pub const File = struct {
245266 }
246267 return result;
247268 },
269 Os.windows => {
270 var pos : system.LARGE_INTEGER = undefined;
271 if (system.SetFilePointerEx(self.handle, 0, &pos, system.FILE_CURRENT) == 0) {
272 const err = system.GetLastError();
273 return switch (err) {
274 system.ERROR.INVALID_PARAMETER => error.BadFd,
275 else => os.unexpectedErrorWindows(err),
276 };
277 }
278
279 assert(pos >= 0);
280 if (@sizeOf(@typeOf(pos)) > @sizeOf(usize)) {
281 if (pos > @maxValue(usize)) {
282 return error.FilePosLargerThanPointerRange;
283 }
284 }
285
286 return usize(pos);
287 },
248288 else => @compileError("unsupported OS"),
249289 }
250290 }
std/os/windows/index.zig+7
......@@ -74,6 +74,9 @@ pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: LPVO
7474 in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD,
7575 in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL;
7676
77pub extern "kernel32" stdcallcc fn SetFilePointerEx(in_fFile: HANDLE, in_liDistanceToMove: LARGE_INTEGER,
78 out_opt_ldNewFilePointer: ?&LARGE_INTEGER, in_dwMoveMethod: DWORD) -> BOOL;
79
7780pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL;
7881
7982pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);
......@@ -289,3 +292,7 @@ pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4;
289292pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32;
290293pub const MOVEFILE_REPLACE_EXISTING = 1;
291294pub const MOVEFILE_WRITE_THROUGH = 8;
295
296pub const FILE_BEGIN = 0;
297pub const FILE_CURRENT = 1;
298pub const FILE_END = 2;
\ No newline at end of file