iostruct 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/lib/iostruct/version.rb +1 -1
- data/lib/iostruct.rb +5 -1
- data/spec/iostruct_spec.rb +23 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a17b782202e0503788fd3b16dffdebddecd62becb30c52ba28161ebde1e300d
|
4
|
+
data.tar.gz: d42484281a468a378902d4589b67201ad4f134660559766690e48849b9f2a46c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db34080f66b2815628e20a6e7b9331089d55a011dd35ae90b9e8f7758cb97332fad7b9caadfcc977a93e104cb0d9b9e3c932e82a2070e54205ba3bb7a52337d4
|
7
|
+
data.tar.gz: d25f3804da77a52630bb4492548c3ab71124c06610558dde277ea2c769a826ce8a720a5df99b2ab3691791b090f6ec4774b65e8072117383ab389e4095451a0f
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
data/lib/iostruct/version.rb
CHANGED
data/lib/iostruct.rb
CHANGED
@@ -94,9 +94,11 @@ module IOStruct
|
|
94
94
|
module ClassMethods
|
95
95
|
# src can be IO or String, or anything that responds to :read or :unpack
|
96
96
|
def read src, size = nil
|
97
|
+
pos = nil
|
97
98
|
size ||= const_get 'SIZE'
|
98
99
|
data =
|
99
100
|
if src.respond_to?(:read)
|
101
|
+
pos = src.tell
|
100
102
|
src.read(size).to_s
|
101
103
|
elsif src.respond_to?(:unpack)
|
102
104
|
src
|
@@ -106,11 +108,13 @@ module IOStruct
|
|
106
108
|
# if data.size < size
|
107
109
|
# $stderr.puts "[!] #{self.to_s} want #{size} bytes, got #{data.size}"
|
108
110
|
# end
|
109
|
-
new(*data.unpack(const_get('FORMAT')))
|
111
|
+
new(*data.unpack(const_get('FORMAT'))).tap{ |x| x.__offset = pos }
|
110
112
|
end
|
111
113
|
end # ClassMethods
|
112
114
|
|
113
115
|
module InstanceMethods
|
116
|
+
attr_accessor :__offset
|
117
|
+
|
114
118
|
def pack
|
115
119
|
to_a.pack self.class.const_get('FORMAT')
|
116
120
|
end
|
data/spec/iostruct_spec.rb
CHANGED
@@ -96,4 +96,27 @@ describe IOStruct do
|
|
96
96
|
it "throws exception on unknown format" do
|
97
97
|
expect { IOStruct.new('K', :x) }.to raise_error('Unknown field type "K"')
|
98
98
|
end
|
99
|
+
|
100
|
+
context '__offset field' do
|
101
|
+
let(:data) { 0x100.times.to_a.pack('L*') }
|
102
|
+
let(:io) { StringIO.new(data) }
|
103
|
+
let(:struct) { IOStruct.new('LLLL', :a, :b, :c, :d) }
|
104
|
+
|
105
|
+
context 'when src is an IO' do
|
106
|
+
it 'is set to the current IO position' do
|
107
|
+
a = []
|
108
|
+
while !io.eof?
|
109
|
+
a << struct.read(io)
|
110
|
+
end
|
111
|
+
expect(a.map(&:__offset)).to eq (0...0x400).step(0x10).to_a
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when src is a string' do
|
116
|
+
it 'is nil' do
|
117
|
+
x = struct.read(data)
|
118
|
+
expect(x.__offset).to be_nil
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
99
122
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iostruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey "Zed" Zaikin
|
@@ -60,6 +60,7 @@ extra_rdoc_files: []
|
|
60
60
|
files:
|
61
61
|
- ".gitignore"
|
62
62
|
- ".rspec"
|
63
|
+
- CHANGELOG.md
|
63
64
|
- Gemfile
|
64
65
|
- Gemfile.lock
|
65
66
|
- LICENSE.txt
|
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
90
|
- !ruby/object:Gem::Version
|
90
91
|
version: '0'
|
91
92
|
requirements: []
|
92
|
-
rubygems_version: 3.5.
|
93
|
+
rubygems_version: 3.5.22
|
93
94
|
signing_key:
|
94
95
|
specification_version: 4
|
95
96
|
summary: A Struct that can read/write itself from/to IO-like objects
|