kuro7-gas 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gas.rb +28 -19
- metadata +2 -2
data/lib/gas.rb
CHANGED
@@ -16,6 +16,10 @@
|
|
16
16
|
|
17
17
|
module Gas
|
18
18
|
|
19
|
+
# Gas::Error
|
20
|
+
class Error < StandardError
|
21
|
+
end
|
22
|
+
|
19
23
|
def encode_num (value)
|
20
24
|
buf = String.new
|
21
25
|
|
@@ -63,7 +67,7 @@ module Gas
|
|
63
67
|
zero_byte_count = 0
|
64
68
|
loop do
|
65
69
|
byte = io.read(1)
|
66
|
-
|
70
|
+
raise Gas::Error, 'failed to read 1 byte', caller unless byte
|
67
71
|
byte = byte[0]
|
68
72
|
break if byte != 0x00
|
69
73
|
zero_byte_count += 1
|
@@ -84,7 +88,7 @@ module Gas
|
|
84
88
|
retval = mask & byte
|
85
89
|
additional_bytes_to_read.times do
|
86
90
|
byte = io.read(1)
|
87
|
-
|
91
|
+
raise Gas::Error, 'failed to read 1 byte', caller unless byte
|
88
92
|
byte = byte[0]
|
89
93
|
retval = (retval << 8) | byte
|
90
94
|
end
|
@@ -96,6 +100,10 @@ module Gas
|
|
96
100
|
|
97
101
|
attr_accessor :parent, :size, :id, :attributes, :payload, :children
|
98
102
|
|
103
|
+
def self.parse (io)
|
104
|
+
Chunk.new.parse(io)
|
105
|
+
end
|
106
|
+
|
99
107
|
def initialize (arg = nil)
|
100
108
|
@parent = nil
|
101
109
|
@size = 0
|
@@ -123,7 +131,7 @@ module Gas
|
|
123
131
|
when IO, StringIO
|
124
132
|
parse(arg)
|
125
133
|
else
|
126
|
-
|
134
|
+
raise Gas::Error, "invalid type: #{arg.class}", caller
|
127
135
|
end
|
128
136
|
end
|
129
137
|
def parse (io)
|
@@ -135,7 +143,7 @@ module Gas
|
|
135
143
|
id_size = decode_num(io)
|
136
144
|
@id = io.read(id_size)
|
137
145
|
unless @id and @id.size == id_size
|
138
|
-
|
146
|
+
raise Gas::Error, "failed to read #{'0x%x' % id_size} bytes", caller
|
139
147
|
end
|
140
148
|
nb_attributes = decode_num(io)
|
141
149
|
@attributes = Hash.new
|
@@ -143,19 +151,19 @@ module Gas
|
|
143
151
|
key_size = decode_num(io)
|
144
152
|
key = io.read(key_size)
|
145
153
|
unless key and key.size == key_size
|
146
|
-
|
154
|
+
raise Gas::Error, "failed to read #{'0x%x' % key_size} bytes" , caller
|
147
155
|
end
|
148
156
|
value_size = decode_num(io)
|
149
157
|
value = io.read(value_size)
|
150
158
|
unless value and value.size == value_size
|
151
|
-
|
159
|
+
raise Gas::Error, "failed to read #{'0x%x' % value_size} bytes", caller
|
152
160
|
end
|
153
161
|
@attributes[key] = value
|
154
162
|
end
|
155
163
|
payload_size = decode_num(io)
|
156
164
|
@payload = io.read(payload_size)
|
157
165
|
unless @payload and @payload.size == payload_size
|
158
|
-
|
166
|
+
raise Gas::Error, "failed to read #{'0x%x' % payload_size} bytes", caller
|
159
167
|
end
|
160
168
|
nb_children = decode_num(io)
|
161
169
|
@children = Array.new
|
@@ -165,7 +173,8 @@ module Gas
|
|
165
173
|
end
|
166
174
|
self
|
167
175
|
end
|
168
|
-
def write (io)
|
176
|
+
def write (io, updated = false)
|
177
|
+
update unless updated
|
169
178
|
io << encode_num(@size)
|
170
179
|
io << encode_num(id.size)
|
171
180
|
io << id
|
@@ -182,7 +191,7 @@ module Gas
|
|
182
191
|
io << @payload
|
183
192
|
io << encode_num(@children.size)
|
184
193
|
@children.each do |child|
|
185
|
-
child.write(io)
|
194
|
+
child.write(io, true)
|
186
195
|
end
|
187
196
|
io
|
188
197
|
end
|
@@ -232,7 +241,7 @@ module Gas
|
|
232
241
|
chunk.parent = self
|
233
242
|
@children << chunk
|
234
243
|
else
|
235
|
-
raise
|
244
|
+
raise Gas::Error, 'invalid type for appending', caller
|
236
245
|
end
|
237
246
|
self
|
238
247
|
end
|
@@ -264,7 +273,7 @@ module Gas
|
|
264
273
|
case meth.to_s
|
265
274
|
when /=\Z/
|
266
275
|
key = meth.to_s[0..-2]
|
267
|
-
raise
|
276
|
+
raise Gas::Error, 'invalid arg count', caller unless args.size == 1
|
268
277
|
self[key] = args.first
|
269
278
|
else
|
270
279
|
return self[meth]
|
@@ -272,10 +281,10 @@ module Gas
|
|
272
281
|
self
|
273
282
|
end
|
274
283
|
|
275
|
-
def test (depth = 0)
|
284
|
+
def test (io = $stderr, depth = 0)
|
276
285
|
pi = proc do |data|
|
277
|
-
print(' ' * depth)
|
278
|
-
puts data
|
286
|
+
io.print(' ' * depth)
|
287
|
+
io.puts data
|
279
288
|
end
|
280
289
|
pi.call '---'
|
281
290
|
pi.call "id: #{id}"
|
@@ -292,15 +301,15 @@ module Gas
|
|
292
301
|
#lines = lines.map {|l|l.strip}
|
293
302
|
m = lines.collect{|l|l.size}.max
|
294
303
|
m = m > 78 ? 78 : m
|
295
|
-
puts "+" << ('-' * m)
|
304
|
+
io.puts "+" << ('-' * m)
|
296
305
|
lines.each do |line|
|
297
|
-
puts "| " << line
|
298
|
-
#puts "| " << line.inspect[1..-2]
|
306
|
+
io.puts "| " << line
|
307
|
+
#io.puts "| " << line.inspect[1..-2]
|
299
308
|
end
|
300
|
-
puts "+" << ('-' * m)
|
309
|
+
io.puts "+" << ('-' * m)
|
301
310
|
end
|
302
311
|
@children.each do |child|
|
303
|
-
child.test(depth+1)
|
312
|
+
child.test(io, depth+1)
|
304
313
|
end
|
305
314
|
end
|
306
315
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kuro7-gas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blanton Black
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-29 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|