示例#1
0
文件: jvgrep.go 项目: koron/jvgrep
func Grep(arg *GrepArg) {
	var f []byte
	var path = ""
	var ok bool
	var stdin *bufio.Reader

	if path, ok = arg.input.(string); ok {
		if fi, err := os.Stat(path); err == nil && fi.Size() == 0 {
			return
		}
		mf, err := mmap.Open(path)
		if err != nil {
			errorline(err.Error() + ": " + path)
			return
		}
		defer mf.Close()
		f = mf.Data()
		doGrep(path, f, arg)
	} else if in, ok := arg.input.(io.Reader); ok {
		stdin = bufio.NewReader(in)
		for {
			f, _, err := stdin.ReadLine()
			doGrep("stdin", f, arg)
			if err != nil {
				break
			}
		}
	}
}
示例#2
0
文件: jvgrep.go 项目: mattn/jvgrep
// Grep do grep.
func Grep(arg *GrepArg) bool {
	n := false
	if in, ok := arg.input.(io.Reader); ok {
		stdin := bufio.NewReader(in)
		for {
			f, _, err := stdin.ReadLine()
			if doGrep("stdin", f, arg) {
				n = true
			}
			if err != nil {
				break
			}
		}
		return n
	}

	path, _ := arg.input.(string)
	if arg.size > 65536*4 {
		mf, err := mmap.Open(path)
		if err != nil {
			errorLine(err.Error() + ": " + path)
			return false
		}
		defer mf.Close()
		f := mf.Data()
		return doGrep(path, f, arg)
	}
	f, err := ioutil.ReadFile(path)
	if err != nil {
		errorLine(err.Error() + ": " + path)
		return false
	}
	return doGrep(path, f, arg)
}