1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Errors *)
|
|
|
|
|
|
|
|
exception Fatal_error
|
|
|
|
|
|
|
|
let fatal_error msg =
|
|
|
|
prerr_string ">> Fatal error: "; prerr_endline msg; raise Fatal_error
|
|
|
|
|
|
|
|
(* List functions *)
|
|
|
|
|
|
|
|
let rec map_end f l1 l2 =
|
|
|
|
match l1 with
|
|
|
|
[] -> l2
|
|
|
|
| hd::tl -> f hd :: map_end f tl l2
|
|
|
|
|
|
|
|
let rec for_all2 pred l1 l2 =
|
|
|
|
match (l1, l2) with
|
|
|
|
([], []) -> true
|
|
|
|
| (hd1::tl1, hd2::tl2) -> pred hd1 hd2 & for_all2 pred tl1 tl2
|
|
|
|
| (_, _) -> false
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let rec filter pred =
|
|
|
|
function
|
|
|
|
[] ->
|
|
|
|
[]
|
|
|
|
| a::l ->
|
|
|
|
if pred a then
|
|
|
|
a::(filter pred l)
|
|
|
|
else
|
|
|
|
filter pred l
|
|
|
|
|
|
|
|
let rec mem_assq x = function
|
|
|
|
[] -> false
|
|
|
|
| (a,b)::l -> a == x or mem_assq x l
|
|
|
|
|
1996-01-04 04:51:11 -08:00
|
|
|
let rec replicate_list elem n =
|
|
|
|
if n <= 0 then [] else elem :: replicate_list elem (n-1)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* File functions *)
|
|
|
|
|
|
|
|
let find_in_path path name =
|
|
|
|
if Filename.is_absolute name then
|
1995-05-04 05:48:07 -07:00
|
|
|
if Sys.file_exists name then name else raise Not_found
|
1995-05-04 03:15:53 -07:00
|
|
|
else begin
|
|
|
|
let rec try_dir = function
|
|
|
|
[] -> raise Not_found
|
|
|
|
| dir::rem ->
|
|
|
|
let fullname = Filename.concat dir name in
|
1995-05-04 05:48:07 -07:00
|
|
|
if Sys.file_exists fullname then fullname else try_dir rem
|
1995-05-04 03:15:53 -07:00
|
|
|
in try_dir path
|
|
|
|
end
|
|
|
|
|
|
|
|
let remove_file filename =
|
|
|
|
try
|
|
|
|
Sys.remove filename
|
|
|
|
with Sys_error msg ->
|
|
|
|
()
|
|
|
|
|
|
|
|
(* Hashtable functions *)
|
|
|
|
|
|
|
|
let create_hashtable size init =
|
1996-04-22 04:15:41 -07:00
|
|
|
let tbl = Hashtbl.create size in
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
|
|
|
|
tbl
|
|
|
|
|
|
|
|
(* String functions *)
|
|
|
|
|
|
|
|
let capitalize s =
|
|
|
|
let r = String.create (String.length s) in
|
|
|
|
String.blit s 0 r 0 (String.length s);
|
|
|
|
let c = s.[0] in
|
|
|
|
if c >= 'a' & c <= 'z' then r.[0] <- Char.chr(Char.code c - 32);
|
|
|
|
r
|
|
|
|
|
|
|
|
let lowercase s =
|
|
|
|
let r = String.create (String.length s) in
|
|
|
|
String.blit s 0 r 0 (String.length s);
|
|
|
|
let c = s.[0] in
|
|
|
|
if c >= 'A' & c <= 'Z' then r.[0] <- Char.chr(Char.code c + 32);
|
|
|
|
r
|
|
|
|
|
|
|
|
(* File copy *)
|
|
|
|
|
|
|
|
let copy_file ic oc =
|
|
|
|
let buff = String.create 0x1000 in
|
|
|
|
let rec copy () =
|
|
|
|
let n = input ic buff 0 0x1000 in
|
|
|
|
if n = 0 then () else (output oc buff 0 n; copy())
|
|
|
|
in copy()
|
|
|
|
|
|
|
|
let copy_file_chunk ic oc len =
|
|
|
|
let buff = String.create 0x1000 in
|
|
|
|
let rec copy n =
|
|
|
|
if n <= 0 then () else begin
|
|
|
|
let r = input ic buff 0 (min n 0x1000) in
|
|
|
|
if r = 0 then raise End_of_file else (output oc buff 0 r; copy(n-r))
|
|
|
|
end
|
|
|
|
in copy len
|
1995-06-05 06:44:14 -07:00
|
|
|
|
|
|
|
(* Integer operations *)
|
|
|
|
|
|
|
|
let rec log2 n =
|
1995-06-15 01:10:54 -07:00
|
|
|
if n <= 1 then 0 else 1 + log2(n asr 1)
|
1995-06-05 06:44:14 -07:00
|
|
|
|
|
|
|
let align n a =
|
1995-12-19 02:19:38 -08:00
|
|
|
if n >= 0 then (n + a - 1) land (-a) else n land (-a)
|
1995-10-26 09:25:24 -07:00
|
|
|
|
|
|
|
let no_overflow_add a b = (a lxor b) lor (a lxor (lnot (a+b))) < 0
|
|
|
|
|
|
|
|
let no_overflow_sub a b = (a lxor (lnot b)) lor (b lxor (a-b)) < 0
|
|
|
|
|