-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,77 @@ | ||
const std = @import("std"); | ||
const windows = std.os.windows; | ||
const kernel32 = windows.kernel32; | ||
|
||
pub usingnamespace std.os.windows; | ||
|
||
pub const FILE_GENERIC_READ = | ||
windows.STANDARD_RIGHTS_READ | | ||
windows.FILE_READ_DATA | | ||
windows.FILE_READ_ATTRIBUTES | | ||
windows.FILE_READ_EA | | ||
windows.SYNCHRONIZE; | ||
pub const FILE_GENERIC_WRITE = | ||
windows.STANDARD_RIGHTS_WRITE | | ||
windows.FILE_WRITE_DATA | | ||
windows.FILE_WRITE_ATTRIBUTES | | ||
windows.FILE_WRITE_EA | | ||
windows.SYNCHRONIZE; | ||
/// Namespace containing missing utils from std | ||
pub const exp = struct { | ||
pub const CreateFileError = error{} || std.os.UnexpectedError; | ||
|
||
pub const CreateFileError = error{} || std.os.UnexpectedError; | ||
pub fn CreateFile( | ||
lpFileName: [*:0]const u16, | ||
dwDesiredAccess: windows.DWORD, | ||
dwShareMode: windows.DWORD, | ||
lpSecurityAttributes: ?*windows.SECURITY_ATTRIBUTES, | ||
dwCreationDisposition: windows.DWORD, | ||
dwFlagsAndAttributes: windows.DWORD, | ||
hTemplateFile: ?windows.HANDLE, | ||
) CreateFileError!windows.HANDLE { | ||
const handle = windows.kernel32.CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); | ||
if (handle == windows.INVALID_HANDLE_VALUE) { | ||
const err = windows.kernel32.GetLastError(); | ||
return switch (err) { | ||
else => windows.unexpectedError(err), | ||
}; | ||
} | ||
|
||
pub fn CreateFile( | ||
lpFileName: [*:0]const u16, | ||
dwDesiredAccess: windows.DWORD, | ||
dwShareMode: windows.DWORD, | ||
lpSecurityAttributes: ?*windows.SECURITY_ATTRIBUTES, | ||
dwCreationDisposition: windows.DWORD, | ||
dwFlagsAndAttributes: windows.DWORD, | ||
hTemplateFile: ?windows.HANDLE, | ||
) CreateFileError!windows.HANDLE { | ||
const handle = windows.kernel32.CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); | ||
if (handle == windows.INVALID_HANDLE_VALUE) { | ||
const err = windows.kernel32.GetLastError(); | ||
return switch (err) { | ||
else => windows.unexpectedError(err), | ||
}; | ||
return handle; | ||
} | ||
|
||
return handle; | ||
} | ||
pub fn ReadFile( | ||
handle: windows.HANDLE, | ||
buffer: []u8, | ||
overlapped: ?*windows.OVERLAPPED, | ||
) windows.ReadFileError!?usize { | ||
var read: windows.DWORD = 0; | ||
const result: windows.BOOL = windows.kernel32.ReadFile(handle, buffer.ptr, @intCast(windows.DWORD, buffer.len), &read, overlapped); | ||
if (result == windows.FALSE) { | ||
const err = windows.kernel32.GetLastError(); | ||
return switch (err) { | ||
windows.Win32Error.IO_PENDING => null, | ||
else => windows.unexpectedError(err), | ||
}; | ||
} | ||
|
||
pub fn ReadFileW( | ||
handle: windows.HANDLE, | ||
buffer: []u8, | ||
overlapped: ?*windows.OVERLAPPED, | ||
) windows.ReadFileError!?usize { | ||
var read: windows.DWORD = 0; | ||
const result: windows.BOOL = kernel32.ReadFile(handle, buffer.ptr, @intCast(windows.DWORD, buffer.len), &read, overlapped); | ||
if (result == windows.FALSE) { | ||
const err = kernel32.GetLastError(); | ||
return switch (err) { | ||
windows.Win32Error.IO_PENDING => null, | ||
else => windows.unexpectedError(err), | ||
}; | ||
return @intCast(usize, read); | ||
} | ||
|
||
return @intCast(usize, read); | ||
} | ||
pub fn WriteFile( | ||
handle: windows.HANDLE, | ||
buffer: []const u8, | ||
overlapped: ?*windows.OVERLAPPED, | ||
) windows.WriteFileError!?usize { | ||
var written: windows.DWORD = 0; | ||
const result: windows.BOOL = windows.kernel32.WriteFile(handle, buffer.ptr, @intCast(windows.DWORD, buffer.len), &written, overlapped); | ||
if (result == windows.FALSE) { | ||
const err = windows.kernel32.GetLastError(); | ||
return switch (err) { | ||
windows.Win32Error.IO_PENDING => null, | ||
else => windows.unexpectedError(err), | ||
}; | ||
} | ||
|
||
pub fn WriteFileW( | ||
handle: windows.HANDLE, | ||
buffer: []const u8, | ||
overlapped: ?*windows.OVERLAPPED, | ||
) windows.WriteFileError!?usize { | ||
var written: windows.DWORD = 0; | ||
const result: windows.BOOL = kernel32.WriteFile(handle, buffer.ptr, @intCast(windows.DWORD, buffer.len), &written, overlapped); | ||
if (result == windows.FALSE) { | ||
const err = kernel32.GetLastError(); | ||
return switch (err) { | ||
windows.Win32Error.IO_PENDING => null, | ||
else => windows.unexpectedError(err), | ||
}; | ||
return @intCast(usize, written); | ||
} | ||
|
||
return @intCast(usize, written); | ||
} | ||
pub const DeleteFileError = error{} || std.os.UnexpectedError; | ||
|
||
pub const DeleteFileError = error{} || std.os.UnexpectedError; | ||
|
||
pub fn DeleteFileW(name: [*:0]const u16) DeleteFileError!void { | ||
const result: windows.BOOL = kernel32.DeleteFileW(name); | ||
if (result == windows.FALSE) { | ||
const err = kernel32.GetLastError(); | ||
return switch (err) { | ||
else => windows.unexpectedError(err), | ||
}; | ||
pub fn DeleteFile(name: [*:0]const u16) DeleteFileError!void { | ||
const result: windows.BOOL = windows.kernel32.DeleteFileW(name); | ||
if (result == windows.FALSE) { | ||
const err = windows.kernel32.GetLastError(); | ||
return switch (err) { | ||
else => windows.unexpectedError(err), | ||
}; | ||
} | ||
} | ||
} | ||
}; |