PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/io/filesystem/regular_file.e

http://github.com/tybor/Liberty
Specman e | 130 lines | 83 code | 15 blank | 32 comment | 3 complexity | 0a3e8a8f718d1fae45c585de01886c1f MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class REGULAR_FILE
  5
  6inherit
  7   FILE
  8
  9create {ANY}
 10   make
 11
 12feature {ANY}
 13   name: FIXED_STRING
 14   path: FIXED_STRING
 15
 16   is_directory: BOOLEAN False
 17   is_regular: BOOLEAN True
 18
 19   as_directory: DIRECTORY
 20      do
 21         check False end
 22      end
 23
 24   as_regular: REGULAR_FILE
 25      do
 26         Result := Current
 27      end
 28
 29   exists: BOOLEAN
 30      local
 31         ft: FILE_TOOLS
 32      do
 33         Result := ft.file_exists(path)
 34      end
 35
 36feature {ANY} -- Text stream access
 37   read: INPUT_STREAM
 38         -- Returns a stream connected for reading the file. If the read stream is not connected anymore,
 39         -- connects it again.
 40         -- Always returns the same object.
 41      require
 42         not is_writing
 43      do
 44         if read_memory = Void then
 45            create read_memory.connect_to(path.out)
 46         elseif not read_memory.is_connected then
 47            read_memory.connect_to(path.out)
 48         end
 49         Result := read_memory
 50      ensure
 51         is_reading
 52      end
 53
 54   is_reading: BOOLEAN
 55      do
 56         Result := read_memory /= Void and then read_memory.is_connected
 57      end
 58
 59   write: OUTPUT_STREAM
 60         -- Returns a stream connected for writing to the file. If the write stream is not connected anymore,
 61         -- connects it again.
 62         -- Always returns the same object.
 63      require
 64         not is_reading
 65      do
 66         if write_memory = Void then
 67            create write_memory.connect_to(path.out)
 68         elseif not write_memory.is_connected then
 69            write_memory.connect_to(path.out)
 70         end
 71         Result := write_memory
 72      ensure
 73         is_writing
 74      end
 75
 76   append: OUTPUT_STREAM
 77         -- Returns a stream connected for appending to the file. If the write stream is already connected,
 78         -- use `write' instead.
 79         -- Always returns the same object.
 80      require
 81         not is_reading
 82         not is_writing
 83      do
 84         if write_memory = Void then
 85            create write_memory.connect_for_appending_to(path.out)
 86         else
 87            write_memory.connect_for_appending_to(path.out)
 88         end
 89         Result := write_memory
 90      ensure
 91         is_writing
 92      end
 93
 94   is_writing: BOOLEAN
 95      do
 96         Result := write_memory /= Void and then write_memory.is_connected
 97      end
 98
 99feature {}
100   make (a_file_path: ABSTRACT_STRING)
101      do
102         path := a_file_path.intern
103         basic_directory.compute_short_name_of(path)
104         name := basic_directory.last_entry.intern
105      end
106
107   read_memory: TEXT_FILE_READ
108   write_memory: TEXT_FILE_WRITE
109
110end -- class REGULAR_FILE
111--
112-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
113--
114-- Permission is hereby granted, free of charge, to any person obtaining a copy
115-- of this software and associated documentation files (the "Software"), to deal
116-- in the Software without restriction, including without limitation the rights
117-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
118-- copies of the Software, and to permit persons to whom the Software is
119-- furnished to do so, subject to the following conditions:
120--
121-- The above copyright notice and this permission notice shall be included in
122-- all copies or substantial portions of the Software.
123--
124-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
125-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
126-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
127-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
128-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
129-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
130-- THE SOFTWARE.