note
	description: "[
		File utilities, for retrieving files and folders and formatting paths.
	]"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	date: "$Date: 2020-04-04 18:39:52 +0000 (Sat, 04 Apr 2020) $"
	revision: "$Revision: 104028 $"

expanded class 
	FILE_UTILITIES

create 
	default_create

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end
	
feature -- Access

	generating_type: TYPE [detachable FILE_UTILITIES]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal alias "≡≡≡" (other: FILE_UTILITIES): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: FILE_UTILITIES): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal alias "" (other: FILE_UTILITIES): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: FILE_UTILITIES)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: FILE_UTILITIES)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: FILE_UTILITIES
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: FILE_UTILITIES)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: FILE_UTILITIES
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: FILE_UTILITIES
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached FILE_UTILITIES
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable FILE_UTILITIES
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature -- Directory operations

	create_directory (a_path: READABLE_STRING_GENERAL)
			-- Creates a directory and any parent directories if they do not exist.
			--
			-- a_path: The directory to create.
		require
			a_path_attached: a_path /= Void
			not_a_path_is_empty: not a_path.is_empty
		local
			is_retried: BOOLEAN
			d: DIRECTORY
		do
			if not is_retried then
				create d.make (a_path)
				if not d.exists then
					d.recursive_create_dir
				end
			end
		ensure
				class
		rescue
			is_retried := True
			retry
		end

	create_directory_path (a_path: PATH)
			-- Creates a directory and any parent directories if they do not exist.
			--
			-- a_path: The path to create.
		require
			a_path_attached: a_path /= Void
			not_a_path_is_empty: not a_path.is_empty
		local
			is_retried: BOOLEAN
			d: DIRECTORY
		do
			if not is_retried then
				create d.make_with_path (a_path)
				if not d.exists then
					d.recursive_create_dir
				end
			end
		ensure
				class
		rescue
			is_retried := True
			retry
		end

	directory_exists (n: READABLE_STRING_GENERAL): BOOLEAN
			-- Does directory of name n exist?
		do
			Result := not n.is_empty and then (create {DIRECTORY}.make (n)).exists
		end

	directory_names (n: READABLE_STRING_32): detachable ARRAYED_LIST [STRING_32]
			-- List of directory names (excluding current and parent directory) in directory with name n.
			-- Or void if directory is not readable (does not exist, cannot be accessed, etc.).
		local
			d: detachable DIRECTORY
			is_retried: BOOLEAN
			f: DIRECTORY
		do
			if not is_retried then
				create d.make (n)
				if d.exists and then d.is_readable then
					d.open_read
					from
						create Result.make (0);
						d.readentry
					until
						not attached d.last_entry_32 as e
					loop
						if (e.count = 1 and then e [1] = '.'.to_character_32) or else (e.count = 2 and then e [1] = '.'.to_character_32 and then e [2] = '.'.to_character_32) then
						else
							create f.make_with_path ((create {PATH}.make_from_string (n)).extended (e))
							if f.exists and then f.is_readable then
								Result.extend (e)
							end
						end;
						d.readentry
					end;
					d.close
				end
			elseif attached d and then not d.is_closed then
				d.close
			end
		rescue
			is_retried := True
			retry
		end

	directory_path_exists (p: PATH): BOOLEAN
			-- Does directory of path p exist?
		do
			Result := not p.is_empty and then (create {DIRECTORY}.make_with_path (p)).exists
		end

	file_names (n: READABLE_STRING_32): detachable ARRAYED_LIST [STRING_32]
			-- List of file names in directory with name n.
			-- Or void if directory is not readable (does not exist, cannot be accessed, etc.).
		local
			d: detachable DIRECTORY
			is_retried: BOOLEAN
			f: RAW_FILE
		do
			if not is_retried then
				create d.make (n)
				if d.exists and then d.is_readable then
					d.open_read
					from
						create Result.make (0);
						d.readentry
					until
						not attached d.last_entry_32 as e
					loop
						create f.make_with_path ((create {PATH}.make_from_string (n)).extended (e))
						if f.exists and then f.is_readable and then f.is_plain then
							Result.extend (e)
						end;
						d.readentry
					end;
					d.close
				end
			elseif attached d and then not d.is_closed then
				d.close
			end
		rescue
			is_retried := True
			retry
		end
	
