(* $Id: escape.ml,v 1.1.1.1 2003/08/10 20:12:30 yori Exp $ *) (* Copyright 2003 Yamagata Yoriyuki *) open Camomile exception Error of string let () = Get_enc.setlocale "" let enc = let enc = ref None in Arg.parse ["--encoding", Arg.String (fun s -> enc := Some s), "Encoding"] (fun _ -> raise (Arg.Bad "Too many arguments")) "Usage: escape [--encoding enc] < input > output"; let s = match !enc with Some s -> s | None -> Get_enc.get_enc () in CharEncoding.of_name s let src = new CharEncoding.in_channel enc stdin let dst = new CharEncoding.out_channel enc stdout let count = ref 0 let () = try while true do incr count; let u = src#get in let n = int_of_uchar u in if n >= 0 && n < 0x80 then dst#put u else if n >= 0x80 && n < 0x10000 then let s = Printf.sprintf "\\u%04x" n in String.iter (fun c -> dst#put (UChar.of_char c)) s else let s = Printf.sprintf "\\U%08x" n in String.iter (fun c -> dst#put (UChar.of_char c)) s done; assert false with CharEncoding.Malformed_code -> Printf.eprintf "Malformed code in the location %d\n" !count; exit 1 | CharEncoding.Out_of_range -> Printf.eprintf "Out of range character in the location %d\n" !count; exit 1 | End_of_file -> dst#close; exit 0