www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Find sequence in binary fiel and advance from there

reply alex1974 <alex.leisser gmail.com> writes:
I want to read a binary file and validate that it contains the 
sequence [73,68,51] (ID3). And than start to read from this 
position onward.

I could read in the whole file as an ubyte array but I would 
prefer a range-based solution. Has anyone done this and could 
point me in the right direction?

Alex
May 07 2019
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, May 07, 2019 at 08:26:16AM +0000, alex1974 via Digitalmars-d-learn
wrote:
 I want to read a binary file and validate that it contains the
 sequence [73,68,51] (ID3). And than start to read from this position
 onward.
 
 I could read in the whole file as an ubyte array but I would prefer a
 range-based solution. Has anyone done this and could point me in the
 right direction?
[...] For I/O, generally a block-based approach would be better than a range over individual bytes. It would be extremely slow if you incurred an OS syscall roundtrip per byte accessed. You can, however, still expose a range of ubyte on top of a buffered underlying access pattern, though, by using .joiner. However, for maximum ease, you could just use std.mmfile and mmap the file into your process address space, and just slice over the file contents as a ubyte[]. Maximum ease and also optimal performance (OS handles the paging for you). T -- Turning your clock 15 minutes ahead won't cure lateness---you're just making time go faster!
May 07 2019