shift()
public
The primary read method for wrapped Strings
and IOs, a single row is pulled from the data source, parsed and returned
as an Array of fields (if header rows are not
used) or a CSV::Row (when header rows are
used).
The data source must be open for
reading.
Show source
def shift
if header_row? and @return_headers and
[Array, String].include? @use_headers.class
if @unconverted_fields
return add_unconverted_fields(parse_headers, Array.new)
else
return parse_headers
end
end
line = ""
loop do
(line += @io.gets(@row_sep)) rescue return nil
parse = line.dup
parse.sub!(@parsers[:line_end], "")
if parse.empty?
@lineno += 1
if @skip_blanks
line = ""
next
elsif @unconverted_fields
return add_unconverted_fields(Array.new, Array.new)
elsif @use_headers
return self.class::Row.new(Array.new, Array.new)
else
return Array.new
end
end
csv = if parse.sub!(@parsers[:leading_fields], "")
[nil] * ($&.length / @col_sep.length)
else
Array.new
end
parse.gsub!(@parsers[:csv_row]) do
csv << if $1.nil?
if $2.empty?
nil
else
if $2.count(@parsers[:return_newline]).zero?
$2
else
raise MalformedCSVError, "Unquoted fields do not allow " +
"\\r or \\n (line #{lineno + 1})."
end
end
else
$1.gsub(@quote_char * 2, @quote_char)
end
""
end
if parse.empty?
@lineno += 1
unconverted = csv.dup if @unconverted_fields
csv = convert_fields(csv) unless @use_headers or @converters.empty?
csv = parse_headers(csv) if @use_headers
if @unconverted_fields and not csv.respond_to? :unconverted_fields
add_unconverted_fields(csv, unconverted)
end
break csv
end
if @io.eof?
raise MalformedCSVError, "Unclosed quoted field on line #{lineno + 1}."
elsif parse =~ @parsers[:bad_field]
raise MalformedCSVError, "Illegal quoting on line #{lineno + 1}."
elsif @field_size_limit and parse.length >= @field_size_limit
raise MalformedCSVError, "Field size exceeded on line #{lineno + 1}."
end
end
end