slicefile.go 934 Bytes
Newer Older
1 2 3 4 5 6 7 8
package files

import "io"

// SliceFile implements File, and provides simple directory handling.
// It contains children files, and is created from a `[]File`.
// SliceFiles are always directories, and can't be read from or closed.
type SliceFile struct {
9 10 11 12 13 14 15
	filename string
	files    []File
	n        int
}

func NewSliceFile(filename string, files []File) *SliceFile {
	return &SliceFile{filename, files, 0}
16 17 18 19 20 21 22
}

func (f *SliceFile) IsDirectory() bool {
	return true
}

func (f *SliceFile) NextFile() (File, error) {
23
	if f.n >= len(f.files) {
24 25
		return nil, io.EOF
	}
26 27
	file := f.files[f.n]
	f.n++
28 29 30 31
	return file, nil
}

func (f *SliceFile) FileName() string {
32
	return f.filename
33 34 35 36 37 38 39 40 41
}

func (f *SliceFile) Read(p []byte) (int, error) {
	return 0, ErrNotReader
}

func (f *SliceFile) Close() error {
	return ErrNotReader
}
42 43 44 45 46 47 48 49

func (f *SliceFile) Peek(n int) File {
	return f.files[n]
}

func (f *SliceFile) Length() int {
	return len(f.files)
}