From 0ab691fe2f45a742c2aa83446a120675910b03d9 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Wed, 5 Oct 2005 16:09:18 +0000 Subject: [PATCH] added find --- helm/ocaml/extlib/hExtlib.ml | 28 ++++++++++++++++++++++++++++ helm/ocaml/extlib/hExtlib.mli | 3 +++ 2 files changed, 31 insertions(+) diff --git a/helm/ocaml/extlib/hExtlib.ml b/helm/ocaml/extlib/hExtlib.ml index d3ae69402..31cf86a75 100644 --- a/helm/ocaml/extlib/hExtlib.ml +++ b/helm/ocaml/extlib/hExtlib.ml @@ -161,6 +161,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} *) diff --git a/helm/ocaml/extlib/hExtlib.mli b/helm/ocaml/extlib/hExtlib.mli index 2565f7861..46ea0b9ce 100644 --- a/helm/ocaml/extlib/hExtlib.mli +++ b/helm/ocaml/extlib/hExtlib.mli @@ -34,6 +34,9 @@ val is_dir: string -> bool (** @return true if file is a directory *) val is_regular: string -> bool (** @return true if file is a regular file *) val mkdir: string -> unit (** create dir and parents. @raise Failure *) + (** find all _files_ matching test under a filesystem root *) +val find: ?test:(string -> bool) -> string -> string list + (** {2 File I/O} *) val input_file: string -> string (** read all the contents of file to string *) -- 2.39.2