-
Notifications
You must be signed in to change notification settings - Fork 0
/
apfile.go
72 lines (60 loc) · 1.3 KB
/
apfile.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
63
64
65
66
67
68
69
70
71
72
package droidalyzer
import (
"bufio"
"fmt"
"os"
"strings"
)
// NCode holds information about networking code occurrences
type NCode struct {
Line uint
Keyword string
}
// APFile represents an Android project file
type APFile struct {
Name string
Path string
Extension string
NCode []NCode
}
// Scan scans a file for keywords (case insensitive)
func (apf *APFile) Scan(keywords *[]string) error {
apfile, err := os.Open(apf.Path)
if err != nil {
return err
}
defer func() {
err := apfile.Close()
if err != nil {
panic(err)
}
}()
var lineNbr uint = 1
scanner := bufio.NewScanner(apfile)
for scanner.Scan() {
for _, keyword := range *keywords {
if strings.Contains(strings.ToLower(scanner.Text()),
strings.ToLower(keyword)) {
nc := NCode{lineNbr, keyword}
apf.NCode = append(apf.NCode, nc)
}
}
lineNbr++
}
return nil
}
// PrintNCSResults prints results of file scan
func (apf *APFile) PrintNCSResults() {
for _, ncode := range apf.NCode {
fmt.Println("Line:", ncode.Line, ", keyword:", ncode.Keyword)
}
}
// NewAPFile creates and returns a new APFile object
func NewAPFile(name string, path string, extension string) *APFile {
apf := &APFile{}
apf.Name = name
apf.Path = path
apf.Extension = extension
apf.NCode = []NCode{}
return apf
}