-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
82 lines (75 loc) · 2.04 KB
/
parser.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
package zombie
import (
"bufio"
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
)
//Parse format file to generates the candidates
func Parse(file string) (<-chan string, *sync.WaitGroup) {
out := make(chan string, runtime.NumCPU()*10)
var wg sync.WaitGroup
//https://golang.org/pkg/sync/#WaitGroup.Add
// This ensure that the first call to wait happens with the waitGroup greater than 0
wg.Add(1)
format, err := os.Open(file)
if err != nil {
log.Fatalf("Failed to: %v", err)
}
defer format.Close()
scanner := bufio.NewScanner(format)
formatArray := [][]string{}
var lineNumber int
for scanner.Scan() {
lineNumber++
line := scanner.Text()
fmt.Println(line)
formatArray = append(formatArray, readFormat(line, lineNumber))
}
go buildCandidates(&wg, formatArray, out, "", true)
return out, &wg
}
// read the format line and put it in an array of candidates
// one time symbol between parts candidates
// first symbol is a delimiter follow by a part
// repeat the delimiter before each part
// ex: aEa3 => [E 3]
// ex: !g!d!e!p => [g d e p]
// ex: %OUI%NON%YES%NO => [OUI NON YES NO]
func readFormat(line string, lineNumber int) []string {
length := len(line)
if length == 0 || length == 1 {
log.Fatalf("Format of file is wrong on line: %d", lineNumber)
}
return strings.Split(line, string(line[0]))[1:]
}
//buildCandidates actually builds the candidates from the formatArray and send them to
//the out channel
func buildCandidates(wg *sync.WaitGroup, candidates [][]string, out chan<- string, part string, firstCall bool) {
if firstCall {
defer close(out)
defer wg.Done()
}
if len(candidates) == 0 {
wg.Add(1)
out <- part
//fmt.Println(part)
} else if len(candidates) == 1 {
for _, element := range candidates[0] {
buildCandidates(wg, [][]string{}, out, part+element, false)
}
} else {
for _, element := range candidates[0] {
buildCandidates(wg, candidates[1:], out, part+element, false)
}
}
}
//Deprecated. The new one use Dispatcher func
func oldPrint(in <-chan string) {
for elem := range in {
fmt.Println(elem)
}
}