X-Git-Url: https://git.xandkar.net/?p=dups.git;a=blobdiff_plain;f=dups.ml;h=84c070722b4226a11403da85d665b2159041f684;hp=a1dca83c2e923745d5cf821f3654c0423dbe0a0b;hb=7b7a6b7f7790d7d1316f625e16c5ae79292cf32b;hpb=1f130f74d08198f0c800ae84e1fbb4b94695e426 diff --git a/dups.ml b/dups.ml index a1dca83..84c0707 100644 --- a/dups.ml +++ b/dups.ml @@ -35,7 +35,7 @@ end = struct ) end -module Directory : sig +module Directory_tree : sig val find_files : string -> string Stream.t end = struct let find_files root = @@ -58,25 +58,16 @@ end = struct () ) in - let next_dir () = - match Queue.take dirs with - | exception Queue.Empty -> - () - | dir -> - explore dir - in - let next_file () = - match Queue.take files with - | exception Queue.Empty -> - None - | file_path -> - Some file_path - in explore root; - Stream.create (fun () -> - next_dir (); - next_file () - ) + let rec next () = + match Queue.is_empty files, Queue.is_empty dirs with + | false, _ -> Some (Queue.take files) + | true , true -> None + | true , false -> + explore (Queue.take dirs); + next () + in + Stream.create next end type input = @@ -87,7 +78,7 @@ let main input = let paths = match input with | Paths_on_stdin -> In_channel.lines stdin - | Root_path root -> Directory.find_files root + | Root_path root -> Directory_tree.find_files root in let paths_by_digest = Hashtbl.create 1_000_000 in let path_count = ref 0 in @@ -121,12 +112,14 @@ let main input = let () = let input = ref Paths_on_stdin in - Arg.parse [] (fun path -> - if Sys.file_exists path then - input := Root_path path - else begin - eprintf "File does not exist: %S\n%!" path; - exit 1 - end - ) ""; + Arg.parse + [] + (function + | path when Sys.file_exists path -> + input := Root_path path + | path -> + eprintf "File does not exist: %S\n%!" path; + exit 1 + ) + ""; main !input