optimize cp473 converter

This commit is contained in:
Robert Janetzko 2022-04-20 10:46:42 +00:00
parent 5f461c4f0f
commit f038523b46
6 changed files with 287 additions and 274 deletions

View File

@ -173,7 +173,7 @@ func parse{{ $obj.Name }}{{ if $plus }}Plus{{ end }}(p *util.XMLParser{{ if $plu
if err != nil { if err != nil {
return nil, err return nil, err
} }
switch data { switch string(data) {
{{- range $sub := ($obj.ActiveSubTypes $plus) }} {{- range $sub := ($obj.ActiveSubTypes $plus) }}
case "{{ $sub.Case }}": case "{{ $sub.Case }}":
{{- if eq 1 (len $sub.Options) }} {{- if eq 1 (len $sub.Options) }}
@ -356,20 +356,20 @@ func (f Field) StartAction(obj Object, plus bool) string {
if f.Type == "int" { if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = num(data)", s, n) return fmt.Sprintf("%sobj.%s = num(data)", s, n)
} else if f.Type == "string" { } else if f.Type == "string" {
return fmt.Sprintf("%sobj.%s = string(data)", s, n) return fmt.Sprintf("%sobj.%s = txt(data)", s, n)
} else if f.Type == "bool" { } else if f.Type == "bool" {
s := "_, err := p.Value()\nif err != nil { return nil, err }\n" s := "_, err := p.Value()\nif err != nil { return nil, err }\n"
return fmt.Sprintf("%sobj.%s = true", s, n) return fmt.Sprintf("%sobj.%s = true", s, n)
} else if f.Type == "enum" { } else if f.Type == "enum" {
return fmt.Sprintf("%sobj.%s = parse%s%s(string(data))", s, n, obj.Name, n) return fmt.Sprintf("%sobj.%s = parse%s%s(txt(data))", s, n, obj.Name, n)
} }
} else { } else {
if f.Type == "int" { if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, num(data))", s, n, n) return fmt.Sprintf("%sobj.%s = append(obj.%s, num(data))", s, n, n)
} else if f.Type == "string" { } else if f.Type == "string" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, string(data))", s, n, n) return fmt.Sprintf("%sobj.%s = append(obj.%s, txt(data))", s, n, n)
} else if f.Type == "enum" { } else if f.Type == "enum" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, parse%s%s(string(data)))", s, n, n, obj.Name, n) return fmt.Sprintf("%sobj.%s = append(obj.%s, parse%s%s(txt(data)))", s, n, n, obj.Name, n)
} }
} }
} }

View File

@ -29,6 +29,7 @@ var static embed.FS
func main() { func main() {
f := flag.String("f", "", "open a file") f := flag.String("f", "", "open a file")
p := flag.Bool("p", false, "start profiling")
flag.Parse() flag.Parse()
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
@ -116,10 +117,12 @@ func main() {
t := templates.New(functions) t := templates.New(functions)
if len(*f) > 0 { if len(*f) > 0 {
defer profile.Start(profile.ProfilePath(".")).Stop() if *p {
go func() { defer profile.Start(profile.ProfilePath(".")).Stop()
http.ListenAndServe(":8081", nil) go func() {
}() http.ListenAndServe(":8081", nil)
}()
}
w, err := model.Parse(*f) w, err := model.Parse(*f)
if err != nil { if err != nil {

File diff suppressed because it is too large Load Diff

View File

@ -55,8 +55,7 @@ func NewLegendsParser(file string) (*util.XMLParser, *os.File, *pb.ProgressBar,
fmt.Println("Successfully Opened", file) fmt.Println("Successfully Opened", file)
converter := util.NewConvertReader(xmlFile) barReader := bar.NewProxyReader(xmlFile)
barReader := bar.NewProxyReader(converter)
d := util.NewXMLParser(bufio.NewReader(barReader)) d := util.NewXMLParser(bufio.NewReader(barReader))
return d, xmlFile, bar, err return d, xmlFile, bar, err
@ -198,11 +197,11 @@ func parseId(p *util.XMLParser) (int, error) {
switch t { switch t {
case util.StartElement: case util.StartElement:
if n == "id" { if n == "id" {
s, err := p.Value() d, err := p.Value()
if err != nil { if err != nil {
return -1, err return -1, err
} }
return strconv.Atoi(s) return strconv.Atoi(string(d))
} else { } else {
p.Skip() p.Skip()
} }
@ -210,11 +209,15 @@ func parseId(p *util.XMLParser) (int, error) {
} }
} }
func num(s string) int { func num(b []byte) int {
v, _ := strconv.Atoi(s) v, _ := strconv.Atoi(string(b))
return v return v
} }
func txt(b []byte) string {
return util.ConvertCp473(b)
}
var sameFields map[string]map[string]map[string]bool var sameFields map[string]map[string]map[string]bool
func exportSameFields() map[string]map[string]string { func exportSameFields() map[string]map[string]string {

View File

@ -26,3 +26,10 @@ func (c *ConvertReader) Read(b []byte) (n int, err error) {
} }
return n, err return n, err
} }
func ConvertCp473(b []byte) string {
for i := range b {
b[i] = cp437[b[i]]
}
return string(b)
}

View File

@ -88,10 +88,10 @@ func (x *XMLParser) Token() (TokenType, string, error) {
} }
} }
func (x *XMLParser) Value() (string, error) { func (x *XMLParser) Value() ([]byte, error) {
if x.selfClose { if x.selfClose {
x.selfClose = false x.selfClose = false
return "", nil return nil, nil
} }
x.scratch.reset() x.scratch.reset()
@ -101,12 +101,12 @@ func (x *XMLParser) Value() (string, error) {
for { for {
b, err := x.reader.ReadByte() b, err := x.reader.ReadByte()
if err != nil { if err != nil {
return "", err return nil, err
} }
if b == '<' { if b == '<' {
f = true f = true
} else if f && b == '>' { } else if f && b == '>' {
return string(x.scratch.bytes()), nil return x.scratch.bytes(), nil
} else if !f { } else if !f {
x.scratch.add(b) x.scratch.add(b)