Search code examples
rebolred-lang

How to do an incremental read of binary files


TL;DR: can I do an incremental read of binary files with Red or Rebol?

I would like to use Red to process some large (13MB to 2GB) structured binary files (Kurzweil synthesizer files). I've used other languages (C, Go, Tcl, Ruby, Dart) to walk through these files, and now I'd like to do the same with Red or Rebol.

Is there a way to incrementally read binary files, byte by byte? All I see is read/binary which seems to slurp the entire file at once (or a part of a file).

I'll need to jump around a little bit, too (either peek at the next byte, or skip to the end of a section, or skip past variable length strings to the start of data).

(Yes, I could make some helpers that tracked the position and used read/part/seek.)

I would like to make a call to the low level OS read/seek if that is possible - something new to learn.

This is on macos, but a portable solution would be great.

Thanks!

PS: "open/read %abc" gives an error "*** Script Error: open does not allow file! for its port argument", even though the help message say the port argument is "port [port! file! url! block!]"


Solution

  • Rebol has ports for that, which are planned for 0.7.0 release in Red. So, current I/O is very basic and buffer-only, and open is a preliminary stub.

    I would like to make a call to the low level OS read/seek if that is possible - something new to learn.

    You can leverage Rebol or Red/System FFI as a learning excercise.