merge with master
[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 "mm_debug.h"
26 #include "mm_file_utils.h"
27
28 #ifdef __MMFILE_MEM_TRACE__
29 EXPORT_API
30 int  mmfile_util_wstrlen (unsigned short *wText)
31 {
32     int n = 0;
33
34     if (NULL == wText)
35     {
36         debug_error ("wText is NULL\n");
37         return MMFILE_UTIL_FAIL;
38     }
39
40     n = 0;
41
42     while ( *(wText+n) != 0 )
43     {
44         n++;
45     }
46
47     return n; 
48 }
49
50 short __WmLngSwapShort( short aShort )
51 {
52         return ( ( aShort << 8 ) + ( aShort >> 8 ) );
53 }
54
55 EXPORT_API
56 short* mmfile_swap_2byte_string (short* mszOutput, short* mszInput, int length)
57 {
58         int     i;
59
60         for ( i = 0; i < length; i++ )
61         {
62                 if ( mszInput[i] == 0 )
63                         break;
64
65                 mszOutput[i] = __WmLngSwapShort( mszInput[i] );
66         }
67
68         mszOutput[i] = 0;
69
70         return mszOutput;
71 }
72
73
74 EXPORT_API
75 char *mmfile_string_convert_debug (const char *str, unsigned int len,
76                              const char *to_codeset, const char *from_codeset,
77                              int *bytes_read,
78                              int *bytes_written,
79                              const char *func,
80                              unsigned int line)
81 {
82     char *tmp = g_convert (str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);
83
84     if (tmp)
85     {
86         fprintf (stderr, "## DEBUG ## %p = g_convert (%p, %u, %p, %p, %p ,%p, %p, %u) by %s() %d\n",
87                           tmp, str, len, to_codeset, from_codeset, bytes_read, bytes_written, func, line);
88     }
89
90     return tmp;
91     
92 }
93
94 EXPORT_API
95 char **mmfile_strsplit (const char *string, const char *delimiter)
96 {
97     return g_strsplit (string, delimiter, -1);
98 }
99
100 EXPORT_API
101 void mmfile_strfreev (char **str_array)
102 {
103     g_strfreev(str_array);
104 }
105
106 EXPORT_API
107 char *mmfile_strdup_debug (const char *str, const char *func, unsigned int line)
108 {
109     char *temp = NULL;
110     
111     if (!str)
112         return NULL;
113     
114     temp = strdup (str);
115
116     if (temp) {
117         fprintf (stderr, "## DEBUG ## %p = strdup (%p) by %s() %d\n", temp, str, func, line);
118     }
119
120     return temp; 
121 }
122
123
124 #else   /* __MMFILE_MEM_TRACE__ */
125
126 EXPORT_API
127 int  mmfile_util_wstrlen (unsigned short *wText)
128 {
129     int n = 0;
130
131     if (NULL == wText)
132     {
133         debug_error ("wText is NULL\n");
134         return MMFILE_UTIL_FAIL;
135     }
136
137     n = 0;
138
139     while ( *(wText+n) != 0 )
140     {
141         n++;
142     }
143
144     return n; 
145 }
146
147 EXPORT_API
148 char *mmfile_string_convert (const char *str, unsigned int len,
149                              const char *to_codeset, const char *from_codeset,
150                              unsigned int *bytes_read,
151                              unsigned int *bytes_written)
152 {
153         char *result = NULL;
154
155         result = g_convert (str, len, to_codeset, from_codeset, bytes_read, bytes_written, NULL);  
156
157         /*if converting failed, return duplicated source string.*/
158         if (result == NULL) {
159 #ifdef __MMFILE_TEST_MODE__
160                 debug_warning ("text encoding failed.[%s][%d]\n", str, len);
161 #endif
162         }
163
164         return result;
165 }
166
167 EXPORT_API
168 char **mmfile_strsplit (const char *string, const char *delimiter)
169 {
170     return g_strsplit (string, delimiter, -1);
171 }
172
173 EXPORT_API
174 void mmfile_strfreev (char **str_array)
175 {
176     g_strfreev(str_array);
177 }
178
179 EXPORT_API
180 char *mmfile_strdup (const char *str)
181 {
182     if (!str)
183         return NULL;
184     
185     return strdup (str);
186 }
187
188 #endif  /*__MMFILE_MEM_TRACE__*/
189