caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] A camlp4 task for reading in 'structs'
@ 2005-12-06  9:08 Jonathan Roewen
  2005-12-06 11:08 ` Gerd Stolpmann
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Roewen @ 2005-12-06  9:08 UTC (permalink / raw)
  To: caml-list

Hi All,

The idea: a type-safe way to parse an IO.input stream as structured
data (think C structs).

I'm wondering if anyone is interested in trying to create a camlp4
extension for reading structured data from an IO.input stream. I'm not
sure if it's at all possible, but I'd like to return a tuple, and have
it type-checked properly.

Something like:

type field = Byte | Word | DWord | Bytes of int | Char | String of int | All;;
(* corresponding output types: int | int | int32 | int list | char |
string | string; *)

What I want is code that I can pattern match easily (lists are okay,
but I get non-exhaustive match warnings all the time, and it doesn't
guarantee that I'm getting the right values. I also need to use a
second type if I want to have values other than ints.

something like: let (a,b,c) = read_struct i [Byte; Char; Word];;
(using IO module, would return IO.No_more_input if it can't read
enough, which should be okay).

where i = IO.input, such as let i = IO.input_string "hello" in...

I figure something should be possible, as the size of the tuple would
match the size of the list. I'm just not sure if camlp4 is capable of
this; I myself would have zero idea where to begin if I have to write
it... so any help would be greatly appreciated, as I would use this a
heck of a lot in my operating system project.

The operations that would use I think would be:
Byte: IO.read_byte
Word: IO.read_ui16
DWord: IO.read_real_i32
Bytes of int: n * IO.read_byte : int list
Char: IO.read
String of int: IO.really_nread n
All: read until IO.No_more_input is raised, returning a string

Actually, I just had a thought: instead of a list, you could use a
tuple as well .. if that'd make it more possible. Ohh, I forgot about
endianness .. not sure how to handle that...

Kindest Regards,

Jonathan


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

end of thread, other threads:[~2005-12-06 11:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-06  9:08 [Caml-list] A camlp4 task for reading in 'structs' Jonathan Roewen
2005-12-06 11:08 ` Gerd Stolpmann

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).