[SOLVED] How do I skip more than one byte at a time in an OCAML-stream ??
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I skip more than one byte at a time in an OCAML-stream ??
Hi,
I try to use OCAML to run through a binary file with different types of records. I need data from just fewer than 5 % of these records. The size of the field which I need from those records is relatively small compared to the total record size. This means that I need to skip a lot of useless bytes in the process.
Every record is preceded by two bytes which contain its total length.
I use a stream, so that I can use argument matching with a function like:
let rec prse=parser [< 'hi ;'lo ; r >] -> ( checkrecord (256*(int_of_char hi)+(int_of_char lo)) r; prse r )
|[<>]->();;
the checkrecord function skips to the end of the record, if it finds the current record type does not contain info to be retrieved, or skips to the start of the field it needs to get data from, before it skips to the end of the record.
My problem is how to ignore all the bytes I do not need. The only way I found to do this is
let skip nr = function thestream->for i = 1 to nr do junk thestream done;;
To do this in a loop seems terribly inefficient.
Cannot I junk n bytes at a time ? Eg with a destructive npeek or so.
Cannot I junk n bytes at a time ? Eg with a destructive npeek or so.
No, there is no such function.
Quote:
To do this in a loop seems terribly inefficient.
First of all, don't assume it's inefficient, test it and see if it runs fast enough. Second, the stream abstraction is meant for sequential access, if you want random access, you should consider a different interface, like the General Input Functions (which include a seek function).
You are right about the sequential access. I had not realized yet, but I cannot match more than the first element in the stream. ie. I cannot do anything like
let checkrecord n = parser
[< ''A' ; ''B' ; r >]->( dosthforAB r)
| [< ''A' ; ''C' ; r >]->(dosthforAC r)
| ..
etc
I would need to bind A and B to variables, and match those variables in a tuple. That will give code which is comparable to what you do with the general input functions. In both cases the code is not much clearer than it is in C.
Ocaml simply is not the right tool for this problem. I shall use C.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.