PageRenderTime 27ms CodeModel.GetById 23ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/io/tools/file_tools.e

http://github.com/tybor/Liberty
Specman e | 303 lines | 227 code | 29 blank | 47 comment | 7 complexity | b29252a76d79d4f5cab22a2ec3332528 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4expanded class FILE_TOOLS
  5   -- This expanded class gather tools relatives to files such as
  6   -- file comparison, file renaming, file deletion, file size, file
  7   -- permissions...
  8   --
  9   -- Note this is a facilities class. Files are referenced with
 10   -- their names (as STRINGs). Consider using functions available in
 11   -- TEXT_FILE_READ if you are already connected to the file.
 12
 13insert
 14   ANY
 15
 16feature {ANY}
 17   same_files (path1, path2: ABSTRACT_STRING): BOOLEAN
 18         -- True if the `path1' file exists and has the very same content as file `path2'.
 19      require
 20         path1 /= Void
 21         path2 /= Void
 22      do
 23         tfr1.connect_to(path1)
 24         if tfr1.is_connected then
 25            tfr2.connect_to(path2)
 26            if tfr2.is_connected then
 27               Result := tfr1.same_as(tfr2)
 28               check
 29                  tfr1_closed_by_same_as: not tfr1.is_connected
 30                  tfr2_closed_by_same_as: not tfr2.is_connected
 31               end
 32            else
 33               tfr1.disconnect
 34            end
 35         end
 36      end
 37
 38   same_physical_file (path1, path2: ABSTRACT_STRING): BOOLEAN
 39         -- True if `path1' and `path2' physically refer to the same file (e.g. symlinks to a same file will
 40         -- return True here)
 41      require
 42         path1 /= Void
 43         path2 /= Void
 44      do
 45         Result := io_same_physical_file(path1.to_external, path2.to_external)
 46      end
 47
 48   file_exists (path: ABSTRACT_STRING): BOOLEAN
 49      require
 50         path /= Void
 51         path.count > 0
 52      do
 53         Result := io_file_exists(path.to_external)
 54      end
 55
 56   is_readable (path: ABSTRACT_STRING): BOOLEAN
 57         -- True if `path' file exists and is either a readable file or an accessible directory.
 58      require
 59         path /= Void
 60      local
 61         bd: BASIC_DIRECTORY
 62      do
 63         if is_directory(path) then
 64            bd.connect_to(path)
 65            if bd.is_connected then
 66               Result := True
 67               bd.disconnect
 68            end
 69         else
 70            tfr1.connect_to(path)
 71            if tfr1.is_connected then
 72               Result := True
 73               tfr1.disconnect
 74            end
 75         end
 76      end
 77
 78   is_empty (path: ABSTRACT_STRING): BOOLEAN
 79         -- True if `path' file exists, is readable and is an empty file.
 80      require
 81         path /= Void
 82         not path.is_empty
 83      do
 84         tfr1.connect_to(path)
 85         if tfr1.is_connected then
 86            tfr1.read_character
 87            Result := tfr1.end_of_input
 88            tfr1.disconnect
 89         end
 90      end
 91
 92   rename_to (old_path, new_path: ABSTRACT_STRING)
 93         -- Try to change the name or the location of a file.
 94      require
 95         old_path /= Void
 96         new_path /= Void
 97      local
 98         p1, p2: POINTER
 99      do
