1
(* $Id: oOChannel.ml,v 1.7 2004/11/03 11:21:53 yori Exp $ *)
2
(* Copyright 2002, 2003 Yamagata Yoriyuki. distributed with LGPL *)
4
class type ['a] obj_input_channel =
6
method get : unit -> 'a
7
method close_in : unit -> unit
10
class type ['a] obj_output_channel =
12
method put : 'a -> unit
13
method flush : unit -> unit
14
method close_out : unit -> unit
17
class ['a] channel_of_stream s =
21
try Stream.next s with Stream.Failure -> raise End_of_file
22
method close_in () = ()
25
let stream_of_channel inchan =
27
try Some (inchan#get()) with End_of_file -> None)
29
class type char_input_channel =
31
method input : string -> int -> int -> int
32
method close_in : unit -> unit
35
class type char_output_channel =
37
method output : string -> int -> int -> int
38
method flush : unit -> unit
39
method close_out : unit -> unit
42
class char_input_channel_of (oc : char #obj_input_channel) =
44
method close_in () = oc#close_in ()
45
method input b p len =
47
(try while !i < p + len - 1 do
50
done; () with End_of_file -> ());
52
if len <= 0 then raise End_of_file else len
55
class char_obj_input_channel_of (ic : char_input_channel) =
56
let b = String.make 1024 '\000' in
61
if !pos >= !len then begin
62
len := ic#input b 0 1024;
69
method close_in () = ic#close_in ()
72
class char_output_channel_of (oc : char #obj_output_channel) =
74
method flush = oc#flush
75
method close_out = oc#close_out
76
method output b p len =
77
for i = p to p+len-1 do oc#put b.[i] done;
81
class char_obj_output_channel_of (out : char_output_channel) =
82
let b = String.make 1024 '\000' in
89
let n = out#output b 0 1024 in
90
String.blit b n b 0 (1024 - n);
93
let n = out#output b 0 !pos in
96
"OOChannel.char_output_channel_of#flush: \
97
Cannot flush the entire buffer";
103
class of_in_channel p_in =
105
method close_in () = close_in p_in
106
method input b p len =
107
let len = input p_in b p len in
108
if len = 0 then raise End_of_file else len
111
class of_out_channel p_out =
113
method close_out () = close_out p_out
114
method output b p len = output p_out b p len; len
115
method flush () = flush p_out