Commits

Dmitry Grebeniuk  committed 6b0a092

.

  • Participants
  • Parent commits 6126be5

Comments (0)

Files changed (9)

 name="dumbstreaming"
 version="0.1"
 description="Packing/unpacking strings into byte stream"
+
+package "io" (
 requires="monad_io"
-archive(byte)="dumbstreaming.cma"
-archive(native)="dumbstreaming.cmxa"
+archive(byte)="dumbstreaming_io.cma"
+archive(native)="dumbstreaming_io.cmxa"
+)
+
+package "it" (
+requires="monad_io, iteratees"
+archive(byte)="dumbstreaming_it.cma"
+archive(native)="dumbstreaming_it.cmxa"
+)
-L=dumbstreaming
-PKG=$(L)
+LIO=dumbstreaming_io
+LIT=dumbstreaming_it
+PKG=dumbstreaming
 VERSION=0.1
 
 all :
-	ocamlbuild $(L).cma $(L).cmxa $(L).a
+	ocamlbuild \
+	  $(LIO).cma $(LIO).cmxa $(LIO).a \
+	  $(LIT).cma $(LIT).cmxa $(LIT).a
 
 install : all
 	ocamlfind install \
 	  -patch-version $(VERSION) \
-	  $(PKG) META \
-	  _build/$(L).cma _build/$(L).cmxa _build/$(L).a \
-	  _build/$(L).cmi _build/ds_types.cmi
+	  $(PKG) META  _build/ds_types.cmi \
+	  _build/$(LIO).cma _build/$(LIO).cmxa _build/$(LIO).a \
+	  _build/$(LIO).cmi \
+	  _build/$(LIT).cma _build/$(LIT).cmxa _build/$(LIT).a \
+	  _build/$(LIT).cmi
 
 uninstall :
 	ocamlfind remove $(PKG)

File dumbstreaming.ml

