示例#1
0
func ReadString(fname string) (s string, err error) {
	err = file.Read(fname, func(fd *os.File) error {
		s, err = encodeio.ReadString(fd)

		return err
	})

	return
}
示例#2
0
func ReadGZIP(fname string) (data []byte, err error) {
	err = file.Read(fname, func(fd *os.File) error {
		data, err = encodeio.ReadGZIP(fd)

		return err
	})

	return
}
示例#3
0
func ReadCommenttedJSON(fname, comment string, v interface{}) error {
	var trim = func(line, delim []byte) []byte {
		if len(delim) > 0 && bytes.HasPrefix(bytes.TrimSpace(line), delim) {
			return nil
		}

		return line
	}

	return file.Read(fname, func(fd *os.File) error {
		bs, err := ioutil.ReadAll(fd)
		if err != nil {
			return err
		}
		bs = bytes2.MultipleLineOperate(bs, unsafe2.Bytes(comment), trim)

		return json.Unmarshal(bs, v)
	})
}
示例#4
0
文件: file.go 项目: sshitaime/gohper
func ReadJSONWithComment(fname string, v interface{}) error {
	return file.Read(fname, func(fd *os.File) error {
		buf := bytes.NewBuffer(make([]byte, 0, 1024))
		br := bufio.NewReader(fd)

		for {
			line, _, err := br.ReadLine()
			if err == io.EOF {
				if len(line) == 0 {
					break
				}
			} else if err != nil {
				return err
			}

			if !bytes.HasPrefix(bytes.TrimSpace(line), commentPrefix) {
				buf.Write(line)
			}
		}
		return encoding.JSON.Unmarshal(buf.Bytes(), v)
	})
}
示例#5
0
func ReadJSON(fname string, v interface{}) error {
	return file.Read(fname, func(fd *os.File) error {
		return json.NewDecoder(fd).Decode(v)
	})
}
示例#6
0
func ReadGOB(fname string, v interface{}) error {
	return file.Read(fname, func(fd *os.File) error {
		return gob.NewDecoder(fd).Decode(v)
	})
}
示例#7
0
func Read(fname string, v interface{}, decoder encodeio.DecodeFunc) error {
	return file.Read(fname, func(fd *os.File) error {
		return encodeio.Read(fd, v, decoder)
	})
}
示例#8
0
文件: file.go 项目: sshitaime/gohper
func Read(fname string, v interface{}, codec encoding.Codec) error {
	return file.Read(fname, func(fd *os.File) error {
		return codec.Decode(fd, v)
	})
}