This commit is contained in:
Will Charczuk 2019-02-13 16:09:26 -08:00
parent 3cb33d48d3
commit 26eaa1d898
76 changed files with 1076 additions and 1717 deletions

51
fileutil.go Normal file
View file

@ -0,0 +1,51 @@
package chart
import (
"bufio"
"io"
"os"
"github.com/blend/go-sdk/exception"
)
// 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 {
return exception.New(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
err = handler(line)
if err != nil {
return exception.New(err)
}
}
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 {
return exception.New(err)
}
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 {
return exception.New(err)
}
}
return nil
}