FileSystem.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C)2005-2016 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. private enum FileKind {
  24. kdir;
  25. kfile;
  26. kother( k : String );
  27. }
  28. @:buildXml('<include name="${HXCPP}/src/hx/libs/std/Build.xml"/>')
  29. @:coreApi
  30. class FileSystem {
  31. public static function exists( path : String ) : Bool {
  32. return sys_exists(makeCompatiblePath(path));
  33. }
  34. public static function rename( path : String, newPath : String ) : Void {
  35. if (sys_rename(path,newPath)==null)
  36. throw "Could not rename:" + path + " to " + newPath;
  37. }
  38. public static function stat( path : String ) : FileStat {
  39. var s : FileStat = sys_stat(makeCompatiblePath(path));
  40. if (s==null)
  41. return { gid:0, uid:0, atime:Date.fromTime(0), mtime:Date.fromTime(0), ctime:Date.fromTime(0), dev:0, ino:0, nlink:0, rdev:0, size:0, mode:0 };
  42. s.atime = Date.fromTime(1000.0*(untyped s.atime));
  43. s.mtime = Date.fromTime(1000.0*(untyped s.mtime));
  44. s.ctime = Date.fromTime(1000.0*(untyped s.ctime));
  45. return s;
  46. }
  47. public static function fullPath( relPath : String ) : String {
  48. return new String(file_full_path(relPath));
  49. }
  50. public static function absolutePath ( relPath : String ) : String {
  51. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  52. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  53. }
  54. static function kind( path : String ) : FileKind {
  55. var k:String = sys_file_type(makeCompatiblePath(path));
  56. return switch(k) {
  57. case "file": kfile;
  58. case "dir": kdir;
  59. default: kother(k);
  60. }
  61. }
  62. public static inline function isDirectory( path : String ) : Bool {
  63. return kind(path) == kdir;
  64. }
  65. public static function createDirectory( path : String ) : Void {
  66. var path = haxe.io.Path.addTrailingSlash(path);
  67. var _p = null;
  68. var parts = [];
  69. while (path != (_p = haxe.io.Path.directory(path))) {
  70. parts.unshift(path);
  71. path = _p;
  72. }
  73. for (part in parts) {
  74. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && sys_create_dir( part, 493 )==null)
  75. throw "Could not create directory:" + part;
  76. }
  77. }
  78. public static function deleteFile( path : String ) : Void {
  79. if (file_delete(path)==null)
  80. throw "Could not delete file:" + path;
  81. }
  82. public static function deleteDirectory( path : String ) : Void {
  83. if (sys_remove_dir(path)==null)
  84. throw "Could not delete directory:" + path;
  85. }
  86. public static function readDirectory( path : String ) : Array<String> {
  87. return sys_read_dir(path);
  88. }
  89. private static inline function makeCompatiblePath(path:String):String {
  90. return if (path.charCodeAt(1) == ":".code && path.length <= 3) {
  91. haxe.io.Path.addTrailingSlash(path);
  92. } else {
  93. haxe.io.Path.removeTrailingSlashes(path);
  94. }
  95. }
  96. private static var sys_exists = cpp.Lib.load("std","sys_exists",1);
  97. private static var file_delete = cpp.Lib.load("std","file_delete",1);
  98. private static var sys_rename = cpp.Lib.load("std","sys_rename",2);
  99. private static var sys_stat = cpp.Lib.load("std","sys_stat",1);
  100. private static var sys_file_type = cpp.Lib.load("std","sys_file_type",1);
  101. private static var sys_create_dir = cpp.Lib.load("std","sys_create_dir",2);
  102. private static var sys_remove_dir = cpp.Lib.load("std","sys_remove_dir",1);
  103. private static var sys_read_dir = cpp.Lib.load("std","sys_read_dir",1);
  104. private static var file_full_path = cpp.Lib.load("std","file_full_path",1);
  105. }