Module: BinaryReader
- Defined in:
- lib/carat/binaryreader.rb
Instance Method Summary collapse
-
#byte_order ⇒ Object
(also: #byteorder)
default is native byte-order.
- #byte_order=(new_byteorder) ⇒ Object (also: #byteorder=)
- #read_cstring ⇒ Object
- #read_int16_big ⇒ Object
- #read_int16_little ⇒ Object
-
#read_int16_native ⇒ Object
Signed.
- #read_int32_big ⇒ Object
- #read_int32_little ⇒ Object
-
#read_int32_native ⇒ Object
Signed.
- #read_int8 ⇒ Object
- #read_word16_big ⇒ Object
- #read_word16_little ⇒ Object
-
#read_word16_native ⇒ Object
Unsigned.
- #read_word32_big ⇒ Object
- #read_word32_little ⇒ Object
-
#read_word32_native ⇒ Object
Unsigned.
-
#read_word8 ⇒ Object
(also: #read_uint8)
no byteorder for 8 bit!.
-
#readn(n) ⇒ Object
read exactly n characters, otherwise raise an exception.
Instance Method Details
#byte_order ⇒ Object Also known as: byteorder
default is native byte-order
26 27 28 |
# File 'lib/carat/binaryreader.rb', line 26 def byte_order @byte_order || ByteOrder::Native end |
#byte_order=(new_byteorder) ⇒ Object Also known as: byteorder=
30 31 32 |
# File 'lib/carat/binaryreader.rb', line 30 def byte_order=(new_byteorder) @byte_order = new_byteorder end |
#read_cstring ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/carat/binaryreader.rb', line 140 def read_cstring str = "" while (c=readn(1)) != "\0" str << c end str end |
#read_int16_big ⇒ Object
76 77 78 79 |
# File 'lib/carat/binaryreader.rb', line 76 def read_int16_big # swap bytes if native=little (but we want big) ru_swap(2, 's', ByteOrder::Little) end |
#read_int16_little ⇒ Object
71 72 73 74 |
# File 'lib/carat/binaryreader.rb', line 71 def read_int16_little # swap bytes if native=big (but we want little) ru_swap(2, 's', ByteOrder::Big) end |
#read_int16_native ⇒ Object
Signed
67 68 69 |
# File 'lib/carat/binaryreader.rb', line 67 def read_int16_native ru(2, 's') end |
#read_int32_big ⇒ Object
108 109 110 111 |
# File 'lib/carat/binaryreader.rb', line 108 def read_int32_big # swap bytes if native=little (but we want big) ru_swap(4, 'l', ByteOrder::Little) end |
#read_int32_little ⇒ Object
103 104 105 106 |
# File 'lib/carat/binaryreader.rb', line 103 def read_int32_little # swap bytes if native=big (but we want little) ru_swap(4, 'l', ByteOrder::Big) end |
#read_int32_native ⇒ Object
Signed
99 100 101 |
# File 'lib/carat/binaryreader.rb', line 99 def read_int32_native ru(4, 'l') end |
#read_int8 ⇒ Object
45 46 47 |
# File 'lib/carat/binaryreader.rb', line 45 def read_int8 ru(1, 'c') end |
#read_word16_big ⇒ Object
61 62 63 |
# File 'lib/carat/binaryreader.rb', line 61 def read_word16_big ru(2, 'n') end |
#read_word16_little ⇒ Object
57 58 59 |
# File 'lib/carat/binaryreader.rb', line 57 def read_word16_little ru(2, 'v') end |
#read_word16_native ⇒ Object
Unsigned
53 54 55 |
# File 'lib/carat/binaryreader.rb', line 53 def read_word16_native ru(2, 'S') end |
#read_word32_big ⇒ Object
93 94 95 |
# File 'lib/carat/binaryreader.rb', line 93 def read_word32_big ru(4, 'N') end |
#read_word32_little ⇒ Object
89 90 91 |
# File 'lib/carat/binaryreader.rb', line 89 def read_word32_little ru(4, 'V') end |
#read_word32_native ⇒ Object
Unsigned
85 86 87 |
# File 'lib/carat/binaryreader.rb', line 85 def read_word32_native ru(4, 'L') end |
#read_word8 ⇒ Object Also known as: read_uint8
no byteorder for 8 bit!
41 42 43 |
# File 'lib/carat/binaryreader.rb', line 41 def read_word8 ru(1, 'C') end |
#readn(n) ⇒ Object
read exactly n characters, otherwise raise an exception.
149 150 151 152 153 |
# File 'lib/carat/binaryreader.rb', line 149 def readn(n) str = read(n) raise "couldn't read #{n} characters" if str.nil? or str.size != n str end |