cauterize 0.0.1.pre11 → 0.0.1.pre12
Sign up to get free protection for your applications and to get access to all the features.
- data/example/Cauterize +6 -0
- data/example/build.sh +8 -5
- data/example/c_example_support/empty_main.c +1 -0
- data/example/c_example_support/example_project_config.c +11 -0
- data/example/c_example_support/example_project_config.h +15 -0
- data/lib/cauterize/builders/c/buildable.rb +26 -2
- data/lib/cauterize/builders/c/builtin.rb +2 -8
- data/lib/cauterize/builders/c/composite.rb +10 -16
- data/lib/cauterize/builders/c/enumeration.rb +15 -21
- data/lib/cauterize/builders/c/fixed_array.rb +20 -26
- data/lib/cauterize/builders/c/group.rb +34 -39
- data/lib/cauterize/builders/c/scalar.rb +2 -8
- data/lib/cauterize/builders/c/variable_array.rb +32 -38
- data/lib/cauterize/c_builder.rb +12 -2
- data/lib/cauterize/formatter.rb +7 -3
- data/lib/cauterize/version.rb +1 -1
- data/spec/support/shared_examples_for_c_buildables.rb +14 -6
- data/support/c/src/cauterize.c +1 -1
- data/support/c/src/cauterize.h +4 -4
- data/support/c/src/cauterize_util.h +0 -41
- data/support/ruby/src/cauterize_ruby_baseclasses.rb +18 -20
- data/support/ruby/src/cauterize_ruby_builtins.rb +10 -10
- metadata +5 -2
data/example/Cauterize
CHANGED
@@ -4,6 +4,12 @@ set_version("1.2.3")
|
|
4
4
|
scalar(:bigint) { |t| t.type_name(:int32) }
|
5
5
|
scalar(:usmallint) { |t| t.type_name(:uint8) }
|
6
6
|
|
7
|
+
variable_array(:uint8_buffer) do |a|
|
8
|
+
a.size_type :uint8
|
9
|
+
a.array_type :uint8
|
10
|
+
a.array_size 128
|
11
|
+
end
|
12
|
+
|
7
13
|
enumeration(:color) do |e|
|
8
14
|
e.value :red
|
9
15
|
e.value :blue
|
data/example/build.sh
CHANGED
@@ -7,11 +7,14 @@ rm -rf c_cauterize_output
|
|
7
7
|
../bin/cauterize generate c c_cauterize_output
|
8
8
|
|
9
9
|
CFLAGS="-Wall -Werror -Wextra"
|
10
|
-
INCLUDES="-Ic_cauterize_output"
|
11
|
-
|
12
|
-
|
13
|
-
gcc $CFLAGS $INCLUDES
|
14
|
-
|
10
|
+
INCLUDES="-Ic_cauterize_output -Ic_example_support"
|
11
|
+
DEFINES="-DUSE_CAUTERIZE_CONFIG_HEADER"
|
12
|
+
|
13
|
+
gcc $CFLAGS $INCLUDES $DEFINES c_example_support/example_project_config.c \
|
14
|
+
c_example_support/empty_main.c \
|
15
|
+
c_cauterize_output/cauterize.c \
|
16
|
+
c_cauterize_output/example_project.c \
|
17
|
+
-o c_cauterize_output/example_project
|
15
18
|
|
16
19
|
rm -rf cs_cauterize_output
|
17
20
|
../bin/cauterize generate cs cs_cauterize_output
|
@@ -0,0 +1 @@
|
|
1
|
+
int main(int argc, char * argv[]) { (void)argc; (void)argv; return 0; }
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#include "example_project_config.h"
|
2
|
+
|
3
|
+
CAUTERIZE_STATUS_T spec_Pack_uint8_buffer(struct Cauterize * dst, struct uint8_buffer * src)
|
4
|
+
{
|
5
|
+
CAUTERIZE_STATUS_T err;
|
6
|
+
|
7
|
+
if (src->length > ARRAY_SIZE(src->data)) { return CA_ERR_INVALID_LENGTH; }
|
8
|
+
if (CA_OK != (err = Pack_uint8(dst, &src->length))) { return err; }
|
9
|
+
|
10
|
+
return CauterizeAppend(dst, src->data, src->length * sizeof(src->data[0]));
|
11
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef EXAMPLE_PROJECT_CONFIG_H
|
2
|
+
#define EXAMPLE_PROJECT_CONFIG_H
|
3
|
+
|
4
|
+
#include "example_project.h"
|
5
|
+
|
6
|
+
/* We're going to define a specialized version of Pack_uint8_buffer which packs
|
7
|
+
* the entire buffer at once. We know this is okay since we're guaranteed not
|
8
|
+
* to have any padding between the bytes in the array. */
|
9
|
+
#define SPECIALIZED_Pack_uint8_buffer spec_Pack_uint8_buffer
|
10
|
+
|
11
|
+
CAUTERIZE_STATUS_T spec_Pack_uint8_buffer(
|
12
|
+
struct Cauterize * dst,
|
13
|
+
struct uint8_buffer * src);
|
14
|
+
|
15
|
+
#endif /* EXAMPLE_PROJECT_CONFIG_H */
|
@@ -33,18 +33,42 @@ module Cauterize
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
# wrapped_(un)packer_defn allow us to wrap every definition in some text #
|
37
|
+
##########################################################################
|
38
|
+
def wrapped_packer_defn(formatter)
|
39
|
+
formatter << packer_sig
|
40
|
+
formatter.braces do
|
41
|
+
formatter.backdent("#ifdef SPECIALIZED_#{packer_sym}")
|
42
|
+
formatter << "return SPECIALIZED_#{packer_sym}(dst, src);"
|
43
|
+
formatter.backdent("#else")
|
44
|
+
packer_defn(formatter)
|
45
|
+
formatter.backdent("#endif")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def wrapped_unpacker_defn(formatter)
|
50
|
+
formatter << unpacker_sig
|
51
|
+
formatter.braces do
|
52
|
+
formatter.backdent("#ifdef SPECIALIZED_#{unpacker_sym}")
|
53
|
+
formatter << "return SPECIALIZED_#{unpacker_sym}(src, dst);"
|
54
|
+
formatter.backdent("#else")
|
55
|
+
unpacker_defn(formatter)
|
56
|
+
formatter.backdent("#endif")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
36
60
|
# Things below here are tested in shared_examples_for_c_buildables #
|
37
61
|
####################################################################
|
38
62
|
|
39
63
|
# Methods that are pretty much the same for everyone.
|
40
64
|
def packer_sym; "Pack_#{@blueprint.name}" end
|
41
|
-
def packer_sig; "
|
65
|
+
def packer_sig; "CAUTERIZE_STATUS_T #{packer_sym}(struct Cauterize * dst, #{render} * src)" end
|
42
66
|
def packer_proto(formatter)
|
43
67
|
formatter << packer_sig + ";"
|
44
68
|
end
|
45
69
|
|
46
70
|
def unpacker_sym; "Unpack_#{@blueprint.name}" end
|
47
|
-
def unpacker_sig; "
|
71
|
+
def unpacker_sig; "CAUTERIZE_STATUS_T #{unpacker_sym}(struct Cauterize * src, #{render} * dst)" end
|
48
72
|
def unpacker_proto(formatter)
|
49
73
|
formatter << unpacker_sig + ";"
|
50
74
|
end
|
@@ -19,17 +19,11 @@ module Cauterize
|
|
19
19
|
|
20
20
|
# These are identical to the Scalar definitions. For now.
|
21
21
|
def packer_defn(formatter)
|
22
|
-
formatter <<
|
23
|
-
formatter.braces do
|
24
|
-
formatter << "return CauterizeAppend(dst, (uint8_t*)src, sizeof(*src));"
|
25
|
-
end
|
22
|
+
formatter << "return CauterizeAppend(dst, (uint8_t*)src, sizeof(*src));"
|
26
23
|
end
|
27
24
|
|
28
25
|
def unpacker_defn(formatter)
|
29
|
-
formatter <<
|
30
|
-
formatter.braces do
|
31
|
-
formatter << "return CauterizeRead(src, (uint8_t*)dst, sizeof(*dst));"
|
32
|
-
end
|
26
|
+
formatter << "return CauterizeRead(src, (uint8_t*)dst, sizeof(*dst));"
|
33
27
|
end
|
34
28
|
|
35
29
|
private
|
@@ -11,27 +11,21 @@ module Cauterize
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def packer_defn(formatter)
|
14
|
-
formatter <<
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
p_sym = Builders.get(:c, field.type).packer_sym
|
19
|
-
formatter << "if (CA_OK != (err = #{p_sym}(dst, &src->#{field.name}))) { return err; }"
|
20
|
-
end
|
21
|
-
formatter << "return CA_OK;"
|
14
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
15
|
+
@blueprint.fields.values.each do |field|
|
16
|
+
p_sym = Builders.get(:c, field.type).packer_sym
|
17
|
+
formatter << "if (CA_OK != (err = #{p_sym}(dst, &src->#{field.name}))) { return err; }"
|
22
18
|
end
|
19
|
+
formatter << "return CA_OK;"
|
23
20
|
end
|
24
21
|
|
25
22
|
def unpacker_defn(formatter)
|
26
|
-
formatter <<
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
u_sym = Builders.get(:c, field.type).unpacker_sym
|
31
|
-
formatter << "if (CA_OK != (err = #{u_sym}(src, &dst->#{field.name}))) { return err; }"
|
32
|
-
end
|
33
|
-
formatter << "return CA_OK;"
|
23
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
24
|
+
@blueprint.fields.values.each do |field|
|
25
|
+
u_sym = Builders.get(:c, field.type).unpacker_sym
|
26
|
+
formatter << "if (CA_OK != (err = #{u_sym}(src, &dst->#{field.name}))) { return err; }"
|
34
27
|
end
|
28
|
+
formatter << "return CA_OK;"
|
35
29
|
end
|
36
30
|
|
37
31
|
def struct_proto(formatter)
|
@@ -13,35 +13,29 @@ module Cauterize
|
|
13
13
|
def packer_defn(formatter)
|
14
14
|
rep_builder = Builders.get(:c, @blueprint.representation)
|
15
15
|
|
16
|
-
|
17
|
-
formatter
|
18
|
-
rep = "enum_representation"
|
19
|
-
rep_builder.declare(formatter, rep)
|
16
|
+
rep = "enum_representation"
|
17
|
+
rep_builder.declare(formatter, rep)
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
19
|
+
formatter << "#{rep} = (#{rep_builder.render})(*src);"
|
20
|
+
formatter << "return #{rep_builder.packer_sym}(dst, &#{rep});"
|
24
21
|
end
|
25
22
|
|
26
23
|
def unpacker_defn(formatter)
|
27
24
|
rep_builder = Builders.get(:c, @blueprint.representation)
|
28
25
|
|
29
|
-
|
30
|
-
formatter
|
31
|
-
rep = "enum_representation"
|
32
|
-
rep_builder.declare(formatter, rep)
|
26
|
+
rep = "enum_representation"
|
27
|
+
rep_builder.declare(formatter, rep)
|
33
28
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
formatter << "CAUTERIZE_STATUS_T s = #{rep_builder.unpacker_sym}(src, &#{rep});"
|
30
|
+
formatter << "if (CA_OK != s)"
|
31
|
+
formatter.braces do
|
32
|
+
formatter << "return s;"
|
33
|
+
end
|
39
34
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
35
|
+
formatter << "else"
|
36
|
+
formatter.braces do
|
37
|
+
formatter << "*dst = (#{render})#{rep};"
|
38
|
+
formatter << "return CA_OK;"
|
45
39
|
end
|
46
40
|
end
|
47
41
|
|
@@ -11,39 +11,33 @@ module Cauterize
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def packer_defn(formatter)
|
14
|
-
formatter <<
|
15
|
-
formatter
|
16
|
-
|
17
|
-
formatter << "size_t i;"
|
18
|
-
formatter.blank_line
|
19
|
-
|
20
|
-
# store each used item in the array
|
21
|
-
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
22
|
-
formatter.braces do
|
23
|
-
formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src->data[i]))) { return err; }"
|
24
|
-
end
|
25
|
-
formatter.blank_line
|
14
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
15
|
+
formatter << "size_t i;"
|
16
|
+
formatter.blank_line
|
26
17
|
|
27
|
-
|
18
|
+
# store each used item in the array
|
19
|
+
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
20
|
+
formatter.braces do
|
21
|
+
formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src->data[i]))) { return err; }"
|
28
22
|
end
|
23
|
+
formatter.blank_line
|
24
|
+
|
25
|
+
formatter << "return CA_OK;"
|
29
26
|
end
|
30
27
|
|
31
28
|
def unpacker_defn(formatter)
|
32
|
-
formatter <<
|
33
|
-
formatter
|
34
|
-
|
35
|
-
formatter << "size_t i;"
|
36
|
-
formatter.blank_line
|
37
|
-
|
38
|
-
# store each used item in the array
|
39
|
-
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
40
|
-
formatter.braces do
|
41
|
-
formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst->data[i]))) { return err; }"
|
42
|
-
end
|
43
|
-
formatter.blank_line
|
29
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
30
|
+
formatter << "size_t i;"
|
31
|
+
formatter.blank_line
|
44
32
|
|
45
|
-
|
33
|
+
# store each used item in the array
|
34
|
+
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
35
|
+
formatter.braces do
|
36
|
+
formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst->data[i]))) { return err; }"
|
46
37
|
end
|
38
|
+
formatter.blank_line
|
39
|
+
|
40
|
+
formatter << "return CA_OK;"
|
47
41
|
end
|
48
42
|
|
49
43
|
def struct_proto(formatter)
|
@@ -17,60 +17,55 @@ module Cauterize
|
|
17
17
|
|
18
18
|
def packer_defn(formatter)
|
19
19
|
enum_builder = Builders.get(:c, @tag_enum)
|
20
|
-
formatter <<
|
21
|
-
formatter.braces do
|
22
|
-
formatter << "CAUTERIZE_STATUS_T err;"
|
23
|
-
|
24
|
-
# pack the tag
|
25
|
-
formatter << "if (CA_OK != (err = #{enum_builder.packer_sym}(dst, &src->tag))) { return err; }"
|
26
|
-
|
27
|
-
# pack the fields
|
28
|
-
formatter << "switch (src->tag)"
|
29
|
-
formatter.braces do
|
30
|
-
having_data.each do |field|
|
31
|
-
bldr = Builders.get(:c, field.type)
|
32
|
-
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
33
|
-
formatter << "if (CA_OK != (err = #{bldr.packer_sym}(dst, &src->data.#{field.name}))) { return err; }"
|
34
|
-
formatter << "break;"
|
35
|
-
end
|
20
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
36
21
|
|
37
|
-
|
22
|
+
# pack the tag
|
23
|
+
formatter << "if (CA_OK != (err = #{enum_builder.packer_sym}(dst, &src->tag))) { return err; }"
|
38
24
|
|
39
|
-
|
40
|
-
|
25
|
+
# pack the fields
|
26
|
+
formatter << "switch (src->tag)"
|
27
|
+
formatter.braces do
|
28
|
+
having_data.each do |field|
|
29
|
+
bldr = Builders.get(:c, field.type)
|
30
|
+
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
31
|
+
formatter << "if (CA_OK != (err = #{bldr.packer_sym}(dst, &src->data.#{field.name}))) { return err; }"
|
41
32
|
formatter << "break;"
|
42
33
|
end
|
43
|
-
|
34
|
+
|
35
|
+
format_no_data_stubs(formatter)
|
36
|
+
|
37
|
+
formatter.backdent "default:"
|
38
|
+
formatter << "return CA_ERR_INVALID_TYPE_TAG;"
|
39
|
+
formatter << "break;"
|
44
40
|
end
|
41
|
+
formatter << "return CA_OK;"
|
45
42
|
end
|
46
43
|
|
47
44
|
def unpacker_defn(formatter)
|
48
45
|
enum_builder = Builders.get(:c, @tag_enum)
|
49
|
-
formatter << unpacker_sig
|
50
|
-
formatter.braces do
|
51
|
-
formatter << "CAUTERIZE_STATUS_T err;"
|
52
46
|
|
53
|
-
|
54
|
-
formatter << "if (CA_OK != (err = #{enum_builder.unpacker_sym}(src, &dst->tag))) { return err; }"
|
47
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
55
48
|
|
56
|
-
|
57
|
-
|
58
|
-
formatter.braces do
|
59
|
-
having_data.each do |field|
|
60
|
-
bldr = Builders.get(:c, field.type)
|
61
|
-
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
62
|
-
formatter << "if (CA_OK != (err = #{bldr.unpacker_sym}(src, &dst->data.#{field.name}))) { return err; }"
|
63
|
-
formatter << "break;"
|
64
|
-
end
|
49
|
+
# unpack the tag
|
50
|
+
formatter << "if (CA_OK != (err = #{enum_builder.unpacker_sym}(src, &dst->tag))) { return err; }"
|
65
51
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
52
|
+
# pack the fields
|
53
|
+
formatter << "switch (dst->tag)"
|
54
|
+
formatter.braces do
|
55
|
+
having_data.each do |field|
|
56
|
+
bldr = Builders.get(:c, field.type)
|
57
|
+
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
58
|
+
formatter << "if (CA_OK != (err = #{bldr.unpacker_sym}(src, &dst->data.#{field.name}))) { return err; }"
|
70
59
|
formatter << "break;"
|
71
60
|
end
|
72
|
-
|
61
|
+
|
62
|
+
format_no_data_stubs(formatter)
|
63
|
+
|
64
|
+
formatter.backdent "default:"
|
65
|
+
formatter << "return CA_ERR_INVALID_TYPE_TAG;"
|
66
|
+
formatter << "break;"
|
73
67
|
end
|
68
|
+
formatter << "return CA_OK;"
|
74
69
|
end
|
75
70
|
|
76
71
|
def struct_proto(formatter)
|
@@ -16,17 +16,11 @@ module Cauterize
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def packer_defn(formatter)
|
19
|
-
formatter <<
|
20
|
-
formatter.braces do
|
21
|
-
formatter << "return CauterizeAppend(dst, (uint8_t*)src, sizeof(*src));"
|
22
|
-
end
|
19
|
+
formatter << "return CauterizeAppend(dst, (uint8_t*)src, sizeof(*src));"
|
23
20
|
end
|
24
21
|
|
25
22
|
def unpacker_defn(formatter)
|
26
|
-
formatter <<
|
27
|
-
formatter.braces do
|
28
|
-
formatter << "return CauterizeRead(src, (uint8_t*)dst, sizeof(*dst));"
|
29
|
-
end
|
23
|
+
formatter << "return CauterizeRead(src, (uint8_t*)dst, sizeof(*dst));"
|
30
24
|
end
|
31
25
|
end
|
32
26
|
end
|
@@ -14,57 +14,51 @@ module Cauterize
|
|
14
14
|
size_type_builder = Builders.get(:c, @blueprint.size_type)
|
15
15
|
array_type_builder = Builders.get(:c, @blueprint.array_type)
|
16
16
|
|
17
|
-
formatter <<
|
17
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
18
|
+
formatter << "size_t i;"
|
19
|
+
formatter.blank_line
|
20
|
+
# check the length
|
21
|
+
formatter << "if (src->length > ARRAY_SIZE(src->data)) { return CA_ERR_INVALID_LENGTH; }"
|
22
|
+
|
23
|
+
# store the length
|
24
|
+
formatter << "if (CA_OK != (err = #{size_type_builder.packer_sym}(dst, &src->length))) { return err; }"
|
25
|
+
formatter.blank_line
|
26
|
+
|
27
|
+
# store each used item in the array
|
28
|
+
formatter << "for (i = 0; i < src->length; i++)"
|
18
29
|
formatter.braces do
|
19
|
-
formatter << "
|
20
|
-
formatter << "size_t i;"
|
21
|
-
formatter.blank_line
|
22
|
-
# check the length
|
23
|
-
formatter << "if (src->length > ARRAY_SIZE(src->data)) { return CA_ERR_INVALID_LENGTH; }"
|
24
|
-
|
25
|
-
# store the length
|
26
|
-
formatter << "if (CA_OK != (err = #{size_type_builder.packer_sym}(dst, &src->length))) { return err; }"
|
27
|
-
formatter.blank_line
|
28
|
-
|
29
|
-
# store each used item in the array
|
30
|
-
formatter << "for (i = 0; i < src->length; i++)"
|
31
|
-
formatter.braces do
|
32
|
-
formatter << "if (CA_OK != (err = #{array_type_builder.packer_sym}(dst, &src->data[i]))) { return err; }"
|
33
|
-
end
|
34
|
-
formatter.blank_line
|
35
|
-
|
36
|
-
formatter << "return CA_OK;"
|
30
|
+
formatter << "if (CA_OK != (err = #{array_type_builder.packer_sym}(dst, &src->data[i]))) { return err; }"
|
37
31
|
end
|
32
|
+
formatter.blank_line
|
33
|
+
|
34
|
+
formatter << "return CA_OK;"
|
38
35
|
end
|
39
36
|
|
40
37
|
def unpacker_defn(formatter)
|
41
38
|
size_type_builder = Builders.get(:c, @blueprint.size_type)
|
42
39
|
array_type_builder = Builders.get(:c, @blueprint.array_type)
|
43
40
|
|
44
|
-
formatter <<
|
45
|
-
formatter
|
46
|
-
|
47
|
-
formatter << "size_t i;"
|
48
|
-
formatter.blank_line
|
49
|
-
|
50
|
-
# read the length
|
51
|
-
formatter << "if (CA_OK != (err = #{size_type_builder.unpacker_sym}(src, &dst->length))) { return err; }"
|
52
|
-
formatter.blank_line
|
41
|
+
formatter << "CAUTERIZE_STATUS_T err;"
|
42
|
+
formatter << "size_t i;"
|
43
|
+
formatter.blank_line
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
45
|
+
# read the length
|
46
|
+
formatter << "if (CA_OK != (err = #{size_type_builder.unpacker_sym}(src, &dst->length))) { return err; }"
|
47
|
+
formatter.blank_line
|
57
48
|
|
49
|
+
# check the length
|
50
|
+
formatter << "if (dst->length > ARRAY_SIZE(dst->data)) { return CA_ERR_INVALID_LENGTH; }"
|
51
|
+
formatter.blank_line
|
58
52
|
|
59
|
-
# store each used item in the array
|
60
|
-
formatter << "for (i = 0; i < dst->length; i++)"
|
61
|
-
formatter.braces do
|
62
|
-
formatter << "if (CA_OK != (err = #{array_type_builder.unpacker_sym}(src, &dst->data[i]))) { return err; }"
|
63
|
-
end
|
64
|
-
formatter.blank_line
|
65
53
|
|
66
|
-
|
54
|
+
# store each used item in the array
|
55
|
+
formatter << "for (i = 0; i < dst->length; i++)"
|
56
|
+
formatter.braces do
|
57
|
+
formatter << "if (CA_OK != (err = #{array_type_builder.unpacker_sym}(src, &dst->data[i]))) { return err; }"
|
67
58
|
end
|
59
|
+
formatter.blank_line
|
60
|
+
|
61
|
+
formatter << "return CA_OK;"
|
68
62
|
end
|
69
63
|
|
70
64
|
def struct_proto(formatter)
|
data/lib/cauterize/c_builder.rb
CHANGED
@@ -72,12 +72,22 @@ module Cauterize
|
|
72
72
|
f << ""
|
73
73
|
f << %Q{#include <cauterize_util.h>}
|
74
74
|
f << %Q{#include "#{@name}.h"}
|
75
|
+
f.blank_line
|
76
|
+
|
77
|
+
f << %Q{/* Some extra configuration information may be provided. This is}
|
78
|
+
f << %Q{ * a good place for the user to put prototypes or defines used}
|
79
|
+
f << %Q{ * elsewhere. This is a user defined file and should be in the}
|
80
|
+
f << %Q{ * include search path. */}
|
81
|
+
f << %Q{#ifdef USE_CAUTERIZE_CONFIG_HEADER}
|
82
|
+
f << %Q{#include "#{@name}_config.h"}
|
83
|
+
f << %Q{#endif}
|
84
|
+
f.blank_line
|
75
85
|
|
76
86
|
instances = BaseType.all_instances
|
77
87
|
builders = instances.map {|i| Builders.get(:c, i)}
|
78
88
|
|
79
|
-
builders.each { |b| b.
|
80
|
-
builders.each { |b| b.
|
89
|
+
builders.each { |b| b.wrapped_packer_defn(f); f.blank_line }
|
90
|
+
builders.each { |b| b.wrapped_unpacker_defn(f); f.blank_line }
|
81
91
|
|
82
92
|
File.open(@c, "wb") do |fh|
|
83
93
|
fh.write(f.to_s)
|
data/lib/cauterize/formatter.rb
CHANGED
@@ -23,9 +23,13 @@ class Formatter
|
|
23
23
|
|
24
24
|
# indent back one level
|
25
25
|
def backdent(line)
|
26
|
-
@indent_level
|
27
|
-
|
28
|
-
|
26
|
+
if 0 == @indent_level
|
27
|
+
@lines << line
|
28
|
+
else
|
29
|
+
@indent_level -= 1
|
30
|
+
@lines << indent(line)
|
31
|
+
@indent_level += 1
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
35
|
def append(text)
|
data/lib/cauterize/version.rb
CHANGED
@@ -30,7 +30,7 @@ module Cauterize
|
|
30
30
|
|
31
31
|
describe :packer_sig do
|
32
32
|
it "looks like this" do
|
33
|
-
r = /
|
33
|
+
r = /CAUTERIZE_STATUS_T (?<sym>[^\(]+)\(struct Cauterize \* dst, (?<rend>(?:(?:struct|enum) )?[^ ]+) \* src\)/
|
34
34
|
subject.packer_sig.should match r
|
35
35
|
m = subject.packer_sig.match(r)
|
36
36
|
m[:sym].should == subject.packer_sym
|
@@ -45,12 +45,16 @@ module Cauterize
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
describe :
|
49
|
-
before { subject.
|
48
|
+
describe :wrapped_packer_defn do
|
49
|
+
before { subject.wrapped_packer_defn(formatter) }
|
50
50
|
|
51
51
|
it "contains the signature" do
|
52
52
|
formatter.to_s.should match Regexp.escape(subject.packer_sig)
|
53
53
|
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe :packer_defn do
|
57
|
+
before { subject.packer_defn(formatter) }
|
54
58
|
|
55
59
|
it "contains a return statement" do
|
56
60
|
formatter.to_s.should match /return.*;/
|
@@ -65,7 +69,7 @@ module Cauterize
|
|
65
69
|
|
66
70
|
describe :unpacker_sig do
|
67
71
|
it "looks like this" do
|
68
|
-
r = /
|
72
|
+
r = /CAUTERIZE_STATUS_T (?<sym>[^\(]+)\(struct Cauterize \* src, (?<rend>(?:(?:struct|enum) )?[^ ]+) \* dst\)/
|
69
73
|
subject.unpacker_sig.should match r
|
70
74
|
m = subject.unpacker_sig.match(r)
|
71
75
|
m[:sym].should == subject.unpacker_sym
|
@@ -80,12 +84,16 @@ module Cauterize
|
|
80
84
|
end
|
81
85
|
end
|
82
86
|
|
83
|
-
describe :
|
84
|
-
before { subject.
|
87
|
+
describe :wrapped_unpacker_defn do
|
88
|
+
before { subject.wrapped_unpacker_defn(formatter) }
|
85
89
|
|
86
90
|
it "contains the signature" do
|
87
91
|
formatter.to_s.should match Regexp.escape(subject.unpacker_sig)
|
88
92
|
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe :unpacker_defn do
|
96
|
+
before { subject.unpacker_defn(formatter) }
|
89
97
|
|
90
98
|
it "contains a return statement" do
|
91
99
|
formatter.to_s.should match /return.*;/
|
data/support/c/src/cauterize.c
CHANGED
data/support/c/src/cauterize.h
CHANGED
@@ -33,22 +33,22 @@ struct Cauterize {
|
|
33
33
|
extern "C" {
|
34
34
|
#endif
|
35
35
|
|
36
|
-
|
36
|
+
CAUTERIZE_STATUS_T CauterizeInitAppend(
|
37
37
|
struct Cauterize * m,
|
38
38
|
uint8_t * buffer,
|
39
39
|
uint32_t length);
|
40
40
|
|
41
|
-
|
41
|
+
CAUTERIZE_STATUS_T CauterizeInitRead(
|
42
42
|
struct Cauterize * m,
|
43
43
|
uint8_t * buffer,
|
44
44
|
uint32_t used);
|
45
45
|
|
46
|
-
|
46
|
+
CAUTERIZE_STATUS_T CauterizeAppend(
|
47
47
|
struct Cauterize * m,
|
48
48
|
uint8_t * src,
|
49
49
|
uint32_t length);
|
50
50
|
|
51
|
-
|
51
|
+
CAUTERIZE_STATUS_T CauterizeRead(
|
52
52
|
struct Cauterize * m,
|
53
53
|
uint8_t * dst,
|
54
54
|
uint32_t length);
|
@@ -5,45 +5,4 @@
|
|
5
5
|
#define CA_MIN(a, b) (((a) < (b)) ? (a) : (b))
|
6
6
|
#define CA_MAX(a,b) ((a) > (b) ? (a) : (b))
|
7
7
|
|
8
|
-
/* Buidling Cauterize files as a DLL. */
|
9
|
-
|
10
|
-
#if defined(BUILDING_DLL) || defined(USING_DLL)
|
11
|
-
#if !defined(_WIN32)
|
12
|
-
#error "Can only build DLL in Windows."
|
13
|
-
#endif
|
14
|
-
|
15
|
-
#define CALLCONV __stdcall
|
16
|
-
#endif
|
17
|
-
|
18
|
-
#if defined(BUILDING_DLL)
|
19
|
-
#define DLLDECL __declspec(dllexport)
|
20
|
-
#elif defined(USING_DLL)
|
21
|
-
#define DLLDECL __declspec(dllimport)
|
22
|
-
#endif
|
23
|
-
|
24
|
-
#ifndef DLLDECL
|
25
|
-
#define DLLDECL
|
26
|
-
#endif
|
27
|
-
|
28
|
-
#ifndef CALLCONV
|
29
|
-
#define CALLCONV
|
30
|
-
#endif
|
31
|
-
|
32
|
-
/* Buidling Cauterize files as a DLL. */
|
33
|
-
|
34
|
-
#if defined(BUILDING_DLL)
|
35
|
-
#define DLLDECL __declspec(dllexport)
|
36
|
-
#elif defined(USING_DLL)
|
37
|
-
#define DLLDECL __declspec(dllimport)
|
38
|
-
#else
|
39
|
-
#define DLLDECL
|
40
|
-
#endif
|
41
|
-
|
42
|
-
#if defined(_WIN32)
|
43
|
-
#warn "building on win32"
|
44
|
-
#define CALLCONV __stdcal
|
45
|
-
#else
|
46
|
-
#define CALLCONV
|
47
|
-
#endif
|
48
|
-
|
49
8
|
#endif /* CAUTERIZE_UTIL_H */
|
@@ -16,11 +16,13 @@ class CauterizeData
|
|
16
16
|
end
|
17
17
|
|
18
18
|
class CauterizeBuiltin < CauterizeData
|
19
|
-
attr_reader :val
|
20
19
|
def initialize(val)
|
21
20
|
raise "Out of range value" if not in_range(val)
|
22
21
|
@val = val
|
23
22
|
end
|
23
|
+
def to_ruby
|
24
|
+
@val
|
25
|
+
end
|
24
26
|
end
|
25
27
|
|
26
28
|
class CauterizeBuiltinInteger < CauterizeBuiltin
|
@@ -29,7 +31,7 @@ class CauterizeBuiltinInteger < CauterizeBuiltin
|
|
29
31
|
end
|
30
32
|
|
31
33
|
def to_i
|
32
|
-
val
|
34
|
+
@val
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
@@ -39,7 +41,7 @@ class CauterizeBuiltinFloat < CauterizeBuiltin
|
|
39
41
|
end
|
40
42
|
|
41
43
|
def to_f
|
42
|
-
val
|
44
|
+
@val
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
@@ -55,8 +57,8 @@ class CauterizeScalar < CauterizeData
|
|
55
57
|
@val = self.class.builtin.construct val
|
56
58
|
end
|
57
59
|
|
58
|
-
def
|
59
|
-
@val.
|
60
|
+
def to_ruby
|
61
|
+
@val.to_ruby
|
60
62
|
end
|
61
63
|
|
62
64
|
def pack
|
@@ -69,12 +71,12 @@ class CauterizeScalar < CauterizeData
|
|
69
71
|
end
|
70
72
|
|
71
73
|
class CauterizeArray < CauterizeData
|
72
|
-
def
|
73
|
-
@elems.map{|e| e.
|
74
|
+
def to_ruby
|
75
|
+
@elems.map{|e| e.to_ruby}
|
74
76
|
end
|
75
77
|
|
76
|
-
def
|
77
|
-
|
78
|
+
def to_string
|
79
|
+
to_ruby.to_a.pack("C*")
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
@@ -106,17 +108,13 @@ class CauterizeVariableArray < CauterizeArray
|
|
106
108
|
raise "Invalid length" if @elems.length > self.class.max_length
|
107
109
|
end
|
108
110
|
|
109
|
-
def val
|
110
|
-
@elems.map{|e| e.val}
|
111
|
-
end
|
112
|
-
|
113
111
|
def pack
|
114
112
|
@length.pack + @elems.inject("") { |sum, n| sum + n.pack }
|
115
113
|
end
|
116
114
|
|
117
115
|
def self.unpackio(str)
|
118
116
|
length = self.size_type.unpackio(str)
|
119
|
-
self.new (1..length.
|
117
|
+
self.new (1..length.to_i).map { self.elem_type.unpackio(str) }
|
120
118
|
end
|
121
119
|
end
|
122
120
|
|
@@ -134,8 +132,8 @@ class CauterizeComposite < CauterizeData
|
|
134
132
|
end]
|
135
133
|
end
|
136
134
|
|
137
|
-
def
|
138
|
-
Hash[@fields.map{|name, value| [name, value.
|
135
|
+
def to_ruby
|
136
|
+
Hash[@fields.map{|name, value| [name, value.to_ruby]}]
|
139
137
|
end
|
140
138
|
|
141
139
|
def pack
|
@@ -168,11 +166,11 @@ class CauterizeEnumeration < CauterizeData
|
|
168
166
|
@field_name = field_name
|
169
167
|
end
|
170
168
|
|
171
|
-
def
|
169
|
+
def to_ruby
|
172
170
|
@field_name
|
173
171
|
end
|
174
172
|
|
175
|
-
def
|
173
|
+
def to_i() self.class.fields[@field_name] end
|
176
174
|
|
177
175
|
def pack
|
178
176
|
self.class.repr_type.construct(self.class.fields[@field_name]).pack
|
@@ -193,12 +191,12 @@ class CauterizeGroup < CauterizeData
|
|
193
191
|
attr_reader :tag
|
194
192
|
attr_reader :data
|
195
193
|
|
196
|
-
def
|
194
|
+
def to_ruby
|
197
195
|
if data.nil?
|
198
196
|
{ tag: tag_field_name }
|
199
197
|
else
|
200
198
|
{ tag: tag_field_name,
|
201
|
-
data: data.
|
199
|
+
data: data.to_ruby }
|
202
200
|
end
|
203
201
|
end
|
204
202
|
|
@@ -3,7 +3,7 @@ require_relative './cauterize_ruby_baseclasses'
|
|
3
3
|
class UInt8 < CauterizeBuiltinInteger
|
4
4
|
def in_range(v) v >= 0 && v < 2**8 end
|
5
5
|
def pack
|
6
|
-
[
|
6
|
+
[to_i].pack("C")
|
7
7
|
end
|
8
8
|
def self.unpackio(str)
|
9
9
|
UInt8.new str.read(1).unpack("C")[0]
|
@@ -13,7 +13,7 @@ end
|
|
13
13
|
class UInt16 < CauterizeBuiltinInteger
|
14
14
|
def in_range(v) v >= 0 && v < 2**16 end
|
15
15
|
def pack
|
16
|
-
[
|
16
|
+
[to_i].pack("S")
|
17
17
|
end
|
18
18
|
def self.unpackio(str)
|
19
19
|
UInt16.new str.read(2).unpack("S")[0]
|
@@ -23,7 +23,7 @@ end
|
|
23
23
|
class UInt32 < CauterizeBuiltinInteger
|
24
24
|
def in_range(v) v >= 0 && v < 2**32 end
|
25
25
|
def pack
|
26
|
-
[
|
26
|
+
[to_i].pack("L")
|
27
27
|
end
|
28
28
|
def self.unpackio(str)
|
29
29
|
UInt32.new str.read(4).unpack("L")[0]
|
@@ -33,7 +33,7 @@ end
|
|
33
33
|
class UInt64 < CauterizeBuiltinInteger
|
34
34
|
def in_range(v) v >= 0 && v < 2**64 end
|
35
35
|
def pack
|
36
|
-
[
|
36
|
+
[to_i].pack("Q")
|
37
37
|
end
|
38
38
|
def self.unpackio(str)
|
39
39
|
UInt64.new str.read(8).unpack("Q")[0]
|
@@ -43,7 +43,7 @@ end
|
|
43
43
|
class Int8 < CauterizeBuiltinInteger
|
44
44
|
def in_range(v) (v >= -2**7) && (v < 2**7) end
|
45
45
|
def pack
|
46
|
-
[
|
46
|
+
[to_i].pack("c")
|
47
47
|
end
|
48
48
|
def self.unpackio(str)
|
49
49
|
Int8.new str.read(1).unpack("c")[0]
|
@@ -53,7 +53,7 @@ end
|
|
53
53
|
class Int16 < CauterizeBuiltinInteger
|
54
54
|
def in_range(v) (v >= -2**15) && (v < 2**15) end
|
55
55
|
def pack
|
56
|
-
[
|
56
|
+
[to_i].pack("s")
|
57
57
|
end
|
58
58
|
def self.unpackio(str)
|
59
59
|
Int16.new str.read(2).unpack("s")[0]
|
@@ -63,7 +63,7 @@ end
|
|
63
63
|
class Int32 < CauterizeBuiltinInteger
|
64
64
|
def in_range(v) (v >= -2**31) && (v < 2**31) end
|
65
65
|
def pack
|
66
|
-
[
|
66
|
+
[to_i].pack("l")
|
67
67
|
end
|
68
68
|
def self.unpackio(str)
|
69
69
|
Int32.new str.read(4).unpack("l")[0]
|
@@ -73,7 +73,7 @@ end
|
|
73
73
|
class Int64 < CauterizeBuiltinInteger
|
74
74
|
def in_range(v) (v >= -2**63) && (v < 2**63) end
|
75
75
|
def pack
|
76
|
-
[
|
76
|
+
[to_i].pack("q")
|
77
77
|
end
|
78
78
|
def self.unpackio(str)
|
79
79
|
Int64.new str.read(8).unpack("q")[0]
|
@@ -101,7 +101,7 @@ end
|
|
101
101
|
class Float32 < CauterizeBuiltinFloat
|
102
102
|
def in_range(v) v > (-3.402823466e38) && v < (3.402823466e38) end
|
103
103
|
def pack
|
104
|
-
[
|
104
|
+
[to_f].pack("f")
|
105
105
|
end
|
106
106
|
def self.unpackio(str)
|
107
107
|
Float32.new str.read(4).unpack("f")[0]
|
@@ -111,7 +111,7 @@ end
|
|
111
111
|
class Float64 < CauterizeBuiltinFloat
|
112
112
|
def in_range(v) true end
|
113
113
|
def pack
|
114
|
-
[
|
114
|
+
[to_f].pack("d")
|
115
115
|
end
|
116
116
|
def self.unpackio(str)
|
117
117
|
Float64.new str.read(8).unpack("d")[0]
|
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.pre12
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -126,6 +126,9 @@ files:
|
|
126
126
|
- cauterize.gemspec
|
127
127
|
- example/Cauterize
|
128
128
|
- example/build.sh
|
129
|
+
- example/c_example_support/empty_main.c
|
130
|
+
- example/c_example_support/example_project_config.c
|
131
|
+
- example/c_example_support/example_project_config.h
|
129
132
|
- example/ruby_ex.rb
|
130
133
|
- lib/cauterize.rb
|
131
134
|
- lib/cauterize/base_type.rb
|
@@ -263,7 +266,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
263
266
|
version: '0'
|
264
267
|
segments:
|
265
268
|
- 0
|
266
|
-
hash:
|
269
|
+
hash: 2978929199543110981
|
267
270
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
268
271
|
none: false
|
269
272
|
requirements:
|