2024-09-03 15:38:54 +02:00
|
|
|
# vim: set noet sw=2 ts=2 sts=2:
|
|
|
|
|
2024-01-22 14:27:06 +01:00
|
|
|
require 'cbor'
|
2024-09-03 15:38:54 +02:00
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
class CBORIO <IO
|
|
|
|
include Enumerable
|
|
|
|
extend Enumerable
|
|
|
|
def orig_io() @io end
|
|
|
|
def to_io() self end
|
|
|
|
def to_i() @io.to_i end
|
|
|
|
def fileno() @io.fileno end
|
|
|
|
def unpacker() @un end
|
|
|
|
def close() @io.close end
|
|
|
|
def close_write() @io.close_write end
|
|
|
|
def close_read() @io.close_read end
|
|
|
|
def write( obj) @io.write CBOR.pack( obj) end
|
|
|
|
def sync() @io.sync end
|
|
|
|
def sync=( v) @io.sync = v end
|
|
|
|
def inspect() "#<#{self.class.name} fd:#{@io.to_i} #{@mode}>" end
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
def stream() Socket.pair( :UNIX, :STREAM, 0).map {|s| self.new s, :rw } end
|
|
|
|
|
|
|
|
def pipe
|
|
|
|
a, b = IO.pipe
|
|
|
|
[self.new( a, :r), self.new( b, :w)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def popen mode = nil, &exe
|
|
|
|
s1, s2 =
|
|
|
|
case mode
|
|
|
|
when :rw, 'rw', nil then self.stream
|
|
|
|
when :r, 'r' then self.pipe
|
|
|
|
when :w, 'w' then self.pipe.reverse
|
|
|
|
end
|
|
|
|
pid =
|
|
|
|
fork do
|
|
|
|
s1.close
|
|
|
|
yield s2
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
s2.close
|
|
|
|
[pid, s1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def open io, mode = :r, &exe
|
|
|
|
s = self.new io, mode
|
|
|
|
if block_given?
|
|
|
|
begin yield s
|
|
|
|
ensure s.close
|
|
|
|
end
|
|
|
|
else s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each fn, &exe
|
|
|
|
open fn, 'r' do |f|
|
|
|
|
f.each &exe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize io, mode = :rw
|
|
|
|
case io
|
|
|
|
when IO then @io = io
|
|
|
|
when String then @io = File.open io, mode
|
|
|
|
when Pathname then @io = io.open mode
|
|
|
|
else raise ArgumentError, "This seems not to be a IO/String/Pathname: #{io.inspect}"
|
|
|
|
end
|
|
|
|
@mode, @un, @io.sync = mode, CBOR::Unpacker.new, true
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
|
|
|
return @un.read rescue EOFError
|
|
|
|
loop do
|
|
|
|
@un.feed @io.readpartial( 1024)
|
|
|
|
return @un.read rescue EOFError
|
|
|
|
end
|
|
|
|
end
|
2024-01-22 14:27:06 +01:00
|
|
|
|
2024-09-03 15:38:54 +02:00
|
|
|
def each &exe
|
|
|
|
return to_enum __method__ unless block_given?
|
|
|
|
loop { yield read }
|
|
|
|
self
|
|
|
|
rescue EOFError
|
|
|
|
self
|
|
|
|
end
|
2024-01-22 14:27:06 +01:00
|
|
|
end
|