]> git.r.bdr.sh - rbdr/mobius/blob - file_path.go
Initial squashed commit
[rbdr/mobius] / file_path.go
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 PathItemCount []byte
28 PathItems []FilePathItem
29 }
30
31 func NewFilePath(b []byte) FilePath {
32 if b == nil {
33 return FilePath{}
34 }
35
36 fp := FilePath{PathItemCount: b[0:2]}
37
38 // number of items in the path
39 pathItemLen := binary.BigEndian.Uint16(b[0:2])
40 pathData := b[2:]
41 for i := uint16(0); i < pathItemLen; i++ {
42 segLen := pathData[2]
43 fp.PathItems = append(fp.PathItems, NewFilePathItem(pathData[:segLen+3]))
44 pathData = pathData[3+segLen:]
45 }
46
47 return fp
48 }
49
50 func (fp *FilePath) String() string {
51 var out []string
52 for _, i := range fp.PathItems {
53 out = append(out, string(i.Name))
54 }
55 return strings.Join(out, pathSeparator)
56 }