FileSystem.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys;
  23. import cs.system.io.DirectoryInfo;
  24. import cs.system.io.File;
  25. import cs.system.io.Directory;
  26. import cs.system.io.FileInfo;
  27. /**
  28. This class allows you to get informations about the files and directories.
  29. **/
  30. @:coreApi
  31. class FileSystem {
  32. /**
  33. Tells if the given file or directory exists.
  34. **/
  35. public static function exists( path : String ) : Bool
  36. {
  37. return (File.Exists(path) || Directory.Exists(path));
  38. }
  39. /**
  40. Rename the corresponding file or directory, allow to move it accross directories as well.
  41. **/
  42. public static function rename( path : String, newpath : String ) : Void
  43. {
  44. Directory.Move(path, newpath);
  45. }
  46. /**
  47. Returns informations for the given file/directory.
  48. **/
  49. public static function stat( path : String ) : FileStat
  50. {
  51. if (File.Exists(path))
  52. {
  53. var fi = new FileInfo(path);
  54. return {
  55. gid: 0, //C# doesn't let you get this info
  56. uid: 0, //same
  57. atime: untyped Date.fromNative(fi.LastAccessTime),
  58. mtime: untyped Date.fromNative(fi.LastWriteTime),
  59. ctime: untyped Date.fromNative(fi.CreationTime),
  60. size: cast(fi.Length, Int), //TODO: maybe change to Int64 for Haxe 3?
  61. dev: 0, //FIXME: not sure what that is
  62. ino: 0, //FIXME: not sure what that is
  63. nlink: 0, //FIXME: not sure what that is
  64. rdev: 0, //FIXME: not sure what that is
  65. mode: 0 //FIXME: not sure what that is
  66. };
  67. } else if (Directory.Exists(path)) {
  68. var fi = new DirectoryInfo(path);
  69. return {
  70. gid: 0, //C# doesn't let you get this info
  71. uid: 0, //same
  72. atime: untyped Date.fromNative(fi.LastAccessTime),
  73. mtime: untyped Date.fromNative(fi.LastWriteTime),
  74. ctime: untyped Date.fromNative(fi.CreationTime),
  75. size: 0, //TODO: maybe change to Int64 for Haxe 3?
  76. dev: 0, //FIXME: not sure what that is
  77. ino: 0, //FIXME: not sure what that is
  78. nlink: 0, //FIXME: not sure what that is
  79. rdev: 0, //FIXME: not sure what that is
  80. mode: 0 //FIXME: not sure what that is
  81. };
  82. } else {
  83. throw "Path '" + path + "' doesn't exist";
  84. }
  85. }
  86. /**
  87. Returns the full path for the given path which is relative to the current working directory.
  88. **/
  89. public static function fullPath( relpath : String ) : String
  90. {
  91. return new FileInfo(relpath).FullName;
  92. }
  93. /**
  94. Tells if the given path is a directory. Throw an exception if it does not exists or is not accesible.
  95. **/
  96. public static function isDirectory( path : String ) : Bool
  97. {
  98. var isdir = Directory.Exists(path);
  99. if (isdir != File.Exists(path))
  100. return isdir;
  101. throw "Path '" + path + "' doesn't exist";
  102. }
  103. /**
  104. Create the given directory. Not recursive : the parent directory must exists.
  105. **/
  106. public static function createDirectory( path : String ) : Void
  107. {
  108. Directory.CreateDirectory(path);
  109. }
  110. /**
  111. Delete a given file.
  112. **/
  113. public static function deleteFile( path : String ) : Void
  114. {
  115. File.Delete(path);
  116. }
  117. /**
  118. Delete a given directory.
  119. **/
  120. public static function deleteDirectory( path : String ) : Void
  121. {
  122. Directory.Delete(path);
  123. }
  124. /**
  125. Read all the files/directories stored into the given directory.
  126. **/
  127. public static function readDirectory( path : String ) : Array<String>
  128. {
  129. var ret = Directory.GetFileSystemEntries(path);
  130. if (ret.Length > 0)
  131. {
  132. var fst = ret[0];
  133. var sep = "/";
  134. if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
  135. sep = "\\";
  136. for (i in 0...ret.Length)
  137. {
  138. var path = ret[i];
  139. ret[i] = path.substr(path.lastIndexOf(sep) + 1);
  140. }
  141. }
  142. return cs.Lib.array( ret );
  143. }
  144. }