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