apple2js/js/formats/prodos/seedling_file.ts
2022-07-23 12:00:38 -07:00

49 lines
1.4 KiB
TypeScript

import type { ProDOSVolume } from '.';
import { ProDOSFile } from './base_file';
import { BitMap } from './bit_map';
import { STORAGE_TYPES } from './constants';
import { FileEntry } from './file_entry';
export class SeedlingFile extends ProDOSFile {
blocks: Uint8Array[];
bitMap: BitMap;
constructor(volume: ProDOSVolume, private fileEntry: FileEntry) {
super(volume);
this.blocks = volume.blocks();
this.bitMap = volume.bitMap();
}
getBlockPointers() {
const pointers = [this.fileEntry.keyPointer];
return pointers;
}
read() {
const seedlingBlock = this.blocks[this.fileEntry.keyPointer];
const data = new Uint8Array(this.fileEntry.eof);
data.set(seedlingBlock.slice(0, this.fileEntry.eof));
return data;
}
write(data: Uint8Array) {
if (this.fileEntry.keyPointer) {
this.delete();
}
this.fileEntry.storageType = STORAGE_TYPES.SEEDLING;
this.fileEntry.keyPointer = this.bitMap.allocBlock();
this.fileEntry.eof = data.byteLength;
const seedlingBlock = this.blocks[this.fileEntry.keyPointer];
seedlingBlock.set(data);
this.fileEntry.write();
}
delete() {
const pointers = this.getBlockPointers();
for (let idx = 0; idx < pointers.length; idx++) {
this.bitMap.freeBlock(pointers[idx]);
}
}
}