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 =
| 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
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_" ""
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
+;;
+