|
1 /* |
|
2 * Copyright (c) 2008-2009 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 the License "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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <errno.h> |
|
20 #include <e32base.h> |
|
21 #include <utf.h> |
|
22 #include "stringconv.h" |
|
23 #include "logger.h" |
|
24 |
|
25 TUint8 *cstrFromUtf16(const TText *aUtf16, TInt aLength, TInt &outputBytes) |
|
26 { |
|
27 TPtrC16 src(aUtf16, aLength); |
|
28 |
|
29 TPtr8 nulldest(0,0); |
|
30 |
|
31 TInt len = CnvUtfConverter::ConvertFromUnicodeToUtf8(nulldest, src); |
|
32 if(len<0) |
|
33 { |
|
34 dbg << Log::Indent() << "ConvertFromUnicodeToUtf8 failed" << Log::Endl(); |
|
35 FatalError(); |
|
36 } |
|
37 |
|
38 TUint8 *buf = new TUint8[len+1]; |
|
39 TPtr8 dest(buf, len); |
|
40 |
|
41 (void) CnvUtfConverter::ConvertFromUnicodeToUtf8(dest, src); |
|
42 buf[len] = 0; // Add NUL termination in case it is used with windows APIs |
|
43 outputBytes = dest.Length(); |
|
44 return buf; |
|
45 } |
|
46 |
|
47 std::string stringFromUtf16(const TText *aUtf16, TInt aLength) |
|
48 { |
|
49 TInt outputBytes = 0; |
|
50 TUint8 *outBuf = cstrFromUtf16(aUtf16, aLength, outputBytes); |
|
51 |
|
52 std::string str((const char *)outBuf, outputBytes); |
|
53 delete [] outBuf; |
|
54 return str; |
|
55 } |
|
56 |
|
57 std::string stringFromUtf16(const TDesC &aUtf16) |
|
58 { |
|
59 return stringFromUtf16(aUtf16.Ptr(), aUtf16.Length()); |
|
60 } |
|
61 |
|
62 TText *utf16FromUtf8(const TUint8 *aUtf8, TInt aLength, TInt &outputWords) |
|
63 { |
|
64 // Expand UTF-8 into internal UTF-16LE representation |
|
65 |
|
66 TPtrC8 src(aUtf8, aLength); |
|
67 |
|
68 TPtr16 nulldest(0,0); |
|
69 |
|
70 TInt len = CnvUtfConverter::ConvertToUnicodeFromUtf8(nulldest, src); |
|
71 if(len<0) |
|
72 { |
|
73 dbg << Log::Indent() << "ConvertToUnicodeFromUtf8 failed" << Log::Endl(); |
|
74 FatalError(); |
|
75 } |
|
76 |
|
77 TUint16 *buf = new TUint16[len+1]; |
|
78 TPtr16 dest(buf, len); |
|
79 |
|
80 (void) CnvUtfConverter::ConvertToUnicodeFromUtf8(dest, src); |
|
81 |
|
82 buf[len] = 0; // Add null termination in case it gets used with windows APIs |
|
83 outputWords = dest.Length(); |
|
84 return buf; |
|
85 } |
|
86 |
|
87 |
|
88 // End of file |