builtin,os: use more precise C. declarations for C.fread, C.fwrite and C.qsort

pull/8131/head
Delyan Angelov 2021-01-15 12:15:22 +02:00
parent 80008a40e2
commit 995f27a7c0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 15 additions and 15 deletions

View File

@ -17,7 +17,7 @@ fn C.free(ptr voidptr)
fn C.exit(code int) 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 fn C.sprintf(a ...voidptr) int
@ -59,7 +59,9 @@ fn C.fopen() voidptr
fn C.fileno(voidptr) int 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 fn C.fclose() int
@ -96,8 +98,6 @@ fn C.rmdir() int
fn C.chdir() int fn C.chdir() int
fn C.fread() int
fn C.rewind() int fn C.rewind() int
fn C.stat(charptr) int fn C.stat(charptr) int

View File

@ -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 { if written == 0 && buf.len != 0 {
return error('0 bytes written') return error('0 bytes written')
} }
@ -54,7 +54,7 @@ pub fn (mut f File) writeln(s string) ?int {
} }
*/ */
// TODO perf // 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 { if written == 0 && s.len != 0 {
return error('0 bytes written') 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') return error('file is not opened')
} }
// TODO perf // 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 { if written == 0 && s.len != 0 {
return error('0 bytes written') 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 // write_to implements the RandomWriter interface
pub fn (mut f File) write_to(pos int, buf []byte) ?int { pub fn (mut f File) write_to(pos int, buf []byte) ?int {
C.fseek(f.cfile, pos, C.SEEK_SET) 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) C.fseek(f.cfile, 0, C.SEEK_END)
return res return res
} }
pub fn (mut f File) write_bytes(data voidptr, size int) int { 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 { pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int {
C.fseek(f.cfile, pos, C.SEEK_SET) 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) C.fseek(f.cfile, 0, C.SEEK_END)
return res 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) C.fseek(f.cfile, pos, C.SEEK_SET)
// errno is only set if fread fails, so clear it first to tell // errno is only set if fread fails, so clear it first to tell
C.errno = 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 { if C.errno != 0 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }
@ -139,7 +139,7 @@ pub fn (f &File) read(mut buf []byte) ?int {
return 0 return 0
} }
C.errno = 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 { if C.errno != 0 {
return error(posix_get_error_msg(C.errno)) 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.fseek(f.cfile, pos, C.SEEK_SET)
C.errno = 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 { if C.errno != 0 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }

View File

@ -59,7 +59,7 @@ pub fn read_bytes(path string) ?[]byte {
} }
C.rewind(fp) C.rewind(fp)
mut res := []byte{len: fsize} 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 { if nr_read_elements == 0 && fsize > 0 {
return error('fread failed') return error('fread failed')
} }
@ -88,7 +88,7 @@ pub fn read_file(path string) ?string {
C.rewind(fp) C.rewind(fp)
unsafe { unsafe {
mut str := malloc(fsize + 1) 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 { if nelements == 0 && fsize > 0 {
free(str) free(str)
return error('fread failed') return error('fread failed')