]> matita.cs.unibo.it Git - helm.git/blobdiff - components/extlib/hExtlib.ml
BIG FAT COMMIT REGARDING COERCIONS:
[helm.git] / components / extlib / hExtlib.ml
index b36a4d89db2630f5d0d192454f706d86fa1054d3..c0364315a62c018923f2c9e76873a35087c15fd2 100644 (file)
@@ -172,6 +172,19 @@ let rec list_findopt f l =
   in
   aux l
 
+let split_nth n l =
+  let rec aux acc n l =
+    match n, l with
+    | 0, _ -> List.rev acc, l
+    | n, [] -> raise (Failure "HExtlib.split_nth")
+    | n, hd :: tl -> aux (hd :: acc) (n - 1) tl in
+  aux [] n l
+
+let list_last l =
+  let l = List.rev l in 
+  try List.hd l with exn -> raise (Failure "HExtlib.list_last")
+;;
+  
 (** {2 File predicates} *)
 
 let is_dir fname =
@@ -314,13 +327,16 @@ let find ?(test = fun _ -> true) path =
 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
 
 let is_dir_empty d =
- let od = Unix.opendir d in
- let rec aux () =
-  let name = Unix.readdir od in
-  if name <> "." && name <> ".." then false else aux () in
- let res = try aux () with End_of_file -> true in
-  Unix.closedir od;
-  res
+ try
+  let od = Unix.opendir d in
+  let rec aux () =
+   let name = Unix.readdir od in
+   if name <> "." && name <> ".." then false else aux () in
+  let res = try aux () with End_of_file -> true in
+   Unix.closedir od;
+   res
+ with
+  Unix.Unix_error _ -> true (* raised by Unix.opendir, we hope :-) *)
 
 let safe_rmdir d = try Unix.rmdir d with Unix.Unix_error _ -> ()