File.hx 3.4 KB

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