PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/io/basic/text_file_read_write.e

http://github.com/tybor/Liberty
Specman e | 239 lines | 178 code | 27 blank | 34 comment | 10 complexity | 29cb82c3cc455abba5726a82ed1c7376 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class TEXT_FILE_READ_WRITE
  5   -- This class allows to read and write a named file on the disk.
  6   -- Note that opening a file in READ and WRITE mode is not a very
  7   -- common case and may lead to performance decrease compared to
  8   -- TEXT_FILE_READ and TEXT_FILE_WRITE performance. Such a file
  9   -- is both an INPUT_STREAM and an OUTPUT_STREAM.
 10
 11inherit
 12   FILE_STREAM
 13      redefine out_in_tagged_out_memory
 14      end
 15   TERMINAL_INPUT_OUTPUT_STREAM
 16      redefine filtered_read_line_in, out_in_tagged_out_memory
 17      end
 18
 19insert
 20   STRING_HANDLER
 21      redefine out_in_tagged_out_memory
 22      end
 23
 24create {ANY}
 25   make, connect_to, connect_for_appending_to
 26
 27feature {ANY}
 28   connect_to (new_path: ABSTRACT_STRING)
 29         -- Open for reading and writing. The stream is positioned at the
 30         -- beginning of the file.
 31      local
 32         s: POINTER; tfw: TEXT_FILE_WRITE; exists: BOOLEAN
 33      do
 34         if (create {FILE_TOOLS}).is_readable(new_path) then
 35            exists := True
 36         else
 37            create tfw.connect_to(new_path)
 38            if tfw.is_connected then
 39               tfw.disconnect
 40               exists := True
 41            end
 42         end
 43         if exists then
 44            check
 45               (create {FILE_TOOLS}).is_readable(new_path)
 46            end
 47            s := text_file_read_write_open(new_path.to_external)
 48            if s.is_not_null then
 49               stream := s
 50               set_path(new_path)
 51            end
 52         end
 53      end
 54
 55   connect_for_appending_to (new_path: ABSTRACT_STRING)
 56         -- Open for reading and writing. The file is created if it does not
 57         -- exist. The stream is positioned at the end of the file.
 58      local
 59         s: POINTER; tfw: TEXT_FILE_WRITE; exists: BOOLEAN
 60      do
 61         if (create {FILE_TOOLS}).is_readable(new_path) then
 62            exists := True
 63         else
 64            create tfw.connect_to(new_path)
 65            if tfw.is_connected then
 66               tfw.disconnect
 67               exists := True
 68            end
 69         end
 70         if exists then
 71            check
 72               (create {FILE_TOOLS}).is_readable(new_path)
 73            end
 74            s := text_file_read_write_append(new_path.to_external)
 75            if s.is_not_null then
 76               stream := s
 77               set_path(new_path)
 78            end
 79         end
 80      end
 81
 82   disconnect
 83      do
 84         io_fclose(stream)
 85         path := Void
 86      end
 87
 88   can_unread_character: BOOLEAN
 89      do
 90         Result := not unread_character_flag
 91      end
 92
 93   end_of_input: BOOLEAN
 94      do
 95         io_flush(stream)
 96         Result := io_feof(stream)
 97      end
 98
 99   out_in_tagged_out_memory
100      do
101         tagged_out_memory.append(once "{TEXT_FILE_READ_WRITE ")
102         tagged_out_memory.append(path)
103         tagged_out_memory.extend('}')
104      end
105
106feature {FILTER_INPUT_STREAM}
107   filtered_last_character: CHARACTER
108
109   filtered_read_character
110      local
111         c: INTEGER
112      do
113         io_flush(stream)
114         c := io_getc(stream)
115         if c >= 0 then
116            filtered_last_character := c.to_character
117         end
118         unread_character_flag := False
119      end
120
121   filtered_unread_character
122      local
123         p: POINTER; c: CHARACTER
124      do
125         p := stream
126         c := last_character
127         io_ungetc(c, p)
128         unread_character_flag := True
129      end
130
131   filtered_read_line_in (str: STRING)
132      do
133         from
134            read_character
135         until
136            end_of_input or else last_character = '%N'
137         loop
138            str.extend(last_character)
139            read_character
140         end
141      end
142
143feature {FILTER_OUTPUT_STREAM}
144   filtered_put_character (c: CHARACTER)
145      do
146         io_flush(stream)
147         io_putc(c, stream)
148      end
149
150   filtered_flush
151      do
152         io_flush(stream)
153      end
154
155feature {FILTER}
156   filtered_descriptor: INTEGER
157      do
158         Result := sequencer_descriptor(stream)
159      end
160
161   filtered_has_descriptor: BOOLEAN True
162
163   filtered_stream_pointer: POINTER
164      do
165         Result := stream
166      end
167
168   filtered_has_stream_pointer: BOOLEAN True
169
170feature {}
171   unread_character_flag: BOOLEAN
172
173   stream: POINTER
174
175   make
176         -- The new created object is not connected. (See also `connect_to' and
177         -- `connect_for_appending_to'.)
178      do
179      ensure
180         not is_connected
181      end
182
183   text_file_read_write_open (a_path_pointer: POINTER): POINTER
184      external "plug_in"
185      alias "{
186         location: "${sys}/plugins"
187         module_name: "io"
188         feature_name: "text_file_read_write_open"
189         }"
190      end
191
192   text_file_read_write_append (a_path_pointer: POINTER): POINTER
193      external "plug_in"
194      alias "{
195         location: "${sys}/plugins"
196         module_name: "io"
197         feature_name: "text_file_read_write_append"
198         }"
199      end
200
201   io_fclose (a_stream_pointer: POINTER)
202      external "plug_in"
203      alias "{
204         location: "${sys}/plugins"
205         module_name: "io"
206         feature_name: "io_fclose"
207         }"
208      end
209
210   io_feof (a_stream_pointer: POINTER): BOOLEAN
211      external "plug_in"
212      alias "{
213         location: "${sys}/plugins"
214         module_name: "io"
215         feature_name: "io_feof"
216         }"
217      end
218
219end -- class TEXT_FILE_READ_WRITE
220--
221-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
222--
223-- Permission is hereby granted, free of charge, to any person obtaining a copy
224-- of this software and associated documentation files (the "Software"), to deal
225-- in the Software without restriction, including without limitation the rights
226-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
227-- copies of the Software, and to permit persons to whom the Software is
228-- furnished to do so, subject to the following conditions:
229--
230-- The above copyright notice and this permission notice shall be included in
231-- all copies or substantial portions of the Software.
232--
233-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
235-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
236-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
237-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
238-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
239-- THE SOFTWARE.