]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/http_getter/http_getter_misc.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / http_getter / http_getter_misc.ml
index 5cb713f206dbf6ad9ea868ac937822cfab7c3da5..7b70a14b1112ce975cc5bc01223d59e3eb4d27bc 100644 (file)
@@ -41,15 +41,14 @@ let tcp_bufsiz = 4096 (* for TCP I/O *)
 
 let fold_file f init fname =
   let inchan = open_in fname in
-  let rec fold_lines' value =
-    try 
-      let line = input_line inchan in 
-      fold_lines' (f value line)
-    with End_of_file -> value
+  let res =
+    try
+      Zack.fold_in f init inchan
+    with e -> close_in inchan; raise e
   in
-  let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
   close_in inchan;
   res
+
 let iter_file f = fold_file (fun _ line -> f line) ()
 
 let hashtbl_sorted_fold f tbl init =
@@ -93,7 +92,7 @@ let http_get_iter_buf ~callback url =
   let (address, port, path) = parse_url url in
   let buf = String.create tcp_bufsiz in
   let (inchan, outchan) = init_socket address port in
-  output_string outchan (sprintf "GET %s HTTP/1.0\r\n\r\n" path);
+  output_string outchan (sprintf "GET %s\r\n" path);
   flush outchan;
   (try
     while true do
@@ -110,6 +109,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 +132,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 +145,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_" ""
 
@@ -215,3 +230,52 @@ let http_get url =
       Some (Http_client.Convenience.http_get url)
     with Http_client.Http_error (code, _) -> None
 
+  (** apply a transformation "string list -> string list" to file lines *)
+let mangle_file ~fname f =
+  let ic = open_in fname in
+  let lines = Zack.input_lines ic in
+  close_in ic;
+  let oc = open_out fname in
+  Zack.output_lines (f lines) oc;
+  close_out oc
+;;
+
+let add_line ~fname ?position line =
+  mangle_file ~fname
+    (fun lines ->
+      match position with
+      | None -> lines @ [line]
+      | Some i ->
+          assert (i >= 0);
+          let rec add_after i = function
+            | (acc, []) -> acc @ [line] (* eof *)
+            | (acc, ((hd::tl) as l)) ->
+                if i = 0 then
+                  acc @ [line] @ l
+                else
+                  add_after (i-1) (acc @ [hd], tl)
+          in
+          add_after i ([], lines))
+;;
+
+let remove_line ~fname position =
+  mangle_file ~fname
+    (fun lines ->
+      assert (position >= 0);
+      let rec remove i = function
+        | (acc, []) -> acc  (* eof *)
+        | (acc, ((hd::tl) as l)) ->
+            if i = 0 then
+              acc @ tl
+            else
+              remove (i-1) (acc @ [hd], tl)
+      in
+      remove position ([], lines))
+;;
+
+let is_blank_line =
+  let blank_line_RE = Pcre.regexp "(^#)|(^\\s*$)" in
+  fun line ->
+    Pcre.pmatch ~rex:blank_line_RE line
+;;
+