From: John Whitington <john@coherentgraphics.co.uk>
To: Martin DeMello <martindemello@gmail.com>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] wrapping a string in an in_channel
Date: Mon, 06 Jan 2014 10:32:23 +0000 [thread overview]
Message-ID: <52CA8637.7010905@coherentgraphics.co.uk> (raw)
In-Reply-To: <CAFrFfuGJ1RfPo9vOx_psAszQO-Lq=J351qC+TGeWAZzv+=aJCQ@mail.gmail.com>
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/
next prev parent reply other threads:[~2014-01-06 10:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-05 8:23 Martin DeMello
2014-01-05 9:49 ` Gerd Stolpmann
2014-01-05 14:23 ` Hezekiah M. Carty
2014-01-06 10:32 ` John Whitington [this message]
2014-01-07 21:02 ` Stéphane Glondu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=52CA8637.7010905@coherentgraphics.co.uk \
--to=john@coherentgraphics.co.uk \
--cc=caml-list@yquem.inria.fr \
--cc=martindemello@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox