/src/lib/io/basic/std_input.e

http://github.com/tybor/Liberty · Specman e · 240 lines · 165 code · 29 blank · 46 comment · 9 complexity · fc79c56c4650202bc778cfb4c06359b3 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class STD_INPUT
  5. --
  6. -- To use the standard input file. As for UNIX, the default standard input is the keyboard.
  7. --
  8. -- Note: only one instance of this class should be necessary. Access it through ANY.std_input.
  9. --
  10. -- See also STANDARD_STREAMS, STD_INPUT_OUTPUT
  11. --
  12. inherit
  13. TERMINAL_INPUT_STREAM
  14. redefine
  15. filtered_read_line_in, dispose
  16. end
  17. insert
  18. STRING_HANDLER
  19. REDIRECTION_TOOLS
  20. rename
  21. restore_default as restore_default_input
  22. redefine
  23. restore_default_input
  24. end
  25. create {ANY}
  26. make
  27. feature {ANY}
  28. is_connected: BOOLEAN True
  29. end_of_input: BOOLEAN
  30. disconnect
  31. do
  32. filter := Void
  33. end
  34. feature {}
  35. make
  36. do
  37. buffer := buffer.calloc(4096)
  38. capacity := 4096
  39. end
  40. feature {ANY}
  41. can_unread_character: BOOLEAN
  42. do
  43. Result := not unread_character_flag
  44. end
  45. feature {FILTER_INPUT_STREAM}
  46. filtered_read_character
  47. do
  48. std_output.flush
  49. unread_character_flag := False
  50. if buffer_position >= buffer_size then
  51. fill_buffer
  52. end
  53. filtered_last_character := buffer.item(buffer_position)
  54. buffer_position := buffer_position + 1
  55. end_of_input := end_reached
  56. end
  57. filtered_unread_character
  58. do
  59. unread_character_flag := True
  60. end_of_input := False
  61. buffer_position := buffer_position - 1
  62. end
  63. filtered_last_character: CHARACTER
  64. filtered_read_line_in (str: STRING)
  65. local
  66. i: INTEGER; stop: BOOLEAN; old_count, new_count: INTEGER; initial_count: INTEGER
  67. do
  68. std_output.flush
  69. from
  70. initial_count := str.count
  71. until
  72. stop
  73. loop
  74. -- search %N in buffer
  75. from
  76. i := buffer_position
  77. until
  78. i >= buffer_size or else buffer.item(i) = '%N'
  79. loop
  80. i := i + 1
  81. end
  82. -- block copy (but copy_slice copies char by char...)
  83. if i > buffer_position then
  84. old_count := str.count
  85. new_count := old_count + i - buffer_position
  86. if str.capacity < new_count then
  87. str.resize((old_count * 2).max(new_count))
  88. end
  89. str.storage.slice_copy(old_count, buffer, buffer_position, i - 1)
  90. str.set_count(new_count)
  91. end
  92. -- next buffer if needed
  93. if i < buffer_size and then buffer.item(i) = '%N' then
  94. stop := True
  95. buffer_position := i + 1
  96. if str.count > initial_count and then str.last = '%R' then
  97. str.remove_last
  98. -- UNIX uses the Line Feed character (ASCII character 10) to
  99. -- denote the end of a line. DOS uses the Carriage Return
  100. -- followed by the Line Feed character (ASCII character 13
  101. -- & ASCII character 10) instead.
  102. end
  103. else
  104. if not end_reached then
  105. fill_buffer
  106. end
  107. stop := end_reached
  108. end
  109. end
  110. unread_character_flag := False
  111. end_of_input := end_reached
  112. end
  113. feature {}
  114. buffer: NATIVE_ARRAY[CHARACTER]
  115. end_reached: BOOLEAN
  116. buffer_position, buffer_size: INTEGER
  117. capacity: INTEGER
  118. unread_character_flag: BOOLEAN
  119. fill_buffer
  120. local
  121. last: CHARACTER
  122. do
  123. if buffer_size > 0 then
  124. last := buffer.item(buffer_size - 1)
  125. end
  126. buffer_size := read_stdin(buffer, 4096)
  127. buffer_position := 0
  128. if buffer_size <= 0 then
  129. end_reached := True
  130. buffer.put(last, 0)
  131. -- needed for unread_character service
  132. --if buffer_size = -1 => exception ?
  133. buffer_size := 1
  134. buffer_position := 1
  135. end
  136. end
  137. feature {FILTER}
  138. filtered_descriptor: INTEGER
  139. do
  140. Result := sequencer_descriptor(stdin)
  141. end
  142. filtered_has_descriptor: BOOLEAN True
  143. filtered_stream_pointer: POINTER
  144. do
  145. Result := stdin
  146. end
  147. filtered_has_stream_pointer: BOOLEAN True
  148. feature {STREAM_HANDLER}
  149. redirect_from (file_name: STRING)
  150. -- Redirect standard input to come from `file_name' instead of the default standard input.
  151. --
  152. -- See also `redirection_succeeded'
  153. do
  154. redirect(open_descriptor_for_read(file_name.to_external))
  155. end
  156. restore_default_input
  157. -- Restore standard input to come from the default standard input.
  158. do
  159. Precursor
  160. end
  161. feature {}
  162. flush
  163. -- Discard any characters remaining in the input buffer.
  164. do
  165. buffer_position := buffer_size
  166. end
  167. read_stdin (buf: NATIVE_ARRAY[CHARACTER]; size: INTEGER): INTEGER
  168. -- return size read or 0 if end of input (-1 on error => exception ?)
  169. external "plug_in"
  170. alias "{
  171. location: "${sys}/plugins"
  172. module_name: "io"
  173. feature_name: "read_stdin"
  174. }"
  175. end
  176. stdin: POINTER
  177. external "plug_in"
  178. alias "{
  179. location: "${sys}/plugins"
  180. module_name: "io"
  181. feature_name: "stdin"
  182. }"
  183. end
  184. dispose
  185. do
  186. check
  187. std_input = Current
  188. end
  189. -- Nothing to dispose for `std_input'.
  190. end
  191. end -- class STD_INPUT
  192. --
  193. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  194. --
  195. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  196. -- of this software and associated documentation files (the "Software"), to deal
  197. -- in the Software without restriction, including without limitation the rights
  198. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  199. -- copies of the Software, and to permit persons to whom the Software is
  200. -- furnished to do so, subject to the following conditions:
  201. --
  202. -- The above copyright notice and this permission notice shall be included in
  203. -- all copies or substantial portions of the Software.
  204. --
  205. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  206. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  207. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  208. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  209. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  210. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  211. -- THE SOFTWARE.