Improve functions to reduce Duplicate Code
[platform/core/multimedia/libmm-fileinfo.git] / utils / mm_file_util_string.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <unicode/ucsdet.h>
26 #include "mm_file_debug.h"
27 #include "mm_file_utils.h"
28
29 #ifdef __MMFILE_MEM_TRACE__
30 short __WmLngSwapShort(short aShort)
31 {
32         return ((aShort << 8) + (aShort >> 8));
33 }
34
35 EXPORT_API
36 short *mmfile_swap_2byte_string(short *mszOutput, short *mszInput, int length)
37 {
38         int     i;
39
40         for (i = 0; i < length; i++) {
41                 if (mszInput[i] == 0)
42                         break;
43
44                 mszOutput[i] = __WmLngSwapShort(mszInput[i]);
45         }
46
47         mszOutput[i] = 0;
48
49         return mszOutput;
50 }
51
52
53 EXPORT_API
54 char *mmfile_string_convert_debug(const char *str, unsigned int len,
55                                                                 const char *to_codeset, const char *from_codeset,
56                                                                 int *bytes_read,
57                                                                 int *bytes_written,
58                                                                 const char *func,
59                                                                 unsigned int line)
60 {
61         char *tmp = g_convert(str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);
62
63         if (tmp) {
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);
66         }
67
68         return tmp;
69
70 }
71
72 EXPORT_API
73 char *mmfile_strdup_debug(const char *str, const char *func, unsigned int line)
74 {
75         char *temp = NULL;
76
77         if (!str)
78                 return NULL;
79
80         temp = strdup(str);
81
82         if (temp) {
83                 debug_msg(RELEASE, "## DEBUG ## %p = strdup (%p) by %s() %d\n", temp, str, func, line);
84         }
85
86         return temp;
87 }
88
89
90 #else   /* __MMFILE_MEM_TRACE__ */
91
92 EXPORT_API
93 char *mmfile_get_charset(const char *str)
94 {
95         UCharsetDetector *ucsd = NULL;
96         const UCharsetMatch *ucm = NULL;
97         UErrorCode status = U_ZERO_ERROR;
98
99         const char *charset = NULL;
100         char *ret_charset = NULL;
101
102         ucsd = ucsdet_open(&status);
103         if (U_FAILURE(status)) {
104                 debug_error(DEBUG, "fail to ucsdet_open\n");
105                 return NULL;
106         }
107
108         ucsdet_enableInputFilter(ucsd, TRUE);
109
110         ucsdet_setText(ucsd, str, strlen(str), &status);
111         if (U_FAILURE(status)) {
112                 debug_error(DEBUG, "fail to ucsdet_setText\n");
113                 goto done;
114         }
115
116         ucm = ucsdet_detect(ucsd, &status);
117         if (U_FAILURE(status)) {
118                 debug_error(DEBUG, "fail to ucsdet_detect\n");
119                 goto done;
120         }
121
122         if (ucm == NULL) {
123                 debug_error(DEBUG, "fail to ucsdet_detect\n");
124                 goto done;
125         }
126
127         charset = ucsdet_getName(ucm, &status);
128         if (U_FAILURE(status)) {
129                 debug_error(DEBUG, "fail to ucsdet_getName\n");
130                 charset = NULL;
131                 goto done;
132         }
133
134 done:
135
136         if (charset != NULL)
137                 ret_charset = strdup(charset);
138
139         ucsdet_close(ucsd);
140
141         return ret_charset;
142 }
143
144 EXPORT_API
145 char *mmfile_string_convert(const char *str, unsigned int len,
146                                 const char *to_codeset, const char *from_codeset,
147                                 gsize *bytes_read,
148                                 unsigned int *bytes_written)
149 {
150         char *result = NULL;
151         GError *err = NULL;
152         /*int i = 0;*/
153         gsize written_len = 0;
154
155         if (len != 0) {
156                 result = g_convert(str, len, to_codeset, from_codeset, bytes_read, &written_len, &err);
157
158                 /*if converting failed, return duplicated source string.*/
159                 if (result == NULL) {
160                         debug_warning(RELEASE, "text encoding failed.[%s][%d]\n", str, len);
161
162                         if (err != NULL) {
163                                 debug_warning(DEBUG, "Error msg [%s]", err->message);
164                                 g_error_free(err);
165                         }
166
167                         written_len = 0;
168                 } else {
169                         /* check carrige return */
170                         unsigned int i = 0;
171                         for (i = 0; i < written_len; i++) {
172                                 if (result[i] == 13) {
173                                         if (result[i + 1] != 10)
174                                                 result[i] = 10;
175                                 }
176                         }
177                 }
178         } else {
179                 written_len = 0;
180         }
181
182         if (bytes_written != NULL) {
183                 *bytes_written = written_len;
184         }
185
186         return result;
187 }
188
189 EXPORT_API
190 char *mmfile_strdup(const char *str)
191 {
192         if (!str)
193                 return NULL;
194
195         return strdup(str);
196 }
197
198 #endif  /*__MMFILE_MEM_TRACE__*/
199
200 EXPORT_API
201 int  mmfile_util_wstrlen(unsigned short *wText)
202 {
203         int n = 0;
204
205         if (NULL == wText) {
206                 debug_error(DEBUG, "wText is NULL\n");
207                 return MMFILE_UTIL_FAIL;
208         }
209
210         n = 0;
211
212         while (*(wText + n) != 0) {
213                 n++;
214         }
215
216         return n;
217 }
218
219 EXPORT_API
220 char **mmfile_strsplit(const char *string, const char *delimiter)
221 {
222         return g_strsplit(string, delimiter, -1);
223 }
224
225 EXPORT_API
226 void mmfile_strfreev(char **str_array)
227 {
228         g_strfreev(str_array);
229 }