-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_aamax_dan_hacket.rb
85 lines (73 loc) · 1.69 KB
/
solution_aamax_dan_hacket.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Parser
def initialize filename, delimiter=","
@fhandle = File.open(filename, 'r')
@delim = delimiter
@header = []
@arr_val = []
line_str = @fhandle.readline
@header = parse_row(line_str)
end
def header
@header
end
def eof?
@fhandle.eof?
end
def read
line_str = @fhandle.readline
parse_row(line_str)
end
def parse_row(line)
ret_val = []
while !line.nil? && line.length > 0
result = parse_field(line)
line = result[1]
ret_val << result[0]
end
ret_val
end
def parse_field(str)
str.chomp!
ret_val = []
if str[0] != '"'
field = str.split(@delim)[0]
ret_val << field
ret_val << str[field.length + 1, str.length]
else
idx = 1
puts " idx: #{idx} quoted field: #{str}" if str[0] == '"'
while true
if str[idx] == '"'
# read to quote followed by delim or eol
if (idx == str.length - 1) || (str[idx + 1] == @delim)
fld = str[1..idx]
str = str[idx + 2, str.length]
ret_val = [fld, str]
break
end
idx += 1
else
# read to next delim or eol
if (idx == str.length - 1) || (str[idx] == @delim)
fld = str[0..idx - 1]
str = str[idx + 1, str.length]
ret_val = [fld, str]
break
end
idx += 1
end
break if str.empty?
end
end
ret_val
end
def close
@fhandle.close if @fhandle
end
end
p = Parser.new('./import_files/prodfile.csv')
puts "Header: #{p.header}"
until p.eof?
puts p.read.map {|v| v}.join(',')
end
p.close