From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 93EB8BB81 for ; Tue, 6 Dec 2005 12:08:13 +0100 (CET) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.173]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jB6B8DLK030158 for ; Tue, 6 Dec 2005 12:08:13 +0100 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1Ejag7-0006WW-00; Tue, 06 Dec 2005 12:08:11 +0100 Received: from [84.58.146.3] (helo=gate.lan.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1Ejag7-00048w-00; Tue, 06 Dec 2005 12:08:11 +0100 Received: from gps.dynxs.de (localhost [127.0.0.1]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id 67482C097; Tue, 6 Dec 2005 12:08:11 +0100 (CET) Received: from 192.168.1.2 (proxying for 194.127.8.19) (SquirrelMail authenticated user gerd) by gps.dynxs.de with HTTP; Tue, 6 Dec 2005 12:08:11 +0100 (CET) Message-ID: <4581.192.168.1.2.1133867291.squirrel@gps.dynxs.de> In-Reply-To: References: Date: Tue, 6 Dec 2005 12:08:11 +0100 (CET) Subject: Re: [Caml-list] A camlp4 task for reading in 'structs' From: "Gerd Stolpmann" To: "Jonathan Roewen" Cc: caml-list@yquem.inria.fr User-Agent: SquirrelMail/1.4.4 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at concorde with ID 4395711D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 camlp:01 gerd:01 stolpmann:01 type-safe:01 structs:01 idl:01 syntax:01 struct:01 idl:01 mli:01 o'caml:01 val:01 val:01 unpack:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Jonathan Roewen said: > Hi All, > > The idea: a type-safe way to parse an IO.input stream as structured > data (think C structs). My SunRPC implementation contains that. It can read and write all data types specified for SunRPC, including all types you mention. Usage is quite simple: - Describe your data structure in an IDL file (syntax close to C), e.g. struct sample { int x; string y<10>; /* String with max length 10 */ }; - Compile that idl file with ocamlrpcgen: ocamlrpcgen -aux sample.x That generates sample_aux.{ml,mli}. Among other things, you can find here: * For every IDL type a corresponding O'Caml type. In this example: type sample =3D { x : int; y : string } * Functions converting values of type sample to/from the generic Xdr.xdr_value: val _to_sample : xdr_value -> sample val _of_sample : sample -> xdr_value * The dynamic representation of the IDL type: val xdrt_sample : xdr_type_term - You can now easily convert values s of type sample to strings: let t =3D Xdr.validate_xdr_type xdrt_sample in (* do this only once *) let v =3D _of_sample s in Xdr.pack_xdr_value_as_string v t [] - And back: let v =3D Xdr.unpack_xdr_value ~fast:true str t [] in _to_sample v Although there are currently no stream functions, this would be easy to a= dd. Note that XDR, the marshalling technique used by SunRPC, is an Internet standard. The representation is portable (e.g. the endianess question is solved). You can, for example, read such values easily in from a C program. Link: http://www.ocaml-programming.de/programming/rpc.html Gerd > 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 =3D Byte | Word | DWord | Bytes of int | Char | String of in= t | > 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) =3D 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 =3D IO.input, such as let i =3D 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 > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > ------------------------------------------------------------ Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de ------------------------------------------------------------