feature -- File name operations

	make_directory_name_in (name: READABLE_STRING_GENERAL; location: READABLE_STRING_GENERAL): READABLE_STRING_GENERAL
			-- A directory name for directory name in directory location.
		do
			Result := make_file_name_in (name, location)
		end

	make_file_name_in (name: READABLE_STRING_GENERAL; location: READABLE_STRING_GENERAL): READABLE_STRING_GENERAL
			-- A file name for file name in directory location.
		do
			Result := (create {PATH}.make_from_string (location)).extended (name).name
		end
	
feature -- File operations

	copy_file (old_name, new_name: READABLE_STRING_GENERAL)
			-- Copy file named old_name to new_name.
		local
			f: detachable RAW_FILE
			t: detachable RAW_FILE
			is_rescued: BOOLEAN
		do
			if is_rescued then
				if attached f and then f.is_open_read then
					f.close
				end
				if attached t and then t.is_open_write then
					t.close
				end
			else
				create f.make_with_name (old_name);
				f.open_read
				create t.make_with_name (new_name);
				t.open_write;
				f.copy_to (t);
				f.close;
				t.close
			end
		rescue
			if not is_rescued then
				is_rescued := True
				retry
			end
		end

	copy_file_path (old_path, new_path: PATH)
			-- Copy file named old_path to new_path.
		local
			f: detachable RAW_FILE
			t: detachable RAW_FILE
			is_rescued: BOOLEAN
		do
			if is_rescued then
				if attached f and then f.is_open_read then
					f.close
				end
				if attached t and then t.is_open_write then
					t.close
				end
			else
				create f.make_with_path (old_path);
				f.open_read
				create t.make_with_path (new_path);
				t.open_write;
				f.copy_to (t);
				f.close;
				t.close
			end
		rescue
			if not is_rescued then
				is_rescued := True
				retry
			end
		end

	file_exists (n: READABLE_STRING_GENERAL): BOOLEAN
			-- Does file of name n exist?
		local
			f: RAW_FILE
			is_retried: BOOLEAN
		do
			if not n.is_empty and then not is_retried then
				create f.make_with_name (n)
				Result := f.exists and then f.is_plain
			end
		rescue
			is_retried := True
			retry
		end

	file_path_exists (p: PATH): BOOLEAN
			-- Does file of path p exist?
		local
			f: RAW_FILE
			is_retried: BOOLEAN
		do
			if not p.is_empty and then not is_retried then
				create f.make_with_path (p)
				Result := f.exists and then f.is_plain
			end
		rescue
			is_retried := True
			retry
		end

	rename_file (old_name, new_name: READABLE_STRING_GENERAL)
			-- Rename file named old_name to new_name.
		do
			(create {RAW_FILE}.make_with_name (old_name)).rename_file (new_name)
		end

	rename_file_path (old_path, new_path: PATH)
			-- Rename file named old_path to new_path.
		do
			(create {RAW_FILE}.make_with_path (old_path)).rename_path (new_path)
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result;
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		local
			s: READABLE_STRING_8
		do
			if attached o then
				s := o.out
				if attached {READABLE_STRING_32} s as s32 then
					Io.put_string_32 (s32)
				elseif attached {READABLE_STRING_8} s as s8 then
					Io.put_string (s8)
				else
					Io.put_string_32 (s.as_string_32)
				end
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature -- Query

	frozen compact_path (a_path: READABLE_STRING_32): detachable STRING_32
			-- Compacts a file path, removing . and ..
			--
			-- a_path: A path to compact.
			-- Result: The compacted path or Void if the path could not be compacted.
		require
			a_path_attached: a_path /= Void
			not_a_path_is_empty: not a_path.is_empty
		local
			l_sep: CHARACTER_32
			l_parts: LIST [READABLE_STRING_32]
			l_part: READABLE_STRING_32
			l_error: BOOLEAN
		do
			l_sep := Operating_environment.Directory_separator.to_character_32
			l_parts := a_path.split (l_sep)
			from
				l_parts.start
			until
				l_parts.after or l_error
			loop
				l_part := l_parts.item
				if l_part.same_string_general (".") then
					l_parts.remove
				elseif l_part.same_string_general ("..") then
					l_parts.remove
					if not l_parts.is_empty then
						l_parts.back
						if not l_parts.before and not l_parts.item.is_empty then
							l_parts.remove
						else
							l_error := True
						end
					end
				else
					l_parts.forth
				end
			end
			if not l_error then
				create Result.make (a_path.count)
				from
					l_parts.start
				until
					l_parts.after
				loop
					Result.append (l_parts.item)
					if not l_parts.islast then
						Result.append_character (l_sep)
					end;
					l_parts.forth
				end
			end
		end

	ends_with (a_folder: PATH; a_end_with: READABLE_STRING_GENERAL; a_levels: INTEGER_32): ARRAYED_LIST [PATH]
			-- Scans a folder for matching files.
			--
			-- a_folder: Folder location to scan.
			-- a_levels: Number of levels to recursively scan. 0 to scan the specified folder only, -1 to scan all folders.
			-- a_end_with: A string results end with, ignoring cases.
		require
			a_folder_exists: directory_path_exists (a_folder)
			a_levels_is_valid: a_levels >= -1
		do
			Result := internal_files_end_with (a_folder, a_end_with, a_levels, True)
		end
	
