Blame src/FileUtilCMP.ml

Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
(*  ocaml-fileutils: files and filenames common operations                    *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  Copyright (C) 2003-2014, Sylvain Le Gall                                  *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is free software; you can redistribute it and/or modify it   *)
Packit 9ff65e
(*  under the terms of the GNU Lesser General Public License as published by  *)
Packit 9ff65e
(*  the Free Software Foundation; either version 2.1 of the License, or (at   *)
Packit 9ff65e
(*  your option) any later version, with the OCaml static compilation         *)
Packit 9ff65e
(*  exception.                                                                *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is distributed in the hope that it will be useful, but       *)
Packit 9ff65e
(*  WITHOUT ANY WARRANTY; without even the implied warranty of                *)
Packit 9ff65e
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file         *)
Packit 9ff65e
(*  COPYING for more details.                                                 *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  You should have received a copy of the GNU Lesser General Public License  *)
Packit 9ff65e
(*  along with this library; if not, write to the Free Software Foundation,   *)
Packit 9ff65e
(*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA             *)
Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
Packit 9ff65e
open FileUtilTypes
Packit 9ff65e
open FilePath
Packit 9ff65e
open FileUtilTEST
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let cmp ?(skip1 = 0) fln1 ?(skip2 = 0) fln2 =
Packit 9ff65e
  if (reduce fln1) = (reduce fln2) then
Packit 9ff65e
    None
Packit 9ff65e
  else if (test (And(Is_readable, Is_file)) fln1)
Packit 9ff65e
      && (test (And(Is_readable, Is_file)) fln2) then begin
Packit 9ff65e
    let fd1 = open_in_bin fln1 in
Packit 9ff65e
    let fd2 = open_in_bin fln2 in
Packit 9ff65e
    let clean_fd () =
Packit 9ff65e
      let () = try close_in fd1 with _ -> () in
Packit 9ff65e
      let () = try close_in fd2 with _ -> () in
Packit 9ff65e
        ()
Packit 9ff65e
    in
Packit 9ff65e
Packit 9ff65e
    let test_empty st =
Packit 9ff65e
      try
Packit 9ff65e
        Stream.empty st;
Packit 9ff65e
        true
Packit 9ff65e
      with Stream.Failure ->
Packit 9ff65e
        false
Packit 9ff65e
    in
Packit 9ff65e
Packit 9ff65e
    let _ = seek_in fd1 skip1 in
Packit 9ff65e
    let _ = seek_in fd2 skip2 in
Packit 9ff65e
    let stream1 = Stream.of_channel fd1 in
Packit 9ff65e
    let stream2 = Stream.of_channel fd2 in
Packit 9ff65e
    try
Packit 9ff65e
      begin
Packit 9ff65e
        while ((Stream.next stream1) = (Stream.next stream2)) do
Packit 9ff65e
          ()
Packit 9ff65e
        done;
Packit 9ff65e
        clean_fd ();
Packit 9ff65e
        Some (Stream.count stream1)
Packit 9ff65e
      end
Packit 9ff65e
    with
Packit 9ff65e
      | Stream.Failure ->
Packit 9ff65e
          begin
Packit 9ff65e
            match ((test_empty stream1), (test_empty stream2)) with
Packit 9ff65e
                true, true  ->
Packit 9ff65e
                  None
Packit 9ff65e
              | true, false
Packit 9ff65e
              | false, true
Packit 9ff65e
              (* Don't know how this case could be... *)
Packit 9ff65e
              | false, false ->
Packit 9ff65e
                  clean_fd ();
Packit 9ff65e
                  Some (Stream.count stream1)
Packit 9ff65e
          end
Packit 9ff65e
      | e ->
Packit 9ff65e
          clean_fd ();
Packit 9ff65e
          raise e
Packit 9ff65e
  end else
Packit 9ff65e
    Some (-1)
Packit 9ff65e
Packit 9ff65e