How can I parse binary files?

Fabio Vitale <fabio@sferaconsulting.it> writes:

This time I'm trying to write a binary file.

Q1: why does the structure MRKMessageFlags does not get the apropriate
values?

I'm not sure. It appears to be a bug in bit-struct. Here's a
workaround; in your code replace the bit that sets the flag bits with:

  # work around bit-struct nested types bug
  flags = msg.flags
  flags.flagSeen = 1
  flags.flagFlagged = 1
  msg.flags = flags

Q2: how do I convert a date to seconds-since-1970?

Don't assign date a string, assign it a Time object. The easiest way
to get one of those is with Time.parse:

  msg.date = Time.parse("Mon Jul 24 12:34:04 2006")

Your file loop now looks like this: (I also added calls to write out
the data to the file)

File.open("imap2.mrk", "wb") {|f|
#<MRKHeader version=1, uid_Validity=1106138982,
# uid_next=5887, last_write_counter=9962,
# unused="\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\r\n">

  mrk_header = MRKHeader.new()
  mrk_header.version = 1
  mrk_header.uid_Validity = 1106138982
  mrk_header.uid_next = 5887
  mrk_header.last_write_counter = 9962
  # I omitted modifying unused since in theory we don't need to
  # change the last two bytes to "\r\n" - it is unused, after all
  puts mrk_header.inspect
  f.write(mrk_header)

  msg = MRKMessage.new()
  msg.filename = "md50000006021.msg"

  # work around bit-struct nested types bug
  flags = msg.flags
  flags.flagSeen = 1
  flags.flagFlagged = 1
  msg.flags = flags

  msg.uid = 5885
  msg.msg_size = 4184
  msg.date = Time.parse("Mon Jul 24 12:34:04 2006")

  puts msg.inspect
  f.write(msg)
}

Q2: have look at Time.parse or ParseDate in stdlib. Keep in mind that
it is reeeeeally slow, it does all sorts of computation (gcd for
example), so if you need it, it helps to cache it's results, or
compute offsets from a known date

J.

···

On 7/24/06, Fabio Vitale <fabio@sferaconsulting.it> wrote:

Q2: how do I convert a date to seconds-since-1970?

Fabio Vitale wrote:

This time I'm trying to write a binary file.

Q1: why does the structure MRKMessageFlags does not get the apropriate values?

The problem is, unfortunately, not something that can easily be fixed in bit-struct's nested fields. When you do assignments like the following:

> msg.flags.flagSeen = 1
> msg.flags.flagAnswered = 0
> msg.flags.flagFlagged = 1

you are operating on a *copy* of the flags structure. The reason is that msg.flags returns a copy of that structure. It doesn't return a reference to a subfield of msg. It might be possible to return an object that delegates back to the msg structure, but I think that might actually be more confusing than the way it is now.

Your best bet is, as Daniel suggested:

     flags = msg.flags
     flags.flagSeen = 1
     flags.flagAnswered = 0
     flags.flagFlagged = 1
     msg.flags = flags

I will try to add a warning about this in the docs.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thank you all!
I'll investigate on the bit-struct nested types bug.
Anyway, with Daniel's workarund it seems to work.

···

--
Posted via http://www.ruby-forum.com/.