indexing description: "[ Implements a playlist for loading filenames. Use this class to store multiple filenames in a list, as files can be searched on creation. This class was designed for having file loading support in a hurry. We recommend to use it whenever you need to find multiple files. Note: If you use {EM_MUSIC_PLAYER} and/or {EM_SOUND_PLAYER}, you don't need such a playlist as both players already have their own. ]" date: "$Date$" revision: "$Revision$" class EM_PLAYLIST create make_with_path, make_with_list, make_with_file, make_empty feature -- Initialization make_with_path (a_path: STRING; an_extension_list: DS_LINKED_LIST [STRING]; do_recursive_search: BOOLEAN) is -- Make playlist from `a_path' with files having an extension -- like one element in `an_extension_list'. -- If you also want to search file in subfolders, please use -- `do_recursive_search'. require a_path_not_void: a_path /= Void do make_empty internal_list := search_directory (a_path, an_extension_list, do_recursive_search) create cursor.make (internal_list) end make_with_list (a_list: DS_LINKED_LIST [STRING]) is -- Make playlist from `a_list'. do make_empty internal_list := a_list create cursor.make (internal_list) end make_with_file (a_filename: STRING) is -- Make with `a_filename'. require a_filename_not_void: a_filename /= Void do make_empty extend (a_filename) ensure count_is_one: count = 1 end make_empty is -- Create empty playlist. do create internal_list.make create cursor.make (internal_list) cursor.start end feature -- File search search_directory (a_path: STRING; an_extension_list: DS_LINKED_LIST [STRING]; do_recursive_search: BOOLEAN): DS_LINKED_LIST [STRING] is -- Search `a_path' in a `do_recursive_search' way for audio files -- with an extension in `an_extension_list'. -- -- Note: Recursive search may take long time, if there are many subfolders. -- Links are not followed by recursive search. require a_path_not_void: a_path /= Void local a_directory: DIRECTORY a_file: RAW_FILE a_linear_representation: ARRAYED_LIST [STRING] an_extension: STRING do create Result.make create a_directory.make (a_path) if a_directory.exists then from a_linear_representation := a_directory.linear_representation a_linear_representation.start until a_linear_representation.after loop -- If not special folders "." and ".." if not (a_linear_representation.item.is_equal (".") or else a_linear_representation.item.is_equal ("..")) then create a_file.make (a_path + "/" + a_linear_representation.item) if a_file.exists then -- If file is a directory and we do recursive search, enter the folder. if do_recursive_search and then a_file.is_directory and then not a_file.is_symlink then Result.append_last (search_directory (a_file.name, an_extension_list, do_recursive_search)) else -- List all files corresponding to one of the extensions. from an_extension_list.start until an_extension_list.after loop create an_extension.make_from_string (a_file.name) an_extension.keep_tail (an_extension_list.item_for_iteration.count) if an_extension.is_equal (an_extension_list.item_for_iteration) then Result.put_last (a_file.name) end an_extension_list.forth end end end end a_linear_representation.forth end end end feature -- Operations get_i_th (an_index: INTEGER): STRING is -- Element at `an_index' from playlist require an_index_is_valid: an_index <= count and an_index > 0 do cursor.go_i_th (an_index) create Result.make_from_string (cursor.item) end next: STRING is -- Next element in playlist do cursor.forth create Result.make_from_string (cursor.item) end back: STRING is -- Previous element in playlist do cursor.back create Result.make_from_string (cursor.item) end extend (a_filename: STRING) is -- Extend playlist with `a_filename'. -- New entries are inserted at the end. require filename_not_void: a_filename /= Void do internal_list.put_last (a_filename) ensure inserted: internal_list.has (a_filename) count_increased: count = old count + 1 end insert (a_filename: STRING; an_index: INTEGER) is -- Insert `a_filename' at position `an_index'. require filename_not_void: a_filename /= Void index_valid: 1 <= an_index and then an_index <= count do cursor.go_i_th (an_index) cursor.put_right (a_filename) ensure inserted: internal_list.item (an_index).is_equal (a_filename) end remove (a_filename: STRING) is -- Removes `a_filename' from playlist. require filename_not_void: a_filename /= Void do internal_list.delete (a_filename) ensure removed: not internal_list.has (a_filename) end wipe_out is -- Remove all playlist entries. do internal_list.wipe_out ensure wiped_out: count = 0 end feature -- Access count: INTEGER is -- Number of playlist entries do Result := internal_list.count end index: INTEGER is -- Current index do Result := cursor.index end item: STRING is -- Current selected playlist entry do create Result.make_from_string (internal_list.item_for_iteration) end has (an_element: like item): BOOLEAN is -- Is `an_element' already in the list? local c: DS_LINKED_LIST_CURSOR [STRING] do create c.make (internal_list) from Result := False c.start until Result or else c.after loop if c.item.is_equal (an_element) then Result := True end c.forth end end position (an_element: like item): INTEGER is -- Position of `an_element' if found or 0 if not found. local c: DS_LINKED_LIST_CURSOR [STRING] break: BOOLEAN do create c.make (internal_list) from Result := 0 c.start until break or else c.after loop if c.item.is_equal (an_element) then Result := c.index break := True end c.forth end end feature {NONE} -- Internal variables internal_list: DS_LINKED_LIST [STRING] -- an internal linked list cursor: DS_LINKED_LIST_CURSOR [STRING] -- Linked list of the cursor invariant internal_list_created: internal_list /= Void cursor_created: cursor /= Void end