1 /***********************************************************************************************
2 *	Copyright: © 2017-2021 UI Manufaktur UG
3 *	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
4 *	Authors: UI Manufaktur Team
5 *	Documentation [DE]: https://ui-manufaktur.com/docu/uim-core/io/file
6 ************************************************************************************************/
7 module uim.core.io.file;
8 
9 import std.file;
10 import uim.core;
11 
12 version(linux) {
13 	bool copy(string fileName, string fromDir, string toDir, bool createMissingDirs = true, bool overwriteExistingFile = true) {
14 		if (fileName.length == 0) return false;
15 		if (fromDir.length == 0) return false;
16 		if (toDir.length == 0) return false;
17 
18 		string from = fromDir;
19 		if (from[$-1] != '/') from ~= "/";
20 
21 		if (!exists(from~fileName)) return false;
22 
23 		string to = toDir; 
24 		if (to[$-1] != '/') to ~= "/";
25 
26 		if (createMissingDirs) {
27 			if (!exists(from)) mkdir(from);
28 			if (!exists(to)) mkdir(to);
29 		}
30 		else {
31 			if (!exists(from)) return false;
32 			if (!exists(to)) return false;
33 		}
34 
35 		if (!overwriteExistingFile && exists(to~fileName)) return false;
36 
37 		try { std.file.copy(from~fileName, to~fileName); }
38 		catch(Exception e) { return false; }
39 
40 		return true;
41 	}
42 }
43 
44 version(linux) {
45 	bool move(string fileName, string fromDir, string toDir, bool createMissingDirs = true, bool overwriteExistingFile = true) {
46 		if (fileName.length == 0) return false;
47 		if (fromDir.length == 0) return false;
48 		if (toDir.length == 0) return false;
49 		
50 		string from = fromDir;
51 		if (from[$-1] != '/') from ~= "/";
52 		
53 		if (!exists(from~fileName)) return false;
54 		
55 		string to = toDir; 
56 		if (to[$-1] != '/') to ~= "/";
57 		
58 		if (createMissingDirs) {
59 			if (!exists(from)) mkdir(from);
60 			if (!exists(to)) mkdir(to);
61 		}
62 		else {
63 			if (!exists(from)) return false;
64 			if (!exists(to)) return false;
65 		}
66 		
67 		if (!overwriteExistingFile && exists(to~fileName)) return false;
68 		
69 		try { fileName.copy(from, to); }
70 		catch(Exception e) { return false; }
71 
72 		try { std.file.remove(from~fileName); }
73 		catch(Exception e) { return false; }
74 
75 		return true;
76 	}
77 }
78 unittest {
79 	
80 }
81 
82 auto dirEntryInfos(string path) {
83   FileInfo[] results;
84   bool dirEntryInfo(FileInfo info) { results ~= info; return true; }
85   listDirectory(path, &dirEntryInfo);
86   return results;
87 }
88 
89 auto dirNames(string path, bool fullName = false) {
90   string[] results = dirEntryInfos(path).filter!(a => a.isDirectory).map!(a => a.name).array;
91   if (fullName) results = results.map!(a => path~"/"~a).array;
92   return results;
93 }
94 
95 auto linkNames(string path, bool fullName = false) {
96   string[] results = dirEntryInfos(path).filter!(a => a.isSymlink).map!(a => a.name).array;
97   if (fullName) results = results.map!(a => path~"/"~a).array;
98   return results;
99 }
100   
101 auto fileNames(string path, bool fullName = false) {
102   string[] results = dirEntryInfos(path).filter!(a => a.isFile).map!(a => a.name).array;
103   if (fullName) results = results.map!(a => path~"/"~a).array;
104   return results;
105 }
106