From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 34441BB81 for ; Tue, 6 Dec 2005 10:08:11 +0100 (CET) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.198]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jB698AOM011830 for ; Tue, 6 Dec 2005 10:08:10 +0100 Received: by wproxy.gmail.com with SMTP id i3so72913wra for ; Tue, 06 Dec 2005 01:08:10 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=T6acuEUnxtOqiJv20XZnQuyh/ISh4fshhWWWo93qR9YQJmmqis25V5oKCW4WbPUm41yTozXk4mA2FZ69fuyDTemoQ38emmvwxFz9ScJ3iyFu5LyVrC77vFAG5fSA77Cc1vMvRb+LXnn78dFYpOrCj2E5q3B6IP8IvTOFroaUUow= Received: by 10.65.205.19 with SMTP id h19mr264812qbq; Tue, 06 Dec 2005 01:08:09 -0800 (PST) Received: by 10.65.35.10 with HTTP; Tue, 6 Dec 2005 01:08:09 -0800 (PST) Message-ID: Date: Tue, 6 Dec 2005 22:08:09 +1300 From: Jonathan Roewen To: caml-list@yquem.inria.fr Subject: [Caml-list] A camlp4 task for reading in 'structs' MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Miltered: at nez-perce with ID 439554FA.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 camlp:01 type-safe:01 structs:01 camlp:01 type-checked:01 byte:01 char:01 char:01 struct:01 byte:01 nread:01 ints:01 int:01 int: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=RCVD_BY_IP autolearn=disabled version=3.0.3 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 =3D 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) =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