]> matita.cs.unibo.it Git - helm.git/commitdiff
added "output" parameter to gzip and gunzip used to specify target file
authorStefano Zacchiroli <zack@upsilon.cc>
Wed, 12 Mar 2003 18:03:46 +0000 (18:03 +0000)
committerStefano Zacchiroli <zack@upsilon.cc>
Wed, 12 Mar 2003 18:03:46 +0000 (18:03 +0000)
for compression and decompression

helm/http_getter/http_getter_misc.ml
helm/http_getter/http_getter_misc.mli

index 4b82dd8e7e067f73deb2267e1bbbbf2e9ec15dcd..70bd814ce581648adac236d19710777b643ac11b 100644 (file)
@@ -133,9 +133,10 @@ let wget ?output url =
   | scheme -> (* unsupported scheme *)
       failwith ("Http_getter_misc.wget: unsupported scheme: " ^ scheme)
 
-let gzip ?(keep = false) fname =
-  debug_print (sprintf "gzipping %s (keep: %b)" fname keep);
-  let (ic, oc) = (open_in fname, Gzip.open_out (fname ^ ".gz")) in
+let gzip ?(keep = false) ?output fname =
+  let output = match output with None -> fname ^ ".gz" | Some fname -> fname in
+  debug_print (sprintf "gzipping %s (keep: %b, output: %s)" fname keep output);
+  let (ic, oc) = (open_in fname, Gzip.open_out output) in
   let buf = String.create bufsiz in
   (try
     while true do
@@ -145,21 +146,33 @@ let gzip ?(keep = false) fname =
   with End_of_file -> ());
   close_in ic; Gzip.close_out oc;
   if not keep then Sys.remove fname
-
-let gunzip ?(keep = false) fname =
-  debug_print (sprintf "gunzipping %s (keep: %b)" fname keep);
-  let basename = Pcre.replace ~rex:trailing_dot_gz_RE fname in
-  assert (basename <> fname);
-  let (ic, oc) = (Gzip.open_in fname, open_out basename) in
+;;
+
+let gunzip ?(keep = false) ?output fname =
+    (* assumption: given file name ends with ".gz" or output is set *)
+  let output =
+    match output with
+    | None ->
+        if (Pcre.pmatch ~rex:trailing_dot_gz_RE fname) then
+          Pcre.replace ~rex:trailing_dot_gz_RE fname
+        else
+          failwith
+            "Http_getter_misc.gunzip: unable to determine output file name"
+    | Some fname -> fname
+  in
+  debug_print (sprintf "gunzipping %s (keep: %b, output: %s)"
+    fname keep output);
+  let (ic, oc) = (Gzip.open_in fname, open_out output) in
   let buf = String.create bufsiz in
   (try
     while true do
       let bytes = Gzip.input ic buf 0 bufsiz in
-      if bytes = 0 then raise End_of_file else output oc buf 0 bytes
+      if bytes = 0 then raise End_of_file else Pervasives.output oc buf 0 bytes
     done
   with End_of_file -> ());
   Gzip.close_in ic; close_out oc;
   if not keep then Sys.remove fname
+;;
 
 let tempfile () = Filename.temp_file "http_getter_" ""
 
index 946446dc6cc35c3ef6f3b7a377b396f2f424914b..2047cf204b852736782672a1f8152b1a916c46ce 100644 (file)
@@ -47,10 +47,14 @@ val cp: string -> string -> unit
   standard wget rules are used. Additionally this function support also the
   "file://" scheme for file system addressing *)
 val wget: ?output: string -> string -> unit
-  (** gzip frontend, if keep = true original file will be kept *)
-val gzip: ?keep: bool -> string -> unit
-  (** gunzip frontend, if keep = true original file will be kept *)
-val gunzip: ?keep: bool -> string -> unit
+  (** gzip frontend. If keep = true original file will be kept, default is
+  false. output is the file on which gzipped data will be saved, default is
+  given file with an added ".gz" suffix *)
+val gzip: ?keep: bool -> ?output: string -> string -> unit
+  (** gunzip frontend. If keep = true original file will be kept, default is
+  false. output is the file on which gunzipped data will be saved, default is
+  given file name without trailing ".gz" *)
+val gunzip: ?keep: bool -> ?output: string -> string -> unit
   (** tempfile frontend, return the name of created file. A special purpose
   suffix is used (actually "_http_getter" *)
 val tempfile: unit -> string