nori/decode.go

43 lines
821 B
Go

package nori
import (
"bytes"
"fmt"
"github.com/suapapa/go_hangul/encoding/cp949"
"io"
"os"
)
func New() *Nori {
return &Nori{}
}
func FromFile(fp string) (*Nori, error) {
file, err := os.Open(fp)
if err != nil {
return nil, err
}
defer file.Close()
return Decode(file)
}
func Decode(r io.Reader) (*Nori, error) {
nori := New()
err := nori.Decode(r)
return nori, err
}
func GetAnimationNames(n *Nori) (map[string]int, error) {
names := make(map[string]int, n.AnimationCount)
for i := range n.Animations {
anim := n.Animations[i]
title, codepageError := cp949.From(anim.Title[:])
if codepageError != nil {
return nil, fmt.Errorf("couldn't convert title of animation %d", i)
}
cleanTitle := string(bytes.SplitN(title[:], []byte{0}, 2)[0])
names[cleanTitle] = i
}
return names, nil
}