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 }
33 if profiling_enabled 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 unopt = function None -> failwith "unopt: None" | Some v -> v
62 (** {2 String processing} *)
64 let split ?(sep = ' ') s =
65 let pieces = ref [] in
67 match (try Some (String.index_from s idx sep) with Not_found -> None) with
69 pieces := String.sub s idx (pos - idx) :: !pieces;
71 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
77 let rec find_left idx =
79 | ' ' | '\t' | '\r' | '\n' -> find_left (idx + 1)
82 let rec find_right idx =
84 | ' ' | '\t' | '\r' | '\n' -> find_right (idx - 1)
87 let s_len = String.length s in
88 let left, right = find_left 0, find_right (s_len - 1) in
89 String.sub s left (right - left + 1)
91 (** {2 List processing} *)
93 let rec list_uniq = function
96 | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl)
97 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
99 let rec filter_map f =
104 | None -> filter_map f tl
105 | Some v -> v :: filter_map f tl)
107 (** {2 File predicates} *)
111 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
112 with Unix.Unix_error _ -> false
114 let is_regular fname =
116 (Unix.stat fname).Unix.st_kind = Unix.S_REG
117 with Unix.Unix_error _ -> false
120 let components = split ~sep:'/' path in
121 let rec aux where = function
124 let path = where ^ "/" ^ piece in
126 Unix.mkdir path 0o755
128 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
129 | Unix.Unix_error (e,_,_) ->
132 ("Unix.mkdir " ^ path ^ " 0o755 :" ^ (Unix.error_message e))));
137 (** {2 Filesystem} *)
139 let input_file fname =
140 let size = (Unix.stat fname).Unix.st_size in
141 let buf = Buffer.create size in
142 let ic = open_in fname in
143 Buffer.add_channel buf ic size;
149 let buf = Buffer.create size in
150 let s = String.create size in
153 let bytes = input ic s 0 size in
154 if bytes = 0 then raise End_of_file
155 else Buffer.add_substring buf s 0 bytes
157 with End_of_file -> ());
160 let output_file ~filename ~text =
161 let oc = open_out filename in
162 output_string oc text;
165 (** {2 Exception handling} *)
167 let finally at_end f arg =
170 with exn -> at_end (); raise exn