File.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C)2005-2019 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.io;
  23. #if doc_gen
  24. enum FileHandle {}
  25. #else
  26. typedef FileHandle = hl.Abstract<"hl_fdesc">;
  27. #end
  28. @:access(Sys)
  29. @:coreApi class File {
  30. public static function getContent(path:String):String {
  31. var bytes = file_contents(Sys.getPath(path), null);
  32. if (bytes == null)
  33. throw new Sys.SysError("Can't read " + path);
  34. return @:privateAccess String.fromUTF8(bytes);
  35. }
  36. public static function getBytes(path:String):haxe.io.Bytes {
  37. var size = 0;
  38. var bytes = file_contents(Sys.getPath(path), size);
  39. if (bytes == null)
  40. throw new Sys.SysError("Can't read " + path);
  41. return @:privateAccess new haxe.io.Bytes(bytes, size);
  42. }
  43. public static function saveContent(path:String, content:String):Void {
  44. var f = write(path);
  45. f.writeString(content);
  46. f.close();
  47. }
  48. public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
  49. var f = write(path);
  50. f.write(bytes);
  51. f.close();
  52. }
  53. public static function read(path:String, binary:Bool = true):FileInput {
  54. var f = file_open(Sys.getPath(path), 0, binary);
  55. if (f == null)
  56. throw new Sys.SysError("Can't open " + path);
  57. return @:privateAccess new FileInput(f);
  58. }
  59. public static function write(path:String, binary:Bool = true):FileOutput {
  60. var f = file_open(Sys.getPath(path), 1, binary);
  61. if (f == null)
  62. throw new Sys.SysError("Can't open " + path + " for writing");
  63. return @:privateAccess new FileOutput(f);
  64. }
  65. public static function append(path:String, binary:Bool = true):FileOutput {
  66. var f = file_open(Sys.getPath(path), 2, binary);
  67. if (f == null)
  68. throw new Sys.SysError("Can't open " + path + " for append");
  69. return @:privateAccess new FileOutput(f);
  70. }
  71. public static function update(path:String, binary:Bool = true):FileOutput {
  72. if (!FileSystem.exists(path)) {
  73. write(path).close();
  74. }
  75. var f = file_open(Sys.getPath(path), 3, binary);
  76. if (f == null)
  77. throw new Sys.SysError("Can't open " + path + " for update");
  78. return @:privateAccess new FileOutput(f);
  79. }
  80. public static function copy(srcPath:String, dstPath:String):Void {
  81. var s = read(srcPath, true);
  82. var d = write(dstPath, true);
  83. d.writeInput(s);
  84. s.close();
  85. d.close();
  86. }
  87. @:hlNative("std", "file_open") static function file_open(path:hl.Bytes, mode:Int, binary:Bool):FileHandle {
  88. return null;
  89. }
  90. @:hlNative("std", "file_contents") static function file_contents(path:hl.Bytes, size:hl.Ref<Int>):hl.Bytes {
  91. return null;
  92. }
  93. }