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

os: add open_file function

This commit is contained in:
KJ Lawrence
2020-01-21 10:58:47 -05:00
committed by Alexander Medvednikov
parent 5deb29a7c9
commit ae3d84df6b
6 changed files with 135 additions and 34 deletions

View File

@ -2,6 +2,8 @@ module os
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
pub const (
path_separator = '/'
)
@ -14,7 +16,6 @@ const (
fn C.symlink(charptr, charptr) int
pub fn init_os_args(argc int, argv &byteptr) []string {
mut args := []string
for i in 0 .. argc {
@ -23,15 +24,6 @@ pub fn init_os_args(argc int, argv &byteptr) []string {
return args
}
// get_error_msg return error code representation in string.
pub fn get_error_msg(code int) string {
ptr_text := C.strerror(code) // voidptr?
if ptr_text == 0 {
return ''
}
return tos3(ptr_text)
}
pub fn ls(path string) ?[]string {
mut res := []string
dir := C.opendir(path.str)
@ -68,9 +60,8 @@ pub fn is_dir(path string) bool {
}
*/
// open opens a file at the specified and returns back a read-only `File` object
pub fn open(path string) ?File {
mut file := File{}
/*
$if linux {
$if !android {
@ -85,9 +76,8 @@ pub fn open(path string) ?File {
}
}
*/
cpath := path.str
file = File{
cfile: C.fopen(charptr(cpath), 'rb')
file := File{
cfile: C.fopen(charptr(path.str), 'rb')
opened: true
}
if isnil(file.cfile) {
@ -96,10 +86,8 @@ pub fn open(path string) ?File {
return file
}
// create creates a file at a specified location and returns a writable `File` object.
// create creates or opens a file at a specified location and returns a write-only `File` object
pub fn create(path string) ?File {
mut fd := 0
mut file := File{}
/*
// NB: android/termux/bionic is also a kind of linux,
// but linux syscalls there sometimes fail,
@ -123,7 +111,7 @@ pub fn create(path string) ?File {
}
}
*/
file = File{
file := File{
cfile: C.fopen(charptr(path.str), 'wb')
opened: true
}
@ -187,7 +175,7 @@ pub fn mkdir(path string) ?bool {
$if !android {
ret := C.syscall(sys_mkdir, apath.str, 511)
if ret == -1 {
return error(get_error_msg(C.errno))
return error(posix_get_error_msg(C.errno))
}
return true
}
@ -195,7 +183,7 @@ pub fn mkdir(path string) ?bool {
*/
r := C.mkdir(apath.str, 511)
if r == -1 {
return error(get_error_msg(C.errno))
return error(posix_get_error_msg(C.errno))
}
return true
}
@ -231,7 +219,12 @@ pub fn symlink(origin, target string) ?bool {
if res == 0 {
return true
}
return error(get_error_msg(C.errno))
return error(posix_get_error_msg(C.errno))
}
// get_error_msg return error code representation in string.
pub fn get_error_msg(code int) string {
return posix_get_error_msg(code)
}
// convert any value to []byte (LittleEndian) and write it