File: bi_dump.ml

package info (click to toggle)
biniou 1.0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 216 kB
  • sloc: ml: 1,808; makefile: 175
file content (93 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download | duplicates (3)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
open Printf

(*
let split s = Str.split (Str.regexp ",") s
*)

let split s =
  let acc = ref [] in
  let stop = ref (String.length s) in
  for i = !stop - 1 downto 0 do
    if s.[i] = ',' then (
      let start = i + 1 in
      acc := String.sub s start (!stop - start) :: !acc;
      stop := i
    )
  done;
  String.sub s 0 !stop :: !acc


let load_lines accu s =
  let ic = open_in s in
  let l = ref accu in
  (try
     while true do
       l := input_line ic :: List.rev !l
     done
   with End_of_file ->
     close_in ic
  );
  !l

let load ic =
  let buf = Buffer.create 1000 in
  try
    while true do
      Buffer.add_char buf (input_char ic);
    done;
    assert false
  with End_of_file ->
    Buffer.contents buf

let ( // ) = Filename.concat

let default_dict_path () =
  try
    match Sys.os_type with
        "Unix" -> Some (Sys.getenv "HOME" // ".bdump-dict")
      | "Win32" -> Some (Sys.getenv "HOMEPATH" // "_bdump-dict")
      | "Cygwin" -> Some (Sys.getenv "HOME" // ".bdump-dict")
      | _ -> None
  with Not_found ->
    None

let load_dictionary dic_file accu =
  match dic_file with
      None -> accu
    | Some fn ->
        if Sys.file_exists fn then
          try
            load_lines accu fn
          with e ->
            failwith (sprintf "Cannot load dictionary from %S: %s\n%!"
                        fn (Printexc.to_string e))
        else
          accu

let write_uniq oc a =
  if Array.length a > 0 then (
    fprintf oc "%s\n" a.(0);
    ignore (
      Array.fold_left (
        fun last x ->
          if last <> x then
            fprintf oc "%s\n" x;
          x
      ) a.(0) a
    )
  )

let save_dictionary dic_file l =
  match dic_file with
      None -> ()
    | Some fn ->
        let a = Array.of_list l in
        Array.sort String.compare a;
        let oc = open_out fn in
        let finally () = close_out_noerr oc in
        try
          write_uniq oc a;
          finally ()
        with e ->
          finally ();
          raise e