Skip to content

Commit fd92fa3

Browse files
committed
Модуль files: функция copy, поддержка строковых аргументов, вместо дескрипторов файлов
1 parent 404c4ab commit fd92fa3

File tree

1 file changed

+49
-78
lines changed
  • src/main/java/com/annimon/ownlang/modules/files

1 file changed

+49
-78
lines changed

src/main/java/com/annimon/ownlang/modules/files/files.java

Lines changed: 49 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.io.InputStreamReader;
1616
import java.io.OutputStreamWriter;
17+
import java.nio.channels.FileChannel;
1718
import java.util.HashMap;
1819
import java.util.Map;
1920

@@ -39,25 +40,26 @@ public void init() {
3940
Functions.set("fclose", new fclose());
4041

4142
// Operations
42-
Functions.set("delete", new delete());
43+
Functions.set("copy", new copy());
44+
Functions.set("delete", fileToBoolean(File::delete));
4345
Functions.set("listFiles", new listFiles());
44-
Functions.set("mkdir", new mkdir());
45-
Functions.set("mkdirs", new mkdirs());
46+
Functions.set("mkdir", fileToBoolean(File::mkdir));
47+
Functions.set("mkdirs", fileToBoolean(File::mkdirs));
4648
Functions.set("rename", new rename());
4749

4850
// Permissions and statuses
49-
Functions.set("canExecute", new canExecute());
50-
Functions.set("canRead", new canRead());
51-
Functions.set("canWrite", new canWrite());
52-
Functions.set("isDirectory", new isDirectory());
53-
Functions.set("isFile", new isFile());
54-
Functions.set("isHidden", new isHidden());
51+
Functions.set("canExecute", fileToBoolean(File::canExecute));
52+
Functions.set("canRead", fileToBoolean(File::canRead));
53+
Functions.set("canWrite", fileToBoolean(File::canWrite));
54+
Functions.set("isDirectory", fileToBoolean(File::isDirectory));
55+
Functions.set("isFile", fileToBoolean(File::isFile));
56+
Functions.set("isHidden", fileToBoolean(File::isHidden));
5557
Functions.set("setExecutable", new setExecutable());
5658
Functions.set("setReadable", new setReadable());
5759
Functions.set("setReadOnly", new setReadOnly());
5860
Functions.set("setWritable", new setWritable());
5961

60-
Functions.set("exists", new exists());
62+
Functions.set("exists", fileToBoolean(File::exists));
6163
Functions.set("fileSize", new fileSize());
6264
Functions.set("getParent", new getParent());
6365
Functions.set("lastModified", new lastModified());
@@ -176,81 +178,31 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
176178
}
177179
}
178180

179-
private static class canExecute extends FileFunction {
180-
@Override
181-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
182-
return NumberValue.fromBoolean(fileInfo.file.canExecute());
183-
}
184-
}
185-
186-
private static class canRead extends FileFunction {
187-
@Override
188-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
189-
return NumberValue.fromBoolean(fileInfo.file.canRead());
190-
}
191-
}
192-
193-
private static class canWrite extends FileFunction {
194-
@Override
195-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
196-
return NumberValue.fromBoolean(fileInfo.file.canWrite());
197-
}
198-
}
199-
200-
private static class delete extends FileFunction {
201-
@Override
202-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
203-
return NumberValue.fromBoolean(fileInfo.file.delete());
204-
}
205-
}
206-
207-
private static class exists extends FileFunction {
208-
@Override
209-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
210-
return NumberValue.fromBoolean(fileInfo.file.exists());
211-
}
212-
}
181+
private static class copy implements Function {
213182

214-
private static class isDirectory extends FileFunction {
215183
@Override
216-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
217-
return NumberValue.fromBoolean(fileInfo.file.isDirectory());
218-
}
219-
}
220-
221-
private static class isFile extends FileFunction {
222-
@Override
223-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
224-
return NumberValue.fromBoolean(fileInfo.file.isFile());
225-
}
226-
}
227-
228-
private static class isHidden extends FileFunction {
229-
@Override
230-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
231-
return NumberValue.fromBoolean(fileInfo.file.isHidden());
184+
public Value execute(Value... args) {
185+
Arguments.check(2, args.length);
186+
try {
187+
final FileInputStream is = new FileInputStream(fileFrom(args[0]));
188+
final FileOutputStream os = new FileOutputStream(fileFrom(args[1]));
189+
final FileChannel ic = is.getChannel();
190+
ic.transferTo(0, ic.size(), os.getChannel());
191+
is.close();
192+
os.close();
193+
return NumberValue.ONE;
194+
} catch (IOException ioe) {
195+
return NumberValue.MINUS_ONE;
196+
}
232197
}
233198
}
234199

235-
private static class mkdir extends FileFunction {
236-
@Override
237-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
238-
return NumberValue.fromBoolean(fileInfo.file.mkdir());
239-
}
240-
}
200+
private static class rename implements Function {
241201

242-
private static class mkdirs extends FileFunction {
243202
@Override
244-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
245-
return NumberValue.fromBoolean(fileInfo.file.mkdirs());
246-
}
247-
}
248-
249-
private static class rename extends FileFunction {
250-
@Override
251-
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
252-
final File dest = files.get(args[1].asInt()).file;
253-
return NumberValue.fromBoolean(fileInfo.file.renameTo(dest));
203+
public Value execute(Value... args) {
204+
Arguments.check(2, args.length);
205+
return NumberValue.fromBoolean( fileFrom(args[0]).renameTo(fileFrom(args[1])) );
254206
}
255207
}
256208

@@ -598,6 +550,25 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
598550
return NumberValue.ONE;
599551
}
600552
}
553+
554+
private static File fileFrom(Value value) {
555+
if (value.type() == Types.NUMBER) {
556+
return files.get(value.asInt()).file;
557+
}
558+
return new File(value.asString());
559+
}
560+
561+
private interface FileToBooleanFunction {
562+
563+
boolean apply(File file);
564+
}
565+
566+
private static Function fileToBoolean(FileToBooleanFunction f) {
567+
return args -> {
568+
Arguments.check(1, args.length);
569+
return NumberValue.fromBoolean(f.apply(fileFrom(args[0])));
570+
};
571+
}
601572

602573
private static class FileInfo {
603574
File file;

0 commit comments

Comments
 (0)