File.hx 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 update( path : String, binary : Bool = true ) : FileOutput {
  67. if (!FileSystem.exists(path)) {
  68. write(path).close();
  69. }
  70. var mode = if (binary) "rb+" else "r+";
  71. var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
  72. return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
  73. }
  74. public static function copy( srcPath : String, dstPath : String ) : Void
  75. {
  76. return python.lib.Shutil.copy(srcPath, dstPath);
  77. }
  78. }