]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/extlib/hExtlib.ml
added find
[helm.git] / helm / ocaml / extlib / hExtlib.ml
index d3ae69402313132ce3457193296b60ca9b32e5d4..31cf86a75d4007d6a570fbee91418cd22330f8aa 100644 (file)
@@ -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} *)