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

os: move file logic to file.v (#6013)

This commit is contained in:
Carlos Esquerdo Bernat 2020-08-01 23:07:37 +02:00 committed by GitHub
parent 055117dc5f
commit d71c11577a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 115 deletions

89
vlib/os/file.v Normal file
View File

@ -0,0 +1,89 @@
module os
pub struct File {
cfile voidptr // Using void* instead of FILE*
pub:
fd int
pub mut:
is_opened bool
}
struct FileInfo {
name string
size int
}
[deprecated]
pub fn (f File) is_opened() bool {
eprintln('warning: `file.is_opened()` has been deprecated, use `file.is_opened` instead')
return f.is_opened
}
// **************************** Write ops ***************************
pub fn (mut f File) write(s string) {
if !f.is_opened {
return
}
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, s.str, s.len)
return
}
}
*/
C.fwrite(s.str, s.len, 1, f.cfile)
}
pub fn (mut f File) writeln(s string) {
if !f.is_opened {
return
}
/*
$if linux {
$if !android {
snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len)
return
}
}
*/
// TODO perf
C.fwrite(s.str, s.len, 1, f.cfile)
C.fputs('\n', f.cfile)
}
pub fn (mut f File) write_bytes(data voidptr, size int) int {
return C.fwrite(data, 1, size, f.cfile)
}
pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) int {
C.fseek(f.cfile, pos, C.SEEK_SET)
res := C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_END)
return res
}
// **************************** 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)
}
// read_bytes_at reads an amount of bytes at the given position in the file
pub fn (f &File) read_bytes_at(size, pos int) []byte {
mut arr := [`0`].repeat(size)
C.fseek(f.cfile, pos, C.SEEK_SET)
nreadbytes := C.fread(arr.data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_SET)
return arr[0..nreadbytes]
}
// **************************** Utility ops ***********************
// write any unwritten data in stream's buffer
pub fn (mut f File) flush() {
if !f.is_opened {
return
}
C.fflush(f.cfile)
}

View File

@ -8,91 +8,6 @@ pub const (
max_path_len = 4096
)
pub struct File {
cfile voidptr // Using void* instead of FILE*
pub:
fd int
mut:
opened bool
}
struct FileInfo {
name string
size int
}
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.fwrite(s.str, s.len, 1, 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.fwrite(s.str, s.len, 1, f.cfile)
C.fputs('\n', f.cfile)
}
pub fn (mut f File) write_bytes(data voidptr, size int) int {
return C.fwrite(data, 1, size, f.cfile)
}
pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) int {
//$if linux {
//}
//$else {
C.fseek(f.cfile, pos, C.SEEK_SET)
res := C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_END)
//}
return res
}
/***************************** 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)
}
// read_bytes_at reads an amount of bytes at the given position in the file
pub fn (f &File) read_bytes_at(size, pos int) []byte {
mut arr := [`0`].repeat(size)
C.fseek(f.cfile, pos, C.SEEK_SET)
nreadbytes := C.fread(arr.data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_SET)
return arr[0..nreadbytes]
}
pub fn read_bytes(path string) ?[]byte {
mut fp := vfopen(path, 'rb')
if isnil(fp) {
@ -128,15 +43,6 @@ pub fn read_file(path string) ?string {
}
}
/***************************** 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 {
@ -155,6 +61,7 @@ pub fn file_size(path string) int {
return s.st_size
}
// move files or folders from one path to other
pub fn mv(old, new string) {
$if windows {
C._wrename(old.to_wide(), new.to_wide())
@ -163,6 +70,7 @@ pub fn mv(old, new string) {
}
}
// copies files or folders from one path to other
pub fn cp(old, new string) ? {
$if windows {
w_old := old.replace('/', '\\')
@ -327,7 +235,7 @@ pub fn open_append(path string) ?File {
if isnil(file.cfile) {
return error('failed to create(append) file "$path"')
}
file.opened = true
file.is_opened = true
return file
}
@ -379,7 +287,7 @@ pub fn open_file(path string, mode string, options ...int) ?File {
return File{
cfile: cfile
fd: fd
opened: true
is_opened: true
}
}
@ -594,9 +502,11 @@ pub fn is_executable(path string) bool {
}
$if solaris {
statbuf := C.stat{}
if C.stat(charptr(path.str), &statbuf) != 0 {
return false
}
unsafe {
if C.stat(charptr(path.str), &statbuf) != 0 {
return false
}
}
return (int(statbuf.st_mode) & ( s_ixusr | s_ixgrp | s_ixoth )) != 0
}
return C.access(charptr(path.str), x_ok) != -1
@ -1010,7 +920,9 @@ pub fn executable() string {
mut result := vcalloc(max_path_len)
mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1]
size := max_path_len
C.sysctl(mib.data, 4, result, &size, 0, 0)
unsafe {
C.sysctl(mib.data, 4, result, &size, 0, 0)
}
return string(result)
}
// "Sadly there is no way to get the full path of the executed file in OpenBSD."
@ -1404,7 +1316,7 @@ pub fn open(path string) ?File {
}
return File{
fd: fd
opened: true
is_opened: true
}
}
}
@ -1417,7 +1329,7 @@ pub fn open(path string) ?File {
return File {
cfile: cfile
fd: fd
opened: true
is_opened: true
}
}
@ -1440,7 +1352,7 @@ pub fn create(path string) ?File {
}
file = File{
fd: fd
opened: true
is_opened: true
}
return file
}
@ -1454,7 +1366,7 @@ pub fn create(path string) ?File {
return File {
cfile: cfile
fd: fd
opened: true
is_opened: true
}
}

View File

@ -20,8 +20,6 @@ pub const (
sys_creat = 85
)
/*
// TODO no pub => error
pub fn write(fd int, data voidptr, nbytes int) int {
@ -47,9 +45,4 @@ pub fn malloc(n int) byteptr {
println('malloc($n)')
return mmap(0, n, 3, 4098, //prot_read|prot_write,
-1,0) //map_private|map_anonymous,
}
pub fn free(b byteptr) {
}
*/

View File

@ -171,10 +171,10 @@ pub fn get_error_msg(code int) string {
}
pub fn (mut f File) close() {
if !f.opened {
if !f.is_opened {
return
}
f.opened = false
f.is_opened = false
/*
$if linux {
$if !android {

View File

@ -305,10 +305,10 @@ pub fn symlink(origin, target string) ?bool {
}
pub fn (mut f File) close() {
if !f.opened {
if !f.is_opened {
return
}
f.opened = false
f.is_opened = false
C.fflush(f.cfile)
C.fclose(f.cfile)
}