2017-05-12 20:12:23 -04:00
|
|
|
package util
|
2016-10-21 15:44:44 -04: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-25 23:01:48 -04: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 15:44:44 -04:00
|
|
|
defer f.Close()
|
2017-04-25 23:01:48 -04:00
|
|
|
var line string
|
2016-10-21 15:44:44 -04:00
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
2017-04-25 23:01:48 -04:00
|
|
|
line = scanner.Text()
|
|
|
|
err = handler(line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-21 15:44:44 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 23:01:48 -04:00
|
|
|
return err
|
|
|
|
|
2016-10-21 15:44:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadByChunks reads a file in `chunkSize` pieces, dispatched to the handler.
|
2017-04-25 23:01:48 -04: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 15:44:44 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
chunk := make([]byte, chunkSize)
|
|
|
|
for {
|
|
|
|
readBytes, err := f.Read(chunk)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
readData := chunk[:readBytes]
|
2017-04-25 23:01:48 -04:00
|
|
|
err = handler(readData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-21 15:44:44 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 23:01:48 -04:00
|
|
|
return err
|
2016-10-21 15:44:44 -04:00
|
|
|
}
|