$Id: lineinput.rb 2226 2006-04-15 03:05:09Z aamine $
Copyright © 2002-2005 Minero Aoki
This program is free software. You can distribute/modify this program under the terms of the GNU LGPL, Lesser General Public License version 2.1.
# File lib/lineinput.rb, line 13 def initialize(f) @input = f @buf = [] @lineno = 0 @eof_p = false end
# File lib/lineinput.rb, line 91 def each while line = gets() yield line end end
# File lib/lineinput.rb, line 24 def eof? @eof_p end
# File lib/lineinput.rb, line 147 def getblock(term_re) buf = [] until_terminator(term_re) do |line| buf.push line end buf end
# File lib/lineinput.rb, line 129 def getlines_until(re) buf = [] until_match(re) do |line| buf.push line end buf end
# File lib/lineinput.rb, line 108 def getlines_while(re) buf = [] while_match(re) do |line| buf.push line end buf end
# File lib/lineinput.rb, line 32 def gets unless @buf.empty? @lineno += 1 return @buf.pop end return nil if @eof_p # to avoid ARGF blocking. line = @input.gets @eof_p = true unless line @lineno += 1 line end
# File lib/lineinput.rb, line 73 def gets_if(re) line = gets() if not line or not (re =~ line) ungets line return nil end line end
# File lib/lineinput.rb, line 82 def gets_unless(re) line = gets() if not line or re =~ line ungets line return nil end line end
# File lib/lineinput.rb, line 20 def inspect "\#<#{self.class} file=#{@input.inspect} line=#{lineno()}>" end
# File lib/lineinput.rb, line 28 def lineno @lineno end
# File lib/lineinput.rb, line 57 def next? peek() ? true : false end
# File lib/lineinput.rb, line 51 def peek line = gets() ungets line if line line end
# File lib/lineinput.rb, line 61 def skip_blank_lines n = 0 while line = gets() unless line.strip.empty? ungets line return n end n += 1 end n end
# File lib/lineinput.rb, line 44 def ungets(line) return unless line @lineno -= 1 @buf.push line line end
# File lib/lineinput.rb, line 118 def until_match(re) while line = gets() if re =~ line ungets line return end yield line end nil end
# File lib/lineinput.rb, line 139 def until_terminator(re) while line = gets() return if re =~ line # discard terminal line yield line end nil end
# File lib/lineinput.rb, line 97 def while_match(re) while line = gets() unless re =~ line ungets line return end yield line end nil end