Commits

Anonymous committed 4e22102

proto2: writing with IO, reading with Iteratees

Comments (0)

Files changed (5)

     value close_out : output_channel -> m unit;
   end
 ;
+
+
+module type IT_TYPE
+ =
+  sig
+    module It_IO
+     :
+      sig
+        type m +'a;
+        value return : 'a -> m 'a;
+        value bind_rev : m 'a -> ('a -> m 'b) -> m 'b;
+        value error : exn -> m 'a;
+      end
+    ;
+
+    type stream 'el;
+    value empty_stream : stream 'el;
+    value chunk_of : 'el -> stream 'el;
+
+    type err_msg = exn;
+
+    type iteratee 'el 'a =
+      [ IE_done of 'a
+      | IE_cont of option err_msg and
+          ( stream 'el
+            -> It_IO.m (iteratee 'el 'a * stream 'el)
+          )
+      ]
+    ;
+
+    value return : 'a -> iteratee 'el 'a
+    ;
+
+    value throw_err : err_msg -> iteratee 'el 'a
+    ;
+
+    value peek : iteratee 'el (option 'el)
+    ;
+
+    value bindI :
+      ('a -> iteratee 'el 'b) -> iteratee 'el 'a -> iteratee 'el 'b
+    ;
+
+    value joinI :
+      iteratee 'el (iteratee 'el' 'a) -> iteratee 'el 'a
+    ;
+
+    value lift : It_IO.m 'a -> iteratee 'el 'a
+    ;
+
+    value liftI : It_IO.m (iteratee 'el 'a) -> iteratee 'el 'a
+    ;
+
+    value drop : int -> iteratee 'el unit
+    ;
+
+    value drop_while : ('el -> bool) -> iteratee 'el unit
+    ;
+
+    value break_chars : (char -> bool) -> iteratee char string
+    ;
+
+    type enumeratee 'elo 'eli 'a = 
+      iteratee 'eli 'a -> iteratee 'elo (iteratee 'eli 'a)
+    ;
+
+    value limit : int -> enumeratee 'el 'el 'a
+    ;
+
+    value sequence_stream : iteratee 'elo 'eli
+      -> enumeratee 'elo 'eli 'a
+    ;
+
+    value catch :
+      (unit -> iteratee 'el 'a) ->
+      ( err_msg ->
+         iteratee 'el 'a
+      ) ->
+      iteratee 'el 'a
+    ;
+
+    value take : int -> enumeratee 'el 'el 'a
+    ;
+
+    value stream2list : iteratee 'el (list 'el)
+    ;
+
+    value run : iteratee 'el 'a -> It_IO.m 'a
+    ;
+
+    value feedI :
+      (stream 'el -> It_IO.m (iteratee 'el 'a  *  stream 'el)) ->
+      stream 'el ->
+      It_IO.m (iteratee 'el 'a)
+    ;
+
+    module Ops
+     :
+      sig
+        (* IO binds: *)
+        value ( %<< ) : ('a -> It_IO.m 'b) -> It_IO.m 'a -> It_IO.m 'b;
+        value ( >>% ) : It_IO.m 'a -> ('a -> It_IO.m 'b) -> It_IO.m 'b;
+
+        (* Iteratees binds: *)
+        value ( =<< ) :
+        ('a -> iteratee 'el 'b) -> iteratee 'el 'a -> iteratee 'el 'b;
+        value ( >>= ) :
+          iteratee 'el 'a -> ('a -> iteratee 'el 'b) -> iteratee 'el 'b;
+
+        (* Enumerators sequence: *)
+(*
+        value ( >>> ) :
+          enumerator 'el 'a -> enumerator 'el 'a -> enumerator 'el 'a
+        ;
+*)
+
+      end
+    ;
+
+  end
+;

dumbstreaming_io.ml

     exception Dumbstreaming of string
     ;
 
-    value (write : IO.output_channel -> string -> IO.m unit) outch str =
+    value (write1 : IO.output_channel -> string -> IO.m unit) outch str =
       let pre = Printf.sprintf "%i\n" (String.length str)
       and post = "\n\n" in
       IO.write outch pre >>= fun () ->
       IO.flush outch
     ;
 
+    value (write : IO.output_channel -> list string -> IO.m unit)
+      outch
+      strs
+     =
+      let count = List.length strs in
+      let lens = List.map String.length strs in
+      let totalsize =
+        List.fold_left Int64.add 0L (List.map Int64.of_int lens) in
+      let lens_txt_list = List.map string_of_int lens in
+      let lens_txt = String.concat "\x20" lens_txt_list in
+      let pre = Printf.sprintf "%Li %i %s\x0A" totalsize count lens_txt in
+      IO.write outch pre >>= fun () ->
+      let rec loop lst =
+        match lst with
+        [ [] -> IO.return ()
+        | [h :: t] ->
+            IO.write outch h >>= fun () ->
+            IO.write outch "\x0A" >>= fun () ->
+            loop t
+        ]
+      in
+      loop strs >>= fun () ->
+      IO.write outch "\x0A" >>= fun () ->
+      IO.flush outch
+    ;
+
     value close_out outch =
       IO.write outch "\n" >>= fun () ->
       IO.flush outch >>= fun () ->

dumbstreaming_io.mli

     exception Dumbstreaming of string
     ;
 
-    value write : IO.output_channel -> string -> IO.m unit
+    (* proto ver1 *)
+    value write1 : IO.output_channel -> string -> IO.m unit
+    ;
+
+    (* proto ver2 *)
+    value write : IO.output_channel -> list string -> IO.m unit
     ;
 
     (* sends "end of stream", closes channel *)

dumbstreaming_it.ml

 value ( & ) f x = f x
 ;
 
-module type IT_TYPE
- =
-  sig
-    module It_IO
-     :
-      sig
-        type m +'a;
-        value return : 'a -> m 'a;
-        value bind_rev : m 'a -> ('a -> m 'b) -> m 'b;
-        value error : exn -> m 'a;
-      end
-    ;
-
-    type stream 'el;
-    value empty_stream : stream 'el;
-    value chunk_of : 'el -> stream 'el;
-
-    type err_msg = exn;
-
-    type iteratee 'el 'a =
-      [ IE_done of 'a
-      | IE_cont of option err_msg and
-          ( stream 'el
-            -> It_IO.m (iteratee 'el 'a * stream 'el)
-          )
-      ]
-    ;
-
-    value return : 'a -> iteratee 'el 'a
-    ;
-
-    value throw_err : err_msg -> iteratee 'el 'a
-    ;
-
-    value peek : iteratee 'el (option 'el)
-    ;
-
-    value bindI :
-      ('a -> iteratee 'el 'b) -> iteratee 'el 'a -> iteratee 'el 'b
-    ;
-
-    value joinI :
-      iteratee 'el (iteratee 'el' 'a) -> iteratee 'el 'a
-    ;
-
-    value lift : It_IO.m 'a -> iteratee 'el 'a
-    ;
-
-    value liftI : It_IO.m (iteratee 'el 'a) -> iteratee 'el 'a
-    ;
-
-    value drop : int -> iteratee 'el unit
-    ;
-
-    value drop_while : ('el -> bool) -> iteratee 'el unit
-    ;
-
-    value break_chars : (char -> bool) -> iteratee char string
-    ;
-
-    type enumeratee 'elo 'eli 'a = 
-      iteratee 'eli 'a -> iteratee 'elo (iteratee 'eli 'a)
-    ;
-
-    value limit : int -> enumeratee 'el 'el 'a
-    ;
-
-    value sequence_stream : iteratee 'elo 'eli
-      -> enumeratee 'elo 'eli 'a
-    ;
-
-    value catch :
-      (unit -> iteratee 'el 'a) ->
-      ( err_msg ->
-         iteratee 'el 'a
-      ) ->
-      iteratee 'el 'a
-    ;
-
-    value take : int -> enumeratee 'el 'el 'a
-    ;
-
-    value stream2list : iteratee 'el (list 'el)
-    ;
-
-    value run : iteratee 'el 'a -> It_IO.m 'a
-    ;
-
-    value feedI :
-      (stream 'el -> It_IO.m (iteratee 'el 'a  *  stream 'el)) ->
-      stream 'el ->
-      It_IO.m (iteratee 'el 'a)
-    ;
-
-    module Ops
-     :
-      sig
-        (* IO binds: *)
-        value ( %<< ) : ('a -> It_IO.m 'b) -> It_IO.m 'a -> It_IO.m 'b;
-        value ( >>% ) : It_IO.m 'a -> ('a -> It_IO.m 'b) -> It_IO.m 'b;
-
-        (* Iteratees binds: *)
-        value ( =<< ) :
-        ('a -> iteratee 'el 'b) -> iteratee 'el 'a -> iteratee 'el 'b;
-        value ( >>= ) :
-          iteratee 'el 'a -> ('a -> iteratee 'el 'b) -> iteratee 'el 'b;
-
-        (* Enumerators sequence: *)
-(*
-        value ( >>> ) :
-          enumerator 'el 'a -> enumerator 'el 'a -> enumerator 'el 'a
-        ;
-*)
-
-      end
-    ;
-
-  end
+open Ds_types
 ;
 
 

dumbstreaming_it.mli

+open Ds_types
+;
+
+module Make (I : IT_TYPE)
+ :
+  sig
+    value read :
+      ~get_piece_it :
+       ( ~totalsize:int64 ->
+         ~totalcount:int ->
+         ~piecesize:int ->
+         ~piecenumber:int ->
+         I.iteratee char 'i
+       )
+      -> ~combine_it : (I.iteratee 'i 'a)
+      -> I.iteratee char (option 'a)
+    ;
+
+
+
+    (* for tests: *)
+    value read_uint : I.iteratee char int;
+
+    (* for tests: *)
+    value read_uint_nz : I.iteratee char int;
+
+    (* for tests: *)
+    value read_lengths : int -> I.iteratee char (list int)
+    ;
+
+  end
+;