BitStructEx 0.0.74 → 0.0.85
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.
- data/src/bit_struct/byte_data.rb +17 -3
- data/src/bit_struct/endianess.rb +12 -0
- data/src/bit_struct/field.rb +4 -0
- data/src/bit_struct/nested.rb +4 -0
- data/src/bit_struct/struct_base.rb +21 -0
- metadata +3 -3
data/src/bit_struct/byte_data.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
|
+
require 'bit_struct/endianess.rb'
|
3
|
+
|
2
4
|
module BitStruct
|
3
5
|
|
4
6
|
class ByteData
|
5
7
|
|
6
|
-
|
7
|
-
BIG_ENDIAN = BIG = MOTOROLA = 1
|
8
|
-
NETWORK_BYTE_ORDER = BIG_ENDIAN
|
8
|
+
include Endianess
|
9
9
|
|
10
10
|
def initialize( data, endianess = nil )
|
11
11
|
raise "Bad input" unless data.respond_to? "[]".to_sym
|
@@ -40,6 +40,10 @@ module BitStruct
|
|
40
40
|
@data[ endianized( idx ) ] = val
|
41
41
|
end
|
42
42
|
|
43
|
+
def reverse
|
44
|
+
ReversedByteData.new self, BIG_ENDIAN
|
45
|
+
end
|
46
|
+
|
43
47
|
private
|
44
48
|
|
45
49
|
def valid( endianess )
|
@@ -53,4 +57,14 @@ module BitStruct
|
|
53
57
|
|
54
58
|
end
|
55
59
|
|
60
|
+
class ReversedByteData < ByteData
|
61
|
+
|
62
|
+
alias super_endianized endianized
|
63
|
+
|
64
|
+
def endianized( idx )
|
65
|
+
res = super_endianized( idx )
|
66
|
+
@data.length - res - 1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
56
70
|
end
|
data/src/bit_struct/field.rb
CHANGED
data/src/bit_struct/nested.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
|
2
2
|
require 'bit_struct/bit_slicer'
|
3
3
|
require 'bit_struct/byte_data'
|
4
|
+
require 'bit_struct/endianess'
|
4
5
|
require 'bit_struct/field'
|
5
6
|
|
6
7
|
# The BitStruct module primarily provides the StructBase class, which in turn
|
@@ -48,6 +49,8 @@ module BitStruct
|
|
48
49
|
#
|
49
50
|
class StructBase
|
50
51
|
|
52
|
+
include Endianess
|
53
|
+
|
51
54
|
attr_reader :data
|
52
55
|
|
53
56
|
def initialize( initial_data = nil, endianess = nil )
|
@@ -64,6 +67,16 @@ module BitStruct
|
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
70
|
+
def to_s( delimiter = "\n" )
|
71
|
+
res = ''
|
72
|
+
self.class.fields.each do |field|
|
73
|
+
slicer = self.class.get_slicer_for field
|
74
|
+
res += delimiter if res.length > 0 && delimiter
|
75
|
+
res += "#{field.name} = #{field.to_s slicer, delimiter}"
|
76
|
+
end
|
77
|
+
res
|
78
|
+
end
|
79
|
+
|
67
80
|
class << self
|
68
81
|
|
69
82
|
def fields
|
@@ -79,6 +92,10 @@ module BitStruct
|
|
79
92
|
def add_field( field )
|
80
93
|
context = self
|
81
94
|
|
95
|
+
define_method "gsf" do |field|
|
96
|
+
context.send :get_slicer_for, field
|
97
|
+
end
|
98
|
+
|
82
99
|
define_method "#{field.name}" do
|
83
100
|
slicer = context.send :get_slicer_for, field
|
84
101
|
slicer.data = @data
|
@@ -94,6 +111,8 @@ module BitStruct
|
|
94
111
|
fields << field
|
95
112
|
end
|
96
113
|
|
114
|
+
public
|
115
|
+
|
97
116
|
def get_slicer_for( field )
|
98
117
|
unless slicers[ field ]
|
99
118
|
bit_offset, bit_length = get_field_spec field
|
@@ -102,6 +121,8 @@ module BitStruct
|
|
102
121
|
slicers[ field ]
|
103
122
|
end
|
104
123
|
|
124
|
+
private
|
125
|
+
|
105
126
|
def get_field_spec( field )
|
106
127
|
index = fields.index field
|
107
128
|
offset = 0
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: BitStructEx
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-05-
|
6
|
+
version: 0.0.85
|
7
|
+
date: 2006-05-18 00:00:00 +02:00
|
8
8
|
summary: Simple DSL for defining bit-based structures on byte data.
|
9
9
|
require_paths:
|
10
10
|
- src
|
@@ -29,9 +29,9 @@ authors:
|
|
29
29
|
- The.French.DJ
|
30
30
|
files:
|
31
31
|
- src/bit_struct
|
32
|
-
- src/rake
|
33
32
|
- src/bit_struct/bit_slicer.rb
|
34
33
|
- src/bit_struct/byte_data.rb
|
34
|
+
- src/bit_struct/endianess.rb
|
35
35
|
- src/bit_struct/field.rb
|
36
36
|
- src/bit_struct/nested.rb
|
37
37
|
- src/bit_struct/struct_base.rb
|