FileSystem.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. private enum FileKind {
  24. kdir;
  25. kfile;
  26. kother( k : String );
  27. }
  28. @:coreApi
  29. class FileSystem {
  30. public static function exists( path : String ) : Bool {
  31. return sys_exists(untyped (haxe.io.Path.removeTrailingSlashes(path)).__s);
  32. }
  33. public static function rename( path : String, newPath : String ) : Void {
  34. untyped sys_rename(path.__s,newPath.__s);
  35. }
  36. public static function stat( path : String ) : FileStat {
  37. var s : FileStat = sys_stat(untyped path.__s);
  38. s.atime = untyped Date.new1(s.atime);
  39. s.mtime = untyped Date.new1(s.mtime);
  40. s.ctime = untyped Date.new1(s.ctime);
  41. return s;
  42. }
  43. public static function fullPath( relPath : String ) : String {
  44. return new String(file_full_path(untyped relPath.__s));
  45. }
  46. public static function absolutePath ( relPath : String ) : String {
  47. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  48. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  49. }
  50. static function kind( path : String ) : FileKind {
  51. var k = new String(sys_file_type(untyped (haxe.io.Path.removeTrailingSlashes(path)).__s));
  52. return switch(k) {
  53. case "file": kfile;
  54. case "dir": kdir;
  55. default: kother(k);
  56. }
  57. }
  58. public static function isDirectory( path : String ) : Bool {
  59. return kind(path) == kdir;
  60. }
  61. public static function createDirectory( path : String ) : Void {
  62. var path = haxe.io.Path.addTrailingSlash(path);
  63. var _p = null;
  64. var parts = [];
  65. while (path != (_p = haxe.io.Path.directory(path))) {
  66. parts.unshift(path);
  67. path = _p;
  68. }
  69. for (part in parts) {
  70. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part))
  71. sys_create_dir( untyped part.__s, 493 );
  72. }
  73. }
  74. public static function deleteFile( path : String ) : Void {
  75. file_delete(untyped path.__s);
  76. }
  77. public static function deleteDirectory( path : String ) : Void {
  78. sys_remove_dir(untyped path.__s);
  79. }
  80. public static function readDirectory( path : String ) : Array<String> {
  81. var l : Array<Dynamic> = sys_read_dir(untyped path.__s);
  82. var a = new Array();
  83. while( l != null ) {
  84. a.push(new String(l[0]));
  85. l = l[1];
  86. }
  87. return a;
  88. }
  89. private static var sys_exists = neko.Lib.load("std","sys_exists",1);
  90. private static var file_delete = neko.Lib.load("std","file_delete",1);
  91. private static var sys_rename = neko.Lib.load("std","sys_rename",2);
  92. private static var sys_stat = neko.Lib.load("std","sys_stat",1);
  93. private static var sys_file_type = neko.Lib.load("std","sys_file_type",1);
  94. private static var sys_create_dir = neko.Lib.load("std","sys_create_dir",2);
  95. private static var sys_remove_dir = neko.Lib.load("std","sys_remove_dir",1);
  96. private static var sys_read_dir = neko.Lib.load("std","sys_read_dir",1);
  97. private static var file_full_path = neko.Lib.load("std","file_full_path",1);
  98. }