Skip to content

Instantly share code, notes, and snippets.

@freeformz
Created September 24, 2010 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freeformz/594779 to your computer and use it in GitHub Desktop.
Save freeformz/594779 to your computer and use it in GitHub Desktop.
silly piper
READ_BUF = 1024
HIGH_WATER = 1024
$fin = nil
$fout = nil
def open_fin
if $fin.nil? || ($fin.is_a?(File) && ($fin.eof? || $fin.closed?))
if File.exists?(ARGV[0])
unless File.pipe?(ARGV[0])
puts "#{ARGV[0]} is not a pipe"
exit
end
else
system("mkfifo #{ARGV[0]}")
end
$fin = File.open(ARGV[0],'r+')
end
end
def open_fout
if $fout.nil? || ($fout.is_a?(File) && $fout.closed?)
$fout = File.open(ARGV[1],'a+')
end
end
def reset
$a = ""
$idx = 0
end
def write_out(extra = :none)
if $fout.closed?
open_fin
end
$fout.write($a)
$fout.flush
$fout.fsync
if extra == :close
$fout.close
end
end
Signal.trap("INT") do
write_out :close
File.unlink(ARGV[0]) if File.exist?(ARGV[0]) && File.pipe?(ARGV[0])
puts "bye"
exit
end
reset
while true
open_fin
open_fout
begin
while data = $fin.readpartial(READ_BUF)
$a += data
$idx += 1
if $idx > HIGH_WATER
write_out
reset
end
end
rescue EOFError => e
write_out
reset
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment