From: Daniel Martin Date: 2006-07-30T08:03:48+09:00 Subject: Re: How can I parse binary files? Joel VanderWerf writes: > 2. Reading BitStructs from a file. > > I'm not sure there's an easier way to do it than in your code, Daniel, > but I'm open to suggestions. All of ruby's IO (including sockets) uses > Strings, so we always have to read a String and then construct a > BitStruct from that string using BitStruct.new. Well, the main issue is that I thought it was a bit clumsy to have to both ask for the byte length and do the reading. Now, I'm not sure this can be done for a structure with a "rest" parameter, but for anything else it should be possible for the user of bit-struct to do the read and structure init in one step, say: mrk_head = MRKHeader.read(f) or mrk_head = MRKHeader.new mrk_head << f Actually, those two steps should be combinable into something like: mrk_head = MRKHeader.new << f The point is that the user of bit-struct should be able to avoid knowing the actual byte length if at all possible. This might even be possible to do with something that has a rest parameter, so long as you have a maximum size limitation on "rest", and then just use however many bytes you get back in one read call (reasonable behavior for sockets). > 3. Defining data wrappers. > > Hm.... I've needed that too. I'll think about it. I can think of at least two ways: one is for most built-in field types to have overrideable data_in and data_out procedures that take a single argument and by default just return their argument but someone who needs, say, a date stored as seconds-since-1970 could then just subclass BitStruct::UnsignedField and override data_in and data_out. The other is to add an extra data_in and data_out option to all fields whose values should be Procs. Actually, it's probably feasible to implement both.