From d6ddfa124d62d44c1c9e700560cf399212c306f4 Mon Sep 17 00:00:00 2001 From: 0x9ef <43169346+0x9ef@users.noreply.github.com> Date: Thu, 18 Jul 2019 21:21:48 +0300 Subject: [PATCH] Fixed get_error_msg for *nix * Fixed undefined: get_error_msg --- vlib/net/socket_win.v | 20 ++++++++++++++++++++ vlib/os/os.v | 2 -- vlib/os/os_nix.v | 11 ++++++++++- vlib/os/os_win.v | 18 ++++++++++++------ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/vlib/net/socket_win.v b/vlib/net/socket_win.v index b50da976fd..5aad9615ff 100644 --- a/vlib/net/socket_win.v +++ b/vlib/net/socket_win.v @@ -3,3 +3,23 @@ module net #flag -lws2_32 #include #include + +// Ref - https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup +const ( + WSA_V1 = 0x100 // C.MAKEWORD(1, 0) + WSA_V11 = 0x101 // C.MAKEWORD(1, 1) + WSA_V2 = 0x200 // C.MAKEWORD(2, 0) + WSA_V21 = 0x201 // C.MAKEWORD(2, 1) + WSA_V22 = 0x202 // C.MAKEWORD(2, 2) +) + +pub fn socket(family int, stype int, prototype int) Socket { + sockfd := C.socket(family, _type, proto) + s := Socket { + sockfd: sockfd + family: family + _type: _type + proto: proto + } + return s +} diff --git a/vlib/os/os.v b/vlib/os/os.v index 2056d45a89..507a444e78 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -483,8 +483,6 @@ pub fn user_os() string { return 'unknown' } - - // home_dir returns path to user's home directory. pub fn home_dir() string { mut home := os.getenv('HOME') diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 0c0c793e0d..0a5e556ecc 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -4,4 +4,13 @@ module os const ( PathSeparator = '/' -) +) + +// get_error_msg return error code representation in string. +pub fn get_error_msg(code int) string { + _ptr_text := C.strerror(code) // voidptr? + if _ptr_text == 0 { + return '' + } + return tos(_ptr_text, C.strlen(_ptr_text)) +} \ No newline at end of file diff --git a/vlib/os/os_win.v b/vlib/os/os_win.v index d1899dfdfb..9a0296b59c 100644 --- a/vlib/os/os_win.v +++ b/vlib/os/os_win.v @@ -67,7 +67,9 @@ const ( MAX_ERROR_CODE = 15841 // ERROR_API_UNAVAILABLE ) -fn ptr_get_error_message(code u32) voidptr { +// ptr_win_get_error_msg return string (voidptr) +// representation of error, only for windows. +fn ptr_win_get_error_msg(code u32) voidptr { mut buf := voidptr(0) // Check for code overflow if code > u32(MAX_ERROR_CODE) { @@ -81,10 +83,14 @@ fn ptr_get_error_message(code u32) voidptr { return buf } -pub fn get_error_msg(code u32) string { - _ptrdata := ptr_get_error_message(code) - if _ptrdata == voidptr(0) { +// get_error_msg return error code representation in string. +pub fn get_error_msg(code int) string { + if code < 0 { // skip negative return '' } - return tos(_ptrdata, C.strlen(_ptrdata)) -} + _ptr_text := ptr_win_get_error_msg(u32(code)) + if _ptr_text == 0 { // compare with null + return '' + } + return tos(_ptr_text, C.strlen(_ptr_text)) +} \ No newline at end of file