package line_oriented

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file line_oriented.ml

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

module L = BatList

type filename = string

let with_in_file fn f =
  let input = open_in_bin fn in
  let res = f input in
  close_in input;
  res

let with_out_file fn f =
  let output = open_out_bin fn in
  let res = f output in
  close_out output;
  res

let with_infile_outfile in_fn out_fn f =
  with_in_file in_fn (fun input ->
      with_out_file out_fn (fun output ->
          f input output
        )
    )

let lines_of_file fn =
  with_in_file fn (fun input ->
      let res, exn = L.unfold_exc (fun () -> input_line input) in
      if exn = End_of_file then res
      else raise exn
    )

let rev_lines_of_file fn =
  with_in_file fn (fun input ->
      let res = ref [] in
      (try
         while true do
           res := (input_line input) :: !res
         done
       with End_of_file -> ()
      );
      !res
    )

let win_EOL = "\r\n"
let unix_EOL = '\n'

let output_EOL =
  if Sys.os_type = "Win32" then
    (fun out -> output_string out win_EOL)
  else
    (fun out -> output_char out unix_EOL)

let append_EOL =
  if Sys.os_type = "Win32" then
    (fun buff -> Buffer.add_string buff win_EOL)
  else
    (fun buff -> Buffer.add_char buff unix_EOL)

let to_string fn =
  let size =
    let stats = Unix.stat fn in
    stats.st_size in
  let buff = Buffer.create size in
  with_in_file fn (fun input ->
      try
        while true do
          let line = input_line input in
          Buffer.add_string buff line;
          append_EOL buff
        done;
        assert(false)
      with End_of_file -> Buffer.contents buff
    )

let lines_to_file fn lines =
  with_out_file fn (fun out ->
      L.iter (fun line ->
          output_string out line;
          output_EOL out
        ) lines
    )

let iter fn f =
  with_in_file fn (fun input ->
      try
        while true do
          f (input_line input)
        done
      with End_of_file -> ()
    )

let iteri fn f =
  let i = ref 0 in
  let g x =
    let y = f !i x in
    incr i;
    y in
  iter fn g

let map fn f =
  with_in_file fn (fun input ->
      let res, exn = L.unfold_exc (fun () -> f (input_line input)) in
      if exn = End_of_file then res
      else raise exn
    )

let fold fn f init =
  with_in_file fn (fun input ->
      let acc = ref init in
      (try
         while true do
           acc := f !acc (input_line input)
         done
       with End_of_file -> ()
      );
      !acc
    )

let rev_map fn f =
  let res = ref [] in
  let g line =
    res := (f line) :: !res in
  iter fn g;
  !res

let mapi fn f =
  with_in_file fn (fun input ->
      let i = ref 0 in
      let res, exn =
        L.unfold_exc (fun () ->
            let curr = f !i (input_line input) in
            incr i;
            curr
          ) in
      if exn = End_of_file then res
      else raise exn
    )

let list_rev_filter p l =
  let rec loop acc = function
    | [] -> acc
    | x :: xs ->
      loop
        (if p x then x :: acc else acc)
        xs in
  loop [] l

let filter fn p =
  list_rev_filter p (rev_lines_of_file fn)

(* count lines *)
let count fn =
  let count = ref 0 in
  iter fn (fun _line -> incr count);
  !count

(* alias *)
let length = count

(* marshal to file *)
let save fn x =
  with_out_file fn (fun out ->
      Marshal.to_channel out x [Marshal.No_sharing]
    )

(* unmarshal from file *)
let restore fn =
  with_in_file fn Marshal.from_channel
OCaml

Innovation. Community. Security.