FileSystem.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys;
  26. private enum FileKind {
  27. kdir;
  28. kfile;
  29. kother( k : String );
  30. }
  31. @:core_api
  32. class FileSystem {
  33. public static function exists( path : String ) : Bool {
  34. return sys_exists(path);
  35. }
  36. public static function rename( path : String, newpath : String ) : Void {
  37. if (sys_rename(path,newpath)==null)
  38. throw "Could not rename:" + path + " to " + newpath;
  39. }
  40. public static function stat( path : String ) : FileStat {
  41. var s : FileStat = sys_stat(path);
  42. if (s==null)
  43. 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 };
  44. s.atime = Date.fromTime(1000.0*(untyped s.atime));
  45. s.mtime = Date.fromTime(1000.0*(untyped s.mtime));
  46. s.ctime = Date.fromTime(1000.0*(untyped s.ctime));
  47. return s;
  48. }
  49. public static function fullPath( relpath : String ) : String {
  50. return new String(file_full_path(relpath));
  51. }
  52. static function kind( path : String ) : FileKind {
  53. var k:String = sys_file_type(path);
  54. return switch(k) {
  55. case "file": kfile;
  56. case "dir": kdir;
  57. default: kother(k);
  58. }
  59. }
  60. public static function isDirectory( path : String ) : Bool {
  61. return kind(path) == kdir;
  62. }
  63. public static function createDirectory( path : String ) : Void {
  64. if (sys_create_dir( path, 493 )==null)
  65. throw "Could not create directory:" + path;
  66. }
  67. public static function deleteFile( path : String ) : Void {
  68. if (file_delete(path)==null)
  69. throw "Could not delete file:" + path;
  70. }
  71. public static function deleteDirectory( path : String ) : Void {
  72. if (sys_remove_dir(path)==null)
  73. throw "Could not delete directory:" + path;
  74. }
  75. public static function readDirectory( path : String ) : Array<String> {
  76. return sys_read_dir(path);
  77. }
  78. private static var sys_exists = cpp.Lib.load("std","sys_exists",1);
  79. private static var file_delete = cpp.Lib.load("std","file_delete",1);
  80. private static var sys_rename = cpp.Lib.load("std","sys_rename",2);
  81. private static var sys_stat = cpp.Lib.load("std","sys_stat",1);
  82. private static var sys_file_type = cpp.Lib.load("std","sys_file_type",1);
  83. private static var sys_create_dir = cpp.Lib.load("std","sys_create_dir",2);
  84. private static var sys_remove_dir = cpp.Lib.load("std","sys_remove_dir",1);
  85. private static var sys_read_dir = cpp.Lib.load("std","sys_read_dir",1);
  86. private static var file_full_path = cpp.Lib.load("std","file_full_path",1);
  87. }