File: compressor.ml

package info (click to toggle)
cpdf 2.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,828 kB
  • sloc: ml: 34,724; makefile: 65; sh: 45
file content (37 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(* compressor <infile> <outfile> [<compress>] substitutes data files specified
   by __DATA:<filename>\n into the <infile> template, writing to <outfile>. The
   data is printed in way which meets OCaml's syntax. It is compressed by zlib.
*)
open Pdfutil

let contents_of_file filename =
  let ch = open_in_bin filename in
  let s = really_input_string ch (in_channel_length ch) in
    close_in ch;
    s

let contents_to_file filename contents =
  let ch = open_out_bin filename in
    output_string ch contents;
    close_out ch

let rec process a = function
  | '_'::'_'::'D'::'A'::'T'::'A'::':'::more ->
      let filename, rest = cleavewhile (neq '\n') more in
      let data = all_but_last (explode (contents_of_file (implode filename))) in
      let compressed = Pdfio.string_of_bytes (Pdfcodec.encode_flate (Pdfio.bytes_of_string (implode data))) in
      let ocaml = explode (Printf.sprintf "%S" compressed) in
        process (rev ocaml @ a) rest
  | h::t -> process (h::a) t
  | [] -> rev a

let go infile outfile compress =
  let indata = explode (contents_of_file infile) in
  let processed = process [] indata in
    contents_to_file outfile (implode processed)

let () =
  match Sys.argv with
  | [|_; infile; outfile|] -> go infile outfile false
  | [|_; infile; outfile; "compress"|] -> go infile outfile true
  | _ -> Printf.eprintf "compressor: unknown command line\n"