-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquery-directive.go
48 lines (46 loc) · 1.14 KB
/
query-directive.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
package main
import (
"io/ioutil"
"strings"
"os"
"strconv"
"bufio"
"fmt"
)
type Mapping struct{
linenum int64
text string
}
func main () {
data, err := ioutil.ReadFile("directives.db")
if err != nil {
panic(err)
}
db := make(map[string][]Mapping)
for _, line := range strings.Split(string(data), "\n") {
cols := strings.Split(line, ":")
if len(cols) != 3 {
continue
}
filename := cols[0]
linenum, _ := strconv.ParseInt(cols[1], 10, 32)
db[filename] = append(db[filename], Mapping{linenum, cols[2]})
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
query := scanner.Text()
cols := strings.Split(query, ":")
filename := cols[0]
linenum,_ := strconv.ParseInt(cols[1], 10, 32)
if mappings, ok := db[filename]; ok {
for i, m := range mappings {
if linenum < m.linenum {
if i > 0 {
fmt.Println(mappings[i-1].text)
}
break
}
}
}
}
}