Note
[llpp.git] / utils.ml
blob8b8f8a65eabbd0478632ae0013b7d1718efe8a32
1 module E = struct
2 let s = "";;
3 let b = Bytes.empty;;
4 let a = [||];;
5 end;;
7 type platform = | Punknown | Plinux | Posx | Psun | Pbsd;;
9 let asciilower = let auld = Char.code 'A' - Char.code 'a' in
10 function
11 | ('A'..'Z') as c -> Char.code c - auld |> Char.chr
12 | c -> c
15 let tempfailureretry f a =
16 let rec g () =
17 try f a with Unix.Unix_error (Unix.EINTR, _, _) -> g ()
18 in g ()
21 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
22 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
23 external toutf8 : int -> string = "ml_keysymtoutf8";;
24 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
25 external spawn : string -> (Unix.file_descr * int) list -> int = "ml_spawn";;
26 external platform : unit -> (platform * string array) = "ml_platform";;
28 let now = Unix.gettimeofday;;
29 let platform, uname = platform ();;
30 let dolog fmt = Format.ksprintf prerr_endline fmt;;
32 let exntos = function
33 | Unix.Unix_error (e, s, a) ->
34 Printf.sprintf "%s(%s) : %s (%d)"
35 s a (Unix.error_message e) (Obj.magic e)
36 | exn -> Printexc.to_string exn
39 let error fmt = Printf.kprintf (fun s -> failwith s) fmt;;
41 module IntSet = Set.Make (struct type t = int let compare = (-) end);;
43 let emptystr s = String.length s = 0;;
44 let nonemptystr s = String.length s > 0;;
45 let bound v minv maxv = max minv (min maxv v);;
47 module Opaque :
48 sig
49 type t = private string
50 val of_string : string -> t
51 val to_string : t -> string
52 end
54 struct
55 type t = string
56 let of_string s = s
57 let to_string t = t
58 end
61 let (~<) = Opaque.of_string;;
62 let (~>) = Opaque.to_string;;
64 let int_of_string_with_suffix s =
65 let l = String.length s in
66 let s1, shift =
67 if l > 1
68 then
69 let p = l-1 in
70 match s.[p] with
71 | 'k' | 'K' -> String.sub s 0 p, 10
72 | 'm' | 'M' -> String.sub s 0 p, 20
73 | 'g' | 'G' -> String.sub s 0 p, 30
74 | _ -> s, 0
75 else s, 0
77 let n = int_of_string s1 in
78 let m = n lsl shift in
79 if m < 0 || m < n
80 then error "value too large"
81 else m
84 let string_with_suffix_of_int n =
85 if n = 0
86 then "0"
87 else
88 let units = [(30, "G"); (20, "M"); (10, "K")] in
89 let prettyint n =
90 let rec loop s n =
91 let h = n mod 1000 in
92 let n = n / 1000 in
93 if n = 0
94 then string_of_int h ^ s
95 else (
96 let s = Printf.sprintf "_%03d%s" h s in
97 loop s n
100 loop E.s n
102 let rec find = function
103 | [] -> prettyint n
104 | (shift, suffix) :: rest ->
105 if (n land ((1 lsl shift) - 1)) = 0
106 then prettyint (n lsr shift) ^ suffix
107 else find rest
109 find units
112 let color_of_string s =
113 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
114 (float r /. 255.0, float g /. 255.0, float b /. 255.0)
118 let rgba_of_string s =
119 Scanf.sscanf
120 s "%d/%d/%d/%d" (fun r g b a ->
121 (float r /. 255.0, float g /. 255.0, float b /. 255.0, float a /. 255.0)
125 let color_to_string (r, g, b) =
126 let r = truncate (r *. 255.0)
127 and g = truncate (g *. 255.0)
128 and b = truncate (b *. 255.0) in
129 Printf.sprintf "%d/%d/%d" r g b
132 let rgba_to_string (r, g, b, a) =
133 let r = truncate (r *. 255.0)
134 and g = truncate (g *. 255.0)
135 and b = truncate (b *. 255.0)
136 and a = truncate (a *. 255.0) in
137 Printf.sprintf "%d/%d/%d/%d" r g b a
140 let abspath path =
141 if Filename.is_relative path
142 then
143 let cwd = Sys.getcwd () in
144 if Filename.is_implicit path
145 then Filename.concat cwd path
146 else Filename.concat cwd (Filename.basename path)
147 else
148 path
151 let nindex s c =
152 try String.index s c
153 with Not_found -> -1
156 module Ne = struct
157 let clo fd f =
158 try tempfailureretry Unix.close fd
159 with exn -> f @@ exntos exn
161 end;;
163 let getenvwithdef name def =
164 match Sys.getenv name with
165 | env -> env
166 | exception Not_found -> def
169 let newlinere = Str.regexp "[\r\n]";;
170 let percentsre = Str.regexp "%s";;
171 let whitere = Str.regexp "[ \t]";;
173 let unit () = ();;
175 let addchar s c =
176 let b = Buffer.create (String.length s + 1) in
177 Buffer.add_string b s;
178 Buffer.add_char b c;
179 Buffer.contents b;
182 let btod b = if b then 1 else 0;;
184 let splitatchar s c = let open String in
185 match index s c with
186 | pos -> sub s 0 pos, sub s (pos+1) (length s - pos - 1)
187 | exception Not_found -> s, E.s
190 let boundastep h step =
191 if step < 0
192 then bound step ~-h 0
193 else bound step 0 h
196 let withoutlastutf8 s =
197 let len = String.length s in
198 if len = 0
199 then s
200 else
201 let rec find pos =
202 if pos = 0
203 then pos
204 else
205 let b = Char.code s.[pos] in
206 if b land 0b11000000 = 0b11000000
207 then pos
208 else find (pos-1)
210 let first =
211 if Char.code s.[len-1] land 0x80 = 0
212 then len-1
213 else find (len-1)
215 String.sub s 0 first;
218 let fdcontents fd =
219 let l = 4096 in
220 let b = Buffer.create l in
221 let s = Bytes.create l in
222 let rec loop () =
223 let n = tempfailureretry (Unix.read fd s 0) l in
224 if n = 0
225 then Buffer.contents b
226 else (
227 Buffer.add_subbytes b s 0 n;
228 loop ()
231 loop ()
234 let filecontents path =
235 let fd = Unix.openfile path [Unix.O_RDONLY] 0o0 in
236 match fdcontents fd with
237 | exception exn ->
238 error "failed to read contents of %s: %s" path @@ exntos exn
239 | s ->
240 Ne.clo fd @@ error "failed to close descriptor for %s: %s" path;
244 let getcmdoutput errfun cmd =
245 let reperror fmt = Printf.kprintf errfun fmt in
246 let clofail s e = error "failed to close %s: %s" s e in
247 match Unix.pipe () with
248 | exception exn ->
249 reperror "pipe failed: %s" @@ exntos exn;
251 | (r, w) ->
252 match spawn cmd [r, -1; w, 1] with
253 | exception exn ->
254 reperror "failed to execute %S: %s" cmd @@ exntos exn;
256 | pid ->
257 Ne.clo w @@ clofail "write end of the pipe";
258 let s =
259 match Unix.waitpid [] pid with
260 | exception exn ->
261 reperror "waitpid on %S %d failed: %s" cmd pid @@ exntos exn;
263 | _pid, Unix.WEXITED 0 ->
264 begin
265 match fdcontents r with
266 | exception exn ->
267 reperror "failed to read output of %S: %s" cmd @@ exntos exn;
269 | s ->
270 let l = String.length s in
271 if l > 0 && s.[l-1] = '\n'
272 then String.sub s 0 (l-1)
273 else s
274 end;
275 | _pid, Unix.WEXITED n ->
276 reperror "%S exited with error code %d" cmd n;
278 | _pid, Unix.WSIGNALED n ->
279 reperror "%S was killed with signal %d" cmd n;
281 | _pid, Unix.WSTOPPED n ->
282 reperror "%S was stopped by signal %d" cmd n;
285 Ne.clo r @@ clofail "read end of the pipe";
289 let geturl =
290 let re = Str.regexp {|.*\(\(https?\|ftp\|mailto\|file\)://[^ ]+\).*|} in
291 fun s ->
292 if Str.string_match re s 0
293 then Str.matched_group 1 s
294 else E.s
297 let substratis s pos subs =
298 let subslen = String.length subs in
299 if String.length s - pos >= subslen
300 then
301 let rec cmp i = i = subslen || (s.[pos+i] = subs.[i]) && cmp (i+1)
302 in cmp 0
303 else false
306 let w8 s pos i = Bytes.set s pos (Char.chr (i land 0xff));;
307 let r8 s pos = Char.code (Bytes.get s pos);;
309 let w16 s pos i =
310 w8 s pos i;
311 w8 s (pos+1) (i lsr 8)
314 let w32 s pos i =
315 w16 s pos i;
316 w16 s (pos+2) (i lsr 16)
319 let r16 s pos =
320 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
321 (rb 0) lor ((rb 1) lsl 8)
324 let r16s s pos =
325 let i = r16 s pos in
326 i - ((i land 0x8000) lsl 1)
329 let r32 s pos =
330 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
331 let l = (rb 0) lor ((rb 1) lsl 8)
332 and u = (rb 2) lor ((rb 3) lsl 8) in
333 (u lsl 16) lor l
336 let r32s s pos =
337 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
338 let v0 = rb 0 and v1 = rb 1 and v2 = rb 2 and v3 = rb 3 in
339 let v = v0 lor (v1 lsl 8) lor (v2 lsl 16) lor (v3 lsl 24) in
340 if v3 land 0x80 = 0 then
342 else
343 (v - (1 lsl 32))
346 let vlog fmt = Format.ksprintf ignore fmt;;