(* encodetext.ml - Don Yang (uguu.org) Given text data in stdin, generate BF program that will output the same text data. 09/03/04 *) (* Open first argument for input if available, otherwise use stdin *) let input () = if Array.length Sys.argv > 1 then open_in_bin Sys.argv.(1) else stdin;; (* Open second argument for input if available, otherwise use stdout *) let output () = if Array.length Sys.argv > 2 then open_out_bin Sys.argv.(2) else stdout;; (* Output character difference *) let diff x c = ( if (snd x) > c then (output_string (fst x) ((String.make ((snd x) - c) '-') ^ ".")) else if (snd x) < c then (output_string (fst x) ((String.make (c - (snd x)) '+') ^ ".")) else (output_char (fst x) '.') ); (fst x, c);; (* Parse file character by character *) let rec parse_txt infile x = let (d, eof) = try (diff x (input_byte infile), 0) with End_of_file -> (x, 1) in if eof != 0 then output_char (fst x) '\n' else parse_txt infile d;; (* Program entry point *) let infile = input() in let outfile = output() in parse_txt infile (outfile, 0); close_out outfile; close_in infile;;