2
0

FileSystem.hx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 cpp.NativeSys;
  24. @:buildXml('<include name="${HXCPP}/src/hx/libs/std/Build.xml"/>')
  25. @:coreApi
  26. class FileSystem {
  27. public static function exists( path : String ) : Bool {
  28. return NativeSys.sys_exists(makeCompatiblePath(path));
  29. }
  30. public static function rename( path : String, newPath : String ) : Void {
  31. NativeSys.sys_rename(path,newPath);
  32. }
  33. public static function stat( path : String ) : FileStat {
  34. var s : FileStat = NativeSys.sys_stat(makeCompatiblePath(path));
  35. if (s==null)
  36. 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 };
  37. s.atime = Date.fromTime(1000.0*(untyped s.atime));
  38. s.mtime = Date.fromTime(1000.0*(untyped s.mtime));
  39. s.ctime = Date.fromTime(1000.0*(untyped s.ctime));
  40. return s;
  41. }
  42. public static function fullPath( relPath : String ) : String {
  43. return NativeSys.file_full_path(relPath);
  44. }
  45. public static function absolutePath ( relPath : String ) : String {
  46. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  47. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  48. }
  49. inline static function kind( path : String ) : String {
  50. return NativeSys.sys_file_type(makeCompatiblePath(path));
  51. }
  52. public static function isDirectory( path : String ) : Bool {
  53. return kind(path) == "dir";
  54. }
  55. public static function createDirectory( path : String ) : Void {
  56. var path = haxe.io.Path.addTrailingSlash(path);
  57. var _p = null;
  58. var parts = [];
  59. while (path != (_p = haxe.io.Path.directory(path))) {
  60. parts.unshift(path);
  61. path = _p;
  62. }
  63. for (part in parts) {
  64. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && !NativeSys.sys_create_dir( part, 493 ))
  65. throw "Could not create directory:" + part;
  66. }
  67. }
  68. public static function deleteFile( path : String ) : Void {
  69. NativeSys.file_delete(path);
  70. }
  71. public static function deleteDirectory( path : String ) : Void {
  72. NativeSys.sys_remove_dir(path);
  73. }
  74. public static function readDirectory( path : String ) : Array<String> {
  75. return NativeSys.sys_read_dir(path);
  76. }
  77. private static inline function makeCompatiblePath(path:String):String {
  78. return if (path.charCodeAt(1) == ":".code && path.length <= 3) {
  79. haxe.io.Path.addTrailingSlash(path);
  80. } else {
  81. haxe.io.Path.removeTrailingSlashes(path);
  82. }
  83. }
  84. }