slcom/slangc/streams/SimpleVFS.sauce

95 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

package slangc.streams;
import slang.vm.SystemCall;
import slang.vm.DeviceHandle;
public class SimpleVFS extends VFS {
public String separator() {
return "/";
}
public File[] list(String path) throws Error {
String[] names = new String[0];
int n = SystemCall.fileList(path, names);
if (n < 0) {
return null;
}
names = new String[n];
SystemCall.fileList(path, names);
File[] files = new File[n];
for (int i = 0; i < n; i++) {
files[i] = file(path + separator() + names[i]);
}
return files;
}
public long size(String path) throws Error {
duck[] stats = new duck[2];
SystemCall.fileStat(path, stats);
if (stats[0] == 1) {
return (long) stats[1];
} else {
return -1;
}
}
public static class SimpleInput extends FileInput {
DeviceHandle dev;
public SimpleInput(VFS vfs, String path, DeviceHandle dev) {
super(vfs, path);
this.dev = dev;
}
public int readBuffer(byte[] buffer, int beginAt, int maxLength) {
uint8[] internal = new uint8[buffer.length];
int total = SystemCall.readBytes(dev, internal, beginAt, maxLength);
for (int i = 0; i < total; i++) {
buffer[beginAt + i] = (byte) internal[beginAt + i];
}
return total;
}
public void close() {
dev.close();
}
}
public FileInput openInput(String path) throws Error {
DeviceHandle dev = SystemCall.fileOpen(path, "rb");
if (dev == null) {
throw new Error("Unable to open file '" + path + "' for reading");
}
return new SimpleInput(this, path, dev);
}
public static class SimpleOutput extends FileOutput {
DeviceHandle dev;
public SimpleOutput(VFS vfs, String path, DeviceHandle dev) {
super(vfs, path);
this.dev = dev;
}
public int writeBuffer(byte[] buffer, int beginAt, int maxLength) {
uint8[] internal = new uint8[buffer.length];
for (int i = 0; i < maxLength; i++) {
internal[beginAt + i] = (uint8) buffer[beginAt + i];
}
int total = SystemCall.writeBytes(dev, internal, beginAt, maxLength);
return total;
}
public void close() {
dev.close();
//SystemCall.devicePoll(dev, true);
}
}
public FileOutput openOutput(String path) throws Error {
DeviceHandle dev = SystemCall.fileOpen(path, "wb");
if (dev == null) {
throw new Error("Unable to open file '" + path + "' for writing");
}
return new SimpleOutput(this, path, dev);
}
}