]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
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 { | |
72dd37f1 JH |
27 | ItemCount []byte |
28 | Items []FilePathItem | |
6988a057 JH |
29 | } |
30 | ||
72dd37f1 JH |
31 | func (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 | ||
44 | func (fp *FilePath) Len() uint16 { | |
45 | return binary.BigEndian.Uint16(fp.ItemCount) | |
6988a057 JH |
46 | } |
47 | ||
48 | func (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 | } | |
c5d9af5a JH |
55 | |
56 | func ReadFilePath(filePathFieldData []byte) string { | |
57 | var fp FilePath | |
58 | err := fp.UnmarshalBinary(filePathFieldData) | |
59 | if err != nil { | |
60 | // TODO | |
61 | } | |
62 | return fp.String() | |
63 | } |