4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <unicode/ucsdet.h>
26 #include "mm_file_debug.h"
27 #include "mm_file_utils.h"
29 #ifdef __MMFILE_MEM_TRACE__
31 int mmfile_util_wstrlen(unsigned short *wText)
36 debug_error(DEBUG, "wText is NULL\n");
37 return MMFILE_UTIL_FAIL;
42 while (*(wText + n) != 0) {
49 short __WmLngSwapShort(short aShort)
51 return ((aShort << 8) + (aShort >> 8));
55 short *mmfile_swap_2byte_string(short *mszOutput, short *mszInput, int length)
59 for (i = 0; i < length; i++) {
63 mszOutput[i] = __WmLngSwapShort(mszInput[i]);
73 char *mmfile_string_convert_debug(const char *str, unsigned int len,
74 const char *to_codeset, const char *from_codeset,
80 char *tmp = g_convert(str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);
83 debug_msg(RELEASE, "## DEBUG ## %p = g_convert (%p, %u, %p, %p, %p ,%p, %p, %u) by %s() %d\n",
84 tmp, str, len, to_codeset, from_codeset, bytes_read, bytes_written, func, line);
92 char **mmfile_strsplit(const char *string, const char *delimiter)
94 return g_strsplit(string, delimiter, -1);
98 void mmfile_strfreev(char **str_array)
100 g_strfreev(str_array);
104 char *mmfile_strdup_debug(const char *str, const char *func, unsigned int line)
114 debug_msg(RELEASE, "## DEBUG ## %p = strdup (%p) by %s() %d\n", temp, str, func, line);
121 #else /* __MMFILE_MEM_TRACE__ */
124 int mmfile_util_wstrlen(unsigned short *wText)
129 debug_error(DEBUG, "wText is NULL\n");
130 return MMFILE_UTIL_FAIL;
135 while (*(wText + n) != 0) {
143 char *mmfile_get_charset(const char *str)
145 UCharsetDetector *ucsd = NULL;
146 const UCharsetMatch *ucm = NULL;
147 UErrorCode status = U_ZERO_ERROR;
149 const char *charset = NULL;
150 char *ret_charset = NULL;
152 ucsd = ucsdet_open(&status);
153 if (U_FAILURE(status)) {
154 debug_error(DEBUG, "fail to ucsdet_open\n");
158 ucsdet_enableInputFilter(ucsd, TRUE);
160 ucsdet_setText(ucsd, str, strlen(str), &status);
161 if (U_FAILURE(status)) {
162 debug_error(DEBUG, "fail to ucsdet_setText\n");
166 ucm = ucsdet_detect(ucsd, &status);
167 if (U_FAILURE(status)) {
168 debug_error(DEBUG, "fail to ucsdet_detect\n");
173 debug_error(DEBUG, "fail to ucsdet_detect\n");
177 charset = ucsdet_getName(ucm, &status);
178 if (U_FAILURE(status)) {
179 debug_error(DEBUG, "fail to ucsdet_getName\n");
187 ret_charset = strdup(charset);
195 char *mmfile_string_convert(const char *str, unsigned int len,
196 const char *to_codeset, const char *from_codeset,
198 unsigned int *bytes_written)
203 gsize written_len = 0;
206 result = g_convert(str, len, to_codeset, from_codeset, bytes_read, &written_len, &err);
208 /*if converting failed, return duplicated source string.*/
209 if (result == NULL) {
210 debug_warning(RELEASE, "text encoding failed.[%s][%d]\n", str, len);
213 debug_warning(DEBUG, "Error msg [%s]", err->message);
219 /* check carrige return */
221 for (i = 0; i < written_len; i++) {
222 if (result[i] == 13) {
223 if (result[i + 1] != 10)
232 if (bytes_written != NULL) {
233 *bytes_written = written_len;
240 char **mmfile_strsplit(const char *string, const char *delimiter)
242 return g_strsplit(string, delimiter, -1);
246 void mmfile_strfreev(char **str_array)
248 g_strfreev(str_array);
252 char *mmfile_strdup(const char *str)
260 #endif /*__MMFILE_MEM_TRACE__*/