Class: RedshiftCsvFile::ScanBuffer

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

Constant Summary collapse

MAX_COLUMN_LENGTH =

1.2MB

(1.2 * (1024 ** 3)).to_i

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ ScanBuffer

Returns a new instance of ScanBuffer.



59
60
61
62
63
# File 'lib/redshift_csv_file.rb', line 59

def initialize(f)
  @f = f
  @s = StringScanner.new("")
  @eof = false
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/redshift_csv_file.rb', line 65

def eof?
  @s.eos? && @eof
end

#fill_bufferObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/redshift_csv_file.rb', line 92

def fill_buffer
  line = @f.gets
  if line
    @s << line
    true
  else
    @eof = true
    false
  end
end

#linenoObject



69
70
71
# File 'lib/redshift_csv_file.rb', line 69

def lineno
  @f.lineno
end

#next_rowObject



73
74
75
# File 'lib/redshift_csv_file.rb', line 73

def next_row
  fill_buffer
end

#read_eolObject



107
108
109
# File 'lib/redshift_csv_file.rb', line 107

def read_eol
  @s.skip(/[ \t\r]*(?:\n|\z)/)
end

#read_separatorObject



103
104
105
# File 'lib/redshift_csv_file.rb', line 103

def read_separator
  @s.skip(/[ \t]*,/)
end

#scan_columnObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redshift_csv_file.rb', line 79

def scan_column
  s = @s
  s.skip(/[ \t]+/)
  until column = s.scan(/"(?:\\.|[^"\\]+)*"/m)
    fill_buffer or return nil
    return nil if s.eos?
    if s.rest_size > MAX_COLUMN_LENGTH
      raise MalformedCSVException, "CSV parse error: too long column at line #{@f.lineno}"
    end
  end
  column
end