caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] wrapping a string in an in_channel
@ 2014-01-05  8:23 Martin DeMello
  2014-01-05  9:49 ` Gerd Stolpmann
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Martin DeMello @ 2014-01-05  8:23 UTC (permalink / raw)
  To: OCaml List

Does OCaml have any equivalent to ruby's StringIO, to wrap an
in_channel around a string and then treat it as if it were a file?

martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] wrapping a string in an in_channel
  2014-01-05  8:23 [Caml-list] wrapping a string in an in_channel Martin DeMello
@ 2014-01-05  9:49 ` Gerd Stolpmann
  2014-01-05 14:23 ` Hezekiah M. Carty
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Gerd Stolpmann @ 2014-01-05  9:49 UTC (permalink / raw)
  To: Martin DeMello; +Cc: OCaml List

[-- Attachment #1: Type: text/plain, Size: 930 bytes --]

Am Sonntag, den 05.01.2014, 00:23 -0800 schrieb Martin DeMello:
> Does OCaml have any equivalent to ruby's StringIO, to wrap an
> in_channel around a string and then treat it as if it were a file?

With the standard library this is not possible.

When I need such a feature, I normally use OCamlnet's channel objects:

http://projects.camlcity.org/projects/dl/ocamlnet-3.7.3/doc/html-main/Netchannels_tut.html

http://projects.camlcity.org/projects/dl/ocamlnet-3.7.3/doc/html-main/Netchannels.html

http://www.ocaml-programming.de/rec/IO-Classes.html

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] wrapping a string in an in_channel
  2014-01-05  8:23 [Caml-list] wrapping a string in an in_channel Martin DeMello
  2014-01-05  9:49 ` Gerd Stolpmann
@ 2014-01-05 14:23 ` Hezekiah M. Carty
  2014-01-06 10:32 ` John Whitington
  2014-01-07 21:02 ` Stéphane Glondu
  3 siblings, 0 replies; 5+ messages in thread
From: Hezekiah M. Carty @ 2014-01-05 14:23 UTC (permalink / raw)
  To: Martin DeMello; +Cc: OCaml List

[-- Attachment #1: Type: text/plain, Size: 357 bytes --]

On Jan 5, 2014 3:23 AM, "Martin DeMello" <martindemello@gmail.com> wrote:
>
> Does OCaml have any equivalent to ruby's StringIO, to wrap an
> in_channel around a string and then treat it as if it were a file?
>
> martin
>

Batteries provides something similar with its BatIO.input_string function.
It returns the Batteries equivalent to an in_channel.

Hez

[-- Attachment #2: Type: text/html, Size: 513 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] wrapping a string in an in_channel
  2014-01-05  8:23 [Caml-list] wrapping a string in an in_channel Martin DeMello
  2014-01-05  9:49 ` Gerd Stolpmann
  2014-01-05 14:23 ` Hezekiah M. Carty
@ 2014-01-06 10:32 ` John Whitington
  2014-01-07 21:02 ` Stéphane Glondu
  3 siblings, 0 replies; 5+ messages in thread
From: John Whitington @ 2014-01-06 10:32 UTC (permalink / raw)
  To: Martin DeMello; +Cc: OCaml List


Martin DeMello wrote:
> Does OCaml have any equivalent to ruby's StringIO, to wrap an
> in_channel around a string and then treat it as if it were a file?

Here's another example of wrapping IO around strings, bigarrays, etc as 
a record of functions:

https://github.com/johnwhitington/camlpdf/blob/master/pdfio.ml

Here's a type:

type input =
   {pos_in : unit -> pos;
    seek_in : pos -> unit;
    input_char : unit -> char option;
    input_byte : unit -> int;
    in_channel_length : pos;
    set_offset : pos -> unit;
    caml_channel : in_channel option;
    source : string}

It's good to add the 'source' field - which is an arbitrary string for 
debugging purposes. Of course, if the input came from a file, we would 
use the file name.

The caml_channel field optionally stores a channel this input was 
derived from. So, we can still drill down and use fast functions from 
the standard library such as Pervasives.really_input.

(The set_offset field is a nasty PDF-related hack: a PDF file can have 
garbage bytes preceding the header, and PDF absolute byte offsets are 
offsets from the header, so every read and write might have to be offset)

Functions in records as an abstraction don't seem to be a speed problem. 
The use of option-returning functions like input_char above rather than 
exception-raising ones or minus-one-as-exception functions like 
input_byte above is big, though, if the data processed is large enough, 
due to garbage collection.

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] wrapping a string in an in_channel
  2014-01-05  8:23 [Caml-list] wrapping a string in an in_channel Martin DeMello
                   ` (2 preceding siblings ...)
  2014-01-06 10:32 ` John Whitington
@ 2014-01-07 21:02 ` Stéphane Glondu
  3 siblings, 0 replies; 5+ messages in thread
From: Stéphane Glondu @ 2014-01-07 21:02 UTC (permalink / raw)
  To: caml-list

Le 05/01/2014 09:23, Martin DeMello a écrit :
> Does OCaml have any equivalent to ruby's StringIO, to wrap an
> in_channel around a string and then treat it as if it were a file?

Technically, it should be possible by using low-level Unix machinery
like in the following:

let wrap_string s =
  let infd, outfd = Unix.pipe () in
  let inc = Unix.in_channel_of_descr infd in
  let outc = Unix.out_channel_of_descr outfd in
  let writer () =
    output_string outc s;
    flush outc;
    close_out outc
  in Thread.create writer ();
  inc

... but the other suggestions are probably better...

Cheers,

-- 
Stéphane


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-01-07 21:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-05  8:23 [Caml-list] wrapping a string in an in_channel Martin DeMello
2014-01-05  9:49 ` Gerd Stolpmann
2014-01-05 14:23 ` Hezekiah M. Carty
2014-01-06 10:32 ` John Whitington
2014-01-07 21:02 ` Stéphane Glondu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).