2019-02-13 19:09:26 -05:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadLines reads a file and calls the handler for each line.
|
|
|
|
func ReadLines(filePath string, handler func(string) error) error {
|
|
|
|
f, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
2019-04-24 15:58:05 -04:00
|
|
|
return err
|
2019-02-13 19:09:26 -05:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
err = handler(line)
|
|
|
|
if err != nil {
|
2019-04-24 15:58:05 -04:00
|
|
|
return err
|
2019-02-13 19:09:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChunks reads a file in `chunkSize` pieces, dispatched to the handler.
|
|
|
|
func ReadChunks(filePath string, chunkSize int, handler func([]byte) error) error {
|
|
|
|
f, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
2019-04-24 15:58:05 -04:00
|
|
|
return err
|
2019-02-13 19:09:26 -05:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
chunk := make([]byte, chunkSize)
|
|
|
|
for {
|
|
|
|
readBytes, err := f.Read(chunk)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
readData := chunk[:readBytes]
|
|
|
|
err = handler(readData)
|
|
|
|
if err != nil {
|
2019-04-24 15:58:05 -04:00
|
|
|
return err
|
2019-02-13 19:09:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|