libbin 1.0.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/libbin.rb CHANGED
@@ -1,49 +1,115 @@
1
1
  require "libbin_c.so"
2
2
 
3
- require_relative 'libbin/alignment'
4
3
  require_relative 'libbin/data_types'
5
4
 
5
+ # Container module, handles the global state default endianness and error output.
6
6
  module LibBin
7
7
 
8
- BIG_ARCHITECTURE = [0x12345678].pack("i") == "\x12\x34\x56\x78"
9
- @__big = nil
10
- @__output = $stderr
8
+ @big = [0x12345678].pack("i") == "\x12\x34\x56\x78"
9
+ @output = $stderr
11
10
 
12
11
  class << self
13
- attr_accessor :__output
12
+ attr_accessor :big
13
+ attr_accessor :output
14
14
  end
15
15
 
16
+ # Returns true the default endianness is big
16
17
  def self.default_big?
17
- if @__big.nil?
18
- BIG_ARCHITECTURE
19
- else
20
- @__big
21
- end
18
+ @big
22
19
  end
23
20
 
24
- class DataConverter
25
- include Alignment
21
+ class Structure
22
+
23
+ # @!parse
24
+ # attr_accessor :__parent
25
+ # attr_accessor :__index
26
+ # attr_accessor :__position
27
+ # attr_accessor :__cur_position
28
+ # attr_accessor :__input
29
+ # attr_accessor :__output
30
+ # attr_accessor :__input_big
31
+ # attr_accessor :__output_big
32
+ #
33
+ # attr_accessor :__offset
34
+ # attr_accessor :__condition
35
+ # attr_accessor :__type
36
+ # attr_accessor :__length
37
+ # attr_accessor :__count
38
+ # attr_accessor :__iterator
39
+ # attr_accessor :__value
40
+ #
41
+ # # @method __dump_fields
42
+ # # Dump fields according to the internal state of +self+
43
+ # # @return [Structure] self
26
44
 
45
+ # @!visibility private
27
46
  def inspect
28
47
  to_s
29
48
  end
30
49
 
50
+ # @!visibility private
31
51
  def self.inherited(subclass)
32
52
  subclass.instance_variable_set(:@fields, [])
33
53
  end
34
54
 
35
- def __size(previous_offset = 0, parent = nil, index = nil)
36
- __shape(previous_offset, parent, index, DataRange).size
55
+ # Returns the size of the structure
56
+ # @param offset [Integer] position in the stream
57
+ # @param parent [Structure] if given, parent structure
58
+ # @param index [Integer] index if part of a repeated field inside parent
59
+ # @return [Integer] size of the structure
60
+ def __size(offset = 0, parent = nil, index = nil)
61
+ __shape(offset, parent, index, DataRange).size
62
+ end
63
+
64
+ # Returns the alignement of the structure
65
+ # @return [Integer] alignment of the structure
66
+ def self.align
67
+ return @always_align if @always_align
68
+ align = @fields.collect(&:align).select { |v| v }.max
69
+ align = 0 unless align
70
+ align
71
+ end
72
+
73
+ # Set the structure as needing to always be aligned
74
+ # @param align [true, Integer] if true use the fields' maximum alignment
75
+ # @return align
76
+ def self.set_always_align(align)
77
+ if align == true
78
+ @always_align = @fields.collect(&:align).select { |v| v }.max
79
+ @always_align = 0 unless align
80
+ else
81
+ raise "alignement must be a power of 2" if align && (align - 1) & align != 0
82
+ @always_align = align
83
+ end
84
+ align
85
+ end
86
+
87
+ class << self
88
+ alias always_align= set_always_align
89
+ attr_reader :always_align
90
+ attr_reader :fields
37
91
  end
38
92
 
39
- def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
93
+ # Returns the size of a structure
94
+ # @param value [Structure,Array<Structure>] field or array of field to get the size of
95
+ # @param offset [Integer] position in the stream
96
+ # @param parent [Structure] if given, parent structure
97
+ # @param index [Integer] index if part of a repeated field inside parent
98
+ # @param length [Integer] if given, the length of the vector
99
+ def self.size(value, offset = 0, parent = nil, index = nil, length = nil)
40
100
  if length
41
- shape(value, previous_offset, parent, index, length).size
101
+ shape(value, offset, parent, index, length).size
42
102
  else
43
- value.__shape(previous_offset, parent, index).size
103
+ value.__shape(offset, parent, index).size
44
104
  end
45
105
  end
46
106
 
107
+ # Return the structure class fields
108
+ # @return [Array<Field>]
109
+ def __fields
110
+ return self.class.fields
111
+ end
112
+
47
113
  end
48
114
 
49
115
  end
data/libbin.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'libbin'
3
- s.version = "1.0.8"
3
+ s.version = "2.0.0"
4
4
  s.author = "Brice Videau"
5
5
  s.email = "brice.videau@gmail.com"
6
6
  s.homepage = "https://github.com/kerilk/libbin"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libbin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Videau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-01 00:00:00.000000000 Z
11
+ date: 2021-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: float-formats
@@ -49,7 +49,6 @@ files:
49
49
  - ext/libbin/pghalf.c
50
50
  - ext/libbin/pghalf.h
51
51
  - lib/libbin.rb
52
- - lib/libbin/alignment.rb
53
52
  - lib/libbin/data_types.rb
54
53
  - libbin.gemspec
55
54
  homepage: https://github.com/kerilk/libbin
@@ -1,14 +0,0 @@
1
- module LibBin
2
-
3
- module Alignment
4
-
5
- def align(val, alignment)
6
- remainder = val % alignment
7
- val += alignment - remainder if remainder > 0
8
- val
9
- end
10
- private :align
11
-
12
- end
13
-
14
- end