Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: csv.rb
# CSV -- module for generating/parsing CSV data.
[0] Fix | Delete
# Copyright (C) 2000-2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>.
[1] Fix | Delete
[2] Fix | Delete
# $Id: csv.rb 11708 2007-02-12 23:01:19Z shyouhei $
[3] Fix | Delete
[4] Fix | Delete
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
[5] Fix | Delete
# redistribute it and/or modify it under the same terms of Ruby's license;
[6] Fix | Delete
# either the dual license version in 2003, or any later version.
[7] Fix | Delete
[8] Fix | Delete
[9] Fix | Delete
class CSV
[10] Fix | Delete
class IllegalFormatError < RuntimeError; end
[11] Fix | Delete
[12] Fix | Delete
# deprecated
[13] Fix | Delete
class Cell < String
[14] Fix | Delete
def initialize(data = "", is_null = false)
[15] Fix | Delete
super(is_null ? "" : data)
[16] Fix | Delete
end
[17] Fix | Delete
[18] Fix | Delete
def data
[19] Fix | Delete
to_s
[20] Fix | Delete
end
[21] Fix | Delete
end
[22] Fix | Delete
[23] Fix | Delete
# deprecated
[24] Fix | Delete
class Row < Array
[25] Fix | Delete
end
[26] Fix | Delete
[27] Fix | Delete
# Open a CSV formatted file for reading or writing.
[28] Fix | Delete
#
[29] Fix | Delete
# For reading.
[30] Fix | Delete
#
[31] Fix | Delete
# EXAMPLE 1
[32] Fix | Delete
# CSV.open('csvfile.csv', 'r') do |row|
[33] Fix | Delete
# p row
[34] Fix | Delete
# end
[35] Fix | Delete
#
[36] Fix | Delete
# EXAMPLE 2
[37] Fix | Delete
# reader = CSV.open('csvfile.csv', 'r')
[38] Fix | Delete
# row1 = reader.shift
[39] Fix | Delete
# row2 = reader.shift
[40] Fix | Delete
# if row2.empty?
[41] Fix | Delete
# p 'row2 not find.'
[42] Fix | Delete
# end
[43] Fix | Delete
# reader.close
[44] Fix | Delete
#
[45] Fix | Delete
# ARGS
[46] Fix | Delete
# filename: filename to parse.
[47] Fix | Delete
# col_sep: Column separator. ?, by default. If you want to separate
[48] Fix | Delete
# fields with semicolon, give ?; here.
[49] Fix | Delete
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
[50] Fix | Delete
# want to separate records with \r, give ?\r here.
[51] Fix | Delete
#
[52] Fix | Delete
# RETURNS
[53] Fix | Delete
# reader instance. To get parse result, see CSV::Reader#each.
[54] Fix | Delete
#
[55] Fix | Delete
#
[56] Fix | Delete
# For writing.
[57] Fix | Delete
#
[58] Fix | Delete
# EXAMPLE 1
[59] Fix | Delete
# CSV.open('csvfile.csv', 'w') do |writer|
[60] Fix | Delete
# writer << ['r1c1', 'r1c2']
[61] Fix | Delete
# writer << ['r2c1', 'r2c2']
[62] Fix | Delete
# writer << [nil, nil]
[63] Fix | Delete
# end
[64] Fix | Delete
#
[65] Fix | Delete
# EXAMPLE 2
[66] Fix | Delete
# writer = CSV.open('csvfile.csv', 'w')
[67] Fix | Delete
# writer << ['r1c1', 'r1c2'] << ['r2c1', 'r2c2'] << [nil, nil]
[68] Fix | Delete
# writer.close
[69] Fix | Delete
#
[70] Fix | Delete
# ARGS
[71] Fix | Delete
# filename: filename to generate.
[72] Fix | Delete
# col_sep: Column separator. ?, by default. If you want to separate
[73] Fix | Delete
# fields with semicolon, give ?; here.
[74] Fix | Delete
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
[75] Fix | Delete
# want to separate records with \r, give ?\r here.
[76] Fix | Delete
#
[77] Fix | Delete
# RETURNS
[78] Fix | Delete
# writer instance. See CSV::Writer#<< and CSV::Writer#add_row to know how
[79] Fix | Delete
# to generate CSV string.
[80] Fix | Delete
#
[81] Fix | Delete
def CSV.open(path, mode, fs = nil, rs = nil, &block)
[82] Fix | Delete
if mode == 'r' or mode == 'rb'
[83] Fix | Delete
open_reader(path, mode, fs, rs, &block)
[84] Fix | Delete
elsif mode == 'w' or mode == 'wb'
[85] Fix | Delete
open_writer(path, mode, fs, rs, &block)
[86] Fix | Delete
else
[87] Fix | Delete
raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
[88] Fix | Delete
end
[89] Fix | Delete
end
[90] Fix | Delete
[91] Fix | Delete
def CSV.foreach(path, rs = nil, &block)
[92] Fix | Delete
open_reader(path, 'r', ',', rs, &block)
[93] Fix | Delete
end
[94] Fix | Delete
[95] Fix | Delete
def CSV.read(path, length = nil, offset = nil)
[96] Fix | Delete
CSV.parse(IO.read(path, length, offset))
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
def CSV.readlines(path, rs = nil)
[100] Fix | Delete
reader = open_reader(path, 'r', ',', rs)
[101] Fix | Delete
begin
[102] Fix | Delete
reader.collect { |row| row }
[103] Fix | Delete
ensure
[104] Fix | Delete
reader.close
[105] Fix | Delete
end
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
def CSV.generate(path, fs = nil, rs = nil, &block)
[109] Fix | Delete
open_writer(path, 'w', fs, rs, &block)
[110] Fix | Delete
end
[111] Fix | Delete
[112] Fix | Delete
# Parse lines from given string or stream. Return rows as an Array of Arrays.
[113] Fix | Delete
def CSV.parse(str_or_readable, fs = nil, rs = nil, &block)
[114] Fix | Delete
if File.exist?(str_or_readable)
[115] Fix | Delete
STDERR.puts("CSV.parse(filename) is deprecated." +
[116] Fix | Delete
" Use CSV.open(filename, 'r') instead.")
[117] Fix | Delete
return open_reader(str_or_readable, 'r', fs, rs, &block)
[118] Fix | Delete
end
[119] Fix | Delete
if block
[120] Fix | Delete
CSV::Reader.parse(str_or_readable, fs, rs) do |row|
[121] Fix | Delete
yield(row)
[122] Fix | Delete
end
[123] Fix | Delete
nil
[124] Fix | Delete
else
[125] Fix | Delete
CSV::Reader.create(str_or_readable, fs, rs).collect { |row| row }
[126] Fix | Delete
end
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
# Parse a line from given string. Bear in mind it parses ONE LINE. Rest of
[130] Fix | Delete
# the string is ignored for example "a,b\r\nc,d" => ['a', 'b'] and the
[131] Fix | Delete
# second line 'c,d' is ignored.
[132] Fix | Delete
#
[133] Fix | Delete
# If you don't know whether a target string to parse is exactly 1 line or
[134] Fix | Delete
# not, use CSV.parse_row instead of this method.
[135] Fix | Delete
def CSV.parse_line(src, fs = nil, rs = nil)
[136] Fix | Delete
fs ||= ','
[137] Fix | Delete
if fs.is_a?(Fixnum)
[138] Fix | Delete
fs = fs.chr
[139] Fix | Delete
end
[140] Fix | Delete
if !rs.nil? and rs.is_a?(Fixnum)
[141] Fix | Delete
rs = rs.chr
[142] Fix | Delete
end
[143] Fix | Delete
idx = 0
[144] Fix | Delete
res_type = :DT_COLSEP
[145] Fix | Delete
row = []
[146] Fix | Delete
begin
[147] Fix | Delete
while res_type == :DT_COLSEP
[148] Fix | Delete
res_type, idx, cell = parse_body(src, idx, fs, rs)
[149] Fix | Delete
row << cell
[150] Fix | Delete
end
[151] Fix | Delete
rescue IllegalFormatError
[152] Fix | Delete
return []
[153] Fix | Delete
end
[154] Fix | Delete
row
[155] Fix | Delete
end
[156] Fix | Delete
[157] Fix | Delete
# Create a line from cells. each cell is stringified by to_s.
[158] Fix | Delete
def CSV.generate_line(row, fs = nil, rs = nil)
[159] Fix | Delete
if row.size == 0
[160] Fix | Delete
return ''
[161] Fix | Delete
end
[162] Fix | Delete
fs ||= ','
[163] Fix | Delete
if fs.is_a?(Fixnum)
[164] Fix | Delete
fs = fs.chr
[165] Fix | Delete
end
[166] Fix | Delete
if !rs.nil? and rs.is_a?(Fixnum)
[167] Fix | Delete
rs = rs.chr
[168] Fix | Delete
end
[169] Fix | Delete
res_type = :DT_COLSEP
[170] Fix | Delete
result_str = ''
[171] Fix | Delete
idx = 0
[172] Fix | Delete
while true
[173] Fix | Delete
generate_body(row[idx], result_str, fs, rs)
[174] Fix | Delete
idx += 1
[175] Fix | Delete
if (idx == row.size)
[176] Fix | Delete
break
[177] Fix | Delete
end
[178] Fix | Delete
generate_separator(:DT_COLSEP, result_str, fs, rs)
[179] Fix | Delete
end
[180] Fix | Delete
result_str
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
# Parse a line from string. Consider using CSV.parse_line instead.
[184] Fix | Delete
# To parse lines in CSV string, see EXAMPLE below.
[185] Fix | Delete
#
[186] Fix | Delete
# EXAMPLE
[187] Fix | Delete
# src = "a,b\r\nc,d\r\ne,f"
[188] Fix | Delete
# idx = 0
[189] Fix | Delete
# begin
[190] Fix | Delete
# parsed = []
[191] Fix | Delete
# parsed_cells, idx = CSV.parse_row(src, idx, parsed)
[192] Fix | Delete
# puts "Parsed #{ parsed_cells } cells."
[193] Fix | Delete
# p parsed
[194] Fix | Delete
# end while parsed_cells > 0
[195] Fix | Delete
#
[196] Fix | Delete
# ARGS
[197] Fix | Delete
# src: a CSV data to be parsed. Must respond '[](idx)'.
[198] Fix | Delete
# src[](idx) must return a char. (Not a string such as 'a', but 97).
[199] Fix | Delete
# src[](idx_out_of_bounds) must return nil. A String satisfies this
[200] Fix | Delete
# requirement.
[201] Fix | Delete
# idx: index of parsing location of 'src'. 0 origin.
[202] Fix | Delete
# out_dev: buffer for parsed cells. Must respond '<<(aString)'.
[203] Fix | Delete
# col_sep: Column separator. ?, by default. If you want to separate
[204] Fix | Delete
# fields with semicolon, give ?; here.
[205] Fix | Delete
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
[206] Fix | Delete
# want to separate records with \r, give ?\r here.
[207] Fix | Delete
#
[208] Fix | Delete
# RETURNS
[209] Fix | Delete
# parsed_cells: num of parsed cells.
[210] Fix | Delete
# idx: index of next parsing location of 'src'.
[211] Fix | Delete
#
[212] Fix | Delete
def CSV.parse_row(src, idx, out_dev, fs = nil, rs = nil)
[213] Fix | Delete
fs ||= ','
[214] Fix | Delete
if fs.is_a?(Fixnum)
[215] Fix | Delete
fs = fs.chr
[216] Fix | Delete
end
[217] Fix | Delete
if !rs.nil? and rs.is_a?(Fixnum)
[218] Fix | Delete
rs = rs.chr
[219] Fix | Delete
end
[220] Fix | Delete
idx_backup = idx
[221] Fix | Delete
parsed_cells = 0
[222] Fix | Delete
res_type = :DT_COLSEP
[223] Fix | Delete
begin
[224] Fix | Delete
while res_type != :DT_ROWSEP
[225] Fix | Delete
res_type, idx, cell = parse_body(src, idx, fs, rs)
[226] Fix | Delete
if res_type == :DT_EOS
[227] Fix | Delete
if idx == idx_backup #((parsed_cells == 0) and cell.nil?)
[228] Fix | Delete
return 0, 0
[229] Fix | Delete
end
[230] Fix | Delete
res_type = :DT_ROWSEP
[231] Fix | Delete
end
[232] Fix | Delete
parsed_cells += 1
[233] Fix | Delete
out_dev << cell
[234] Fix | Delete
end
[235] Fix | Delete
rescue IllegalFormatError
[236] Fix | Delete
return 0, 0
[237] Fix | Delete
end
[238] Fix | Delete
return parsed_cells, idx
[239] Fix | Delete
end
[240] Fix | Delete
[241] Fix | Delete
# Convert a line from cells data to string. Consider using CSV.generate_line
[242] Fix | Delete
# instead. To generate multi-row CSV string, see EXAMPLE below.
[243] Fix | Delete
#
[244] Fix | Delete
# EXAMPLE
[245] Fix | Delete
# row1 = ['a', 'b']
[246] Fix | Delete
# row2 = ['c', 'd']
[247] Fix | Delete
# row3 = ['e', 'f']
[248] Fix | Delete
# src = [row1, row2, row3]
[249] Fix | Delete
# buf = ''
[250] Fix | Delete
# src.each do |row|
[251] Fix | Delete
# parsed_cells = CSV.generate_row(row, 2, buf)
[252] Fix | Delete
# puts "Created #{ parsed_cells } cells."
[253] Fix | Delete
# end
[254] Fix | Delete
# p buf
[255] Fix | Delete
#
[256] Fix | Delete
# ARGS
[257] Fix | Delete
# src: an Array of String to be converted to CSV string. Must respond to
[258] Fix | Delete
# 'size' and '[](idx)'. src[idx] must return String.
[259] Fix | Delete
# cells: num of cells in a line.
[260] Fix | Delete
# out_dev: buffer for generated CSV string. Must respond to '<<(string)'.
[261] Fix | Delete
# col_sep: Column separator. ?, by default. If you want to separate
[262] Fix | Delete
# fields with semicolon, give ?; here.
[263] Fix | Delete
# row_sep: Row separator. nil by default. nil means "\r\n or \n". If you
[264] Fix | Delete
# want to separate records with \r, give ?\r here.
[265] Fix | Delete
#
[266] Fix | Delete
# RETURNS
[267] Fix | Delete
# parsed_cells: num of converted cells.
[268] Fix | Delete
#
[269] Fix | Delete
def CSV.generate_row(src, cells, out_dev, fs = nil, rs = nil)
[270] Fix | Delete
fs ||= ','
[271] Fix | Delete
if fs.is_a?(Fixnum)
[272] Fix | Delete
fs = fs.chr
[273] Fix | Delete
end
[274] Fix | Delete
if !rs.nil? and rs.is_a?(Fixnum)
[275] Fix | Delete
rs = rs.chr
[276] Fix | Delete
end
[277] Fix | Delete
src_size = src.size
[278] Fix | Delete
if (src_size == 0)
[279] Fix | Delete
if cells == 0
[280] Fix | Delete
generate_separator(:DT_ROWSEP, out_dev, fs, rs)
[281] Fix | Delete
end
[282] Fix | Delete
return 0
[283] Fix | Delete
end
[284] Fix | Delete
res_type = :DT_COLSEP
[285] Fix | Delete
parsed_cells = 0
[286] Fix | Delete
generate_body(src[parsed_cells], out_dev, fs, rs)
[287] Fix | Delete
parsed_cells += 1
[288] Fix | Delete
while ((parsed_cells < cells) and (parsed_cells != src_size))
[289] Fix | Delete
generate_separator(:DT_COLSEP, out_dev, fs, rs)
[290] Fix | Delete
generate_body(src[parsed_cells], out_dev, fs, rs)
[291] Fix | Delete
parsed_cells += 1
[292] Fix | Delete
end
[293] Fix | Delete
if (parsed_cells == cells)
[294] Fix | Delete
generate_separator(:DT_ROWSEP, out_dev, fs, rs)
[295] Fix | Delete
else
[296] Fix | Delete
generate_separator(:DT_COLSEP, out_dev, fs, rs)
[297] Fix | Delete
end
[298] Fix | Delete
parsed_cells
[299] Fix | Delete
end
[300] Fix | Delete
[301] Fix | Delete
# Private class methods.
[302] Fix | Delete
class << self
[303] Fix | Delete
private
[304] Fix | Delete
[305] Fix | Delete
def open_reader(path, mode, fs, rs, &block)
[306] Fix | Delete
file = File.open(path, mode)
[307] Fix | Delete
if block
[308] Fix | Delete
begin
[309] Fix | Delete
CSV::Reader.parse(file, fs, rs) do |row|
[310] Fix | Delete
yield(row)
[311] Fix | Delete
end
[312] Fix | Delete
ensure
[313] Fix | Delete
file.close
[314] Fix | Delete
end
[315] Fix | Delete
nil
[316] Fix | Delete
else
[317] Fix | Delete
reader = CSV::Reader.create(file, fs, rs)
[318] Fix | Delete
reader.close_on_terminate
[319] Fix | Delete
reader
[320] Fix | Delete
end
[321] Fix | Delete
end
[322] Fix | Delete
[323] Fix | Delete
def open_writer(path, mode, fs, rs, &block)
[324] Fix | Delete
file = File.open(path, mode)
[325] Fix | Delete
if block
[326] Fix | Delete
begin
[327] Fix | Delete
CSV::Writer.generate(file, fs, rs) do |writer|
[328] Fix | Delete
yield(writer)
[329] Fix | Delete
end
[330] Fix | Delete
ensure
[331] Fix | Delete
file.close
[332] Fix | Delete
end
[333] Fix | Delete
nil
[334] Fix | Delete
else
[335] Fix | Delete
writer = CSV::Writer.create(file, fs, rs)
[336] Fix | Delete
writer.close_on_terminate
[337] Fix | Delete
writer
[338] Fix | Delete
end
[339] Fix | Delete
end
[340] Fix | Delete
[341] Fix | Delete
def parse_body(src, idx, fs, rs)
[342] Fix | Delete
fs_str = fs
[343] Fix | Delete
fs_size = fs_str.size
[344] Fix | Delete
rs_str = rs || "\n"
[345] Fix | Delete
rs_size = rs_str.size
[346] Fix | Delete
fs_idx = rs_idx = 0
[347] Fix | Delete
cell = Cell.new
[348] Fix | Delete
state = :ST_START
[349] Fix | Delete
quoted = cr = false
[350] Fix | Delete
c = nil
[351] Fix | Delete
last_idx = idx
[352] Fix | Delete
while c = src[idx]
[353] Fix | Delete
unless quoted
[354] Fix | Delete
fschar = (c == fs_str[fs_idx])
[355] Fix | Delete
rschar = (c == rs_str[rs_idx])
[356] Fix | Delete
# simple 1 char backtrack
[357] Fix | Delete
if !fschar and c == fs_str[0]
[358] Fix | Delete
fs_idx = 0
[359] Fix | Delete
fschar = true
[360] Fix | Delete
if state == :ST_START
[361] Fix | Delete
state = :ST_DATA
[362] Fix | Delete
elsif state == :ST_QUOTE
[363] Fix | Delete
raise IllegalFormatError
[364] Fix | Delete
end
[365] Fix | Delete
end
[366] Fix | Delete
if !rschar and c == rs_str[0]
[367] Fix | Delete
rs_idx = 0
[368] Fix | Delete
rschar = true
[369] Fix | Delete
if state == :ST_START
[370] Fix | Delete
state = :ST_DATA
[371] Fix | Delete
elsif state == :ST_QUOTE
[372] Fix | Delete
raise IllegalFormatError
[373] Fix | Delete
end
[374] Fix | Delete
end
[375] Fix | Delete
end
[376] Fix | Delete
if c == ?"
[377] Fix | Delete
fs_idx = rs_idx = 0
[378] Fix | Delete
if cr
[379] Fix | Delete
raise IllegalFormatError
[380] Fix | Delete
end
[381] Fix | Delete
cell << src[last_idx, (idx - last_idx)]
[382] Fix | Delete
last_idx = idx
[383] Fix | Delete
if state == :ST_DATA
[384] Fix | Delete
if quoted
[385] Fix | Delete
last_idx += 1
[386] Fix | Delete
quoted = false
[387] Fix | Delete
state = :ST_QUOTE
[388] Fix | Delete
else
[389] Fix | Delete
raise IllegalFormatError
[390] Fix | Delete
end
[391] Fix | Delete
elsif state == :ST_QUOTE
[392] Fix | Delete
cell << c.chr
[393] Fix | Delete
last_idx += 1
[394] Fix | Delete
quoted = true
[395] Fix | Delete
state = :ST_DATA
[396] Fix | Delete
else # :ST_START
[397] Fix | Delete
quoted = true
[398] Fix | Delete
last_idx += 1
[399] Fix | Delete
state = :ST_DATA
[400] Fix | Delete
end
[401] Fix | Delete
elsif fschar or rschar
[402] Fix | Delete
if fschar
[403] Fix | Delete
fs_idx += 1
[404] Fix | Delete
end
[405] Fix | Delete
if rschar
[406] Fix | Delete
rs_idx += 1
[407] Fix | Delete
end
[408] Fix | Delete
sep = nil
[409] Fix | Delete
if fs_idx == fs_size
[410] Fix | Delete
if state == :ST_START and rs_idx > 0 and fs_idx < rs_idx
[411] Fix | Delete
state = :ST_DATA
[412] Fix | Delete
end
[413] Fix | Delete
cell << src[last_idx, (idx - last_idx - (fs_size - 1))]
[414] Fix | Delete
last_idx = idx
[415] Fix | Delete
fs_idx = rs_idx = 0
[416] Fix | Delete
if cr
[417] Fix | Delete
raise IllegalFormatError
[418] Fix | Delete
end
[419] Fix | Delete
sep = :DT_COLSEP
[420] Fix | Delete
elsif rs_idx == rs_size
[421] Fix | Delete
if state == :ST_START and fs_idx > 0 and rs_idx < fs_idx
[422] Fix | Delete
state = :ST_DATA
[423] Fix | Delete
end
[424] Fix | Delete
if !(rs.nil? and cr)
[425] Fix | Delete
cell << src[last_idx, (idx - last_idx - (rs_size - 1))]
[426] Fix | Delete
last_idx = idx
[427] Fix | Delete
end
[428] Fix | Delete
fs_idx = rs_idx = 0
[429] Fix | Delete
sep = :DT_ROWSEP
[430] Fix | Delete
end
[431] Fix | Delete
if sep
[432] Fix | Delete
if state == :ST_DATA
[433] Fix | Delete
return sep, idx + 1, cell;
[434] Fix | Delete
elsif state == :ST_QUOTE
[435] Fix | Delete
return sep, idx + 1, cell;
[436] Fix | Delete
else # :ST_START
[437] Fix | Delete
return sep, idx + 1, nil
[438] Fix | Delete
end
[439] Fix | Delete
end
[440] Fix | Delete
elsif rs.nil? and c == ?\r
[441] Fix | Delete
# special \r treatment for backward compatibility
[442] Fix | Delete
fs_idx = rs_idx = 0
[443] Fix | Delete
if cr
[444] Fix | Delete
raise IllegalFormatError
[445] Fix | Delete
end
[446] Fix | Delete
cell << src[last_idx, (idx - last_idx)]
[447] Fix | Delete
last_idx = idx
[448] Fix | Delete
if quoted
[449] Fix | Delete
state = :ST_DATA
[450] Fix | Delete
else
[451] Fix | Delete
cr = true
[452] Fix | Delete
end
[453] Fix | Delete
else
[454] Fix | Delete
fs_idx = rs_idx = 0
[455] Fix | Delete
if state == :ST_DATA or state == :ST_START
[456] Fix | Delete
if cr
[457] Fix | Delete
raise IllegalFormatError
[458] Fix | Delete
end
[459] Fix | Delete
state = :ST_DATA
[460] Fix | Delete
else # :ST_QUOTE
[461] Fix | Delete
raise IllegalFormatError
[462] Fix | Delete
end
[463] Fix | Delete
end
[464] Fix | Delete
idx += 1
[465] Fix | Delete
end
[466] Fix | Delete
if state == :ST_START
[467] Fix | Delete
if fs_idx > 0 or rs_idx > 0
[468] Fix | Delete
state = :ST_DATA
[469] Fix | Delete
else
[470] Fix | Delete
return :DT_EOS, idx, nil
[471] Fix | Delete
end
[472] Fix | Delete
elsif quoted
[473] Fix | Delete
raise IllegalFormatError
[474] Fix | Delete
elsif cr
[475] Fix | Delete
raise IllegalFormatError
[476] Fix | Delete
end
[477] Fix | Delete
cell << src[last_idx, (idx - last_idx)]
[478] Fix | Delete
last_idx = idx
[479] Fix | Delete
return :DT_EOS, idx, cell
[480] Fix | Delete
end
[481] Fix | Delete
[482] Fix | Delete
def generate_body(cell, out_dev, fs, rs)
[483] Fix | Delete
if cell.nil?
[484] Fix | Delete
# empty
[485] Fix | Delete
else
[486] Fix | Delete
cell = cell.to_s
[487] Fix | Delete
row_data = cell.dup
[488] Fix | Delete
if (row_data.gsub!('"', '""') or
[489] Fix | Delete
row_data.index(fs) or
[490] Fix | Delete
(rs and row_data.index(rs)) or
[491] Fix | Delete
(/[\r\n]/ =~ row_data) or
[492] Fix | Delete
(cell.empty?))
[493] Fix | Delete
out_dev << '"' << row_data << '"'
[494] Fix | Delete
else
[495] Fix | Delete
out_dev << row_data
[496] Fix | Delete
end
[497] Fix | Delete
end
[498] Fix | Delete
end
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function