-
Notifications
You must be signed in to change notification settings - Fork 3
/
readlinebyline.go
62 lines (56 loc) · 1.28 KB
/
readlinebyline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file is part of grepp.
//
// Copyright (C) 2012-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"bufio"
"fmt"
"io"
"os"
l "github.com/DavidGamba/dgtools/grepp/logging"
)
var errorBufferSizeTooSmall = fmt.Errorf("buffer size too small")
type LineError struct {
Line string
LineNumber int
Error error
}
func ReadLineByLine(filename string, bufferSize int) <-chan LineError {
l.Debug.Printf("[readLineByLine] %s : %d\n", filename, bufferSize)
c := make(chan LineError)
go func() {
fh, err := os.Open(filename)
if err != nil {
c <- LineError{Error: err}
close(c)
return
}
defer fh.Close()
reader := bufio.NewReaderSize(fh, bufferSize)
// line number
n := 0
for {
n++
line, isPrefix, err := reader.ReadLine()
if isPrefix {
err := fmt.Errorf("%s: %w", filename, errorBufferSizeTooSmall)
c <- LineError{Error: err}
break
}
// stop reading file
if err != nil {
if err != io.EOF {
c <- LineError{Error: err}
}
break
}
c <- LineError{Line: string(line), LineNumber: n}
}
close(c)
}()
return c
}