in reply to Re^8: Out of memory problems
in thread Out of memory problems
What would you do if you had an occation where the data wasn't on cut (ie. the first pattern was offset by 10 bytes) and the user had no way of knowing?
This reads a two framesized chunk and uses a regex to discover the alignment of the first full frame within it. If the offset is non-zero, then it discards that many bytes from the front of buffer (and issues a warning to indicate that), tops up the buffer to two full frames thereby aligning the read pointer to the start of the 3rd frame. Processes and outputs the first two full frames and then processes the rest a frame at a time as before.
#! perl -sw use strict; use bytes; open IN, '< :raw', $ARGV[ 0 ] or die "$ARGV[ 0 ] : $!"; open OUT, '> :raw', $ARGV[ 1 ] or die "$ARGV[ 1 ] : $!"; ## Grab a double buffer load first time so we can check & correct alig +nment local $/ = \768; my $buf = <IN>; ## Read two frames worth ## Check alignment. Assumes the xf4 .191 xf4 is unique per frame? $buf =~ m[(\xF4.{191}\xF4)]; ## Record the offset to the first frame my $offset = $-[0]; ## If there was an offset to the first match if( $offset != 0 ) { ## Chop off the leading junk substr( $buf, 0, $offset, '' ); ## Top up the buffer to two full frames read( IN, $buf, $offset, 768 - $offset ); warn "$offset bytes discarded from front of file."; } ## Process the first two whole frames print OUT unpack 'x2 a190 x2 a58 x132' x 2, $buf ## Now process as before local $/ = \384; ## Read file in 384 byte chunks. while( <IN> ) { print OUT unpack 'x2 a190 x2 a58', $_; } close IN; close OUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: Out of memory problems
by tperdue (Sexton) on Oct 26, 2004 at 12:31 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2004 at 12:45 UTC | |
by tperdue (Sexton) on Oct 26, 2004 at 13:14 UTC | |
|
Re^10: Out of memory problems
by tperdue (Sexton) on Oct 26, 2004 at 16:50 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2004 at 17:44 UTC |