| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::MATFile; |
|
2
|
|
|
|
|
|
|
require Exporter; |
|
3
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
4
|
|
|
|
|
|
|
@EXPORT_OK = qw/read_matfile mi2word mx2word/; |
|
5
|
1
|
|
|
1
|
|
26243
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
42
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
7
|
1
|
|
|
1
|
|
21
|
use 5.008; |
|
|
1
|
|
|
|
|
9
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
92
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
10
|
1
|
|
|
1
|
|
1124
|
use Gzip::Faster; |
|
|
1
|
|
|
|
|
1572
|
|
|
|
1
|
|
|
|
|
133
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Nasty flag for debuggers |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERBOSE; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Matlab constants |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Top-level types |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use constant { |
|
21
|
1
|
|
|
|
|
346
|
miINT8 => 1, |
|
22
|
|
|
|
|
|
|
miUINT8 => 2, |
|
23
|
|
|
|
|
|
|
miINT16 => 3, |
|
24
|
|
|
|
|
|
|
miUINT16 => 4, |
|
25
|
|
|
|
|
|
|
miINT32 => 5, |
|
26
|
|
|
|
|
|
|
miUINT32 => 6, |
|
27
|
|
|
|
|
|
|
miSINGLE => 7, |
|
28
|
|
|
|
|
|
|
miRESERVE1 => 8, |
|
29
|
|
|
|
|
|
|
miDOUBLE => 9, |
|
30
|
|
|
|
|
|
|
miRESERVE2 => 10, |
|
31
|
|
|
|
|
|
|
miRESERVE3 => 11, |
|
32
|
|
|
|
|
|
|
miINT64 => 12, |
|
33
|
|
|
|
|
|
|
miUINT64 => 13, |
|
34
|
|
|
|
|
|
|
miMATRIX => 14, |
|
35
|
|
|
|
|
|
|
miCOMPRESSED => 15, |
|
36
|
|
|
|
|
|
|
miUTF8 => 16, |
|
37
|
|
|
|
|
|
|
miUTF16 => 17, |
|
38
|
|
|
|
|
|
|
miUTF32 => 18, |
|
39
|
1
|
|
|
1
|
|
6
|
}; |
|
|
1
|
|
|
|
|
1
|
|
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my %names = reverse ( |
|
42
|
|
|
|
|
|
|
miINT8 => 1, |
|
43
|
|
|
|
|
|
|
miUINT8 => 2, |
|
44
|
|
|
|
|
|
|
miINT16 => 3, |
|
45
|
|
|
|
|
|
|
miUINT16 => 4, |
|
46
|
|
|
|
|
|
|
miINT32 => 5, |
|
47
|
|
|
|
|
|
|
miUINT32 => 6, |
|
48
|
|
|
|
|
|
|
miSINGLE => 7, |
|
49
|
|
|
|
|
|
|
miRESERVE1 => 8, |
|
50
|
|
|
|
|
|
|
miDOUBLE => 9, |
|
51
|
|
|
|
|
|
|
miRESERVE2 => 10, |
|
52
|
|
|
|
|
|
|
miRESERVE3 => 11, |
|
53
|
|
|
|
|
|
|
miINT64 => 12, |
|
54
|
|
|
|
|
|
|
miUINT64 => 13, |
|
55
|
|
|
|
|
|
|
miMATRIX => 14, |
|
56
|
|
|
|
|
|
|
miCOMPRESSED => 15, |
|
57
|
|
|
|
|
|
|
miUTF8 => 16, |
|
58
|
|
|
|
|
|
|
miUTF16 => 17, |
|
59
|
|
|
|
|
|
|
miUTF32 => 18, |
|
60
|
|
|
|
|
|
|
); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Matrix types |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
use constant { |
|
65
|
1
|
|
|
|
|
3603
|
mxCELL_CLASS => 1, |
|
66
|
|
|
|
|
|
|
mxSTRUCT_CLASS => 2, |
|
67
|
|
|
|
|
|
|
mxOBJECT_CLASS => 3, |
|
68
|
|
|
|
|
|
|
mxCHAR_CLASS => 4, |
|
69
|
|
|
|
|
|
|
mxSPARSE_CLASS => 5, |
|
70
|
|
|
|
|
|
|
mxDOUBLE_CLASS => 6, |
|
71
|
|
|
|
|
|
|
mxSINGLE_CLASS => 7, |
|
72
|
|
|
|
|
|
|
mxINT8_CLASS => 8, |
|
73
|
|
|
|
|
|
|
mxUINT8_CLASS => 9, |
|
74
|
|
|
|
|
|
|
mxINT16_CLASS => 10, |
|
75
|
|
|
|
|
|
|
mxUINT16_CLASS => 11, |
|
76
|
|
|
|
|
|
|
mxINT32_CLASS => 12, |
|
77
|
|
|
|
|
|
|
mxUINT32_CLASS => 13, |
|
78
|
|
|
|
|
|
|
mxINT64_CLASS => 14, |
|
79
|
|
|
|
|
|
|
mxUINT64_CLASS => 15, |
|
80
|
1
|
|
|
1
|
|
6
|
}; |
|
|
1
|
|
|
|
|
2
|
|
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my %mx = reverse ( |
|
83
|
|
|
|
|
|
|
mxCELL_CLASS => 1, |
|
84
|
|
|
|
|
|
|
mxSTRUCT_CLASS => 2, |
|
85
|
|
|
|
|
|
|
mxOBJECT_CLASS => 3, |
|
86
|
|
|
|
|
|
|
mxCHAR_CLASS => 4, |
|
87
|
|
|
|
|
|
|
mxSPARSE_CLASS => 5, |
|
88
|
|
|
|
|
|
|
mxDOUBLE_CLASS => 6, |
|
89
|
|
|
|
|
|
|
mxSINGLE_CLASS => 7, |
|
90
|
|
|
|
|
|
|
mxINT8_CLASS => 8, |
|
91
|
|
|
|
|
|
|
mxUINT8_CLASS => 9, |
|
92
|
|
|
|
|
|
|
mxINT16_CLASS => 10, |
|
93
|
|
|
|
|
|
|
mxUINT16_CLASS => 11, |
|
94
|
|
|
|
|
|
|
mxINT32_CLASS => 12, |
|
95
|
|
|
|
|
|
|
mxUINT32_CLASS => 13, |
|
96
|
|
|
|
|
|
|
mxINT64_CLASS => 14, |
|
97
|
|
|
|
|
|
|
mxUINT64_CLASS => 15, |
|
98
|
|
|
|
|
|
|
); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# See MATFile.pod for documentation |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub mi2word |
|
103
|
|
|
|
|
|
|
{ |
|
104
|
0
|
|
|
0
|
1
|
0
|
my ($type) = @_; |
|
105
|
0
|
|
|
|
|
0
|
return $names{$type}; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# See MATFile.pod for documentation |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub mx2word |
|
111
|
|
|
|
|
|
|
{ |
|
112
|
0
|
|
|
0
|
1
|
0
|
my ($class) = @_; |
|
113
|
0
|
|
|
|
|
0
|
return $mx{$class}; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# See MATFile.pod for documentation |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub read_matfile |
|
119
|
|
|
|
|
|
|
{ |
|
120
|
1
|
|
|
1
|
1
|
8
|
my ($file_name) = @_; |
|
121
|
1
|
|
|
|
|
3
|
validate_file_name ($file_name); |
|
122
|
1
|
|
|
|
|
9
|
my $obj = Data::MATFile->new ($file_name); |
|
123
|
1
|
|
|
|
|
3
|
$obj->read_file_header (); |
|
124
|
|
|
|
|
|
|
|
|
125
|
5
|
|
|
|
|
8
|
again: |
|
126
|
|
|
|
|
|
|
my $ok = $obj->read_object (); |
|
127
|
5
|
100
|
|
|
|
7
|
if ($ok) { |
|
128
|
4
|
|
|
|
|
8
|
goto again; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
1
|
|
|
|
|
2
|
return $obj; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Private functions below this. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 read_file_header |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$obj->read_file_header (); |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Read the header of the MAT-File. This stores the "descriptive text" of |
|
141
|
|
|
|
|
|
|
the file in a field |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$obj->{descriptive_text} |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
It also sets the endian-ness of the file. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub read_file_header |
|
150
|
|
|
|
|
|
|
{ |
|
151
|
1
|
|
|
1
|
0
|
2
|
my ($obj) = @_; |
|
152
|
1
|
|
|
|
|
4
|
$obj->{descriptive_text} = $obj->read_bytes (116, 1); |
|
153
|
1
|
50
|
|
|
|
3
|
if ($obj->{verbose}) { |
|
154
|
0
|
|
|
|
|
0
|
print "The descriptive text is \"$obj->{descriptive_text}\".\n"; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
1
|
|
|
|
|
3
|
$obj->{subsys_data_offset} = $obj->read_bytes (8); |
|
157
|
|
|
|
|
|
|
# Header Flag Fields |
|
158
|
1
|
|
|
|
|
2
|
my $hff; |
|
159
|
1
|
|
|
|
|
2
|
$hff = $obj->read_bytes (4, 1); |
|
160
|
1
|
|
|
|
|
5
|
my ($v, $e) = unpack ("SS", $hff); |
|
161
|
1
|
50
|
|
|
|
3
|
if ($v != 0x0100) { |
|
162
|
0
|
|
|
|
|
0
|
carp "$obj->{file_name}: Unknown version $v"; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
1
|
|
|
|
|
2
|
$obj->set_endianness ($e); |
|
165
|
1
|
50
|
|
|
|
2
|
if ($obj->{verbose}) { |
|
166
|
0
|
|
|
|
|
0
|
print "Successfully read file header.\n"; |
|
167
|
|
|
|
|
|
|
} |
|
168
|
1
|
|
|
|
|
1
|
return; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 validate_file_name |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
validate_file_name ('file-name.mat'); |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Given a file name, make sure that it is not an empty string and that |
|
176
|
|
|
|
|
|
|
the file exists. This does not return a value since it dies on error. |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub validate_file_name |
|
181
|
|
|
|
|
|
|
{ |
|
182
|
1
|
|
|
1
|
0
|
2
|
my ($file_name) = @_; |
|
183
|
1
|
50
|
|
|
|
3
|
if (! $file_name) { |
|
184
|
0
|
|
|
|
|
0
|
croak "supply a file name"; |
|
185
|
|
|
|
|
|
|
} |
|
186
|
1
|
50
|
|
|
|
24
|
if (! -f $file_name) { |
|
187
|
0
|
|
|
|
|
0
|
croak "no such file '$file_name'"; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
1
|
|
|
|
|
2
|
return; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 new |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
my $obj = Data::MATFile->new ('file-name.mat'); |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Start a new object for reading the specified file name. The file name |
|
197
|
|
|
|
|
|
|
must be supplied, but is not checked (the checking is part of |
|
198
|
|
|
|
|
|
|
L). |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub new |
|
203
|
|
|
|
|
|
|
{ |
|
204
|
1
|
|
|
1
|
0
|
1
|
my ($package, $file_name) = @_; |
|
205
|
1
|
|
|
|
|
3
|
my $obj = { |
|
206
|
|
|
|
|
|
|
file_name => $file_name, |
|
207
|
|
|
|
|
|
|
}; |
|
208
|
1
|
|
|
|
|
2
|
bless $obj; |
|
209
|
1
|
|
|
|
|
6
|
$obj->{verbose} = $VERBOSE; |
|
210
|
1
|
50
|
|
|
|
3
|
if ($obj->{verbose}) { |
|
211
|
0
|
|
|
|
|
0
|
print <<'EOF'; |
|
212
|
|
|
|
|
|
|
__ __ _ _ _ _ |
|
213
|
|
|
|
|
|
|
\ \ / /__ _ __| |__ ___ ___ ___ ___ | |__ (_) ___ ___| |_ |
|
214
|
|
|
|
|
|
|
\ \ / / _ \ '__| '_ \ / _ \/ __|/ _ \ / _ \| '_ \| |/ _ \/ __| __| |
|
215
|
|
|
|
|
|
|
\ V / __/ | | |_) | (_) \__ \ __/ | (_) | |_) | | __/ (__| |_ |
|
216
|
|
|
|
|
|
|
\_/ \___|_| |_.__/ \___/|___/\___| \___/|_.__// |\___|\___|\__| |
|
217
|
|
|
|
|
|
|
|__/ |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
EOF |
|
220
|
0
|
|
|
|
|
0
|
print "File name is $obj->{file_name}.\n"; |
|
221
|
|
|
|
|
|
|
} |
|
222
|
1
|
|
|
|
|
3
|
$obj->open_matfile (); |
|
223
|
1
|
|
|
|
|
2
|
return $obj; |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head2 open_matfile |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
$obj->open_matfile (); |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Open the MAT-File for reading and set up the file handle in the object. |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub open_matfile |
|
235
|
|
|
|
|
|
|
{ |
|
236
|
1
|
|
|
1
|
0
|
1
|
my ($obj) = @_; |
|
237
|
1
|
|
|
|
|
32
|
open ($obj->{fh}, "<:raw", $obj->{file_name}); |
|
238
|
1
|
50
|
|
|
|
4
|
if (! $obj->{fh}) { |
|
239
|
|
|
|
|
|
|
# Don't use $obj->error since file not open yet. |
|
240
|
0
|
|
|
|
|
0
|
croak "could not open $obj->{file_name}: $!"; |
|
241
|
|
|
|
|
|
|
} |
|
242
|
1
|
|
|
|
|
1
|
return; |
|
243
|
|
|
|
|
|
|
} |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 set_endianness |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
$obj->set_endianness ($flag); |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Set the endian-ness of C<$obj> from C<$flag>, which is part of the |
|
250
|
|
|
|
|
|
|
header, as read in L. |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=cut |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub set_endianness |
|
255
|
|
|
|
|
|
|
{ |
|
256
|
1
|
|
|
1
|
0
|
2
|
my ($obj, $flag) = @_; |
|
257
|
1
|
|
|
|
|
4
|
my $e = chr ($flag / 0x100) . chr ($flag % 0x100); |
|
258
|
1
|
50
|
|
|
|
3
|
if ($e eq 'MI') { |
|
|
|
0
|
|
|
|
|
|
|
259
|
1
|
|
|
|
|
2
|
$obj->{endian} = 0; |
|
260
|
|
|
|
|
|
|
} |
|
261
|
|
|
|
|
|
|
elsif ($e eq 'IM') { |
|
262
|
0
|
|
|
|
|
0
|
$obj->{endian} = 1; |
|
263
|
0
|
|
|
|
|
0
|
$obj->error ("Opposite endian-ness files are not supported"); |
|
264
|
|
|
|
|
|
|
} |
|
265
|
|
|
|
|
|
|
else { |
|
266
|
0
|
|
|
|
|
0
|
$obj->error ("Endian-ness flag $flag of $obj->{file_name} was not parsed"); |
|
267
|
|
|
|
|
|
|
} |
|
268
|
1
|
50
|
|
|
|
3
|
if ($obj->{verbose}) { |
|
269
|
0
|
|
|
|
|
0
|
print "Endian-ness of input file is $obj->{endian}.\n"; |
|
270
|
|
|
|
|
|
|
} |
|
271
|
1
|
|
|
|
|
1
|
return; |
|
272
|
|
|
|
|
|
|
} |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub set_data |
|
275
|
|
|
|
|
|
|
{ |
|
276
|
0
|
|
|
0
|
0
|
0
|
my ($obj, $data) = @_; |
|
277
|
0
|
|
|
|
|
0
|
$obj->{input_data} = $data; |
|
278
|
|
|
|
|
|
|
} |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
# Data-reading functions |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 read_object |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
my $array = $obj->read_object (); |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Read an object from the file. |
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=cut |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
sub read_object |
|
291
|
|
|
|
|
|
|
{ |
|
292
|
5
|
|
|
5
|
0
|
8
|
my ($obj) = @_; |
|
293
|
5
|
50
|
|
|
|
8
|
die "bad call" if wantarray != 0; |
|
294
|
5
|
|
|
|
|
8
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
295
|
5
|
100
|
|
|
|
11
|
if ($obj->{eof}) { |
|
296
|
1
|
|
|
|
|
1
|
return undef; |
|
297
|
|
|
|
|
|
|
} |
|
298
|
4
|
50
|
|
|
|
8
|
if ($type == miCOMPRESSED) { |
|
|
|
50
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
# print "Oh, you have $n_bytes of data.\n"; |
|
300
|
0
|
|
|
|
|
0
|
my $cdata = $obj->read_bytes ($n_bytes); |
|
301
|
0
|
|
|
|
|
0
|
my $uncdata = gunzip ($cdata); |
|
302
|
0
|
|
|
|
|
0
|
$obj->set_data ($uncdata); |
|
303
|
|
|
|
|
|
|
# print "Read again as uncompressed.\n"; |
|
304
|
0
|
|
|
|
|
0
|
my $value = $obj->read_object (); |
|
305
|
0
|
|
|
|
|
0
|
$obj->set_data (undef); |
|
306
|
0
|
|
|
|
|
0
|
return $value; |
|
307
|
|
|
|
|
|
|
} |
|
308
|
|
|
|
|
|
|
elsif ($type == miMATRIX) { |
|
309
|
4
|
|
|
|
|
7
|
return $obj->read_matrix ($n_bytes, $data); |
|
310
|
|
|
|
|
|
|
} |
|
311
|
|
|
|
|
|
|
else { |
|
312
|
0
|
|
|
|
|
0
|
my $name = $names{$type}; |
|
313
|
0
|
0
|
|
|
|
0
|
if (! defined $name) { |
|
314
|
0
|
|
|
|
|
0
|
$name = 'unknown'; |
|
315
|
|
|
|
|
|
|
} |
|
316
|
0
|
|
|
|
|
0
|
$obj->error ("cannot handle non-matrix data of type $name here"); |
|
317
|
|
|
|
|
|
|
} |
|
318
|
0
|
|
|
|
|
0
|
return undef; |
|
319
|
|
|
|
|
|
|
} |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
sub read_matrix |
|
322
|
|
|
|
|
|
|
{ |
|
323
|
4
|
|
|
4
|
0
|
6
|
my ($obj, $n_bytes, $data) = @_; |
|
324
|
4
|
50
|
|
|
|
6
|
if ($obj->{verbose}) { |
|
325
|
0
|
|
|
|
|
0
|
print <<'EOF'; |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
__ _ _ __ _ __ __ _ _ _ |
|
328
|
|
|
|
|
|
|
/ _` | '__| '__/ _` | | | | |
|
329
|
|
|
|
|
|
|
| (_| | | | | | (_| | |_| | |
|
330
|
|
|
|
|
|
|
\__,_|_| |_| \__,_|\__, | |
|
331
|
|
|
|
|
|
|
|___/ |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
EOF |
|
334
|
|
|
|
|
|
|
} |
|
335
|
4
|
|
|
|
|
7
|
my ($class) = $obj->read_array_flags (); |
|
336
|
|
|
|
|
|
|
# Flag for numeric classes. |
|
337
|
4
|
|
|
|
|
7
|
my $numeric = $obj->is_numeric ($class); |
|
338
|
4
|
|
|
|
|
11
|
my @dim = $obj->get_array_dimensions (); |
|
339
|
4
|
|
|
|
|
7
|
my $name = $obj->get_array_name (); |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
# This is the return value. It has to be stored in this |
|
342
|
|
|
|
|
|
|
# convoluted way because we need to store things like the |
|
343
|
|
|
|
|
|
|
# "complex" flag and the name of the matrix. |
|
344
|
|
|
|
|
|
|
|
|
345
|
4
|
|
|
|
|
10
|
my $matrix = { |
|
346
|
|
|
|
|
|
|
dimensions => \@dim, |
|
347
|
|
|
|
|
|
|
name => $name, |
|
348
|
|
|
|
|
|
|
class => $class, |
|
349
|
|
|
|
|
|
|
}; |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
# Handle the different possible types of matrix data. |
|
352
|
|
|
|
|
|
|
|
|
353
|
4
|
50
|
|
|
|
5
|
if ($numeric) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
354
|
4
|
|
|
|
|
7
|
my $numbers = $obj->get_numeric_array ($class, \@dim); |
|
355
|
4
|
|
|
|
|
6
|
$matrix->{array} = $numbers, |
|
356
|
|
|
|
|
|
|
} |
|
357
|
|
|
|
|
|
|
elsif ($class == mxSTRUCT_CLASS) { |
|
358
|
0
|
|
|
|
|
0
|
my $field_names = $obj->read_field_names (); |
|
359
|
0
|
|
|
|
|
0
|
for (0..$#$field_names) { |
|
360
|
0
|
|
|
|
|
0
|
my $name = $field_names->[$_]; |
|
361
|
0
|
0
|
|
|
|
0
|
if (! $name) { |
|
362
|
0
|
|
|
|
|
0
|
$obj->error ("no name for element $_"); |
|
363
|
|
|
|
|
|
|
} |
|
364
|
0
|
|
|
|
|
0
|
my $subarray = $obj->read_matrix (); |
|
365
|
0
|
|
|
|
|
0
|
$matrix->{array}->{$name} = $subarray; |
|
366
|
|
|
|
|
|
|
} |
|
367
|
|
|
|
|
|
|
} |
|
368
|
|
|
|
|
|
|
elsif ($class == mxCELL_CLASS) { |
|
369
|
0
|
|
|
|
|
0
|
my $cells = $obj->read_cells (\@dim); |
|
370
|
0
|
|
|
|
|
0
|
$matrix->{array} = $cells; |
|
371
|
|
|
|
|
|
|
} |
|
372
|
|
|
|
|
|
|
elsif ($class == mxCHAR_CLASS) { |
|
373
|
0
|
|
|
|
|
0
|
my $chars = $obj->read_chars (); |
|
374
|
0
|
0
|
|
|
|
0
|
if ($obj->{verbose}) { |
|
375
|
0
|
|
|
|
|
0
|
print "$_: $chars\n"; |
|
376
|
|
|
|
|
|
|
} |
|
377
|
0
|
|
|
|
|
0
|
$matrix->{array} = $chars; |
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
else { |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# Unsupported matrix class. |
|
382
|
|
|
|
|
|
|
|
|
383
|
0
|
|
|
|
|
0
|
$obj->error ("cannot handle $mx{$class} matrices"); |
|
384
|
|
|
|
|
|
|
} |
|
385
|
|
|
|
|
|
|
|
|
386
|
4
|
50
|
|
|
|
10
|
if (! defined $matrix->{array}) { |
|
387
|
0
|
|
|
|
|
0
|
die "error: empty data section of matrix"; |
|
388
|
|
|
|
|
|
|
} |
|
389
|
4
|
|
|
|
|
5
|
$obj->{data}->{$matrix->{name}} = $matrix; |
|
390
|
4
|
|
|
|
|
7
|
return $matrix; |
|
391
|
|
|
|
|
|
|
} |
|
392
|
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
=head2 read_chars |
|
394
|
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
my $chars = $obj->read_chars (); |
|
396
|
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
Read the characters from a cell matrix consisting of characters. |
|
398
|
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
=cut |
|
400
|
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
sub read_chars |
|
402
|
|
|
|
|
|
|
{ |
|
403
|
0
|
|
|
0
|
0
|
0
|
my ($obj) = @_; |
|
404
|
0
|
|
|
|
|
0
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
405
|
0
|
0
|
|
|
|
0
|
if (! $data) { |
|
406
|
0
|
|
|
|
|
0
|
$data = $obj->read_bytes ($n_bytes); |
|
407
|
|
|
|
|
|
|
} |
|
408
|
0
|
|
|
|
|
0
|
my @chars = unpack ("ax" x ($n_bytes/2), $data); |
|
409
|
0
|
|
|
|
|
0
|
my $chars = join '', @chars; |
|
410
|
0
|
|
|
|
|
0
|
return $chars; |
|
411
|
|
|
|
|
|
|
} |
|
412
|
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=head2 read_cells |
|
414
|
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
$obj->read_cells ($dim); |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
Read the cells of a mxCELL_CLASS submatrix. |
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
=cut |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
sub read_cells |
|
422
|
|
|
|
|
|
|
{ |
|
423
|
0
|
|
|
0
|
0
|
0
|
my ($obj, $dim) = @_; |
|
424
|
0
|
0
|
|
|
|
0
|
die "bad call" if wantarray () != 0; |
|
425
|
0
|
|
|
|
|
0
|
my $n_cells = n_from_dim ($dim); |
|
426
|
0
|
|
|
|
|
0
|
my @cells; |
|
427
|
0
|
|
|
|
|
0
|
for (1..$n_cells) { |
|
428
|
0
|
|
|
|
|
0
|
my $cell = $obj->read_matrix (); |
|
429
|
0
|
|
|
|
|
0
|
push @cells, $cell; |
|
430
|
|
|
|
|
|
|
} |
|
431
|
0
|
|
|
|
|
0
|
return \@cells; |
|
432
|
|
|
|
|
|
|
} |
|
433
|
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=head2 read_array_flags |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
my ($class) = $obj->read_array_flags (); |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
Read the Array Flags. Currently this only returns the class of the |
|
439
|
|
|
|
|
|
|
array. For the sake of future expansion, always call in list context. |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
=cut |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
sub read_array_flags |
|
444
|
|
|
|
|
|
|
{ |
|
445
|
4
|
|
|
4
|
0
|
4
|
my ($obj) = @_; |
|
446
|
4
|
|
|
|
|
4
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
447
|
4
|
50
|
|
|
|
7
|
if ($type != miUINT32) { |
|
448
|
0
|
|
|
|
|
0
|
$obj->error ("bad type $type for array flags"); |
|
449
|
|
|
|
|
|
|
} |
|
450
|
4
|
50
|
|
|
|
5
|
if ($n_bytes != 8) { |
|
451
|
0
|
|
|
|
|
0
|
$obj->error ("array flags $n_bytes not 8 bytes"); |
|
452
|
|
|
|
|
|
|
} |
|
453
|
4
|
|
|
|
|
5
|
my $f = $obj->read_bytes (8); |
|
454
|
4
|
|
|
|
|
5
|
my ($x, $y) = unpack ("LL", $f); |
|
455
|
4
|
|
|
|
|
5
|
my $flags = int ($x / 0x100) % 0x100; |
|
456
|
4
|
|
|
|
|
5
|
my $complex = $flags & 0x10; |
|
457
|
4
|
50
|
|
|
|
6
|
if ($obj->{verbose}) { |
|
458
|
0
|
0
|
|
|
|
0
|
if ($complex) { |
|
459
|
0
|
|
|
|
|
0
|
print "Is complex.\n"; |
|
460
|
|
|
|
|
|
|
} |
|
461
|
|
|
|
|
|
|
else { |
|
462
|
0
|
|
|
|
|
0
|
print "Is not complex.\n"; |
|
463
|
|
|
|
|
|
|
} |
|
464
|
|
|
|
|
|
|
} |
|
465
|
4
|
50
|
|
|
|
5
|
if ($complex) { |
|
466
|
0
|
|
|
|
|
0
|
$obj->error ("Cannot handle complex matrices"); |
|
467
|
|
|
|
|
|
|
} |
|
468
|
4
|
|
|
|
|
4
|
my $class = $x % 0x100; |
|
469
|
4
|
50
|
|
|
|
5
|
if ($class > 15) { |
|
470
|
0
|
|
|
|
|
0
|
carp "Unknown matrix class $class"; |
|
471
|
|
|
|
|
|
|
} |
|
472
|
4
|
50
|
|
|
|
9
|
if ($obj->{verbose}) { |
|
473
|
0
|
|
|
|
|
0
|
print "Class is $mx{$class}.\n"; |
|
474
|
|
|
|
|
|
|
} |
|
475
|
4
|
|
|
|
|
5
|
return ($class); |
|
476
|
|
|
|
|
|
|
} |
|
477
|
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=head2 read_field_names |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
my $names = $obj->read_field_names (); |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
Read the field names of a "Structure MAT-File Data Element". |
|
483
|
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=cut |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
sub read_field_names |
|
487
|
|
|
|
|
|
|
{ |
|
488
|
0
|
|
|
0
|
0
|
0
|
my ($obj) = @_; |
|
489
|
0
|
0
|
|
|
|
0
|
if ($obj->{verbose}) { |
|
490
|
0
|
|
|
|
|
0
|
print "Reading field names of a structure.\n"; |
|
491
|
|
|
|
|
|
|
} |
|
492
|
0
|
|
|
|
|
0
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
493
|
0
|
0
|
|
|
|
0
|
if ($type != miINT32) { |
|
494
|
0
|
|
|
|
|
0
|
$obj->error ("wrong type $type for field name length"); |
|
495
|
|
|
|
|
|
|
} |
|
496
|
0
|
0
|
|
|
|
0
|
if ($n_bytes != 4) { |
|
497
|
0
|
|
|
|
|
0
|
$obj->error ("wrong number $n_bytes for field name length"); |
|
498
|
|
|
|
|
|
|
} |
|
499
|
0
|
0
|
|
|
|
0
|
if (! defined $data) { |
|
500
|
0
|
|
|
|
|
0
|
$obj->error ("Field name length is not provided"); |
|
501
|
|
|
|
|
|
|
} |
|
502
|
0
|
|
|
|
|
0
|
my $field_name_length = unpack ("l", $data); |
|
503
|
0
|
0
|
|
|
|
0
|
if ($obj->{verbose}) { |
|
504
|
0
|
|
|
|
|
0
|
print "Field name length is $field_name_length.\n"; |
|
505
|
|
|
|
|
|
|
} |
|
506
|
0
|
|
|
|
|
0
|
($type, $n_bytes, $data) = $obj->read_data_header (); |
|
507
|
0
|
0
|
|
|
|
0
|
if ($type != miINT8) { |
|
508
|
0
|
|
|
|
|
0
|
$obj->error ("wrong type $type for field names"); |
|
509
|
|
|
|
|
|
|
} |
|
510
|
0
|
|
|
|
|
0
|
my $n_names = $n_bytes / $field_name_length; |
|
511
|
0
|
0
|
|
|
|
0
|
if ($n_names != int ($n_names)) { |
|
512
|
0
|
|
|
|
|
0
|
$obj->error ("name field bytes $n_bytes not multiple of $field_name_length"); |
|
513
|
|
|
|
|
|
|
} |
|
514
|
|
|
|
|
|
|
# The list of extracted names. |
|
515
|
0
|
|
|
|
|
0
|
my @names; |
|
516
|
0
|
|
|
|
|
0
|
for (1..$n_names) { |
|
517
|
0
|
|
|
|
|
0
|
my $name = $obj->read_bytes ($field_name_length); |
|
518
|
0
|
|
|
|
|
0
|
$name = unpack ("A*", $name); |
|
519
|
0
|
0
|
|
|
|
0
|
if ($obj->{verbose}) { |
|
520
|
0
|
|
|
|
|
0
|
print "Name: \"$name\"\n"; |
|
521
|
|
|
|
|
|
|
} |
|
522
|
0
|
|
|
|
|
0
|
push @names, $name; |
|
523
|
|
|
|
|
|
|
} |
|
524
|
0
|
|
|
|
|
0
|
return \@names; |
|
525
|
|
|
|
|
|
|
} |
|
526
|
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
=head2 |
|
528
|
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
if (is_numeric ($class)) { |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
A true or false routine which returns true if C<$class> is a numeric |
|
532
|
|
|
|
|
|
|
matrix type and false if not. |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
=cut |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
sub is_numeric |
|
537
|
|
|
|
|
|
|
{ |
|
538
|
4
|
|
|
4
|
0
|
4
|
my ($obj, $class) = @_; |
|
539
|
4
|
|
|
|
|
2
|
my $numeric; |
|
540
|
4
|
0
|
33
|
|
|
9
|
if ($class == mxDOUBLE_CLASS || |
|
|
|
|
33
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
541
|
|
|
|
|
|
|
$class == mxSINGLE_CLASS || |
|
542
|
|
|
|
|
|
|
$class == mxINT8_CLASS || |
|
543
|
|
|
|
|
|
|
$class == mxUINT8_CLASS || |
|
544
|
|
|
|
|
|
|
$class == mxINT16_CLASS || |
|
545
|
|
|
|
|
|
|
$class == mxUINT16_CLASS || |
|
546
|
|
|
|
|
|
|
$class == mxINT32_CLASS || |
|
547
|
|
|
|
|
|
|
$class == mxUINT32_CLASS) { |
|
548
|
4
|
|
|
|
|
3
|
$numeric = 1; |
|
549
|
|
|
|
|
|
|
} |
|
550
|
4
|
50
|
|
|
|
7
|
if ($obj->{verbose}) { |
|
551
|
0
|
0
|
|
|
|
0
|
if ($numeric) { |
|
552
|
0
|
|
|
|
|
0
|
print "Numeric.\n"; |
|
553
|
|
|
|
|
|
|
} |
|
554
|
|
|
|
|
|
|
else { |
|
555
|
0
|
|
|
|
|
0
|
print "Non-numeric.\n"; |
|
556
|
|
|
|
|
|
|
} |
|
557
|
|
|
|
|
|
|
} |
|
558
|
4
|
|
|
|
|
3
|
return $numeric; |
|
559
|
|
|
|
|
|
|
} |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
=head2 get_numeric_array |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
$array = $obj->get_numeric_array ($class, \@dim); |
|
564
|
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
Given an array class for numeric data, C<$class>, and the dimensions |
|
566
|
|
|
|
|
|
|
of the array, C<@dim>, this returns an array reference of numerical |
|
567
|
|
|
|
|
|
|
data read from the file handle and arranged into a multidimensional |
|
568
|
|
|
|
|
|
|
array using C<@dim>. |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
=cut |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
sub get_numeric_array |
|
573
|
|
|
|
|
|
|
{ |
|
574
|
4
|
|
|
4
|
0
|
4
|
my ($obj, $class, $dim) = @_; |
|
575
|
4
|
|
|
|
|
6
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
576
|
4
|
|
|
|
|
4
|
my $numbers; |
|
577
|
4
|
50
|
|
|
|
6
|
if ($type == miDOUBLE) { |
|
578
|
4
|
50
|
|
|
|
5
|
if ($class != mxDOUBLE_CLASS) { |
|
579
|
0
|
|
|
|
|
0
|
$obj->error ("Mismatch of class and data type"); |
|
580
|
|
|
|
|
|
|
} |
|
581
|
4
|
|
|
|
|
7
|
$numbers = $obj->get_doubles ($n_bytes); |
|
582
|
|
|
|
|
|
|
} |
|
583
|
|
|
|
|
|
|
else { |
|
584
|
0
|
0
|
|
|
|
0
|
if (! $data) { |
|
585
|
0
|
|
|
|
|
0
|
$data = $obj->read_bytes ($n_bytes); |
|
586
|
|
|
|
|
|
|
} |
|
587
|
0
|
0
|
|
|
|
0
|
if ($type == miINT8) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
588
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("c*", $data)]; |
|
589
|
|
|
|
|
|
|
} |
|
590
|
|
|
|
|
|
|
elsif ($type == miUINT8) { |
|
591
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("C*", $data)]; |
|
592
|
|
|
|
|
|
|
} |
|
593
|
|
|
|
|
|
|
elsif ($type == miINT16) { |
|
594
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("s*", $data)]; |
|
595
|
|
|
|
|
|
|
} |
|
596
|
|
|
|
|
|
|
elsif ($type == miUINT16) { |
|
597
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("S*", $data)]; |
|
598
|
|
|
|
|
|
|
} |
|
599
|
|
|
|
|
|
|
elsif ($type == miINT32) { |
|
600
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("l*", $data)]; |
|
601
|
|
|
|
|
|
|
} |
|
602
|
|
|
|
|
|
|
elsif ($type == miUINT32) { |
|
603
|
0
|
|
|
|
|
0
|
$numbers = [unpack ("L*", $data)]; |
|
604
|
0
|
|
|
|
|
0
|
print "NUMBERS @$numbers\n"; |
|
605
|
|
|
|
|
|
|
} |
|
606
|
|
|
|
|
|
|
else { |
|
607
|
0
|
|
|
|
|
0
|
die "$names{$type} not supported"; |
|
608
|
|
|
|
|
|
|
} |
|
609
|
|
|
|
|
|
|
} |
|
610
|
4
|
|
|
|
|
7
|
return $obj->make_n_d_array ($dim, $numbers); |
|
611
|
|
|
|
|
|
|
} |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
=head2 make_n_d_array |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
my $array = make_n_d_array (\@dim, \@numbers); |
|
616
|
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
Make an n-dimensional array with dimensions from C<@dim> with the |
|
618
|
|
|
|
|
|
|
values in C<@numbers>. For example, if C<@dim = (2, 3)> and |
|
619
|
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
@numbers = (1, 2, 3, 4, 5, 6), |
|
621
|
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
the return value of this is an array reference formatted as follows: |
|
623
|
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
$array = [[1, 2, 3], [4, 5, 6]]; |
|
625
|
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
using the values in C<@dim>. |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=cut |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
sub make_n_d_array |
|
631
|
|
|
|
|
|
|
{ |
|
632
|
4
|
|
|
4
|
0
|
4
|
my ($obj, $dim, $numbers) = @_; |
|
633
|
4
|
|
|
|
|
5
|
my $n = n_from_dim ($dim); |
|
634
|
4
|
50
|
|
|
|
7
|
if (scalar @$numbers != $n) { |
|
635
|
0
|
|
|
|
|
0
|
$obj->error ("Got $n numbers, expected ". (scalar @$numbers)); |
|
636
|
|
|
|
|
|
|
} |
|
637
|
4
|
|
|
|
|
27
|
my $m = []; |
|
638
|
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
# For the two-D case, we can zip through the matrix: |
|
640
|
|
|
|
|
|
|
|
|
641
|
4
|
50
|
|
|
|
7
|
if (@$dim == 2) { |
|
642
|
4
|
|
|
|
|
5
|
my $d = $dim->[0]; |
|
643
|
4
|
|
|
|
|
6
|
for my $i (0..$n - 1) { |
|
644
|
22
|
|
|
|
|
14
|
my $j = int ($i / $d); |
|
645
|
22
|
|
|
|
|
16
|
my $k = $i % $d; |
|
646
|
22
|
|
|
|
|
32
|
$m->[$k][$j] = $numbers->[$i]; |
|
647
|
|
|
|
|
|
|
} |
|
648
|
|
|
|
|
|
|
} |
|
649
|
|
|
|
|
|
|
else { |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
# _ _ |
|
652
|
|
|
|
|
|
|
# | |__ ___ __ _ _ _ ___ ___ ___ __| | ___ |
|
653
|
|
|
|
|
|
|
# | '_ \ / _ \ / _` | | | / __| / __/ _ \ / _` |/ _ \ |
|
654
|
|
|
|
|
|
|
# | |_) | (_) | (_| | |_| \__ \ | (_| (_) | (_| | __/ |
|
655
|
|
|
|
|
|
|
# |_.__/ \___/ \__, |\__,_|___/ \___\___/ \__,_|\___| |
|
656
|
|
|
|
|
|
|
# |___/ |
|
657
|
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
# This is mega-slow and totally insane |
|
659
|
|
|
|
|
|
|
|
|
660
|
0
|
|
|
|
|
0
|
for my $i (0..$n - 1) { |
|
661
|
0
|
|
|
|
|
0
|
my @i; |
|
662
|
0
|
|
|
|
|
0
|
my $ij = $i; |
|
663
|
0
|
|
|
|
|
0
|
for my $j (0..$#$dim) { |
|
664
|
0
|
|
|
|
|
0
|
my $d = $dim->[$j]; |
|
665
|
0
|
|
|
|
|
0
|
$i[$j] = $ij % $d; |
|
666
|
0
|
|
|
|
|
0
|
$ij /= $d; |
|
667
|
|
|
|
|
|
|
} |
|
668
|
0
|
|
|
|
|
0
|
my $stuff = '$m->[' . join ('][', @i) . ']'; |
|
669
|
0
|
|
|
|
|
0
|
$stuff = "$stuff=$numbers->[$i]"; |
|
670
|
|
|
|
|
|
|
# print "@i $stuff\n"; |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
# Horrors! |
|
673
|
|
|
|
|
|
|
|
|
674
|
0
|
|
|
|
|
0
|
eval $stuff; |
|
675
|
|
|
|
|
|
|
} |
|
676
|
|
|
|
|
|
|
} |
|
677
|
|
|
|
|
|
|
# for (@$m) { |
|
678
|
|
|
|
|
|
|
# print "@$_\n"; |
|
679
|
|
|
|
|
|
|
# } |
|
680
|
4
|
|
|
|
|
6
|
return $m; |
|
681
|
|
|
|
|
|
|
} |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
=head2 n_from_dim |
|
684
|
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
my $n = n_from_dim (\@dim); |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
Given the dimensions of a matrix, return the total number of |
|
688
|
|
|
|
|
|
|
elements in the matrix. |
|
689
|
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
=cut |
|
691
|
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
sub n_from_dim |
|
693
|
|
|
|
|
|
|
{ |
|
694
|
4
|
|
|
4
|
0
|
2
|
my ($dim) = @_; |
|
695
|
4
|
|
|
|
|
3
|
my $n = 1; |
|
696
|
4
|
|
|
|
|
5
|
for (@$dim) { |
|
697
|
8
|
|
|
|
|
6
|
$n *= $_; |
|
698
|
|
|
|
|
|
|
} |
|
699
|
4
|
|
|
|
|
5
|
return $n; |
|
700
|
|
|
|
|
|
|
} |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
=head2 get_doubles |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
my $doubles = $obj->get_doubles ($n_bytes); |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
Get C<$n_bytes / 8> doubles and put them in an array. |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
=cut |
|
709
|
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
sub get_doubles |
|
711
|
|
|
|
|
|
|
{ |
|
712
|
4
|
|
|
4
|
0
|
4
|
my ($obj, $n_bytes) = @_; |
|
713
|
4
|
50
|
|
|
|
6
|
if ($n_bytes % 8 != 0) { |
|
714
|
0
|
|
|
|
|
0
|
$obj->error ("Bad byte count $n_bytes for doubles"); |
|
715
|
|
|
|
|
|
|
} |
|
716
|
4
|
|
|
|
|
4
|
my $n_doubles = $n_bytes / 8; |
|
717
|
4
|
|
|
|
|
4
|
my $bytes = $obj->read_bytes ($n_bytes); |
|
718
|
4
|
|
|
|
|
3
|
my @doubles; |
|
719
|
4
|
|
|
|
|
8
|
for (0..$n_doubles - 1) { |
|
720
|
22
|
|
|
|
|
31
|
my $double = parse_double (substr ($bytes, $_ * 8, 8)); |
|
721
|
22
|
50
|
|
|
|
33
|
if ($obj->{verbose}) { |
|
722
|
0
|
|
|
|
|
0
|
printf "Got a $double.\n"; |
|
723
|
|
|
|
|
|
|
} |
|
724
|
22
|
|
|
|
|
21
|
push @doubles, $double; |
|
725
|
|
|
|
|
|
|
} |
|
726
|
4
|
|
|
|
|
5
|
return \@doubles; |
|
727
|
|
|
|
|
|
|
} |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
=head2 parse_double |
|
730
|
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
my $double = parse_double ($bytes); |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
This converts the MAT-File double (eight bytes, the IEEE 754 "double |
|
734
|
|
|
|
|
|
|
64" format) into a Perl floating point number. |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
=cut |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
# http://www.perlmonks.org/bare/?node_id=703222 |
|
739
|
|
|
|
|
|
|
|
|
740
|
3
|
|
|
3
|
0
|
1744
|
sub double_from_hex { unpack 'd', scalar reverse pack 'H*', $_[0] } |
|
741
|
|
|
|
|
|
|
|
|
742
|
1
|
|
|
1
|
|
8
|
use constant POS_INF => double_from_hex '7FF0000000000000'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
9
|
|
|
743
|
1
|
|
|
1
|
|
6
|
use constant NEG_INF => double_from_hex 'FFF0000000000000'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
3
|
|
|
744
|
1
|
|
|
1
|
|
7
|
use constant NaN => double_from_hex '7FF8000000000000'; |
|
|
1
|
|
|
|
|
14
|
|
|
|
1
|
|
|
|
|
3
|
|
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
sub parse_double |
|
747
|
|
|
|
|
|
|
{ |
|
748
|
22
|
|
|
22
|
0
|
25
|
my ($bytes) = @_; |
|
749
|
22
|
|
|
|
|
24
|
my ($bottom, $top) = unpack ("LL", $bytes); |
|
750
|
|
|
|
|
|
|
# Reference: |
|
751
|
|
|
|
|
|
|
# http://en.wikipedia.org/wiki/Double_precision_floating-point_format |
|
752
|
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
# Eight zero bytes represents 0.0. |
|
754
|
22
|
50
|
|
|
|
37
|
if ($bottom == 0) { |
|
|
|
50
|
|
|
|
|
|
|
755
|
0
|
0
|
|
|
|
0
|
if ($top == 0) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
756
|
0
|
|
|
|
|
0
|
return 0; |
|
757
|
|
|
|
|
|
|
} |
|
758
|
|
|
|
|
|
|
elsif ($top == 0x80000000) { |
|
759
|
0
|
|
|
|
|
0
|
return -0; |
|
760
|
|
|
|
|
|
|
} |
|
761
|
|
|
|
|
|
|
elsif ($top == 0x7ff00000) { |
|
762
|
0
|
|
|
|
|
0
|
return POS_INF; |
|
763
|
|
|
|
|
|
|
} |
|
764
|
|
|
|
|
|
|
elsif ($top == 0xfff00000) { |
|
765
|
0
|
|
|
|
|
0
|
return NEG_INF; |
|
766
|
|
|
|
|
|
|
} |
|
767
|
|
|
|
|
|
|
} |
|
768
|
|
|
|
|
|
|
elsif ($top == 0x7ff00000) { |
|
769
|
0
|
|
|
|
|
0
|
return NaN; |
|
770
|
|
|
|
|
|
|
} |
|
771
|
22
|
|
|
|
|
16
|
my $sign = $top >> 31; |
|
772
|
|
|
|
|
|
|
# print "$sign\n"; |
|
773
|
22
|
|
|
|
|
16
|
my $exponent = (($top >> 20) & 0x7FF) - 1023; |
|
774
|
|
|
|
|
|
|
# print "$exponent\n"; |
|
775
|
22
|
|
|
|
|
14
|
my $e = ($top >> 20) & 0x7FF; |
|
776
|
22
|
|
|
|
|
14
|
my $t = $top & 0xFFFFF; |
|
777
|
|
|
|
|
|
|
# printf ("--> !%011b%020b \n--> %032b\n", $e, $t, $top); |
|
778
|
22
|
|
|
|
|
19
|
my $mantissa = ($bottom + ($t*(2**32))) / 2**52 + 1; |
|
779
|
|
|
|
|
|
|
# print "$mantissa\n"; |
|
780
|
22
|
|
|
|
|
31
|
my $double = (-1)**$sign * 2**$exponent * $mantissa; |
|
781
|
22
|
|
|
|
|
19
|
return $double; |
|
782
|
|
|
|
|
|
|
} |
|
783
|
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
=head2 get_array_name |
|
785
|
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
my $name = $obj->get_array_name (); |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
Get the name of an array from the next point in the file. If the array |
|
789
|
|
|
|
|
|
|
does not have a name, the return value is the undefined value. |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=cut |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
sub get_array_name |
|
794
|
|
|
|
|
|
|
{ |
|
795
|
4
|
|
|
4
|
0
|
4
|
my ($obj) = @_; |
|
796
|
4
|
50
|
|
|
|
6
|
if ($obj->{verbose}) { |
|
797
|
0
|
|
|
|
|
0
|
print "Trying to read the array's name.\n"; |
|
798
|
|
|
|
|
|
|
} |
|
799
|
4
|
|
|
|
|
5
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
800
|
4
|
50
|
|
|
|
6
|
if ($type != miINT8) { |
|
801
|
0
|
|
|
|
|
0
|
$obj->error ("bad type $type for array name"); |
|
802
|
|
|
|
|
|
|
} |
|
803
|
|
|
|
|
|
|
# Arrays within a struct have name fields with zero bytes. |
|
804
|
4
|
50
|
|
|
|
6
|
if ($n_bytes == 0) { |
|
805
|
0
|
|
|
|
|
0
|
return undef; |
|
806
|
|
|
|
|
|
|
} |
|
807
|
4
|
50
|
|
|
|
5
|
if (! $data) { |
|
808
|
4
|
|
|
|
|
5
|
$data = $obj->read_bytes ($n_bytes); |
|
809
|
|
|
|
|
|
|
} |
|
810
|
|
|
|
|
|
|
# Remove trailing bytes from the string. |
|
811
|
4
|
|
|
|
|
7
|
$data = substr $data, 0, $n_bytes; |
|
812
|
4
|
50
|
|
|
|
6
|
if ($obj->{verbose}) { |
|
813
|
0
|
|
|
|
|
0
|
print "The array's name is \"$data\".\n"; |
|
814
|
|
|
|
|
|
|
} |
|
815
|
4
|
|
|
|
|
5
|
return $data; |
|
816
|
|
|
|
|
|
|
} |
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
=head2 get_array_dimensions |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
my @dim = $obj->get_array_dimensions (); |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
Get the dimensions of an array from the file, as an array. Matlab |
|
823
|
|
|
|
|
|
|
arrays always have at least two dimensions, so @dim is at least size |
|
824
|
|
|
|
|
|
|
2. |
|
825
|
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
=cut |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
sub get_array_dimensions |
|
829
|
|
|
|
|
|
|
{ |
|
830
|
4
|
50
|
|
4
|
0
|
7
|
die "Bad call" if (! wantarray); |
|
831
|
4
|
|
|
|
|
3
|
my ($obj) = @_; |
|
832
|
4
|
|
|
|
|
5
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
833
|
4
|
50
|
|
|
|
7
|
if ($type != miINT32) { |
|
834
|
0
|
|
|
|
|
0
|
$obj->error ("bad type $type for array dimensions"); |
|
835
|
|
|
|
|
|
|
} |
|
836
|
4
|
|
|
|
|
6
|
my $dim = $obj->read_bytes ($n_bytes); |
|
837
|
4
|
|
|
|
|
10
|
my @dim = unpack ("L" x ($n_bytes / 4), $dim); |
|
838
|
4
|
50
|
|
|
|
7
|
if ($obj->{verbose}) { |
|
839
|
0
|
|
|
|
|
0
|
print "The array's dimensions are @dim.\n"; |
|
840
|
|
|
|
|
|
|
} |
|
841
|
4
|
|
|
|
|
6
|
return @dim; |
|
842
|
|
|
|
|
|
|
} |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=head2 read_data_header |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
my ($type, $n_bytes, $data) = $obj->read_data_header (); |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
Read an eight-byte data header from the file and parse it. |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=cut |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
sub read_data_header |
|
853
|
|
|
|
|
|
|
{ |
|
854
|
21
|
|
|
21
|
0
|
13
|
my ($obj) = @_; |
|
855
|
21
|
50
|
|
|
|
29
|
if ($obj->{verbose}) { |
|
856
|
0
|
|
|
|
|
0
|
print "Reading data header.\n"; |
|
857
|
|
|
|
|
|
|
} |
|
858
|
21
|
|
|
|
|
20
|
my $h = $obj->read_bytes (8); |
|
859
|
21
|
100
|
|
|
|
28
|
if ($obj->{eof}) { |
|
860
|
1
|
|
|
|
|
1
|
return; |
|
861
|
|
|
|
|
|
|
} |
|
862
|
20
|
|
|
|
|
23
|
my ($type, $n_bytes, $data) = $obj->parse_data_header ($h); |
|
863
|
20
|
|
|
|
|
22
|
return ($type, $n_bytes, $data); |
|
864
|
|
|
|
|
|
|
} |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
=head2 parse_data_header |
|
868
|
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
my ($type, $n_bytes, $data) = $obj->parse_data_header ($h); |
|
870
|
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
Parse the data header in C<$h> without reading from the file |
|
872
|
|
|
|
|
|
|
itself. C<$h> must be eight bytes long. |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
This takes an eight-byte data header and parses out the type and the |
|
875
|
|
|
|
|
|
|
number of bytes from it. Should this be a Small Data Element Format |
|
876
|
|
|
|
|
|
|
piece of data, it will also return the value in C<$data>. If C<$data> |
|
877
|
|
|
|
|
|
|
is undefined, that indicates that it was not a Small Data Element |
|
878
|
|
|
|
|
|
|
Format piece of data, so the data will need to be read separately. |
|
879
|
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=cut |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
sub parse_data_header |
|
883
|
|
|
|
|
|
|
{ |
|
884
|
20
|
|
|
20
|
0
|
16
|
my ($obj, $h) = @_; |
|
885
|
20
|
50
|
|
|
|
25
|
if ($obj->{verbose}) { |
|
886
|
0
|
|
|
|
|
0
|
print "Parsing data header.\n"; |
|
887
|
|
|
|
|
|
|
} |
|
888
|
20
|
50
|
|
|
|
24
|
if (length $h != 8) { |
|
889
|
0
|
|
|
|
|
0
|
$obj->error ("Bad length " . (length $h) . " for data header"); |
|
890
|
|
|
|
|
|
|
} |
|
891
|
20
|
|
|
|
|
12
|
my $type; |
|
892
|
|
|
|
|
|
|
my $n_bytes; |
|
893
|
20
|
|
|
|
|
33
|
($type, $n_bytes) = unpack ("LL", $h); |
|
894
|
20
|
|
|
|
|
12
|
my $data; |
|
895
|
20
|
|
|
|
|
22
|
my $sdef = int ($type / 0x10000); |
|
896
|
20
|
50
|
|
|
|
24
|
if ($sdef) { |
|
897
|
|
|
|
|
|
|
# Small Data Element Format |
|
898
|
0
|
0
|
|
|
|
0
|
if ($obj->{verbose}) { |
|
899
|
0
|
|
|
|
|
0
|
print "Small Data Element Format with $sdef bytes.\n"; |
|
900
|
|
|
|
|
|
|
} |
|
901
|
0
|
|
|
|
|
0
|
$n_bytes = $sdef; |
|
902
|
0
|
|
|
|
|
0
|
$type = $type % 0x10000; |
|
903
|
0
|
|
|
|
|
0
|
(undef, $data) = unpack ("a4a4", $h); |
|
904
|
|
|
|
|
|
|
# Remove padding bytes |
|
905
|
0
|
|
|
|
|
0
|
$data = substr ($data, 0, $n_bytes); |
|
906
|
|
|
|
|
|
|
} |
|
907
|
20
|
50
|
|
|
|
21
|
if ($type > 18) { |
|
908
|
0
|
|
|
|
|
0
|
$obj->warning ("Unknown data type $type"); |
|
909
|
|
|
|
|
|
|
} |
|
910
|
20
|
50
|
|
|
|
23
|
if ($obj->{verbose}) { |
|
911
|
|
|
|
|
|
|
printf "%X: Type and number of bytes are $names{$type} and %u.\n", |
|
912
|
0
|
|
|
|
|
0
|
tell ($obj->{fh}) - 8, $n_bytes; |
|
913
|
|
|
|
|
|
|
} |
|
914
|
20
|
|
|
|
|
21
|
return ($type, $n_bytes, $data); |
|
915
|
|
|
|
|
|
|
} |
|
916
|
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
# Basic byte reader |
|
918
|
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
=head2 read_bytes |
|
920
|
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
my $bytes = $obj->read_bytes ($n_bytes); |
|
922
|
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
Read C<$n_bytes> bytes from the MAT-File. It rounds the number up to |
|
924
|
|
|
|
|
|
|
an eight-byte multiple unless a second, true, argument is given: |
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
my $bytes = $obj->read_bytes ($n_bytes, 1); |
|
927
|
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
=cut |
|
929
|
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
sub read_bytes |
|
931
|
|
|
|
|
|
|
{ |
|
932
|
40
|
|
|
40
|
0
|
29
|
my ($obj, $n, $no_pad) = @_; |
|
933
|
40
|
|
|
|
|
23
|
my $r; |
|
934
|
40
|
|
|
|
|
28
|
my $nbytes = $n; |
|
935
|
40
|
50
|
|
|
|
50
|
if ($obj->{eof}) { |
|
936
|
0
|
|
|
|
|
0
|
croak "attempt to read beyond end of $obj->{file_name}"; |
|
937
|
|
|
|
|
|
|
} |
|
938
|
40
|
50
|
|
|
|
42
|
if ($obj->{verbose}) { |
|
939
|
0
|
0
|
|
|
|
0
|
if ($no_pad) { |
|
940
|
0
|
|
|
|
|
0
|
print "Not padding to eight-byte boundary.\n"; |
|
941
|
|
|
|
|
|
|
} |
|
942
|
|
|
|
|
|
|
} |
|
943
|
40
|
|
|
|
|
18
|
my $padded; |
|
944
|
|
|
|
|
|
|
# Round up to eight-byte boundary. |
|
945
|
40
|
100
|
100
|
|
|
133
|
if (! $no_pad && $n % 8 != 0) { |
|
946
|
4
|
|
|
|
|
4
|
$nbytes += (8 - $n % 8); |
|
947
|
4
|
|
|
|
|
4
|
$padded = 1; |
|
948
|
|
|
|
|
|
|
} |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
# Read from uncompressed data if available. |
|
951
|
|
|
|
|
|
|
|
|
952
|
40
|
50
|
|
|
|
43
|
if ($obj->{input_data}) { |
|
953
|
0
|
|
|
|
|
0
|
print "Reading $nbytes ($n) from uncompressed data.\n"; |
|
954
|
0
|
|
|
|
|
0
|
my $has = length ($obj->{input_data}); |
|
955
|
0
|
0
|
|
|
|
0
|
if ($nbytes > $has) { |
|
956
|
0
|
|
|
|
|
0
|
$obj->error ("Cannot read $nbytes, only have $has left"); |
|
957
|
|
|
|
|
|
|
} |
|
958
|
0
|
|
|
|
|
0
|
$r = substr ($obj->{input_data}, 0, $nbytes); |
|
959
|
|
|
|
|
|
|
# Remove $nbytes bytes from input_data. |
|
960
|
0
|
|
|
|
|
0
|
$obj->{input_data} = substr ($obj->{input_data}, $nbytes); |
|
961
|
|
|
|
|
|
|
} |
|
962
|
|
|
|
|
|
|
else { |
|
963
|
40
|
|
|
|
|
74
|
my $nread = read ($obj->{fh}, $r, $nbytes); |
|
964
|
40
|
50
|
|
|
|
45
|
if (! defined $nread) { |
|
965
|
0
|
|
|
|
|
0
|
$obj->error ("could not read $n bytes: $!"); |
|
966
|
|
|
|
|
|
|
} |
|
967
|
40
|
100
|
|
|
|
78
|
if ($nread == 0) { |
|
|
|
50
|
|
|
|
|
|
|
968
|
1
|
|
|
|
|
3
|
$obj->{eof} = 1; |
|
969
|
|
|
|
|
|
|
} |
|
970
|
|
|
|
|
|
|
elsif ($nread != $nbytes) { |
|
971
|
0
|
|
|
|
|
0
|
$obj->error ("Tried to read $n bytes and got $nread"); |
|
972
|
|
|
|
|
|
|
} |
|
973
|
|
|
|
|
|
|
} |
|
974
|
|
|
|
|
|
|
# If the read data is padded, chop off the padding. |
|
975
|
40
|
100
|
|
|
|
45
|
if ($padded) { |
|
976
|
4
|
50
|
|
|
|
5
|
if ($obj->{verbose}) { |
|
977
|
0
|
|
|
|
|
0
|
print "Chopping padding to length $n.\n"; |
|
978
|
|
|
|
|
|
|
} |
|
979
|
4
|
|
|
|
|
7
|
$r = substr ($r, 0, $n); |
|
980
|
|
|
|
|
|
|
} |
|
981
|
40
|
|
|
|
|
47
|
return $r; |
|
982
|
|
|
|
|
|
|
} |
|
983
|
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
# Error-handlers |
|
985
|
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
=head2 error |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
$obj->error ($message); |
|
989
|
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
This is for errors during reading of the file. A call to "croak" with |
|
991
|
|
|
|
|
|
|
C<$message> and the current file location and name taken from C<$obj>. |
|
992
|
|
|
|
|
|
|
|
|
993
|
|
|
|
|
|
|
=cut |
|
994
|
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
sub error |
|
996
|
|
|
|
|
|
|
{ |
|
997
|
0
|
|
|
0
|
0
|
|
my ($obj, $message) = @_; |
|
998
|
0
|
|
|
|
|
|
$message = $obj->error_message ($message); |
|
999
|
0
|
|
|
|
|
|
croak $message; |
|
1000
|
|
|
|
|
|
|
} |
|
1001
|
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
=head2 warning |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
$obj->warning ($message); |
|
1005
|
|
|
|
|
|
|
|
|
1006
|
|
|
|
|
|
|
This is for warnings during reading of the file. A call to "carp" with |
|
1007
|
|
|
|
|
|
|
C<$message> and the current file location and name taken from C<$obj>. |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
=cut |
|
1010
|
|
|
|
|
|
|
|
|
1011
|
|
|
|
|
|
|
sub warning |
|
1012
|
|
|
|
|
|
|
{ |
|
1013
|
0
|
|
|
0
|
0
|
|
my ($obj, $message) = @_; |
|
1014
|
0
|
|
|
|
|
|
$message = $obj->error_message ($message); |
|
1015
|
0
|
|
|
|
|
|
carp $message; |
|
1016
|
|
|
|
|
|
|
} |
|
1017
|
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
=head2 error_message |
|
1019
|
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
my $message = $obj->error_message ($message); |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
Make an error message with the current byte and file name. |
|
1023
|
|
|
|
|
|
|
|
|
1024
|
|
|
|
|
|
|
=cut |
|
1025
|
|
|
|
|
|
|
|
|
1026
|
|
|
|
|
|
|
sub error_message |
|
1027
|
|
|
|
|
|
|
{ |
|
1028
|
0
|
|
|
0
|
0
|
|
my ($obj, $message) = @_; |
|
1029
|
0
|
|
|
|
|
|
my $byte = tell ($obj->{fh}); |
|
1030
|
0
|
|
|
|
|
|
my $hexbyte = sprintf ("%X", $byte); |
|
1031
|
0
|
|
|
|
|
|
$message = "$message near byte $byte (0x$hexbyte) of $obj->{file_name}"; |
|
1032
|
0
|
|
|
|
|
|
return $message; |
|
1033
|
|
|
|
|
|
|
} |
|
1034
|
|
|
|
|
|
|
|
|
1035
|
|
|
|
|
|
|
1; |
|
1036
|
|
|
|
|
|
|
|