~sbinet/star-tex

star-tex/kpath/db.go -rw-r--r-- 992 bytes
9bb27e8aSebastien Binet ci: bump to freebsd/latest 7 months ago
                                                                                
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
// Copyright ©2021 The star-tex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kpath

import (
	"bufio"
	"fmt"
	"io"
	stdpath "path"
	"strings"
)

func parseDB(root string, r io.Reader) (Context, error) {
	db := make(map[string][]string)
	sc := bufio.NewScanner(r)
	dir := root
	for sc.Scan() {
		txt := strings.TrimSpace(sc.Text())
		if txt == "" {
			continue
		}
		if txt[0] == '%' {
			continue
		}

		switch {
		case isDirDB(txt):
			dir = stdpath.Join(root, strings.TrimRight(txt, ":"))
		default:
			db[txt] = append(db[txt], stdpath.Join(dir, txt))
		}
	}

	err := sc.Err()
	if err != nil && err != io.EOF {
		return Context{}, fmt.Errorf("could not scan db file: %w", err)
	}

	return Context{db: db}, nil
}

func isDirDB(name string) bool {
	return strings.HasSuffix(name, ":") && (strings.HasPrefix(name, "/") ||
		strings.HasPrefix(name, "./") ||
		strings.HasPrefix(name, "../"))
}