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 char *mmfile_get_charset(const char *str)
31 UCharsetDetector *ucsd = NULL;
32 const UCharsetMatch *ucm = NULL;
33 UErrorCode status = U_ZERO_ERROR;
35 const char *charset = NULL;
36 char *ret_charset = NULL;
38 ucsd = ucsdet_open(&status);
39 if (U_FAILURE(status)) {
40 debug_error(DEBUG, "fail to ucsdet_open\n");
44 ucsdet_enableInputFilter(ucsd, true);
46 ucsdet_setText(ucsd, str, strlen(str), &status);
47 if (U_FAILURE(status)) {
48 debug_error(DEBUG, "fail to ucsdet_setText\n");
52 ucm = ucsdet_detect(ucsd, &status);
53 if (U_FAILURE(status)) {
54 debug_error(DEBUG, "fail to ucsdet_detect\n");
59 debug_error(DEBUG, "fail to ucsdet_detect\n");
63 charset = ucsdet_getName(ucm, &status);
64 if (U_FAILURE(status)) {
65 debug_error(DEBUG, "fail to ucsdet_getName\n");
73 ret_charset = strdup(charset);
81 char *mmfile_convert_to_utf8(const char *str, ssize_t len, const char *from_codeset)
87 mm_file_retvm_if_fails(DEBUG, len != 0, NULL);
89 /*if both to_codeset and from_codeset are same, return duplicated string.*/
90 if (g_strcmp0(MMFILE_CODESET_UTF8, from_codeset) == 0)
91 return g_strndup(str, len);
93 result = g_convert(str, len, MMFILE_CODESET_UTF8, from_codeset, NULL, NULL, &err);
95 /*if converting failed, return null string.*/
97 debug_warning(RELEASE, "text encoding failed.[%s][%zd]\n", str, len);
99 debug_warning(DEBUG, "Error msg [%s]", err->message);
106 split = g_strsplit(result, "\r", -1);
109 result = g_strjoinv(NULL, split);