1 module orelang.operator.FileOperators;
2 import orelang.operator.IOperator,
3        orelang.Engine,
4        orelang.Value;
5 import std.conv,
6        std.file;
7 
8 class RemoveFileOperator {
9   public Value call(Engine engine, Value[] args) {
10     string path = args[0].type == ValueType.SymbolValue ? engine.eval(args[0]).getString : args[0].getString;
11 
12     if (exists(path)) {
13       if (isFile(path)) {
14         remove(path);
15       } else {
16         throw new Error("[remove-file]" ~ path ~ " is not a file, it is a directory, use remove-dir function instead.");
17       }
18     } else {
19       throw new Error("[remove-file] No such a file or directory: " ~ path);
20     }
21 
22     return new Value;
23   }
24 }
25 
26 class RemoveDirOperator {
27   public Value call(Engine engine, Value[] args) {
28     string path = args[0].type == ValueType.SymbolValue ? engine.eval(args[0]).getString : args[0].getString;
29 
30     if (exists(path)) {
31       if (isDir(path)) {
32         rmdir(path);
33       } else {
34         throw new Error("[remove-dir]" ~ path ~ " is not a directory, it is a file, use remove-file function instead.");
35       }
36     } else {
37       throw new Error("[remove-dir] No such a file or directory: " ~ path);
38     }
39 
40     return new Value;
41   }
42 }
43 
44 class GetcwdOperator : IOperator {
45   public Value call(Engine engine, Value[] args) {
46     return new Value(getcwd);
47   }
48 }
49 
50 class GetsizeOperator : IOperator {
51   public Value call(Engine engine, Value[] args) {
52     float ret;
53     string path = args[0].type == ValueType.SymbolValue ? engine.eval(args[0]).getString : args[0].getString;
54 
55    if (exists(path)) {
56       if (isFile(path)) {
57         ret = getSize(path).to!float;
58       } else {
59         throw new Error("[get-size]" ~ path ~ " is not a file, it is a directory");
60       }
61     } else {
62       throw new Error("[get-size] No such a file or directory: " ~ path);
63     }
64 
65 
66     return new Value(ret);
67   }
68 }