drop md4 i?86 specific asm implementations
[mldonkey.git] / src / utils / cdk / file.ml
blobb63ce211ca8498d5eab9505c4820e1f60487874f
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
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 *)
21 let to_string name =
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
29 if tmp = 0 then
30 Bytes.sub_string buf 0 nb_read
31 else
32 let nb_read = nb_read + tmp in
33 let buf =
34 if nb_read = buf_size then
35 String2.resize buf (2 * buf_size)
36 else buf
38 iter buf nb_read
40 iter buf 0)
42 let read_whole_chan chan =
43 let buf = Buffer.create 1024 in
44 let rec loop () =
45 Buffer.add_char buf (input_char chan);
46 loop ()
48 try
49 loop ()
50 with
51 End_of_file -> close_in chan; buf
53 let to_string_alt name =
54 let chan = open_in_bin name in
55 read_whole_chan chan
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 ->
60 try
61 let rec rcpy () =
62 let c = input_byte in_chan in
63 output_byte out_chan c;
64 flush out_chan;
65 rcpy ()
67 rcpy ()
68 with End_of_file -> ()))
70 let from_string name s =
71 Unix2.tryopen_write_bin name (fun oc -> output_string oc s)
73 let iter f name =
74 Unix2.tryopen_read_bin name (fun ic ->
75 try
76 while true do
77 let line = input_line ic in
78 f line
79 done
80 with End_of_file -> ())
82 let from_value name s =
83 Unix2.tryopen_write_bin name (fun oc -> output_value oc s)
85 let to_value name =
86 Unix2.tryopen_read_bin name (fun ic -> input_value ic)