data_inspect 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/data_inspect.rb +97 -89
  2. metadata +1 -1
@@ -2,7 +2,7 @@
2
2
  class DataInspect
3
3
 
4
4
  def self.version
5
- '0.0.4'
5
+ '0.0.5'
6
6
  end
7
7
 
8
8
 
@@ -11,124 +11,132 @@ class DataInspect
11
11
  yield reader
12
12
  end
13
13
 
14
- end
15
14
 
16
15
 
17
- class Reader
18
16
 
19
- @@unpack_formats = {
20
- unsigned_int: {
21
- 1 => { not_applicable: 'C' },
22
- 2 => { big_endian: 'n', little_endian: 'v' },
23
- 4 => { big_endian: 'N', little_endian: 'V' }
24
- },
25
17
 
26
- signed_int: {
27
- 1 => { not_applicable: 'c' }
28
- },
29
18
 
30
- ieee_single_precision_float: {
31
- 4 => { big_endian: 'g', little_endian: 'e' }
32
- },
19
+ class Reader
20
+
21
+ @@unpack_formats = {
22
+ unsigned_int: {
23
+ 1 => { not_applicable: 'C' },
24
+ 2 => { big_endian: 'n', little_endian: 'v' },
25
+ 4 => { big_endian: 'N', little_endian: 'V' }
26
+ },
27
+
28
+ signed_int: {
29
+ 1 => { not_applicable: 'c' }
30
+ },
31
+
32
+ ieee_single_precision_float: {
33
+ 4 => { big_endian: 'g', little_endian: 'e' }
34
+ },
35
+
36
+ ieee_double_precision_float: {
37
+ 8 => { big_endian: 'G', little_endian: 'E' }
38
+ }
33
39
 
34
- ieee_double_precision_float: {
35
- 8 => { big_endian: 'G', little_endian: 'E' }
36
40
  }
37
41
 
38
- }
42
+ def initialize(filename)
43
+ @file = File.open(filename, 'rb')
44
+ end
39
45
 
40
- def initialize(filename)
41
- @file = File.open(filename, 'rb')
42
- end
46
+ def each_byte
47
+ offset = 0
48
+ @file.each_byte do |byte|
49
+ yield byte, offset
50
+ offset += 1
51
+ end
52
+ end
43
53
 
44
- def each_byte
45
- offset = 0
46
- @file.each_byte do |byte|
47
- yield byte, offset
48
- offset += 1
54
+ # optional parameter size is the number of bytes to read.
55
+ # returns a string, representing the binary data.
56
+ def next_byte(size=1)
57
+ @file.read(size)
49
58
  end
50
- end
51
59
 
52
- # optional parameter size is the number of bytes to read.
53
- # returns a string, representing the binary data.
54
- def next_byte(size=1)
55
- @file.read(size)
56
- end
60
+ def next_unsigned_int(size=1, byte_order=:not_applicable)
61
+ unpack_format = format(:unsigned_int, size, byte_order)
62
+ unpack(size, unpack_format)
63
+ end
57
64
 
58
- def next_unsigned_int(size=1, byte_order=:not_applicable)
59
- unpack_format = format(:unsigned_int, size, byte_order)
60
- unpack(size, unpack_format)
61
- end
65
+ # Ruby doesn't do this for us automatically so we
66
+ # have to add our own code (UnsignedToSignedInteger class).
67
+ def next_signed_int(size, byte_order)
68
+ unsigned_value = next_unsigned_int(size, byte_order)
69
+ UnsignedToSignedInteger.toSignedInteger(unsigned_value, size)
70
+ end
71
+
72
+ # Next IEEE single-precision floating point number
73
+ # (4 bytes)
74
+ def next_ieee_single_precision_float(byte_order)
75
+ size = 4
76
+ unpack_format = format(:ieee_single_precision_float, size, byte_order)
77
+ unpack(size, unpack_format)
78
+ end
62
79
 
63
- # Ruby doesn't do this for us automatically so we
64
- # have to add our own code (UnsignedToSignedInteger class).
65
- def next_signed_int(size, byte_order)
66
- unsigned_value = next_unsigned_int(size, byte_order)
67
- UnsignedToSignedInteger.toSignedInteger(unsigned_value, size)
68
- end
69
-
70
- # Next IEEE single-precision floating point number
71
- # (4 bytes)
72
- def next_ieee_single_precision_float(byte_order)
73
- size = 4
74
- unpack_format = format(:ieee_single_precision_float, size, byte_order)
75
- unpack(size, unpack_format)
76
- end
77
80
 
81
+ # Next IEEE double-precision floating point number
82
+ # (8 bytes)
83
+ def next_ieee_double_precision_float(byte_order)
84
+ size = 8
85
+ unpack_format = format(:ieee_double_precision_float, size, byte_order)
86
+ unpack(size, unpack_format)
87
+ end
78
88
 
79
- # Next IEEE double-precision floating point number
80
- # (8 bytes)
81
- def next_ieee_double_precision_float(byte_order)
82
- size = 8
83
- unpack_format = format(:ieee_double_precision_float, size, byte_order)
84
- unpack(size, unpack_format)
85
- end
89
+ # If the file is at the end of stream.
90
+ def atEOS?
91
+ @file.eof?
92
+ end
86
93
 
87
- # If the file is at the end of stream.
88
- def atEOS?
89
- @file.eof?
90
- end
94
+ # Seek to a given offset in the file.
95
+ def seek(offset)
96
+ @file.seek(offset)
97
+ end
91
98
 
92
- # Seek to a given offset in the file.
93
- def seek(offset)
94
- @file.seek(offset)
95
- end
99
+ private
96
100
 
97
- private
101
+ # returns the String.unpack format string given the
102
+ # parameter details (data type, size, and byte order).
103
+ # byte_order is not required when size (bytes) is 1.
104
+ # size defaults to 1.
105
+ def format(type, size=1, byte_order=:not_applicable)
106
+ @@unpack_formats[type][size][byte_order]
107
+ end
98
108
 
99
- # returns the String.unpack format string given the
100
- # parameter details (data type, size, and byte order).
101
- # byte_order is not required when size (bytes) is 1.
102
- # size defaults to 1.
103
- def format(type, size=1, byte_order=:not_applicable)
104
- @@unpack_formats[type][size][byte_order]
105
- end
109
+ # Consumes 'size' bytes from the file,
110
+ # uses String.unpack() to interpret that data,
111
+ # using the given unpack format string.
112
+ def unpack(size, unpack_format)
113
+ @file.read(size).unpack(unpack_format)[0]
114
+ end
106
115
 
107
- # Consumes 'size' bytes from the file,
108
- # uses String.unpack() to interpret that data,
109
- # using the given unpack format string.
110
- def unpack(size, unpack_format)
111
- @file.read(size).unpack(unpack_format)[0]
112
116
  end
113
117
 
114
- end
118
+ class UnsignedToSignedInteger
115
119
 
116
- class UnsignedToSignedInteger
120
+ # Size is in bytes, and should be 1, 2 or 4.
121
+ def self.toSignedInteger(unsigned_value, size)
122
+ subtrahend = 2 ** (size * 8)
117
123
 
118
- # Size is in bytes, and should be 1, 2 or 4.
119
- def self.toSignedInteger(unsigned_value, size)
120
- subtrahend = 2 ** (size * 8)
124
+ puts "subtrahend is #{subtrahend}."
121
125
 
122
- puts "subtrahend is #{subtrahend}."
126
+ max_value = 2 ** (size * 8 - 1) - 1
127
+ puts "max value is #{max_value}"
123
128
 
124
- max_value = 2 ** (size * 8 - 1) - 1
125
- puts "max value is #{max_value}"
129
+ result = unsigned_value
130
+ result -= subtrahend if (unsigned_value > max_value)
126
131
 
127
- result = unsigned_value
128
- result -= subtrahend if (unsigned_value > max_value)
132
+ return result
133
+ end
129
134
 
130
- return result
131
135
  end
132
136
 
137
+
138
+
133
139
  end
134
140
 
141
+
142
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_inspect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: