mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Добавлены функции для работы с файлами
This commit is contained in:
parent
50866ae9f3
commit
23f61a64e5
@ -27,16 +27,38 @@ public final class files implements Module {
|
|||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
files = new HashMap<>();
|
files = new HashMap<>();
|
||||||
|
Variables.set("FILES_COMPARATOR", new FunctionValue(new filesComparatorFunction()));
|
||||||
|
|
||||||
Functions.set("fopen", new fopen());
|
Functions.set("fopen", new fopen());
|
||||||
Functions.set("listFiles", new listFiles());
|
Functions.set("flush", new flush());
|
||||||
|
Functions.set("fclose", new fclose());
|
||||||
|
|
||||||
|
// Operations
|
||||||
Functions.set("delete", new delete());
|
Functions.set("delete", new delete());
|
||||||
|
Functions.set("listFiles", new listFiles());
|
||||||
|
Functions.set("mkdir", new mkdir());
|
||||||
|
Functions.set("mkdirs", new mkdirs());
|
||||||
Functions.set("rename", new rename());
|
Functions.set("rename", new rename());
|
||||||
Functions.set("exists", new exists());
|
|
||||||
|
// Permissions and statuses
|
||||||
|
Functions.set("canExecute", new canExecute());
|
||||||
|
Functions.set("canRead", new canRead());
|
||||||
|
Functions.set("canWrite", new canWrite());
|
||||||
Functions.set("isDirectory", new isDirectory());
|
Functions.set("isDirectory", new isDirectory());
|
||||||
Functions.set("isFile", new isFile());
|
Functions.set("isFile", new isFile());
|
||||||
Functions.set("mkdir", new mkdir());
|
Functions.set("isHidden", new isHidden());
|
||||||
|
Functions.set("setExecutable", new setExecutable());
|
||||||
|
Functions.set("setReadable", new setReadable());
|
||||||
|
Functions.set("setReadOnly", new setReadOnly());
|
||||||
|
Functions.set("setWritable", new setWritable());
|
||||||
|
|
||||||
|
Functions.set("exists", new exists());
|
||||||
Functions.set("fileSize", new fileSize());
|
Functions.set("fileSize", new fileSize());
|
||||||
|
Functions.set("getParent", new getParent());
|
||||||
|
Functions.set("lastModified", new lastModified());
|
||||||
|
Functions.set("setLastModified", new setLastModified());
|
||||||
|
|
||||||
|
// IO
|
||||||
Functions.set("readBoolean", new readBoolean());
|
Functions.set("readBoolean", new readBoolean());
|
||||||
Functions.set("readByte", new readByte());
|
Functions.set("readByte", new readByte());
|
||||||
Functions.set("readBytes", new readBytes());
|
Functions.set("readBytes", new readBytes());
|
||||||
@ -62,8 +84,27 @@ public final class files implements Module {
|
|||||||
Functions.set("writeUTF", new writeUTF());
|
Functions.set("writeUTF", new writeUTF());
|
||||||
Functions.set("writeLine", new writeLine());
|
Functions.set("writeLine", new writeLine());
|
||||||
Functions.set("writeText", new writeText());
|
Functions.set("writeText", new writeText());
|
||||||
Functions.set("flush", new flush());
|
}
|
||||||
Functions.set("fclose", new fclose());
|
|
||||||
|
private static class filesComparatorFunction implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
Arguments.checkAtLeast(2, args.length);
|
||||||
|
|
||||||
|
final int fd1 = args[0].asInt();
|
||||||
|
final int fd2 = args[1].asInt();
|
||||||
|
if (!files.containsKey(fd1)) {
|
||||||
|
return NumberValue.of(files.containsKey(fd2) ? 1 : 0);
|
||||||
|
}
|
||||||
|
if (!files.containsKey(fd2)) {
|
||||||
|
return NumberValue.of(files.containsKey(fd1) ? -1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
final File file1 = files.get(fd1).file;
|
||||||
|
final File file2 = files.get(fd2).file;
|
||||||
|
return NumberValue.of(file1.compareTo(file2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class fopen implements Function {
|
private static class fopen implements Function {
|
||||||
@ -130,6 +171,27 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class canExecute extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.canExecute());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class canRead extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.canRead());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class canWrite extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.canWrite());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class delete extends FileFunction {
|
private static class delete extends FileFunction {
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
@ -158,6 +220,13 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class isHidden extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.isHidden());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class mkdir extends FileFunction {
|
private static class mkdir extends FileFunction {
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
@ -165,6 +234,13 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class mkdirs extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.mkdirs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class rename extends FileFunction {
|
private static class rename extends FileFunction {
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
@ -180,6 +256,69 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class getParent extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
final String parent = fileInfo.file.getParent();
|
||||||
|
return new StringValue(parent == null ? "" : parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class lastModified extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.of(fileInfo.file.lastModified());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class setLastModified extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
final long time;
|
||||||
|
if (args[1].type() == Types.NUMBER) {
|
||||||
|
time = ((NumberValue)args[1]).asLong();
|
||||||
|
} else {
|
||||||
|
time = (long) args[1].asNumber();
|
||||||
|
}
|
||||||
|
fileInfo.file.setLastModified(time);
|
||||||
|
return NumberValue.ONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class setReadOnly extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
return NumberValue.fromBoolean(fileInfo.file.setReadOnly());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class setExecutable extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
final boolean ownerOnly = (args.length >= 3) ? (args[2].asInt() != 0) : true;
|
||||||
|
return NumberValue.fromBoolean(
|
||||||
|
fileInfo.file.setExecutable(args[1].asInt() != 0, ownerOnly));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class setReadable extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
final boolean ownerOnly = (args.length >= 3) ? (args[2].asInt() != 0) : true;
|
||||||
|
return NumberValue.fromBoolean(
|
||||||
|
fileInfo.file.setReadable(args[1].asInt() != 0, ownerOnly));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class setWritable extends FileFunction {
|
||||||
|
@Override
|
||||||
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
final boolean ownerOnly = (args.length >= 3) ? (args[2].asInt() != 0) : true;
|
||||||
|
return NumberValue.fromBoolean(
|
||||||
|
fileInfo.file.setWritable(args[1].asInt() != 0, ownerOnly));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class readBoolean extends FileFunction {
|
private static class readBoolean extends FileFunction {
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
||||||
|
@ -121,6 +121,7 @@ def testFiles() {
|
|||||||
assertEquals(1002003004005006007, readLong(f))
|
assertEquals(1002003004005006007, readLong(f))
|
||||||
assertEquals(float(fpNumber), readFloat(f))
|
assertEquals(float(fpNumber), readFloat(f))
|
||||||
assertEquals(-1, readInt(f)) // EOF
|
assertEquals(-1, readInt(f)) // EOF
|
||||||
|
assertEquals(0, FILES_COMPARATOR(f, f))
|
||||||
fclose(f)
|
fclose(f)
|
||||||
|
|
||||||
f = fopen("test.file", "i")
|
f = fopen("test.file", "i")
|
||||||
|
Loading…
Reference in New Issue
Block a user