-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpg.go
44 lines (39 loc) · 879 Bytes
/
cpg.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
package shapefile
import (
"archive/zip"
"fmt"
"io"
"strings"
"golang.org/x/net/html/charset"
)
// CPG a CPG is a .cpg file.
type CPG struct {
Charset string
}
// ReadCPG reads a CPG from an io.Reader.
func ReadCPG(r io.Reader, _ int64) (*CPG, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
enc, name := charset.Lookup(strings.ToLower(string(data)))
if enc == nil {
return nil, fmt.Errorf("unknown charset '%s'", (string(data)))
}
return &CPG{
Charset: name,
}, nil
}
// ReadCPGZipFile reads a CPG from a *zip.File.
func ReadCPGZipFile(zipFile *zip.File) (*CPG, error) {
readCloser, err := zipFile.Open()
if err != nil {
return nil, err
}
defer readCloser.Close()
cpg, err := ReadCPG(readCloser, int64(zipFile.UncompressedSize64))
if err != nil {
return nil, fmt.Errorf("%s: %w", zipFile.Name, err)
}
return cpg, nil
}