Browse thread
How to read different ints from a Bigarray?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2009-10-29 (10:34) |
From: | Goswin von Brederlow <goswin-v-b@w...> |
Subject: | Re: [Caml-list] Re: How to read different ints from a Bigarray? |
Goswin von Brederlow <goswin-v-b@web.de> writes: > blue storm <bluestorm.dylc@gmail.com> writes: > >> On Wed, Oct 28, 2009 at 6:57 PM, Goswin von Brederlow <goswin-v-b@web.de> wrote: >>> Maybe ideal would be a format string based interface that calls C with >>> a format string and a record of values. Because what I really need is >>> to read/write records in an architecture independend way. Something >>> like >>> >>> type t = { x:int; y:char; z:int64 } >>> let t_format = "%2u%c%8d" >>> >>> put_formated buf t_format t >>> >>> But how to get that type safe? Maybe a camlp4 module that generates >>> the format string and type from a single declaration so they always >>> match. >> >> It's possibly off-topic, but you might be interested in Richard >> Jones's Bitstring project [1] wich deals with similar issues quite >> nicely in my opinion. >> >> [1] http://code.google.com/p/bitstring/ > > No, quite on-topic. > > I glanced at the examples and code and it looks to me though as if > this can only parse bitstrings but not create them from a pattern. > You have > > let parse_foo bits = > bitmatch bits with > | { x : 16 : littleendian; y : 16 : littleendian } -> fun x y -> (x, y) > > but no > > let unparse_foo (x, y) = > bitmake { x : 16 : littleendian; y : 16 : littleendian } x y > > > Idealy would be something along > > let pattern = make_pattern { x : 16 : littleendian; y : 16 : littleendian } > let parse_foo bits = parse pattern (fun x y -> (x, y)) > let unparse_foo (x, y) = unparse pattern x y > > But I know how to do that with CPS already. I just need the primitives > to get/set the basic types. > > MfG > Goswin And I was wrong. There is http://code.google.com/p/bitstring/source/browse/trunk/examples/make_ipv4_header.ml as an example. Not ideal since parsing and unparsing will duplicate the pattern definition but that will be locale for each type. MfG Goswin