Commit | Line | Data |
---|---|---|
c8f896e5 JH |
1 | /* Copyright (c) 2004-2005 Nokia. All rights reserved. */ |
2 | ||
3 | /* The PerlUtil class is licensed under the same terms as Perl itself. */ | |
4 | ||
5 | /* See PerlUtil.pod for documentation. */ | |
6 | ||
7 | #define PERLUTIL_CPP | |
8 | ||
9 | #include "PerlUtil.h" | |
10 | ||
11 | #include <utf.h> | |
12 | ||
13 | #undef Copy | |
33c3d051 | 14 | #undef New |
c8f896e5 JH |
15 | |
16 | EXPORT_C SV* PerlUtil::newSvPVfromTDesC8(const TDesC8& aDesC8) { | |
17 | dTHX; | |
18 | return newSVpvn((const char *)aDesC8.Ptr(), aDesC8.Size()); | |
19 | } | |
20 | ||
21 | EXPORT_C void PerlUtil::setSvPVfromTDesC8(SV* sv, const TDesC8& aDesC8) { | |
22 | dTHX; | |
23 | sv_setpvn(sv, (const char *)aDesC8.Ptr(), aDesC8.Size()); | |
24 | } | |
25 | ||
26 | EXPORT_C HBufC8* PerlUtil::newHBufC8fromSvPV(SV* sv) { | |
27 | dTHX; | |
28 | return PerlUtil::newHBufC8fromPVn((const U8 *)SvPV_nolen(sv), SvLEN(sv)); | |
29 | } | |
30 | ||
31 | EXPORT_C void PerlUtil::setTDes8fromSvPV(TDes8& aDes8, SV* sv) { | |
32 | dTHX; | |
33 | PerlUtil::setTDes8fromPVn(aDes8, (const U8 *)SvPV_nolen(sv), SvLEN(sv)); | |
34 | } | |
35 | ||
36 | EXPORT_C SV* PerlUtil::newSvPVfromTDesC16(const TDesC16& aDesC16) { | |
37 | dTHX; | |
38 | SV* sv = NULL; | |
39 | HBufC8* hBuf8 = HBufC8::New(aDesC16.Length() * 3); | |
40 | ||
41 | if (hBuf8) { | |
42 | TPtr8 aPtr8(hBuf8->Des()); | |
43 | CnvUtfConverter::ConvertFromUnicodeToUtf8(aPtr8, aDesC16); | |
44 | sv = newSVpvn((const char *)(hBuf8->Ptr()), hBuf8->Size()); | |
45 | delete hBuf8; | |
46 | hBuf8 = NULL; | |
47 | SvUTF8_on(sv); | |
48 | } | |
49 | ||
50 | return sv; | |
51 | } | |
52 | ||
53 | EXPORT_C void PerlUtil::setSvPVfromTDesC16(SV* sv, const TDesC16& aDesC16) { | |
54 | dTHX; | |
55 | HBufC8* hBuf8 = HBufC8::New(aDesC16.Length() * 3); | |
56 | ||
57 | if (hBuf8) { | |
58 | TPtr8 aPtr8(hBuf8->Des()); | |
59 | CnvUtfConverter::ConvertFromUnicodeToUtf8(aPtr8, aDesC16); | |
60 | sv_setpvn(sv, (const char *)(hBuf8->Ptr()), hBuf8->Size()); | |
61 | delete hBuf8; | |
62 | hBuf8 = NULL; | |
63 | SvUTF8_on(sv); | |
64 | } | |
65 | } | |
66 | ||
67 | EXPORT_C HBufC16* PerlUtil::newHBufC16fromSvPV(SV* sv) { | |
68 | dTHX; | |
69 | return PerlUtil::newHBufC16fromPVn((const U8 *)SvPV_nolen(sv), SvLEN(sv), SvUTF8(sv)); | |
70 | } | |
71 | ||
72 | void PerlUtil::setTDes16fromSvPV(TDes16& aDes16, SV* sv) { | |
73 | dTHX; | |
74 | PerlUtil::setTDes16fromPVn(aDes16, (const U8 *)SvPV_nolen(sv), SvLEN(sv), SvUTF8(sv)); | |
75 | } | |
76 | ||
77 | EXPORT_C HBufC8* PerlUtil::newHBufC8fromPVn(const U8* s, STRLEN n) { | |
78 | HBufC8* aBuf8 = HBufC8::New(n); | |
79 | if (aBuf8) { | |
80 | TPtr8 ptr8 = aBuf8->Des(); | |
81 | ptr8.Copy(s, n); | |
82 | } | |
83 | return aBuf8; | |
84 | } | |
85 | ||
86 | EXPORT_C void PerlUtil::setTDes8fromPVn(TDes8& aDes8, const U8* s, STRLEN n) { | |
87 | // TODO: grow aDes8 if needed | |
88 | aDes8.Copy(s, n); | |
89 | } | |
90 | ||
91 | EXPORT_C HBufC16* PerlUtil::newHBufC16fromPVn(const U8 *s, STRLEN n, bool utf8) { | |
92 | HBufC16 *hBuf16 = HBufC16::New(n); // overallocate | |
93 | ||
94 | if (hBuf16) { | |
95 | if (utf8) { | |
96 | TPtr16 aPtr16(hBuf16->Des()); | |
97 | TPtrC8 aPtrC8(s, n); | |
98 | CnvUtfConverter::ConvertToUnicodeFromUtf8(aPtr16, aPtrC8); | |
99 | } else { | |
100 | TPtrC8 aPtrC8(s, n); | |
101 | hBuf16->Des().Copy(aPtrC8); | |
102 | } | |
103 | } | |
104 | ||
105 | return hBuf16; | |
106 | } | |
107 | ||
108 | EXPORT_C void PerlUtil::setTDes16fromPVn(TDes16& aDes16, const U8 *s, STRLEN n, bool utf8) { | |
109 | // TODO: grow aDes16 if needed | |
110 | if (utf8) { | |
111 | TPtrC8 aPtrC8(s, n); | |
112 | CnvUtfConverter::ConvertToUnicodeFromUtf8(aDes16, aPtrC8); | |
113 | } else { | |
114 | TPtrC8 aPtrC8(s, n); | |
115 | aDes16.Copy(aPtrC8); | |
116 | } | |
117 | } |