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__
30 short __WmLngSwapShort(short aShort)
32 return ((aShort << 8) + (aShort >> 8));
36 short *mmfile_swap_2byte_string(short *mszOutput, short *mszInput, int length)
40 for (i = 0; i < length; i++) {
44 mszOutput[i] = __WmLngSwapShort(mszInput[i]);
54 char *mmfile_string_convert_debug(const char *str, unsigned int len,
55 const char *to_codeset, const char *from_codeset,
61 char *tmp = g_convert(str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);
64 debug_msg(RELEASE, "## DEBUG ## %p = g_convert (%p, %u, %p, %p, %p ,%p, %p, %u) by %s() %d\n",
65 tmp, str, len, to_codeset, from_codeset, bytes_read, bytes_written, func, line);
73 char *mmfile_strdup_debug(const char *str, const char *func, unsigned int line)
83 debug_msg(RELEASE, "## DEBUG ## %p = strdup (%p) by %s() %d\n", temp, str, func, line);
90 #else /* __MMFILE_MEM_TRACE__ */
93 char *mmfile_get_charset(const char *str)
95 UCharsetDetector *ucsd = NULL;
96 const UCharsetMatch *ucm = NULL;
97 UErrorCode status = U_ZERO_ERROR;
99 const char *charset = NULL;
100 char *ret_charset = NULL;
102 ucsd = ucsdet_open(&status);
103 if (U_FAILURE(status)) {
104 debug_error(DEBUG, "fail to ucsdet_open\n");
108 ucsdet_enableInputFilter(ucsd, TRUE);
110 ucsdet_setText(ucsd, str, strlen(str), &status);
111 if (U_FAILURE(status)) {
112 debug_error(DEBUG, "fail to ucsdet_setText\n");
116 ucm = ucsdet_detect(ucsd, &status);
117 if (U_FAILURE(status)) {
118 debug_error(DEBUG, "fail to ucsdet_detect\n");
123 debug_error(DEBUG, "fail to ucsdet_detect\n");
127 charset = ucsdet_getName(ucm, &status);
128 if (U_FAILURE(status)) {
129 debug_error(DEBUG, "fail to ucsdet_getName\n");
137 ret_charset = strdup(charset);
145 char *mmfile_string_convert(const char *str, unsigned int len,
146 const char *to_codeset, const char *from_codeset,
148 unsigned int *bytes_written)
153 gsize written_len = 0;
156 result = g_convert(str, len, to_codeset, from_codeset, bytes_read, &written_len, &err);
158 /*if converting failed, return duplicated source string.*/
159 if (result == NULL) {
160 debug_warning(RELEASE, "text encoding failed.[%s][%d]\n", str, len);
163 debug_warning(DEBUG, "Error msg [%s]", err->message);
169 /* check carrige return */
171 for (i = 0; i < written_len; i++) {
172 if (result[i] == 13) {
173 if (result[i + 1] != 10)
182 if (bytes_written != NULL) {
183 *bytes_written = written_len;
190 char *mmfile_strdup(const char *str)
198 #endif /*__MMFILE_MEM_TRACE__*/
201 int mmfile_util_wstrlen(unsigned short *wText)
206 debug_error(DEBUG, "wText is NULL\n");
207 return MMFILE_UTIL_FAIL;
212 while (*(wText + n) != 0) {
220 char **mmfile_strsplit(const char *string, const char *delimiter)
222 return g_strsplit(string, delimiter, -1);
226 void mmfile_strfreev(char **str_array)
228 g_strfreev(str_array);