<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/07/ee704e826e5fbe1ad4b4e1a4e6378562"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-07-18T09:13:30"
  subject="Re: [Caml-list] string buffer as (in|out)_channel"
  prev="2003/07/3b065504b55d12e81921bb2ba4cd2ebb"
  next="2003/07/9cc4ec136348d1af781de9f8f7ad3a6d"
  prev-in-thread="2003/07/e098218c5547829c62c6d7fa6ccb376d"
  next-in-thread="2003/07/9cc4ec136348d1af781de9f8f7ad3a6d"
  prev-thread="2003/07/997f00f2e1af843a0ab59c0e0a1a80a0"
  next-thread="2003/07/ccd92eb3783074eb7e12bf2b325ce13c"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] string buffer as (in|out)_channel">
<msg 
  url="2003/07/4212e9f5c49ebf65377b958a70b8c309"
  from="Chris Hecker &lt;checker@d...&gt;"
  author="Chris Hecker"
  date="2003-07-17T07:06:04"
  subject="[Caml-list] string buffer as (in|out)_channel">
<msg 
  url="2003/07/e098218c5547829c62c6d7fa6ccb376d"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-07-17T07:39:29"
  subject="Re: [Caml-list] string buffer as (in|out)_channel">
</msg>
<msg 
  url="2003/07/ee704e826e5fbe1ad4b4e1a4e6378562"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-07-18T09:13:30"
  subject="Re: [Caml-list] string buffer as (in|out)_channel">
</msg>
<msg 
  url="2003/07/9cc4ec136348d1af781de9f8f7ad3a6d"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-07-18T09:40:34"
  subject="Re: [Caml-list] string buffer as (in|out)_channel">
</msg>
</msg>
</thread>

<contents>
Chris Hecker wrote:

&gt;
&gt; Is there any way to do something similar to the C++ string stream 
&gt; stuff, where you treat strings (or Buffer.t's) as in_ or 
&gt; out_channels?  The Format module has a formatter for strings, but 
&gt; there doesn't seem to be a way to do it with Pervasives.  Something like:
&gt;
&gt; val out_channel_of_buffer : Buffer.t -&gt; out_channel
&gt; val in_channel_of_buffer : Buffer.t -&gt; in_channel
&gt;
&gt; and maybe _of_string versions too (probably necessary because you 
&gt; can't convert a string to a buffer for the in_channel), which fill up 
&gt; and don't expand.
&gt;
&gt; It'd be very useful to be able to do this. 

For reading, this might do what you want:

(* wrap a Buffer with a Stream, for reading *)
let stream_of_buffer buf =
  Stream.of_string(Buffer.contents buf)
;;

For writing, I guess this sort of thing would do it:

module Ostream :
  sig
    type t = char -&gt; unit
    val of_buffer : Buffer.t -&gt; t
    val of_string : string -&gt; t
    val of_out_channel : out_channel -&gt; t
    val output_string : t -&gt; string -&gt; unit
    val output_value : t -&gt; 'a -&gt; unit
  end
    =
  struct
    type t = char -&gt; unit

    let of_buffer (buf:Buffer.t) : t =
      fun (c:char) -&gt; Buffer.add_char buf c

    let of_string (s:string) : t =
      let i = ref 0 in
      fun (c:char) -&gt; s.[!i] &lt;- c  

    let of_out_channel (chan:out_channel) : t =
      fun (c:char) -&gt; output_char chan c

    let output_string (ostream:t) (str:string) =
      String.iter ostream str
   
    let output_value (ostream:t) thing =
      let str = Marshal.to_string thing [] in
      output_string ostream str
     
  end

Maybe it would be prettier to make a matching Istream module,
but I guess it's better to stay with the well-established Stream module.

Issac


&gt;
&gt;
&gt; Thanks,
&gt; Chris
&gt;
&gt; PS.  Please cc me on replies, I temporarily unsubscribed from the 
&gt; list, thanks!
&gt;
&gt; -------------------
&gt; To unsubscribe, mail caml-list-request@inria.fr Archives: 
&gt; http://caml.inria.fr
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: 
&gt; http://caml.inria.fr/FAQ/
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt;
&gt;


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

