In Files

  • csv/parser.rb

CSV::Parser

Constants

SCANNER_TEST

Public Class Methods

new(input, options) click to toggle source
 
               # File csv/parser.rb, line 169
def initialize(input, options)
  @input = input
  @options = options
  @samples = []
  @parsed = false

  prepare
end
            

Public Instance Methods

column_separator() click to toggle source
 
               # File csv/parser.rb, line 178
def column_separator
  @column_separator
end
            
field_size_limit() click to toggle source
 
               # File csv/parser.rb, line 190
def field_size_limit
  @field_size_limit
end
            
header_row?() click to toggle source
 
               # File csv/parser.rb, line 206
def header_row?
  @use_headers and @headers.nil?
end
            
headers() click to toggle source
 
               # File csv/parser.rb, line 202
def headers
  @headers
end
            
liberal_parsing?() click to toggle source
 
               # File csv/parser.rb, line 218
def liberal_parsing?
  @liberal_parsing
end
            
line() click to toggle source
 
               # File csv/parser.rb, line 226
def line
  last_line
end
            
lineno() click to toggle source
 
               # File csv/parser.rb, line 222
def lineno
  @lineno
end
            
parse(&block) click to toggle source
 
               # File csv/parser.rb, line 230
def parse(&block)
  return to_enum(__method__) unless block_given?

  return if @parsed

  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?
        break if row.empty? and value.nil?
        row << value
        emit_row(row, &block)
        break
      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

  @parsed = true
end
            
quote_character() click to toggle source
 
               # File csv/parser.rb, line 186
def quote_character
  @quote_character
end
            
return_headers?() click to toggle source
 
               # File csv/parser.rb, line 210
def return_headers?
  @return_headers
end
            
row_separator() click to toggle source
 
               # File csv/parser.rb, line 182
def row_separator
  @row_separator
end
            
skip_blanks?() click to toggle source
 
               # File csv/parser.rb, line 214
def skip_blanks?
  @skip_blanks
end
            
skip_lines() click to toggle source
 
               # File csv/parser.rb, line 194
def skip_lines
  @skip_lines
end
            
unconverted_fields?() click to toggle source
 
               # File csv/parser.rb, line 198
def unconverted_fields?
  @unconverted_fields
end
            
use_headers?() click to toggle source
 
               # File csv/parser.rb, line 297
def use_headers?
  @use_headers
end