mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Модуль files: функция copy, поддержка строковых аргументов, вместо дескрипторов файлов
This commit is contained in:
parent
404c4abfdd
commit
fd92fa3d3d
@ -14,6 +14,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -39,25 +40,26 @@ public final class files implements Module {
|
|||||||
Functions.set("fclose", new fclose());
|
Functions.set("fclose", new fclose());
|
||||||
|
|
||||||
// Operations
|
// Operations
|
||||||
Functions.set("delete", new delete());
|
Functions.set("copy", new copy());
|
||||||
|
Functions.set("delete", fileToBoolean(File::delete));
|
||||||
Functions.set("listFiles", new listFiles());
|
Functions.set("listFiles", new listFiles());
|
||||||
Functions.set("mkdir", new mkdir());
|
Functions.set("mkdir", fileToBoolean(File::mkdir));
|
||||||
Functions.set("mkdirs", new mkdirs());
|
Functions.set("mkdirs", fileToBoolean(File::mkdirs));
|
||||||
Functions.set("rename", new rename());
|
Functions.set("rename", new rename());
|
||||||
|
|
||||||
// Permissions and statuses
|
// Permissions and statuses
|
||||||
Functions.set("canExecute", new canExecute());
|
Functions.set("canExecute", fileToBoolean(File::canExecute));
|
||||||
Functions.set("canRead", new canRead());
|
Functions.set("canRead", fileToBoolean(File::canRead));
|
||||||
Functions.set("canWrite", new canWrite());
|
Functions.set("canWrite", fileToBoolean(File::canWrite));
|
||||||
Functions.set("isDirectory", new isDirectory());
|
Functions.set("isDirectory", fileToBoolean(File::isDirectory));
|
||||||
Functions.set("isFile", new isFile());
|
Functions.set("isFile", fileToBoolean(File::isFile));
|
||||||
Functions.set("isHidden", new isHidden());
|
Functions.set("isHidden", fileToBoolean(File::isHidden));
|
||||||
Functions.set("setExecutable", new setExecutable());
|
Functions.set("setExecutable", new setExecutable());
|
||||||
Functions.set("setReadable", new setReadable());
|
Functions.set("setReadable", new setReadable());
|
||||||
Functions.set("setReadOnly", new setReadOnly());
|
Functions.set("setReadOnly", new setReadOnly());
|
||||||
Functions.set("setWritable", new setWritable());
|
Functions.set("setWritable", new setWritable());
|
||||||
|
|
||||||
Functions.set("exists", new exists());
|
Functions.set("exists", fileToBoolean(File::exists));
|
||||||
Functions.set("fileSize", new fileSize());
|
Functions.set("fileSize", new fileSize());
|
||||||
Functions.set("getParent", new getParent());
|
Functions.set("getParent", new getParent());
|
||||||
Functions.set("lastModified", new lastModified());
|
Functions.set("lastModified", new lastModified());
|
||||||
@ -176,81 +178,31 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class canExecute extends FileFunction {
|
private static class copy implements Function {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
public Value execute(Value... args) {
|
||||||
return NumberValue.fromBoolean(fileInfo.file.canExecute());
|
Arguments.check(2, args.length);
|
||||||
|
try {
|
||||||
|
final FileInputStream is = new FileInputStream(fileFrom(args[0]));
|
||||||
|
final FileOutputStream os = new FileOutputStream(fileFrom(args[1]));
|
||||||
|
final FileChannel ic = is.getChannel();
|
||||||
|
ic.transferTo(0, ic.size(), os.getChannel());
|
||||||
|
is.close();
|
||||||
|
os.close();
|
||||||
|
return NumberValue.ONE;
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
return NumberValue.MINUS_ONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class canRead extends FileFunction {
|
private static class rename implements Function {
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.canRead());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class canWrite extends FileFunction {
|
|
||||||
@Override
|
@Override
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
public Value execute(Value... args) {
|
||||||
return NumberValue.fromBoolean(fileInfo.file.canWrite());
|
Arguments.check(2, args.length);
|
||||||
}
|
return NumberValue.fromBoolean( fileFrom(args[0]).renameTo(fileFrom(args[1])) );
|
||||||
}
|
|
||||||
|
|
||||||
private static class delete extends FileFunction {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.delete());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class exists extends FileFunction {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class isDirectory extends FileFunction {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.isDirectory());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class isFile extends FileFunction {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.isFile());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.mkdir());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
@Override
|
|
||||||
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
|
|
||||||
final File dest = files.get(args[1].asInt()).file;
|
|
||||||
return NumberValue.fromBoolean(fileInfo.file.renameTo(dest));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -599,6 +551,25 @@ public final class files implements Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static File fileFrom(Value value) {
|
||||||
|
if (value.type() == Types.NUMBER) {
|
||||||
|
return files.get(value.asInt()).file;
|
||||||
|
}
|
||||||
|
return new File(value.asString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface FileToBooleanFunction {
|
||||||
|
|
||||||
|
boolean apply(File file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Function fileToBoolean(FileToBooleanFunction f) {
|
||||||
|
return args -> {
|
||||||
|
Arguments.check(1, args.length);
|
||||||
|
return NumberValue.fromBoolean(f.apply(fileFrom(args[0])));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static class FileInfo {
|
private static class FileInfo {
|
||||||
File file;
|
File file;
|
||||||
DataInputStream dis;
|
DataInputStream dis;
|
||||||
|
Loading…
Reference in New Issue
Block a user