mirror of
https://github.com/cjdenio/crossword.git
synced 2026-10-03 11:06:19 +00:00
fix text encoding for utf-8 puz files
This commit is contained in:
1 file changed
+15
-10
+15
-10
@@ -75,18 +75,22 @@ func startOfAcrossClue(puzzle string, width, index int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func readText(b *bufio.Reader) (string, error) {
|
func readText(b *bufio.Reader, version string) (string, error) {
|
||||||
text, err := b.ReadString(0x00)
|
text, err := b.ReadString(0x00)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
decoded, err := charmap.ISO8859_1.NewDecoder().String(text[:len(text)-1])
|
if version == "1" {
|
||||||
if err != nil {
|
decoded, err := charmap.ISO8859_1.NewDecoder().String(text[:len(text)-1])
|
||||||
return "", err
|
if err != nil {
|
||||||
}
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
return decoded, nil
|
return decoded, nil
|
||||||
|
} else {
|
||||||
|
return string(text[:len(text)-1]), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParsePuz(file []byte) (*Puzzle, error) {
|
func ParsePuz(file []byte) (*Puzzle, error) {
|
||||||
@@ -99,6 +103,7 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
puzzle := new(Puzzle)
|
puzzle := new(Puzzle)
|
||||||
|
|
||||||
// read header
|
// read header
|
||||||
|
version := string(file[0x18:0x1B])
|
||||||
puzzle.Width = int(file[0x2C])
|
puzzle.Width = int(file[0x2C])
|
||||||
puzzle.Height = int(file[0x2D])
|
puzzle.Height = int(file[0x2D])
|
||||||
puzzle.ClueCount = int(binary.LittleEndian.Uint16(file[0x2E:0x30]))
|
puzzle.ClueCount = int(binary.LittleEndian.Uint16(file[0x2E:0x30]))
|
||||||
@@ -116,19 +121,19 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
// read text
|
// read text
|
||||||
buf := bufio.NewReader(bytes.NewReader(file[stateEnd:]))
|
buf := bufio.NewReader(bytes.NewReader(file[stateEnd:]))
|
||||||
|
|
||||||
title, err := readText(buf)
|
title, err := readText(buf, string(version[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
puzzle.Title = title
|
puzzle.Title = title
|
||||||
|
|
||||||
author, err := readText(buf)
|
author, err := readText(buf, string(version[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
puzzle.Author = author
|
puzzle.Author = author
|
||||||
|
|
||||||
copyright, err := readText(buf)
|
copyright, err := readText(buf, string(version[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -137,7 +142,7 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
// read clues
|
// read clues
|
||||||
clues := make([]string, 0, puzzle.ClueCount)
|
clues := make([]string, 0, puzzle.ClueCount)
|
||||||
for range puzzle.ClueCount {
|
for range puzzle.ClueCount {
|
||||||
clue, err := readText(buf)
|
clue, err := readText(buf, string(version[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user