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

os: minor fixes

This commit is contained in:
penguindark 2020-05-22 06:21:11 +02:00 committed by GitHub
parent edabd57e8a
commit 8bf290acf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 68 deletions

View File

@ -77,6 +77,58 @@ pub fn (f File) is_opened() bool {
return f.opened
}
/***************************** Write ops ****************************/
pub fn (mut f File) write(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, s.str, s.len)
return
}
}
*/
C.fputs(s.str, f.cfile)
}
pub fn (mut f File) writeln(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len)
return
}
}
*/
// TODO perf
C.fputs(s.str, f.cfile)
C.fputs('\n', f.cfile)
}
pub fn (mut f File) write_bytes(data voidptr, size int) {
C.fwrite(data, 1, size, f.cfile)
}
pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) {
//$if linux {
//}
//$else {
C.fseek(f.cfile, pos, C.SEEK_SET)
C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_END)
//}
}
/***************************** Read ops ****************************/
// read_bytes reads an amount of bytes from the beginning of the file
pub fn (f &File) read_bytes(size int) []byte {
return f.read_bytes_at(size, 0)
@ -106,6 +158,7 @@ pub fn read_bytes(path string) ?[]byte {
return res[0..nr_read_elements * fsize]
}
// read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string {
mode := 'rb'
@ -125,6 +178,15 @@ pub fn read_file(path string) ?string {
return string(str,fsize)
}
/***************************** Utility ops ************************/
pub fn (mut f File) flush() {
if !f.opened {
return
}
C.fflush(f.cfile)
}
/***************************** OS ops ************************/
// file_size returns the size of the file located in `path`.
pub fn file_size(path string) int {
mut s := C.stat{}
@ -337,22 +399,7 @@ pub fn open_file(path string, mode string, options ...int) ?File {
}
}
pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) {
//$if linux {
//}
//$else {
C.fseek(f.cfile, pos, C.SEEK_SET)
C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_END)
//}
}
pub fn (mut f File) flush() {
if !f.opened {
return
}
C.fflush(f.cfile)
}
// system starts the specified command, waits for it to complete, and returns its code.
fn vpopen(path string) voidptr {
@ -1324,36 +1371,3 @@ pub fn create(path string) ?File {
opened: true
}
}
pub fn (mut f File) write(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, s.str, s.len)
return
}
}
*/
C.fputs(s.str, f.cfile)
}
pub fn (mut f File) writeln(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len)
return
}
}
*/
// TODO perf
C.fputs(s.str, f.cfile)
C.fputs('\n', f.cfile)
}

View File

@ -137,21 +137,6 @@ pub fn get_error_msg(code int) string {
return posix_get_error_msg(code)
}
// convert any value to []byte (LittleEndian) and write it
// for example if we have write(7, 4), "07 00 00 00" gets written
// write(0x1234, 2) => "34 12"
pub fn (mut f File) write_bytes(data voidptr, size int) {
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, data, 1)
return
}
}
*/
C.fwrite(data, 1, size, f.cfile)
}
pub fn (mut f File) close() {
if !f.opened {
return

View File

@ -302,10 +302,6 @@ pub fn symlink(origin, target string) ?bool {
return error(get_error_msg(int(C.GetLastError())))
}
pub fn (mut f File) write_bytes(data voidptr, size int) {
C.fwrite(data, 1, size, f.cfile)
}
pub fn (mut f File) close() {
if !f.opened {
return

View File

@ -1,7 +1,8 @@
fn test_pointer_arithmetic() {
arr := [1, 2, 3, 4]
unsafe {
mut parr := *int(arr.data)
mut parr := &int(arr.data)
assert 1 == *parr
parr++
assert 2 == *parr
parr++
@ -10,6 +11,7 @@ fn test_pointer_arithmetic() {
}
}
/*
fn test_multi_level_pointer_dereferencing() {
n := 100
pn := &n
@ -22,3 +24,4 @@ fn test_multi_level_pointer_dereferencing() {
}
assert n == 300 // updated by the unsafe pointer manipulation
}
*/