support webm format
[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 EXPORT_API
31 int  mmfile_util_wstrlen(unsigned short *wText)
32 {
33         int n = 0;
34
35         if (NULL == wText) {
36                 debug_error(DEBUG, "wText is NULL\n");
37                 return MMFILE_UTIL_FAIL;
38         }
39
40         n = 0;
41
42         while (*(wText + n) != 0) {
43                 n++;
44         }
45
46         return n;
47 }
48
49 short __WmLngSwapShort(short aShort)
50 {
51         return ((aShort << 8) + (aShort >> 8));
52 }
53
54 EXPORT_API
55 short *mmfile_swap_2byte_string(short *mszOutput, short *mszInput, int length)
56 {
57         int     i;
58
59         for (i = 0; i < length; i++) {
60                 if (mszInput[i] == 0)
61                         break;
62
63                 mszOutput[i] = __WmLngSwapShort(mszInput[i]);
64         }
65
66         mszOutput[i] = 0;
67
68         return mszOutput;
69 }
70
71
72 EXPORT_API
73 char *mmfile_string_convert_debug(const char *str, unsigned int len,
74                                                                 const char *to_codeset, const char *from_codeset,
75                                                                 int *bytes_read,
76                                                                 int *bytes_written,
77                                                                 const char *func,
78                                                                 unsigned int line)
79 {
80         char *tmp = g_convert(str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);
81
82         if (tmp) {
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);
85         }
86
87         return tmp;
88
89 }
90
91 EXPORT_API
92 char **mmfile_strsplit(const char *string, const char *delimiter)
93 {
94         return g_strsplit(string, delimiter, -1);
95 }
96
97 EXPORT_API
98 void mmfile_strfreev(char **str_array)
99 {
100         g_strfreev(str_array);
101 }
102
103 EXPORT_API
104 char *mmfile_strdup_debug(const char *str, const char *func, unsigned int line)
105 {
106         char *temp = NULL;
107
108         if (!str)
109                 return NULL;
110
111         temp = strdup(str);
112
113         if (temp) {
114                 debug_msg(RELEASE, "## DEBUG ## %p = strdup (%p) by %s() %d\n", temp, str, func, line);
115         }
116
117         return temp;
118 }
119
120
121 #else   /* __MMFILE_MEM_TRACE__ */
122
123 EXPORT_API
124 int  mmfile_util_wstrlen(unsigned short *wText)
125 {
126         int n = 0;
127
128         if (NULL == wText) {
129                 debug_error(DEBUG, "wText is NULL\n");
130                 return MMFILE_UTIL_FAIL;
131         }
132
133         n = 0;
134
135         while (*(wText + n) != 0) {
136                 n++;
137         }
138
139         return n;
140 }
141
142 EXPORT_API
143 char *mmfile_get_charset(const char *str)
144 {
145         UCharsetDetector *ucsd = NULL;
146         const UCharsetMatch *ucm = NULL;
147         UErrorCode status = U_ZERO_ERROR;
148
149         const char *charset = NULL;
150         char *ret_charset = NULL;
151
152         ucsd = ucsdet_open(&status);
153         if (U_FAILURE(status)) {
154                 debug_error(DEBUG, "fail to ucsdet_open\n");
155                 return NULL;
156         }
157
158         ucsdet_enableInputFilter(ucsd, TRUE);
159
160         ucsdet_setText(ucsd, str, strlen(str), &status);
161         if (U_FAILURE(status)) {
162                 debug_error(DEBUG, "fail to ucsdet_setText\n");
163                 goto done;
164         }
165
166         ucm = ucsdet_detect(ucsd, &status);
167         if (U_FAILURE(status)) {
168                 debug_error(DEBUG, "fail to ucsdet_detect\n");
169                 goto done;
170         }
171
172         if (ucm == NULL) {
173                 debug_error(DEBUG, "fail to ucsdet_detect\n");
174                 goto done;
175         }
176
177         charset = ucsdet_getName(ucm, &status);
178         if (U_FAILURE(status)) {
179                 debug_error(DEBUG, "fail to ucsdet_getName\n");
180                 charset = NULL;
181                 goto done;
182         }
183
184 done:
185
186         if (charset != NULL)
187                 ret_charset = strdup(charset);
188
189         ucsdet_close(ucsd);
190
191         return ret_charset;
192 }
193
194 EXPORT_API
195 char *mmfile_string_convert(const char *str, unsigned int len,
196                                 const char *to_codeset, const char *from_codeset,
197                                 gsize *bytes_read,
198                                 unsigned int *bytes_written)
199 {
200         char *result = NULL;
201         GError *err = NULL;
202         /*int i = 0;*/
203         gsize written_len = 0;
204
205         if (len != 0) {
206                 result = g_convert(str, len, to_codeset, from_codeset, bytes_read, &written_len, &err);
207
208                 /*if converting failed, return duplicated source string.*/
209                 if (result == NULL) {
210                         debug_warning(RELEASE, "text encoding failed.[%s][%d]\n", str, len);
211
212                         if (err != NULL) {
213                                 debug_warning(DEBUG, "Error msg [%s]", err->message);
214                                 g_error_free(err);
215                         }
216
217                         written_len = 0;
218                 } else {
219                         /* check carrige return */
220                         unsigned int i = 0;
221                         for (i = 0; i < written_len; i++) {
222                                 if (result[i] == 13) {
223                                         if (result[i + 1] != 10)
224                                                 result[i] = 10;
225                                 }
226                         }
227                 }
228         } else {
229                 written_len = 0;
230         }
231
232         if (bytes_written != NULL) {
233                 *bytes_written = written_len;
234         }
235
236         return result;
237 }
238
239 EXPORT_API
240 char **mmfile_strsplit(const char *string, const char *delimiter)
241 {
242         return g_strsplit(string, delimiter, -1);
243 }
244
245 EXPORT_API
246 void mmfile_strfreev(char **str_array)
247 {
248         g_strfreev(str_array);
249 }
250
251 EXPORT_API
252 char *mmfile_strdup(const char *str)
253 {
254         if (!str)
255                 return NULL;
256
257         return strdup(str);
258 }
259
260 #endif  /*__MMFILE_MEM_TRACE__*/
261