/
searchTarget.go
294 lines (250 loc) · 6.24 KB
/
searchTarget.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"fmt"
"strconv"
"io"
"strings"
"bufio"
"os"
"github.com/gotools/logs"
"github.com/gotools/files"
"github.com/gotools/lists/fixedlist"
"errors"
)
const (
SEARCH_NO_MATCH = 1
SEARCH_MATCH = 2
SEARCH_NO_MATCH_UP = 3
SEARCH_MATCH_GREP_LINE = 4
)
func SearchMatch(f *os.File, targetArray []string, checkfile string) {
sourceBuf := bufio.NewReader(f)
index := 0
lineNo := 0
resultArray := make([]string, len(targetArray))
for {
line, err := sourceBuf.ReadString('\n')
if err != nil || io.EOF == err {
break
}
lineNo = lineNo + 1
if strings.Contains(line, targetArray[index]) {
line = fmt.Sprintf("%s:%d %s", checkfile, lineNo, line)
resultArray[index] = line
index = index + 1
} else if index < len(targetArray) {
index = 0
continue
}
if index == len(targetArray) {
for _, data := range resultArray {
logs.Debug(data)
}
index = 0
}
}
}
func SearchNoMatch(f *os.File, targetArray []string, checkfile string) {
sourceBuf := bufio.NewReader(f)
index := 0
lineNo := 0
resultArray := make([]string, len(targetArray))
for {
line, err := sourceBuf.ReadString('\n')
if err != nil || io.EOF == err {
break
}
lineNo = lineNo + 1
line = fmt.Sprintf("%s:%d %s", checkfile, lineNo, line)
if strings.Contains(line, targetArray[index]) {
resultArray[index] = line
index = index + 1
} else if index < len(targetArray) && index > 0 {
for i:= 0; i < index; i++ {
logs.Debug(resultArray[i])
}
logs.Debug(line)
index = 0
continue
}
if index >= len(targetArray) {
index = 0
}
}
}
func SearchNoMatchUp(f *os.File, targetArray []string, checkfile string) {
sourceBuf := bufio.NewReader(f)
index := 0
lineNo := 0
if len(targetArray) < 2 {
logs.Debug("the target string must exceed two lines or two lines.")
return
}
resultArray := make([]string, len(targetArray))
for {
line, err := sourceBuf.ReadString('\n')
if err != nil || io.EOF == err {
break
}
lineNo = lineNo + 1
line = fmt.Sprintf("%s:%d %s", checkfile, lineNo, line)
if index == 0 && strings.Contains(line, targetArray[index]) == false {
resultArray[index] = line
index = index + 1
} else if index == 1 && strings.Contains(line, targetArray[index]) == false && strings.Contains(line, targetArray[0]) == false {
index = 0;
resultArray[index] = line
index = index + 1
} else if index == 1 && strings.Contains(line, targetArray[index]) {
for i:= 0; i < index; i++ {
logs.Debug(resultArray[i])
}
logs.Debug(line)
index = 0
} else {
index = 0
}
}
}
func SearchMatchGrepLine(f *os.File, targetArray []string, checkfile string) {
//read the number of line from args
printNum := 1
lineNum := 1
if len(os.Args) >= 5 {
lineNum,_ = strconv.Atoi(os.Args[4])
}
if lineNum <= 0 {
lineNum = 1
}
sourceBuf := bufio.NewReader(f)
index := 0
lineNo := 0
fixedList := fixedlist.New(len(targetArray) + lineNum)
bakFixedList := fixedlist.New(lineNum)
for {
line, err := sourceBuf.ReadString('\n')
if err != nil || io.EOF == err {
break
}
lineNo = lineNo + 1
line = fmt.Sprintf("%s:%d %s", checkfile, lineNo, line)
if strings.Contains(line, targetArray[index]) {
index = index + 1
fixedList.Add(line)
} else {
fixedList.Add(line)
index = 0
}
if index < len (targetArray) {
continue
}
if index >= len(targetArray) {
tmpLineNum := 1
for {
//read lineNum line to bakFixedList
line, err := sourceBuf.ReadString('\n')
if err != nil || io.EOF == err {//read finished
break
}
lineNo = lineNo + 1
line = fmt.Sprintf("%s:%d %s", checkfile, lineNo, line)
bakFixedList.Add(line)
tmpLineNum = tmpLineNum + 1
if tmpLineNum >= lineNum {
break
}
}
}
if (index >= len(targetArray)) {
//print fixedList comment
logs.Debug("xxxxxxxxxxxxxx%d timesxxxxxxxxxxxx\n", printNum)
printNum = printNum + 1
for {
v := fixedList.PopFront()
if v == nil {
break
}
logs.Debug("%s", v)
}
//print bakFixedList comment
for {
v := bakFixedList.PopFront()
if v == nil {
break
}
logs.Debug("%s", v)
fixedList.Add(v)
}
}
index = 0
}
}
func HandleArg() (string, string, int, error){
targetFile := "target.txt"
checkFile := "source.txt"
searchType := SEARCH_NO_MATCH_UP
var err error
if len(os.Args) > 1 && len(os.Args) < 4 {
logs.Debug("usage searchTarget error, please use help")
return "", "", 0, errors.New("usage error")
}
if len(os.Args) >= 4 {
searchType, err = strconv.Atoi(os.Args[3])
if err == nil {
targetFile = os.Args[1]
checkFile = os.Args[2]
} else {
searchType = SEARCH_NO_MATCH_UP
}
}
typeStr := "SEARCH_NO_MATCH"
if searchType == SEARCH_MATCH {
typeStr = "SEARCH_MATCH"
} else if searchType == SEARCH_NO_MATCH_UP {
typeStr = "SEARCH_NO_MATCH_UP"
}
logs.Debug("targetFileName:%s, checkFileName:%s, searchType:%s\n", targetFile, checkFile, typeStr)
return targetFile, checkFile, searchType, nil
}
func Help() bool {
if len(os.Args) == 2 && strings.EqualFold(os.Args[1], "help") {
logs.Debug("Usage: searchTarget [targetFileName checkFileName searchType][line num]")
logs.Debug("searchType have two values 1: SEARCH_NO_MATCH 2:SEARCH_MATCH 3:SEARCH_NO_MATCH_UP 4:SEARCH_MATCH_GREP_LINE")
logs.Debug("when use SEARCH_MATCH_GREP_LINE searchType, need line num parameter")
return true
}
return false
}
func main() {
logs.SetUsePrefix(false)
if Help() {
return
}
fileName, sourceName, searchType, err := HandleArg()
if err != nil {
return
}
targetArray, err := file.ReadFile(fileName)
if err != nil {
logs.Debug("open file(%s) error:%s", fileName, err.Error())
return
}
sourceFile, err := os.Open(sourceName)
if err != nil {
logs.Debug("open file(%s) error: %s", sourceName, err.Error())
return
}
defer sourceFile.Close()
switch searchType {
case SEARCH_NO_MATCH:
SearchNoMatch(sourceFile, targetArray, sourceName)
case SEARCH_MATCH:
SearchMatch(sourceFile, targetArray, sourceName)
case SEARCH_NO_MATCH_UP:
SearchNoMatchUp(sourceFile, targetArray, sourceName)
case SEARCH_MATCH_GREP_LINE:
SearchMatchGrepLine(sourceFile, targetArray, sourceName)
default:
logs.Debug("no match search type")
}
}