FileInput.hx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C)2005-2017 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 haxe.io.Eof;
  24. @:coreApi
  25. class FileInput extends haxe.io.Input {
  26. private var __f : File.FileHandle;
  27. function new(f:File.FileHandle) : Void {
  28. __f = f;
  29. }
  30. public override function readByte() : Int {
  31. var r = untyped __call__('fread', __f, 1);
  32. if(untyped __call__('feof', __f)) return throw new haxe.io.Eof();
  33. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  34. return untyped __call__('ord', r);
  35. }
  36. public override function readBytes( s : haxe.io.Bytes, p : Int, l : Int ) : Int {
  37. if(untyped __call__('feof', __f)) return throw new haxe.io.Eof();
  38. var r : String = untyped __call__('fread', __f, l);
  39. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  40. var b = haxe.io.Bytes.ofString(r);
  41. s.blit(p, b, 0, r.length);
  42. return r.length;
  43. }
  44. public override function close() : Void {
  45. super.close();
  46. if(__f != null) untyped __call__('fclose', __f);
  47. }
  48. public function seek( p : Int, pos : FileSeek ) : Void {
  49. var w;
  50. switch( pos ) {
  51. case SeekBegin: w = untyped __php__('SEEK_SET');
  52. case SeekCur : w = untyped __php__('SEEK_CUR');
  53. case SeekEnd : w = untyped __php__('SEEK_END');
  54. }
  55. var r = untyped __call__('fseek', __f, p, w);
  56. if(untyped __physeq__(r, false)) throw haxe.io.Error.Custom('An error occurred');
  57. }
  58. public function tell() : Int {
  59. var r = untyped __call__('ftell', __f);
  60. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  61. return cast r;
  62. }
  63. public function eof() : Bool {
  64. return untyped __call__('feof', __f);
  65. }
  66. override function readLine() : String {
  67. var r : String = untyped __call__('fgets', __f);
  68. if (untyped __physeq__(false, r))
  69. throw new Eof();
  70. return untyped __call__("rtrim", r, "\r\n");
  71. }
  72. }