mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v test v => v test-compiler
This commit is contained in:
committed by
Alexander Medvednikov
parent
854309a7d8
commit
ec15bfb7d1
31
vlib/os/os.v
31
vlib/os/os.v
@@ -33,6 +33,8 @@ pub const (
|
||||
|
||||
pub struct File {
|
||||
cfile voidptr // Using void* instead of FILE*
|
||||
mut:
|
||||
opened bool
|
||||
}
|
||||
|
||||
struct FileInfo {
|
||||
@@ -68,14 +70,17 @@ fn C.getenv(byteptr) &char
|
||||
fn C.sigaction(int, voidptr, int)
|
||||
|
||||
|
||||
pub fn (f File) is_opened() bool {
|
||||
return f.opened
|
||||
}
|
||||
|
||||
// read_bytes reads an amount of bytes from the beginning of the file
|
||||
pub fn (f File) read_bytes(size int) []byte {
|
||||
pub fn (f mut 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 {
|
||||
pub fn (f mut 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)
|
||||
@@ -281,6 +286,7 @@ pub fn open(path string) ?File {
|
||||
if isnil(file.cfile) {
|
||||
return error('failed to open file "$path"')
|
||||
}
|
||||
file.opened = true
|
||||
return file
|
||||
}
|
||||
|
||||
@@ -302,6 +308,7 @@ pub fn create(path string) ?File {
|
||||
if isnil(file.cfile) {
|
||||
return error('failed to create file "$path"')
|
||||
}
|
||||
file.opened = true
|
||||
return file
|
||||
}
|
||||
|
||||
@@ -322,10 +329,11 @@ pub fn open_append(path string) ?File {
|
||||
if isnil(file.cfile) {
|
||||
return error('failed to create(append) file "$path"')
|
||||
}
|
||||
file.opened = true
|
||||
return file
|
||||
}
|
||||
|
||||
pub fn (f File) write(s string) {
|
||||
pub fn (f mut File) write(s string) {
|
||||
C.fputs(s.str, f.cfile)
|
||||
// C.fwrite(s.str, 1, s.len, f.cfile)
|
||||
}
|
||||
@@ -333,17 +341,18 @@ pub fn (f File) write(s string) {
|
||||
// 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 (f File) write_bytes(data voidptr, size int) {
|
||||
pub fn (f mut File) write_bytes(data voidptr, size int) {
|
||||
C.fwrite(data, 1, size, f.cfile)
|
||||
}
|
||||
|
||||
pub fn (f File) write_bytes_at(data voidptr, size, pos int) {
|
||||
pub fn (f mut File) write_bytes_at(data voidptr, size, pos int) {
|
||||
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 (f File) writeln(s string) {
|
||||
pub fn (f mut File) writeln(s string) {
|
||||
if !f.opened { return }
|
||||
// C.fwrite(s.str, 1, s.len, f.cfile)
|
||||
// ss := s.clone()
|
||||
// TODO perf
|
||||
@@ -352,11 +361,15 @@ pub fn (f File) writeln(s string) {
|
||||
C.fputs('\n', f.cfile)
|
||||
}
|
||||
|
||||
pub fn (f File) flush() {
|
||||
pub fn (f mut File) flush() {
|
||||
if !f.opened { return }
|
||||
C.fflush(f.cfile)
|
||||
}
|
||||
|
||||
pub fn (f File) close() {
|
||||
pub fn (f mut File) close() {
|
||||
if !f.opened { return }
|
||||
f.opened = false
|
||||
C.fflush(f.cfile)
|
||||
C.fclose(f.cfile)
|
||||
}
|
||||
|
||||
@@ -707,7 +720,7 @@ pub fn home_dir() string {
|
||||
|
||||
// write_file writes `text` data to a file in `path`.
|
||||
pub fn write_file(path, text string) {
|
||||
f := os.create(path) or {
|
||||
mut f := os.create(path) or {
|
||||
return
|
||||
}
|
||||
f.write(text)
|
||||
|
||||
@@ -43,7 +43,7 @@ fn test_write_and_read_bytes() {
|
||||
file_name := './byte_reader_writer.tst'
|
||||
payload := [`I`, `D`, `D`, `Q`, `D`]
|
||||
|
||||
file_write := os.create(os.realpath(file_name)) or {
|
||||
mut file_write := os.create(os.realpath(file_name)) or {
|
||||
eprintln('failed to create file $file_name')
|
||||
return
|
||||
}
|
||||
@@ -56,7 +56,7 @@ fn test_write_and_read_bytes() {
|
||||
|
||||
assert payload.len == os.file_size(file_name)
|
||||
|
||||
file_read := os.open(os.realpath(file_name)) or {
|
||||
mut file_read := os.open(os.realpath(file_name)) or {
|
||||
eprintln('failed to open file $file_name')
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user