From 6711a414dc29a70178e5b96a81262cdd9aeff13d Mon Sep 17 00:00:00 2001 From: Andrii Kroitor Date: Mon, 17 Oct 2016 18:51:59 +0300 Subject: [PATCH] edje_edit: refactor eet file usage Summary: Move opening for read/write/read-write and error message to internal method Reuse eet file from Edje instead of opening it again in read-only mode Add wrapper for eet_close to skip closing if internal file was used Reviewers: NikaWhite Subscribers: cedric, jpeg, #eflete Differential Revision: https://phab.enlightenment.org/D4353 --- src/lib/edje/edje_edit.c | 211 +++++++++++++++++++++++------------------------ 1 file changed, 102 insertions(+), 109 deletions(-) diff --git a/src/lib/edje/edje_edit.c b/src/lib/edje/edje_edit.c index 3d4dbf3..fc33a2f 100644 --- a/src/lib/edje/edje_edit.c +++ b/src/lib/edje/edje_edit.c @@ -165,6 +165,35 @@ _edje_edit_program_script_free(Program_Script *ps) free(ps); } +static Eet_File * +_edje_edit_eet_open(Edje *ed, Eet_File_Mode mode) +{ + Eet_File *eetf; + + switch (mode) + { + case EET_FILE_MODE_INVALID: + return NULL; + case EET_FILE_MODE_READ: + return ed->file->ef; + case EET_FILE_MODE_WRITE: + case EET_FILE_MODE_READ_WRITE: + eetf = eet_open(ed->path, mode); + if (!eetf) + ERR("Unable to open \"%s\" for writing output", ed->path); + return eetf; + } + return NULL; +} + +static void +_edje_edit_eet_close(Eet_File *ef) +{ + Eet_File_Mode mode = eet_mode_get(ef); + if (mode != EET_FILE_MODE_READ) + eet_close(ef); +} + EOLIAN static Eina_Bool _edje_edit_efl_file_file_set(Eo *obj, Edje_Edit *eed, const char *file, const char *group) { @@ -206,9 +235,11 @@ _edje_edit_efl_file_file_set(Eo *obj, Edje_Edit *eed, const char *file, const ch return ret; } + GET_ED_OR_RETURN(EINA_FALSE); + eed->program_scripts = eina_hash_int32_new((Eina_Free_Cb)_edje_edit_program_script_free); - ef = eet_open(file, EET_FILE_MODE_READ); + ef = _edje_edit_eet_open(ed, EET_FILE_MODE_READ); efl_del(file_obj); snprintf(buf, sizeof(buf), "edje/scripts/embryo/source/%i", @@ -233,7 +264,7 @@ _edje_edit_efl_file_file_set(Eo *obj, Edje_Edit *eed, const char *file, const ch } free(keys); } - eet_close(ef); + _edje_edit_eet_close(ef); ret = EINA_TRUE; @@ -423,12 +454,9 @@ _edje_edit_file_import(Edje *ed, const char *path, const char *entry, int compre } /* Write file data to edje file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Unable to open \"%s\" for writing output", ed->path); - goto on_error; - } + goto on_error; if (eet_write(eetf, entry, fdata, fsize, compress) <= 0) { @@ -441,7 +469,7 @@ _edje_edit_file_import(Edje *ed, const char *path, const char *entry, int compre if (!_edje_edit_edje_file_save(eetf, ed->file)) goto on_error; - eet_close(eetf); + _edje_edit_eet_close(eetf); eina_file_map_free(f, fdata); eina_file_close(f); @@ -449,7 +477,7 @@ _edje_edit_file_import(Edje *ed, const char *path, const char *entry, int compre return EINA_TRUE; on_error: - if (eetf) eet_close(eetf); + if (eetf) _edje_edit_eet_close(eetf); eina_file_map_free(f, fdata); eina_file_close(f); @@ -494,10 +522,9 @@ _edje_import_image_file(Edje *ed, const char *path, int id) } /* open the eet file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) { - ERR("Unable to open \"%s\" for writing output", ed->path); evas_object_del(im); return EINA_FALSE; } @@ -513,7 +540,7 @@ _edje_import_image_file(Edje *ed, const char *path, int id) { ERR("Unable to write image part \"%s\" part entry to %s", entry, ed->path); - eet_close(eetf); + _edje_edit_eet_close(eetf); evas_object_del(im); return EINA_FALSE; } @@ -524,11 +551,11 @@ _edje_import_image_file(Edje *ed, const char *path, int id) if (!_edje_edit_edje_file_save(eetf, ed->file)) { eet_delete(eetf, entry); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_TRUE; } @@ -1243,19 +1270,16 @@ edje_edit_sound_sample_del(Evas_Object *obj, const char *name) Eet_File *eetf; Edje_Sound_Sample *sound_sample_last; - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - WRN("Unable to open \"%s\" for writing output", ed->path); - return EINA_FALSE; - } + return EINA_FALSE; snprintf(sample, sizeof(sample), "edje/sounds/%i", sound_sample->id); if (eet_delete(eetf, sample) <= 0) { WRN("Unable to delete \"%s\" sound", sample); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } @@ -1277,16 +1301,16 @@ edje_edit_sound_sample_del(Evas_Object *obj, const char *name) if (!_delete_play_actions(obj, name, EDJE_ACTION_TYPE_SOUND_SAMPLE, eetf)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } if (!_edje_edit_edje_file_save(eetf, ed->file)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); } GET_EED_OR_RETURN(EINA_FALSE); @@ -1365,12 +1389,9 @@ edje_edit_sound_tone_del(Evas_Object *obj, const char *name) { Eet_File *eetf; - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - WRN("Unable to open file \"%s\" for writing output", ed->path); - return EINA_FALSE; - } + return EINA_FALSE; _edje_if_string_free(ed, &sound_tone->name); --ed->file->sound_dir->tones_count; @@ -1390,16 +1411,16 @@ edje_edit_sound_tone_del(Evas_Object *obj, const char *name) if (!_delete_play_actions(obj, name, EDJE_ACTION_TYPE_SOUND_TONE, eetf)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } if (!_edje_edit_edje_file_save(eetf, ed->file)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); } GET_EED_OR_RETURN(EINA_FALSE); @@ -1562,21 +1583,18 @@ edje_edit_sound_samplebuffer_get(Evas_Object *obj, const char *sample_name) sample = &ed->file->sound_dir->samples[i]; if (!strcmp(sample->name, sample_name)) { - ef = eet_mmap(ed->file->f); + ef = _edje_edit_eet_open(ed, EET_FILE_MODE_READ); if (!ef) - { - ERR("Cannot open edje file '%s' for samples", ed->path); - return NULL; - } + return NULL; snprintf(snd_id_str, sizeof(snd_id_str), "edje/sounds/%i", sample->id); data = eet_read_direct(ef, snd_id_str, &len); if (len <= 0) { ERR("Sample from edj file '%s' has 0 length", ed->path); - eet_close(ef); + _edje_edit_eet_close(ef); return NULL; } - eet_close(ef); + _edje_edit_eet_close(ef); return eina_binbuf_manage_new(data, len, EINA_TRUE); } } @@ -1684,18 +1702,15 @@ edje_edit_group_copy(Evas_Object *obj, const char *group_name, const char *copy_ if (eina_hash_find(ed->file->collection, copy_name)) return EINA_FALSE; - eetf = eet_open(ed->file->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Edje_Edit: Error. unable to open \"%s\" " - "for writing output", ed->file->path); - return EINA_FALSE; - } + return EINA_FALSE; + snprintf(buf, sizeof(buf), "edje/collections/%d", e->id); epc = eet_data_read(eetf, _edje_edd_edje_part_collection, buf); if (!epc) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } @@ -1728,7 +1743,7 @@ edje_edit_group_copy(Evas_Object *obj, const char *group_name, const char *copy_ de = _alloc(sizeof(Edje_Part_Collection_Directory_Entry)); if (!de) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } @@ -1778,7 +1793,7 @@ edje_edit_group_copy(Evas_Object *obj, const char *group_name, const char *copy_ save_status = _edje_edit_edje_file_save(eetf, ed->file); _edje_collection_free(ed->file, epc, de); - eet_close(eetf); + _edje_edit_eet_close(eetf); return save_status; } @@ -1897,13 +1912,10 @@ edje_edit_group_del(Evas_Object *obj, const char *group_name) } /* Remove collection/id from eet file */ - eetf = eet_open(ed->file->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Edje_Edit: Error. unable to open \"%s\" " - "for writing output", ed->file->path); - return EINA_FALSE; - } + return EINA_FALSE; + snprintf(buf, sizeof(buf), "edje/collections/%d", e->id); eet_delete(eetf, buf); snprintf(buf, sizeof(buf), "edje/scripts/embryo/compiled/%d", e->id); @@ -1922,7 +1934,7 @@ edje_edit_group_del(Evas_Object *obj, const char *group_name) while (count); free(keys); } - eet_close(eetf); + _edje_edit_eet_close(eetf); l = NULL; g = NULL; /* Free Group and all it's Aliases */ @@ -8639,29 +8651,26 @@ edje_edit_font_del(Evas_Object *obj, const char *alias) Eet_File *eetf; /* open the eet file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Unable to open \"%s\" for writing output", ed->path); - return EINA_FALSE; - } + return EINA_FALSE; snprintf(entry, sizeof(entry), "edje/fonts/%s", alias); if (eet_delete(eetf, entry) <= 0) { ERR("Unable to delete \"%s\" font entry", entry); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } /* write the edje_file */ if (!_edje_edit_edje_file_save(eetf, ed->file)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); } eina_hash_del(ed->file->fonts, alias, fnt); @@ -9292,12 +9301,9 @@ edje_edit_image_set_del(Evas_Object *obj, const char *name) --ed->file->image_dir->sets_count; /* open the eet file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Unable to open \"%s\" for writing output", ed->path); - return EINA_FALSE; - } + return EINA_FALSE; if (de_last->id != de->id) { @@ -9335,7 +9341,7 @@ edje_edit_image_set_del(Evas_Object *obj, const char *name) } if (!_edje_edit_collection_save(eetf, pce->ref)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } } @@ -9345,7 +9351,7 @@ edje_edit_image_set_del(Evas_Object *obj, const char *name) sizeof(Edje_Image_Directory_Set_Entry) * ed->file->image_dir->sets_count); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_TRUE; } @@ -9720,10 +9726,9 @@ edje_edit_image_replace(Evas_Object *obj, const char *name, const char *new_name it = eina_hash_iterator_data_new(ed->file->collection); /* open the eet file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) { - ERR("Unable to open \"%s\" for writing output", ed->path); eina_iterator_free(it); return EINA_FALSE; } @@ -9753,7 +9758,7 @@ edje_edit_image_replace(Evas_Object *obj, const char *name, const char *new_name } if (!_edje_edit_collection_save(eetf, pce->ref)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); eina_iterator_free(it); return EINA_FALSE; } @@ -9761,7 +9766,7 @@ edje_edit_image_replace(Evas_Object *obj, const char *name, const char *new_name } eina_iterator_free(it); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_TRUE; } @@ -9958,19 +9963,16 @@ edje_edit_image_del(Evas_Object *obj, const char *name) int size = 0; /* open the eet file */ - eetf = eet_open(ed->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Unable to open \"%s\" for writing output", ed->path); - return EINA_FALSE; - } + return EINA_FALSE; snprintf(entry, sizeof(entry), "edje/images/%i", de->id); if (eet_delete(eetf, entry) <= 0) { ERR("Unable to delete \"%s\" font entry", entry); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } if (de_last->id != de->id) @@ -10020,7 +10022,7 @@ edje_edit_image_del(Evas_Object *obj, const char *name) } if (!_edje_edit_collection_save(eetf, pce->ref)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } } @@ -10032,11 +10034,11 @@ edje_edit_image_del(Evas_Object *obj, const char *name) /* write the edje_file */ if (!_edje_edit_edje_file_save(eetf, ed->file)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); } _edje_edit_flag_script_dirty(eed, EINA_TRUE); @@ -15533,7 +15535,7 @@ _edje_generate_source_of_group(Edje *ed, Edje_Part_Collection_Directory_Entry *p obj = edje_edit_object_add(ed->base->evas); if (!edje_object_file_set(obj, ed->file->path, group)) return EINA_FALSE; - ef = eet_open(ed->file->path, EET_FILE_MODE_READ); + ef = _edje_edit_eet_open(ed, EET_FILE_MODE_READ); if (!ef) { evas_object_del(obj); @@ -15654,7 +15656,7 @@ _edje_generate_source_of_group(Edje *ed, Edje_Part_Collection_Directory_Entry *p return EINA_FALSE; } - eet_close(ef); + _edje_edit_eet_close(ef); evas_object_del(obj); return ret; } @@ -16053,13 +16055,9 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ INF("** path: %s", ef->path); /* Open the eet file */ - eetf = eet_open(ef->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) - { - ERR("Error. unable to open \"%s\" for writing output", - ef->path); - return EINA_FALSE; - } + return EINA_FALSE; /* Set compiler name */ if (strcmp(ef->compiler, "edje_edit")) @@ -16067,7 +16065,7 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ if (!_edje_edit_edje_file_save(eetf, ef)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } @@ -16079,7 +16077,7 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ "[id: %d]", ed->collection->id); if (!_edje_edit_collection_save(eetf, ed->collection)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } } @@ -16099,7 +16097,7 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ "[id: %d]", edc->id); if (!_edje_edit_collection_save(eetf, edc)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } } @@ -16113,7 +16111,7 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ "[id: %d]", ce->id); if (!_edje_edit_collection_save(eetf, ce->ref)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } } @@ -16175,11 +16173,11 @@ _edje_edit_internal_save(Evas_Object *obj, int current_only, Eina_Bool generate_ if (generate_source) if (!_edje_edit_source_save(eetf, obj)) { - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } - eet_close(eetf); + _edje_edit_eet_close(eetf); /* Update mtime */ { @@ -16208,26 +16206,22 @@ edje_edit_clean_save_as(Evas_Object *obj, const char *new_file_name) new_file_name); return EINA_FALSE; } - ef = eet_open(ed->file->path, EET_FILE_MODE_READ); + ef = _edje_edit_eet_open(ed, EET_FILE_MODE_READ); if (!ef) - { - ERR("Error. unable to open \"%s\" for reading", - ed->file->path); - return EINA_FALSE; - } + return EINA_FALSE; ef_out = eet_open(new_file_name, EET_FILE_MODE_WRITE); if (!ef_out) { ERR("Error. unable to open \"%s\" for writing output", new_file_name); - eet_close(ef); + _edje_edit_eet_close(ef); return EINA_FALSE; } /* copying file structure */ if (!_edje_edit_edje_file_save(ef_out, ed->file)) { - eet_close(ef); + _edje_edit_eet_close(ef); eet_close(ef_out); return EINA_FALSE; } @@ -16277,7 +16271,7 @@ edje_edit_clean_save_as(Evas_Object *obj, const char *new_file_name) /* generating source code */ _edje_edit_source_save(ef_out, obj); - eet_close(ef); + _edje_edit_eet_close(ef); eet_close(ef_out); return EINA_TRUE; @@ -16315,10 +16309,9 @@ edje_edit_without_source_save(Evas_Object *obj, Eina_Bool current_group) return EINA_FALSE; } sfl->list = NULL; - eetf = eet_open(ed->file->path, EET_FILE_MODE_READ_WRITE); + eetf = _edje_edit_eet_open(ed, EET_FILE_MODE_READ_WRITE); if (!eetf) { - ERR("Error. Unable to open \"%s\" for cleaning source", ed->file->path); free(sfl); return EINA_FALSE; } @@ -16327,12 +16320,12 @@ edje_edit_without_source_save(Evas_Object *obj, Eina_Bool current_group) { ERR("Unable to clean edc source from edj file"); free(sfl); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_FALSE; } free(sfl); - eet_close(eetf); + _edje_edit_eet_close(eetf); return EINA_TRUE; } -- 2.7.4