Class: GDV::Format::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/gdv/format/reader.rb

Constant Summary collapse

INPUT_CODING =

FIXME: For now, we assume ISO-8859-15 encoding of all input files. There are other possibilities, like CP850; when we encounter them, we need a way to set the source character encoding

"ISO-8859-15"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Reader


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gdv/format/reader.rb', line 95

def initialize(io)
    @features = [:pad_short_lines]
    if io.is_a?(String)
        @features << :close_at_eof
        @io = File.open(io, "r:#{INPUT_CODING}")
    else
        @io = io
    end
    @line = nil
    @lineno = 0
    @unknown = 0
    @records = []
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io


93
94
95
# File 'lib/gdv/format/reader.rb', line 93

def io
  @io
end

#linenoObject (readonly)

Returns the value of attribute lineno


93
94
95
# File 'lib/gdv/format/reader.rb', line 93

def lineno
  @lineno
end

#unknownObject (readonly)

Returns the value of attribute unknown


93
94
95
# File 'lib/gdv/format/reader.rb', line 93

def unknown
  @unknown
end

Instance Method Details

#feature?(name) ⇒ Boolean


109
110
111
# File 'lib/gdv/format/reader.rb', line 109

def feature?(name)
    @features.include?(name)
end

#getrecRecord?

Read the next record from the input stream


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gdv/format/reader.rb', line 127

def getrec
    unless @records.empty?
        return @records.shift
    end
    getline unless @line
    return nil if @line.nil?
    lines = [ @line ]
    if rectype = @line.rectype
        loop do
            getline
            break if @line.nil? || rectype.nil?
            break if rectype != @line.rectype
            break if lines.last.snr >= @line.snr

            lines << @line
        end
    end
    return Record.new(rectype, lines, @lineno - lines.size)
end

#match(cond) ⇒ Record?

Return the next record, provided it matches cond there is no next record


164
165
166
# File 'lib/gdv/format/reader.rb', line 164

def match(cond)
    getrec if match?(cond)
end

#match!(cond) ⇒ Record

Return the next record, provided it matches cond; if it doesn't, raise a MatchError

Raises:

  • (MatchError)

    if the next record does not match cond


172
173
174
175
176
# File 'lib/gdv/format/reader.rb', line 172

def match!(cond)
    rec = match(cond)
    raise MatchError.new(self, cond) unless rec
    rec
end

#match?(cond) ⇒ Boolean

Check if the next record matches the condition cond without consuming the record matches, false otherwise


151
152
153
154
155
156
157
158
# File 'lib/gdv/format/reader.rb', line 151

def match?(cond)
    rec = peek
    return false if rec.nil?
    path = rec.rectype.parts.first.path
    cond.keys.all? do |k|
        cond_match(path[k], cond[k])
    end
end

#parse(klass) ⇒ Object

Parse records according to the grammar in klass.grammar


179
180
181
# File 'lib/gdv/format/reader.rb', line 179

def parse(klass)
    Parser.new(self, klass).result
end

#peekRecord


118
119
120
121
122
# File 'lib/gdv/format/reader.rb', line 118

def peek
    rec = getrec
    unshift(rec)
    rec
end

#unshift(rec) ⇒ Object


113
114
115
# File 'lib/gdv/format/reader.rb', line 113

def unshift(rec)
    @records.unshift(rec)
end