-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.go
81 lines (66 loc) · 1.56 KB
/
include.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
// Includer stores context of includes
type Includer struct {
includedirs []string
dirContens map[string]map[string]bool
fileDeps map[string][]string
}
// NewIncluder creates includer
func NewIncluder(pathes []string) *Includer {
includer := Includer{pathes, make(map[string]map[string]bool), make(map[string][]string)}
// read directory contencs of all include dirs once
for _, i := range pathes {
var err error
f, err := os.Open(i)
if err != nil {
panic("could not open dir " + i)
}
defer f.Close()
includer.dirContens[i] = make(map[string]bool)
filelist, _ := f.Readdirnames(-1)
for _, de := range filelist {
includer.dirContens[i][de] = true
}
if err != nil {
panic("error in reading dir " + i)
}
}
return &includer
}
// ProcessFile ...
func (i *Includer) ProcessFile(file string) []string {
dependencies := make([]string, 0, 1024)
f, err := os.Open(file)
if err != nil {
panic("could not open file " + file)
}
defer f.Close()
fmt.Println("reading ", file)
regex, _ := regexp.Compile(`^\s*#\s*include\s*([<|"])(.*)[>|"]`)
reader := bufio.NewReader(f)
for {
line, _, err := reader.ReadLine()
if err != nil {
break
}
match := regex.FindSubmatch(line)
if match != nil {
// delim := string(match[1])
includename := string(match[2])
fmt.Println(includename)
for _, ii := range i.includedirs {
_, ok := i.dirContens[ii][includename]
if ok {
dependencies = append(dependencies, ii+"/"+includename)
}
}
}
}
return dependencies
}