File: file.go

package info (click to toggle)
golang-github-paulrosania-go-charset 0.0~git20151028.0.621bb39-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 504 kB
  • ctags: 236
  • sloc: makefile: 9
file content (40 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download
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
package charset

import (
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
)

var files = make(map[string]func() (io.ReadCloser, error))

// RegisterDataFile registers the existence of a given data
// file with the given name that may be used by a character-set converter.
// It is intended to be used by packages that wish to embed
// data in the executable binary, and should not be
// used normally.
func RegisterDataFile(name string, open func() (io.ReadCloser, error)) {
	files[name] = open
}

// CharsetDir gives the location of the default data file directory.
// This directory will be used for files with names that have not
// been registered with RegisterDataFile.
var CharsetDir = "../../../../../../../datafiles"

func readFile(name string) (data []byte, err error) {
	var r io.ReadCloser
	if open := files[name]; open != nil {
		r, err = open()
		if err != nil {
			return
		}
	} else {
		r, err = os.Open(filepath.Join(CharsetDir, name))
		if err != nil {
			return
		}
	}
	return ioutil.ReadAll(r)
}