FileSystem.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C)2005-2017 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. @:coreApi
  28. class FileSystem {
  29. public static function exists( path : String ) : Bool
  30. {
  31. return (File.Exists(path) || Directory.Exists(path));
  32. }
  33. public static function rename( path : String, newPath : String ) : Void
  34. {
  35. Directory.Move(path, newPath);
  36. }
  37. @:access(Date.fromNative)
  38. public static function stat( path : String ) : FileStat
  39. {
  40. if (File.Exists(path))
  41. {
  42. var fi = new FileInfo(path);
  43. return {
  44. gid: 0, //C# doesn't let you get this info
  45. uid: 0, //same
  46. atime: Date.fromNative(fi.LastAccessTime),
  47. mtime: Date.fromNative(fi.LastWriteTime),
  48. ctime: Date.fromNative(fi.CreationTime),
  49. size: cast(fi.Length, Int), //TODO: maybe change to Int64 for Haxe 3?
  50. dev: 0, //FIXME: not sure what that is
  51. ino: 0, //FIXME: not sure what that is
  52. nlink: 0, //FIXME: not sure what that is
  53. rdev: 0, //FIXME: not sure what that is
  54. mode: 0 //FIXME: not sure what that is
  55. };
  56. } else if (Directory.Exists(path)) {
  57. var fi = new DirectoryInfo(path);
  58. return {
  59. gid: 0, //C# doesn't let you get this info
  60. uid: 0, //same
  61. atime: Date.fromNative(fi.LastAccessTime),
  62. mtime: Date.fromNative(fi.LastWriteTime),
  63. ctime: Date.fromNative(fi.CreationTime),
  64. size: 0, //TODO: maybe change to Int64 for Haxe 3?
  65. dev: 0, //FIXME: not sure what that is
  66. ino: 0, //FIXME: not sure what that is
  67. nlink: 0, //FIXME: not sure what that is
  68. rdev: 0, //FIXME: not sure what that is
  69. mode: 0 //FIXME: not sure what that is
  70. };
  71. } else {
  72. throw "Path '" + path + "' doesn't exist";
  73. }
  74. }
  75. public static function fullPath( relPath : String ) : String
  76. {
  77. return new FileInfo(relPath).FullName;
  78. }
  79. public static function absolutePath ( relPath : String ) : String {
  80. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  81. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  82. }
  83. public static function isDirectory( path : String ) : Bool
  84. {
  85. var isdir = Directory.Exists(path);
  86. if (isdir != File.Exists(path))
  87. return isdir;
  88. throw "Path '" + path + "' doesn't exist";
  89. }
  90. public static function createDirectory( path : String ) : Void
  91. {
  92. Directory.CreateDirectory(path);
  93. }
  94. public static function deleteFile( path : String ) : Void
  95. {
  96. if (!File.Exists(path)) throw "Path '" + path + "' doesn't exist";
  97. File.Delete(path);
  98. }
  99. public static function deleteDirectory( path : String ) : Void
  100. {
  101. if (!Directory.Exists(path)) throw "Path '" + path + "' doesn't exist";
  102. Directory.Delete(path);
  103. }
  104. public static function readDirectory( path : String ) : Array<String>
  105. {
  106. var ret = Directory.GetFileSystemEntries(path);
  107. if (ret.Length > 0)
  108. {
  109. var fst = ret[0];
  110. var sep = "/";
  111. if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
  112. sep = "\\";
  113. for (i in 0...ret.Length)
  114. {
  115. var path = ret[i];
  116. ret[i] = path.substr(path.lastIndexOf(sep) + 1);
  117. }
  118. }
  119. return cs.Lib.array( ret );
  120. }
  121. }