Fix to get the maximum text size from the engine meta data
[platform/core/uifw/tts.git] / common / tts_config_parser.c
old mode 100755 (executable)
new mode 100644 (file)
index 17c4a27..135b392
@@ -29,6 +29,7 @@
 #define TTS_TAG_ENGINE_VOICE           "voice"
 #define TTS_TAG_ENGINE_VOICE_TYPE      "type"
 #define TTS_TAG_ENGINE_PITCH_SUPPORT   "pitch-support"
+#define TTS_TAG_ENGINE_TEXT_SIZE       "text-size"
 
 #define TTS_TAG_CONFIG_BASE_TAG                "tts-config"
 #define TTS_TAG_CONFIG_ENGINE_ID       "engine"
@@ -46,6 +47,9 @@
 extern char* tts_tag();
 
 static xmlDocPtr g_config_doc = NULL;
+char g_engine_id[128] = {0,};
+char g_setting[128] = {0,};
+char g_language[128] = {0,};
 
 int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info)
 {
@@ -94,6 +98,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        if (NULL == temp) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Out of memory");
                xmlFreeDoc(doc);
+               doc = NULL;
                return -1;
        }
 
@@ -102,32 +107,45 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        temp->voices = NULL;
        temp->setting = NULL;
        temp->pitch_support = false;
+       temp->text_size = 0;
 
        while (cur != NULL) {
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_NAME)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->name) free(temp->name);
+                               if (NULL != temp->name) {
+                                       free(temp->name);
+                                       temp->name = NULL;
+                               }
                                temp->name = strdup((char*)key);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_NAME);
                        }
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_ID)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->uuid) free(temp->uuid);
+                               if (NULL != temp->uuid) {
+                                       free(temp->uuid);
+                                       temp->uuid = NULL;
+                               }
                                temp->uuid = strdup((char*)key);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_ID);
                        }
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_SETTING)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->setting)      free(temp->setting);
+                               if (NULL != temp->setting) {
+                                       free(temp->setting);
+                                       temp->setting = NULL;
+                               }
                                temp->setting = strdup((char*)key);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_SETTING);
                        }
@@ -155,6 +173,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                                                        temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_USER_DEFINED;
                                                }
                                                xmlFree(attr);
+                                               attr = NULL;
                                        } else {
                                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE_TYPE);
                                                free(temp_voice);
@@ -164,9 +183,13 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
 
                                        key = xmlNodeGetContent(voice_node);
                                        if (NULL != key) {
-                                               if (NULL != temp_voice->language)       free(temp_voice->language);
+                                               if (NULL != temp_voice->language) {
+                                                       free(temp_voice->language);
+                                                       temp_voice->language = NULL;
+                                               }
                                                temp_voice->language = strdup((char*)key);
                                                xmlFree(key);
+                                               key = NULL;
                                                temp->voices = g_slist_append(temp->voices, temp_voice);
                                        } else {
                                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE);
@@ -189,6 +212,15 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_PITCH_SUPPORT);
                        }
+               } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_TEXT_SIZE)) {
+                       key = xmlNodeGetContent(cur);
+                       if (NULL != key) {
+                               temp->text_size = atoi((char*)key);
+                               xmlFree(key);
+                       } else {
+                               SLOG(LOG_INFO, tts_tag(), "[INFO] text size is unlimited.");
+                               temp->text_size = 0;
+                       }
                }
                cur = cur->next;
        }
@@ -215,26 +247,42 @@ int tts_parser_free_engine_info(tts_engine_info_s* engine_info)
                return -1;
        }
 
-       if (NULL != engine_info->name)          free(engine_info->name);
-       if (NULL != engine_info->uuid)          free(engine_info->uuid);
-       if (NULL != engine_info->setting)       free(engine_info->setting);
+       if (NULL != engine_info->name) {
+               free(engine_info->name);
+               engine_info->name = NULL;
+       }
+       if (NULL != engine_info->uuid) {
+               free(engine_info->uuid);
+               engine_info->uuid = NULL;
+       }
+       if (NULL != engine_info->setting) {
+               free(engine_info->setting);
+               engine_info->setting = NULL;
+       }
 
        tts_config_voice_s *temp_voice;
        temp_voice = g_slist_nth_data(engine_info->voices, 0);
 
        while (NULL != temp_voice) {
                if (NULL != temp_voice) {
-                       if (NULL != temp_voice->language)       free(temp_voice->language);
+                       if (NULL != temp_voice->language) {
+                               free(temp_voice->language);
+                               temp_voice->language = NULL;
+                       }
                        engine_info->voices = g_slist_remove(engine_info->voices, temp_voice);
                        free(temp_voice);
+                       temp_voice = NULL;
                }
 
                temp_voice = g_slist_nth_data(engine_info->voices, 0);
        }
 
-       if (NULL != engine_info)        free(engine_info);
+       if (NULL != engine_info) {
+               free(engine_info);
+               engine_info = NULL;
+       }
 
-       return 0;       
+       return 0;
 }
 
 int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
@@ -258,7 +306,7 @@ int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
                /* Get a first item */
                iter = g_slist_nth(engine_info->voices, 0);
 
-               int i = 1;      
+               int i = 1;
                while (NULL != iter) {
                        /*Get handle data from list*/
                        temp_voice = iter->data;
@@ -298,6 +346,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                doc = xmlParseFile(TTS_DEFAULT_CONFIG);
                if (doc == NULL) {
                        SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse file error : %s", TTS_DEFAULT_CONFIG);
+                       xmlCleanupParser();
                        return -1;
                }
                is_default_open = true;
@@ -314,6 +363,7 @@ int tts_parser_load_config(tts_config_s** config_info)
 
                        if (TTS_RETRY_COUNT == retry_count) {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse file error : %s", TTS_CONFIG);
+                               xmlCleanupParser();
                                return -1;
                        }
                }
