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

os: implement win_read_string on os.Process (#13428)

This commit is contained in:
Juergen Donnerstag 2022-02-11 12:02:00 +01:00 committed by GitHub
parent e0618f94e8
commit 654bc49d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ fn C.GenerateConsoleCtrlEvent(event u32, pgid u32) bool
fn C.GetModuleHandleA(name &char) HMODULE fn C.GetModuleHandleA(name &char) HMODULE
fn C.GetProcAddress(handle voidptr, procname &byte) voidptr fn C.GetProcAddress(handle voidptr, procname &byte) voidptr
fn C.TerminateProcess(process HANDLE, exit_code u32) bool fn C.TerminateProcess(process HANDLE, exit_code u32) bool
fn C.PeekNamedPipe(hNamedPipe voidptr, lpBuffer voidptr, nBufferSize int, lpBytesRead voidptr, lpTotalBytesAvail voidptr, lpBytesLeftThisMessage voidptr) bool
type FN_NTSuspendResume = fn (voidptr) type FN_NTSuspendResume = fn (voidptr)
@ -174,9 +175,37 @@ fn (mut p Process) win_write_string(idx int, s string) {
} }
fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) { fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) {
panic('WProcess.read_string $idx is not implemented yet') mut wdata := &WProcess(p.wdata)
if wdata == 0 {
return '', 0 return '', 0
} }
mut rhandle := &u32(0)
if idx == 1 {
rhandle = wdata.child_stdout_read
}
if idx == 2 {
rhandle = wdata.child_stderr_read
}
if rhandle == 0 {
return '', 0
}
mut bytes_avail := int(0)
unsafe {
if C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) == false {
return '', 0
}
}
if bytes_avail == 0 {
return '', 0
}
mut bytes_read := int(0)
buf := []byte{len: bytes_avail + 300}
unsafe {
C.ReadFile(rhandle, &buf[0], buf.cap, voidptr(&bytes_read), 0)
}
return buf[..bytes_read].bytestr(), bytes_read
}
fn (mut p Process) win_slurp(idx int) string { fn (mut p Process) win_slurp(idx int) string {
mut wdata := &WProcess(p.wdata) mut wdata := &WProcess(p.wdata)