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/.
30 let profiling_enabled = ComponentsConf.profiling
32 let something_profiled = ref false
35 if !something_profiled then
37 (fun _ -> prerr_endline
38 (Printf.sprintf "!! %-39s %6s %9s %9s %9s"
39 "function" "#calls" "total" "max" "average"))
41 let profiling_printings = ref (fun _ -> true)
42 let set_profiling_printings f = profiling_printings := f
44 type profiler = { profile : 'a 'b. ('a -> 'b) -> 'a -> 'b }
45 let profile ?(enable = true) s =
46 if profiling_enabled && enable then
47 let total = ref 0.0 in
51 let before = Unix.gettimeofday () in
55 let after = Unix.gettimeofday () in
56 let delta = after -. before in
57 total := !total +. delta;
58 if delta > !max then max := delta;
62 let after = Unix.gettimeofday () in
63 let delta = after -. before in
64 total := !total +. delta;
65 if delta > !max then max := delta;
70 if !profiling_printings s && !total <> 0. then
72 something_profiled := true;
74 (Printf.sprintf "!! %-39s %6d %9.4f %9.4f %9.4f"
75 s !calls !total !max (!total /. (float_of_int !calls)))
79 { profile = fun f x -> f x }
81 (** {2 Optional values} *)
83 let map_option f = function None -> None | Some v -> Some (f v)
84 let iter_option f = function None -> () | Some v -> f v
85 let unopt = function None -> failwith "unopt: None" | Some v -> v
87 (** {2 String processing} *)
89 let split ?(sep = ' ') s =
90 let pieces = ref [] in
92 match (try Some (String.index_from s idx sep) with Not_found -> None) with
94 pieces := String.sub s idx (pos - idx) :: !pieces;
96 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
102 let rec find_left idx =
104 | ' ' | '\t' | '\r' | '\n' -> find_left (idx + 1)
107 let rec find_right idx =
109 | ' ' | '\t' | '\r' | '\n' -> find_right (idx - 1)
112 let s_len = String.length s in
113 let left, right = find_left 0, find_right (s_len - 1) in
114 String.sub s left (right - left + 1)
116 (** {2 Char processing} *)
119 let code = Char.code c in
120 (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
123 let code = Char.code c in
124 code >= 48 && code <= 57
127 let code = Char.code c in
128 code = 9 || code = 10 || code = 13 || code = 32
130 let is_alphanum c = is_alpha c || is_digit c
132 (** {2 List processing} *)
134 let rec list_uniq ?(eq=(=)) = function
137 | h1::h2::tl when eq h1 h2 -> list_uniq ~eq (h2 :: tl)
138 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq ~eq tl
140 let rec filter_map f =
145 | None -> filter_map f tl
146 | Some v -> v :: filter_map f tl)
148 let list_concat ?(sep = []) =
152 | [ last ] -> List.flatten (List.rev (last :: acc))
153 | hd :: tl -> aux ([sep; hd] @ acc) tl
157 let rec list_findopt f l =
158 let rec aux = function
163 | Some _ as rc -> rc)
167 (** {2 File predicates} *)
171 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
172 with Unix.Unix_error _ -> false
174 let is_regular fname =
176 (Unix.stat fname).Unix.st_kind = Unix.S_REG
177 with Unix.Unix_error _ -> false
180 let components = split ~sep:'/' path in
181 let rec aux where = function
185 if where = "" then piece else where ^ "/" ^ piece in
187 Unix.mkdir path 0o755
189 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
190 | Unix.Unix_error (e,_,_) ->
193 ("Unix.mkdir " ^ path ^ " 0o755 :" ^ (Unix.error_message e))));
196 let where = if path.[0] = '/' then "/" else "" in
199 (** {2 Filesystem} *)
201 let input_file fname =
202 let size = (Unix.stat fname).Unix.st_size in
203 let buf = Buffer.create size in
204 let ic = open_in fname in
205 Buffer.add_channel buf ic size;
211 let buf = Buffer.create size in
212 let s = String.create size in
215 let bytes = input ic s 0 size in
216 if bytes = 0 then raise End_of_file
217 else Buffer.add_substring buf s 0 bytes
219 with End_of_file -> ());
222 let output_file ~filename ~text =
223 let oc = open_out filename in
224 output_string oc text;
228 let len = String.length s in
229 let buf = Buffer.create 0 in
233 if Buffer.length buf > 0
234 then List.rev (Buffer.contents buf :: acc)
237 if is_blank s.[i] then
238 if Buffer.length buf > 0 then begin
239 let s = Buffer.contents buf in
241 aux (s :: acc) (i + 1)
245 Buffer.add_char buf s.[i];
252 (* Rules: * "~name" -> home dir of "name"
253 * "~" -> value of $HOME if defined, home dir of the current user otherwise *)
255 let get_home login = (Unix.getpwnam login).Unix.pw_dir in
257 let len = String.length s in
258 if len > 0 && s.[0] = '~' then begin
259 let login_len = ref 1 in
260 while !login_len < len && is_alphanum (s.[!login_len]) do
263 let login = String.sub s 1 (!login_len - 1) in
267 try Sys.getenv "HOME" with Not_found -> get_home (Unix.getlogin ())
271 home ^ String.sub s !login_len (len - !login_len)
272 with Not_found | Invalid_argument _ -> s
276 String.concat " " (List.map expand_one (blank_split s))
278 let find ?(test = fun _ -> true) path =
279 let rec aux acc todo =
284 let handle = Unix.opendir path in
286 let matching_files = ref [] in
289 match Unix.readdir handle with
292 let qentry = path ^ "/" ^ entry in
294 if is_dir qentry then
295 dirs := qentry :: !dirs
296 else if test qentry then
297 matching_files := qentry :: !matching_files;
298 with Unix.Unix_error _ -> ())
300 with End_of_file -> Unix.closedir handle);
301 aux (!matching_files @ acc) (!dirs @ tl)
302 with Unix.Unix_error _ -> aux acc tl
306 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
309 let od = Unix.opendir d in
311 let name = Unix.readdir od in
312 if name <> "." && name <> ".." then false else aux () in
313 let res = try aux () with End_of_file -> true in
317 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()
319 let rec rmdir_descend d =
320 if is_dir_empty d then
323 rmdir_descend (Filename.dirname d)
327 (** {2 Exception handling} *)
329 let finally at_end f arg =
332 with exn -> at_end (); raise exn
337 (** {2 Localized exceptions } *)
339 exception Localized of Token.flocation * exn
341 let loc_of_floc = function
342 | { Lexing.pos_cnum = loc_begin }, { Lexing.pos_cnum = loc_end } ->
345 let floc_of_loc (loc_begin, loc_end) =
347 { Lexing.pos_fname = ""; Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
348 Lexing.pos_cnum = loc_begin }
350 let floc_end = { floc_begin with Lexing.pos_cnum = loc_end } in
351 (floc_begin, floc_end)
353 let dummy_floc = floc_of_loc (-1, -1)
355 let raise_localized_exception ~offset floc exn =
356 let (x, y) = loc_of_floc floc in
357 let x = offset + x in
358 let y = offset + y in
359 let flocb,floce = floc in
361 { flocb with Lexing.pos_cnum = x }, { floce with Lexing.pos_cnum = y }
363 raise (Localized (floc, exn))