...
This commit is contained in:
24
examples/core/file.nub
Normal file
24
examples/core/file.nub
Normal file
@@ -0,0 +1,24 @@
|
||||
module file
|
||||
|
||||
export func read_text(path: string): ^u8 {
|
||||
let fd = sys::open(path.ptr 0 0o644)
|
||||
if fd < 0 {
|
||||
return ^u8(0) // failed to open
|
||||
}
|
||||
|
||||
let buf_size: u64 = 4096
|
||||
let buf = c::malloc(buf_size) as ^u8
|
||||
if buf == ^u8(0) {
|
||||
return ^u8(0)
|
||||
}
|
||||
|
||||
let bytes_read = sys::read(fd, buf, buf_size)
|
||||
if bytes_read < 0 {
|
||||
c::free(buf)
|
||||
return ^u8(0)
|
||||
}
|
||||
|
||||
buf[bytes_read] = 0
|
||||
|
||||
return buf
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
module core
|
||||
|
||||
extern func puts(text: ^u8)
|
||||
|
||||
export func print(text: string) {
|
||||
puts(text.ptr)
|
||||
sys::write(0 text.ptr text.length)
|
||||
}
|
||||
|
||||
export func println(text: string) {
|
||||
print(text)
|
||||
print("\n")
|
||||
}
|
||||
5
examples/core/sys.nub
Normal file
5
examples/core/sys.nub
Normal file
@@ -0,0 +1,5 @@
|
||||
module sys
|
||||
|
||||
export extern func read(fd: u32 buf: ^u8 count: u64): i64
|
||||
export extern func write(fd: u32 buf: ^u8 count: u64): i64
|
||||
export extern func open(fileName: ^u8 flags: i32 mode: u16): i64
|
||||
Reference in New Issue
Block a user