From d6e6ddf99685dbd365c0154796ffd0cb2df584f6 Mon Sep 17 00:00:00 2001 From: "xubin.gao" Date: Sun, 29 Dec 2019 19:24:56 +0800 Subject: [PATCH] Add gfile.ReadLines and gfile.ReadByteLines read file content line by line --- os/gfile/gfile_contents.go | 35 +++++++++++++++++ os/gfile/gfile_z_readline_test.go | 59 +++++++++++++++++++++++++++++ os/gfile/testdata/readline/file.log | 5 +++ 3 files changed, 99 insertions(+) create mode 100644 os/gfile/gfile_z_readline_test.go create mode 100644 os/gfile/testdata/readline/file.log diff --git a/os/gfile/gfile_contents.go b/os/gfile/gfile_contents.go index 4823e0f8b..bf3c77594 100644 --- a/os/gfile/gfile_contents.go +++ b/os/gfile/gfile_contents.go @@ -7,9 +7,12 @@ package gfile import ( + "bufio" "io" "io/ioutil" "os" + + "github.com/gogf/gf/util/gconv" ) var ( @@ -160,3 +163,35 @@ func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte { } return nil } + +// ReadLines reads file content line by line, which is passed to the callback function as string. +// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker. +// +// Note that the parameter passed to callback function might be an empty value, and the last non-empty line +// will be passed to callback function even if it has no newline marker. +func ReadLines(file string, callback func(text string)) error { + cb := func(bytes []byte) { + callback(gconv.UnsafeBytesToStr(bytes)) + } + return ReadByteLines(file, cb) +} + +// ReadByteLines reads file content line by line, which is passed to the callback function as []byte. +// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker. +// +// Note that the parameter passed to callback function might be an empty value, and the last non-empty line +// will be passed to callback function even if it has no newline marker. +func ReadByteLines(file string, callback func(bytes []byte)) error { + f, err := os.Open(file) + if err != nil { + return err + } + defer f.Close() + + scanner := bufio.NewScanner(f) + + for scanner.Scan() { + callback(scanner.Bytes()) + } + return nil +} diff --git a/os/gfile/gfile_z_readline_test.go b/os/gfile/gfile_z_readline_test.go new file mode 100644 index 000000000..8fe537cf1 --- /dev/null +++ b/os/gfile/gfile_z_readline_test.go @@ -0,0 +1,59 @@ +// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gfile_test + +import ( + "github.com/gogf/gf/debug/gdebug" + "testing" + + "github.com/gogf/gf/os/gfile" + "github.com/gogf/gf/test/gtest" +) + +func Test_NotFound(t *testing.T) { + gtest.Case(t, func() { + teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/error.log" + callback := func(line string) { + } + err := gfile.ReadLines(teatFile, callback) + gtest.AssertNE(err, nil) + }) +} + +func Test_ReadLines(t *testing.T) { + gtest.Case(t, func() { + expectList := []string{"a", "b", "c", "d", "e"} + + getList := make([]string, 0) + callback := func(line string) { + getList = append(getList, line) + } + + teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" + err := gfile.ReadLines(teatFile, callback) + + gtest.AssertEQ(getList, expectList) + gtest.AssertEQ(err, nil) + }) +} + +func Test_ReadByteLines(t *testing.T) { + gtest.Case(t, func() { + expectList := [][]byte{[]byte("a"), []byte("b"), []byte("c"), []byte("d"), []byte("e")} + + getList := make([][]byte, 0) + callback := func(line []byte) { + getList = append(getList, line) + } + + teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" + err := gfile.ReadByteLines(teatFile, callback) + + gtest.AssertEQ(getList, expectList) + gtest.AssertEQ(err, nil) + }) +} diff --git a/os/gfile/testdata/readline/file.log b/os/gfile/testdata/readline/file.log new file mode 100644 index 000000000..940532533 --- /dev/null +++ b/os/gfile/testdata/readline/file.log @@ -0,0 +1,5 @@ +a +b +c +d +e