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