go-chart/util/file_util.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

package util
2016-10-21 21:44:44 +02:00
import (
"bufio"
"io"
"os"
)
var (
// File contains file utility functions
File = fileUtil{}
)
type fileUtil struct{}
// ReadByLines reads a file and calls the handler for each line.
2017-04-26 05:01:48 +02:00
func (fu fileUtil) ReadByLines(filePath string, handler func(line string) error) error {
var f *os.File
var err error
if f, err = os.Open(filePath); err == nil {
2016-10-21 21:44:44 +02:00
defer f.Close()
2017-04-26 05:01:48 +02:00
var line string
2016-10-21 21:44:44 +02:00
scanner := bufio.NewScanner(f)
for scanner.Scan() {
2017-04-26 05:01:48 +02:00
line = scanner.Text()
err = handler(line)
if err != nil {
return err
}
2016-10-21 21:44:44 +02:00
}
}
2017-04-26 05:01:48 +02:00
return err
2016-10-21 21:44:44 +02:00
}
// ReadByChunks reads a file in `chunkSize` pieces, dispatched to the handler.
2017-04-26 05:01:48 +02:00
func (fu fileUtil) ReadByChunks(filePath string, chunkSize int, handler func(line []byte) error) error {
var f *os.File
var err error
if f, err = os.Open(filePath); err == nil {
2016-10-21 21:44:44 +02:00
defer f.Close()
chunk := make([]byte, chunkSize)
for {
readBytes, err := f.Read(chunk)
if err == io.EOF {
break
}
readData := chunk[:readBytes]
2017-04-26 05:01:48 +02:00
err = handler(readData)
if err != nil {
return err
}
2016-10-21 21:44:44 +02:00
}
}
2017-04-26 05:01:48 +02:00
return err
2016-10-21 21:44:44 +02:00
}