[erlang-questions] matching binary against a file
Jeff Rogers
dvrsn@REDACTED
Thu Jun 21 22:55:20 CEST 2007
Mike McNally wrote:
>> Now this is all fine as long as I first read the entire string into
>> memory, but because you can't know beforehand how many bytes will be
>> read you have to overestimate and then handle whatever is left over.
>
> I've had success with a pattern that involves one process that reads the
> file block by block and only handles (in your case) the UTF-8
> interpretation. That process can send each value (character code) it
> recognizes to a client process. The higher-level code then gets a
> stream of ready-to-use characters just by calling "receive".
(Sorry for the double send)
But what is the best way for implementing this other process (which I
imagine ends up looking something like 'file' but with different
functions for reading different data types)? Catch the match and read
the next block on a badmatch?
e.g.,
read_3bytes({Buffer, File}) ->
case catch << Bytes:3/binary, Remainder/binary >> = Buffer of
{EXIT,{{badmatch,_},_} ->
{ok,NextBlock}=file:read(File,BLOCKSIZE),
read_3bytes({<< Buffer/binary, NextBlock/binary >>, File);
Any ->
{ok, Bytes, {Remainder, File}}
end.
This seems reasonable enough, except that I must transform every
instance of the binary match this way. I'd like to just wrap this in
something cleaner and more maintainable than repeating the same block of
code 20-odd times. But there isn't any way to pass a pattern to a
wrapping function and say "try to match this pattern and if it fails, do
some stuff and try again". Maybe I could make it into a macro, or make
the binary match into a fun and pass that? I'm not sure what the erlish
way is.
Thanks
-J
More information about the erlang-questions
mailing list