Extension of zlib

R

Robert Klemme

Hi,

does anybody else think this (or something similar) would be a useful
extension to zlib?

file zliba.rb:

require 'zlib'

# Conveniently process a number of files that may
# be a mixture of gzip compressed and uncompressed
# files.

ARGFZ = Class.new do
include Enumerable

def each(&b)
::ARGV.each do |arg|
if arg == "-"
$stdin.each(&b)
else
io = ::Zlib::GzipReader.open(arg) rescue ::File.open(arg)
begin
io.each(&b)
ensure
io.close
end
end
end
self
end

# addional methods for gets etc. could be added
# but seeking is too hard to be possible

end.new

Example usage, file catz.rb:

#!/bin/env ruby

require 'zliba' # this would be 'zlib' once this is integrated

ARGFZ.each do |line|
puts line
end

Then on shell prompt:

catz.rb foo foo.gz

Will print all lines from foo and then from foo.gz.

Kind regards

robert
 
D

Daniel Berger

Hi,

does anybody else think this (or something similar) would be a useful
extension to zlib?

file zliba.rb:

require 'zlib'

# Conveniently process a number of files that may
# be a mixture of gzip compressed and uncompressed
# files.

ARGFZ =3D Class.new do
=A0 include Enumerable

=A0 def each(&b)
=A0 =A0 ::ARGV.each do |arg|
=A0 =A0 =A0 if arg =3D=3D "-"
=A0 =A0 =A0 =A0 $stdin.each(&b)
=A0 =A0 =A0 else
=A0 =A0 =A0 =A0 io =3D ::Zlib::GzipReader.open(arg) rescue ::File.open(arg= )
=A0 =A0 =A0 =A0 begin
=A0 =A0 =A0 =A0 =A0 io.each(&b)
=A0 =A0 =A0 =A0 ensure
=A0 =A0 =A0 =A0 =A0 io.close
=A0 =A0 =A0 =A0 end
=A0 =A0 =A0 end
=A0 =A0 end
=A0 =A0 self
=A0 end

=A0 # addional methods for gets etc. could be added
=A0 # but seeking is too hard to be possible

end.new

Example usage, file catz.rb:

#!/bin/env ruby

require 'zliba' # this would be 'zlib' once this is integrated

ARGFZ.each do |line|
=A0 puts line
end

Then on shell prompt:

catz.rb foo foo.gz

Will print all lines from foo and then from foo.gz.

This seems like something that belongs in a custom option handler, not
in zlib directly, especially since I strongly suspect you're going to
break some option parsers with that code.

While we're on the topic of zlib, see zliby:

http://rubyforge.org/projects/zliby/

Regards,

Dan
 
R

Robert Klemme

On May 20, 9:15 am, "Robert Klemme" <[email protected]>
wrote:

This seems like something that belongs in a custom option handler, not
in zlib directly, especially since I strongly suspect you're going to
break some option parsers with that code.

I don't believe so. ARGFZ plays the same role as ARGF and your argument
would apply there as well. But it's not the case since option
processing is typically done *before* ARGF / ARGFZ are employed (see
OptionParser documentation for examples).
While we're on the topic of zlib, see zliby:

http://rubyforge.org/projects/zliby/

I am not sure I get your point here. Can you elaborate?

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top