sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include <errno.h>
|
sl@0
|
20 |
#include <e32base.h>
|
sl@0
|
21 |
#include <utf.h>
|
sl@0
|
22 |
#include "stringconv.h"
|
sl@0
|
23 |
#include "logger.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
TUint8 *cstrFromUtf16(const TText *aUtf16, TInt aLength, TInt &outputBytes)
|
sl@0
|
26 |
{
|
sl@0
|
27 |
TPtrC16 src(aUtf16, aLength);
|
sl@0
|
28 |
|
sl@0
|
29 |
TPtr8 nulldest(0,0);
|
sl@0
|
30 |
|
sl@0
|
31 |
TInt len = CnvUtfConverter::ConvertFromUnicodeToUtf8(nulldest, src);
|
sl@0
|
32 |
if(len<0)
|
sl@0
|
33 |
{
|
sl@0
|
34 |
dbg << Log::Indent() << "ConvertFromUnicodeToUtf8 failed" << Log::Endl();
|
sl@0
|
35 |
FatalError();
|
sl@0
|
36 |
}
|
sl@0
|
37 |
|
sl@0
|
38 |
TUint8 *buf = new TUint8[len+1];
|
sl@0
|
39 |
TPtr8 dest(buf, len);
|
sl@0
|
40 |
|
sl@0
|
41 |
(void) CnvUtfConverter::ConvertFromUnicodeToUtf8(dest, src);
|
sl@0
|
42 |
buf[len] = 0; // Add NUL termination in case it is used with windows APIs
|
sl@0
|
43 |
outputBytes = dest.Length();
|
sl@0
|
44 |
return buf;
|
sl@0
|
45 |
}
|
sl@0
|
46 |
|
sl@0
|
47 |
std::string stringFromUtf16(const TText *aUtf16, TInt aLength)
|
sl@0
|
48 |
{
|
sl@0
|
49 |
TInt outputBytes = 0;
|
sl@0
|
50 |
TUint8 *outBuf = cstrFromUtf16(aUtf16, aLength, outputBytes);
|
sl@0
|
51 |
|
sl@0
|
52 |
std::string str((const char *)outBuf, outputBytes);
|
sl@0
|
53 |
delete [] outBuf;
|
sl@0
|
54 |
return str;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
std::string stringFromUtf16(const TDesC &aUtf16)
|
sl@0
|
58 |
{
|
sl@0
|
59 |
return stringFromUtf16(aUtf16.Ptr(), aUtf16.Length());
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
TText *utf16FromUtf8(const TUint8 *aUtf8, TInt aLength, TInt &outputWords)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
// Expand UTF-8 into internal UTF-16LE representation
|
sl@0
|
65 |
|
sl@0
|
66 |
TPtrC8 src(aUtf8, aLength);
|
sl@0
|
67 |
|
sl@0
|
68 |
TPtr16 nulldest(0,0);
|
sl@0
|
69 |
|
sl@0
|
70 |
TInt len = CnvUtfConverter::ConvertToUnicodeFromUtf8(nulldest, src);
|
sl@0
|
71 |
if(len<0)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
dbg << Log::Indent() << "ConvertToUnicodeFromUtf8 failed" << Log::Endl();
|
sl@0
|
74 |
FatalError();
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
TUint16 *buf = new TUint16[len+1];
|
sl@0
|
78 |
TPtr16 dest(buf, len);
|
sl@0
|
79 |
|
sl@0
|
80 |
(void) CnvUtfConverter::ConvertToUnicodeFromUtf8(dest, src);
|
sl@0
|
81 |
|
sl@0
|
82 |
buf[len] = 0; // Add null termination in case it gets used with windows APIs
|
sl@0
|
83 |
outputWords = dest.Length();
|
sl@0
|
84 |
return buf;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
|
sl@0
|
88 |
// End of file
|