]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/http_getter/http_getter_misc.ml
debian release 0.0.4-2:
[helm.git] / helm / http_getter / http_getter_misc.ml
index 1d9c59273f39ce50c504459814b5730771b66510..70bd814ce581648adac236d19710777b643ac11b 100644 (file)
@@ -110,6 +110,9 @@ let http_get_iter_buf ~callback url =
   close_in inchan (* close also outchan, same fd *)
 
 let wget ?output url =
+  debug_print
+    (sprintf "wgetting %s (output: %s)" url
+      (match output with None -> "default" | Some f -> f));
   match url with
   | url when Pcre.pmatch ~rex:file_scheme_RE url -> (* file:// *)
       (let src_fname = Pcre.replace ~rex:file_scheme_RE url in
@@ -130,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
@@ -142,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_" ""