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
|
// Copyright 2012 Jonas mg
//
// 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 file
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)
// Contain returns whether the named file contains the byte slice b. The
// return value is a boolean.
func Contain(filename string, b []byte) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, fmt.Errorf("Contain: %s", err)
}
defer f.Close()
buf := bufio.NewReader(f)
for {
line, err := buf.ReadBytes('\n')
if err == io.EOF {
break
}
if bytes.Contains(line, b) {
return true, nil
}
}
return false, nil
}
// ContainString returns whether the named file contains the string s. The
// return value is a boolean.
func ContainString(filename, s string) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, fmt.Errorf("ContainString: %s", err)
}
defer f.Close()
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
if err == io.EOF {
break
}
if strings.Contains(line, s) {
return true, nil
}
}
return false, nil
}
|