From 995f27a7c08391f9a230a6ca03ff0247f3e520a1 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 15 Jan 2021 12:15:22 +0200 Subject: [PATCH] builtin,os: use more precise C. declarations for C.fread, C.fwrite and C.qsort --- vlib/builtin/cfns.c.v | 8 ++++---- vlib/os/file.c.v | 18 +++++++++--------- vlib/os/os_c.v | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index 4f22e1e7f6..20f31dc449 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -17,7 +17,7 @@ fn C.free(ptr voidptr) fn C.exit(code int) -fn C.qsort(voidptr, int, int, qsort_callback_func) +fn C.qsort(base voidptr, items size_t, item_size size_t, cb qsort_callback_func) fn C.sprintf(a ...voidptr) int @@ -59,7 +59,9 @@ fn C.fopen() voidptr fn C.fileno(voidptr) int -fn C.fwrite() int +fn C.fread(ptr voidptr, item_size size_t, items size_t, stream &C.FILE) size_t + +fn C.fwrite(ptr voidptr, item_size size_t, items size_t, stream &C.FILE) size_t fn C.fclose() int @@ -96,8 +98,6 @@ fn C.rmdir() int fn C.chdir() int -fn C.fread() int - fn C.rewind() int fn C.stat(charptr) int diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 355cd15729..f6c8e229e5 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -33,7 +33,7 @@ pub fn (mut f File) write(buf []byte) ?int { } } */ - written := C.fwrite(buf.data, buf.len, 1, f.cfile) + written := int(C.fwrite(buf.data, buf.len, 1, f.cfile)) if written == 0 && buf.len != 0 { return error('0 bytes written') } @@ -54,7 +54,7 @@ pub fn (mut f File) writeln(s string) ?int { } */ // TODO perf - written := C.fwrite(s.str, s.len, 1, f.cfile) + written := int(C.fwrite(s.str, s.len, 1, f.cfile)) if written == 0 && s.len != 0 { return error('0 bytes written') } @@ -70,7 +70,7 @@ pub fn (mut f File) write_string(s string) ?int { return error('file is not opened') } // TODO perf - written := C.fwrite(s.str, s.len, 1, f.cfile) + written := int(C.fwrite(s.str, s.len, 1, f.cfile)) if written == 0 && s.len != 0 { return error('0 bytes written') } @@ -80,18 +80,18 @@ pub fn (mut f File) write_string(s string) ?int { // write_to implements the RandomWriter interface pub fn (mut f File) write_to(pos int, buf []byte) ?int { C.fseek(f.cfile, pos, C.SEEK_SET) - res := C.fwrite(buf.data, 1, buf.len, f.cfile) + res := int(C.fwrite(buf.data, 1, buf.len, f.cfile)) C.fseek(f.cfile, 0, C.SEEK_END) return res } pub fn (mut f File) write_bytes(data voidptr, size int) int { - return C.fwrite(data, 1, size, f.cfile) + return int(C.fwrite(data, 1, size, f.cfile)) } pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int { C.fseek(f.cfile, pos, C.SEEK_SET) - res := C.fwrite(data, 1, size, f.cfile) + res := int(C.fwrite(data, 1, size, f.cfile)) C.fseek(f.cfile, 0, C.SEEK_END) return res } @@ -123,7 +123,7 @@ pub fn (f &File) read_bytes_into(pos int, mut buf []byte) ?int { C.fseek(f.cfile, pos, C.SEEK_SET) // errno is only set if fread fails, so clear it first to tell C.errno = 0 - nbytes := C.fread(buf.data, 1, buf.len, f.cfile) + nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile)) if C.errno != 0 { return error(posix_get_error_msg(C.errno)) } @@ -139,7 +139,7 @@ pub fn (f &File) read(mut buf []byte) ?int { return 0 } C.errno = 0 - nbytes := C.fread(buf.data, 1, buf.len, f.cfile) + nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile)) if C.errno != 0 { return error(posix_get_error_msg(C.errno)) } @@ -153,7 +153,7 @@ pub fn (f &File) read_at(pos int, mut buf []byte) ?int { } C.fseek(f.cfile, pos, C.SEEK_SET) C.errno = 0 - nbytes := C.fread(buf.data, 1, buf.len, f.cfile) + nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile)) if C.errno != 0 { return error(posix_get_error_msg(C.errno)) } diff --git a/vlib/os/os_c.v b/vlib/os/os_c.v index 369971617c..6a666cd7be 100644 --- a/vlib/os/os_c.v +++ b/vlib/os/os_c.v @@ -59,7 +59,7 @@ pub fn read_bytes(path string) ?[]byte { } C.rewind(fp) mut res := []byte{len: fsize} - nr_read_elements := C.fread(res.data, fsize, 1, fp) + nr_read_elements := int(C.fread(res.data, fsize, 1, fp)) if nr_read_elements == 0 && fsize > 0 { return error('fread failed') } @@ -88,7 +88,7 @@ pub fn read_file(path string) ?string { C.rewind(fp) unsafe { mut str := malloc(fsize + 1) - nelements := C.fread(str, fsize, 1, fp) + nelements := int(C.fread(str, fsize, 1, fp)) if nelements == 0 && fsize > 0 { free(str) return error('fread failed')