cauterize 0.0.1.pre10 → 0.0.1.pre11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Cauterize
2
- VERSION = "0.0.1.pre10"
2
+ VERSION = "0.0.1.pre11"
3
3
  end
@@ -1,11 +1,5 @@
1
1
  require 'stringio'
2
2
 
3
- def takeBytes!(num_bytes, str)
4
- byte = str.slice!(0, num_bytes)
5
- raise "Unexpected end of string" if byte == ""
6
- byte
7
- end
8
-
9
3
  class CauterizeData
10
4
  def self.construct(x)
11
5
  if x.is_a? CauterizeData
@@ -27,21 +21,44 @@ class CauterizeBuiltin < CauterizeData
27
21
  raise "Out of range value" if not in_range(val)
28
22
  @val = val
29
23
  end
24
+ end
25
+
26
+ class CauterizeBuiltinInteger < CauterizeBuiltin
27
+ def initialize(val)
28
+ super(val.to_i)
29
+ end
30
+
30
31
  def to_i
31
- return @val.to_i
32
+ val
33
+ end
34
+ end
35
+
36
+ class CauterizeBuiltinFloat < CauterizeBuiltin
37
+ def initialize(val)
38
+ super(val.to_f)
32
39
  end
40
+
33
41
  def to_f
34
- return @val.to_f
42
+ val
43
+ end
44
+ end
45
+
46
+ class CauterizeBuiltinBool < CauterizeBuiltin
47
+ def initialize(val)
48
+ super((val)? true : false)
35
49
  end
36
50
  end
37
51
 
38
- class CauterizeScalar < CauterizeData
39
- attr_reader :val
40
52
 
53
+ class CauterizeScalar < CauterizeData
41
54
  def initialize(val)
42
55
  @val = self.class.builtin.construct val
43
56
  end
44
57
 
58
+ def val
59
+ @val.val
60
+ end
61
+
45
62
  def pack
46
63
  @val.pack
47
64
  end
@@ -51,8 +68,17 @@ class CauterizeScalar < CauterizeData
51
68
  end
52
69
  end
53
70
 
71
+ class CauterizeArray < CauterizeData
72
+ def val
73
+ @elems.map{|e| e.val}
74
+ end
75
+
76
+ def to_s
77
+ val.to_a.pack("C*")
78
+ end
79
+ end
54
80
 
55
- class CauterizeFixedArray < CauterizeData
81
+ class CauterizeFixedArray < CauterizeArray
56
82
  attr_reader :elems
57
83
 
58
84
  def initialize(elems)
@@ -70,7 +96,7 @@ class CauterizeFixedArray < CauterizeData
70
96
  end
71
97
 
72
98
 
73
- class CauterizeVariableArray < CauterizeData
99
+ class CauterizeVariableArray < CauterizeArray
74
100
  attr_reader :length
75
101
  attr_reader :elems
76
102
 
@@ -80,6 +106,10 @@ class CauterizeVariableArray < CauterizeData
80
106
  raise "Invalid length" if @elems.length > self.class.max_length
81
107
  end
82
108
 
109
+ def val
110
+ @elems.map{|e| e.val}
111
+ end
112
+
83
113
  def pack
84
114
  @length.pack + @elems.inject("") { |sum, n| sum + n.pack }
85
115
  end
@@ -104,6 +134,10 @@ class CauterizeComposite < CauterizeData
104
134
  end]
105
135
  end
106
136
 
137
+ def val
138
+ Hash[@fields.map{|name, value| [name, value.val]}]
139
+ end
140
+
107
141
  def pack
108
142
  @fields.values.inject("") { |sum, v| sum + v.pack }
109
143
  end
@@ -134,6 +168,10 @@ class CauterizeEnumeration < CauterizeData
134
168
  @field_name = field_name
135
169
  end
136
170
 
171
+ def val
172
+ @field_name
173
+ end
174
+
137
175
  def val() self.class.fields[@field_name] end
138
176
 
139
177
  def pack
@@ -146,7 +184,7 @@ class CauterizeEnumeration < CauterizeData
146
184
  end
147
185
 
148
186
  def self.unpackio(str)
149
- self.from_int(self.repr_type.unpackio(str))
187
+ self.from_int(self.repr_type.unpackio(str).to_i)
150
188
  end
151
189
  end
152
190
 
@@ -155,23 +193,37 @@ class CauterizeGroup < CauterizeData
155
193
  attr_reader :tag
156
194
  attr_reader :data
157
195
 
158
- def to_tag_name(field_name)
159
- (self.class.tag_prefix + field_name.to_s).to_sym
196
+ def val
197
+ if data.nil?
198
+ { tag: tag_field_name }
199
+ else
200
+ { tag: tag_field_name,
201
+ data: data.val }
202
+ end
203
+ end
204
+
205
+ def tag_field_name
206
+ self.class.from_tag_field_name(@tag.field_name)
207
+ end
208
+
209
+ def self.tag_from_field_name(field_name)
210
+ self.tag_type.construct((self.tag_prefix + field_name.to_s).to_sym)
160
211
  end
161
212
 
162
- def self.from_field_name(tag_name)
213
+ def self.from_tag_field_name(tag_name)
163
214
  t = tag_name.to_s
164
215
  t.slice!(self.tag_prefix)
165
216
  t.to_sym
166
217
  end
167
218
 
168
- def initialize(tag, data = nil)
169
- @tag = self.class.tag_type.construct(to_tag_name(tag))
170
- field_class = self.class.fields[self.class.from_field_name(@tag.field_name)]
219
+ def initialize(h)
220
+ @tag = self.class.tag_from_field_name(h[:tag])
221
+
222
+ field_class = self.class.fields[tag_field_name]
171
223
  if field_class.nil?
