Debian Codegraph @

charset/file.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package charset
2
3 import (
4 "io"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8 )
9
10 var files = make(map[string]func() (io.ReadCloser, error))
11
12 // RegisterDataFile registers the existence of a given data
13 // file with the given name that may be used by a character-set converter.
14 // It is intended to be used by packages that wish to embed
15 // data in the executable binary, and should not be
16 // used normally.
17 func RegisterDataFile(name string, open func() (io.ReadCloser, error)) {
18 files[name] = open
19 }
20
21 // CharsetDir gives the location of the default data file directory.
22 // This directory will be used for files with names that have not
23 // been registered with RegisterDataFile.
•24 var CharsetDir = "../../datafiles"
25
26 func readFile(name string) (data []byte, err error) {
27 var r io.ReadCloser
28 if open := files[name]; open != nil {
29 r, err = open()
30 if err != nil {
31 return
32 }
33 } else {
34 r, err = os.Open(filepath.Join(CharsetDir, name))
35 if err != nil {
36 return
37 }
38 }
39 return ioutil.ReadAll(r)
40 }