| 1 | =head1 NAME |
| 2 | |
| 3 | PDL::IO::IDL -- I/O of IDL Save Files |
| 4 | |
| 5 | =head1 DESCRIPTION |
| 6 | |
| 7 | PDL::IO::IDL allows you to read and write IDL(tm) data files. |
| 8 | |
| 9 | Currently, only reading is implemented. Scalars, arrays, |
| 10 | and structures are all supported. Heap pointers, compiled code, and |
| 11 | objects are not supported. Of those three, only heap pointers are |
| 12 | likely to be supported in the future. |
| 13 | |
| 14 | This code was not developed by RSI, makers of IDL. |
| 15 | |
| 16 | =head1 NOTES |
| 17 | |
| 18 | These things seem to work: |
| 19 | |
| 20 | =over 3 |
| 21 | |
| 22 | =item BYTE, SHORT, LONG, FLOAT, and DOUBLE numeric types and arrays |
| 23 | |
| 24 | All of these types seem to work fine. The corresponding variable is |
| 25 | stored as a PDL in the hash element with the same name as the original |
| 26 | variable in the file. Arrays are byteswapped as needed and are read in so |
| 27 | that the dim list has the same indexing order within PDL as it did within IDL. |
| 28 | |
| 29 | =item STRINGs and arrays of STRINGs |
| 30 | |
| 31 | String types are stored as Perl list refs, in the hash element with |
| 32 | the same name as the original variable in the file. |
| 33 | |
| 34 | =item Structures |
| 35 | |
| 36 | Structures are stored as hash refs. The elements of the hash may be |
| 37 | accessed as values within the hash. |
| 38 | |
| 39 | =item Common blocks |
| 40 | |
| 41 | Variables that are notated as being in a common block are read as |
| 42 | normal. Common-block names are collected in the special hash value |
| 43 | '+common', which contains a hash each keyword of which is the name of |
| 44 | a common block and each value of which is an array of variable names. |
| 45 | |
| 46 | =back |
| 47 | |
| 48 | These things are known to be not working and may one day be fixed: |
| 49 | |
| 50 | =over 3 |
| 51 | |
| 52 | =item COMPLEX numbers |
| 53 | |
| 54 | These could be implemented as 2-arrays or as PDL::Complex values, but aren't yet. |
| 55 | |
| 56 | =item PTR types |
| 57 | |
| 58 | These could be implemented as perl refs but currently aren't. |
| 59 | |
| 60 | =item writing |
| 61 | |
| 62 | Maybe one day -- but why bother writing a broken file format? NetCDF is better. |
| 63 | |
| 64 | =back |
| 65 | |
| 66 | These things are known to be not working and will probably never be fixed |
| 67 | |
| 68 | =over 3 |
| 69 | |
| 70 | =item Compiled code |
| 71 | |
| 72 | Decompiling IDL code is a violation of the IDL end-user license. To |
| 73 | implement this, someone who does not hold an IDL license would have to |
| 74 | reverse-engineer a set of .SAV files sent to that person by someone |
| 75 | else with an IDL license. |
| 76 | |
| 77 | =item Objects |
| 78 | |
| 79 | IDL objects contain compiled code. |
| 80 | |
| 81 | =back |
| 82 | |
| 83 | =head1 FUNCTIONS |
| 84 | |
| 85 | =cut |
| 86 | |
| 87 | package PDL::IO::IDLscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/; |
| 88 | |
| 89 | use strictscip-perl cpan PDL-IO-IDL 2.099 strict/; |
| 90 | use warningsscip-perl cpan PDL-IO-IDL 2.099 warnings/; |
| 91 | use Exporterscip-perl cpan PDL-IO-IDL 2.099 Exporter/ (); |
| 92 | package PDL::IO::IDLscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/; |
| 93 | our @ISAscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@ISA`. = qw( Exporter ); |
| 94 | our @EXPORT_OKscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@EXPORT_OK`. = qw( ridl ); |
| 95 | our @EXPORTscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@EXPORT`. = @EXPORT_OKscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@EXPORT_OK`.; |
| 96 | our @EXPORT_TAGSscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@EXPORT_TAGS`. = ( Func=>[@EXPORT_OKscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@EXPORT_OK`.] ); |
| 97 | |
| 98 | our $VERSIONscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$VERSION. = "2.099"; |
| 99 | $VERSIONscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$VERSION. = eval $VERSIONscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$VERSION.; |
| 100 | |
| 101 | use PDLscip-perl cpan PDL-IO-IDL 2.099 PDL/; |
| 102 | use PDL::Exporterscip-perl cpan PDL-IO-IDL 2.099 PDL/Exporter/; |
| 103 | use Carpscip-perl cpan PDL-IO-IDL 2.099 Carp/; |
| 104 | |
| 105 | use PDL::Typesscip-perl cpan PDL-IO-IDL 2.099 PDL/Types/; |
| 106 | |
| 107 | =head2 ridl |
| 108 | |
| 109 | =for usage |
| 110 | |
| 111 | $x = ridl("foo.sav"); |
| 112 | |
| 113 | =for ref |
| 114 | |
| 115 | Read an IDL save file from a file. |
| 116 | |
| 117 | Upon successful completion, $x is a hash ref containing all of the |
| 118 | variables that are present in the save file, indexed by original |
| 119 | variable name. |
| 120 | |
| 121 | IDL identifiers are case insensitive; they're all converted to |
| 122 | upper-case in the hash that gets returned. This may be adjustable at |
| 123 | a future date. Furthermore, because IDL identifiers can't contain |
| 124 | special characters, some fields that start with '+' are used to store |
| 125 | metadata about the file itself. |
| 126 | |
| 127 | Numeric arrays are stored as PDLs, structures are stored as hashes, |
| 128 | and string and structure arrays are stored as perl lists. Named |
| 129 | structure types don't exist in perl in the same way that they do in |
| 130 | IDL, so named structures are described in the 'structs' field of the |
| 131 | global metadata. Anonymous structures are treated as simple hashes. |
| 132 | Named structures are also simple hashes, but they also contain a field |
| 133 | '+name' that refers to the name of the structure type. |
| 134 | |
| 135 | =cut |
| 136 | |
| 137 | |
| 138 | sub ridlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/ridl(). { |
| 139 | my( $name ) = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 140 | |
| 141 | STDERR->autoflushscip-perl cpan PDL-IO-IDL 2.099 STDERR/autoflush().(1); |
| 142 | |
| 143 | openscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/open().(IDLSAV,"<$name") || barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("ridl: Can't open `$name' for reading\n"); |
| 144 | |
| 145 | my $hash = read_preamblescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/read_preamble().(); |
| 146 | |
| 147 | read_recordsscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/read_records().($hash); |
| 148 | |
| 149 | my @snames = sortscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sort(). keysscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/keys(). %{$PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table.}; |
| 150 | @snames = grepscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/grep().(!m/^\+/,@snames); |
| 151 | if(@snames) { |
| 152 | $hash->{'+structs'}={}; |
| 153 | local $_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$_.; |
| 154 | for(@snames) { |
| 155 | $hash->{'+structs'}->{$_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$_.} = |
| 156 | $PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table.->{$_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$_.}->{'names'}; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return $hash; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | ############################################################ |
| 165 | ## |
| 166 | ## Data structure definitions... |
| 167 | ## |
| 168 | ## This is a list, each element of which contains a description and |
| 169 | ## subroutine to read that particular record type. |
| 170 | ## |
| 171 | |
| 172 | our $typesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$types. = [ ['START_MARKER',undef] # 0 (start of SAVE file) |
| 173 | ,['COMMON_BLOCK',\&r_comscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_com().] # 1 (COMMON block definition) |
| 174 | ,['VARIABLE',\&r_varscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_var().] # 2 (Variable data) |
| 175 | ,['SYSTEM_VARIABLE',undef] # 3 (System variable data) |
| 176 | ,undef # 4 (??) |
| 177 | ,undef # 5 (??) |
| 178 | ,['END_MARKER',\&r_endscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_end().] # 6 (End of SAVE file) |
| 179 | ,undef # 7 (??) |
| 180 | ,undef # 8 (??) |
| 181 | ,undef # 9 (??) |
| 182 | ,['TIMESTAMP',\&r_tsscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_ts().] # 10 (Timestamp of the save file) |
| 183 | ,undef # 11 (??) |
| 184 | ,['COMPILED',undef] # 12 (Compiled procedure or func) |
| 185 | ,['IDENTIFICATION',undef] # 13 (Author identification) |
| 186 | ,['VERSION',\&r_vscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_v().] # 14 (IDL Version information) |
| 187 | ,['HEAP_HEADER',undef] # 15 (Heap index information) |
| 188 | ,['HEAP_DATA',undef] # 16 (Heap data) |
| 189 | ,['PROMOTE64',\&r_p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_p64().] # 17 (Starts 64-bit file offsets) |
| 190 | ]; |
| 191 | |
| 192 | |
| 193 | ############################################################ |
| 194 | ## |
| 195 | ## Vtypes -- Representations of IDL scalar variable types. |
| 196 | ## The first element is the name, the second element is either a |
| 197 | ## perl string (that should be fed to unpack) or a code ref to a |
| 198 | ## sub that decodes the type. |
| 199 | ## |
| 200 | |
| 201 | our $vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes. = [ |
| 202 | undef # 0 |
| 203 | ,["Byte", \&r_byte_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_byte_pdl()., [] ] # 1 |
| 204 | ,["Short", \&r_n_castscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_cast()., [long,short] ] # 2 |
| 205 | ,["Long", \&r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl()., [long] ] # 3 |
| 206 | ,["Float", \&r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl()., [float] ] # 4 |
| 207 | ,["Double", \&r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl()., [double] ] # 5 |
| 208 | ,["Complex", undef ] # 6 |
| 209 | ,["String", \&r_strvarscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_strvar()., [] ] # 7 |
| 210 | ,["Structure", sub {}, [] ] # 8 |
| 211 | ,["ComplexDbl",undef ] # 9 |
| 212 | ,["HeapPtr", undef ] # 10 |
| 213 | ,["Object", undef ] # 11 |
| 214 | ,["UShort", \&r_n_castscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_cast()., [long,ushort] ] # 12 |
| 215 | ,["ULong", \&r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl()., [long] ] # 13 |
| 216 | ,["LongLong", undef ] # 14 |
| 217 | ,["ULongLong", undef ] # 15 |
| 218 | ]; |
| 219 | |
| 220 | |
| 221 | ### |
| 222 | # Cheesy way to check if 64-bit is OK |
| 223 | our $quad_okscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$quad_ok. = eval { my @a = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "q","00000001"; $a[0]; }; |
| 224 | |
| 225 | ### Initialized in read_preamble. |
| 226 | our $little_endianscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$little_endian.; |
| 227 | our $swabscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$swab.; |
| 228 | our $p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$p64.; |
| 229 | |
| 230 | |
| 231 | ############################## |
| 232 | # |
| 233 | # read_preamble |
| 234 | # |
| 235 | # Reads the preamble of a file and returns the preamble as a hash |
| 236 | # ref. In case of failure, it barfs. Also initializes the structure table. |
| 237 | # |
| 238 | |
| 239 | sub read_preamblescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/read_preamble(). { |
| 240 | my $buf; |
| 241 | my $out; |
| 242 | |
| 243 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4) || barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf(). ("PDL::IO::IDL: Couldn't read preamble\n"); |
| 244 | my @sig = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack().("a2S",$buf); |
| 245 | |
| 246 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("PDL::IO::IDL: This isn't an IDL save file (wrong magic)\n") |
| 247 | if($sig[0] ne 'SR'); |
| 248 | |
| 249 | if($sig[1] == 1024 || $sig[1] == 4) { |
| 250 | $little_endianscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$little_endian. = ($sig[1] == 1024); |
| 251 | } else { |
| 252 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf(). "Unrecognized IDL save file type\n"; |
| 253 | } |
| 254 | |
| 255 | $swabscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$swab. = $little_endianscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$little_endian.; |
| 256 | |
| 257 | $p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$p64. = 0; |
| 258 | |
| 259 | $PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table. = {}; |
| 260 | |
| 261 | return {"+meta"=>{}}; |
| 262 | } |
| 263 | |
| 264 | ############################## |
| 265 | # |
| 266 | # read_records |
| 267 | # |
| 268 | # Reads all the records of the file. Splits out into several other |
| 269 | # types of record reader... |
| 270 | # |
| 271 | # |
| 272 | |
| 273 | sub read_recordsscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/read_records(). { |
| 274 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 275 | my ($buf, $tbuf); |
| 276 | |
| 277 | my $retval; |
| 278 | |
| 279 | my %nexts; |
| 280 | my $tag_count = 0; |
| 281 | do { |
| 282 | |
| 283 | ### Read header of the record |
| 284 | |
| 285 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV, $tbuf, 4) || barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("PDL::IO::IDL: unexpected EOF\n"); |
| 286 | my $type = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$tbuf; |
| 287 | |
| 288 | ### Record the next seek location |
| 289 | ### (and discard 8 more bytes) |
| 290 | |
| 291 | my $next; |
| 292 | if($p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$p64.) { |
| 293 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Reading 64-bit location..." if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 294 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,8 + 8); |
| 295 | my @next = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "NN",$buf; |
| 296 | $next = $next[1] + 2**32 * $next[0]; |
| 297 | } else { |
| 298 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Reading 32-bit location..." if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 299 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4 + 8); |
| 300 | $next = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 301 | } |
| 302 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "$next\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 303 | |
| 304 | ### |
| 305 | ### Infinite-loop detector |
| 306 | ### |
| 307 | |
| 308 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("Repeat index finder was activated! This is a bug or a problem with your file.\n") |
| 309 | if($nexts{$next}) ; |
| 310 | $nexts{$next} = 1; |
| 311 | |
| 312 | ### |
| 313 | ### Call the appropriate handling routine |
| 314 | ### |
| 315 | |
| 316 | $retval = 1; |
| 317 | |
| 318 | if(definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined(). $typesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$types.->[$type]) { |
| 319 | if(definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined(). ($typesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$types.->[$type]->[1])) { |
| 320 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Found record of type $types->[$type]->[0]...\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 321 | $retval = &{scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$typesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$types.->[scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$type]->[1]}(scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$hash)scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().; |
| 322 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "OK.\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 323 | } else { |
| 324 | print STDERR "Ignoring record of type ".$typesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$types.->[$type]->[0]." - not implemented.\n"; |
| 325 | } |
| 326 | } else { |
| 327 | print STDERR "\nIgnoring record of unknown type $type - not implemented.\n"; |
| 328 | } |
| 329 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Seeking $next ($tag_count tags read so far...)\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 330 | $tag_count++; |
| 331 | sysseekscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysseek().(IDLSAV, $next, 0); |
| 332 | $FOO::hashscip-perl cpan PDL-IO-IDL 2.099 FOO/$hash. = $hash; |
| 333 | } while($retval); |
| 334 | |
| 335 | } |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | ############################## |
| 341 | # r_com |
| 342 | # |
| 343 | # Jumptable entry for the COMMONBLOCK keyword -- this loads |
| 344 | # the variable names that belong in the COMMON block into a |
| 345 | # metavariable. |
| 346 | |
| 347 | sub r_comscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_com(). { |
| 348 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 349 | my $buf; |
| 350 | |
| 351 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); |
| 352 | my $nvars = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 353 | |
| 354 | my $name = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 355 | $hash->{"+common"}->{$name} = []; |
| 356 | |
| 357 | for my $i(1..$nvars) { |
| 358 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().(@{$hash->{"+common"}->{$name}},r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().()); |
| 359 | } |
| 360 | |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | ############################## |
| 368 | # r_end |
| 369 | # |
| 370 | # Jumptable entry for the END TABLE keyword -- just return 0. |
| 371 | |
| 372 | sub r_endscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_end(). { 0; } |
| 373 | |
| 374 | |
| 375 | ############################## |
| 376 | # r_ts |
| 377 | # |
| 378 | # TIMESTAMP record handler |
| 379 | # |
| 380 | sub r_tsscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_ts(). { |
| 381 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 382 | my $buf; |
| 383 | |
| 384 | ### Read and discard a LONARR(258) -- why? I don't know. |
| 385 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,1024); |
| 386 | $hash->{"+meta"}->{t_date} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 387 | $hash->{"+meta"}->{t_user} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 388 | $hash->{"+meta"}->{t_host} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 389 | |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | |
| 395 | ############################## |
| 396 | # r_version |
| 397 | # |
| 398 | # VERSION record handler |
| 399 | # |
| 400 | sub r_vscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_v(). { |
| 401 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 402 | my $buf; |
| 403 | my $version; |
| 404 | |
| 405 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); |
| 406 | $version = $hash->{"+meta"}->{v_fmt} = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 407 | |
| 408 | # barf("Unknown IDL save file version ".$version) |
| 409 | print STDERR "Warning: IDL file is v$version (neither 5 nor 6); winging it. Check results!\n" |
| 410 | if($version != 5 && $version != 6); |
| 411 | |
| 412 | $hash->{"+meta"}->{v_arch} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 413 | $hash->{"+meta"}->{v_os} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 414 | $hash->{"+meta"}->{v_release} = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | ############################## |
| 419 | # r_p64 |
| 420 | sub r_p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_p64(). { |
| 421 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 422 | $p64scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$p64. = 1; |
| 423 | } |
| 424 | |
| 425 | ############################## |
| 426 | # r_var |
| 427 | # |
| 428 | # VARIABLE reader - parse a single variable out of a VARIABLE record. |
| 429 | # |
| 430 | |
| 431 | sub r_varscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_var(). { |
| 432 | my $hash = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 433 | |
| 434 | ### Read in the variable name |
| 435 | my $name = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 436 | |
| 437 | |
| 438 | ### Read in and parse the type |
| 439 | |
| 440 | my $buf; |
| 441 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,8); |
| 442 | my ($type,$flags) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "NN",$buf; |
| 443 | |
| 444 | unless(definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined(). $vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]) { |
| 445 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("PDL::IO::IDL: Unknown variable type $type"); |
| 446 | } |
| 447 | |
| 448 | unless(definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined(). $vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[1]) { |
| 449 | print STDERR "Ignoring variable $name: unsupported type ".$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[0]."\n"; |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Variable $name found (flags is $flags)...\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 454 | |
| 455 | if((($flags & 4) == 0) and (($flags & 32) == 0)) { |
| 456 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "it's a scalar\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 457 | |
| 458 | |
| 459 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); |
| 460 | my($seven) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 461 | if($seven != 7) { |
| 462 | print STDERR "Warning: expected data-start key (7) but got $seven, for variable $name\n"; |
| 463 | } |
| 464 | |
| 465 | ## Scalar case |
| 466 | $hash->{$name} = |
| 467 | &{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[1]} |
| 468 | ($flags, [], @{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[2]}) |
| 469 | } else { |
| 470 | ## Array case |
| 471 | |
| 472 | my($arrdesc) = r_arraydescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_arraydesc().(); |
| 473 | |
| 474 | if(($flags & 32) == 0) { |
| 475 | |
| 476 | ## Simple array case |
| 477 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); |
| 478 | my($indicator) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 479 | |
| 480 | print STDERR "Warning: Reading data from an array but got code $indicator (expected 7)\n" |
| 481 | if($indicator != 7); |
| 482 | |
| 483 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "simple array...type=$type\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 484 | |
| 485 | my @args= ($flags,[ @{$arrdesc->{dims}}[0..$arrdesc->{ndims}-1]], |
| 486 | @{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[2]}); |
| 487 | my $pdl = &{scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$type]->[1]}(scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().@args)scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().; |
| 488 | $hash->{$name} = $pdl; |
| 489 | |
| 490 | } else { |
| 491 | |
| 492 | ## Structure case |
| 493 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "structure...\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 494 | my($sname) = r_structdescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_structdesc().(); |
| 495 | |
| 496 | my @structs; |
| 497 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Reading $arrdesc->{nelem} structures....\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 498 | my $i; |
| 499 | |
| 500 | {my $buf; sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4);} |
| 501 | |
| 502 | for ($i=0;$i<$arrdesc->{nelem};$i++) { |
| 503 | if($PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test. && !($i%100)){ |
| 504 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "$i of $arrdesc->{nelem}...\n"; |
| 505 | } |
| 506 | |
| 507 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().(@structs,r_structscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_struct().($sname)); |
| 508 | } |
| 509 | |
| 510 | # Make a multi-dimensional list that contains the structs |
| 511 | $hash->{$name} = multi_dimifyscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/multi_dimify().($arrdesc,\@structs,0); |
| 512 | |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | |
| 517 | return 1; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | ############################## |
| 522 | # multi_dimify |
| 523 | # |
| 524 | # Take a linear list of items and an array descriptor, and |
| 525 | # hand back a multi-dimensional perl list with the correct dimension |
| 526 | # according to the descriptor. (This isn't necessary for PDL types, |
| 527 | # only for structures and strings). |
| 528 | # |
| 529 | |
| 530 | sub multi_dimifyscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/multi_dimify(). { |
| 531 | |
| 532 | my($arrdesc,$structs,$n) = @_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@_`.; |
| 533 | |
| 534 | return shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift(). @{$structs} |
| 535 | if($arrdesc->{ndims} <= $n or |
| 536 | $arrdesc->{ndims} == 0 or |
| 537 | $arrdesc->{ndims}-$n == 1 && $arrdesc->{dims}->[$n]==1); |
| 538 | |
| 539 | |
| 540 | if($arrdesc->{ndims} - $n == 1){ |
| 541 | my @ret = splicescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/splice(). @{$structs},0,$arrdesc->{dims}->[$n]; |
| 542 | return \@ret; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | my $out = []; |
| 547 | my $i; |
| 548 | for ($i=0;$i<$arrdesc->{dims}->[$n];$i++) { |
| 549 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().(@{$out},multi_dimifyscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/multi_dimify().($arrdesc,$structs,$n+1)); |
| 550 | } |
| 551 | |
| 552 | return $out; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | |
| 557 | ###################################################################### |
| 558 | ###################################################################### |
| 559 | |
| 560 | |
| 561 | # |
| 562 | # r_arraydesc - read an array descriptor from the file |
| 563 | # |
| 564 | |
| 565 | our $r_arraydesc_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_arraydesc_table. = ['a','b','nbytes','nelem','ndims','c','d','nmax']; |
| 566 | |
| 567 | sub r_arraydescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_arraydesc(). { |
| 568 | my $out = {}; |
| 569 | my $buf; |
| 570 | |
| 571 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4*8); |
| 572 | |
| 573 | my(@vals) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack().("N"x8,$buf); |
| 574 | print STDERR "r_arraydesc_table: vals[0]=".$vals[0]." (should be 8)\n" |
| 575 | if($vals[0] != 8); |
| 576 | for my $i(0..7) { |
| 577 | $out->{$r_arraydesc_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_arraydesc_table.->[$i]} = $vals[$i]; |
| 578 | } |
| 579 | my $nmax = $vals[7]; |
| 580 | my $nelem = $vals[3]; |
| 581 | |
| 582 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,$nmax*4); |
| 583 | $out->{dims} = [unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack().("N"x$nmax,$buf)]; |
| 584 | my $dims = pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/pdl().(@{$out->{dims}}); |
| 585 | |
| 586 | $out->{pdldims} = $dims; |
| 587 | |
| 588 | print STDERR "PDL::IO::IDL: Inconsistent array dimensions in variable (nelem=$nelem, dims=".joinscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/join().("x",@{$out->{dims}}).")" |
| 589 | if($nelem != $dims->prodscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/prod().); |
| 590 | |
| 591 | $out; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | ############################## |
| 596 | # |
| 597 | # r_structdesc reads a structure description and stores it in the struct_table. |
| 598 | # You get back the name of the structure. |
| 599 | # |
| 600 | |
| 601 | sub r_structdescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_structdesc(). { |
| 602 | my $buf; |
| 603 | |
| 604 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Reading a structure description...\n" if($PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 605 | |
| 606 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); # Discard initial long (value=9) from descriptor |
| 607 | my($name) = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); # Have to store structures in the structure table. |
| 608 | $name =~ s/\s//g; |
| 609 | |
| 610 | $name = "+anon".scalarscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/scalar().(keysscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/keys(). %{$PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table.}) |
| 611 | if($name eq ''); |
| 612 | |
| 613 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4*3); |
| 614 | my($predef,$ntags,$nbytes) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack().("N"x3,$buf); |
| 615 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "predef=$predef,ntags=$ntags,nbytes=$nbytes\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 616 | if(!($predef & 1)) { |
| 617 | my $i; |
| 618 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "not predefined. ntags=$ntags..\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 619 | |
| 620 | my $st = $PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table.->{$name} = { |
| 621 | "ntags" => $ntags |
| 622 | ,"nbytes"=> $nbytes |
| 623 | ,"names" => [] |
| 624 | ,"arrays" => [] |
| 625 | ,"structs" => [] |
| 626 | }; |
| 627 | |
| 628 | ### Read tag descriptors. |
| 629 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,3*4*$ntags); |
| 630 | $st->{descrip} = [(unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N"x(3*$ntags), $buf)]; |
| 631 | |
| 632 | |
| 633 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "ntags is $ntags\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 634 | ### Read tag names. |
| 635 | for $i(0..$ntags-1) { |
| 636 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().(@{$st->{names}},r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().()); |
| 637 | } |
| 638 | |
| 639 | ### Search for nested arrays & structures |
| 640 | my ($nstructs,$narrays) = (0,0); |
| 641 | |
| 642 | for $i(0..$ntags-1) { |
| 643 | my $x = $st->{descrip}->[$i*3+2]; |
| 644 | |
| 645 | $nstructs++ if($x & 32); |
| 646 | $narrays++ if($x & 38); |
| 647 | } |
| 648 | |
| 649 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "narrays=$narrays\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 650 | for $i(0..($narrays-1)) { |
| 651 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().( @{$st->{arrays}}, r_arraydescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_arraydesc().() ); |
| 652 | } |
| 653 | |
| 654 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "nstructs=$nstructs\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug. || $PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 655 | for $i(0..($nstructs-1)) { |
| 656 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().( @{$st->{structs}}, r_structdescscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_structdesc().() ); |
| 657 | } |
| 658 | |
| 659 | } |
| 660 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "finished with structure desc...\n" if($PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 661 | return $name; |
| 662 | } |
| 663 | |
| 664 | ############################## |
| 665 | # |
| 666 | # r_struct |
| 667 | # |
| 668 | # Given the name of a structure type, read in exactly one of them. |
| 669 | # If I were smarter, this would be the same code as the variable |
| 670 | # reader, but I'm not so it's only similar. |
| 671 | # |
| 672 | our $r_struct_recursionscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_struct_recursion. = 0; |
| 673 | |
| 674 | sub r_structscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_struct(). { |
| 675 | my($sname) = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 676 | |
| 677 | |
| 678 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). +("_ "x$r_struct_recursionscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_struct_recursion.) . "Reading a structure...\n" if($PDL::IO::IDL::testscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$test.); |
| 679 | my $zz=$r_struct_recursionscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_struct_recursion.; |
| 680 | local($r_struct_recursionscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$r_struct_recursion.) = $zz++; |
| 681 | |
| 682 | # Get the structure descriptor from the table. |
| 683 | my($sd) = $PDL::IO::IDL::struct_tablescip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$struct_table.->{$sname}; |
| 684 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf(). "Unknown structure type $sname" unless definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined().($sd); |
| 685 | |
| 686 | # Initialize the structure itself and the array and structure indices. |
| 687 | my($struct) = {}; |
| 688 | $struct->{'+name'} = $sname unless($sname =~ m/^\+/); |
| 689 | |
| 690 | my($array_no, $struct_no); |
| 691 | |
| 692 | # Loop over tags and snarf each one |
| 693 | my($i); |
| 694 | for($i=0;$i<$sd->{ntags};$i++) { |
| 695 | my($name) = $sd->{names}->[$i]; |
| 696 | |
| 697 | my($type) = $sd->{descrip}->[$i*3+1]; |
| 698 | my($flags) = $sd->{descrip}->[$i*3+2]; |
| 699 | |
| 700 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "reading tag #$i ($sd->{names}->[$i])\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 701 | |
| 702 | barfscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/barf().("PDL::IO::IDL: Unknown variable type $type in structure") |
| 703 | unless definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined().($vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]); |
| 704 | |
| 705 | unless(definedscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/defined().($vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[1])) { |
| 706 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "Skipping tag $name in structure - unsupported type ".$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[0]."\n"; |
| 707 | $array_no++ if($flags & 38); |
| 708 | $struct_no++ if($flags & 32); |
| 709 | |
| 710 | } else { |
| 711 | |
| 712 | if( (($flags & 4)==0) and (($flags & 32)==0) ) { |
| 713 | ## Scalar tag case |
| 714 | $struct->{$name} = &{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[1]} |
| 715 | ($flags, [], @{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[2]}); |
| 716 | } else { |
| 717 | |
| 718 | ### Array and/or structure case ### |
| 719 | |
| 720 | my($arrdesc) = $sd->{arrays}->[$array_no++]; |
| 721 | # sysread(IDLSAV,my $buf,4); # skip indicator |
| 722 | |
| 723 | if(($flags & 32) == 0) { |
| 724 | |
| 725 | ### Tag is a simple array ### |
| 726 | |
| 727 | my @args = ($flags,[ @{$arrdesc->{dims}}[0..$arrdesc->{ndims}-1]], |
| 728 | @{$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[$type]->[2]}); |
| 729 | my $pdl = &{scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$vtypesscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$vtypes.->[scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().$type]->[1]}(scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().@args)scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`&{}`().; |
| 730 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). " pdl is $pdl\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 731 | $struct->{$name} = $pdl; |
| 732 | |
| 733 | } else { |
| 734 | |
| 735 | ### Tag is a structure ### |
| 736 | |
| 737 | my $tsname = $sd->{structs}->[$struct_no++]; |
| 738 | my @structs = (); |
| 739 | for $i(1..$arrdesc->{nelem}) { |
| 740 | pushscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/push().(@structs,r_structscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_struct().($tsname)); |
| 741 | } |
| 742 | |
| 743 | $struct->{$name} = multi_dimifyscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/multi_dimify().($arrdesc,\@structs,0); |
| 744 | |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | } # end of ntags loop |
| 749 | |
| 750 | return $struct; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | ############################## |
| 757 | # |
| 758 | # r_string |
| 759 | # |
| 760 | # Reads a string value, leaving the file pointer correctly aligned |
| 761 | # on a 32-bit boundary (if it started that way). Returns the string as |
| 762 | # a perl scalar. |
| 763 | # |
| 764 | sub r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().{ |
| 765 | my ($buf,$foo); |
| 766 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV, $buf, 4); # Read the length... |
| 767 | |
| 768 | my ($len) = unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "N",$buf; |
| 769 | # Pad the length out to the next 32-bit boundary |
| 770 | |
| 771 | my $plen = $len - ($len % -4) ; |
| 772 | |
| 773 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,$plen); |
| 774 | return unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "A$len",$buf; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | ############################## |
| 779 | # |
| 780 | # r_strvar |
| 781 | # |
| 782 | # Reads a string variable (different than r_string because |
| 783 | # of the extra length duplication in the IDL file...) |
| 784 | # |
| 785 | sub r_strvarscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_strvar(). { |
| 786 | my $buf; |
| 787 | my $flags = shiftscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/shift().; |
| 788 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,$buf,4); |
| 789 | return r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 790 | } |
| 791 | |
| 792 | ############################## |
| 793 | # |
| 794 | # r_byte_pdl |
| 795 | # |
| 796 | # Reads a byte PDL (stored as a strvar) |
| 797 | # |
| 798 | sub r_byte_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_byte_pdl(). { |
| 799 | my($flags,$dims) = @_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@_`.; |
| 800 | |
| 801 | sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV,my $buf,4) |
| 802 | if($#$dims > 1); |
| 803 | |
| 804 | my $x = r_stringscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_string().(); |
| 805 | |
| 806 | my $pdl = PDL->newscip-perl cpan PDL-IO-IDL 2.099 PDL/new().; |
| 807 | $pdl->set_datatypescip-perl cpan PDL-IO-IDL 2.099 PDL/set_datatype().(byte->enumscip-perl cpan PDL-IO-IDL 2.099 byte/enum().); |
| 808 | $pdl->setdimsscip-perl cpan PDL-IO-IDL 2.099 PDL/setdims().($dims); |
| 809 | ${ $pdl->get_datarefscip-perl cpan PDL-IO-IDL 2.099 PDL/get_dataref().() } = $x; |
| 810 | $pdl->upd_datascip-perl cpan PDL-IO-IDL 2.099 PDL/upd_data().; |
| 811 | |
| 812 | $pdl; |
| 813 | } |
| 814 | |
| 815 | ############################## |
| 816 | # |
| 817 | # r_n_pdl |
| 818 | # |
| 819 | # Reads <n> normal integer-type numerical values as a pdl. |
| 820 | # You feed in the dimlist and type, you get back the |
| 821 | # final pdl. The read is padded to the nearest word boundary. |
| 822 | # |
| 823 | |
| 824 | sub r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl(). { |
| 825 | my($flags,$dims,$type) = @_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@_`.; |
| 826 | $type = PDL::Type->newscip-perl cpan PDL-IO-IDL 2.099 PDL/Type/new().($type); |
| 827 | my $nelem = pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/pdl().($dims)->prodscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/prod().; |
| 828 | my $hunksize = PDL::Core::howbigscip-perl cpan PDL-IO-IDL 2.099 PDL/Core/howbig().($type->enumscip-perl cpan PDL-IO-IDL 2.099 PDL/Type/enum().) * $nelem; |
| 829 | my $pdl = PDL->new_from_specificationscip-perl cpan PDL-IO-IDL 2.099 PDL/new_from_specification().($type,@$dims); |
| 830 | my $dref = $pdl->get_datarefscip-perl cpan PDL-IO-IDL 2.099 PDL/get_dataref().(); |
| 831 | my $len = sysreadscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/sysread().(IDLSAV, $$dref, $hunksize - ($hunksize % -4) ); |
| 832 | $pdl->upd_datascip-perl cpan PDL-IO-IDL 2.099 PDL/upd_data().; |
| 833 | printscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/print(). "bytes were ",joinscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/join().(",",unpackscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/unpack(). "C"x($hunksize-($hunksize%-4)),$$dref),"\n" if($PDL::debugscip-perl cpan PDL-IO-IDL 2.099 PDL/$debug.); |
| 834 | $type->scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`->()`().bswapscip-perl cpan PDL-IO-IDL 2.099 PDL/Type/bswap().->(scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`->()`().$pdl)scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`->()`(). if $swabscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/$swab.; |
| 835 | $pdl; |
| 836 | } |
| 837 | |
| 838 | sub r_n_castscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_cast(). { |
| 839 | my($flags,$dims,$type1,$type2) = @_scip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/`@_`.; |
| 840 | |
| 841 | (r_n_pdlscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/r_n_pdl().($flags,$dims,$type1))->convertscip-perl cpan PDL-IO-IDL 2.099 PDL/IO/IDL/convert().($type2); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | =head1 AUTHOR, LICENSE, NO WARRANTY |
| 846 | |
| 847 | THIS CODE IS PROVIDED WITH NO WARRANTY and may be distributed and/or |
| 848 | modified under the same terms as PDL itself. |
| 849 | |
| 850 | This code is based on the analysis of the IDL save file format |
| 851 | published by Craig Markwardt in 2002. |
| 852 | |
| 853 | IDL is a trademark of Research Systems Incorporated (RSI). The PDL |
| 854 | development team, and authors of this code, are not affiliated with RSI. |
| 855 | |
| 856 | =cut |
| 857 | |
| 858 | 1; |