/src/lib/io/core/input_stream.e

http://github.com/tybor/Liberty · Specman e · 182 lines · 133 code · 18 blank · 31 comment · 11 complexity · 4a4bad5ff32839d01eeae4ed6184a441 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class INPUT_STREAM
  5. --
  6. -- An input stream is a flow of characters that can be read from some source (be it an Eiffel object or an
  7. -- external object)
  8. --
  9. -- Input stream usage is available in tutorial/io and Liberty Eiffel FAQ.
  10. --
  11. inherit
  12. STREAM
  13. insert
  14. INPUT_STREAM_TOOLS
  15. FILTERABLE
  16. redefine filter
  17. end
  18. feature {ANY}
  19. read_character
  20. do
  21. filtered_read_character
  22. ensure then
  23. is_connected
  24. end
  25. read_line_in (buffer: STRING)
  26. do
  27. filtered_read_line_in(buffer)
  28. end
  29. read_available_in (buffer: STRING; limit: INTEGER)
  30. do
  31. filtered_read_available_in(buffer, limit)
  32. end
  33. unread_character
  34. do
  35. filtered_unread_character
  36. ensure
  37. not end_of_input
  38. end
  39. last_character: CHARACTER
  40. do
  41. Result := filtered_last_character
  42. ensure
  43. is_connected
  44. not end_of_input
  45. end
  46. detach
  47. do
  48. if filter /= Void then
  49. filter.do_detach
  50. filter := Void
  51. end
  52. end
  53. feature {FILTER_INPUT_STREAM}
  54. filtered_read_character
  55. require
  56. is_connected
  57. can_read_character
  58. deferred
  59. end
  60. filtered_unread_character
  61. require
  62. is_connected
  63. can_unread_character
  64. deferred
  65. end
  66. filtered_last_character: CHARACTER
  67. require
  68. is_connected
  69. valid_last_character
  70. deferred
  71. end
  72. filtered_read_available_in (buffer: STRING; limit: INTEGER)
  73. require
  74. is_connected
  75. buffer /= Void
  76. local
  77. i: INTEGER
  78. do
  79. -- Default implementation
  80. if can_read_character then
  81. from
  82. i := 1
  83. filtered_read_character
  84. until
  85. end_of_input or else not can_read_character or else i > limit
  86. loop
  87. buffer.extend(filtered_last_character)
  88. filtered_read_character
  89. i := i + 1
  90. end
  91. if i > limit then
  92. filtered_unread_character
  93. end
  94. end
  95. end
  96. filtered_read_line_in (buffer: STRING)
  97. require
  98. is_connected
  99. buffer /= Void
  100. do
  101. -- Default implementation
  102. if can_read_character then
  103. from
  104. filtered_read_character
  105. until
  106. end_of_input or else not can_read_character or else filtered_last_character = '%N'
  107. loop
  108. inspect
  109. filtered_last_character
  110. when '%R' then
  111. if can_read_character then
  112. filtered_read_character
  113. if filtered_last_character /= '%N' then
  114. buffer.extend('%R')
  115. buffer.extend(filtered_last_character)
  116. filtered_read_character
  117. end
  118. end
  119. when '%N' then
  120. else
  121. buffer.extend(filtered_last_character)
  122. filtered_read_character
  123. end
  124. end
  125. end
  126. end
  127. feature {FILTER}
  128. filter: FILTER_INPUT_STREAM
  129. feature {ANY}
  130. event_can_read: EVENT_DESCRIPTOR
  131. do
  132. Result := can_read
  133. if Result = Void then
  134. create can_read.make(Current)
  135. Result := can_read
  136. end
  137. end
  138. feature {}
  139. can_read: CAN_READ_DATA_FROM_STREAM
  140. new_url: URL
  141. do
  142. create Result.from_stream(Current, True, False)
  143. end
  144. end -- class INPUT_STREAM
  145. --
  146. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  147. --
  148. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  149. -- of this software and associated documentation files (the "Software"), to deal
  150. -- in the Software without restriction, including without limitation the rights
  151. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  152. -- copies of the Software, and to permit persons to whom the Software is
  153. -- furnished to do so, subject to the following conditions:
  154. --
  155. -- The above copyright notice and this permission notice shall be included in
  156. -- all copies or substantial portions of the Software.
  157. --
  158. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  159. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  160. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  161. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  162. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  163. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  164. -- THE SOFTWARE.