-open Ds_types;
-
-module Make (IO : IO_Type)
- =
-  struct
-
-    value ( >>= ) = IO.bind_rev
-    ;
-
-    exception Dumbstreaming of string
-    ;
-
-    value (write : 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.write outch str >>= fun () ->
-      IO.write outch post >>= fun () ->
-      IO.flush outch
-    ;
-
-    value close_out outch =
-      IO.write outch "\n" >>= fun () ->
-      IO.flush outch >>= fun () ->
-      IO.close_out outch
-    ;
-
-    value max_len_digits = 10
-    ;
-
-    value err msg = IO.error (Dumbstreaming msg)
-    ;
-
-    value sprintf fmt = Printf.sprintf fmt
-    ;
-
-    value read_char inch =
-      let str = String.make 1 '\x00' in
-      IO.read_into inch str 0 1 >>= fun has_read ->
-      if has_read = 0
-      then err "end of channel"
-      else if has_read = 1
-      then IO.return str.[0]
-      else err "read_char: bad 'has_read'"
-    ;
-
-    value read_len_or_eos inch =
-      inner ~anydigit:False max_len_digits 0
-      where rec inner ~anydigit left acc =
-        if left < 0
-        then
-          err (sprintf "len > %i" max_len_digits)
-        else
-          read_char inch >>= fun c ->
-          if c = '\n'
-          then
-            IO.return (
-              if anydigit
-              then Some acc
-              else None
-              )
-          else
-            if c >= '0' && c <= '9'
-            then
-              let d = (Char.code c) - (Char.code '0') in
-              let new_acc = 10 * acc + d in
-              inner ~anydigit:True (left - 1) new_acc
-            else
-              err (sprintf "excepted decimal number (length)")
-    ;
-
-    value read_into_exact inch buf ofs len =
-      loop ~ofs ~len
-      where rec loop ~ofs ~len =
-        let () = assert (len >= 0) in
-        if len = 0
-        then
-          IO.return ()
-        else
-          IO.read_into inch buf ofs len >>= fun has_read ->
-          if has_read = 0
-          then
-            err "unexpected eof"
-          else
-            loop ~ofs:(ofs + has_read) ~len:(len - has_read)
-    ;
-
-    value read_the_char inch c =
-      read_char inch >>= fun r ->
-      if r = c
-      then IO.return ()
-      else err (sprintf "excepted %C, found %C" c r)
-    ;
-
-    value read_msg_post inch =
-      read_the_char inch '\n' >>= fun () ->
-      read_the_char inch '\n'
-    ;
-
-    value read inch =
-      read_len_or_eos inch >>= fun len_or_eos ->
-      match len_or_eos with
-      [ None ->
-          IO.close_in inch >>= fun () ->
-          IO.return None
-      | Some len ->
-          if len > Sys.max_string_length
-          then err "string is longer than Sys.max_string_length"
-          else
-            let r = String.make len '\x00' in
-            read_into_exact inch r 0 len >>= fun () ->
-            read_msg_post inch >>= fun () ->
-            IO.return (Some r)
-      ]
-    ;
-
-  end
-;

File dumbstreaming.mli

-open Ds_types;
-
-module Make (IO : IO_Type)
- :
-  sig
-
-    exception Dumbstreaming of string
-    ;
-
-    value write : IO.output_channel -> string -> IO.m unit
-    ;
-
-    (* sends "end of stream", closes channel *)
-    value close_out : IO.output_channel -> IO.m unit
-    ;
-
-    (* on "end of stream" returns None, closes channel *)
-    value read : IO.input_channel -> IO.m (option string)
-    ;
-
-  end
-;

File dumbstreaming.mllib

-Dumbstreaming
-Ds_types

File dumbstreaming_io.ml

+open Ds_types;
+
+module Make (IO : IO_Type)
+ =
+  struct
+
+    value ( >>= ) = IO.bind_rev
+    ;
+
+    exception Dumbstreaming of string
+    ;
+
+    value (write : 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.write outch str >>= fun () ->
+      IO.write outch post >>= fun () ->
+      IO.flush outch
+    ;
+
+    value close_out outch =
+      IO.write outch "\n" >>= fun () ->
+      IO.flush outch >>= fun () ->
+      IO.close_out outch
+    ;
+
+    value max_len_digits = 10
+    ;
+
+    value err msg = IO.error (Dumbstreaming msg)
+    ;
+
+    value sprintf fmt = Printf.sprintf fmt
+    ;
+
+    value read_char inch =
+      let str = String.make 1 '\x00' in
+      IO.read_into inch str 0 1 >>= fun has_read ->
+      if has_read = 0
+      then err "end of channel"
+      else if has_read = 1
+      then IO.return str.[0]
+      else err "read_char: bad 'has_read'"
+    ;
+
+    value read_len_or_eos inch =
+      inner ~anydigit:False max_len_digits 0
+      where rec inner ~anydigit left acc =
+        if left < 0
+        then
+          err (sprintf "len > %i" max_len_digits)
+        else
+          read_char inch >>= fun c ->
+          if c = '\n'
+          then
+            IO.return (
+              if anydigit
+              then Some acc
+              else None
+              )
+          else
+            if c >= '0' && c <= '9'
+            then
+              let d = (Char.code c) - (Char.code '0') in
+              let new_acc = 10 * acc + d in
+              inner ~anydigit:True (left - 1) new_acc
+            else
+              err (sprintf "excepted decimal number (length)")
+    ;
+
+    value read_into_exact inch buf ofs len =
+      loop ~ofs ~len
+      where rec loop ~ofs ~len =
+        let () = assert (len >= 0) in
+        if len = 0
+        then
+          IO.return ()
+        else
+          IO.read_into inch buf ofs len >>= fun has_read ->
+          if has_read = 0
+          then
+            err "unexpected eof"
+          else
+            loop ~ofs:(ofs + has_read) ~len:(len - has_read)
+    ;
+
+    value read_the_char inch c =
+      read_char inch >>= fun r ->
+      if r = c
+      then IO.return ()
+      else err (sprintf "excepted %C, found %C" c r)
+    ;
+
+    value read_msg_post inch =
+      read_the_char inch '\n' >>= fun () ->
+      read_the_char inch '\n'
+    ;
+
+    value read inch =
+      read_len_or_eos inch >>= fun len_or_eos ->
+      match len_or_eos with
+      [ None ->
+          IO.close_in inch >>= fun () ->
+          IO.return None
+      | Some len ->
+          if len > Sys.max_string_length
+          then err "string is longer than Sys.max_string_length"
+          else
+            let r = String.make len '\x00' in
+            read_into_exact inch r 0 len >>= fun () ->
+            read_msg_post inch >>= fun () ->
+            IO.return (Some r)
+      ]
+    ;
+
+  end
+;

File dumbstreaming_io.mli

+open Ds_types;
+
+module Make (IO : IO_Type)
+ :
+  sig
+
+    exception Dumbstreaming of string
+    ;
+
+    value write : IO.output_channel -> string -> IO.m unit
+    ;
+
+    (* sends "end of stream", closes channel *)
+    value close_out : IO.output_channel -> IO.m unit
+    ;
+
+    (* on "end of stream" returns None, closes channel *)
+    value read : IO.input_channel -> IO.m (option string)
+    ;
+
+  end
+;

File dumbstreaming_io.mllib

+Dumbstreaming
+Ds_types

File dumbstreaming_it.ml

+module type IT_TYPE
+ =
+  sig
+  end
+;
+
+
+value read : (ntotal -> npart -> nbytes -> iteratee char 'i)
+          -> iteratee 'i 'a
+          -> iteratee char 'a
+;
+
+
+
+module Make (I : IT_TYPE)
+ :
+  sig
+    value read
+     : (int -> list (I.iteratees char 'a)) ->
+       I.iteratee char (list 'a)
+    ;
+  end
+ =
+  struct
+  end
+;
+
+
+int_of_string = fold (fun acc char -> 10*acc + char) 0
+
+int_of_string : iteratee char int =
+  cont & step 0
+  step acc =
+    Chunk char -> cont & step &   10*acc + char
+    EOF -> return acc
+
+
+read : (ntotal -> npart -> nbytes -> 'a -> iteratee char 'a)
+    -> iteratee char 'a =
+  cont & step init_state
+  step state =
+    Chunk it -> cont & step &   join & take n[i] & it
+    EOF -> return state
+
+
+read : iteratee (ntotal * npart * nbytes) (iteratee char 'a) ->
+       iteratee char 'a
+
+ (ntotal -> npart -> nbytes -> 'a -> iteratee char 'a)
+    -> iteratee char 'a =
+
+
+
+value read : ('a -> iteratee char 'a)
+         -> 'a -> iteratee char 'a