indexing
	cluster: "demangler"
	namespace: "candidate"
	description: "Report errors in a `SYNCHRONISED_DIRECTORY' object."
	original_author: "Justin Finnerty"
	original_date: "Oct 2005"
	current_date: "$Date: 2005/10/30 03:41:51 $"
	current_author: "$Author: finnerty $"
	revision: "$Revision: 2.2 $"	
	-- $Log: dearch_report.e,v $
	-- Revision 2.2  2005/10/30 03:41:51  finnerty
	-- Split REPORT_VIEW into REPORT_CONTROL and REPORT_VIEW. Made changes to other classes as necessary
	--
	-- Revision 2.1  2005/10/28 04:21:14  finnerty
	-- New report to search for duplicate archives.
	--

class DEARCH_REPORT
	-- Report errors in a `SYNCHRONISED_DIRECTORY' object. This report
	-- searches sets of archive files that are duplicated.

inherit
	DUPLICATE_REPORT
		redefine
			generate
		end

creation {ANY}
	make_dearch_report
	
feature {ANY}
	-- Access
	label: UC_STRING is
		once
			!!Result.make_from_utf8 ("Remove duplicate archives")
		end

	help: UC_STRING is
		once
			!!Result.make_from_utf8 ("This report analyses the store %
			%directory for duplicated archive files. If a pairs of %
			%archives have identical content then the report marks one %
			%archive file for deletion.")
		end

	config_label: STRING is
			-- The label used for this report in the configuration file
		once
			Result := "archive-suffix"
		end
	
feature {DUPLICATE_REPORT}
	-- Creation method
	make_dearch_report (a_sync_dir: SYNCHRONISED_DIRECTORY) is
			-- Initialise attributes to valid values
		do
			make_duplicate_report (a_sync_dir)
			-- We set the comparison to uncompressed as we are only 
			-- interested in bit-wise comparisons
			m_compress_type := Uncompressed
		end

feature {ANY}
	-- Manipulate a report
	generate is
			-- Analyse a directory and create report by building up the
			-- `transforms' set.
		do
			if m_suffix_set.is_empty then
				update_suffix_set
			end
			-- (1) For each "archive" suffix, search for matching
			-- filenames.  (2) If and archive filename has more
			-- than one entry, the exact same size and  (4) have identical content on
			-- bitwise comparison  (5) add DELETE_TRANSFORM object for
			-- the second filename.
			from
				m_suffix_set.start
			until
				m_suffix_set.off
			loop
				-- Search filename list for names with `m_current_suffix'
				-- (1) For each duplicate suffix, search for pairs of files.
				set_current_suffix (m_suffix_set.item_for_iteration)
				from
					sync_dir.namelist.start
				until
					sync_dir.namelist.off
				loop
					if sync_dir.namelist.item_for_iteration.count > 1 then
						if match_tail (sync_dir.namelist.key_for_iteration) then
							compare_files_in_list (sync_dir.namelist.item_for_iteration)
						end
					end
					sync_dir.namelist.forth
				end
				m_suffix_set.forth
			end
		end

feature {DUPLICATE_REPORT}
	-- Hidden methods
	frozen compare_files_in_list (a_list: DS_LIST[FILE_SYSTEM_OBJECT]) is
			-- Compare pairs of files for identity.
		local
			l_index: INTEGER
			l_cursor: DS_LIST_CURSOR[FILE_SYSTEM_OBJECT]
			l_fso1, l_fso2: FILE_SYSTEM_OBJECT
		do
			-- (3) If pair of archives have the exact same size
			l_cursor := a_list.new_cursor
			from
				a_list.start
				l_index := 2
			until
				l_index > a_list.count or else
				a_list.off
			loop
				l_fso1 := a_list.item_for_iteration
				from
					l_cursor.go_i_th (l_index)
				until
					l_cursor.off
				loop
					l_fso2 := l_cursor.item
					-- (4) have identical content on bitwise
					-- comparison
					if compare_fso (l_fso1, l_fso2, m_compress_type, view) then
						new_delete_transform (l_fso1)
						-- Short-circuit: Don't need to do more comparisons
						l_cursor.go_after
					else
						l_cursor.forth
					end
				end
				l_index := l_index + 1
				a_list.forth
			end
		end
		
	
end -- class DEARCH_REPORT
