File.hx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C)2005-2012 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. import python.io.IoTools;
  24. import sys.io.FileInput;
  25. /**
  26. API for reading and writing to files.
  27. See `sys.FileSystem` for the complementary file system API.
  28. **/
  29. @:coreApi
  30. class File {
  31. public static function getContent( path : String ) : String
  32. {
  33. var f:python.lib.io.TextIOBase = cast python.lib.Builtins.open(path, "r", -1, "utf-8", null, "");
  34. var content = f.read(-1);
  35. f.close();
  36. return content;
  37. }
  38. public static function saveContent( path : String, content : String ) : Void {
  39. var f:python.lib.io.TextIOBase = cast python.lib.Builtins.open(path, "w", -1, "utf-8", null, "");
  40. f.write(content);
  41. f.close();
  42. }
  43. public static function getBytes( path : String ) : haxe.io.Bytes {
  44. var f:python.lib.io.RawIOBase = cast python.lib.Builtins.open(path, "rb", -1);
  45. var size = f.read(-1);
  46. var b = haxe.io.Bytes.ofData(size);
  47. f.close();
  48. return b;
  49. }
  50. public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
  51. var f:python.lib.io.RawIOBase = cast python.lib.Builtins.open(path, "wb", -1);
  52. f.write(bytes.getData());
  53. f.close();
  54. }
  55. public static function read( path : String, binary : Bool = true ) : FileInput {
  56. var mode = if (binary) "rb" else "r";
  57. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  58. return if (binary) IoTools.createFileInputFromBytes(cast f) else IoTools.createFileInputFromText(cast f);
  59. }
  60. public static function write( path : String, binary : Bool = true ) : FileOutput {
  61. var mode = if (binary) "wb" else "w";
  62. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  63. return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
  64. }
  65. public static function append( path : String, binary : Bool = true ) : FileOutput {
  66. var mode = if (binary) "ab" else "a";
  67. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  68. return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
  69. }
  70. public static function copy( srcPath : String, dstPath : String ) : Void
  71. {
  72. return python.lib.Shutil.copy(srcPath, dstPath);
  73. }
  74. }