-
Notifications
You must be signed in to change notification settings - Fork 0
/
plist.go
82 lines (74 loc) · 1.78 KB
/
plist.go
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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package loaders
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/limetext/loaders/plist"
"github.com/quarnster/parser"
)
func plistconv(buf *bytes.Buffer, node *parser.Node) error {
switch node.Name {
case "Key":
buf.WriteString("\"" + node.Data() + "\": ")
case "String":
n := node.Data()
n = strings.Replace(n, "\\", "\\\\", -1)
n = strings.Replace(n, "\"", "\\\"", -1)
n = strings.Replace(n, "\n", "\\n", -1)
n = strings.Replace(n, "\t", "\\t", -1)
n = strings.Replace(n, ">", ">", -1)
n = strings.Replace(n, "<", "<", -1)
buf.WriteString("\"" + n + "\"")
case "EmptyString":
buf.WriteString("\"\"")
case "Dictionary":
buf.WriteString("{\n\t")
for i, child := range node.Children {
if i != 0 && i&1 == 0 {
buf.WriteString(",\n\t")
}
if err := plistconv(buf, child); err != nil {
return err
}
}
buf.WriteString("}\n")
case "Array":
buf.WriteString("[\n\t")
for i, child := range node.Children {
if i != 0 {
buf.WriteString(",\n\t")
}
if err := plistconv(buf, child); err != nil {
return err
}
}
buf.WriteString("]\n\t")
case "Integer", "Bool":
buf.WriteString(node.Data())
case "EndOfFile":
default:
return errors.New(fmt.Sprintf("Unhandled node: %s", node.Name))
}
return nil
}
func LoadPlist(data []byte, intf interface{}) error {
var p plist.PLIST
r := strings.NewReplacer("\r", "", "\v", "")
if !p.Parse(r.Replace(string(data))) {
return p.Error()
}
var (
root = p.RootNode()
buf bytes.Buffer
)
for _, child := range root.Children {
if err := plistconv(&buf, child); err != nil {
return err
}
}
return LoadJSON(buf.Bytes(), intf)
}