LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2012, 09:33 AM   #1
muggabug
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
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
 
Old 04-29-2012, 11:29 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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).
 
Old 04-30-2012, 07:13 AM   #3
muggabug
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Time (gnome panel applet) - seconds skip by 2, sometimes springheeljim Linux - Desktop 0 08-23-2011 09:57 AM
[SOLVED] memcpy fails to copy data, but byte by byte assignment work venu_s Programming 7 07-08-2011 03:29 AM
Reading one byte at a time. prushik Programming 5 09-20-2009 01:35 AM
how to skip fsck at boot time? ItsNotMe SUSE / openSUSE 4 04-28-2005 05:31 AM
skip the crontab job for one time ust Linux - General 2 04-20-2005 03:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration