File.hx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko.io;
  26. enum FileHandle {
  27. }
  28. enum FileSeek {
  29. SeekBegin;
  30. SeekCur;
  31. SeekEnd;
  32. }
  33. /**
  34. API for reading and writing to files.
  35. **/
  36. class File {
  37. public static function getContent( path : String ) {
  38. return new String(file_contents(untyped path.__s));
  39. }
  40. public static function getBytes( path : String ) {
  41. return neko.Lib.bytesReference(getContent(path));
  42. }
  43. public static function read( path : String, binary : Bool ) {
  44. return new FileInput(untyped file_open(path.__s,(if( binary ) "rb" else "r").__s));
  45. }
  46. public static function write( path : String, binary : Bool ) {
  47. return new FileOutput(untyped file_open(path.__s,(if( binary ) "wb" else "w").__s));
  48. }
  49. public static function append( path : String, binary : Bool ) {
  50. return new FileOutput(untyped file_open(path.__s,(if( binary ) "ab" else "a").__s));
  51. }
  52. public static function copy( src : String, dst : String ) {
  53. var s = read(src,true);
  54. var d = write(dst,true);
  55. d.writeInput(s);
  56. s.close();
  57. d.close();
  58. }
  59. public static function stdin() {
  60. return new FileInput(file_stdin());
  61. }
  62. public static function stdout() {
  63. return new FileOutput(file_stdout());
  64. }
  65. public static function stderr() {
  66. return new FileOutput(file_stderr());
  67. }
  68. public static function getChar( echo : Bool ) : Int {
  69. return getch(echo);
  70. }
  71. private static var file_stdin = neko.Lib.load("std","file_stdin",0);
  72. private static var file_stdout = neko.Lib.load("std","file_stdout",0);
  73. private static var file_stderr = neko.Lib.load("std","file_stderr",0);
  74. private static var file_contents = neko.Lib.load("std","file_contents",1);
  75. private static var file_open = neko.Lib.load("std","file_open",2);
  76. private static var getch = neko.Lib.load("std","sys_getch",1);
  77. }