add load from file
This commit is contained in:
parent
8ce34a1a4b
commit
4746d0f897
|
@ -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)
|
Loading…
Reference in New Issue