FileSystem.hx 3.9 KB

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