1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/os/os_windows.v

192 lines
5.7 KiB
V
Raw Normal View History

module os
#flag -lws2_32
#include <winsock2.h>
2019-10-24 12:36:57 +03:00
pub const (
2019-10-12 22:18:19 +03:00
path_separator = '\\'
2019-09-14 23:48:30 +03:00
)
2019-07-16 02:57:03 +03:00
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
// A handle to an object.
2019-07-14 23:59:25 +03:00
type HANDLE voidptr
2019-08-17 16:17:43 +03:00
// win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
struct Filetime {
2019-08-17 16:17:43 +03:00
dwLowDateTime u32
dwHighDateTime u32
}
// win: WIN32_FIND_DATA
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw
struct Win32finddata {
2019-08-17 16:17:43 +03:00
mut:
dwFileAttributes u32
2019-09-29 04:16:15 +03:00
ftCreationTime Filetime
ftLastAccessTime Filetime
ftLastWriteTime Filetime
2019-08-17 16:17:43 +03:00
nFileSizeHigh u32
nFileSizeLow u32
dwReserved0 u32
dwReserved1 u32
2019-09-14 23:48:30 +03:00
cFileName [260]u16 // MAX_PATH = 260
2019-08-17 16:17:43 +03:00
cAlternateFileName [14]u16 // 14
dwFileType u32
dwCreatorType u32
wFinderFlags u16
}
2019-09-14 23:48:30 +03:00
fn init_os_args(argc int, argv &byteptr) []string {
mut args := []string
mut args_list := &voidptr(0)
mut args_count := 0
args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count)
for i := 0; i < args_count; i++ {
args << string_from_wide(&u16(args_list[i]))
}
C.LocalFree(args_list)
return args
}
2019-08-17 16:17:43 +03:00
2019-10-17 14:30:05 +03:00
pub fn ls(path string) ?[]string {
mut find_file_data := Win32finddata{}
2019-08-16 15:05:11 +03:00
mut dir_files := []string
// We can also check if the handle is valid. but using dir_exists instead
// h_find_dir := C.FindFirstFile(path.str, &find_file_data)
// if (INVALID_HANDLE_VALUE == h_find_dir) {
// return dir_files
// }
// C.FindClose(h_find_dir)
if !dir_exists(path) {
2019-11-02 22:37:29 +03:00
return error('ls() couldnt open dir "$path": directory does not exist')
2019-08-16 15:05:11 +03:00
}
// NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR)
// we need to add files to path eg. c:\windows\*.dll or :\windows\*
2019-09-14 23:48:30 +03:00
path_files := '$path\\*'
2019-08-16 15:05:11 +03:00
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
// we should use FindFirstFileW and FindNextFileW
h_find_files := C.FindFirstFile(path_files.to_wide(), &find_file_data)
first_filename := string_from_wide(&u16(find_file_data.cFileName))
if first_filename != '.' && first_filename != '..' {
dir_files << first_filename
}
for C.FindNextFile(h_find_files, &find_file_data) {
filename := string_from_wide(&u16(find_file_data.cFileName))
if filename != '.' && filename != '..' {
dir_files << filename.clone()
}
}
C.FindClose(h_find_files)
return dir_files
2019-09-14 23:48:30 +03:00
}
2019-08-16 15:05:11 +03:00
2019-08-17 16:17:43 +03:00
pub fn dir_exists(path string) bool {
_path := path.replace('/', '\\')
attr := int(C.GetFileAttributes(_path.to_wide()))
if attr == C.INVALID_FILE_ATTRIBUTES {
return false
}
if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
return true
}
return false
2019-09-14 23:48:30 +03:00
}
2019-08-17 16:17:43 +03:00
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) {
_path := path.replace('/', '\\')
// Windows doesnt recursively create the folders
// so we need to help it out here
if _path.last_index('\\') != -1 {
mkdir(_path.all_before_last('\\'))
}
C.CreateDirectory(_path.to_wide(), 0)
2019-09-14 23:48:30 +03:00
}
2019-08-17 16:17:43 +03:00
2019-07-14 23:59:25 +03:00
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
pub fn get_file_handle(path string) HANDLE {
mode := 'rb'
2019-07-24 13:16:45 +03:00
_fd := C._wfopen(path.to_wide(), mode.to_wide())
2019-07-14 23:59:25 +03:00
if _fd == 0 {
return HANDLE(INVALID_HANDLE_VALUE)
}
2019-07-14 23:59:25 +03:00
_handle := C._get_osfhandle(C._fileno(_fd)) // CreateFile? - hah, no -_-
return _handle
}
2019-07-14 23:59:25 +03:00
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea
2019-09-14 23:48:30 +03:00
// get_module_filename retrieves the fully qualified path for the file that contains the specified module.
2019-07-14 23:59:25 +03:00
// The module must have been loaded by the current process.
pub fn get_module_filename(handle HANDLE) ?string {
2019-09-14 23:48:30 +03:00
mut sz := int(4096) // Optimized length
2019-07-24 13:16:45 +03:00
mut buf := &u16(malloc(4096))
2019-07-14 23:59:25 +03:00
for {
status := C.GetModuleFileName(handle, &buf, sz)
2019-10-27 09:36:40 +03:00
match status {
SUCCESS {
_filename := string_from_wide2(buf, sz)
return _filename
}
else {
// Must handled with GetLastError and converted by FormatMessage
return error('Cannot get file name from handle')
}
2019-07-14 23:59:25 +03:00
}
}
panic('this should be unreachable') // TODO remove unreachable after loop
2019-07-14 23:59:25 +03:00
}
2019-07-15 20:30:40 +03:00
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea#parameters
const (
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
FORMAT_MESSAGE_FROM_STRING = 0x00000400
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
)
// Ref - winnt.h
const (
SUBLANG_NEUTRAL = 0x00
SUBLANG_DEFAULT = 0x01
LANG_NEUTRAL = (SUBLANG_NEUTRAL)
2019-09-14 23:48:30 +03:00
)
2019-07-15 20:30:40 +03:00
// Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-
const (
MAX_ERROR_CODE = 15841 // ERROR_API_UNAVAILABLE
)
2019-09-14 23:48:30 +03:00
// ptr_win_get_error_msg return string (voidptr)
// representation of error, only for windows.
fn ptr_win_get_error_msg(code u32) voidptr {
2019-07-15 20:30:40 +03:00
mut buf := voidptr(0)
// Check for code overflow
if code > u32(MAX_ERROR_CODE) {
return buf
}
2019-07-15 20:30:40 +03:00
C.FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buf, 0, 0)
return buf
}
// get_error_msg return error code representation in string.
pub fn get_error_msg(code int) string {
if code < 0 { // skip negative
return ''
}
_ptr_text := ptr_win_get_error_msg(u32(code))
if _ptr_text == 0 { // compare with null
return ''
}
2019-10-26 14:58:05 +03:00
return string_from_wide(_ptr_text)
2019-07-29 19:21:36 +03:00
}