4 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <unicode/uchar.h>
24 #include <unicode/ustring.h>
25 #include <unicode/ucol.h>
29 #include "media-util-err.h"
30 #include "media-svc-debug.h"
31 #include "media-svc-localize-utils.h"
33 #define SAFE_STRLEN(src) ((src) ? strlen(src) : 0)
35 int _media_svc_check_utf8(char c)
37 if ((c & 0xff) < (128 & 0xff))
39 else if ((c & (char)0xe0) == (char)0xc0)
41 else if ((c & (char)0xf0) == (char)0xe0)
43 else if ((c & (char)0xf8) == (char)0xf0)
45 else if ((c & (char)0xfc) == (char)0xf8)
47 else if ((c & (char)0xfe) == (char)0xfc)
50 return MS_MEDIA_ERR_INVALID_PARAMETER;
53 int SAFE_SNPRINTF(char **buf, int *buf_size, int len, const char *src)
61 remain = *buf_size - len;
62 if (remain > (int)strlen(src) + 1) {
63 temp_len = snprintf((*buf) + len, remain, "%s", src);
68 temp = realloc(*buf, *buf_size * 2);
72 *buf_size = *buf_size * 2;
73 remain = *buf_size - len;
74 if (remain > (int)strlen(src) + 1)
77 temp_len = snprintf((*buf) + len, remain, "%s", src);
82 static int __media_svc_remove_special_char(const char *src, char *dest, int dest_size)
84 int s_pos = 0, d_pos = 0, char_type, src_size;
87 media_svc_error("The parameter(src) is NULL");
91 src_size = strlen(src);
93 while (src[s_pos] != 0) {
94 char_type = _media_svc_check_utf8(src[s_pos]);
96 if (0 < char_type && char_type < dest_size - d_pos && char_type <= src_size - s_pos) {
97 memcpy(dest + d_pos, src + s_pos, char_type);
101 media_svc_error("The parameter(src:%s) has invalid character set", src);
103 return MS_MEDIA_ERR_INVALID_PARAMETER;
111 static inline int __media_svc_collation_str(const char *src, char **dest)
114 UErrorCode status = U_ZERO_ERROR;
115 UChar *tmp_result = NULL;
117 char region[50] = {0};
119 const char *en_us = "en_US.UTF-8";
121 lang = vconf_get_str(VCONFKEY_LANGSET);
123 if (strlen(lang) < 50) {
124 strncpy(region, lang, strlen(lang));
127 media_svc_error("Lang size error(%s)", lang);
131 strncpy(region, en_us, strlen(en_us));
134 char *dot = strchr(region, '.');
138 collator = ucol_open(region, &status);
140 media_svc_retvm_if(U_FAILURE(status), MS_MEDIA_ERR_INTERNAL, "ucol_open() Failed(%s)", u_errorName(status));
142 u_strFromUTF8(NULL, 0, &size, src, strlen(src), &status);
143 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
144 media_svc_error("u_strFromUTF8 to get the dest length Failed(%s)", u_errorName(status));
145 ucol_close(collator);
146 return MS_MEDIA_ERR_INTERNAL;
148 status = U_ZERO_ERROR;
149 tmp_result = calloc(1, sizeof(UChar) * (size + 1));
150 u_strFromUTF8(tmp_result, size + 1, NULL, src, -1, &status);
151 if (U_FAILURE(status)) {
152 media_svc_error("u_strFromUTF8 Failed(%s)", u_errorName(status));
154 ucol_close(collator);
155 return MS_MEDIA_ERR_INTERNAL;
158 size = ucol_getSortKey(collator, tmp_result, -1, NULL, 0);
159 *dest = calloc(1, sizeof(uint8_t) * (size + 1));
160 size = ucol_getSortKey(collator, tmp_result, -1, (uint8_t *) * dest, size + 1);
162 ucol_close(collator);
164 return MS_MEDIA_ERR_NONE;
167 int _media_svc_collation_str(const char *src, char **dest)
170 char temp[SAFE_STRLEN(src) + 1];
172 ret = __media_svc_remove_special_char(src, temp, sizeof(temp));
173 media_svc_retvm_if(ret < MS_MEDIA_ERR_NONE, ret, "__ctsvc_remove_special_char() Failed(%d)", ret);
175 return __media_svc_collation_str(temp, dest);