172
- @data = data
224
+ @data = nil
173
225
  else
174
- @data = field_class.construct(data)
226
+ @data = field_class.construct(h[:data])
175
227
  end
176
228
  end
177
229
 
@@ -185,12 +237,12 @@ class CauterizeGroup < CauterizeData
185
237
 
186
238
  def self.unpackio(str)
187
239
  tag = self.tag_type.unpackio(str)
188
- field_name = self.from_field_name(tag.field_name)
189
- data = self.fields[field_name]
190
- if data.nil?
191
- self.new(field_name)
240
+ field_name = self.from_tag_field_name(tag.field_name)
241
+ data_type = self.fields[field_name]
242
+ if data_type.nil?
243
+ self.new({ tag: field_name })
192
244
  else
193
- self.new(field_name, data.unpackio(str))
245
+ self.new({ tag: field_name, data: data_type.unpackio(str) })
194
246
  end
195
247
  end
196
248
  end
@@ -1,6 +1,6 @@
1
1
  require_relative './cauterize_ruby_baseclasses'
2
2
 
3
- class UInt8 < CauterizeBuiltin
3
+ class UInt8 < CauterizeBuiltinInteger
4
4
  def in_range(v) v >= 0 && v < 2**8 end
5
5
  def pack
6
6
  [val.to_i].pack("C")
@@ -10,7 +10,7 @@ class UInt8 < CauterizeBuiltin
10
10
  end
11
11
  end
12
12
 
13
- class UInt16 < CauterizeBuiltin
13
+ class UInt16 < CauterizeBuiltinInteger
14
14
  def in_range(v) v >= 0 && v < 2**16 end
15
15
  def pack
16
16
  [val.to_i].pack("S")
@@ -20,7 +20,7 @@ class UInt16 < CauterizeBuiltin
20
20
  end
21
21
  end
22
22
 
23
- class UInt32 < CauterizeBuiltin
23
+ class UInt32 < CauterizeBuiltinInteger
24
24
  def in_range(v) v >= 0 && v < 2**32 end
25
25
  def pack
26
26
  [val.to_i].pack("L")
@@ -30,7 +30,7 @@ class UInt32 < CauterizeBuiltin
30
30
  end
31
31
  end
32
32
 
33
- class UInt64 < CauterizeBuiltin
33
+ class UInt64 < CauterizeBuiltinInteger
34
34
  def in_range(v) v >= 0 && v < 2**64 end
35
35
  def pack
36
36
  [val.to_i].pack("Q")
@@ -40,7 +40,7 @@ class UInt64 < CauterizeBuiltin
40
40
  end
41
41
  end
42
42
 
43
- class Int8 < CauterizeBuiltin
43
+ class Int8 < CauterizeBuiltinInteger
44
44
  def in_range(v) (v >= -2**7) && (v < 2**7) end
45
45
  def pack
46
46
  [val.to_i].pack("c")
@@ -50,7 +50,7 @@ class Int8 < CauterizeBuiltin
50
50
  end
51
51
  end
52
52
 
53
- class Int16 < CauterizeBuiltin
53
+ class Int16 < CauterizeBuiltinInteger
54
54
  def in_range(v) (v >= -2**15) && (v < 2**15) end
55
55
  def pack
56
56
  [val.to_i].pack("s")
@@ -60,7 +60,7 @@ class Int16 < CauterizeBuiltin
60
60
  end
61
61
  end
62
62
 
63
- class Int32 < CauterizeBuiltin
63
+ class Int32 < CauterizeBuiltinInteger
64
64
  def in_range(v) (v >= -2**31) && (v < 2**31) end
65
65
  def pack
66
66
  [val.to_i].pack("l")
@@ -70,7 +70,7 @@ class Int32 < CauterizeBuiltin
70
70
  end
71
71
  end
72
72
 
73
- class Int64 < CauterizeBuiltin
73
+ class Int64 < CauterizeBuiltinInteger
74
74
  def in_range(v) (v >= -2**63) && (v < 2**63) end
75
75
  def pack
76
76
  [val.to_i].pack("q")
@@ -80,7 +80,7 @@ class Int64 < CauterizeBuiltin
80
80
  end
81
81
  end
82
82
 
83
- class Bool < CauterizeBuiltin
83
+ class Bool < CauterizeBuiltinBool
84
84
  def in_range(v) true end
85
85
  def pack
86
86
  if @val
@@ -98,7 +98,7 @@ class Bool < CauterizeBuiltin
98
98
  end
99
99
  end
100
100
 
101
- class Float32 < CauterizeBuiltin
101
+ class Float32 < CauterizeBuiltinFloat
102
102
  def in_range(v) v > (-3.402823466e38) && v < (3.402823466e38) end
103
103
  def pack
104
104
  [val.to_f].pack("f")
@@ -108,7 +108,7 @@ class Float32 < CauterizeBuiltin
108
108
  end
109
109
  end
110
110
 
111
- class Float64 < CauterizeBuiltin
111
+ class Float64 < CauterizeBuiltinFloat
112
112
  def in_range(v) true end
113
113
  def pack
114
114
  [val.to_f].pack("d")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cauterize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre10
4
+ version: 0.0.1.pre11
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-31 00:00:00.000000000 Z
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -263,7 +263,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
263
263
  version: '0'
264
264
  segments:
265
265
  - 0
266
- hash: 3445789086503525251
266
+ hash: -3567501395549835058
267
267
  required_rubygems_version: !ruby/object:Gem::Requirement
268
268
  none: false
269
269
  requirements: