1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 (* read a whole file *)
22 Unix2.tryopen_read_bin name
(fun chan
->
23 let buf_size = 1024 in
24 let buf = Bytes.create
buf_size in
25 let rec iter buf nb_read
=
26 let buf_size = Bytes.length
buf in
27 let to_read = min
(buf_size - nb_read
) 8192 in
28 let tmp = input chan
buf nb_read
to_read in
30 Bytes.sub_string
buf 0 nb_read
32 let nb_read = nb_read + tmp in
34 if nb_read = buf_size then
35 String2.resize
buf (2 * buf_size)
42 let read_whole_chan chan
=
43 let buf = Buffer.create
1024 in
45 Buffer.add_char
buf (input_char chan
);
51 End_of_file
-> close_in chan
; buf
53 let to_string_alt name
=
54 let chan = open_in_bin name
in
57 let to_copy in_name out_name
=
58 Unix2.tryopen_read_bin in_name
(fun in_chan
->
59 Unix2.tryopen_write_bin out_name
(fun out_chan
->
62 let c = input_byte in_chan
in
63 output_byte out_chan
c;
68 with End_of_file
-> ()))
70 let from_string name s
=
71 Unix2.tryopen_write_bin name
(fun oc
-> output_string oc s
)
74 Unix2.tryopen_read_bin name
(fun ic
->
77 let line = input_line ic
in
80 with End_of_file
-> ())
82 let from_value name s
=
83 Unix2.tryopen_write_bin name
(fun oc
-> output_value oc s
)
86 Unix2.tryopen_read_bin name
(fun ic
-> input_value ic
)