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 (* we should use a key in te registry, but we can't see the registry.. *)
30 let profiling_enabled = true
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 () 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 (** {2 File predicates} *)
142 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
143 with Unix.Unix_error _ -> false
145 let is_regular fname =
147 (Unix.stat fname).Unix.st_kind = Unix.S_REG
148 with Unix.Unix_error _ -> false
151 let components = split ~sep:'/' path in
152 let rec aux where = function
155 let path = where ^ "/" ^ piece in
157 Unix.mkdir path 0o755
159 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
160 | Unix.Unix_error (e,_,_) ->
163 ("Unix.mkdir " ^ path ^ " 0o755 :" ^ (Unix.error_message e))));
168 (** {2 Filesystem} *)
170 let input_file fname =
171 let size = (Unix.stat fname).Unix.st_size in
172 let buf = Buffer.create size in
173 let ic = open_in fname in
174 Buffer.add_channel buf ic size;
180 let buf = Buffer.create size in
181 let s = String.create size in
184 let bytes = input ic s 0 size in
185 if bytes = 0 then raise End_of_file
186 else Buffer.add_substring buf s 0 bytes
188 with End_of_file -> ());
191 let output_file ~filename ~text =
192 let oc = open_out filename in
193 output_string oc text;
197 let len = String.length s in
198 let buf = Buffer.create 0 in
202 if Buffer.length buf > 0
203 then List.rev (Buffer.contents buf :: acc)
206 if is_blank s.[i] then
207 if Buffer.length buf > 0 then begin
208 let s = Buffer.contents buf in
210 aux (s :: acc) (i + 1)
214 Buffer.add_char buf s.[i];
221 (* Rules: * "~name" -> home dir of "name"
222 * "~" -> value of $HOME if defined, home dir of the current user otherwise *)
224 let get_home login = (Unix.getpwnam login).Unix.pw_dir in
226 let len = String.length s in
227 if len > 0 && s.[0] = '~' then begin
228 let login_len = ref 1 in
229 while !login_len < len && is_alphanum (s.[!login_len]) do
232 let login = String.sub s 1 (!login_len - 1) in
236 try Sys.getenv "HOME" with Not_found -> get_home (Unix.getlogin ())
240 home ^ String.sub s !login_len (len - !login_len)
241 with Not_found | Invalid_argument _ -> s
245 String.concat " " (List.map expand_one (blank_split s))
247 let find ?(test = fun _ -> true) path =
248 let rec aux acc todo =
253 let handle = Unix.opendir path in
255 let matching_files = ref [] in
258 match Unix.readdir handle with
261 let qentry = path ^ "/" ^ entry in
263 if is_dir qentry then
264 dirs := qentry :: !dirs
265 else if test qentry then
266 matching_files := qentry :: !matching_files;
267 with Unix.Unix_error _ -> ())
269 with End_of_file -> Unix.closedir handle);
270 aux (!matching_files @ acc) (!dirs @ tl)
271 with Unix.Unix_error _ -> aux acc tl
275 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
278 let od = Unix.opendir d in
280 let name = Unix.readdir od in
281 if name <> "." && name <> ".." then false else aux () in
282 let res = try aux () with End_of_file -> true in
286 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()
288 let rec rmdir_descend d =
289 if is_dir_empty d then
292 rmdir_descend (Filename.dirname d)
296 (** {2 Exception handling} *)
298 let finally at_end f arg =
301 with exn -> at_end (); raise exn
306 (** {2 Localized exceptions } *)
308 exception Localized of Token.flocation * exn
310 let loc_of_floc = function
311 | { Lexing.pos_cnum = loc_begin }, { Lexing.pos_cnum = loc_end } ->
314 let raise_localized_exception ~offset floc exn =
315 let (x, y) = loc_of_floc floc in
316 let x = offset + x in
317 let y = offset + y in
318 let flocb,floce = floc in
320 { flocb with Lexing.pos_cnum = x }, { floce with Lexing.pos_cnum = y }
322 raise (Localized (floc, exn))