(* $Id: recode.ml,v 1.1.1.1 2003/08/10 20:12:28 yori Exp $ *) (* Copyright 2003 Yamagata Yoriyuki *) open Camomile let src, dst, srcenc, dstenc, lconv = let src = ref None in let dst = ref None in let srcenc = ref None in let dstenc = ref None in let lconv = ref None in let set_stdio () = if !src = None then src := Some stdin else if !dst = None then dst := Some stdout else raise (Arg.Bad "Too many files") in let set_file s = if !src = None then src := Some (open_in s) else if !dst = None then dst := Some (open_out s) else raise (Arg.Bad "Too many files") in Arg.parse ["-f", Arg.String (fun s -> srcenc := Some (CharEncoding.of_name s)), "Encoding of src"; "-t", Arg.String (fun s -> dstenc := Some (CharEncoding.of_name s)), "Encoding of dst"; "--lf", Arg.Unit (fun () -> lconv := Some `LF), "Converte the line separators to LF"; "--cr", Arg.Unit (fun () -> lconv := Some `CR), "Convert the line separators to CR"; "--crlf", Arg.Unit (fun () -> lconv := Some `CRLF), "Converte the line separators to CRLF"; "--nel", Arg.Unit (fun () -> lconv := Some `NEL), "Convert the line separators to NEL"; "--ls", Arg.Unit (fun () -> lconv := Some `LS), "Convert the line separators to Unicode Line Separator"; "--ps", Arg.Unit (fun () -> lconv := Some `PS), "Convert the line separators to Unicode Paragraph Separator"; "--no-line-conv", Arg.Unit (fun () -> lconv := None), "Leave the line separators unchanged (default)"; "-", Arg.Unit set_stdio, "stdin/out"] set_file "recode -f srcenc -t dstenc src dst :\n read [src] encoded [srcenc] and output the contents to [dst] using\n the encoding [dscenc]. - specifies standard input/output."; match !src, !dst, !srcenc, !dstenc with Some src, Some dst, Some srcenc, Some dstenc -> (src, dst, srcenc, dstenc, !lconv) | _ -> failwith "Missing argument" (* let src_chan = new CharEncoding.in_channel srcenc src *) (* let dst_chan = let outchan = new CharEncoding.out_channel dstenc dst in match lconv with None -> outchan | Some op -> (new ULine.output op outchan :> uchar obj_output_channel) *) let src_chan = let inchan = new CharEncoding.in_channel srcenc src in match lconv with None -> inchan | Some op -> (new ULine.input op inchan :> uchar obj_input_channel) let dst_chan = new CharEncoding.out_channel dstenc dst let () = try while true do let u = src_chan#get in dst_chan#put u done with End_of_file -> dst_chan#close