ROBOTestFrame/read_hexdump [ Functions ]

FUNCTION

Reads a hexdump made with xxd (part of vim http://www.vim.org/) This makes it possible to add files with all kinds of different formats and characters.

Storing it in hexdump format makes sure that these files are not changed when they are checked into cvs or unzipped.

This makes is possible to test cr/lf problems and internationalization issues.

INPUTS

RETURNS The decoded content of the file as a single string.

SOURCE

sub read_hexdump {
    my $path = shift;
    my $file = IO::File->new("<$path") or die "$path : $!";

    my $string = '';
    my @all_bytes = ();
    while ( my $line = <$file> ) {
        $line =~ s/^\S+:\s//; # remove address
        $line =~ s/\s\s+.*$//; # remove ascii
        $line =~ s/(\S\S)(\S\S)/$1 $2/g;
        # Now only the words are left.
        my @data = split( /\s/, $line );
        my @bytes = map { chr hex } @data;
        push( @all_bytes, @bytes );
    }
    # TODO try a join() here.
    foreach my $c ( @all_bytes ) {
        $string .= $c;
    }

    $file->close();
    return $string;
}