PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/io/directory.d

http://github.com/wilkie/djehuty
D | 257 lines | 159 code | 47 blank | 51 comment | 33 complexity | 67d41826f89536e2be0ed6842612b74a MD5 | raw file
  1/*
  2 * directory.d
  3 *
  4 * This file contains the logic for the Directory class, which allows
  5 * traversal of the filesystem.
  6 *
  7 * Author: Dave Wilkinson
  8 *
  9 */
 10
 11module io.directory;
 12
 13import core.string;
 14import core.system;
 15import core.definitions;
 16import core.exception;
 17
 18import io.file;
 19import io.console;
 20
 21import platform.vars.directory;
 22
 23import scaffold.directory;
 24
 25// Section: Core
 26
 27// Description: This class represents a file system directory.
 28class Directory {
 29	// Description: This constructor will create a Directory object that represents the root.
 30	this() {
 31		_isRoot = true;
 32		_path = "";
 33	}
 34
 35	// Description: This constructor will create a Directory at the specified path.
 36	// path: A valid universal path.
 37	this(string path) {
 38		_isRoot = false;
 39		if (path.length > 0 && path[0] == '/') {
 40			// absolute path
 41			_path = path.dup;
 42		}
 43		else {
 44			// relative path
 45
 46			// get the working directory
 47			Directory cur = System.FileSystem.currentDir;
 48
 49			// create an absolute path
 50			_path = cur.path ~ "/" ~ path;
 51		}
 52		bool check = DirectoryCreate(_pfVars,_path);
 53		if (check == false) {
 54			throw new Exception("Directory Cannot be Created");
 55		}
 56
 57		// retrieve _name
 58		foreach_reverse(int i, chr; _path) {
 59			if (chr == '/' && i < _path.length - 1) {
 60				_name = _path[i+1.._path.length].dup;
 61				break;
 62			}
 63		}
 64	}
 65
 66	static Directory openOrCreate(string path) {
 67		Directory ret = open(path);
 68		if (ret is null) {
 69			return Directory.create(path);
 70		}
 71		return ret;
 72	}
 73
 74	static Directory open(string path) {
 75		Directory ret = new Directory;
 76		ret._isRoot = false;
 77		if (path.length > 0 && path[0] == '/') {
 78			// absolute path
 79			ret._path = path.dup;
 80		}
 81		else {
 82			// relative path
 83
 84			// get the working directory
 85			Directory cur = System.FileSystem.currentDir;
 86
 87			// create an absolute path
 88			ret._path = cur.path ~ "/" ~ path;
 89		}
 90
 91		// retrieve _name
 92		foreach_reverse(int i, chr; ret._path) {
 93			if (chr == '/' && i < ret._path.length - 1) {
 94				ret._name = ret._path[i+1..ret._path.length].dup;
 95				break;
 96			}
 97		}
 98		if (!DirectoryFileIsDir(ret._path)) {
 99			return null;
100		}
101		return ret;
102	}
103
104	static Directory create(string path) {
105		return new Directory(path);
106	}
107
108	bool isDir(string _name) {
109		return DirectoryFileIsDir(_path ~ "/" ~ _name);
110	}
111
112	void move(Directory to) {
113		move(to.path());
114	}
115
116	void move(string path) {
117		if (DirectoryMove(_path, path ~ "/" ~ _name)) {
118			_parent = Directory.open(path);
119			_path = path ~ "/" ~ _name;
120		}
121	}
122
123	void copy(string path) {
124		if (DirectoryCopy(_path, path)) {
125			_parent = null;
126			_path = path;
127
128			// retrieve _name
129			foreach_reverse(int i, chr; _path) {
130				if (chr == '/' && i < _path.length - 1) {
131					_name = _path[i+1.._path.length].dup;
132					break;
133				}
134			}
135		}
136	}
137
138	void copy(Directory to, string newName = null) {
139		if (newName is null) { newName = _name; }
140
141		copy(to.path() ~ "/" ~ newName);
142	}
143
144	// Description: This function will return a String representing the _name of the directory.
145	// Returns: The _name of the directory.
146	string name() {
147		return _name.dup;
148	}
149
150	// Description: This function will return a String representing the path of this directory.
151	// Returns: The path of the directory.
152	string path() {
153		return _path.dup;
154	}
155
156	// Description: This function will rename the directory, if possible.
157	// newName: The new _name for the directory.
158	void name(string newName) {
159		// Rename directory
160
161		if (isRoot) {
162			// XXX: Exception
163		}
164		else {
165			// Change the _name of the directory (if possible)
166			DirectoryRename(_path, newName);
167			_path = this.parent.path ~ "/" ~ newName;
168			_name = newName;
169		}
170	}
171
172	// Description: This function will open the file specified by the parameter if it exists within the directory.
173	// filename: The _name of the file to open.
174	// Returns: Will return null if the file cannot be opened or found, will return a valid File otherwise.
175	File openFile(string filename) {
176		return File.open(this.path ~ "/" ~ filename);
177	}
178
179	// Description: This function will create a new file in this directory.
180	// filename: The _name of the file to create.
181	// Returns: Will return null if the file cannot be created, will return a valid File otherwise.
182	File createFile(string filename) {
183		return File.create(this.path ~ "/" ~ filename);
184	}
185
186	// Description: This function will return the _parent directory of the current path.
187	// Returns: The Directory object representing the _parent directory and null if the current directory is the root.
188	Directory parent() {
189		if (isRoot) { return null; }
190
191		if (_parent is null) {
192			Console.putln(_path);
193
194			foreach_reverse(int i, chr; _path) {
195				if (chr == '/')	{
196					// truncate
197					Console.putln(_path[0..i]);
198					_parent = Directory.open(_path[0..i]);
199					return _parent;
200				}
201			}
202		}
203
204		return _parent;
205	}
206
207	// Description: This function will return the Directory representing the directory specified within the current path.
208	// directoryName: The _name of the directory.
209	// Returns: The child directory specified.
210	Directory traverse(string directoryName) {
211		if (isDir(directoryName)) {
212			Directory ret = Directory.open(_path ~ "/" ~ directoryName);
213
214			ret._parent = this;
215
216			return ret;
217		}
218		return null;
219	}
220
221	// Description: This function will return whether or not the object represents the root.
222	// Returns: Will return true when the Directory is root and false otherwise.
223	bool isRoot() {
224		return _isRoot;
225	}
226
227	// Description: This function will return an array of filenames that are found within this directory.
228	// Returns: An array of filenames.
229	string[] list() {
230		return DirectoryList(_pfVars, _path);
231	}
232
233	bool opEquals(Directory d) {
234		return _path == d._path;
235	}
236
237	bool opEquals(string d) {
238		return _path == d;
239	}
240
241	// this should work:
242	alias Object.opEquals opEquals;
243
244	override char[] toString() {
245		return this.path.dup;
246	}
247
248protected:
249
250	string _name;
251	string _path;
252	Directory _parent;
253
254	bool _isRoot;
255
256	DirectoryPlatformVars _pfVars;
257}