Class: FortranSequentialReader

Inherits:
FortranSequential show all
Defined in:
ext/fortio/lib/fortio/fortran_sequential.rb

Constant Summary

Constants inherited from FortranSequential

FortranSequential::ENDIAN, FortranSequential::FMT_NET, FortranSequential::FMT_VAX

Instance Method Summary collapse

Methods inherited from FortranSequential

#eof?, #get_pack_fmt, open

Constructor Details

#initialize(io, opt = {:endian=>nil}) ⇒ FortranSequentialReader

Returns a new instance of FortranSequentialReader.



250
251
252
253
254
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 250

def initialize (io, opt={:endian=>nil})
  @io = io
  @endian = opt[:endian] || ENDIAN
  @fmt = get_pack_fmt(@endian)
end

Instance Method Details

#closeObject



302
303
304
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 302

def close
  @io.close
end

#read(*dummy) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 286

def read (*dummy)
  if @io.eof?
    return nil
  else
    length = @io.read(4).unpack(@fmt["l"]).first
    data = @io.read(length)
    if data.length != length
      raise "record too short (record length should be #{length})"
    end
    if length != @io.read(4).unpack(@fmt["l"]).first
      raise "mismatched record length (should be #{length})"
    end
    return data
  end
end

#record(n = nil) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 256

def record (n=nil)
  if n 
    @io.rewind
    skip(n)
  end
  data = read
  if data
    rec = FortranSequential::Record.new(data, @endian, @fmt)
    if block_given?
      return yield(rec)
    else
      return rec
    end
  else
    return nil
  end
end

#skip(n = 1) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
# File 'ext/fortio/lib/fortio/fortran_sequential.rb', line 274

def skip (n=1)
  n.times do 
    unless @io.eof?
      length = @io.read(4).unpack(@fmt["l"]).first
      @io.pos += length
      if length != @io.read(4).unpack(@fmt["l"]).first
        raise "invalid record length (should be #{length})"
      end
    end
  end
end