in reply to Storing unordered data from file in memory
This is so weird. I was just working with S-record files yesterday. Here's the start of my S-record parser.
#!/usr/bin/perl use strict; use warnings; process( $ARGV[0] ); sub process { my $file = shift; # First, grab the contents of the firmware hex (S19) file my $contents = slurp( $file ); my @lines = split /\n/, $contents; my %data; for my $line ( @lines ) { next if $line =~ /^S0/; # skip comments next if $line =~ /^S7/; # skip 32-bit termination records next if $line =~ /^S8/; # skip 24-bit termination records next if $line =~ /^S9/; # skop 16-bit termination records # We could do all sorts of things to bulletproof this but it w +orks my ( $rectype, $length, $address, $data, $checksum ) = $line =~ / ^ # start of line (S[123]) # S1, S2, or S3 record ([0-9A-F]{2}) # record length including the address +and checksum ([0-9A-F]{8}) # address ([0-9A-F]+) # data or payload ([0-9A-F]{2}) # checksum \r?$ # end of line /x; my @bytes = unpack "(A2)*", $data; my $numbytes = scalar @bytes; my $nextaddr = sprintf( "%X", hex( $address ) + $numbytes ); $data{ $address } = { rectype => $rectype, length => $length, data => $data, checksum => $checksum, bytes => [ @bytes ], numbytes => $numbytes, nextaddr => $nextaddr, }; } my @ordered = sort { hex( $a ) <=> hex( $b ) } keys %data; for my $recno ( 0 .. $#ordered ) { my $curraddr = $ordered[ $recno ]; if ( ! exists $data{ $curraddr } ) { print "$curraddr <no data found>\n"; next; } my $rec = $data{ $curraddr }; my $nextrec = $ordered[ $recno + 1 ]; print "$rec->{rectype} $curraddr $rec->{length} $rec->{data} $ +rec->{checksum}\n"; if ( defined $nextrec && $nextrec ne $rec->{nextaddr} ) { print " <no data until $nextrec>\n"; } } } sub slurp { my $file = shift; my $text = do { local( @ARGV, $/ ) = $file ; <> }; return $text; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Storing unordered data from file in memory
by Dirk80 (Pilgrim) on Jul 21, 2010 at 19:59 UTC | |
by AnomalousMonk (Archbishop) on Jul 21, 2010 at 21:02 UTC | |
by Mr. Muskrat (Canon) on Jul 22, 2010 at 20:37 UTC |