Add SAFE_SNPRINTF header & function for tw 18/41618/2 accepted/tizen/mobile/20150617.233321 accepted/tizen/tv/20150617.233333 accepted/tizen/wearable/20150617.233351 submit/tizen/20150617.110542
authorMinje Ahn <minje.ahn@samsung.com>
Wed, 17 Jun 2015 02:08:11 +0000 (11:08 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Wed, 17 Jun 2015 02:15:28 +0000 (11:15 +0900)
Change-Id: I927c818a96c8bb2001f77d591f88f9c06adf1b20
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-localize-utils.c
src/common/media-svc-localize_ch.c
src/include/common/media-svc-localize-utils.h

index e09470d..46fc99c 100755 (executable)
 #include <string.h>
 #include <stdlib.h>
 
+#include <unicode/uchar.h>
+#include <unicode/ustring.h>
+#include <unicode/ucol.h>
+
 #include "media-util-err.h"
 #include "media-svc-debug.h"
 #include "media-svc-localize-utils.h"
@@ -73,3 +77,101 @@ int SAFE_SNPRINTF(char **buf, int *buf_size, int len, const char *src)
                return temp_len;
        }
 }
+
+static int __media_svc_remove_special_char(const char *src, char *dest, int dest_size)
+{
+       int s_pos=0, d_pos=0, char_type, src_size;
+
+       if (NULL == src) {
+               media_svc_error("The parameter(src) is NULL");
+               dest[d_pos] = '\0';
+               return 0;
+       }
+       src_size = strlen(src);
+
+       while (src[s_pos] != 0) {
+               char_type = _media_svc_check_utf8(src[s_pos]);
+
+               if (0 < char_type && char_type < dest_size - d_pos && char_type <= src_size - s_pos) {
+                       memcpy(dest+d_pos, src+s_pos, char_type);
+                       d_pos += char_type;
+                       s_pos += char_type;
+               }
+               else {
+                       media_svc_error("The parameter(src:%s) has invalid character set", src);
+                       dest[d_pos] = '\0';
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
+       }
+
+       dest[d_pos] = '\0';
+       return d_pos;
+}
+
+static inline int __media_svc_collation_str(const char *src, char **dest)
+{
+       int32_t size = 0;
+       UErrorCode status = U_ZERO_ERROR;
+       UChar *tmp_result = NULL;
+       UCollator *collator;
+       char region[50] = {0};
+       char *lang = NULL;
+
+       //lang = vconf_get_str(VCONFKEY_LANGSET);
+       if(lang != NULL) {
+               if(strlen(lang) < 50) {
+                       strncpy (region, lang, strlen(lang));
+                       free(lang);
+               } else {
+                       media_svc_error("Lang size error(%s)", lang);
+                       free(lang);
+               }
+       } else {
+               strncpy(region, "en_US.UTF-8", strlen("en_US.UTF-8"));
+       }
+
+       char *dot = strchr(region, '.');
+       if (dot)
+               *dot = '\0';
+
+       collator = ucol_open(region, &status);
+
+       media_svc_retvm_if(U_FAILURE(status), MS_MEDIA_ERR_INTERNAL,
+                       "ucol_open() Failed(%s)", u_errorName(status));
+
+       u_strFromUTF8(NULL, 0, &size, src, strlen(src), &status);
+       if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
+               media_svc_error("u_strFromUTF8 to get the dest length Failed(%s)", u_errorName(status));
+               ucol_close(collator);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+       status = U_ZERO_ERROR;
+       tmp_result = calloc(1, sizeof(UChar) * (size + 1));
+       u_strFromUTF8(tmp_result, size + 1, NULL, src, -1, &status);
+       if (U_FAILURE(status)){
+               media_svc_error("u_strFromUTF8 Failed(%s)", u_errorName(status));
+               free(tmp_result);
+               ucol_close(collator);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       size = ucol_getSortKey(collator, tmp_result, -1, NULL, 0);
+       *dest = calloc(1, sizeof(uint8_t) * (size + 1));
+       size = ucol_getSortKey(collator, tmp_result, -1, (uint8_t *)*dest, size + 1);
+
+       ucol_close(collator);
+       free(tmp_result);
+       return MS_MEDIA_ERR_NONE;
+}
+
+int _media_svc_collation_str(const char *src, char **dest)
+{
+       int ret;
+       char temp[SAFE_STRLEN(src) + 1];
+
+       ret = __media_svc_remove_special_char(src, temp, sizeof(temp));
+       media_svc_retvm_if(ret < MS_MEDIA_ERR_NONE, ret, "__ctsvc_remove_special_char() Failed(%d)", ret);
+
+       return __media_svc_collation_str(temp, dest);
+}
+
index 0c99181..c51e3d7 100755 (executable)
@@ -2226,6 +2226,7 @@ int _media_svc_convert_chinese_to_pinyin(const char *src, pinyin_name_s **name,
        for (count = 0; count < len; count++) {
                if (temp_result[count] == 0xFEFF ||temp_result[count] == 0xFFFE) {//  ||temp_result[count] == 0xFF) {
                        media_svc_error("FIND BOM, GO TO THE NEXT CHARACTER");
+                       pinyin_spell_count[count] = 1;
                        continue;
                }
 
@@ -2268,6 +2269,9 @@ int _media_svc_convert_chinese_to_pinyin(const char *src, pinyin_name_s **name,
        int pinyin_initial_len = sizeof(char) * (len * 5 + 1);
        for(i=0; i < count ; i++) {
                for(j=0;j<total_count;j++) {
+                       if (pinyin_spell_count[i] == 0)
+                               continue;
+
                        int index = (j/repeat) %pinyin_spell_count[i];
 
                        if (i==0) {
index 1099655..30c45ab 100755 (executable)
@@ -25,4 +25,8 @@
 
 int _media_svc_check_utf8(char c);
 
+int SAFE_SNPRINTF(char **buf, int *buf_size, int len, const char *src);
+
+int _media_svc_collation_str(const char *src, char **dest);
+
 #endif // __MEDIA_SVC_LOCALIZE_UTILS_H__
\ No newline at end of file