/usr/lib/gwyddion/perl/Gwyddion/dump.pm is in gwyddion 2.44-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | package Gwyddion::dump;
# @(#) $Id: dump.pm 15061 2013-06-14 08:20:39Z dn2010 $
# Written by Yeti <yeti@gwyddion.net>, Public domain.
=head1 NAME
Gwyddion::dump -- Gwyddion plug-in proxy dump dumb file format handling.
=head1 FUNCTIONS
=over
=cut
use warnings;
use strict;
require Exporter;
use Config;
use IO::File;
our @ISA = qw( Exporter );
our @EXPORT_OK = qw( read write );
our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
# Sanity check
our $sizeof_double = length pack 'd', 42.0;
die "Need IEEE double format" if $sizeof_double != 8;
die "Non-little-endian byteorders not implemented"
if $Config{ 'byteorder' } ne '1234'
and $Config{ 'byteorder' } ne '12345678';
sub _dmove {
my ( $hash1, $key1, $hash2, $key2, $type ) = @_;
return if ! exists $hash1->{ $key1 };
my $value = $hash1->{ $key1 };
if ( defined $type ) {
if ( $type eq 'int' ) { $value = int $value }
elsif ( $type eq 'float' ) { $value = $value + 0.0 }
else { die "Internal error: Cannot convert to " . $type }
}
$hash2->{ $key2 } = $value;
delete $hash1->{ $key1 };
}
sub _read_dfield {
my ( $fh, $data, $base ) = @_;
my ( $c, $a, %dfield, $n, @a );
return undef if $fh->eof;
$fh->read( $c, 1 );
if ( $c ne '[' ) {
$fh->ungetc( ord $c );
return undef;
}
_dmove( $data, $base . '/xres', \%dfield, 'xres', 'int' );
_dmove( $data, $base . '/yres', \%dfield, 'yres', 'int' );
_dmove( $data, $base . '/xreal', \%dfield, 'xreal', 'float' );
_dmove( $data, $base . '/yreal', \%dfield, 'yreal', 'float' );
_dmove( $data, $base . '/unit-xy', \%dfield, 'unit-xy' );
_dmove( $data, $base . '/unit-z', \%dfield, 'unit-z' );
$n = $dfield{ 'xres' } * $dfield{ 'yres' };
$fh->read( $a, $n*$sizeof_double );
@a = unpack( "d[$n]", $a );
$dfield{ 'data' } = \@a;
$c = $fh->getline();
die if $c ne "]]\n";
$data->{ $base } = \%dfield;
return 'True';
}
=item read( filename )
Read a Gwyddion plug-in proxy dump file.
The file is returned as a hash table of dump key, value pairs.
Data fields are packed as references to hashes with following keys
(not all has to be present):
`xres' (x-resolution, in number of samples),
`yres' (y-resolution, in number of samples),
`xreal' (real x size, in base SI units),
`yreal' (real y size, in base SI units),
`unit-xy' (lateral units, base SI, like `m'),
`unit-z' (value units, base SI, like `m' or `A'),
`data' (the data field data itself, an array of floats).
Fatal errors are not handled, the function simply dies. If you have
anything meaningful to do after a fatal error, you have to catch
the error.
=cut
sub read {
my $fh = new IO::File;
my $line_re = "^([^=]+)=(.*)\n";
my $field_re = "^([^=]+)=\\[\n";
my %data;
die if ! $fh->open( $_[0], '<:bytes' );
while ( my $line = $fh->getline() ) {
if ( $line =~ m/$field_re/ ) {
my $key = $1;
next if _read_dfield( $fh, \%data, $key );
}
if ( $line =~ m/$line_re/ ) {
my $key = $1;
my $val = $2;
$data{ $key } = $val;
}
else {
die "Can't understand input\n"
}
}
$fh->close();
return \%data;
}
sub _dwrite {
my ( $fh, $dfield, $base, $key, $fmt ) = @_;
local $, = undef;
local $\ = undef;
if ( exists $dfield->{ $key } ) {
$fh->printf( '%s/%s=' . $fmt . "\n", $base, $key, $dfield->{ $key } )
}
}
sub _write_dfield {
my ( $fh, $dfield, $base ) = @_;
my ( $n, $a );
local $, = undef;
local $\ = undef;
_dwrite( $fh, $dfield, $base, 'xres', '%d' );
_dwrite( $fh, $dfield, $base, 'yres', '%d' );
_dwrite( $fh, $dfield, $base, 'xreal', '%g' );
_dwrite( $fh, $dfield, $base, 'yreal', '%g' );
_dwrite( $fh, $dfield, $base, 'unit-xy', '%s' );
_dwrite( $fh, $dfield, $base, 'unit-z', '%s' );
$n = $dfield->{ 'xres' }*$dfield->{ 'yres' };
$fh->print( "$base=[\n[" );
$a = $dfield->{ 'data' };
$a = pack( "d[$n]", @$a );
$fh->print( "$a]]\n" );
}
=item write( data, filename )
Write a Gwyddion plug-in proxy dump file.
The hash table to write is expected to follow the same conventions as
those returned by read(), please see its description for more.
Fatal errors are not handled, the function simply dies. If you have
anything meaningful to do after a fatal error, you have to catch
the error.
=cut
sub write {
my $fh = new IO::File;
my $data = $_[0];
$fh->open( $_[1], '>:bytes' );
local $, = undef;
local $\ = undef;
for my $k ( keys %$data ) {
my $v = $data->{ $k };
next if ref $v;
$fh->print( "$k=$v\n" );
}
for my $k ( keys %$data ) {
my $v = $data->{ $k };
next if not ref $v;
_write_dfield( $fh, $v, $k );
}
$fh->close();
}
1;
=back
|