2003-06-19 20:08:52 +04:00
|
|
|
# CSV -- module for generating/parsing CSV data.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
# Copyright (C) 2000-2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>.
|
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
# $Id$
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
|
|
|
# redistribute it and/or modify it under the same terms of Ruby's license;
|
|
|
|
# either the dual license version in 2003, or any later version.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
class CSV
|
2004-02-16 17:41:49 +03:00
|
|
|
class IllegalFormatError < RuntimeError; end
|
|
|
|
|
2004-05-26 18:30:30 +04:00
|
|
|
# Open a CSV formatted file for reading or writing.
|
|
|
|
#
|
|
|
|
# For reading.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-02-16 17:41:49 +03:00
|
|
|
# EXAMPLE 1
|
2004-05-26 18:30:30 +04:00
|
|
|
# CSV.open('csvfile.csv', 'r') do |row|
|
|
|
|
# p row
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# EXAMPLE 2
|
|
|
|
# reader = CSV.open('csvfile.csv', 'r')
|
2004-02-16 17:41:49 +03:00
|
|
|
# row1 = reader.shift
|
|
|
|
# row2 = reader.shift
|
|
|
|
# if row2.empty?
|
|
|
|
# p 'row2 not find.'
|
2003-06-19 20:08:52 +04:00
|
|
|
# end
|
2004-02-16 17:41:49 +03:00
|
|
|
# reader.close
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-02-16 17:41:49 +03:00
|
|
|
# ARGS
|
|
|
|
# filename: filename to parse.
|
|
|
|
# col_sep: Column separator. ?, by default. If you want to separate
|
|
|
|
# fields with semicolon, give ?; here.
|
|
|
|
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
|
|
|
|
# want to separate records with \r, give ?\r here.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-02-16 17:41:49 +03:00
|
|
|
# RETURNS
|
|
|
|
# reader instance. To get parse result, see CSV::Reader#each.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
#
|
2004-05-26 18:30:30 +04:00
|
|
|
# For writing.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-05-26 18:30:30 +04:00
|
|
|
# EXAMPLE 1
|
|
|
|
# CSV.open('csvfile.csv', 'w') do |writer|
|
2004-02-16 17:41:49 +03:00
|
|
|
# writer << ['r1c1', 'r1c2']
|
|
|
|
# writer << ['r2c1', 'r2c2']
|
|
|
|
# writer << [nil, nil]
|
|
|
|
# end
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-05-26 18:30:30 +04:00
|
|
|
# EXAMPLE 2
|
|
|
|
# writer = CSV.open('csvfile.csv', 'w')
|
|
|
|
# writer << ['r1c1', 'r1c2'] << ['r2c1', 'r2c2'] << [nil, nil]
|
|
|
|
# writer.close
|
|
|
|
#
|
2003-06-19 20:08:52 +04:00
|
|
|
# ARGS
|
2004-02-16 17:41:49 +03:00
|
|
|
# filename: filename to generate.
|
2003-06-19 20:08:52 +04:00
|
|
|
# col_sep: Column separator. ?, by default. If you want to separate
|
|
|
|
# fields with semicolon, give ?; here.
|
2003-09-15 14:07:42 +04:00
|
|
|
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
|
|
|
|
# want to separate records with \r, give ?\r here.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# RETURNS
|
2004-02-16 17:41:49 +03:00
|
|
|
# writer instance. See CSV::Writer#<< and CSV::Writer#add_row to know how
|
|
|
|
# to generate CSV string.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-05-26 18:30:30 +04:00
|
|
|
def CSV.open(path, mode, fs = nil, rs = nil, &block)
|
|
|
|
if mode == 'r' or mode == 'rb'
|
|
|
|
open_reader(path, mode, fs, rs, &block)
|
|
|
|
elsif mode == 'w' or mode == 'wb'
|
|
|
|
open_writer(path, mode, fs, rs, &block)
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def CSV.foreach(path, rs = nil, &block)
|
|
|
|
open_reader(path, 'r', ',', rs, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def CSV.read(path, length = nil, offset = nil)
|
|
|
|
CSV.parse(IO.read(path, length, offset))
|
|
|
|
end
|
|
|
|
|
|
|
|
def CSV.readlines(path, rs = nil)
|
|
|
|
reader = open_reader(path, 'r', ',', rs)
|
|
|
|
begin
|
|
|
|
reader.collect { |row| row }
|
|
|
|
ensure
|
|
|
|
reader.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-05-20 21:24:04 +04:00
|
|
|
def CSV.generate(path, fs = nil, rs = nil, &block)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
open_writer(path, 'w', fs, rs, &block)
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
2004-05-26 18:30:30 +04:00
|
|
|
# Parse lines from given string or stream. Return rows as an Array of Arrays.
|
|
|
|
def CSV.parse(str_or_readable, fs = nil, rs = nil, &block)
|
|
|
|
if block
|
|
|
|
CSV::Reader.parse(str_or_readable, fs, rs) do |row|
|
|
|
|
yield(row)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
CSV::Reader.create(str_or_readable, fs, rs).collect { |row| row }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# Parse a line from given string. Bear in mind it parses ONE LINE. Rest of
|
|
|
|
# the string is ignored for example "a,b\r\nc,d" => ['a', 'b'] and the
|
|
|
|
# second line 'c,d' is ignored.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
2004-02-16 17:41:49 +03:00
|
|
|
# If you don't know whether a target string to parse is exactly 1 line or
|
|
|
|
# not, use CSV.parse_row instead of this method.
|
2004-05-20 21:24:04 +04:00
|
|
|
def CSV.parse_line(src, fs = nil, rs = nil)
|
|
|
|
fs ||= ','
|
|
|
|
if fs.is_a?(Fixnum)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
fs = fs.chr
|
|
|
|
end
|
|
|
|
if !rs.nil? and rs.is_a?(Fixnum)
|
|
|
|
rs = rs.chr
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
idx = 0
|
|
|
|
res_type = :DT_COLSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
row = []
|
2003-06-19 20:08:52 +04:00
|
|
|
begin
|
2004-05-20 21:24:04 +04:00
|
|
|
while res_type == :DT_COLSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
res_type, idx, cell = parse_body(src, idx, fs, rs)
|
|
|
|
row << cell
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
rescue IllegalFormatError
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
return []
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
row
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# Create a line from cells. each cell is stringified by to_s.
|
2004-05-20 21:24:04 +04:00
|
|
|
def CSV.generate_line(row, fs = nil, rs = nil)
|
|
|
|
if row.size == 0
|
2003-06-19 20:08:52 +04:00
|
|
|
return ''
|
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
fs ||= ','
|
|
|
|
if fs.is_a?(Fixnum)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
fs = fs.chr
|
|
|
|
end
|
|
|
|
if !rs.nil? and rs.is_a?(Fixnum)
|
|
|
|
rs = rs.chr
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
res_type = :DT_COLSEP
|
|
|
|
result_str = ''
|
|
|
|
idx = 0
|
|
|
|
while true
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_body(row[idx], result_str, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
idx += 1
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
if (idx == row.size)
|
2003-06-19 20:08:52 +04:00
|
|
|
break
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_separator(:DT_COLSEP, result_str, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
result_str
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# Parse a line from string. Consider using CSV.parse_line instead.
|
|
|
|
# To parse lines in CSV string, see EXAMPLE below.
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# src = "a,b\r\nc,d\r\ne,f"
|
|
|
|
# idx = 0
|
|
|
|
# begin
|
|
|
|
# parsed = []
|
|
|
|
# parsed_cells, idx = CSV.parse_row(src, idx, parsed)
|
|
|
|
# puts "Parsed #{ parsed_cells } cells."
|
|
|
|
# p parsed
|
|
|
|
# end while parsed_cells > 0
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# ARGS
|
|
|
|
# src: a CSV data to be parsed. Must respond '[](idx)'.
|
|
|
|
# src[](idx) must return a char. (Not a string such as 'a', but 97).
|
|
|
|
# src[](idx_out_of_bounds) must return nil. A String satisfies this
|
|
|
|
# requirement.
|
|
|
|
# idx: index of parsing location of 'src'. 0 origin.
|
2004-05-26 18:30:30 +04:00
|
|
|
# out_dev: buffer for parsed cells. Must respond '<<(aString)'.
|
2003-06-19 20:08:52 +04:00
|
|
|
# col_sep: Column separator. ?, by default. If you want to separate
|
|
|
|
# fields with semicolon, give ?; here.
|
2003-09-15 14:07:42 +04:00
|
|
|
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
|
|
|
|
# want to separate records with \r, give ?\r here.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# RETURNS
|
|
|
|
# parsed_cells: num of parsed cells.
|
|
|
|
# idx: index of next parsing location of 'src'.
|
|
|
|
#
|
2004-05-20 21:24:04 +04:00
|
|
|
def CSV.parse_row(src, idx, out_dev, fs = nil, rs = nil)
|
|
|
|
fs ||= ','
|
|
|
|
if fs.is_a?(Fixnum)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
fs = fs.chr
|
|
|
|
end
|
|
|
|
if !rs.nil? and rs.is_a?(Fixnum)
|
|
|
|
rs = rs.chr
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
idx_backup = idx
|
|
|
|
parsed_cells = 0
|
|
|
|
res_type = :DT_COLSEP
|
|
|
|
begin
|
2004-05-20 21:24:04 +04:00
|
|
|
while res_type != :DT_ROWSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
res_type, idx, cell = parse_body(src, idx, fs, rs)
|
2004-05-20 21:24:04 +04:00
|
|
|
if res_type == :DT_EOS
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
if idx == idx_backup #((parsed_cells == 0) and cell.nil?)
|
2003-06-19 20:08:52 +04:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
res_type = :DT_ROWSEP
|
|
|
|
end
|
|
|
|
parsed_cells += 1
|
|
|
|
out_dev << cell
|
|
|
|
end
|
|
|
|
rescue IllegalFormatError
|
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
return parsed_cells, idx
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# Convert a line from cells data to string. Consider using CSV.generate_line
|
|
|
|
# instead. To generate multi-row CSV string, see EXAMPLE below.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# EXAMPLE
|
2004-05-26 18:30:30 +04:00
|
|
|
# row1 = ['a', 'b']
|
|
|
|
# row2 = ['c', 'd']
|
|
|
|
# row3 = ['e', 'f']
|
2003-06-19 20:08:52 +04:00
|
|
|
# src = [row1, row2, row3]
|
|
|
|
# buf = ''
|
|
|
|
# src.each do |row|
|
|
|
|
# parsed_cells = CSV.generate_row(row, 2, buf)
|
|
|
|
# puts "Created #{ parsed_cells } cells."
|
|
|
|
# end
|
|
|
|
# p buf
|
2004-02-16 17:41:49 +03:00
|
|
|
#
|
|
|
|
# ARGS
|
2004-05-26 18:30:30 +04:00
|
|
|
# src: an Array of String to be converted to CSV string. Must respond to
|
|
|
|
# 'size' and '[](idx)'. src[idx] must return String.
|
2004-02-16 17:41:49 +03:00
|
|
|
# cells: num of cells in a line.
|
|
|
|
# out_dev: buffer for generated CSV string. Must respond to '<<(string)'.
|
|
|
|
# col_sep: Column separator. ?, by default. If you want to separate
|
|
|
|
# fields with semicolon, give ?; here.
|
|
|
|
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
|
|
|
|
# want to separate records with \r, give ?\r here.
|
|
|
|
#
|
|
|
|
# RETURNS
|
|
|
|
# parsed_cells: num of converted cells.
|
|
|
|
#
|
2004-05-20 21:24:04 +04:00
|
|
|
def CSV.generate_row(src, cells, out_dev, fs = nil, rs = nil)
|
|
|
|
fs ||= ','
|
|
|
|
if fs.is_a?(Fixnum)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
fs = fs.chr
|
|
|
|
end
|
|
|
|
if !rs.nil? and rs.is_a?(Fixnum)
|
|
|
|
rs = rs.chr
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
src_size = src.size
|
|
|
|
if (src_size == 0)
|
|
|
|
if cells == 0
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_separator(:DT_ROWSEP, out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
res_type = :DT_COLSEP
|
|
|
|
parsed_cells = 0
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_body(src[parsed_cells], out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
parsed_cells += 1
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
while ((parsed_cells < cells) and (parsed_cells != src_size))
|
|
|
|
generate_separator(:DT_COLSEP, out_dev, fs, rs)
|
|
|
|
generate_body(src[parsed_cells], out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
parsed_cells += 1
|
|
|
|
end
|
|
|
|
if (parsed_cells == cells)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_separator(:DT_ROWSEP, out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
else
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
generate_separator(:DT_COLSEP, out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-02-16 17:41:49 +03:00
|
|
|
parsed_cells
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
|
|
|
# Private class methods.
|
2004-02-16 17:41:49 +03:00
|
|
|
class << self
|
|
|
|
private
|
|
|
|
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def open_reader(path, mode, fs, rs, &block)
|
|
|
|
file = File.open(path, mode)
|
2004-02-16 17:41:49 +03:00
|
|
|
if block
|
|
|
|
begin
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
CSV::Reader.parse(file, fs, rs) do |row|
|
2004-02-16 17:41:49 +03:00
|
|
|
yield(row)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
else
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
reader = CSV::Reader.create(file, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
reader.close_on_terminate
|
|
|
|
reader
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def open_writer(path, mode, fs, rs, &block)
|
|
|
|
file = File.open(path, mode)
|
2004-02-16 17:41:49 +03:00
|
|
|
if block
|
|
|
|
begin
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
CSV::Writer.generate(file, fs, rs) do |writer|
|
2004-02-16 17:41:49 +03:00
|
|
|
yield(writer)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
else
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
writer = CSV::Writer.create(file, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
writer.close_on_terminate
|
|
|
|
writer
|
|
|
|
end
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def parse_body(src, idx, fs, rs)
|
|
|
|
fs_str = fs
|
|
|
|
fs_size = fs_str.size
|
|
|
|
rs_str = rs || "\n"
|
|
|
|
rs_size = rs_str.size
|
2004-05-20 21:24:04 +04:00
|
|
|
fs_idx = rs_idx = 0
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
cell = ''
|
2003-06-19 20:08:52 +04:00
|
|
|
state = :ST_START
|
2004-05-20 21:24:04 +04:00
|
|
|
quoted = cr = false
|
2003-06-19 20:08:52 +04:00
|
|
|
c = nil
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
last_idx = idx
|
2004-05-20 21:24:04 +04:00
|
|
|
while c = src[idx]
|
|
|
|
unless quoted
|
|
|
|
fschar = (c == fs_str[fs_idx])
|
|
|
|
rschar = (c == rs_str[rs_idx])
|
|
|
|
# simple 1 char backtrack
|
|
|
|
if !fschar and c == fs_str[0]
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
fs_idx = 0
|
2004-05-20 21:24:04 +04:00
|
|
|
fschar = true
|
|
|
|
if state == :ST_START
|
|
|
|
state = :ST_DATA
|
|
|
|
elsif state == :ST_QUOTE
|
|
|
|
raise IllegalFormatError
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
if !rschar and c == rs_str[0]
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
rs_idx = 0
|
2004-05-20 21:24:04 +04:00
|
|
|
rschar = true
|
|
|
|
if state == :ST_START
|
|
|
|
state = :ST_DATA
|
|
|
|
elsif state == :ST_QUOTE
|
|
|
|
raise IllegalFormatError
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
end
|
|
|
|
if c == ?"
|
|
|
|
fs_idx = rs_idx = 0
|
|
|
|
if cr
|
|
|
|
raise IllegalFormatError
|
|
|
|
end
|
|
|
|
cell << src[last_idx, (idx - last_idx)]
|
|
|
|
last_idx = idx
|
|
|
|
if state == :ST_DATA
|
2003-06-19 20:08:52 +04:00
|
|
|
if quoted
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
last_idx += 1
|
2003-06-19 20:08:52 +04:00
|
|
|
quoted = false
|
|
|
|
state = :ST_QUOTE
|
|
|
|
else
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
elsif state == :ST_QUOTE
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
cell << c.chr
|
|
|
|
last_idx += 1
|
2003-06-19 20:08:52 +04:00
|
|
|
quoted = true
|
|
|
|
state = :ST_DATA
|
|
|
|
else # :ST_START
|
|
|
|
quoted = true
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
last_idx += 1
|
2003-06-19 20:08:52 +04:00
|
|
|
state = :ST_DATA
|
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
elsif fschar or rschar
|
|
|
|
if fschar
|
|
|
|
fs_idx += 1
|
|
|
|
end
|
|
|
|
if rschar
|
|
|
|
rs_idx += 1
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
sep = nil
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
if fs_idx == fs_size
|
2004-05-20 21:24:04 +04:00
|
|
|
if state == :ST_START and rs_idx > 0 and fs_idx < rs_idx
|
|
|
|
state = :ST_DATA
|
|
|
|
end
|
|
|
|
cell << src[last_idx, (idx - last_idx - (fs_size - 1))]
|
|
|
|
last_idx = idx
|
|
|
|
fs_idx = rs_idx = 0
|
2003-06-19 20:08:52 +04:00
|
|
|
if cr
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
sep = :DT_COLSEP
|
|
|
|
elsif rs_idx == rs_size
|
|
|
|
if state == :ST_START and fs_idx > 0 and rs_idx < fs_idx
|
|
|
|
state = :ST_DATA
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
if !(rs.nil? and cr)
|
|
|
|
cell << src[last_idx, (idx - last_idx - (rs_size - 1))]
|
|
|
|
last_idx = idx
|
|
|
|
end
|
|
|
|
fs_idx = rs_idx = 0
|
|
|
|
sep = :DT_ROWSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
if sep
|
|
|
|
if state == :ST_DATA
|
|
|
|
return sep, idx + 1, cell;
|
|
|
|
elsif state == :ST_QUOTE
|
|
|
|
return sep, idx + 1, cell;
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
else # :ST_START
|
2004-05-20 21:24:04 +04:00
|
|
|
return sep, idx + 1, nil
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
end
|
|
|
|
elsif rs.nil? and c == ?\r
|
|
|
|
# special \r treatment for backward compatibility
|
2004-05-20 21:24:04 +04:00
|
|
|
fs_idx = rs_idx = 0
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
if cr
|
|
|
|
raise IllegalFormatError
|
|
|
|
end
|
|
|
|
cell << src[last_idx, (idx - last_idx)]
|
|
|
|
last_idx = idx
|
|
|
|
if quoted
|
|
|
|
state = :ST_DATA
|
|
|
|
else
|
|
|
|
cr = true
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
else
|
2004-05-20 21:24:04 +04:00
|
|
|
fs_idx = rs_idx = 0
|
|
|
|
if state == :ST_DATA or state == :ST_START
|
2003-06-19 20:08:52 +04:00
|
|
|
if cr
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
state = :ST_DATA
|
|
|
|
else # :ST_QUOTE
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
idx += 1
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
2004-05-20 21:24:04 +04:00
|
|
|
if state == :ST_START
|
|
|
|
if fs_idx > 0 or rs_idx > 0
|
|
|
|
state = :ST_DATA
|
|
|
|
else
|
|
|
|
return :DT_EOS, idx, nil
|
|
|
|
end
|
2003-06-19 20:08:52 +04:00
|
|
|
elsif quoted
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
2003-06-19 20:08:52 +04:00
|
|
|
elsif cr
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise IllegalFormatError
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
cell << src[last_idx, (idx - last_idx)]
|
|
|
|
last_idx = idx
|
|
|
|
return :DT_EOS, idx, cell
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
|
|
|
def generate_body(cell, out_dev, fs, rs)
|
|
|
|
if cell.nil?
|
|
|
|
# empty
|
|
|
|
else
|
2004-05-20 21:24:04 +04:00
|
|
|
cell = cell.to_s
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
row_data = cell.dup
|
|
|
|
if (row_data.gsub!('"', '""') or
|
|
|
|
row_data.index(fs) or
|
|
|
|
(rs and row_data.index(rs)) or
|
|
|
|
(/[\r\n]/ =~ row_data) or
|
|
|
|
(cell.empty?))
|
2003-06-19 20:08:52 +04:00
|
|
|
out_dev << '"' << row_data << '"'
|
|
|
|
else
|
|
|
|
out_dev << row_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
|
|
|
def generate_separator(type, out_dev, fs, rs)
|
2003-06-19 20:08:52 +04:00
|
|
|
case type
|
|
|
|
when :DT_COLSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
out_dev << fs
|
2003-06-19 20:08:52 +04:00
|
|
|
when :DT_ROWSEP
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
out_dev << (rs || "\n")
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# CSV formatted string/stream reader.
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
# read CSV lines untill the first column is 'stop'.
|
2004-02-16 17:41:49 +03:00
|
|
|
#
|
|
|
|
# CSV::Reader.parse(File.open('bigdata', 'rb')) do |row|
|
|
|
|
# p row
|
|
|
|
# break if !row[0].is_null && row[0].data == 'stop'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class Reader
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
# Parse CSV data and get lines. Given block is called for each parsed row.
|
|
|
|
# Block value is always nil. Rows are not cached for performance reason.
|
2004-05-26 18:30:30 +04:00
|
|
|
def Reader.parse(str_or_readable, fs = ',', rs = nil, &block)
|
|
|
|
reader = Reader.create(str_or_readable, fs, rs)
|
|
|
|
if block
|
|
|
|
reader.each do |row|
|
|
|
|
yield(row)
|
|
|
|
end
|
|
|
|
reader.close
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
reader
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns reader instance.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def Reader.create(str_or_readable, fs = ',', rs = nil)
|
2004-02-16 17:41:49 +03:00
|
|
|
case str_or_readable
|
|
|
|
when IO
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
IOReader.new(str_or_readable, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
when String
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
StringReader.new(str_or_readable, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
else
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
IOReader.new(str_or_readable, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
while true
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
row = []
|
2004-02-16 17:41:49 +03:00
|
|
|
parsed_cells = get_row(row)
|
|
|
|
if parsed_cells == 0
|
|
|
|
break
|
|
|
|
end
|
|
|
|
yield(row)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def shift
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
row = []
|
2004-02-16 17:41:49 +03:00
|
|
|
parsed_cells = get_row(row)
|
|
|
|
row
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
terminate
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def initialize(dev)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise RuntimeError.new('Do not instanciate this class directly.')
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_row(row)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise NotImplementedError.new('Method get_row must be defined in a derived class.')
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def terminate
|
|
|
|
# Define if needed.
|
|
|
|
end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
|
|
|
|
class StringReader < Reader
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def initialize(string, fs = ',', rs = nil)
|
|
|
|
@fs = fs
|
|
|
|
@rs = rs
|
2004-02-16 17:41:49 +03:00
|
|
|
@dev = string
|
|
|
|
@idx = 0
|
|
|
|
if @dev[0, 3] == "\xef\xbb\xbf"
|
|
|
|
@idx += 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_row(row)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
parsed_cells, next_idx = CSV.parse_row(@dev, @idx, row, @fs, @rs)
|
|
|
|
if parsed_cells == 0 and next_idx == 0 and @idx != @dev.size
|
2004-02-16 17:41:49 +03:00
|
|
|
raise IllegalFormatError.new
|
|
|
|
end
|
|
|
|
@idx = next_idx
|
|
|
|
parsed_cells
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class IOReader < Reader
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def initialize(io, fs = ',', rs = nil)
|
2004-02-16 17:41:49 +03:00
|
|
|
@io = io
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
@fs = fs
|
|
|
|
@rs = rs
|
2004-02-16 17:41:49 +03:00
|
|
|
@dev = CSV::IOBuf.new(@io)
|
|
|
|
@idx = 0
|
|
|
|
if @dev[0] == 0xef and @dev[1] == 0xbb and @dev[2] == 0xbf
|
|
|
|
@idx += 3
|
|
|
|
end
|
|
|
|
@close_on_terminate = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tell this reader to close the IO when terminated (Triggered by invoking
|
|
|
|
# CSV::IOReader#close).
|
|
|
|
def close_on_terminate
|
|
|
|
@close_on_terminate = true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_row(row)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
parsed_cells, next_idx = CSV.parse_row(@dev, @idx, row, @fs, @rs)
|
|
|
|
if parsed_cells == 0 and next_idx == 0 and !@dev.is_eos?
|
2004-02-16 17:41:49 +03:00
|
|
|
raise IllegalFormatError.new
|
|
|
|
end
|
|
|
|
dropped = @dev.drop(next_idx)
|
|
|
|
@idx = next_idx - dropped
|
|
|
|
parsed_cells
|
|
|
|
end
|
|
|
|
|
|
|
|
def terminate
|
|
|
|
if @close_on_terminate
|
|
|
|
@io.close
|
|
|
|
end
|
|
|
|
|
|
|
|
if @dev
|
|
|
|
@dev.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# CSV formatted string/stream writer.
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# Write rows to 'csvout' file.
|
|
|
|
#
|
|
|
|
# outfile = File.open('csvout', 'wb')
|
|
|
|
# CSV::Writer.generate(outfile) do |csv|
|
|
|
|
# csv << ['c1', nil, '', '"', "\r\n", 'c2']
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# outfile.close
|
|
|
|
#
|
|
|
|
class Writer
|
2004-05-26 18:30:30 +04:00
|
|
|
# Given block is called with the writer instance. str_or_writable must
|
|
|
|
# handle '<<(string)'.
|
|
|
|
def Writer.generate(str_or_writable, fs = ',', rs = nil, &block)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
writer = Writer.create(str_or_writable, fs, rs)
|
2004-05-26 18:30:30 +04:00
|
|
|
if block
|
|
|
|
yield(writer)
|
|
|
|
writer.close
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
writer
|
|
|
|
end
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# str_or_writable must handle '<<(string)'.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def Writer.create(str_or_writable, fs = ',', rs = nil)
|
|
|
|
BasicWriter.new(str_or_writable, fs, rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# dump CSV stream to the device. argument must be an Array of String.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def <<(row)
|
|
|
|
CSV.generate_row(row, row.size, @dev, @fs, @rs)
|
2004-02-16 17:41:49 +03:00
|
|
|
self
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
alias add_row <<
|
2004-02-16 17:41:49 +03:00
|
|
|
|
|
|
|
def close
|
|
|
|
terminate
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def initialize(dev)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise RuntimeError.new('Do not instanciate this class directly.')
|
2004-02-16 17:41:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def terminate
|
|
|
|
# Define if needed.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class BasicWriter < Writer
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def initialize(str_or_writable, fs = ',', rs = nil)
|
|
|
|
@fs = fs
|
|
|
|
@rs = rs
|
2004-02-16 17:41:49 +03:00
|
|
|
@dev = str_or_writable
|
|
|
|
@close_on_terminate = false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tell this writer to close the IO when terminated (Triggered by invoking
|
|
|
|
# CSV::BasicWriter#close).
|
|
|
|
def close_on_terminate
|
|
|
|
@close_on_terminate = true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def terminate
|
|
|
|
if @close_on_terminate
|
|
|
|
@dev.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
private
|
2004-02-16 17:41:49 +03:00
|
|
|
|
|
|
|
# Buffered stream.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# EXAMPLE 1 -- an IO.
|
|
|
|
# class MyBuf < StreamBuf
|
|
|
|
# # Do initialize myself before a super class. Super class might call my
|
|
|
|
# # method 'read'. (Could be awful for C++ user. :-)
|
|
|
|
# def initialize(s)
|
|
|
|
# @s = s
|
|
|
|
# super()
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # define my own 'read' method.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
# # CAUTION: Returning nil means EnfOfStream.
|
2003-06-19 20:08:52 +04:00
|
|
|
# def read(size)
|
|
|
|
# @s.read(size)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # release buffers. in Ruby which has GC, you do not have to call this...
|
|
|
|
# def terminate
|
|
|
|
# @s = nil
|
|
|
|
# super()
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# buf = MyBuf.new(STDIN)
|
|
|
|
# my_str = ''
|
|
|
|
# p buf[0, 0] # => '' (null string)
|
|
|
|
# p buf[0] # => 97 (char code of 'a')
|
|
|
|
# p buf[0, 1] # => 'a'
|
|
|
|
# my_str = buf[0, 5]
|
|
|
|
# p my_str # => 'abcde' (5 chars)
|
|
|
|
# p buf[0, 6] # => "abcde\n" (6 chars)
|
|
|
|
# p buf[0, 7] # => "abcde\n" (6 chars)
|
|
|
|
# p buf.drop(3) # => 3 (dropped chars)
|
|
|
|
# p buf.get(0, 2) # => 'de' (2 chars)
|
|
|
|
# p buf.is_eos? # => false (is not EOS here)
|
|
|
|
# p buf.drop(5) # => 3 (dropped chars)
|
|
|
|
# p buf.is_eos? # => true (is EOS here)
|
|
|
|
# p buf[0] # => nil (is EOS here)
|
|
|
|
#
|
|
|
|
# EXAMPLE 2 -- String.
|
|
|
|
# This is a conceptual example. No pros with this.
|
2004-02-16 17:41:49 +03:00
|
|
|
#
|
2003-06-19 20:08:52 +04:00
|
|
|
# class StrBuf < StreamBuf
|
|
|
|
# def initialize(s)
|
|
|
|
# @str = s
|
|
|
|
# @idx = 0
|
|
|
|
# super()
|
|
|
|
# end
|
2004-02-16 17:41:49 +03:00
|
|
|
#
|
2003-06-19 20:08:52 +04:00
|
|
|
# def read(size)
|
|
|
|
# str = @str[@idx, size]
|
|
|
|
# @idx += str.size
|
|
|
|
# str
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
class StreamBuf
|
2004-02-16 17:41:49 +03:00
|
|
|
# get a char or a partial string from the stream.
|
|
|
|
# idx: index of a string to specify a start point of a string to get.
|
|
|
|
# unlike String instance, idx < 0 returns nil.
|
|
|
|
# n: size of a string to get.
|
|
|
|
# returns char at idx if n == nil.
|
|
|
|
# returns a partial string, from idx to (idx + n) if n != nil. at EOF,
|
|
|
|
# the string size could not equal to arg n.
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
def [](idx, n = nil)
|
2003-06-19 20:08:52 +04:00
|
|
|
if idx < 0
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if (idx_is_eos?(idx))
|
|
|
|
if n and (@offset + idx == buf_size(@cur_buf))
|
|
|
|
# Like a String, 'abc'[4, 1] returns nil and
|
|
|
|
# 'abc'[3, 1] returns '' not nil.
|
|
|
|
return ''
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
my_buf = @cur_buf
|
|
|
|
my_offset = @offset
|
|
|
|
next_idx = idx
|
|
|
|
while (my_offset + next_idx >= buf_size(my_buf))
|
|
|
|
if (my_buf == @buf_tail_idx)
|
|
|
|
unless add_buf
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next_idx = my_offset + next_idx - buf_size(my_buf)
|
|
|
|
my_buf += 1
|
|
|
|
my_offset = 0
|
|
|
|
end
|
|
|
|
loc = my_offset + next_idx
|
|
|
|
if !n
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
return @buf_list[my_buf][loc] # Fixnum of char code.
|
2003-06-19 20:08:52 +04:00
|
|
|
elsif (loc + n - 1 < buf_size(my_buf))
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
return @buf_list[my_buf][loc, n] # String.
|
2003-06-19 20:08:52 +04:00
|
|
|
else # should do loop insted of (tail) recursive call...
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
res = @buf_list[my_buf][loc, BufSize]
|
2003-06-19 20:08:52 +04:00
|
|
|
size_added = buf_size(my_buf) - loc
|
|
|
|
if size_added > 0
|
|
|
|
idx += size_added
|
|
|
|
n -= size_added
|
|
|
|
ret = self[idx, n]
|
|
|
|
if ret
|
|
|
|
res << ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias get []
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# drop a string from the stream.
|
|
|
|
# returns dropped size. at EOF, dropped size might not equals to arg n.
|
|
|
|
# Once you drop the head of the stream, access to the dropped part via []
|
|
|
|
# or get returns nil.
|
2003-06-19 20:08:52 +04:00
|
|
|
def drop(n)
|
|
|
|
if is_eos?
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
size_dropped = 0
|
|
|
|
while (n > 0)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
if !@is_eos or (@cur_buf != @buf_tail_idx)
|
2003-06-19 20:08:52 +04:00
|
|
|
if (@offset + n < buf_size(@cur_buf))
|
|
|
|
size_dropped += n
|
|
|
|
@offset += n
|
|
|
|
n = 0
|
|
|
|
else
|
|
|
|
size = buf_size(@cur_buf) - @offset
|
|
|
|
size_dropped += size
|
|
|
|
n -= size
|
|
|
|
@offset = 0
|
|
|
|
unless rel_buf
|
|
|
|
unless add_buf
|
|
|
|
break
|
|
|
|
end
|
|
|
|
@cur_buf = @buf_tail_idx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
size_dropped
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def is_eos?
|
|
|
|
return idx_is_eos?(0)
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2004-03-29 11:54:38 +04:00
|
|
|
# WARN: Do not instantiate this class directly. Define your own class
|
2004-02-16 17:41:49 +03:00
|
|
|
# which derives this class and define 'read' instance method.
|
2003-06-19 20:08:52 +04:00
|
|
|
def initialize
|
|
|
|
@buf_list = []
|
|
|
|
@cur_buf = @buf_tail_idx = -1
|
|
|
|
@offset = 0
|
|
|
|
@is_eos = false
|
|
|
|
add_buf
|
|
|
|
@cur_buf = @buf_tail_idx
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
protected
|
2004-02-16 17:41:49 +03:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def terminate
|
|
|
|
while (rel_buf); end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
# protected method 'read' must be defined in derived classes.
|
|
|
|
# CAUTION: Returning a string which size is not equal to 'size' means
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
# EnfOfStream. When it is not at EOS, you must block the callee, try to
|
2004-02-16 17:41:49 +03:00
|
|
|
# read and return the sized string.
|
2003-06-19 20:08:52 +04:00
|
|
|
def read(size) # raise EOFError
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
raise NotImplementedError.new('Method read must be defined in a derived class.')
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
private
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def buf_size(idx)
|
|
|
|
@buf_list[idx].size
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_buf
|
|
|
|
if @is_eos
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
str_read = read(BufSize)
|
|
|
|
rescue EOFError
|
|
|
|
str_read = nil
|
|
|
|
rescue
|
|
|
|
terminate
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
if str_read.nil?
|
|
|
|
@is_eos = true
|
|
|
|
@buf_list.push('')
|
|
|
|
@buf_tail_idx += 1
|
|
|
|
false
|
|
|
|
else
|
|
|
|
@buf_list.push(str_read)
|
|
|
|
@buf_tail_idx += 1
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def rel_buf
|
|
|
|
if (@cur_buf < 0)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
@buf_list[@cur_buf] = nil
|
|
|
|
if (@cur_buf == @buf_tail_idx)
|
|
|
|
@cur_buf = -1
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
@cur_buf += 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def idx_is_eos?(idx)
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
(@is_eos and ((@cur_buf < 0) or (@cur_buf == @buf_tail_idx)))
|
2003-06-19 20:08:52 +04:00
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
BufSize = 1024 * 8
|
|
|
|
end
|
|
|
|
|
2004-02-16 17:41:49 +03:00
|
|
|
# Buffered IO.
|
2003-06-19 20:08:52 +04:00
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# # File 'bigdata' could be a giga-byte size one!
|
|
|
|
# buf = CSV::IOBuf.new(File.open('bigdata', 'rb'))
|
|
|
|
# CSV::Reader.new(buf).each do |row|
|
|
|
|
# p row
|
|
|
|
# break if row[0].data == 'admin'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class IOBuf < StreamBuf
|
|
|
|
def initialize(s)
|
|
|
|
@s = s
|
|
|
|
super()
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def close
|
|
|
|
terminate
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2004-02-16 17:41:49 +03:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def read(size)
|
|
|
|
@s.read(size)
|
|
|
|
end
|
* lib/csv.rb: writes lines with "\n" when row separator is not given.
formerly it was "\r\n".
* lib/csv.rb: [CAUTION] API change
* CSV::Row removed. a row is represented as just an Array. since
CSV::Row was a subclass of Array, it won't hurt almost all programs
except one which depended CSV::Row#match.
* CSV::Cell removed. a cell is represented as just a String or
nil(NULL). this change will cause widespread destruction.
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.is_null # Cell#is_null
p "(NULL)"
else
p cell.data # Cell#data
end
end
end
must be just;
CSV.open("foo.csv", "r") do |row|
row.each do |cell|
if cell.nil?
p "(NULL)"
else
p cell
end
end
end
* lib/csv.rb: [CAUTION] record separator(CR, LF, CR+LF) behavior
change. CSV.open, CSV.parse, and CSV,generate now do not force
opened file binmode. formerly it set binmode explicitly.
with CSV.open, binmode of opened file depends the given mode
parameter "r", "w", "rb", and "wb". CSV.parse and CSV.generate open
file with "r" and "w".
setting mode properly is user's responsibility now.
* lib/csv.rb: accepts String as a fs (field separator/column separator)
and rs (record separator/row separator)
* lib/csv.rb: added CSV.foreach(path, rs = nil, &block). CSV.foreach
now does not handle "| cmd" as a path different from IO.foreach.
needed?
* test/csv/test_csv.rb: updated.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-05-18 16:34:33 +04:00
|
|
|
|
2003-06-19 20:08:52 +04:00
|
|
|
def terminate
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|