Extract to get base node from config 30/277430/1
authorwn.jang <wn.jang@samsung.com>
Thu, 7 Jul 2022 01:56:49 +0000 (10:56 +0900)
committerwn.jang <wn.jang@samsung.com>
Thu, 7 Jul 2022 01:57:30 +0000 (10:57 +0900)
Change-Id: Icd8c0c2270f7aa3ce1dbdd05606c50078177a31f

common/stt_config_parser.c

index 3459ff3..8f53330 100644 (file)
 
 static xmlDocPtr g_config_doc = NULL;
 
-int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info)
+static int __stt_parser_get_base_node_from_config(xmlDocPtr config_doc, xmlNodePtr* node_ptr, const char* base_tag)
 {
-       if (NULL == path || NULL == engine_info) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
-               return -1;
-       }
-
-       xmlDocPtr doc = NULL;
-       xmlNodePtr cur = NULL;
-       xmlChar *key;
-
-       doc = xmlParseFile(path);
-       if (doc == NULL) {
-               return -1;
-       }
-
-       cur = xmlDocGetRootElement(doc);
+       xmlNodePtr cur = xmlDocGetRootElement(config_doc);
        if (cur == NULL) {
                //LCOV_EXCL_START
                SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               xmlFreeDoc(doc);
                return -1;
                //LCOV_EXCL_STOP
        }
 
-       if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_BASE_TAG)) {
+       if (xmlStrcmp(cur->name, (const xmlChar *)base_tag)) {
                //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT 'stt-engine'");
-               xmlFreeDoc(doc);
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", base_tag);
                return -1;
                //LCOV_EXCL_STOP
        }
@@ -88,10 +72,34 @@ int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info
        if (cur == NULL) {
                //LCOV_EXCL_START
                SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               xmlFreeDoc(doc);
                return -1;
                //LCOV_EXCL_STOP
        }
+       *node_ptr = cur;
+       return 0;
+}
+
+int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info)
+{
+       if (NULL == path || NULL == engine_info) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
+               return -1;
+       }
+
+       xmlDocPtr doc = NULL;
+       xmlNodePtr cur = NULL;
+       xmlChar *key;
+
+       doc = xmlParseFile(path);
+       if (doc == NULL) {
+               return -1;
+       }
+
+       if (0 != __stt_parser_get_base_node_from_config(doc, &cur, STT_TAG_ENGINE_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
+               xmlFreeDoc(doc);
+               return -1;
+       }
 
        /* alloc engine info */
        stt_engine_info_s* temp;
@@ -366,22 +374,8 @@ int stt_parser_load_config(stt_config_s** config_info)
                }
        }
 
-       cur = xmlDocGetRootElement(doc);
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document"); //LCOV_EXCL_LINE
-               xmlFreeDoc(doc);
-               return -1;
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG); //LCOV_EXCL_LINE
-               xmlFreeDoc(doc);
-               return -1;
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document"); //LCOV_EXCL_LINE
+       if (0 != __stt_parser_get_base_node_from_config(doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                xmlFreeDoc(doc);
                return -1;
        }
@@ -550,20 +544,8 @@ int stt_parser_set_engine(const char* engine_id, const char* setting, const char
                return -1;
 
        xmlNodePtr cur = NULL;
-       cur = xmlDocGetRootElement(g_config_doc);
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               return -1;
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
-               return -1;
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
+       if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                return -1;
        }
 
@@ -609,20 +591,8 @@ int stt_parser_set_language(const char* language)
                return -1;
 
        xmlNodePtr cur = NULL;
-       cur = xmlDocGetRootElement(g_config_doc);
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               return -1;
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
-               return -1;
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
+       if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                return -1;
        }
 
@@ -648,20 +618,8 @@ int stt_parser_set_auto_lang(bool value)
                return -1;
 
        xmlNodePtr cur = NULL;
-       cur = xmlDocGetRootElement(g_config_doc);
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               return -1;
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
-               return -1;
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
+       if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                return -1;
        }
 
@@ -692,20 +650,8 @@ int stt_parser_set_silence_detection(bool value)
                return -1;
 
        xmlNodePtr cur = NULL;
-       cur = xmlDocGetRootElement(g_config_doc);
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               return -1;
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
-               return -1;
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
+       if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                return -1;
        }
 
@@ -1023,30 +969,9 @@ int stt_parser_get_time_info(GSList** time_list)
                return -1;
        }
 
-       cur = xmlDocGetRootElement(doc);
-       if (cur == NULL) {
-               //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               xmlFreeDoc(doc);
+       if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_TIME_BASE_TAG)) {
+               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
                return -1;
-               //LCOV_EXCL_STOP
-       }
-
-       if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_BASE_TAG)) {
-               //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT '%s'", STT_TAG_TIME_BASE_TAG);
-               xmlFreeDoc(doc);
-               return -1;
-               //LCOV_EXCL_STOP
-       }
-
-       cur = cur->xmlChildrenNode;
-       if (cur == NULL) {
-               //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
-               xmlFreeDoc(doc);
-               return -1;
-               //LCOV_EXCL_STOP
        }
 
        /* alloc time info */