feature {NONE} -- Query

	frozen internal_files_end_with (a_folder: PATH; a_end_with: READABLE_STRING_GENERAL; a_levels: INTEGER_32; a_recursive: BOOLEAN): ARRAYED_LIST [PATH]
			-- Scans a folder for matching files.
			--
			-- a_folder: Folder location to scan.
			-- a_levels: Number of levels to recursively scan. 0 to scan the specified folder only, -1 to scan all folders.
			-- a_end_with: A string results end with, ignoring cases
		require
			a_folder_exists: directory_path_exists (a_folder)
			a_levels_is_valid: a_levels >= -1
		local
			l_dn: PATH
			l_dir: detachable DIRECTORY
			l_retried: BOOLEAN
			l_path: PATH
			f: RAW_FILE
			d: DIRECTORY
		do
			if not l_retried then
				if a_recursive then
					l_dn := a_folder.absolute_path
				else
					l_dn := a_folder
				end
				create l_dir.make_with_path (l_dn)
				if l_dir.exists and then l_dir.is_readable then
					l_dir.open_read
					from
						create Result.make (l_dir.count);
						l_dir.readentry
					until
						not attached l_dir.last_entry_32 as e
					loop
						l_path := l_dn.extended (e)
						create f.make_with_path (l_path)
						if f.exists and then f.is_readable and then f.is_plain and then e.as_lower.ends_with_general (a_end_with.as_lower) then
							Result.extend (l_path)
						else
							if (e.count = 1 and then e [1] = '.'.to_character_32) or else (e.count = 2 and then e [1] = '.'.to_character_32 and then e [2] = '.'.to_character_32) then
							elseif a_levels > 0 or else a_levels = -1 then
								create d.make_with_path (l_path)
								if d.exists and then d.is_readable then
									Result.append (internal_files_end_with (l_path, a_end_with, (a_levels - 1).max (-1), False))
								end
							end
						end;
						l_dir.readentry
					end;
					l_dir.close
				else
					create Result.make (0)
				end
			elseif l_dir /= Void and then not l_dir.is_closed then
				l_dir.close
				create Result.make (0)
			else
				create Result.make (0)
			end
		rescue
			l_retried := True
			retry
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_32
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ".as_string_32)
				create l_exc;
				l_msg.append (generating_type.name_32);
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

note
	copyright: "Copyright (c) 1984-2020, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	licensing_options: "http://www.eiffel.com/licensing"
	copying: "[
		This file is part of Eiffel Software's Eiffel Development Environment.
		
		Eiffel Software's Eiffel Development Environment is free
		software; you can redistribute it and/or modify it under
		the terms of the GNU General Public License as published
		by the Free Software Foundation, version 2 of the License
		(available at the URL listed under "license" above).
		
		Eiffel Software's Eiffel Development Environment is
		distributed in the hope that it will be useful, but
		WITHOUT ANY WARRANTY; without even the implied warranty
		of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		See the GNU General Public License for more details.
		
		You should have received a copy of the GNU General Public
		License along with Eiffel Software's Eiffel Development
		Environment; if not, write to the Free Software Foundation,
		Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
	]"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class FILE_UTILITIES

Generated by ISE EiffelStudio