-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (116 loc) · 2.76 KB
/
Copy pathmain.go
File metadata and controls
136 lines (116 loc) · 2.76 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
"time"
)
type word struct {
strOfInterest string
blocks []block
fileName string
}
type block struct {
blckURI string
repeats int
}
var Word word
func main() {
var filePath string
switch len(os.Args) {
case 1:
fmt.Println("type a file name: ")
fmt.Scan(&filePath)
fmt.Println("type the word to find in file: ")
fmt.Scan(&Word.strOfInterest)
case 2:
filePath = os.Args[1]
fmt.Println("type the word to find in file: ")
fmt.Scan(&Word.strOfInterest)
case 3:
filePath = os.Args[1]
Word.strOfInterest = os.Args[2]
default:
fmt.Println(`Number of imput arguments not 3.`)
fmt.Println("Format should be: app_name <file> <word>")
fmt.Println(`If <word> has spaces put the backslash before space: 'open\ the\ door'`)
return
}
f, err := os.Open(filePath)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
destFilePath := "recLog.txt"
fOut, err := os.OpenFile(destFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Println("error creating file:", destFilePath)
log.Fatalln(err)
}
defer fOut.Close()
m := findWord(f, Word)
err = record(fOut, m)
if err != nil {
log.Println("unable to record in to the file", destFilePath)
log.Fatalln(err)
}
}
func findWord(r io.Reader, w word) word {
reWord := regexp.MustCompile(`(?i)\b` + w.strOfInterest + `\b`)
reURI := regexp.MustCompile(`\w+:(\/?\/?)[^\s]+`)
scanner := bufio.NewScanner(r)
var text string
scanner.Split(crunchSplitFunc)
for scanner.Scan() {
var b block
text = scanner.Text()
mtchWord := reWord.FindAllStringSubmatch(text, -1)
b.blckURI = reURI.FindString(text)
b.repeats = len(mtchWord)
w.blocks = append(w.blocks, b)
}
return w
}
func record(r io.Writer, w word) error {
log1 := "-------------------------\n" + time.Now().Format(time.UnixDate) + "\nResults fo finding of word '" + w.strOfInterest + "':\n"
_, err := fmt.Fprintf(r, log1)
if err != nil {
return err
}
for i, v := range w.blocks {
a := i + 1
log2 := "Blokc number " + strconv.Itoa(a)
_, err := fmt.Fprintln(r, log2)
if err != nil {
return err
}
log3 := "At URI: " + v.blckURI
_, err = fmt.Fprintln(r, log3)
if err != nil {
return err
}
log4 := "Word '" + w.strOfInterest + "' was found " + strconv.Itoa(v.repeats) + " times."
_, err = fmt.Fprintln(r, log4)
if err != nil {
return err
}
}
return nil
}
func crunchSplitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
re := regexp.MustCompile(`\nFILE [\d]+\.[\d]+`)
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := re.FindStringIndex(string(data)); len(i) > 0 && i[0] >= 0 {
return i[0] + 1, data[0:i[0]], nil
}
if atEOF {
return len(data), data, nil
}
return
}