]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "strings" | |
6 | ) | |
7 | ||
8 | const pathSeparator = "/" // File path separator TODO: make configurable to support Windows | |
9 | ||
10 | // FilePathItem represents the file or directory portion of a delimited file path (e.g. foo and bar in "/foo/bar") | |
11 | // 00 00 | |
12 | // 09 | |
13 | // 73 75 62 66 6f 6c 64 65 72 // "subfolder" | |
14 | type FilePathItem struct { | |
15 | Len byte | |
16 | Name []byte | |
17 | } | |
18 | ||
19 | func NewFilePathItem(b []byte) FilePathItem { | |
20 | return FilePathItem{ | |
21 | Len: b[2], | |
22 | Name: b[3:], | |
23 | } | |
24 | } | |
25 | ||
26 | type FilePath struct { | |
27 | ItemCount []byte | |
28 | Items []FilePathItem | |
29 | } | |
30 | ||
31 | func (fp *FilePath) UnmarshalBinary(b []byte) error { | |
32 | fp.ItemCount = b[0:2] | |
33 | ||
34 | pathData := b[2:] | |
35 | for i := uint16(0); i < fp.Len(); i++ { | |
36 | segLen := pathData[2] | |
37 | fp.Items = append(fp.Items, NewFilePathItem(pathData[:segLen+3])) | |
38 | pathData = pathData[3+segLen:] | |
39 | } | |
40 | ||
41 | return nil | |
42 | } | |
43 | ||
44 | func (fp *FilePath) Len() uint16 { | |
45 | return binary.BigEndian.Uint16(fp.ItemCount) | |
46 | } | |
47 | ||
48 | func (fp *FilePath) String() string { | |
49 | var out []string | |
50 | for _, i := range fp.Items { | |
51 | out = append(out, string(i.Name)) | |
52 | } | |
53 | return strings.Join(out, pathSeparator) | |
54 | } |