@@ -324,6 +374,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
                doc = NULL;
+               xmlCleanupParser();
                return -1;
        }
 
@@ -331,6 +382,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] The wrong type, root node is NOT %s", TTS_TAG_CONFIG_BASE_TAG);
                xmlFreeDoc(doc);
                doc = NULL;
+               xmlCleanupParser();
                return -1;
        }
 
@@ -339,6 +391,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Empty document");
                xmlFreeDoc(doc);
                doc = NULL;
+               xmlCleanupParser();
                return -1;
        }
 
@@ -348,33 +401,38 @@ int tts_parser_load_config(tts_config_s** config_info)
        if (NULL == temp) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Out of memory");
                xmlFreeDoc(doc);
+               doc = NULL;
+               xmlCleanupParser();
                return -1;
        }
 
-       temp->engine_id = NULL;
-       temp->setting = NULL;
-       temp->language = NULL;
+       memset(g_engine_id, '\0', sizeof(g_engine_id));
+       memset(g_setting, '\0', sizeof(g_setting));
+       memset(g_language, '\0', sizeof(g_language));
+
+       temp->engine_id = g_engine_id;
+       temp->setting = g_setting;
+       temp->language = g_language;
 
        while (cur != NULL) {
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_ID)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->engine_id)    free(temp->engine_id);
-                               temp->engine_id = strdup((char*)key);
+                               strncpy(temp->engine_id, (char*)key, sizeof(g_engine_id) - 1);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] engine id is NULL");
                        }
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_SETTING)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->setting)      free(temp->setting);
-                               temp->setting = strdup((char*)key);
+                               strncpy(temp->setting, (char*)key, sizeof(g_setting) - 1);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] setting path is NULL");
                        }
-                       
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_AUTO_VOICE)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
@@ -388,6 +446,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                                }
 
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] voice type is NULL");
                        }
@@ -406,15 +465,16 @@ int tts_parser_load_config(tts_config_s** config_info)
                                }
 
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] voice type is NULL");
                        }
                } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_LANGUAGE)) {
                        key = xmlNodeGetContent(cur);
                        if (NULL != key) {
-                               if (NULL != temp->language)     free(temp->language);
-                               temp->language = strdup((char*)key);
+                               strncpy(temp->language, (char*)key, sizeof(g_language) - 1);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] engine uuid is NULL");
                        }
@@ -432,6 +492,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                        if (NULL != key) {
                                temp->pitch = atoi((char*)key);
                                xmlFree(key);
+                               key = NULL;
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Pitch is NULL");
                        }
@@ -473,22 +534,12 @@ int tts_parser_unload_config(tts_config_s* config_info)
                g_config_doc = NULL;
        }
        if (NULL != config_info) {
-               if (NULL != config_info->engine_id)     {
-                       free(config_info->engine_id);
-                       config_info->engine_id = NULL;
-               }
-               if (NULL != config_info->setting) {
-                       free(config_info->setting);
-                       config_info->setting = NULL;
-               }
-               if (NULL != config_info->language) {
-                       free(config_info->language);
-                       config_info->language = NULL;
-               }
                free(config_info);
                config_info = NULL;
        }
 
+       xmlCleanupParser();
+
        return 0;
 }
 
@@ -518,6 +569,7 @@ int tts_parser_copy_xml(const char* original, const char* destination)
        }
 
        xmlFreeDoc(doc);
+       doc = NULL;
        SLOG(LOG_DEBUG, tts_tag(), "[SUCCESS] Copying xml");
 
        return 0;
@@ -569,7 +621,7 @@ int tts_parser_set_engine(const char* engine_id, const char* setting, const char
                        default:                                xmlNodeSetContent(cur, (const xmlChar*)TTS_TAG_VOICE_TYPE_FEMALE);      break;
                        }
                }
-               
+
                cur = cur->next;
        }
 
@@ -826,12 +878,17 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old engine id(%s), New engine(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *engine)    free(*engine);
+                                                       if (NULL != *engine) {
+                                                               free(*engine);
+                                                               *engine = NULL;
+                                                       }
                                                        *engine = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -845,12 +902,17 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old engine setting(%s), New engine setting(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *setting)   free(*setting);
+                                                       if (NULL != *setting) {
+                                                               free(*setting);
+                                                               *setting = NULL;
+                                                       }
                                                        *setting = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -871,8 +933,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                        }
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -886,12 +950,17 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                if (0 != xmlStrcmp(key_old, key_new)) {
                                                        SLOG(LOG_DEBUG, tts_tag(), "Old language(%s), New language(%s)", 
                                                                (char*)key_old, (char*)key_new);
-                                                       if (NULL != *language)  free(*language);
+                                                       if (NULL != *language) {
+                                                               free(*language);
+                                                               *language = NULL;
+                                                       }
                                                        *language = strdup((char*)key_new);
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -916,8 +985,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                        }
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -934,8 +1005,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                        *speech_rate = atoi((char*)key_new);
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");
@@ -952,8 +1025,10 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
                                                        *pitch = atoi((char*)key_new);
                                                }
                                                xmlFree(key_new);
+                                               key_new = NULL;
                                        }
                                        xmlFree(key_old);
+                                       key_old = NULL;
                                }
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] old config and new config are different");