File: hierarchy.go

package info (click to toggle)
golang-github-vbatts-go-mtree 0.5.4%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 796 kB
  • sloc: sh: 198; makefile: 80
file content (48 lines) | stat: -rw-r--r-- 1,109 bytes parent folder | download | duplicates (4)
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 mtree

import (
	"io"
	"sort"
)

// DirectoryHierarchy is the mapped structure for an mtree directory hierarchy
// spec
type DirectoryHierarchy struct {
	Entries []Entry
}

// WriteTo simplifies the output of the resulting hierarchy spec
func (dh DirectoryHierarchy) WriteTo(w io.Writer) (n int64, err error) {
	sort.Sort(byPos(dh.Entries))
	var sum int64
	for _, e := range dh.Entries {
		str := e.String()
		i, err := io.WriteString(w, str+"\n")
		if err != nil {
			return sum, err
		}
		sum += int64(i)
	}
	return sum, nil
}

// UsedKeywords collects and returns all the keywords used in a
// a DirectoryHierarchy
func (dh DirectoryHierarchy) UsedKeywords() []Keyword {
	usedkeywords := []Keyword{}
	for _, e := range dh.Entries {
		switch e.Type {
		case FullType, RelativeType, SpecialType:
			if e.Type != SpecialType || e.Name == "/set" {
				kvs := e.Keywords
				for _, kv := range kvs {
					kw := KeyVal(kv).Keyword().Prefix()
					if !InKeywordSlice(kw, usedkeywords) {
						usedkeywords = append(usedkeywords, KeywordSynonym(string(kw)))
					}
				}
			}
		}
	}
	return usedkeywords
}