Input.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /**
  27. An Input is an abstract reader. A specific input implementation will only
  28. have to override the [readChar] and maybe [read] and [close] methods. See
  29. [File] and [StringInput] for two ways of creating an Input.
  30. **/
  31. class Input {
  32. public function readChar() : Int {
  33. return throw "Not implemented";
  34. }
  35. public function readBytes( s : String, p : Int, len : Int ) : Int {
  36. var k = len;
  37. while( k > 0 ) {
  38. var c = readChar();
  39. untyped __dollar__sset(s.__s,p,c);
  40. p += 1;
  41. k -= 1;
  42. }
  43. return len;
  44. }
  45. public function close() {
  46. readBytes = function(_,_,_) { return throw Error.Closed; };
  47. readChar = function() { return throw Error.Closed; };
  48. close = function() { };
  49. }
  50. /* ------------------ API ------------------ */
  51. public function readAll( ?bufsize : Int ) : String {
  52. if( bufsize == null )
  53. bufsize = (1 << 14); // 16 Ko
  54. var buf = neko.Lib.makeString(bufsize);
  55. var total = new StringBuf();
  56. try {
  57. while( true ) {
  58. var len = readBytes(buf,0,bufsize);
  59. if( len == 0 )
  60. throw Error.Blocked;
  61. total.addSub(buf,0,len);
  62. }
  63. } catch( e : Eof ) {
  64. }
  65. return total.toString();
  66. }
  67. public function readFullBytes( s : String, pos : Int, len : Int ) {
  68. while( len > 0 ) {
  69. var k = readBytes(s,pos,len);
  70. pos += k;
  71. len -= k;
  72. }
  73. }
  74. public function read( nbytes : Int ) : String {
  75. var s = neko.Lib.makeString(nbytes);
  76. var p = 0;
  77. while( nbytes > 0 ) {
  78. var k = readBytes(s,p,nbytes);
  79. if( k == 0 ) throw Error.Blocked;
  80. p += k;
  81. nbytes -= k;
  82. }
  83. return s;
  84. }
  85. public function readUntil( end : Int ) : String {
  86. var buf = new StringBuf();
  87. var last : Int;
  88. while( (last = readChar()) != end )
  89. buf.addChar( last );
  90. return buf.toString();
  91. }
  92. public function readLine() : String {
  93. var buf = new StringBuf();
  94. var last : Int;
  95. var s;
  96. try {
  97. while( (last = readChar()) != 10 )
  98. buf.addChar( last );
  99. s = buf.toString();
  100. if( s.charCodeAt(s.length-1) == 13 ) s = s.substr(0,-1);
  101. } catch( e : Eof ) {
  102. s = buf.toString();
  103. if( s.length == 0 )
  104. neko.Lib.rethrow(e);
  105. }
  106. return s;
  107. }
  108. public function readFloat() : Float {
  109. return _float_of_bytes(untyped read(4).__s,false);
  110. }
  111. public function readFloatB() : Float {
  112. return _float_of_bytes(untyped read(4).__s,true);
  113. }
  114. public function readDouble() : Float {
  115. return _double_of_bytes(untyped read(8).__s,false);
  116. }
  117. public function readDoubleB() : Float {
  118. return _double_of_bytes(untyped read(8).__s,true);
  119. }
  120. public function readInt8() {
  121. var n = readChar();
  122. if( n >= 128 )
  123. return n - 256;
  124. return n;
  125. }
  126. public function readInt16() {
  127. var ch1 = readChar();
  128. var ch2 = readChar();
  129. var n = ch1 | (ch2 << 8);
  130. if( ch2 & 128 != 0 )
  131. return n - 65536;
  132. return n;
  133. }
  134. public function readUInt16() {
  135. var ch1 = readChar();
  136. var ch2 = readChar();
  137. return ch1 | (ch2 << 8);
  138. }
  139. public function readUInt16B() {
  140. var ch1 = readChar();
  141. var ch2 = readChar();
  142. return ch2 | (ch1 << 8);
  143. }
  144. public function readInt24() {
  145. var ch1 = readChar();
  146. var ch2 = readChar();
  147. var ch3 = readChar();
  148. var n = ch1 | (ch2 << 8) | (ch3 << 16);
  149. if( ch3 & 128 != 0 )
  150. return n - (1 << 24);
  151. return n;
  152. }
  153. public function readUInt24() {
  154. var ch1 = readChar();
  155. var ch2 = readChar();
  156. var ch3 = readChar();
  157. return ch1 | (ch2 << 8) | (ch3 << 16);
  158. }
  159. public function readUInt24B() {
  160. var ch1 = readChar();
  161. var ch2 = readChar();
  162. var ch3 = readChar();
  163. return ch3 | (ch2 << 8) | (ch1 << 16);
  164. }
  165. public function readInt32() {
  166. var ch1 = readChar();
  167. var ch2 = readChar();
  168. var ch3 = readChar();
  169. var ch4 = readChar();
  170. if( (ch4 & 128) != 0 ) {
  171. if( ch4 & 64 == 0 ) throw Error.Overflow;
  172. return ch1 | (ch2 << 8) | (ch3 << 16) | ((ch4 & 127) << 24);
  173. } else {
  174. if( ch4 & 64 != 0 ) throw Error.Overflow;
  175. return ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
  176. }
  177. }
  178. public function readUInt32() {
  179. var ch1 = readChar();
  180. var ch2 = readChar();
  181. var ch3 = readChar();
  182. var ch4 = readChar();
  183. if( ch4 >= 64 ) throw Error.Overflow;
  184. return ch1 | (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
  185. }
  186. public function readUInt32B() {
  187. var ch1 = readChar();
  188. var ch2 = readChar();
  189. var ch3 = readChar();
  190. var ch4 = readChar();
  191. if( ch1 >= 64 ) throw Error.Overflow;
  192. return ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24);
  193. }
  194. static var _float_of_bytes = neko.Lib.load("std","float_of_bytes",2);
  195. static var _double_of_bytes = neko.Lib.load("std","double_of_bytes",2);
  196. }