Class: CSV::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/csv/parser.rb

Defined Under Namespace

Classes: InputsScanner, InvalidEncoding, Scanner, UnoptimizedStringIO

Instance Method Summary collapse

Constructor Details

#initialize(input, options) ⇒ Parser

Returns a new instance of Parser.



169
170
171
172
173
174
175
# File 'lib/csv/parser.rb', line 169

def initialize(input, options)
  @input = input
  @options = options
  @samples = []

  prepare
end

Instance Method Details

#column_separatorObject



177
178
179
# File 'lib/csv/parser.rb', line 177

def column_separator
  @column_separator
end

#field_size_limitObject



189
190
191
# File 'lib/csv/parser.rb', line 189

def field_size_limit
  @field_size_limit
end

#header_row?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/csv/parser.rb', line 205

def header_row?
  @use_headers and @headers.nil?
end

#headersObject



201
202
203
# File 'lib/csv/parser.rb', line 201

def headers
  @headers
end

#liberal_parsing?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/csv/parser.rb', line 217

def liberal_parsing?
  @liberal_parsing
end

#lineObject



225
226
227
# File 'lib/csv/parser.rb', line 225

def line
  last_line
end

#linenoObject



221
222
223
# File 'lib/csv/parser.rb', line 221

def lineno
  @lineno
end

#parse(&block) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/csv/parser.rb', line 229

def parse(&block)
  return to_enum(__method__) unless block_given?

  if @return_headers and @headers
    headers = Row.new(@headers, @raw_headers, true)
    if @unconverted_fields
      headers = add_unconverted_fields(headers, [])
    end
    yield headers
  end

  row = []
  begin
    @scanner = build_scanner
    skip_needless_lines
    start_row
    while true
      @quoted_column_value = false
      @unquoted_column_value = false
      value = parse_column_value
      if value and @field_size_limit and value.size >= @field_size_limit
        raise MalformedCSVError.new("Field size exceeded", @lineno + 1)
      end
      if parse_column_end
        row << value
      elsif parse_row_end
        if row.empty? and value.nil?
          emit_row([], &block) unless @skip_blanks
        else
          row << value
          emit_row(row, &block)
          row = []
        end
        skip_needless_lines
        start_row
      elsif @scanner.eos?
        return if row.empty? and value.nil?
        row << value
        emit_row(row, &block)
        return
      else
        if @quoted_column_value
          message = "Do not allow except col_sep_split_separator " +
            "after quoted fields"
          raise MalformedCSVError.new(message, @lineno + 1)
        elsif @unquoted_column_value and @scanner.scan(@cr_or_lf)
          message = "Unquoted fields do not allow \\r or \\n"
          raise MalformedCSVError.new(message, @lineno + 1)
        elsif @scanner.rest.start_with?(@quote_character)
          message = "Illegal quoting"
          raise MalformedCSVError.new(message, @lineno + 1)
        else
          raise MalformedCSVError.new("TODO: Meaningful message",
                                      @lineno + 1)
        end
      end
    end
  rescue InvalidEncoding
    message = "Invalid byte sequence in #{@encoding}"
    raise MalformedCSVError.new(message, @lineno + 1)
  end
end

#quote_characterObject



185
186
187
# File 'lib/csv/parser.rb', line 185

def quote_character
  @quote_character
end

#return_headers?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/csv/parser.rb', line 209

def return_headers?
  @return_headers
end

#row_separatorObject



181
182
183
# File 'lib/csv/parser.rb', line 181

def row_separator
  @row_separator
end

#skip_blanks?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/csv/parser.rb', line 213

def skip_blanks?
  @skip_blanks
end

#skip_linesObject



193
194
195
# File 'lib/csv/parser.rb', line 193

def skip_lines
  @skip_lines
end

#unconverted_fields?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/csv/parser.rb', line 197

def unconverted_fields?
  @unconverted_fields
end