File.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.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 ) throw new Sys.SysError("Can't read "+path);
  33. return @:privateAccess String.fromUTF8(bytes);
  34. }
  35. public static function getBytes( path : String ) : haxe.io.Bytes {
  36. var size = 0;
  37. var bytes = file_contents(Sys.getPath(path), size);
  38. if( bytes == null ) throw new Sys.SysError("Can't read "+path);
  39. return @:privateAccess new haxe.io.Bytes(bytes, size);
  40. }
  41. public static function saveContent( path : String, content : String ) : Void {
  42. var f = write(path);
  43. f.writeString(content);
  44. f.close();
  45. }
  46. public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
  47. var f = write(path);
  48. f.write(bytes);
  49. f.close();
  50. }
  51. public static function read( path : String, binary : Bool = true ) : FileInput {
  52. var f = file_open(Sys.getPath(path),0,binary);
  53. if( f == null ) throw new Sys.SysError("Can't open "+path);
  54. return @:privateAccess new FileInput(f);
  55. }
  56. public static function write( path : String, binary : Bool = true ) : FileOutput {
  57. var f = file_open(Sys.getPath(path),1,binary);
  58. if( f == null ) throw new Sys.SysError("Can't open "+path+" for writing");
  59. return @:privateAccess new FileOutput(f);
  60. }
  61. public static function append( path : String, binary : Bool = true ) : FileOutput {
  62. var f = file_open(Sys.getPath(path),2,binary);
  63. if( f == null ) throw new Sys.SysError("Can't open "+path+" for append");
  64. return @:privateAccess new FileOutput(f);
  65. }
  66. public static function copy( srcPath : String, dstPath : String ) : Void {
  67. var s = read(srcPath,true);
  68. var d = write(dstPath,true);
  69. d.writeInput(s);
  70. s.close();
  71. d.close();
  72. }
  73. @:hlNative("std", "file_open") static function file_open( path : hl.Bytes, mode : Int, binary : Bool ) : FileHandle { return null; }
  74. @:hlNative("std", "file_contents") static function file_contents( path : hl.Bytes, size : hl.Ref<Int> ) : hl.Bytes { return null; }
  75. }