2
0

File.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C)2005-2018 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. {
  29. var f:python.lib.io.TextIOBase = cast python.lib.Builtins.open(path, "r", -1, "utf-8", null, "");
  30. var content = f.read(-1);
  31. f.close();
  32. return content;
  33. }
  34. public static function saveContent( path : String, content : String ) : Void {
  35. var f:python.lib.io.TextIOBase = cast python.lib.Builtins.open(path, "w", -1, "utf-8", null, "");
  36. f.write(content);
  37. f.close();
  38. }
  39. public static function getBytes( path : String ) : haxe.io.Bytes {
  40. var f:python.lib.io.RawIOBase = cast python.lib.Builtins.open(path, "rb", -1);
  41. var size = f.read(-1);
  42. var b = haxe.io.Bytes.ofData(size);
  43. f.close();
  44. return b;
  45. }
  46. public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
  47. var f:python.lib.io.RawIOBase = cast python.lib.Builtins.open(path, "wb", -1);
  48. f.write(bytes.getData());
  49. f.close();
  50. }
  51. public static function read( path : String, binary : Bool = true ) : FileInput {
  52. var mode = if (binary) "rb" else "r";
  53. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  54. return if (binary) IoTools.createFileInputFromBytes(cast f) else IoTools.createFileInputFromText(cast f);
  55. }
  56. public static function write( path : String, binary : Bool = true ) : FileOutput {
  57. var mode = if (binary) "wb" else "w";
  58. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  59. return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
  60. }
  61. public static function append( path : String, binary : Bool = true ) : FileOutput {
  62. var mode = if (binary) "ab" else "a";
  63. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  64. return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
  65. }
  66. public static function copy( srcPath : String, dstPath : String ) : Void
  67. {
  68. return python.lib.Shutil.copy(srcPath, dstPath);
  69. }
  70. }