cauterize 0.0.1.pre8 → 0.0.1.pre9
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/cauterize +1 -1
- data/lib/cauterize/builders/c/group.rb +2 -2
- data/lib/cauterize/builders/ruby/buildable.rb +11 -0
- data/lib/cauterize/builders/ruby/builtin.rb +25 -0
- data/lib/cauterize/builders/ruby/composite.rb +23 -0
- data/lib/cauterize/builders/ruby/enumeration.rb +27 -0
- data/lib/cauterize/builders/ruby/fixed_array.rb +20 -0
- data/lib/cauterize/builders/ruby/group.rb +35 -0
- data/lib/cauterize/builders/ruby/scalar.rb +19 -0
- data/lib/cauterize/builders/ruby/variable_array.rb +22 -0
- data/lib/cauterize/cauterize.rb +7 -0
- data/lib/cauterize/ruby_builder.rb +34 -0
- data/lib/cauterize/version.rb +1 -1
- data/spec/builders_spec.rb +4 -0
- data/spec/ruby_builder_spec.rb +80 -0
- data/support/c/src/cauterize.h +1 -1
- data/support/ruby/src/cauterize_ruby_baseclasses.rb +196 -0
- data/support/ruby/src/cauterize_ruby_builtins.rb +122 -0
- metadata +16 -3
data/bin/cauterize
CHANGED
@@ -30,7 +30,7 @@ class CauterizeCmdline < Thor
|
|
30
30
|
end
|
31
31
|
|
32
32
|
language = language.downcase
|
33
|
-
if %w(c cs doc).include? language
|
33
|
+
if %w(c cs ruby doc).include? language
|
34
34
|
Cauterize.generate language, target_dir, desc_file
|
35
35
|
else
|
36
36
|
raise CmdLineException.new("Language '#{language}' is not supported.")
|
@@ -37,7 +37,7 @@ module Cauterize
|
|
37
37
|
format_no_data_stubs(formatter)
|
38
38
|
|
39
39
|
formatter.backdent "default:"
|
40
|
-
formatter << "return
|
40
|
+
formatter << "return CA_ERR_INVALID_TYPE_TAG;"
|
41
41
|
formatter << "break;"
|
42
42
|
end
|
43
43
|
formatter << "return CA_OK;"
|
@@ -66,7 +66,7 @@ module Cauterize
|
|
66
66
|
format_no_data_stubs(formatter)
|
67
67
|
|
68
68
|
formatter.backdent "default:"
|
69
|
-
formatter << "return
|
69
|
+
formatter << "return CA_ERR_INVALID_TYPE_TAG;"
|
70
70
|
formatter << "break;"
|
71
71
|
end
|
72
72
|
formatter << "return CA_OK;"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class BuiltIn < Buildable
|
3
|
+
@@RUBY_TYPE_MAPPING = {
|
4
|
+
1 => {signed: "Int8", unsigned: "UInt8", :float => nil, :bool => "Bool"},
|
5
|
+
2 => {signed: "Int16", unsigned: "UInt16", :float => nil, :bool => nil},
|
6
|
+
4 => {signed: "Int32", unsigned: "UInt32", :float => "Float32", :bool => nil},
|
7
|
+
8 => {signed: "Int64", unsigned: "UInt64", :float => "Float64", :bool => nil},
|
8
|
+
}
|
9
|
+
|
10
|
+
def render
|
11
|
+
render_rbtype
|
12
|
+
end
|
13
|
+
|
14
|
+
def class_defn(f)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def render_rbtype
|
20
|
+
@@RUBY_TYPE_MAPPING[@blueprint.byte_length][@blueprint.flavor]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Cauterize::Builders.register(:ruby, Cauterize::BuiltIn, Cauterize::Builders::Ruby::BuiltIn)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class Composite < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_defn(f)
|
8
|
+
f << "class #{render} < CauterizeComposite"
|
9
|
+
f << " def self.fields"
|
10
|
+
f << " {"
|
11
|
+
@blueprint.fields.values.each_with_index do |field, i|
|
12
|
+
t = Cauterize::Builders.get(:ruby, field.type).render
|
13
|
+
f << " #{field.name}: #{t},"
|
14
|
+
end
|
15
|
+
f << " }"
|
16
|
+
f << " end"
|
17
|
+
f << "end"
|
18
|
+
f << ""
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Cauterize::Builders.register(:ruby, Cauterize::Composite, Cauterize::Builders::Ruby::Composite)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class Enumeration < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_defn(f)
|
8
|
+
rep_builder = Cauterize::Builders.get(:ruby, @blueprint.representation)
|
9
|
+
f << "class #{render} < CauterizeEnumeration"
|
10
|
+
f << " def self.repr_type() #{rep_builder.render} end"
|
11
|
+
f << " def self.fields"
|
12
|
+
f << " {"
|
13
|
+
@blueprint.values.values.each do |v|
|
14
|
+
f << " #{v.name.to_s.upcase}: #{v.value},"
|
15
|
+
end
|
16
|
+
f << " }"
|
17
|
+
f << " end"
|
18
|
+
@blueprint.values.values.each do |v|
|
19
|
+
f << " #{v.name.to_s.upcase} = #{render}.new(:#{v.name.to_s.upcase})"
|
20
|
+
end
|
21
|
+
f << "end"
|
22
|
+
f << ""
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Cauterize::Builders.register(:ruby, Cauterize::Enumeration, Cauterize::Builders::Ruby::Enumeration)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class FixedArray < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_defn(f)
|
8
|
+
array_type_bldr = Cauterize::Builders.get(:ruby, @blueprint.array_type)
|
9
|
+
x = <<EOF
|
10
|
+
class #{render} < CauterizeFixedArray
|
11
|
+
def self.length () #{@blueprint.array_size} end
|
12
|
+
def self.elem_type() #{array_type_bldr.render} end
|
13
|
+
end
|
14
|
+
EOF
|
15
|
+
f << x
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Cauterize::Builders.register(:ruby, Cauterize::FixedArray, Cauterize::Builders::Ruby::FixedArray)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class Group < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
def initialize(blueprint)
|
7
|
+
super(blueprint)
|
8
|
+
@tag_enum = blueprint.tag_enum
|
9
|
+
end
|
10
|
+
|
11
|
+
def class_defn(f)
|
12
|
+
tag = Cauterize::Builders.get(:ruby, @tag_enum).render
|
13
|
+
|
14
|
+
f << "class #{render} < CauterizeGroup"
|
15
|
+
f << " def self.tag_type() #{tag} end"
|
16
|
+
f << " def self.tag_prefix() '#{@blueprint.tag_enum.name.upcase}_' end"
|
17
|
+
f << " def self.fields"
|
18
|
+
f << " {"
|
19
|
+
@blueprint.fields.values.each_with_index do |field, i|
|
20
|
+
if field.type
|
21
|
+
t = Cauterize::Builders.get(:ruby, field.type).render
|
22
|
+
f << " #{field.name.to_s.upcase}: #{t},"
|
23
|
+
else
|
24
|
+
f << " #{field.name.to_s.upcase}: nil,"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
f << " }"
|
28
|
+
f << " end"
|
29
|
+
f << "end"
|
30
|
+
f << ""
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Cauterize::Builders.register(:ruby, Cauterize::Group, Cauterize::Builders::Ruby::Group)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class Scalar < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_defn(f)
|
8
|
+
tn_bldr = Cauterize::Builders.get(:ruby, @blueprint.type_name)
|
9
|
+
x = <<EOF
|
10
|
+
class #{render} < CauterizeScalar
|
11
|
+
def self.builtin() #{tn_bldr.render} end
|
12
|
+
end
|
13
|
+
EOF
|
14
|
+
f << x
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Cauterize::Builders.register(:ruby, Cauterize::Scalar, Cauterize::Builders::Ruby::Scalar)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Cauterize::Builders::Ruby
|
2
|
+
class VariableArray < Buildable
|
3
|
+
def render
|
4
|
+
@blueprint.name.to_s.camel
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_defn(f)
|
8
|
+
array_type_bldr = Cauterize::Builders.get(:ruby, @blueprint.array_type)
|
9
|
+
size_type_bldr = Cauterize::Builders.get(:ruby, @blueprint.size_type)
|
10
|
+
x = <<EOF
|
11
|
+
class #{render} < CauterizeVariableArray
|
12
|
+
def self.size_type () #{size_type_bldr.render} end
|
13
|
+
def self.max_length () #{@blueprint.array_size} end
|
14
|
+
def self.elem_type() #{array_type_bldr.render} end
|
15
|
+
end
|
16
|
+
EOF
|
17
|
+
f << x
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Cauterize::Builders.register(:ruby, Cauterize::VariableArray, Cauterize::Builders::Ruby::VariableArray)
|
data/lib/cauterize/cauterize.rb
CHANGED
@@ -29,6 +29,13 @@ module Cauterize
|
|
29
29
|
Cauterize::CSBuilder.new(cs_file, output_prefix)
|
30
30
|
end
|
31
31
|
|
32
|
+
# Generate the Ruby code corresponding to the generated configuration
|
33
|
+
def make_builder_ruby(target_dir, output_prefix)
|
34
|
+
rb_file = File.join(target_dir, "#{output_prefix}.rb")
|
35
|
+
|
36
|
+
Cauterize::RubyBuilder.new(rb_file, output_prefix)
|
37
|
+
end
|
38
|
+
|
32
39
|
def make_builder_doc(target_dir, output_prefix)
|
33
40
|
doc_file = File.join(target_dir, "#{output_prefix}.txt")
|
34
41
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module Cauterize
|
4
|
+
class RubyBuilder
|
5
|
+
attr_reader :rb
|
6
|
+
def initialize(rb_file, name="cauterize")
|
7
|
+
@rb = rb_file
|
8
|
+
@name = name.camel
|
9
|
+
end
|
10
|
+
|
11
|
+
def build
|
12
|
+
build_rb
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_rb
|
16
|
+
f = []
|
17
|
+
|
18
|
+
f << "# WARNING: This is generated code. Do not edit this file directly."
|
19
|
+
f << ""
|
20
|
+
f << "require_relative './cauterize_ruby_builtins'"
|
21
|
+
f << ""
|
22
|
+
|
23
|
+
instances = BaseType.all_instances
|
24
|
+
builders = instances.map {|i| Builders.get(:ruby, i)}
|
25
|
+
builders.each { |b| b.class_defn(f) }
|
26
|
+
f << ""
|
27
|
+
|
28
|
+
File.open(@rb, "wb") do |fh|
|
29
|
+
fh.write(f.join("\n").to_s)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/cauterize/version.rb
CHANGED
data/spec/builders_spec.rb
CHANGED
@@ -5,6 +5,7 @@ module Cauterize
|
|
5
5
|
before do
|
6
6
|
class X; def initialize(i); end; end
|
7
7
|
class Y; def initialize(i); end; end
|
8
|
+
class Z; def initialize(i); end; end
|
8
9
|
|
9
10
|
# save the old list so that we can restore it later.
|
10
11
|
old_builders = nil
|
@@ -34,6 +35,9 @@ module Cauterize
|
|
34
35
|
|
35
36
|
Cauterize::Builders.register(:cs, Cauterize::Scalar, Y)
|
36
37
|
Cauterize::Builders.get(:cs, s).class.should be Y
|
38
|
+
|
39
|
+
Cauterize::Builders.register(:ruby, Cauterize::Scalar, Z)
|
40
|
+
Cauterize::Builders.get(:ruby, s).class.should be Z
|
37
41
|
end
|
38
42
|
|
39
43
|
it "raises an error on duplicate registrations" do
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Cauterize
|
5
|
+
describe Cauterize::RubyBuilder do
|
6
|
+
before do
|
7
|
+
@tempdir = Dir.mktmpdir
|
8
|
+
@rb_path = File.join(@tempdir, "testing.rb")
|
9
|
+
|
10
|
+
@rb = RubyBuilder.new(@rb_path, "testing")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe :build do
|
14
|
+
before do
|
15
|
+
Cauterize.set_version("1.2.3")
|
16
|
+
|
17
|
+
Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
|
18
|
+
|
19
|
+
Cauterize.fixed_array(:mac_address) do |fa|
|
20
|
+
fa.array_type :small_uint
|
21
|
+
fa.array_size 6
|
22
|
+
end
|
23
|
+
|
24
|
+
Cauterize.variable_array(:mac_table) do |t|
|
25
|
+
t.array_type :mac_address
|
26
|
+
t.array_size 64
|
27
|
+
t.size_type :small_uint
|
28
|
+
end
|
29
|
+
|
30
|
+
Cauterize.variable_array(:name) do |va|
|
31
|
+
va.array_type :small_uint
|
32
|
+
va.size_type :small_uint
|
33
|
+
va.array_size 32
|
34
|
+
end
|
35
|
+
|
36
|
+
Cauterize.enumeration(:gender) do |e|
|
37
|
+
e.value :male
|
38
|
+
e.value :female
|
39
|
+
end
|
40
|
+
|
41
|
+
Cauterize.composite(:place) do |c|
|
42
|
+
c.field :name, :name
|
43
|
+
c.field :elevation, :uint32
|
44
|
+
end
|
45
|
+
|
46
|
+
Cauterize.composite(:person) do |c|
|
47
|
+
c.field :first_name, :name
|
48
|
+
c.field :last_name, :name
|
49
|
+
c.field :gender, :gender
|
50
|
+
end
|
51
|
+
|
52
|
+
Cauterize.composite(:dog) do |c|
|
53
|
+
c.field :name, :name
|
54
|
+
c.field :gender, :gender
|
55
|
+
c.field :leg_count, :small_uint
|
56
|
+
end
|
57
|
+
|
58
|
+
Cauterize.group(:creature) do |g|
|
59
|
+
g.field :person, :person
|
60
|
+
g.field :dog, :dog
|
61
|
+
end
|
62
|
+
|
63
|
+
@rb.build
|
64
|
+
@ruby_text = File.read(@rb.rb)
|
65
|
+
@ruby_lines = @ruby_text.lines.to_a
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "header generation" do
|
69
|
+
it "informs the user the code is generated" do
|
70
|
+
@ruby_text.should include("generated code. Do not edit")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "requires 'cauterize_ruby_baseclasses'" do
|
74
|
+
@ruby_text.should include("require_relative './cauterize_ruby_builtins'")
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/support/c/src/cauterize.h
CHANGED
@@ -17,7 +17,7 @@ typedef enum cauterize_status {
|
|
17
17
|
CA_ERR_NOT_ENOUGH_SPACE = 2,
|
18
18
|
CA_ERR_NOT_ENOUGH_DATA = 3,
|
19
19
|
CA_ERR_INVALID_LENGTH = 4,
|
20
|
-
|
20
|
+
CA_ERR_INVALID_TYPE_TAG = 5,
|
21
21
|
CA_ERR_INVALID_ENUM_VAL = 6,
|
22
22
|
CA_ERR_GENERAL = INT32_MAX,
|
23
23
|
} CAUTERIZE_STATUS_T;
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'stringio'
|
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
|
+
class CauterizeData
|
10
|
+
def self.construct(x)
|
11
|
+
if x.is_a? CauterizeData
|
12
|
+
raise "Invalid Type: was #{x.class}, expected #{self.name}" if not x.is_a?(self)
|
13
|
+
x
|
14
|
+
else
|
15
|
+
self.new x
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.unpack(x)
|
20
|
+
self.unpackio(StringIO.new(x))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class CauterizeBuiltin < CauterizeData
|
25
|
+
attr_reader :val
|
26
|
+
def initialize(val)
|
27
|
+
raise "Out of range value" if not in_range(val)
|
28
|
+
@val = val
|
29
|
+
end
|
30
|
+
def to_i
|
31
|
+
return @val.to_i
|
32
|
+
end
|
33
|
+
def to_f
|
34
|
+
return @val.to_f
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class CauterizeScalar < CauterizeData
|
39
|
+
attr_reader :val
|
40
|
+
|
41
|
+
def initialize(val)
|
42
|
+
@val = self.class.builtin.construct val
|
43
|
+
end
|
44
|
+
|
45
|
+
def pack
|
46
|
+
@val.pack
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.unpackio(str)
|
50
|
+
self.new self.builtin.unpackio(str)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
class CauterizeFixedArray < CauterizeData
|
56
|
+
attr_reader :elems
|
57
|
+
|
58
|
+
def initialize(elems)
|
59
|
+
@elems = elems.map { |e| self.class.elem_type.construct(e) }
|
60
|
+
raise "Invalid length" if @elems.length != self.class.length
|
61
|
+
end
|
62
|
+
|
63
|
+
def pack
|
64
|
+
@elems.inject("") { |sum, n| sum + n.pack }
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.unpackio(str)
|
68
|
+
self.new (1..self.length).map { self.elem_type.unpackio(str) }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
class CauterizeVariableArray < CauterizeData
|
74
|
+
attr_reader :length
|
75
|
+
attr_reader :elems
|
76
|
+
|
77
|
+
def initialize(elems)
|
78
|
+
@elems = elems.map { |e| self.class.elem_type.construct(e) }
|
79
|
+
@length = self.class.size_type.new @elems.length
|
80
|
+
raise "Invalid length" if @elems.length > self.class.max_length
|
81
|
+
end
|
82
|
+
|
83
|
+
def pack
|
84
|
+
@length.pack + @elems.inject("") { |sum, n| sum + n.pack }
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.unpackio(str)
|
88
|
+
length = self.size_type.unpackio(str)
|
89
|
+
self.new (1..length.val).map { self.elem_type.unpackio(str) }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
class CauterizeComposite < CauterizeData
|
95
|
+
attr_reader :fields
|
96
|
+
|
97
|
+
def initialize(field_values)
|
98
|
+
missing_keys = self.class.fields.keys - field_values.keys
|
99
|
+
extra_keys = field_values.keys - self.class.fields.keys
|
100
|
+
raise "missing fields #{missing_keys}" if not missing_keys.empty?
|
101
|
+
raise "extra fields #{extra_keys}" if not extra_keys.empty?
|
102
|
+
@fields = Hash[field_values.map do |field_name, value|
|
103
|
+
[field_name, self.class.fields[field_name].construct(value)]
|
104
|
+
end]
|
105
|
+
end
|
106
|
+
|
107
|
+
def pack
|
108
|
+
@fields.values.inject("") { |sum, v| sum + v.pack }
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.unpackio(str)
|
112
|
+
self.new Hash[self.fields.keys.map do |k|
|
113
|
+
[k, self.fields[k].unpackio(str)]
|
114
|
+
end]
|
115
|
+
end
|
116
|
+
|
117
|
+
alias orig_method_missing method_missing
|
118
|
+
|
119
|
+
def method_missing(m, *args, &block)
|
120
|
+
if fields[m]
|
121
|
+
fields[m]
|
122
|
+
else
|
123
|
+
orig_method_missing(m, *args, &block)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
class CauterizeEnumeration < CauterizeData
|
130
|
+
attr_reader :field_name
|
131
|
+
|
132
|
+
def initialize(field_name)
|
133
|
+
raise "Invalid field name #{field_name}" if not self.class.fields.keys.include?(field_name)
|
134
|
+
@field_name = field_name
|
135
|
+
end
|
136
|
+
|
137
|
+
def val() self.class.fields[@field_name] end
|
138
|
+
|
139
|
+
def pack
|
140
|
+
self.class.repr_type.construct(self.class.fields[@field_name]).pack
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.from_int(i)
|
144
|
+
raise "Invalid enumeration value #{i.to_i}" if not self.fields.values.include? i.to_i
|
145
|
+
self.new(self.fields.invert[i.to_i])
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.unpackio(str)
|
149
|
+
self.from_int(self.repr_type.unpackio(str))
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
class CauterizeGroup < CauterizeData
|
155
|
+
attr_reader :tag
|
156
|
+
attr_reader :data
|
157
|
+
|
158
|
+
def to_tag_name(field_name)
|
159
|
+
(self.class.tag_prefix + field_name.to_s).to_sym
|
160
|
+
end
|
161
|
+
|
162
|
+
def self.from_field_name(tag_name)
|
163
|
+
t = tag_name.to_s
|
164
|
+
t.slice!(self.tag_prefix)
|
165
|
+
t.to_sym
|
166
|
+
end
|
167
|
+
|
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)]
|
171
|
+
if field_class.nil?
|
172
|
+
@data = data
|
173
|
+
else
|
174
|
+
@data = field_class.construct(data)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def pack
|
179
|
+
if @data.nil?
|
180
|
+
@tag.pack
|
181
|
+
else
|
182
|
+
@tag.pack + @data.pack
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.unpackio(str)
|
187
|
+
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)
|
192
|
+
else
|
193
|
+
self.new(field_name, data.unpackio(str))
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require_relative './cauterize_ruby_baseclasses'
|
2
|
+
|
3
|
+
class UInt8 < CauterizeBuiltin
|
4
|
+
def in_range(v) v >= 0 && v < 2**8 end
|
5
|
+
def pack
|
6
|
+
[val.to_i].pack("C")
|
7
|
+
end
|
8
|
+
def self.unpackio(str)
|
9
|
+
UInt8.new str.read(1).unpack("C")[0]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class UInt16 < CauterizeBuiltin
|
14
|
+
def in_range(v) v >= 0 && v < 2**16 end
|
15
|
+
def pack
|
16
|
+
[val.to_i].pack("S")
|
17
|
+
end
|
18
|
+
def self.unpackio(str)
|
19
|
+
UInt16.new str.read(2).unpack("S")[0]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class UInt32 < CauterizeBuiltin
|
24
|
+
def in_range(v) v >= 0 && v < 2**32 end
|
25
|
+
def pack
|
26
|
+
[val.to_i].pack("L")
|
27
|
+
end
|
28
|
+
def self.unpackio(str)
|
29
|
+
UInt32.new str.read(4).unpack("L")[0]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class UInt64 < CauterizeBuiltin
|
34
|
+
def in_range(v) v >= 0 && v < 2**64 end
|
35
|
+
def pack
|
36
|
+
[val.to_i].pack("Q")
|
37
|
+
end
|
38
|
+
def self.unpackio(str)
|
39
|
+
UInt64.new str.read(8).unpack("Q")[0]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Int8 < CauterizeBuiltin
|
44
|
+
def in_range(v) (v >= -2**7) && (v < 2**7) end
|
45
|
+
def pack
|
46
|
+
[val.to_i].pack("c")
|
47
|
+
end
|
48
|
+
def self.unpackio(str)
|
49
|
+
Int8.new str.read(1).unpack("c")[0]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Int16 < CauterizeBuiltin
|
54
|
+
def in_range(v) (v >= -2**15) && (v < 2**15) end
|
55
|
+
def pack
|
56
|
+
[val.to_i].pack("s")
|
57
|
+
end
|
58
|
+
def self.unpackio(str)
|
59
|
+
Int16.new str.read(2).unpack("s")[0]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Int32 < CauterizeBuiltin
|
64
|
+
def in_range(v) (v >= -2**31) && (v < 2**31) end
|
65
|
+
def pack
|
66
|
+
[val.to_i].pack("l")
|
67
|
+
end
|
68
|
+
def self.unpackio(str)
|
69
|
+
Int32.new str.read(4).unpack("l")[0]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Int64 < CauterizeBuiltin
|
74
|
+
def in_range(v) (v >= -2**63) && (v < 2**63) end
|
75
|
+
def pack
|
76
|
+
[val.to_i].pack("q")
|
77
|
+
end
|
78
|
+
def self.unpackio(str)
|
79
|
+
Int64.new str.read(8).unpack("q")[0]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Bool < CauterizeBuiltin
|
84
|
+
def in_range(v) true end
|
85
|
+
def pack
|
86
|
+
if @val
|
87
|
+
[1].pack("C")
|
88
|
+
else
|
89
|
+
[0].pack("C")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
def self.unpackio(str)
|
93
|
+
if str.read(1).unpack("C")[0] == 0
|
94
|
+
Bool.new false
|
95
|
+
else
|
96
|
+
Bool.new true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class Float32 < CauterizeBuiltin
|
102
|
+
def in_range(v) v > (-3.402823466e38) && v < (3.402823466e38) end
|
103
|
+
def pack
|
104
|
+
[val.to_f].pack("f")
|
105
|
+
end
|
106
|
+
def self.unpackio(str)
|
107
|
+
Float32.new str.read(4).unpack("f")[0]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
class Float64 < CauterizeBuiltin
|
112
|
+
def in_range(v) true end
|
113
|
+
def pack
|
114
|
+
[val.to_f].pack("d")
|
115
|
+
end
|
116
|
+
def self.unpackio(str)
|
117
|
+
Float64.new str.read(8).unpack("d")[0]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
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.
|
4
|
+
version: 0.0.1.pre9
|
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-
|
12
|
+
date: 2013-07-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -155,6 +155,14 @@ files:
|
|
155
155
|
- lib/cauterize/builders/doc/group.rb
|
156
156
|
- lib/cauterize/builders/doc/scalar.rb
|
157
157
|
- lib/cauterize/builders/doc/variable_array.rb
|
158
|
+
- lib/cauterize/builders/ruby/buildable.rb
|
159
|
+
- lib/cauterize/builders/ruby/builtin.rb
|
160
|
+
- lib/cauterize/builders/ruby/composite.rb
|
161
|
+
- lib/cauterize/builders/ruby/enumeration.rb
|
162
|
+
- lib/cauterize/builders/ruby/fixed_array.rb
|
163
|
+
- lib/cauterize/builders/ruby/group.rb
|
164
|
+
- lib/cauterize/builders/ruby/scalar.rb
|
165
|
+
- lib/cauterize/builders/ruby/variable_array.rb
|
158
166
|
- lib/cauterize/builtin.rb
|
159
167
|
- lib/cauterize/c_builder.rb
|
160
168
|
- lib/cauterize/cauterize.rb
|
@@ -165,6 +173,7 @@ files:
|
|
165
173
|
- lib/cauterize/fixed_array.rb
|
166
174
|
- lib/cauterize/formatter.rb
|
167
175
|
- lib/cauterize/group.rb
|
176
|
+
- lib/cauterize/ruby_builder.rb
|
168
177
|
- lib/cauterize/scalar.rb
|
169
178
|
- lib/cauterize/snake_case.rb
|
170
179
|
- lib/cauterize/variable_array.rb
|
@@ -196,6 +205,7 @@ files:
|
|
196
205
|
- spec/enumeration_spec.rb
|
197
206
|
- spec/fixed_array_spec.rb
|
198
207
|
- spec/group_spec.rb
|
208
|
+
- spec/ruby_builder_spec.rb
|
199
209
|
- spec/scalar_spec.rb
|
200
210
|
- spec/spec_helper.rb
|
201
211
|
- spec/support/shared_examples_for_array_buildables.rb
|
@@ -236,6 +246,8 @@ files:
|
|
236
246
|
- support/cs/test/CauterizeVariableArrayFormatterTest.cs
|
237
247
|
- support/cs/test/OrderAttributeTest.cs
|
238
248
|
- support/cs/test/SerializedRepresentationAttributeTest.cs
|
249
|
+
- support/ruby/src/cauterize_ruby_baseclasses.rb
|
250
|
+
- support/ruby/src/cauterize_ruby_builtins.rb
|
239
251
|
homepage: https://github.com/sw17ch/cauterize
|
240
252
|
licenses: []
|
241
253
|
post_install_message:
|
@@ -250,7 +262,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
250
262
|
version: '0'
|
251
263
|
segments:
|
252
264
|
- 0
|
253
|
-
hash:
|
265
|
+
hash: -966914952996441061
|
254
266
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
255
267
|
none: false
|
256
268
|
requirements:
|
@@ -291,6 +303,7 @@ test_files:
|
|
291
303
|
- spec/enumeration_spec.rb
|
292
304
|
- spec/fixed_array_spec.rb
|
293
305
|
- spec/group_spec.rb
|
306
|
+
- spec/ruby_builder_spec.rb
|
294
307
|
- spec/scalar_spec.rb
|
295
308
|
- spec/spec_helper.rb
|
296
309
|
- spec/support/shared_examples_for_array_buildables.rb
|