X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=helm%2Focaml%2Fextlib%2FhExtlib.ml;h=9f4f2d9c904bef51d1c3121eeeec90aad4ca050e;hb=fb94e5a71be508516514dfe50528ccfb3cd2da91;hp=d3ae69402313132ce3457193296b60ca9b32e5d4;hpb=0f553c84d45f6f57977ae200291fd38882655ad3;p=helm.git diff --git a/helm/ocaml/extlib/hExtlib.ml b/helm/ocaml/extlib/hExtlib.ml index d3ae69402..9f4f2d9c9 100644 --- a/helm/ocaml/extlib/hExtlib.ml +++ b/helm/ocaml/extlib/hExtlib.ml @@ -57,6 +57,7 @@ let profile = (** {2 Optional values} *) let map_option f = function None -> None | Some v -> Some (f v) +let iter_option f = function None -> () | Some v -> f v let unopt = function None -> failwith "unopt: None" | Some v -> v (** {2 String processing} *) @@ -104,6 +105,15 @@ let rec filter_map f = | None -> filter_map f tl | Some v -> v :: filter_map f tl) +let list_concat ?(sep = []) = + let rec aux acc = + function + | [] -> [] + | [ last ] -> List.flatten (List.rev (last :: acc)) + | hd :: tl -> aux ([sep; hd] @ acc) tl + in + aux [] + (** {2 File predicates} *) let is_dir fname = @@ -161,6 +171,34 @@ let output_file ~filename ~text = let oc = open_out filename in output_string oc text; close_out oc + +let find ?(test = fun _ -> true) path = + let rec aux acc todo = + match todo with + | [] -> acc + | path :: tl -> + try + let handle = Unix.opendir path in + let dirs = ref [] in + let matching_files = ref [] in + (try + while true do + match Unix.readdir handle with + | "." | ".." -> () + | entry -> + let qentry = path ^ "/" ^ entry in + (try + if is_dir qentry then + dirs := qentry :: !dirs + else if test qentry then + matching_files := qentry :: !matching_files; + with Unix.Unix_error _ -> ()) + done + with End_of_file -> Unix.closedir handle); + aux (!matching_files @ acc) (!dirs @ tl) + with Unix.Unix_error _ -> aux acc tl + in + aux [] [path] (** {2 Exception handling} *)