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 168
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 177
def column_separator
  @column_separator
end
            
field_size_limit() click to toggle source
 
               # File csv/parser.rb, line 189
def field_size_limit
  @field_size_limit
end
            
header_row?() click to toggle source
 
               # File csv/parser.rb, line 205
def header_row?
  @use_headers and @headers.nil?
end
            
headers() click to toggle source
 
               # File csv/parser.rb, line 201
def headers
  @headers
end
            
liberal_parsing?() click to toggle source
 
               # File csv/parser.rb, line 217
def liberal_parsing?
  @liberal_parsing
end
            
line() click to toggle source
 
               # File csv/parser.rb, line 225
def line
  last_line
end
            
lineno() click to toggle source
 
               # File csv/parser.rb, line 221
def lineno
  @lineno
end
            
parse(&block) click to toggle source
 
               # File csv/parser.rb, line 229
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 185
def quote_character
  @quote_character
end
            
return_headers?() click to toggle source
 
               # File csv/parser.rb, line 209
def return_headers?
  @return_headers
end
            
row_separator() click to toggle source
 
               # File csv/parser.rb, line 181
def row_separator
  @row_separator
end
            
skip_blanks?() click to toggle source
 
               # File csv/parser.rb, line 213
def skip_blanks?
  @skip_blanks
end
            
skip_lines() click to toggle source
 
               # File csv/parser.rb, line 193
def skip_lines
  @skip_lines
end
            
unconverted_fields?() click to toggle source
 
               # File csv/parser.rb, line 197
def unconverted_fields?
  @unconverted_fields
end
            
use_headers?() click to toggle source
 
               # File csv/parser.rb, line 296
def use_headers?
  @use_headers
end