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 profiling_printings = ref (fun () -> true)
33 let set_profiling_printings f = profiling_printings := f
35 type profiler = { profile : 'a 'b. ('a -> 'b) -> 'a -> 'b }
36 let profile ?(enable = true) =
37 if profiling_enabled && enable then
39 let total = ref 0.0 in
41 let before = Unix.gettimeofday () in
44 let after = Unix.gettimeofday () in
45 total := !total +. (after -. before);
49 let after = Unix.gettimeofday () in
50 total := !total +. (after -. before);
55 if !profiling_printings () && !total <> 0. then
57 ("!! TOTAL TIME SPENT IN " ^ s ^ ": " ^ string_of_float !total));
60 function _ -> { profile = fun f x -> f x }
62 (** {2 Optional values} *)
64 let map_option f = function None -> None | Some v -> Some (f v)
65 let iter_option f = function None -> () | Some v -> f v
66 let unopt = function None -> failwith "unopt: None" | Some v -> v
68 (** {2 String processing} *)
70 let split ?(sep = ' ') s =
71 let pieces = ref [] in
73 match (try Some (String.index_from s idx sep) with Not_found -> None) with
75 pieces := String.sub s idx (pos - idx) :: !pieces;
77 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
83 let rec find_left idx =
85 | ' ' | '\t' | '\r' | '\n' -> find_left (idx + 1)
88 let rec find_right idx =
90 | ' ' | '\t' | '\r' | '\n' -> find_right (idx - 1)
93 let s_len = String.length s in
94 let left, right = find_left 0, find_right (s_len - 1) in
95 String.sub s left (right - left + 1)
97 (** {2 Char processing} *)
100 let code = Char.code c in
101 (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
104 let code = Char.code c in
105 code >= 48 && code <= 57
108 let code = Char.code c in
109 code = 9 || code = 10 || code = 13 || code = 32
111 let is_alphanum c = is_alpha c || is_digit c
113 (** {2 List processing} *)
115 let rec list_uniq ?(eq=(=)) = function
118 | h1::h2::tl when eq h1 h2 -> list_uniq ~eq (h2 :: tl)
119 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq ~eq tl
121 let rec filter_map f =
126 | None -> filter_map f tl
127 | Some v -> v :: filter_map f tl)
129 let list_concat ?(sep = []) =
133 | [ last ] -> List.flatten (List.rev (last :: acc))
134 | hd :: tl -> aux ([sep; hd] @ acc) tl
138 let rec list_findopt f l =
139 let rec aux = function
144 | Some _ as rc -> rc)
148 (** {2 File predicates} *)
152 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
153 with Unix.Unix_error _ -> false
155 let is_regular fname =
157 (Unix.stat fname).Unix.st_kind = Unix.S_REG
158 with Unix.Unix_error _ -> false
161 let components = split ~sep:'/' path in
162 let rec aux where = function
166 if where = "" then piece else where ^ "/" ^ piece in
168 Unix.mkdir path 0o755
170 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
171 | Unix.Unix_error (e,_,_) ->
174 ("Unix.mkdir " ^ path ^ " 0o755 :" ^ (Unix.error_message e))));
177 let where = if path.[0] = '/' then "/" else "" in
180 (** {2 Filesystem} *)
182 let input_file fname =
183 let size = (Unix.stat fname).Unix.st_size in
184 let buf = Buffer.create size in
185 let ic = open_in fname in
186 Buffer.add_channel buf ic size;
192 let buf = Buffer.create size in
193 let s = String.create size in
196 let bytes = input ic s 0 size in
197 if bytes = 0 then raise End_of_file
198 else Buffer.add_substring buf s 0 bytes
200 with End_of_file -> ());
203 let output_file ~filename ~text =
204 let oc = open_out filename in
205 output_string oc text;
209 let len = String.length s in
210 let buf = Buffer.create 0 in
214 if Buffer.length buf > 0
215 then List.rev (Buffer.contents buf :: acc)
218 if is_blank s.[i] then
219 if Buffer.length buf > 0 then begin
220 let s = Buffer.contents buf in
222 aux (s :: acc) (i + 1)
226 Buffer.add_char buf s.[i];
233 (* Rules: * "~name" -> home dir of "name"
234 * "~" -> value of $HOME if defined, home dir of the current user otherwise *)
236 let get_home login = (Unix.getpwnam login).Unix.pw_dir in
238 let len = String.length s in
239 if len > 0 && s.[0] = '~' then begin
240 let login_len = ref 1 in
241 while !login_len < len && is_alphanum (s.[!login_len]) do
244 let login = String.sub s 1 (!login_len - 1) in
248 try Sys.getenv "HOME" with Not_found -> get_home (Unix.getlogin ())
252 home ^ String.sub s !login_len (len - !login_len)
253 with Not_found | Invalid_argument _ -> s
257 String.concat " " (List.map expand_one (blank_split s))
259 let find ?(test = fun _ -> true) path =
260 let rec aux acc todo =
265 let handle = Unix.opendir path in
267 let matching_files = ref [] in
270 match Unix.readdir handle with
273 let qentry = path ^ "/" ^ entry in
275 if is_dir qentry then
276 dirs := qentry :: !dirs
277 else if test qentry then
278 matching_files := qentry :: !matching_files;
279 with Unix.Unix_error _ -> ())
281 with End_of_file -> Unix.closedir handle);
282 aux (!matching_files @ acc) (!dirs @ tl)
283 with Unix.Unix_error _ -> aux acc tl
287 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
290 let od = Unix.opendir d in
292 let name = Unix.readdir od in
293 if name <> "." && name <> ".." then false else aux () in
294 let res = try aux () with End_of_file -> true in
298 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()
300 let rec rmdir_descend d =
301 if is_dir_empty d then
304 rmdir_descend (Filename.dirname d)
308 (** {2 Exception handling} *)
310 let finally at_end f arg =
313 with exn -> at_end (); raise exn
318 (** {2 Localized exceptions } *)
320 exception Localized of Token.flocation * exn
322 let loc_of_floc = function
323 | { Lexing.pos_cnum = loc_begin }, { Lexing.pos_cnum = loc_end } ->
326 let floc_of_loc (loc_begin, loc_end) =
328 { Lexing.pos_fname = ""; Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
329 Lexing.pos_cnum = loc_begin }
331 let floc_end = { floc_begin with Lexing.pos_cnum = loc_end } in
332 (floc_begin, floc_end)
334 let dummy_floc = floc_of_loc (-1, -1)
336 let raise_localized_exception ~offset floc exn =
337 let (x, y) = loc_of_floc floc in
338 let x = offset + x in
339 let y = offset + y in
340 let flocb,floce = floc in
342 { flocb with Lexing.pos_cnum = x }, { floce with Lexing.pos_cnum = y }
344 raise (Localized (floc, exn))