-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
94 lines (81 loc) · 2.41 KB
/
Copy pathparser.go
File metadata and controls
94 lines (81 loc) · 2.41 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package imc
import (
"encoding/xml"
"fmt"
"io"
"os"
)
type XMLProtocol struct {
XMLName xml.Name `xml:"messages"`
Name string `xml:"name,attr"`
Version string `xml:"version,attr"`
Types []XMLType `xml:"types>type"`
Enums []XMLEnum `xml:"enumerations>def"`
Bitfields []XMLBitfield `xml:"bitfields>def"`
Groups []XMLMessageGroup `xml:"message-groups>message-group"`
Header []XMLField `xml:"header>field"`
Footer []XMLField `xml:"footer>field"`
Messages []XMLMessage `xml:"message"`
}
type XMLType struct {
Name string `xml:"name,attr"`
Size int `xml:"size,attr"`
}
type XMLEnum struct {
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
Prefix string `xml:"prefix,attr"`
Values []XMLValue `xml:"value"`
}
type XMLBitfield struct {
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
Prefix string `xml:"prefix,attr"`
Values []XMLValue `xml:"value"`
}
type XMLValue struct {
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
}
type XMLMessageGroup struct {
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
MsgTypes []XMLMessageType `xml:"message-type"`
}
type XMLMessageType struct {
Abbrev string `xml:"abbrev,attr"`
}
type XMLMessage struct {
ID uint16 `xml:"id,attr"`
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
Fields []XMLField `xml:"field"`
}
type XMLField struct {
Name string `xml:"name,attr"`
Abbrev string `xml:"abbrev,attr"`
Type string `xml:"type,attr"`
Unit string `xml:"unit,attr"`
Value string `xml:"value,attr"`
Fixed bool `xml:"fixed,attr"`
EnumDef string `xml:"enum-def,attr"`
Bitfield string `xml:"bitfield-def,attr"` // Check if it's bitfield or bitfield-def in XML
Values []XMLValue `xml:"value"` // Inline values for enums/bitfields
}
func ParseXML(path string) (*XMLProtocol, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return ParseReader(file)
}
func ParseReader(r io.Reader) (*XMLProtocol, error) {
var proto XMLProtocol
decoder := xml.NewDecoder(r)
if err := decoder.Decode(&proto); err != nil {
return nil, fmt.Errorf("failed to decode IMC XML: %w", err)
}
return &proto, nil
}