1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
29 let profiling_enabled = true
31 type profiler = { profile : 'a 'b. ('a -> 'b) -> 'a -> 'b }
32 let profile ?(enable = true) =
33 if profiling_enabled && enable then
35 let total = ref 0.0 in
37 let before = Unix.gettimeofday () in
40 let after = Unix.gettimeofday () in
41 total := !total +. (after -. before);
45 let after = Unix.gettimeofday () in
46 total := !total +. (after -. before);
52 ("!! TOTAL TIME SPENT IN " ^ s ^ ": " ^ string_of_float !total));
55 function _ -> { profile = fun f x -> f x }
57 (** {2 Optional values} *)
59 let map_option f = function None -> None | Some v -> Some (f v)
60 let iter_option f = function None -> () | Some v -> f v
61 let unopt = function None -> failwith "unopt: None" | Some v -> v
63 (** {2 String processing} *)
65 let split ?(sep = ' ') s =
66 let pieces = ref [] in
68 match (try Some (String.index_from s idx sep) with Not_found -> None) with
70 pieces := String.sub s idx (pos - idx) :: !pieces;
72 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
78 let rec find_left idx =
80 | ' ' | '\t' | '\r' | '\n' -> find_left (idx + 1)
83 let rec find_right idx =
85 | ' ' | '\t' | '\r' | '\n' -> find_right (idx - 1)
88 let s_len = String.length s in
89 let left, right = find_left 0, find_right (s_len - 1) in
90 String.sub s left (right - left + 1)
92 (** {2 List processing} *)
94 let rec list_uniq = function
97 | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl)
98 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
100 let rec filter_map f =
105 | None -> filter_map f tl
106 | Some v -> v :: filter_map f tl)
108 let list_concat ?(sep = []) =
112 | [ last ] -> List.flatten (List.rev (last :: acc))
113 | hd :: tl -> aux ([sep; hd] @ acc) tl
117 (** {2 File predicates} *)
121 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
122 with Unix.Unix_error _ -> false
124 let is_regular fname =
126 (Unix.stat fname).Unix.st_kind = Unix.S_REG
127 with Unix.Unix_error _ -> false
130 let components = split ~sep:'/' path in
131 let rec aux where = function
134 let path = where ^ "/" ^ piece in
136 Unix.mkdir path 0o755
138 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
139 | Unix.Unix_error (e,_,_) ->
142 ("Unix.mkdir " ^ path ^ " 0o755 :" ^ (Unix.error_message e))));
147 (** {2 Filesystem} *)
149 let input_file fname =
150 let size = (Unix.stat fname).Unix.st_size in
151 let buf = Buffer.create size in
152 let ic = open_in fname in
153 Buffer.add_channel buf ic size;
159 let buf = Buffer.create size in
160 let s = String.create size in
163 let bytes = input ic s 0 size in
164 if bytes = 0 then raise End_of_file
165 else Buffer.add_substring buf s 0 bytes
167 with End_of_file -> ());
170 let output_file ~filename ~text =
171 let oc = open_out filename in
172 output_string oc text;
175 let find ?(test = fun _ -> true) path =
176 let rec aux acc todo =
181 let handle = Unix.opendir path in
183 let matching_files = ref [] in
186 match Unix.readdir handle with
189 let qentry = path ^ "/" ^ entry in
191 if is_dir qentry then
192 dirs := qentry :: !dirs
193 else if test qentry then
194 matching_files := qentry :: !matching_files;
195 with Unix.Unix_error _ -> ())
197 with End_of_file -> Unix.closedir handle);
198 aux (!matching_files @ acc) (!dirs @ tl)
199 with Unix.Unix_error _ -> aux acc tl
203 (** {2 Exception handling} *)
205 let finally at_end f arg =
208 with exn -> at_end (); raise exn