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

fix Windows warnings

This commit is contained in:
Nicolas Sauzede
2019-11-16 00:30:50 +01:00
committed by Alexander Medvednikov
parent e577b40743
commit 1dadf9d966
13 changed files with 92 additions and 61 deletions

View File

@ -66,7 +66,7 @@ const (
)
const(
INVALID_HANDLE_VALUE = -1
INVALID_HANDLE_VALUE = voidptr(-1)
)
// https://docs.microsoft.com/en-us/windows/console/setconsolemode

View File

@ -109,7 +109,7 @@ pub fn read_file(path string) ?string {
pub fn file_size(path string) int {
mut s := C.stat{}
$if windows {
C._wstat(path.to_wide(), &s)
C._wstat(path.to_wide(), voidptr(&s))
} $else {
C.stat(*char(path.str), &s)
}
@ -583,13 +583,13 @@ pub fn get_raw_line() string {
buf := &byte(malloc(max_line_chars*2))
if is_atty(0) > 0 {
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := 0
C.ReadConsole(h_input, buf, max_line_chars * 2, &nr_chars, 0)
return string_from_wide2(&u16(buf), nr_chars)
mut nr_chars := u32(0)
C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&nr_chars), 0)
return string_from_wide2(&u16(buf), int(nr_chars))
}
res := int( C.fgetws(buf, max_line_chars, C.stdin ) )
res := C.fgetws(&u16(buf), max_line_chars, C.stdin )
len := int( C.wcslen(&u16(buf)) )
if 0 != res { return string_from_wide2( &u16(buf), len ) }
if !isnil(res) { return string_from_wide2( &u16(buf), len ) }
return ''
} $else {
max := size_t(256)
@ -830,7 +830,9 @@ pub fn realpath(fpath string) string {
mut fullpath := calloc( MAX_PATH )
mut res := 0
$if windows {
res = int( C._fullpath( fullpath, fpath.str, MAX_PATH ) )
// here we want an int==0 if _fullpath failed , in which case
// it would return NULL, and !isnil(NULL) would be false==0
res = int( !isnil(C._fullpath( fullpath, fpath.str, MAX_PATH )) )
}
$else{
if fpath.len != strlen(fpath.str) {
@ -900,19 +902,25 @@ fn C.fork() int
fn C.wait() int
pub fn fork() int {
mut pid := -1
$if !windows {
pid := C.fork()
return pid
pid = C.fork()
}
panic('os.fork not supported in windows') // TODO
$if windows {
panic('os.fork not supported in windows') // TODO
}
return pid
}
pub fn wait() int {
mut pid := -1
$if !windows {
pid := C.wait(0)
return pid
pid = C.wait(0)
}
panic('os.wait not supported in windows') // TODO
$if !windows {
panic('os.wait not supported in windows') // TODO
}
return pid
}
pub fn file_last_mod_unix(path string) int {

View File

@ -78,7 +78,7 @@ 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)
args_list = &voidptr(C.CommandLineToArgvW(C.GetCommandLine(), &args_count))
for i := 0; i < args_count; i++ {
args << string_from_wide(&u16(args_list[i]))
}
@ -103,12 +103,12 @@ pub fn ls(path string) ?[]string {
path_files := '$path\\*'
// 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)
h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&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) {
for C.FindNextFile(h_find_files, voidptr(&find_file_data)) {
filename := string_from_wide(&u16(find_file_data.cFileName))
if filename != '.' && filename != '..' {
dir_files << filename.clone()
@ -120,11 +120,11 @@ pub fn ls(path string) ?[]string {
pub fn dir_exists(path string) bool {
_path := path.replace('/', '\\')
attr := int(C.GetFileAttributes(_path.to_wide()))
if attr == C.INVALID_FILE_ATTRIBUTES {
attr := C.GetFileAttributesW(_path.to_wide())
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) {
return false
}
if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
return true
}
return false
@ -149,7 +149,7 @@ pub fn get_file_handle(path string) HANDLE {
if _fd == 0 {
return HANDLE(INVALID_HANDLE_VALUE)
}
_handle := C._get_osfhandle(C._fileno(_fd)) // CreateFile? - hah, no -_-
_handle := HANDLE(C._get_osfhandle(C._fileno(_fd))) // CreateFile? - hah, no -_-
return _handle
}
@ -160,7 +160,7 @@ pub fn get_module_filename(handle HANDLE) ?string {
mut sz := int(4096) // Optimized length
mut buf := &u16(malloc(4096))
for {
status := C.GetModuleFileName(handle, &buf, sz)
status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz))
match status {
SUCCESS {
_filename := string_from_wide2(buf, sz)
@ -209,7 +209,7 @@ fn ptr_win_get_error_msg(code u32) voidptr {
FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buf, 0, 0)
0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), voidptr(&buf), 0, 0)
return buf
}
@ -237,12 +237,12 @@ pub fn exec(cmd string) ?Result {
sa.nLength = sizeof(C.SECURITY_ATTRIBUTES)
sa.bInheritHandle = true
create_pipe_result := C.CreatePipe(&child_stdout_read, &child_stdout_write, &sa, 0)
create_pipe_result := int(C.CreatePipe(voidptr(&child_stdout_read), voidptr(&child_stdout_write), voidptr(&sa), 0))
if create_pipe_result == 0 {
error_msg := get_error_msg(int(C.GetLastError()))
return error('exec failed (CreatePipe): $error_msg')
}
set_handle_info_result := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT, 0)
set_handle_info_result := int(C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT, 0))
if set_handle_info_result == 0 {
error_msg := get_error_msg(int(C.GetLastError()))
panic('exec failed (SetHandleInformation): $error_msg')
@ -256,8 +256,8 @@ pub fn exec(cmd string) ?Result {
start_info.hStdError = child_stdout_write
start_info.dwFlags = u32(C.STARTF_USESTDHANDLES)
command_line := [32768]u16
C.ExpandEnvironmentStrings(cmd.to_wide(), &command_line, 32768)
create_process_result := C.CreateProcess(0, command_line, 0, 0, C.TRUE, 0, 0, 0, &start_info, &proc_info)
C.ExpandEnvironmentStringsW(cmd.to_wide(), voidptr(&command_line), 32768)
create_process_result := int(C.CreateProcessW(0, command_line, 0, 0, C.TRUE, 0, 0, 0, voidptr(&start_info), voidptr(&proc_info)))
if create_process_result == 0 {
error_msg := get_error_msg(int(C.GetLastError()))
return error('exec failed (CreateProcess): $error_msg')
@ -265,23 +265,23 @@ pub fn exec(cmd string) ?Result {
C.CloseHandle(child_stdin)
C.CloseHandle(child_stdout_write)
buf := [1000]byte
mut bytes_read := 0
mut bytes_read := u32(0)
mut read_data := ''
for {
readfile_result := C.ReadFile(child_stdout_read, buf, 1000, &bytes_read, 0)
read_data += tos(buf, bytes_read)
if (readfile_result == 0 || bytes_read == 0) {
readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0)
read_data += tos(buf, int(bytes_read))
if (readfile_result == false || int(bytes_read) == 0) {
break
}
}
read_data = read_data.trim_space()
exit_code := 0
exit_code := u32(0)
C.WaitForSingleObject(proc_info.hProcess, C.INFINITE)
C.GetExitCodeProcess(proc_info.hProcess, &exit_code)
C.GetExitCodeProcess(proc_info.hProcess, voidptr(&exit_code))
C.CloseHandle(proc_info.hProcess)
C.CloseHandle(proc_info.hThread)
return Result {
output: read_data
exit_code: exit_code
exit_code: int(exit_code)
}
}