-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_csv.ml
49 lines (41 loc) · 1.03 KB
/
util_csv.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
open Lwt
type ('a,'b) columns = {
headers: string list;
make_row: (string list->'b)->'a;
}
let col ~header string_of = {
headers = [header];
make_row = fun ret x -> ret [string_of x];
}
let (^^) x y = {
headers = x.headers @ y.headers;
make_row = fun ret -> x.make_row (fun x_row ->
y.make_row (fun y_row ->
ret (x_row @ y_row)));
}
let headers cols =
cols.headers
let make_row cols =
cols.make_row (fun row -> row)
let with_stream_channel f =
let stream, push = Lwt_stream.create () in
let channel = object
method output bytes ofs len =
for i = ofs to ofs+len-1 do
push (Some (Bytes.get bytes i))
done;
len
method close_out () =
push None
end in
async (fun () ->
finalize
(fun () -> f (Csv.to_out_obj channel))
(fun () -> return (push None)));
stream
let of_string s =
Csv.input_all (Csv.of_string s)
let to_string ll =
let buf = Buffer.create 1024 in
Csv.output_all (Csv.to_buffer buf) ll;
Buffer.contents buf