Class: Lightwave::LightwaveStringIO

Inherits:
StringIO
  • Object
show all
Defined in:
lib/lightwave.rb

Instance Method Summary collapse

Instance Method Details

#read_chunk(size = 4) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lightwave.rb', line 57

def read_chunk(size=4)
  label = read_type
  size_in = read(size)
  if size_in.length != size
    raise 'String was shorter than expected.'
  end
  size_str = (["\0"]*(4-size)).join + size_in
  chunk_size = size_str.unpack('N')[0]
  data = read(chunk_size)
  if data.length % 2 == 1
    read(1)
  end
  if data.length != chunk_size
    raise 'Chunk was shorter than expected.  Size string was ' + size_in.inspect + ', expected ' + chunk_size.to_s + ', read ' + data.length.to_s
  end
  return [label, LightwaveStringIO.new(data), size_str]
end

#read_colourObject



74
75
76
# File 'lib/lightwave.rb', line 74

def read_colour
  result = [read_float, read_float, read_float]
end

#read_floatObject



48
49
50
# File 'lib/lightwave.rb', line 48

def read_float
  read(4).unpack('g')[0]
end

#read_poly_setObject



27
28
29
30
31
32
# File 'lib/lightwave.rb', line 27

def read_poly_set
  spec = self.read(2).unpack('n')[0]
  vert_count = spec & 0x03FF
  flags = spec & 0xFC00
  return Array.new(vert_count){self.read_vx}
end

#read_stringObject



33
34
35
36
37
38
39
# File 'lib/lightwave.rb', line 33

def read_string
  result = readline("\0")
  if result.length % 2 == 1
    read(1)
  end
  return result.strip
end

#read_string_listObject



40
41
42
43
44
45
46
47
# File 'lib/lightwave.rb', line 40

def read_string_list
  # used in the TAGS block
  result  = []
  while !self.eof?
    result << read_string
  end
  return result
end

#read_typeObject



13
14
15
# File 'lib/lightwave.rb', line 13

def read_type
  read(4)
end

#read_u2Object



51
52
53
# File 'lib/lightwave.rb', line 51

def read_u2
  read(2).unpack('n')[0]
end

#read_u4Object



54
55
56
# File 'lib/lightwave.rb', line 54

def read_u4
  read(4).unpack('N')[0]
end

#read_vectorObject



10
11
12
# File 'lib/lightwave.rb', line 10

def read_vector
  read(12).unpack('g3')
end

#read_vxObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lightwave.rb', line 16

def read_vx
  # Reads a variable-length index.
  marker_str = read(2, "  ")
  #if !marker_str then raise "Null marker." end # These tests *seriously* slow things down
  if marker_str[0] != 255
    return marker_str.unpack('n')[0]
  else
    #if !low_byte then raise "Null low byte." end
    return ("\0#{marker_str[1]}" + read(2, "  ")).unpack('N')[0]
  end
end