From ba9fbfe42b8511db44ef3a341f96a8a9dd614b32 Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Wed, 29 Jun 2022 15:20:04 +0900 Subject: [PATCH] Reorder precondition checking code - Issue: In the ttsd_engine_agent.c file, the precondition checking code of each function has no constant rule for coding. This can decrease the readability of the code. - Solution: To improve the readbility of the code, this patch reorders the precondition checking code of each function by constant way. According to this patch, each function first checks the validation of parameters and then checks initialization of the module and engine. Change-Id: If9498fd406ca29bf693493f959b521bf02b9604b Signed-off-by: Suyeon Hwang --- server/ttsd_engine_agent.c | 72 ++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/server/ttsd_engine_agent.c b/server/ttsd_engine_agent.c index c59a261..799a65b 100644 --- a/server/ttsd_engine_agent.c +++ b/server/ttsd_engine_agent.c @@ -303,11 +303,6 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback) { SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent DEBUG] load current engine START"); - if (false == __is_agent_initialized()) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized"); - return TTSD_ERROR_INVALID_STATE; - } - if (NULL == callback) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine"); return TTSD_ERROR_ENGINE_NOT_FOUND; @@ -324,6 +319,11 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback) return TTSD_ERROR_ENGINE_NOT_FOUND; } + if (false == __is_agent_initialized()) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized"); + return TTSD_ERROR_INVALID_STATE; + } + /* check whether current engine is loaded or not */ if (__is_engine_loaded()) { SLOG(LOG_INFO, tts_tag(), "[Engine Agent] Engine has already been loaded "); @@ -766,17 +766,17 @@ int ttsd_engine_agent_is_credential_needed(unsigned int uid, bool* credential_ne int ttsd_engine_load_voice(const char* lang, const int vctype) { + if (NULL == lang) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No language parameter"); + return TTSD_ERROR_INVALID_PARAMETER; + } + int ret = __check_engine_initialized_and_loaded(); if (TTSD_ERROR_NONE != ret) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); return ret; } - if (NULL == lang) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No language parameter"); - return TTSD_ERROR_INVALID_PARAMETER; - } - /* 1. Find voice info */ ttsvoice_s* data = __get_ttsvoice(lang, vctype); if (NULL == data) { @@ -815,17 +815,17 @@ int ttsd_engine_load_voice(const char* lang, const int vctype) int ttsd_engine_unload_voice(const char* lang, const int vctype) { + if (NULL == lang) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No language parameter"); + return TTSD_ERROR_INVALID_PARAMETER; + } + int ret = __check_engine_initialized_and_loaded(); if (TTSD_ERROR_NONE != ret) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); return ret; } - if (NULL == lang) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No language parameter"); - return TTSD_ERROR_INVALID_PARAMETER; - } - /* 1. Find voice info */ ttsvoice_s* data = __get_ttsvoice(lang, vctype); if (NULL == data) { @@ -980,17 +980,17 @@ int ttsd_engine_get_voice_list(GList** voice_list) int ttsd_engine_get_default_voice(char** lang, int* vctype) { + if (NULL == lang || NULL == vctype) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] BAD Parameter"); + return TTSD_ERROR_INVALID_PARAMETER; + } + int ret = __check_engine_initialized_and_loaded(); if (TTSD_ERROR_NONE != ret) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); return ret; } - if (NULL == lang || NULL == vctype) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] BAD Parameter"); - return TTSD_ERROR_INVALID_PARAMETER; - } - if (NULL != g_engine_info->default_lang) { *lang = strdup(g_engine_info->default_lang); *vctype = g_engine_info->default_vctype; @@ -1006,17 +1006,17 @@ int ttsd_engine_get_default_voice(char** lang, int* vctype) int ttsd_engine_set_private_data(const char* key, const char* data) { + if (NULL == key) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid parameter"); + return TTSD_ERROR_INVALID_PARAMETER; + } + int ret = __check_engine_initialized_and_loaded(); if (TTSD_ERROR_NONE != ret) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); return ret; } - if (NULL == key) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid parameter"); - return TTSD_ERROR_INVALID_PARAMETER; - } - if (NULL == g_engine_info->callbacks->private_data_set) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not supported feature"); return TTSD_ERROR_NOT_SUPPORTED_FEATURE; @@ -1032,17 +1032,16 @@ int ttsd_engine_set_private_data(const char* key, const char* data) int ttsd_engine_get_private_data(const char* key, char** data) { - int ret = __check_engine_initialized_and_loaded(); - if (TTSD_ERROR_NONE != ret) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); - return ret; - } - if (NULL == key || NULL == data) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid parameter"); return TTSD_ERROR_INVALID_PARAMETER; } + int ret = __check_engine_initialized_and_loaded(); + if (TTSD_ERROR_NONE != ret) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); + return ret; + } if (NULL == g_engine_info->callbacks->private_data_requested) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not supported feature"); @@ -1065,17 +1064,16 @@ int ttsd_engine_get_private_data(const char* key, char** data) int ttsd_engine_check_app_agreed(const char* appid, bool* is_agreed) { - int ret = __check_engine_initialized_and_loaded(); - if (TTSD_ERROR_NONE != ret) { - SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); - return ret; - } - if (NULL == appid || NULL == is_agreed) { SLOG(LOG_WARN, tts_tag(), "[Engine Agent WARNING] Invalid parameter, appid is NULL"); return TTSD_ERROR_NONE; } + int ret = __check_engine_initialized_and_loaded(); + if (TTSD_ERROR_NONE != ret) { + SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret)); + return ret; + } if (NULL == g_engine_info->callbacks->check_app_agreed) { SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not supported feature"); -- 2.7.4