]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_path.go
Add tests and clean up
[rbdr/mobius] / hotline / file_path.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "encoding/binary"
5 "strings"
6)
7
8const 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"
14type FilePathItem struct {
15 Len byte
16 Name []byte
17}
18
19func NewFilePathItem(b []byte) FilePathItem {
20 return FilePathItem{
21 Len: b[2],
22 Name: b[3:],
23 }
24}
25
26type FilePath struct {
72dd37f1
JH
27 ItemCount []byte
28 Items []FilePathItem
6988a057
JH
29}
30
72dd37f1
JH
31func (fp *FilePath) UnmarshalBinary(b []byte) error {
32 fp.ItemCount = b[0:2]
6988a057 33
6988a057 34 pathData := b[2:]
72dd37f1 35 for i := uint16(0); i < fp.Len(); i++ {
6988a057 36 segLen := pathData[2]
72dd37f1 37 fp.Items = append(fp.Items, NewFilePathItem(pathData[:segLen+3]))
6988a057
JH
38 pathData = pathData[3+segLen:]
39 }
40
72dd37f1
JH
41 return nil
42}
43
44func (fp *FilePath) Len() uint16 {
45 return binary.BigEndian.Uint16(fp.ItemCount)
6988a057
JH
46}
47
48func (fp *FilePath) String() string {
49 var out []string
72dd37f1 50 for _, i := range fp.Items {
6988a057
JH
51 out = append(out, string(i.Name))
52 }
53 return strings.Join(out, pathSeparator)
54}