indexing description: "[ Makes filehandling easier ]" date: "$Date$" revision: "$Revision$" class EM_FILE_LOADER inherit EM_SHARED_BITMAP_FACTORY export {NONE} all end EM_SHARED_AUDIO_FACTORY export {NONE} all end EM_SHARED_ERROR_HANDLER export {NONE} all end KL_SHARED_FILE_SYSTEM export {NONE} all end create {EM_SHARED_FILE_LOADER} make feature -- Init make is -- Create new file_loader do create text_opening_event create current_filename.make_empty ignore_missing_file := false ensure ignore_missing_file_is_deactivated: ignore_missing_file = false end feature -- Access last_text_file : PLAIN_TEXT_FILE -- Last textfile created last_bitmap : EM_BITMAP -- Last bitmap created last_sound : EM_SOUND -- Last sound created last_music : EM_MUSIC -- Last music created last_animation: EM_ANIMATION -- Last animation created last_sprite: EM_SPRITE -- Last sprite created last_ttf_font: EM_COLOR_TTF_FONT -- Last color ttf-font created last_bmp_font: EM_BMP_FONT -- Last bitmapfont created feature -- load text files create_text_file_read_write(a_filename: STRING) is -- create new text file, read/write-mode do create last_text_file.make_create_read_write (a_filename) ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end load_text_file_read (a_filename: STRING) is -- load to read do create current_filename.make_from_string (a_filename) load_text_file (agent impl_read) ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end load_text_file_read_write (a_filename: STRING) is -- load file to read or write do create current_filename.make_from_string (a_filename) load_text_file (agent impl_read_write) ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end load_text_file_write (a_filename: STRING) is -- load file to write do create current_filename.make_from_string (a_filename) load_text_file (agent impl_write) ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end load_text_file_append (a_filename: STRING) is -- load file to append do create current_filename.make_from_string (a_filename) load_text_file (agent impl_append) ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end load_text_file_or_create (a_filename:STRING) is -- load file a_filename if exists else create empty file do if file_system.file_exists (a_filename) then load_text_file_read_write (a_filename) else create_text_file_read_write (a_filename) end ensure last_text_file_not_void_or_exception: last_text_file /= void or Error_handler.has_error_occured end feature -- load drawable element load_bitmap (a_filename: STRING) is -- load a_filename to last_bitmap_file local error_occured: BOOLEAN do if not error_occured then bitmap_factory.create_bitmap_from_image (a_filename) last_bitmap := bitmap_factory.last_bitmap end ensure last_bitmap_not_void_or_exception: last_bitmap /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end load_animation (a_filename: STRING) is -- load a_filename animation local dummy_animation: EM_ANIMATION error_occured: BOOLEAN do if not error_occured then --TODO proper errorhandling in EM_ANIMATION create dummy_animation.make_from_file (a_filename) last_animation := dummy_animation end ensure last_animation_not_void_or_exception: last_animation /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end load_sprite (a_filename: STRING; an_x, a_y:INTEGER) is -- load a_filename animation local dummy_animation: EM_ANIMATION dummy_sprite: EM_SPRITE error_occured: BOOLEAN do if not error_occured then --TODO proper errorhandling in EM_ANIMATION create dummy_animation.make_from_file (a_filename) create dummy_sprite.make_from_animation (dummy_animation,an_x,a_y) last_sprite := dummy_sprite end ensure last_sprite_not_void_or_exception: last_sprite /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end load_ttf_font (a_filename: STRING; a_point_size: INTEGER) is -- load a_filename ttf-font local dummy_font: EM_COLOR_TTF_FONT error_occured: BOOLEAN do if not error_occured then create dummy_font.make_from_ttf_file (a_filename, a_point_size) last_ttf_font := dummy_font end ensure last_ttf_font_not_void_or_exception: last_ttf_font /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end load_bmp_font (a_filename: STRING) is -- load a_filename bitmap-font local font_bitmap: EM_BITMAP dummy_font: EM_BMP_FONT error_occured: BOOLEAN do if not error_occured then bitmap_factory.create_bitmap_from_image (a_filename) font_bitmap := bitmap_factory.last_bitmap create dummy_font.make (font_bitmap) last_bmp_font := dummy_font end ensure last_bmp_font_not_void_or_exception: last_bmp_font /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end feature -- load sound load_sound_file (a_filename: STRING) is -- load a_filename to last_sound_file local error_occured: BOOLEAN do if not error_occured then audio_factory.create_sound_from_file (a_filename) last_sound := audio_factory.last_sound end ensure last_sound_not_void_or_exception: last_sound /= void or Error_handler.has_error_occured rescue if ignore_missing_file then error_occured := true retry end end load_music_file (a_filename: STRING) is -- load a_filename to last_music_file local error_occured: BOOLEAN do if not error_occured then audio_factory.create_music_from_file (a_filename) last_music := audio_factory.last_music end ensure last_music_not_void_or_exception: last_music /= void or Error_handler.has_error_occured rescue has_error := true if ignore_missing_file then error_occured := true retry end end feature -- Set status set_ignore_missing_file(a_bool: BOOLEAN) is -- set igrnore_missing_file do ignore_missing_file := a_bool ensure state_is_set: ignore_missing_file = a_bool end reset_error is -- set has_error to false do has_error := false ensure has_error_is_reseted: has_error = false end wipeout is -- set all last_ -values void do last_animation := void last_bitmap := void last_music := void last_sound := void last_text_file := void last_ttf_font := void last_bmp_font := void last_sprite := void ensure all_last_video_items_are_void: last_animation = void and last_bitmap = void and last_sprite = void all_last_font_itmes_are_void: last_bmp_font = void and last_ttf_font = void all_last_audio_items_are_void: last_sound = void and last_music = void last_text_file_is_void: last_text_file = void end feature -- Status report ignore_missing_file: BOOLEAN -- Go on if not able to load file? has_error: BOOLEAN -- Did an error occured? file_exists (a_filename: STRING): BOOLEAN is -- Does file a_filename exist? require a_filename_not_void: a_filename /= void do Result := file_system.file_exists (a_filename) end file_extension (a_filename: STRING): STRING is -- Returns the file-extension of a_filename require a_filename_not_void: a_filename /= void do Result := file_system.extension (a_filename) end feature {NONE} -- Implementation get_filepath(a_filename:STRING):STRING is -- get full pathname of a_filename require a_filename_not_void: a_filename /= void do create Result.make_from_string(file_system.pathname (file_system.current_working_directory,a_filename)) end feature {NONE} -- Implementation text-files load_text_file (a_open_procedure : PROCEDURE [ANY, TUPLE]) is -- load a_filename to last_text_file require a_open_procedure_not_void: a_open_procedure /= void text_opening_event_not_void: text_opening_event /= void local error_occured: BOOLEAN do last_text_file := Void if not error_occured then text_opening_event.subscribe_kamikaze (a_open_procedure) if file_system.file_exists (current_filename) then if file_system.is_file_readable (current_filename) then text_opening_event.publish ([]) else Error_handler.raise_error (Error_handler.em_error_plain_text_file_not_readable, [get_filepath (current_filename)]) end else Error_handler.raise_error (Error_handler.em_error_plain_text_file_does_not_exist, [get_filepath (current_filename)]) end end rescue has_error := true if ignore_missing_file then error_occured := true retry end end impl_read is -- implementaiton of open_read do create last_text_file.make_open_read (create {STRING}.make_from_string(current_filename)) end impl_read_write is -- implementation of open_read_write do create last_text_file.make_open_read_write (create {STRING}.make_from_string(current_filename)) end impl_read_append is -- implementation of open_read_append do create last_text_file.make_open_read_append (create {STRING}.make_from_string(current_filename)) end impl_append is -- implementation of open_append do create last_text_file.make_open_append (create {STRING}.make_from_string(current_filename)) end impl_write is -- implementation of open_write do create last_text_file.make_open_write (create {STRING}.make_from_string(current_filename)) end current_filename: STRING -- name of current file text_opening_event : EM_EVENT_CHANNEL [TUPLE[]] -- how to open a text_file invariant text_opening_event_not_void: text_opening_event /= void current_filename_is_not_void: current_filename /= void end