-
Notifications
You must be signed in to change notification settings - Fork 14
/
data.go
95 lines (78 loc) · 1.95 KB
/
data.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
83
84
85
86
87
88
89
90
91
92
93
94
95
package tilepix
import (
"bytes"
"compress/gzip"
"compress/zlib"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
/*
___ _
| \ __ _| |_ __ _
| |) / _` | _/ _` |
|___/\__,_|\__\__,_|
*/
// Data is a TMX file structure holding data.
type Data struct {
Encoding string `xml:"encoding,attr"`
Compression string `xml:"compression,attr"`
RawData []byte `xml:",innerxml"`
// DataTiles is only used when layer encoding is XML.
DataTiles []*DataTile `xml:"tile"`
}
func (d *Data) String() string {
return fmt.Sprintf("Data{Compression: %s, DataTiles count: %d}", d.Compression, len(d.DataTiles))
}
func (d *Data) decodeBase64() (data []byte, err error) {
rawData := bytes.TrimSpace(d.RawData)
r := bytes.NewReader(rawData)
encr := base64.NewDecoder(base64.StdEncoding, r)
var comr io.Reader
switch d.Compression {
case "gzip":
log.Debug("decodeBase64: compression is gzip")
comr, err = gzip.NewReader(encr)
if err != nil {
return
}
case "zlib":
log.Debug("decodeBase64: compression is zlib")
comr, err = zlib.NewReader(encr)
if err != nil {
return
}
case "":
log.Debug("decodeBase64: no compression")
comr = encr
default:
err = ErrUnknownCompression
log.WithError(ErrUnknownCompression).WithField("Compression", d.Compression).Error("decodeBase64: unable to handle this compression type")
return
}
return ioutil.ReadAll(comr)
}
func (d *Data) decodeCSV() ([]GID, error) {
cleaner := func(r rune) rune {
if (r >= '0' && r <= '9') || r == ',' {
return r
}
return -1
}
rawDataClean := strings.Map(cleaner, string(d.RawData))
str := strings.Split(string(rawDataClean), ",")
gids := make([]GID, len(str))
for i, s := range str {
d, err := strconv.ParseUint(s, 10, 32)
if err != nil {
log.WithError(err).WithField("String to convert", s).Error("decodeCSV: could not parse UInt")
return nil, err
}
gids[i] = GID(d)
}
return gids, nil
}