100         if file_exists(new_path) then
101            delete(new_path)
102         end
103         p1 := old_path.to_external
104         p2 := new_path.to_external
105         last_rename_succeeded := io_rename(p1, p2)
106      end
107
108   last_rename_succeeded: BOOLEAN
109         -- True if the last call to `rename_to` was successful.
110
111   copy_to (source_path, target_path: ABSTRACT_STRING)
112         -- Try to copy the source into the target.
113      require
114         source_path /= Void
115         target_path /= Void
116      local
117         src, tgt: POINTER
118      do
119         src := source_path.to_external
120         tgt := target_path.to_external
121         last_copy_succeeded := io_copy(src, tgt)
122      end
123
124   last_copy_succeeded: BOOLEAN
125         -- True if the last call to `copy_to` was successful.
126
127   delete (path: ABSTRACT_STRING)
128         -- Try to delete the given `path' file.
129      require
130         path /= Void
131      local
132         p: POINTER
133      do
134         p := path.to_external
135         last_delete_succeeded := io_remove(p)
136      end
137
138   last_delete_succeeded: BOOLEAN
139         -- True if the last call to `delete` was successful.
140
141   size_of (path: ABSTRACT_STRING): INTEGER
142         -- Total size of file `path' in number of bytes.
143         -- When the corresponding file does not exists, the Result is negative.
144      require
145         path /= Void
146         path.count > 0
147      local
148         p: POINTER
149      do
150         p := path.to_external
151         Result := fstat_st_size(p)
152      end
153
154   last_change_of (path: ABSTRACT_STRING): TIME
155         -- Of the last modification of `path'.
156      require
157         path /= Void
158         path.count > 0
159      local
160         p: POINTER; time_memory: INTEGER_64
161      do
162         p := path.to_external
163         time_memory := fstat_st_mtime(p)
164         Result.set_time_memory(time_memory)
165      end
166
167   is_file (path: ABSTRACT_STRING): BOOLEAN
168         -- Is `path' a regular file?
169      require
170         path /= Void
171         path.count > 0
172      do
173         Result := fstat_st_is_file(path.to_external)
174      end
175
176   is_directory (path: ABSTRACT_STRING): BOOLEAN
177         -- Is `path' a directory?
178      require
179         path /= Void
180         path.count > 0
181      do
182         Result := fstat_st_is_dir(path.to_external)
183      end
184
185feature {}
186   io_remove (path: POINTER): BOOLEAN
187         -- To implement `delete'.
188      external "plug_in"
189      alias "{
190         location: "${sys}/plugins"
191         module_name: "io"
192         feature_name: "io_remove"
193         }"
194      end
195
196   io_rename (old_path, new_path: POINTER): BOOLEAN
197      external "plug_in"
198      alias "{
199         location: "${sys}/plugins"
200         module_name: "io"
201         feature_name: "io_rename"
202         }"
203      end
204
205   io_copy (source, target: POINTER): BOOLEAN
206      external "plug_in"
207      alias "{
208         location: "${sys}/plugins"
209         module_name: "io"
210         feature_name: "io_copy"
211         }"
212      end
213
214   io_file_exists (path: POINTER): BOOLEAN
215      external "plug_in"
216      alias "{
217         location: "${sys}/plugins"
218         module_name: "io"
219         feature_name: "io_file_exists"
220         }"
221      end
222
223   io_same_physical_file (path1, path2: POINTER): BOOLEAN
224      external "plug_in"
225      alias "{
226         location: "${sys}/plugins"
227         module_name: "io"
228         feature_name: "io_same_physical_file"
229         }"
230      end
231
232   fstat_st_size (path: POINTER): INTEGER
233      external "plug_in"
234      alias "{
235         location: "${sys}/plugins/io"
236         module_name: "fstat"
237         feature_name: "fstat_st_size"
238         }"
239      end
240
241   fstat_st_mtime (path: POINTER): INTEGER_64
242      external "plug_in"
243      alias "{
244         location: "${sys}/plugins/io"
245         module_name: "fstat"
246         feature_name: "fstat_st_mtime"
247         }"
248      end
249
250   fstat_st_is_file (path: POINTER): BOOLEAN
251      external "plug_in"
252      alias "{
253         location: "${sys}/plugins/io"
254         module_name: "fstat"
255         feature_name: "fstat_st_is_file"
256         }"
257      end
258
259   fstat_st_is_dir (path: POINTER): BOOLEAN
260      external "plug_in"
261      alias "{
262         location: "${sys}/plugins/io"
263         module_name: "fstat"
264         feature_name: "fstat_st_is_dir"
265         }"
266      end
267
268   tfr1: TEXT_FILE_READ
269      once
270         create Result.make
271      end
272
273   tfr2: TEXT_FILE_READ
274      once
275         create Result.make
276      end
277
278   tmp_string: STRING
279      once
280         create Result.make(256)
281      end
282
283end -- class FILE_TOOLS
284--
285-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
286--
287-- Permission is hereby granted, free of charge, to any person obtaining a copy
288-- of this software and associated documentation files (the "Software"), to deal
289-- in the Software without restriction, including without limitation the rights
290-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
291-- copies of the Software, and to permit persons to whom the Software is
292-- furnished to do so, subject to the following conditions:
293--
294-- The above copyright notice and this permission notice shall be included in
295-- all copies or substantial portions of the Software.
296--
297-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
298-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
299-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
300-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
301-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
302-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
303-- THE SOFTWARE.