Class: LineInput

Inherits:
Object
  • Object
show all
Defined in:
lib/refe/lineinput.rb

Overview

lineinput.rb

Copyright © 2002-2003 Minero Aoki <[email protected]>

This program is free software. You can distribute/modify this program under the terms of the GNU Lesser General Public License version 2 or later.

$ docutils Id: lineinput.rb,v 1.1 2003/08/02 14:48:40 aamine Exp $

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ LineInput

Returns a new instance of LineInput.



15
16
17
18
19
20
# File 'lib/refe/lineinput.rb', line 15

def initialize( f )
  @input = f
  @buf = []
  @lineno = 0
  @eof_p = false
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/refe/lineinput.rb', line 22

def eof?
  @buf.empty? and @input.eof?
end

#getsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/refe/lineinput.rb', line 30

def gets
  @lineno += 1
  if @buf.empty?
    if @eof_p     # to avoid ARGF blocking.
      nil
    else
      line = @input.gets
      @eof_p = true unless line
      line
    end
  else
    @buf.pop
  end
end

#gets_if(re) ⇒ Object

def save_index

begin
  save = @i
  yield
ensure
  @i = save
end

end



60
61
62
63
64
65
66
67
# File 'lib/refe/lineinput.rb', line 60

def gets_if( re )
  line = gets()
  if not line or not (re === line)
    ungets line
    return nil
  end
  line
end

#gets_unless(re) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/refe/lineinput.rb', line 69

def gets_unless( re )
  line = gets()
  if not line or re === line
    ungets line
    return nil
  end
  line
end

#linenoObject



26
27
28
# File 'lib/refe/lineinput.rb', line 26

def lineno
  @lineno
end

#ungets(line) ⇒ Object



45
46
47
48
49
# File 'lib/refe/lineinput.rb', line 45

def ungets( line )
  return unless line
  @buf.push line
  line
end

#until_match(re) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/refe/lineinput.rb', line 89

def until_match( re )
  while line = gets()
    if re === line
      ungets line
      return
    end
    yield line
  end
  nil
end

#until_terminator(re) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/refe/lineinput.rb', line 100

def until_terminator( re )
  while line = gets()
    return if re === line   # discard terminal line
    yield line
  end
  nil
end

#while_match(re) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/refe/lineinput.rb', line 78

def while_match( re )
  while line = gets()
    unless re === line
      ungets line
      return
    end
    yield line
  end
  nil
end