LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I skip more than one byte at a time in an OCAML-stream ?? (https://www.linuxquestions.org/questions/programming-9/how-do-i-skip-more-than-one-byte-at-a-time-in-an-ocaml-stream-942404/)

muggabug 04-29-2012 09:33 AM

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.

thx for tips

ntubski 04-29-2012 11:29 AM

I don't have much experience with OCaml, but looking at the documentation for Stream:
Quote:

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).

muggabug 04-30-2012 07:13 AM

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.


All times are GMT -5. The time now is 06:50 AM.