(* $Id: usort.ml,v 1.2 2003/08/16 08:06:58 yori Exp $ *) (* Copyright 2003 Yamagata Yoriyuki *) open Camomile exception Error of string let () = Get_enc.setlocale "" let get_locale () = let s = Get_enc.get_locale () in try let i = String.index s '.' in let s1 = String.sub s 0 i in try let i = String.index_from s i '@' in let s2 = String.sub s (i + 1) (String.length s - i) in if s2 = "" then s1 else s1 ^ "_" ^ s2 with Not_found -> s1 with Not_found -> s let enc, lb, locale = let enc = ref (Get_enc.get_enc ()) in let lb = ref `LF in let locale = ref (get_locale ()) in Arg.parse ["--encoding", Arg.String (fun s -> enc := s), "Encoding"; "--lf", Arg.Unit (fun () -> lb := `LF), "Use LF as a line break. (default)"; "--cr", Arg.Unit (fun () -> lb := `CR), "Use CR as a line break."; "--crlf", Arg.Unit (fun () -> lb := `CRLF), "Use CRLF as a line break."; "--nel", Arg.Unit (fun () -> lb := `NEL), "Use NEL as a line break."; "--ls", Arg.Unit (fun () -> lb := `LS), "Use Unicode Line Separator as a line break."; "--ps", Arg.Unit (fun () -> lb := `PS), "Use Unicode Paragraph Separator as a line break."; "--locale", Arg.String (fun s -> locale := s), "Locale";] (fun _ -> raise (Arg.Bad "Too many arguments")) "Usage: usort [options] < input > output"; CharEncoding.of_name !enc, !lb, !locale module UTF8Line = ULine.Make (UTF8) let src = new UTF8Line.input_line (new CharEncoding.in_channel enc stdin) let dst = new UTF8Line.output_line ~sp:lb (new CharEncoding.out_channel enc stdout) module Comp = UCol.Make (UTF8) let count = ref 0 let lines = let lines = ref [] in try while true do incr count; let t = src#get in let key = Comp.sort_key ~locale t in lines := (t, key) :: !lines (* lines := t :: !lines *) done; assert false with CharEncoding.Malformed_code -> Printf.eprintf "Malformed code in the line %d\n" !count; exit 1 | CharEncoding.Out_of_range -> Printf.eprintf "Out of range character in the line %d\n" !count; exit 1 | End_of_file -> src#close; !lines let compare (_, k1) (_, k2) = Pervasives.compare k1 k2 let lines = List.sort compare lines (* let lines = List.sort (Comp.compare ~locale) lines *) let () = List.iter (fun (t, _) -> dst#put t) lines (* let () = List.iter (fun t -> dst#put t) lines *) let () = dst#close let () = exit 0