~ubuntu-branches/ubuntu/lucid/camomile/lucid

« back to all changes in this revision

Viewing changes to public/oOChannel.ml

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Le Gall
  • Date: 2005-12-03 01:18:55 UTC
  • Revision ID: james.westby@ubuntu.com-20051203011855-qzvwlld1xyqnl62t
Tags: upstream-0.6.3
ImportĀ upstreamĀ versionĀ 0.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: oOChannel.ml,v 1.7 2004/11/03 11:21:53 yori Exp $ *)
 
2
(* Copyright 2002, 2003 Yamagata Yoriyuki. distributed with LGPL *)
 
3
 
 
4
class type ['a] obj_input_channel =
 
5
  object
 
6
    method get : unit -> 'a
 
7
    method close_in : unit -> unit
 
8
  end
 
9
 
 
10
class type ['a] obj_output_channel =
 
11
  object
 
12
    method put : 'a -> unit
 
13
    method flush : unit -> unit
 
14
    method close_out : unit -> unit
 
15
  end
 
16
 
 
17
class ['a] channel_of_stream s  =
 
18
  object
 
19
    val s = s
 
20
    method get () :'a = 
 
21
      try Stream.next s with Stream.Failure -> raise End_of_file
 
22
    method close_in () = ()
 
23
  end
 
24
 
 
25
let stream_of_channel inchan =
 
26
  Stream.from (fun _ ->
 
27
    try Some (inchan#get()) with End_of_file -> None)
 
28
 
 
29
class type char_input_channel =
 
30
  object
 
31
    method input : string -> int -> int -> int
 
32
    method close_in : unit -> unit
 
33
  end
 
34
 
 
35
class type char_output_channel =
 
36
  object
 
37
    method output : string -> int -> int -> int
 
38
    method flush : unit -> unit
 
39
    method close_out : unit -> unit
 
40
  end
 
41
 
 
42
class char_input_channel_of (oc : char #obj_input_channel) =
 
43
  object (self)
 
44
    method close_in () = oc#close_in ()
 
45
    method input b p len =
 
46
      let i = ref p in
 
47
        (try while !i < p + len - 1 do
 
48
          b.[!i] <- oc#get();
 
49
          incr i;
 
50
         done; () with End_of_file -> ());
 
51
        let len = !i - p in
 
52
          if len <= 0 then raise End_of_file else len
 
53
  end
 
54
 
 
55
class char_obj_input_channel_of (ic : char_input_channel) =
 
56
  let b = String.make 1024 '\000' in
 
57
  let pos = ref 0 in
 
58
  let len = ref 0 in
 
59
  object (self)
 
60
    method get () =
 
61
      if !pos >= !len then begin
 
62
        len := ic#input b 0 1024;
 
63
        pos := 0;
 
64
        self#get ()
 
65
       end else
 
66
         let c = b.[!pos] in
 
67
           incr pos;
 
68
           c
 
69
    method close_in () = ic#close_in ()
 
70
  end
 
71
    
 
72
class char_output_channel_of (oc : char #obj_output_channel) =
 
73
  object
 
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;
 
78
      len
 
79
  end
 
80
 
 
81
class char_obj_output_channel_of (out : char_output_channel) =
 
82
  let b = String.make 1024 '\000' in
 
83
  let pos = ref 0 in
 
84
  object
 
85
    method put c =
 
86
      b.[!pos] <- c;
 
87
      incr pos;
 
88
      if !pos >= 1024 then 
 
89
        let n = out#output b 0 1024 in
 
90
          String.blit b n b 0 (1024 - n);
 
91
          pos := 1024 - n
 
92
    method flush () =    
 
93
      let n = out#output b 0 !pos in
 
94
        if n < !pos then 
 
95
          failwith 
 
96
            "OOChannel.char_output_channel_of#flush: \
 
97
             Cannot flush the entire buffer";
 
98
    method close_out () = 
 
99
      out#flush ();
 
100
      out#close_out () 
 
101
  end
 
102
 
 
103
class of_in_channel p_in =
 
104
  object
 
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
 
109
  end
 
110
 
 
111
class of_out_channel p_out =
 
112
  object
 
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
 
116
  end