add load from file

This commit is contained in:
tali 2024-01-15 14:19:02 -05:00
parent 8ce34a1a4b
commit 4746d0f897
1 changed files with 25 additions and 0 deletions

25
src/asset.ml Normal file
View File

@ -0,0 +1,25 @@
open! Import
include (val Ohlog.sublogs logger "asset")
exception Error of string * string
let load_file path =
trace (fun m -> m "open text file %S" path);
let abspath =
Unix.realpath
(Printf.sprintf "assets/%s" path)
in
let fd =
try Unix.openfile abspath [O_RDONLY] 0
with Unix.Unix_error (ENOENT, _, _) ->
raise (Error (path, "not found"))
in
let len = (Unix.fstat fd).st_size in
let buf = Bytes.create len in
trace (fun m -> m "length=%d" len);
let rec read i =
match Unix.read fd buf i (len - i) with
| 0 -> Unix.close fd; i
| n -> read (i + n)
in
Bytes.sub_string buf 0 (read 0)