Io.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 lua;
  23. import haxe.extern.Rest;
  24. /**
  25. Input and Output Facilities
  26. **/
  27. @:native("_G.io")
  28. extern class Io {
  29. static var stdin:FileHandle;
  30. static var stderr:FileHandle;
  31. static var stdout:FileHandle;
  32. /**
  33. Function to close regular files.
  34. **/
  35. static function close(?file:FileHandle):Void;
  36. /**
  37. Saves any written data to file.
  38. **/
  39. static function flush():Void;
  40. /**
  41. When called with a file name, it opens the named file (in text mode),
  42. and sets its handle as the default input file. When called with a file handle,
  43. it simply sets this file handle as the default input file.
  44. When called without parameters, it returns the current default input file.
  45. In case of errors this function raises the error, instead of returning an
  46. error code.
  47. **/
  48. @:overload(function(file:String):Void {})
  49. static function input(file:FileHandle):Void;
  50. /**
  51. Opens the given file name in read mode and returns an iterator function that,
  52. each time it is called, returns a new line from the file.
  53. **/
  54. static function lines(?file:String):NativeIterator<String>;
  55. /**
  56. This function opens a file, in the mode specified in the string mode.
  57. It returns a new file handle, or, in case of errors, `null` plus an error message.
  58. The mode string can be any of the following:
  59. * `"r"`: read mode (the default)
  60. * `"w"`: write mode
  61. * `"a"`: append mode
  62. * `"r+"`: update mode, all previous data is preserved
  63. * `"w+"`: update mode, all previous data is erased
  64. * `"a+"`: append update mode, previous data is preserved, writing is only
  65. allowed at the end of file
  66. The mode string can also have a `b` at the end, which is needed in some systems
  67. to open the file in binary mode. This string is exactly what is used in the
  68. standard C function fopen.
  69. **/
  70. static function open(filename:String, ?mode:String):FileHandle;
  71. /**
  72. Starts program `command` in a separated process and returns a file handle that
  73. you can use to read data from this program (if mode is `"r"`, the default)
  74. or to write data to this program (if mode is `"w"`).
  75. This function is system dependent and is not available on all platforms.
  76. **/
  77. static function popen(command:String, ?mode:String):FileHandle;
  78. @:overload(function(?count:Int):String {})
  79. static function read(?filename:String):String;
  80. /**
  81. Writes the value of each of its arguments to the file. The arguments must
  82. be strings or numbers.
  83. To write other values, use `Lua.tostring` or `NativeStringTools.format`
  84. before write.
  85. **/
  86. static function write(v:Rest<String>):Void;
  87. static function output(?file:String):FileHandle;
  88. /**
  89. Returns a handle for a temporary file. This file is opened in update mode
  90. and it is automatically removed when the program ends.
  91. **/
  92. static function tmpfile():FileHandle;
  93. /**
  94. Checks whether `obj` is a valid file handle.
  95. **/
  96. static function type(obj:FileHandle):IoType;
  97. }
  98. /**
  99. A enumerator that describes the output of `Io.type()`.
  100. **/
  101. enum abstract IoType(String) {
  102. var File = "file";
  103. var ClosedFile = "closed file";
  104. var NotAFile = null;
  105. @:to public function toString() {
  106. return this;
  107. }
  108. }