Add ReadFile, ReadCollection

This commit is contained in:
Sasha Koshka 2024-08-31 02:39:10 -04:00
parent 132473a8fe
commit 5821510143

26
asv.go
View File

@ -41,6 +41,32 @@ func NewDecoder (reader io.Reader) *Decoder {
} }
} }
// ReadCollection reads files until EOF and returns them in a collection. This
// function will return nil instead of io.EOF.
func (this *Decoder) ReadCollection () (collection Collection, err error) {
for {
file, _, err := this.ReadFile()
collection = append(collection, file)
if err == io.EOF { return collection, nil}
if err != nil { return collection, err }
}
}
// ReadFile reads groups until a separator higher than or equal to FileSeparator
// is encountered, or an EOF is encountered. The function will then return the
// resulting File, as well as the separator that caused it to terminate. On EOF,
// it will return 0 for next and io.EOF for err.
func (this *Decoder) ReadFile () (file File, next rune, err error) {
for {
group, next, err := this.ReadGroup()
file = append(file, group)
if err != nil { return file, 0, err }
if next >= FileSeparator {
return file, next, nil
}
}
}
// ReadGroup reads records until a separator higher than or equal to // ReadGroup reads records until a separator higher than or equal to
// GroupSeparator is encountered, or an EOF is encountered. The function will // GroupSeparator is encountered, or an EOF is encountered. The function will
// then return the resulting Group, as well as the separator that caused it to // then return the resulting Group, as well as the separator that caused it to