2024-03-12 02:16:02 +00:00
|
|
|
// Copyright Terence J. Boldt (c)2021-2024
|
2022-01-23 21:58:34 +00:00
|
|
|
// Use of this source code is governed by an MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file provides a file in memory
|
|
|
|
|
|
|
|
package prodos
|
|
|
|
|
2022-01-23 23:51:44 +00:00
|
|
|
// MemoryFile containts file data and size
|
2022-01-23 21:58:34 +00:00
|
|
|
type MemoryFile struct {
|
|
|
|
data []byte
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2022-12-31 14:14:27 +00:00
|
|
|
// ReaderWriterAt is an interface for both ReaderAt and WriterAt combined
|
2022-03-04 23:08:33 +00:00
|
|
|
type ReaderWriterAt interface {
|
|
|
|
ReadAt(data []byte, offset int64) (int, error)
|
|
|
|
WriteAt(data []byte, offset int64) (int, error)
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:51:44 +00:00
|
|
|
// NewMemoryFile creates an in-memory file of the specified size in bytes
|
2022-01-23 21:58:34 +00:00
|
|
|
func NewMemoryFile(size int) *MemoryFile {
|
|
|
|
return &MemoryFile{make([]byte, size), size}
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:51:44 +00:00
|
|
|
// WriteAt writes data to the specified offset in the file
|
2022-01-23 21:58:34 +00:00
|
|
|
func (memoryFile *MemoryFile) WriteAt(data []byte, offset int64) (int, error) {
|
|
|
|
copy(memoryFile.data[int(offset):], data)
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:51:44 +00:00
|
|
|
// ReadAt reads data from the specified offset in the file
|
2022-01-23 21:58:34 +00:00
|
|
|
func (memoryFile *MemoryFile) ReadAt(data []byte, offset int64) (int, error) {
|
|
|
|
copy(data, memoryFile.data[int(offset):])
|
|
|
|
return len(data), nil
|
|
|
|
}
|