Module: BinaryIO::Readable

Included in:
BinaryIO
Defined in:
lib/binaryio/readable.rb

Overview

This mixin solely depends on method read(n), which must be defined in the class/module where you mix in this module.

Copyright (c) Michael Neumann

Instance Method Summary (collapse)

Instance Method Details

- (Object) byte_order Also known as: byteorder

default is native byte-order



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

def byte_order
  @byte_order || ByteOrder::Native
end

- (Object) byte_order=(new_byteorder) Also known as: byteorder=



17
18
19
# File 'lib/binaryio/readable.rb', line 17

def byte_order=(new_byteorder)
  @byte_order = new_byteorder
end

- (Object) read_cstring



127
128
129
130
131
132
133
# File 'lib/binaryio/readable.rb', line 127

def read_cstring
  str = ""
  while (c=readn(1)) != "\0"
    str << c
  end
  str
end

- (Object) read_int16_big



63
64
65
66
# File 'lib/binaryio/readable.rb', line 63

def read_int16_big
  # swap bytes if native=little (but we want big)
  ru_swap(2, 's', ByteOrder::Little)
end

- (Object) read_int16_little



58
59
60
61
# File 'lib/binaryio/readable.rb', line 58

def read_int16_little
  # swap bytes if native=big (but we want little)
  ru_swap(2, 's', ByteOrder::Big)
end

- (Object) read_int16_native

Signed



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

def read_int16_native
  ru(2, 's')
end

- (Object) read_int32_big



95
96
97
98
# File 'lib/binaryio/readable.rb', line 95

def read_int32_big
  # swap bytes if native=little (but we want big)
  ru_swap(4, 'l', ByteOrder::Little)
end

- (Object) read_int32_little



90
91
92
93
# File 'lib/binaryio/readable.rb', line 90

def read_int32_little
  # swap bytes if native=big (but we want little)
  ru_swap(4, 'l', ByteOrder::Big)
end

- (Object) read_int32_native

Signed



86
87
88
# File 'lib/binaryio/readable.rb', line 86

def read_int32_native
  ru(4, 'l')
end

- (Object) read_int8



32
33
34
# File 'lib/binaryio/readable.rb', line 32

def read_int8
  ru(1, 'c')
end

- (Object) read_template(*tmpl, &block)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/binaryio/readable.rb', line 138

def read_template(*tmpl, &block)
  arr = [] if block.nil?
  rep = 1 
  tmpl.each { |spec|
    case spec
    when Fixnum
      rep = spec
    when Symbol, String
      rep.times do 
        val = send("read_#{spec}")
        if block.nil?
          arr << val 
        else
          block.call(val)
        end
      end
      rep = 1
    else
      raise
    end
  }
  arr
end

- (Object) read_template_to_hash(*tmpl)



163
164
165
166
167
168
169
# File 'lib/binaryio/readable.rb', line 163

def read_template_to_hash(*tmpl)
  hash = {}
  tmpl.each do |key, typ|
    hash[key] = send("read_#{typ}")
  end
  hash
end

- (Object) read_word16_big



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

def read_word16_big
  ru(2, 'n')
end

- (Object) read_word16_little



44
45
46
# File 'lib/binaryio/readable.rb', line 44

def read_word16_little
  ru(2, 'v')
end

- (Object) read_word16_native

Unsigned



40
41
42
# File 'lib/binaryio/readable.rb', line 40

def read_word16_native
  ru(2, 'S')
end

- (Object) read_word32_big



80
81
82
# File 'lib/binaryio/readable.rb', line 80

def read_word32_big
  ru(4, 'N')
end

- (Object) read_word32_little



76
77
78
# File 'lib/binaryio/readable.rb', line 76

def read_word32_little
  ru(4, 'V')
end

- (Object) read_word32_native

Unsigned



72
73
74
# File 'lib/binaryio/readable.rb', line 72

def read_word32_native
  ru(4, 'L')
end

- (Object) read_word8 Also known as: read_uint8

no byteorder for 8 bit!



28
29
30
# File 'lib/binaryio/readable.rb', line 28

def read_word8
  ru(1, 'C')
end

- (Object) readn(n)

read exactly n characters, otherwise raise an exception.



172
173
174
175
176
# File 'lib/binaryio/readable.rb', line 172

def readn(n)
  str = read(n)
  raise "couldn't read #{n} characters" if str.nil? or str.size != n
  str
end