FileSystem.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys;
  26. import system.io.DirectoryInfo;
  27. import system.io.File;
  28. import system.io.Directory;
  29. import system.io.FileInfo;
  30. /**
  31. This class allows you to get informations about the files and directories.
  32. **/
  33. class FileSystem {
  34. /**
  35. Tells if the given file or directory exists.
  36. **/
  37. public static function exists( path : String ) : Bool
  38. {
  39. return (File.Exists(path) || Directory.Exists(path));
  40. }
  41. /**
  42. Rename the corresponding file or directory, allow to move it accross directories as well.
  43. **/
  44. public static function rename( path : String, newpath : String ) : Void
  45. {
  46. Directory.Move(path, newpath);
  47. }
  48. /**
  49. Returns informations for the given file/directory.
  50. **/
  51. public static function stat( path : String ) : FileStat
  52. {
  53. if (File.Exists(path))
  54. {
  55. var fi = new FileInfo(path);
  56. return {
  57. gid: 0, //C# doesn't let you get this info
  58. uid: 0, //same
  59. atime: untyped Date.fromNative(fi.LastAccessTime),
  60. mtime: untyped Date.fromNative(fi.LastWriteTime),
  61. ctime: untyped Date.fromNative(fi.CreationTime),
  62. size: cast(fi.Length, Int), //TODO: maybe change to Int64 for Haxe 3?
  63. dev: 0, //FIXME: not sure what that is
  64. ino: 0, //FIXME: not sure what that is
  65. nlink: 0, //FIXME: not sure what that is
  66. rdev: 0, //FIXME: not sure what that is
  67. mode: 0 //FIXME: not sure what that is
  68. };
  69. } else if (Directory.Exists(path)) {
  70. var fi = new DirectoryInfo(path);
  71. return {
  72. gid: 0, //C# doesn't let you get this info
  73. uid: 0, //same
  74. atime: untyped Date.fromNative(fi.LastAccessTime),
  75. mtime: untyped Date.fromNative(fi.LastWriteTime),
  76. ctime: untyped Date.fromNative(fi.CreationTime),
  77. size: 0, //TODO: maybe change to Int64 for Haxe 3?
  78. dev: 0, //FIXME: not sure what that is
  79. ino: 0, //FIXME: not sure what that is
  80. nlink: 0, //FIXME: not sure what that is
  81. rdev: 0, //FIXME: not sure what that is
  82. mode: 0 //FIXME: not sure what that is
  83. };
  84. } else {
  85. throw "Path '" + path + "' doesn't exist";
  86. }
  87. }
  88. /**
  89. Returns the full path for the given path which is relative to the current working directory.
  90. **/
  91. public static function fullPath( relpath : String ) : String
  92. {
  93. return new FileInfo(relpath).FullName;
  94. }
  95. /**
  96. Tells if the given path is a directory. Throw an exception if it does not exists or is not accesible.
  97. **/
  98. public static function isDirectory( path : String ) : Bool
  99. {
  100. var isdir = Directory.Exists(path);
  101. if (isdir != File.Exists(path))
  102. return isdir;
  103. throw "Path '" + path + "' doesn't exist";
  104. }
  105. /**
  106. Create the given directory. Not recursive : the parent directory must exists.
  107. **/
  108. public static function createDirectory( path : String ) : Void
  109. {
  110. Directory.CreateDirectory(path);
  111. }
  112. /**
  113. Delete a given file.
  114. **/
  115. public static function deleteFile( path : String ) : Void
  116. {
  117. File.Delete(path);
  118. }
  119. /**
  120. Delete a given directory.
  121. **/
  122. public static function deleteDirectory( path : String ) : Void
  123. {
  124. Directory.Delete(path);
  125. }
  126. /**
  127. Read all the files/directories stored into the given directory.
  128. **/
  129. public static function readDirectory( path : String ) : Array<String>
  130. {
  131. var ret = Directory.GetFileSystemEntries(path);
  132. if (ret.Length > 0)
  133. {
  134. var fst = ret[0];
  135. var sep = "/";
  136. if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
  137. sep = "\\";
  138. for (i in 0...ret.Length)
  139. {
  140. var path = ret[i];
  141. ret[i] = path.substr(path.lastIndexOf(sep) + 1);
  142. }
  143. }
  144. return cs.Lib.array( ret );
  145. }
  146. }