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;...@@ -48,6 +48,7 @@ error PathNotFound;
48error OutOfMemory;48error OutOfMemory;
49error Unseekable;49error Unseekable;
50error EndOfFile;50error EndOfFile;
51error FilePosLargerThanPointerRange;
5152
52pub fn getStdErr() -> %File {53pub fn getStdErr() -> %File {
53 const handle = if (is_windows)54 const handle = if (is_windows)
...@@ -204,6 +205,15 @@ pub const File = struct {...@@ -204,6 +205,15 @@ pub const File = struct {
204 };205 };
205 }206 }
206 },207 },
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 },
207 else => @compileError("unsupported OS"),217 else => @compileError("unsupported OS"),
208 }218 }
209 }219 }
...@@ -211,7 +221,8 @@ pub const File = struct {...@@ -211,7 +221,8 @@ pub const File = struct {
211 pub fn seekTo(self: &File, pos: usize) -> %void {221 pub fn seekTo(self: &File, pos: usize) -> %void {
212 switch (builtin.os) {222 switch (builtin.os) {
213 Os.linux, Os.macosx, Os.ios => {223 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);
215 const err = system.getErrno(result);226 const err = system.getErrno(result);
216 if (err > 0) {227 if (err > 0) {
217 return switch (err) {228 return switch (err) {
...@@ -224,6 +235,16 @@ pub const File = struct {...@@ -224,6 +235,16 @@ pub const File = struct {
224 };235 };
225 }236 }
226 },237 },
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 },
227 else => @compileError("unsupported OS: " ++ @tagName(builtin.os)),248 else => @compileError("unsupported OS: " ++ @tagName(builtin.os)),
228 }249 }
229 }250 }
...@@ -245,6 +266,25 @@ pub const File = struct {...@@ -245,6 +266,25 @@ pub const File = struct {
245 }266 }
246 return result;267 return result;
247 },268 },
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 },
248 else => @compileError("unsupported OS"),288 else => @compileError("unsupported OS"),
249 }289 }
250 }290 }
std/os/windows/index.zig+7
...@@ -74,6 +74,9 @@ pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: LPVO...@@ -74,6 +74,9 @@ pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: LPVO
74 in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD,74 in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD,
75 in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL;75 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
77pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL;80pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL;
7881
79pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);82pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);
...@@ -289,3 +292,7 @@ pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4;...@@ -289,3 +292,7 @@ pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4;
289pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32;292pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32;
290pub const MOVEFILE_REPLACE_EXISTING = 1;293pub const MOVEFILE_REPLACE_EXISTING = 1;
291pub const MOVEFILE_WRITE_THROUGH = 8;294pub 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