|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Contains the source for Descriptor to char * conversions |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "libutils.h" |
|
21 |
|
22 |
|
23 |
|
24 /** |
|
25 * Converts a descriptor of type TBuf16 to character stream |
|
26 * |
|
27 * @param aSrc is the descriptor to be converted , aDes is the |
|
28 * reference to the character sream where the result of conversion |
|
29 * is stored , n_size specifies the conversion size of the string |
|
30 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, |
|
31 * -2 is EInvalidSize , -4 is EInvalidPointer) |
|
32 */ |
|
33 EXPORT_C int Tbuf16ToChar(TDes16& aSrc, char* aDes, int& n_size) |
|
34 { |
|
35 unsigned int ilen = aSrc.Length(); |
|
36 int retval = ESuccess; |
|
37 wchar_t *temp16String = NULL; |
|
38 int minusone = -1; |
|
39 |
|
40 if (0 == ilen) |
|
41 { |
|
42 return EDescriptorNoData; |
|
43 } |
|
44 else if(!aDes) |
|
45 { |
|
46 return EInvalidPointer; |
|
47 } |
|
48 |
|
49 else if(n_size < ilen*2+1) |
|
50 { |
|
51 n_size = ilen*2; |
|
52 return EInvalidSize; |
|
53 } |
|
54 |
|
55 temp16String = new wchar_t [ilen+1]; |
|
56 if (!temp16String) |
|
57 { |
|
58 return EInsufficientSystemMemory; |
|
59 } |
|
60 |
|
61 wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen); |
|
62 temp16String[ilen] = L'\0'; |
|
63 |
|
64 if(minusone != wcstombs(aDes, (const wchar_t*)temp16String, ilen*2)) |
|
65 { |
|
66 aDes[ilen*2] = '\0'; |
|
67 } |
|
68 else |
|
69 { |
|
70 retval = EInvalidWCSSequence; |
|
71 } |
|
72 |
|
73 delete []temp16String; |
|
74 return retval; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Converts a descriptor of type TBufC16 to character stream |
|
79 * |
|
80 * @param aSrc is the descriptor to be converted , aDes is the |
|
81 * reference to the character sream where the result of conversion |
|
82 * is stored , n_size specifies the conversion size of the string |
|
83 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, |
|
84 * -2 is EInvalidSize , -4 is EInvalidPointer) |
|
85 */ |
|
86 EXPORT_C int Tbufc16ToChar(TDesC& aSrc, char* aDes, int& n_size) |
|
87 { |
|
88 int retval = ESuccess; |
|
89 unsigned int ilen = 0 ; |
|
90 ilen = aSrc.Length(); |
|
91 wchar_t* temp16String = NULL; |
|
92 int minusone = -1; |
|
93 |
|
94 if (0 == ilen) |
|
95 { |
|
96 return EDescriptorNoData; |
|
97 } |
|
98 else if (!aDes) |
|
99 { |
|
100 return EInvalidPointer; |
|
101 } |
|
102 else if(n_size < ilen*2+1) |
|
103 { |
|
104 n_size = ilen*2 + 1; |
|
105 return EInvalidSize; |
|
106 } |
|
107 |
|
108 temp16String = new wchar_t [ilen+1]; |
|
109 if (!temp16String) |
|
110 { |
|
111 return EInsufficientSystemMemory; |
|
112 } |
|
113 |
|
114 wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen); |
|
115 temp16String[ilen] = L'\0'; |
|
116 |
|
117 if(minusone != wcstombs(aDes, (const wchar_t*)temp16String, ilen*2)) |
|
118 { |
|
119 aDes[ilen*2] = '\0'; |
|
120 } |
|
121 else |
|
122 { |
|
123 retval = EInvalidWCSSequence; |
|
124 } |
|
125 |
|
126 delete []temp16String; |
|
127 return retval; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Converts a descriptor of type TLitc16 to character stream |
|
132 * |
|
133 * @param aSrc is the descriptor to be converted , aDes is the |
|
134 * reference to the character sream where the result of conversion |
|
135 * is stored , n_size specifies the conversion size of the string |
|
136 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, -2 is EInvalidSize |
|
137 * -3 is EDescriptorNoData , -4 is EInvalidPointer) |
|
138 */ |
|
139 |
|
140 EXPORT_C int Tlitc16ToChar(const TDesC16& aSrc, char* aDes, int& n_size) |
|
141 { |
|
142 unsigned int ilen = 0; |
|
143 int retval = ESuccess; |
|
144 ilen = aSrc.Length(); |
|
145 wchar_t* temp16String = NULL; |
|
146 int minusone = -1; |
|
147 |
|
148 if (0 == ilen ) |
|
149 { |
|
150 return EDescriptorNoData; |
|
151 } |
|
152 else if ( !aDes ) |
|
153 { |
|
154 return EInvalidPointer; |
|
155 } |
|
156 else if (n_size < ilen*2 +1) |
|
157 { |
|
158 n_size = ilen*2 + 1; |
|
159 return EInvalidSize; |
|
160 } |
|
161 |
|
162 |
|
163 temp16String = new wchar_t [ilen+1]; |
|
164 if (!temp16String) |
|
165 { |
|
166 return EInsufficientSystemMemory; |
|
167 } |
|
168 |
|
169 wmemcpy(temp16String, (const wchar_t*)aSrc.Ptr(), ilen); |
|
170 temp16String[ilen] = L'\0'; |
|
171 |
|
172 if(minusone != wcstombs(aDes, temp16String, ilen*2)) |
|
173 { |
|
174 aDes[ilen*2] = '\0'; |
|
175 } |
|
176 else |
|
177 { |
|
178 retval = EInvalidWCSSequence; |
|
179 } |
|
180 |
|
181 delete []temp16String; |
|
182 return retval; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Converts a descriptor of type TPtr16 to character stream |
|
187 * |
|
188 * @param aSrc is the descriptor to be converted , aDes is the |
|
189 * reference to the character sream where the result of conversion |
|
190 * is stored , n_size specifies the conversion size of the string |
|
191 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, |
|
192 * -2 is EInvalidSize , -4 is EInvalidPointer) |
|
193 */ |
|
194 |
|
195 EXPORT_C int Tptr16ToCharp (const TDes16& aSrc, char* aDes, int& n_size) |
|
196 { |
|
197 unsigned int ilen = 0 ; |
|
198 int retval = ESuccess; |
|
199 ilen = aSrc.Length(); |
|
200 wchar_t* temp16String = NULL; |
|
201 int minusone = -1; |
|
202 |
|
203 if ( !aDes ) |
|
204 { |
|
205 return EInvalidPointer; |
|
206 } |
|
207 else if (n_size < ilen*2+1) |
|
208 { |
|
209 n_size = ilen*2 + 1; |
|
210 return EInvalidSize; |
|
211 } |
|
212 |
|
213 temp16String = new wchar_t [ilen+1]; |
|
214 if (!temp16String) |
|
215 { |
|
216 return EInsufficientSystemMemory; |
|
217 } |
|
218 |
|
219 wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen); |
|
220 temp16String[ilen] = L'\0'; |
|
221 |
|
222 if(minusone != wcstombs(aDes, temp16String, ilen*2)) |
|
223 { |
|
224 aDes[ilen*2] = '\0'; |
|
225 } |
|
226 else |
|
227 { |
|
228 retval = EInvalidWCSSequence; |
|
229 } |
|
230 |
|
231 delete []temp16String; |
|
232 return retval; |
|
233 } |
|
234 |
|
235 /** |
|
236 * Converts a descriptor of type TPtrC16 to character stream |
|
237 * |
|
238 * @param aSrc is the descriptor to be converted , aDes is the |
|
239 * reference to the character sream where the result of conversion |
|
240 * is stored , n_size specifies the conversion size of the string |
|
241 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, |
|
242 * -2 is EInvalidSize , -4 is EInvalidPointer) |
|
243 */ |
|
244 |
|
245 EXPORT_C int Tptrc16ToCharp(TPtrC16& aSrc, char* aDes, int& n_size) |
|
246 { |
|
247 int retval = ESuccess; |
|
248 unsigned int ilen = aSrc.Length(); |
|
249 int minusone = -1; |
|
250 wchar_t* temp16String = NULL; |
|
251 |
|
252 if(!aDes) |
|
253 { |
|
254 return EInvalidPointer; |
|
255 } |
|
256 else if (n_size < ilen*2+1) |
|
257 { |
|
258 n_size = ilen*2+1; |
|
259 return EInvalidSize; |
|
260 } |
|
261 |
|
262 temp16String = new wchar_t [ilen+1]; |
|
263 if (!temp16String) |
|
264 { |
|
265 return EInsufficientSystemMemory; |
|
266 } |
|
267 |
|
268 wmemcpy(temp16String, (const wchar_t *)aSrc.Ptr(), ilen); |
|
269 temp16String[ilen] = L'\0'; |
|
270 |
|
271 if(minusone != wcstombs(aDes, temp16String, ilen*2)) |
|
272 { |
|
273 aDes[ilen*2] = '\0'; |
|
274 } |
|
275 else |
|
276 { |
|
277 retval = EInvalidWCSSequence; |
|
278 } |
|
279 |
|
280 delete []temp16String; |
|
281 return retval; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Converts a descriptor of type RBuf16 to character stream |
|
286 * |
|
287 * @param aSrc is the descriptor to be converted , aDes is the |
|
288 * reference to the character sream where the result of conversion |
|
289 * is stored , n_size specifies the conversion size of the string |
|
290 * @return Status code (0 is ESuccess, -1 is EInsufficientMemory, |
|
291 * -2 is EInvalidSize , -4 is EInvalidPointer) |
|
292 */ |
|
293 |
|
294 EXPORT_C int Rbuf16ToChar(TDes16& aSrc, char* aDes, int& n_size) |
|
295 { |
|
296 unsigned int ilen = aSrc.Length(); |
|
297 int retval = ESuccess; |
|
298 wchar_t* temp16String = NULL; |
|
299 int minusone = -1; |
|
300 |
|
301 if (0 == ilen) |
|
302 { |
|
303 return EDescriptorNoData; |
|
304 } |
|
305 else if(!aDes) |
|
306 { |
|
307 return EInvalidPointer; |
|
308 } |
|
309 else if (n_size < ilen*2+1) |
|
310 { |
|
311 n_size = ilen*2+1; |
|
312 return EInvalidSize; |
|
313 } |
|
314 |
|
315 temp16String = new wchar_t [ilen+1]; |
|
316 if (!temp16String) |
|
317 { |
|
318 return EInsufficientSystemMemory; |
|
319 } |
|
320 |
|
321 wmemcpy(temp16String,(const wchar_t *) aSrc.Ptr(), ilen); |
|
322 temp16String[ilen] = L'\0'; |
|
323 |
|
324 if(minusone != wcstombs(aDes, temp16String, ilen*2)) |
|
325 { |
|
326 aDes[ilen*2] = '\0'; |
|
327 } |
|
328 else |
|
329 { |
|
330 retval = EInvalidWCSSequence; |
|
331 } |
|
332 |
|
333 delete []temp16String; |
|
334 return retval; |
|
335 } |