154 lines
5.0 KiB
Ziran
154 lines
5.0 KiB
Ziran
#import, file "../src/app/app_clock_host.zi";
|
|
// Host functions the storage modules call that a test does not provide. It is
|
|
// built as a separate root so that no generated header declares them twice.
|
|
|
|
// The storage code logs through raylib's variadic TraceLog; the test drops
|
|
// the message and ignores the arguments.
|
|
#program_export "TraceLog"
|
|
host_trace_log :: (level: s32, format: *u8, arguments: *void) {
|
|
unused level
|
|
unused arguments
|
|
if format == null { return }
|
|
length: usize = 0
|
|
while format[length] != cast(u8)0 { length += 1 }
|
|
unused HostWrite(2, cast(*void)format, length)
|
|
unused HostWrite(2, cast(*void)*newline[0], 1)
|
|
}
|
|
|
|
newline: [1]u8 = .[10];
|
|
|
|
c :: #system_library "c";
|
|
HostOpen :: (path: *u8, mode: *u8) -> *void #foreign c "fopen";
|
|
HostClose :: (file: *void) -> s32 #foreign c "fclose";
|
|
HostRead :: (data: *void, size: usize, count: usize, file: *void) -> usize #foreign c "fread";
|
|
HostSeek :: (file: *void, offset: s64, origin: s32) -> s32 #foreign c "fseek";
|
|
HostTell :: (file: *void) -> s64 #foreign c "ftell";
|
|
HostAllocate :: (size: usize) -> *void #foreign c "malloc";
|
|
HostRelease :: (memory: *void) #foreign c "free";
|
|
HostWrite :: (descriptor: s32, data: *void, count: usize) -> s64 #foreign c "write";
|
|
|
|
read_mode: [3]u8;
|
|
|
|
// Reads a whole file into memory the caller releases with UnloadFileData.
|
|
#program_export "LoadFileData"
|
|
host_load_file_data :: (path: *u8, size: *s32) -> *u8 {
|
|
if size != null { size.* = 0 }
|
|
if path == null { return null }
|
|
read_mode[0] = cast(u8)114
|
|
read_mode[1] = cast(u8)98
|
|
read_mode[2] = cast(u8)0
|
|
file := HostOpen(path, *read_mode[0])
|
|
if file == null { return null }
|
|
if HostSeek(file, 0, 2) != 0 {
|
|
HostClose(file)
|
|
return null
|
|
}
|
|
length := HostTell(file)
|
|
if length < 0 || length > 2147483647 || HostSeek(file, 0, 0) != 0 {
|
|
HostClose(file)
|
|
return null
|
|
}
|
|
data := cast(*u8)HostAllocate(cast(usize)length + 1)
|
|
if data == null {
|
|
HostClose(file)
|
|
return null
|
|
}
|
|
if cast(s64)HostRead(cast(*void)data, 1, cast(usize)length, file) != length {
|
|
HostRelease(cast(*void)data)
|
|
HostClose(file)
|
|
return null
|
|
}
|
|
data[length] = cast(u8)0
|
|
HostClose(file)
|
|
if size != null { size.* = cast(s32)length }
|
|
return data
|
|
}
|
|
|
|
#program_export "UnloadFileData"
|
|
host_unload_file_data :: (data: *u8) {
|
|
HostRelease(cast(*void)data)
|
|
}
|
|
|
|
// Directory listing for legacy text sessions, as raylib's FilePathList.
|
|
PathList :: struct {
|
|
capacity: u32
|
|
count: u32
|
|
paths: **u8
|
|
}
|
|
|
|
HostPipeOpen :: (command: *u8, mode: *u8) -> *void #foreign c "popen";
|
|
HostPipeClose :: (pipe: *void) -> s32 #foreign c "pclose";
|
|
HostLine :: (buffer: *u8, size: s32, pipe: *void) -> *u8 #foreign c "fgets";
|
|
HostCopy :: (destination: *u8, source: *u8, size: usize) -> *u8 #foreign c "strncpy";
|
|
|
|
command_bytes: [1200]u8;
|
|
line_bytes: [1100]u8;
|
|
pipe_mode: [2]u8;
|
|
|
|
// Lists the regular files under `root` (recursively), one path each.
|
|
#program_export "LoadDirectoryFilesEx"
|
|
host_load_directory_files :: (root: *u8, filter: *u8, recursive: bool) -> PathList {
|
|
unused filter
|
|
unused recursive
|
|
list: PathList = PathList.{0, 0, null}
|
|
if root == null { return list }
|
|
prefix: string = "find \""
|
|
index: s64 = 0
|
|
while index < prefix.count {
|
|
command_bytes[index] = prefix[index]
|
|
index += 1
|
|
}
|
|
position: s64 = 0
|
|
while root[position] != cast(u8)0 && index < 1100 {
|
|
command_bytes[index] = root[position]
|
|
index += 1
|
|
position += 1
|
|
}
|
|
tail: string = "\" -type f 2>/dev/null"
|
|
position = 0
|
|
while position < tail.count {
|
|
command_bytes[index] = tail[position]
|
|
index += 1
|
|
position += 1
|
|
}
|
|
command_bytes[index] = cast(u8)0
|
|
pipe_mode[0] = cast(u8)114
|
|
pipe_mode[1] = cast(u8)0
|
|
pipe := HostPipeOpen(*command_bytes[0], *pipe_mode[0])
|
|
if pipe == null { return list }
|
|
capacity: usize = 4096
|
|
list.paths = cast(**u8)HostAllocate(capacity * cast(usize)8)
|
|
if list.paths == null {
|
|
HostPipeClose(pipe)
|
|
return list
|
|
}
|
|
list.capacity = cast(u32)capacity
|
|
while HostLine(*line_bytes[0], 1100, pipe) != null && cast(usize)list.count < capacity {
|
|
length: s32 = 0
|
|
while line_bytes[length] != cast(u8)0 && line_bytes[length] != cast(u8)10 { length += 1 }
|
|
line_bytes[length] = cast(u8)0
|
|
copy := cast(*u8)HostAllocate(cast(usize)length + 1)
|
|
if copy == null { break }
|
|
unused HostCopy(copy, *line_bytes[0], cast(usize)length + 1)
|
|
list.paths[list.count] = copy
|
|
list.count += 1
|
|
}
|
|
HostPipeClose(pipe)
|
|
return list
|
|
}
|
|
|
|
#program_export "UnloadDirectoryFiles"
|
|
host_unload_directory_files :: (paths: PathList) {
|
|
index: u32 = 0
|
|
while index < paths.count {
|
|
HostRelease(cast(*void)paths.paths[index])
|
|
index += 1
|
|
}
|
|
if paths.paths != null { HostRelease(cast(*void)paths.paths) }
|
|
}
|
|
|
|
#program_export "IsPathFile"
|
|
host_is_path_file :: (path: *u8) -> bool {
|
|
return path != null
|
|
}
|