blog/go/fonts/font/reader.go

171 lines
3.2 KiB
Go
Raw Normal View History

2024-09-13 15:10:58 +08:00
package font
import (
"bytes"
"encoding/binary"
"io"
"os"
)
type Reader interface {
Read([]byte) (int, error)
Close() error
ReadInt8() (int8, error)
ReadInt16() (int16, error)
ReadInt32() (int32, error)
ReadInt64() (int64, error)
ReadUint8() (uint8, error)
ReadUint16() (uint16, error)
ReadUint32() (uint32, error)
ReadUint64() (uint64, error)
ReadString(int) (string, error)
ReadBytes(int) ([]byte, error)
Seek(int64, int) (int64, error)
Size() (int64, error)
}
type reader struct {
path string
fp *os.File
}
func NewReader(file string) (Reader, error) {
fp, err := os.OpenFile(file, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
r := &reader{
fp: fp,
path: file,
}
return r, nil
}
func (r *reader) Read(p []byte) (int, error) {
if r.fp == nil {
return 0, nil
}
return r.fp.Read(p)
}
func (r *reader) Close() error {
if r.fp != nil {
return r.fp.Close()
}
return nil
}
func (r *reader) ReadCloser() io.ReadCloser {
return r
}
func (r *reader) ReadInt8() (int8, error) {
buff := make([]byte, 1)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
reader := bytes.NewReader(buff)
n := int8(0)
return n, binary.Read(reader, binary.LittleEndian, &n)
}
func (r *reader) ReadInt16() (int16, error) {
buff := make([]byte, 2)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
reader := bytes.NewReader(buff)
n := int16(0)
return n, binary.Read(reader, binary.LittleEndian, &n)
}
func (r *reader) ReadInt32() (int32, error) {
buff := make([]byte, 4)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
reader := bytes.NewReader(buff)
n := int32(0)
return n, binary.Read(reader, binary.LittleEndian, &n)
}
func (r *reader) ReadInt64() (int64, error) {
buff := make([]byte, 8)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
reader := bytes.NewReader(buff)
n := int64(0)
return n, binary.Read(reader, binary.LittleEndian, &n)
}
func (r *reader) ReadUint8() (uint8, error) {
buff := make([]byte, 1)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
return uint8(buff[0]), nil
}
func (r *reader) ReadUint16() (uint16, error) {
buff := make([]byte, 2)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(buff), nil
}
func (r *reader) ReadUint32() (uint32, error) {
buff := make([]byte, 4)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(buff), nil
}
func (r *reader) ReadUint64() (uint64, error) {
buff := make([]byte, 8)
_, err := r.fp.Read(buff)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(buff), nil
}
func (r *reader) ReadString(len int) (string, error) {
buff := make([]byte, len)
_, err := r.fp.Read(buff)
if err != nil {
return "", err
}
return string(buff), nil
}
func (r *reader) ReadBytes(len int) ([]byte, error) {
buff := make([]byte, len)
_, err := r.fp.Read(buff)
if err != nil {
return []byte{}, err
}
return buff, nil
}
func (r *reader) Seek(offset int64, whence int) (int64, error) {
return r.fp.Seek(offset, whence)
}
func (r *reader) Size() (int64, error) {
fi, err := r.fp.Stat()
if err != nil {
return 0, err
}
return fi.Size(), nil
}