File.hx 3.2 KB

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