| 1 |
|
| 2 |
|
| 3 |
package dstream |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"math" |
| 9 |
"os" |
| 10 |
"strconv" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
func (cs *CSVReader) Next() bool { |
| 16 |
|
| 17 |
if cs.done { |
| 18 |
return false |
| 19 |
} |
| 20 |
|
| 21 |
if cs.limitchunk > 0 && cs.limitchunk <= cs.chunknum { |
| 22 |
cs.done = true |
| 23 |
return false |
| 24 |
} |
| 25 |
|
| 26 |
cs.chunknum++ |
| 27 |
|
| 28 |
truncate(cs.bdata) |
| 29 |
|
| 30 |
for j := 0; j < cs.chunkSize; j++ { |
| 31 |
|
| 32 |
|
| 33 |
var rec []string |
| 34 |
var err error |
| 35 |
if cs.firstrow != nil { |
| 36 |
rec = cs.firstrow |
| 37 |
cs.firstrow = nil |
| 38 |
} else { |
| 39 |
rec, err = cs.csvrdr.Read() |
| 40 |
if err == io.EOF { |
| 41 |
cs.done = true |
| 42 |
return ilen(cs.bdata[0]) > 0 |
| 43 |
} else if err != nil { |
| 44 |
if cs.skipErrors { |
| 45 |
os.Stderr.WriteString(fmt.Sprintf("%v\n", err)) |
| 46 |
continue |
| 47 |
} |
| 48 |
panic(err) |
| 49 |
} |
| 50 |
} |
| 51 |
cs.nobs++ |
| 52 |
|
| 53 |
for pos, typ := range cs.types { |
| 54 |
fpos := cs.filepos[pos] |
| 55 |
switch typ.Type { |
| 56 |
case String: |
| 57 |
x := rec[fpos] |
| 58 |
u := cs.bdata[pos].([]string) |
| 59 |
cs.bdata[pos] = append(u, string(x)) |
| 60 |
case Time: |
| 61 |
x := cs.parseTime(rec[fpos]) |
| 62 |
u := cs.bdata[pos].([]time.Time) |
| 63 |
cs.bdata[pos] = append(u, time.Time(x)) |
| 64 |
case Uint8: |
| 65 |
x, err := strconv.Atoi(rec[fpos]) |
| 66 |
if err != nil { |
| 67 |
panic(err) |
| 68 |
} |
| 69 |
u := cs.bdata[pos].([]uint8) |
| 70 |
cs.bdata[pos] = append(u, uint8(x)) |
| 71 |
case Uint16: |
| 72 |
x, err := strconv.Atoi(rec[fpos]) |
| 73 |
if err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
u := cs.bdata[pos].([]uint16) |
| 77 |
cs.bdata[pos] = append(u, uint16(x)) |
| 78 |
case Uint32: |
| 79 |
x, err := strconv.Atoi(rec[fpos]) |
| 80 |
if err != nil { |
| 81 |
panic(err) |
| 82 |
} |
| 83 |
u := cs.bdata[pos].([]uint32) |
| 84 |
cs.bdata[pos] = append(u, uint32(x)) |
| 85 |
case Uint64: |
| 86 |
x, err := strconv.Atoi(rec[fpos]) |
| 87 |
if err != nil { |
| 88 |
panic(err) |
| 89 |
} |
| 90 |
u := cs.bdata[pos].([]uint64) |
| 91 |
cs.bdata[pos] = append(u, uint64(x)) |
| 92 |
case Int8: |
| 93 |
x, err := strconv.Atoi(rec[fpos]) |
| 94 |
if err != nil { |
| 95 |
panic(err) |
| 96 |
} |
| 97 |
u := cs.bdata[pos].([]int8) |
| 98 |
cs.bdata[pos] = append(u, int8(x)) |
| 99 |
case Int16: |
| 100 |
x, err := strconv.Atoi(rec[fpos]) |
| 101 |
if err != nil { |
| 102 |
panic(err) |
| 103 |
} |
| 104 |
u := cs.bdata[pos].([]int16) |
| 105 |
cs.bdata[pos] = append(u, int16(x)) |
| 106 |
case Int32: |
| 107 |
x, err := strconv.Atoi(rec[fpos]) |
| 108 |
if err != nil { |
| 109 |
panic(err) |
| 110 |
} |
| 111 |
u := cs.bdata[pos].([]int32) |
| 112 |
cs.bdata[pos] = append(u, int32(x)) |
| 113 |
case Int64: |
| 114 |
x, err := strconv.Atoi(rec[fpos]) |
| 115 |
if err != nil { |
| 116 |
panic(err) |
| 117 |
} |
| 118 |
u := cs.bdata[pos].([]int64) |
| 119 |
cs.bdata[pos] = append(u, int64(x)) |
| 120 |
case Float32: |
| 121 |
x, err := strconv.ParseFloat(rec[fpos], 64) |
| 122 |
if err != nil { |
| 123 |
x = math.NaN() |
| 124 |
} |
| 125 |
u := cs.bdata[pos].([]float32) |
| 126 |
cs.bdata[pos] = append(u, float32(x)) |
| 127 |
case Float64: |
| 128 |
x, err := strconv.ParseFloat(rec[fpos], 64) |
| 129 |
if err != nil { |
| 130 |
x = math.NaN() |
| 131 |
} |
| 132 |
u := cs.bdata[pos].([]float64) |
| 133 |
cs.bdata[pos] = append(u, float64(x)) |
| 134 |
default: |
| 135 |
panic("unknown type") |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return true |
| 141 |
} |
| 142 |
|
| 143 |
func (cs *CSVReader) setbdata() { |
| 144 |
|
| 145 |
cs.bdata = make([]interface{}, len(cs.names)) |
| 146 |
|
| 147 |
for pos, dtype := range cs.dtypes { |
| 148 |
switch dtype { |
| 149 |
case String: |
| 150 |
cs.bdata[pos] = make([]string, 0) |
| 151 |
case Time: |
| 152 |
cs.bdata[pos] = make([]time.Time, 0) |
| 153 |
case Uint8: |
| 154 |
cs.bdata[pos] = make([]uint8, 0) |
| 155 |
case Uint16: |
| 156 |
cs.bdata[pos] = make([]uint16, 0) |
| 157 |
case Uint32: |
| 158 |
cs.bdata[pos] = make([]uint32, 0) |
| 159 |
case Uint64: |
| 160 |
cs.bdata[pos] = make([]uint64, 0) |
| 161 |
case Int8: |
| 162 |
cs.bdata[pos] = make([]int8, 0) |
| 163 |
case Int16: |
| 164 |
cs.bdata[pos] = make([]int16, 0) |
| 165 |
case Int32: |
| 166 |
cs.bdata[pos] = make([]int32, 0) |
| 167 |
case Int64: |
| 168 |
cs.bdata[pos] = make([]int64, 0) |
| 169 |
case Float32: |
| 170 |
cs.bdata[pos] = make([]float32, 0) |
| 171 |
case Float64: |
| 172 |
cs.bdata[pos] = make([]float64, 0) |
| 173 |
default: |
| 174 |
panic("Unknown type") |
| 175 |
} |
| 176 |
} |
| 177 |
} |