1
0
forked from MTSR/mapserver

parse test

This commit is contained in:
Thomas Rudin 2019-01-07 16:35:26 +01:00
parent 9afcfed658
commit 904cb3496d
3 changed files with 37 additions and 12 deletions

View File

@ -11,17 +11,17 @@ type MapBlock struct {
type Metadata struct {
Inventories map[int]map[string]*Inventory
Pairs map[int]map[string]*string
Pairs map[int]map[string]string
}
func (md *Metadata) GetPairsMap(pos int) map[string]*string {
func (md *Metadata) GetPairsMap(pos int) map[string]string {
if md.Pairs == nil {
md.Pairs = make(map[int]map[string]*string)
md.Pairs = make(map[int]map[string]string)
}
pairsMap := md.Pairs[pos]
if pairsMap == nil {
pairsMap = make(map[string]*string)
pairsMap = make(map[string]string)
md.Pairs[pos] = pairsMap
}

View File

@ -49,6 +49,10 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
buf := new(bytes.Buffer)
io.Copy(buf, z)
if cr.Count == 0 {
return 0, errors.New("no data")
}
metadata := buf.Bytes()
offset := 0
@ -86,17 +90,14 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
offset+=keyLength
valueLength := readU32(metadata, offset)
offset+=4;
offset+=4
value := string(metadata[offset:keyLength+offset])
value := string(metadata[offset:valueLength+offset])
offset+=valueLength
pairsMap[key] = &value
pairsMap[key] = value
offset++
log.Println("MD item", i, offset, position, valuecount, valueLength, keyLength, pairsMap)//XXX
}
var currentInventoryName *string = nil
@ -108,7 +109,7 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
txt := scanner.Text()
offset += len(txt) + 1;
log.Println("inv", txt)
log.Println("inv", txt)//XXX
if txt == INVENTORY_END {
currentInventoryName = nil

View File

@ -1,6 +1,7 @@
package mapblockparser
import (
"fmt"
"testing"
"io/ioutil"
"strconv"
@ -40,6 +41,7 @@ func TestParse(t *testing.T){
}
mapblock, err := Parse(data)
fmt.Println("mapblock.Metadata", mapblock.Metadata)
if err != nil {
t.Error(err)
@ -56,4 +58,26 @@ func TestParse(t *testing.T){
if len(mapblock.Mapdata) != 16384 {
t.Error("Mapdata length wrong")
}
}
pairs := mapblock.Metadata.GetPairsMap(0)
if pairs["owner"] != "pipo" {
t.Error(pairs["owner"])
}
}
func TestParse2(t *testing.T){
log.SetLevel(log.DebugLevel)
data, err := ioutil.ReadFile("testdata/0.9.0")
if err != nil {
t.Error(err)
}
_, err = Parse(data)
//fmt.Println("mapblock.Metadata", mapblock.Metadata)
if err != nil {
t.Error(err)
}
}