summaryrefslogtreecommitdiff
path: root/symbian/PerlUtil.cpp
blob: b97907274998e5a3119de87db840fb019a7a6e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (c) 2004-2005 Nokia. All rights reserved. */
 
/* The PerlUtil class is licensed under the same terms as Perl itself. */

/* See PerlUtil.pod for documentation. */

#define PERLUTIL_CPP

#include "PerlUtil.h"

#include <utf.h>

#undef Copy
#undef New

EXPORT_C SV* PerlUtil::newSvPVfromTDesC8(const TDesC8& aDesC8) {
  dTHX;
  return newSVpvn((const char *)aDesC8.Ptr(), aDesC8.Size());
}

EXPORT_C void PerlUtil::setSvPVfromTDesC8(SV* sv, const TDesC8& aDesC8) {
  dTHX;
  sv_setpvn(sv, (const char *)aDesC8.Ptr(), aDesC8.Size());
}

EXPORT_C HBufC8* PerlUtil::newHBufC8fromSvPV(SV* sv) {
  dTHX;
  return PerlUtil::newHBufC8fromPVn((const U8 *)SvPV_nolen(sv), SvLEN(sv));
}

EXPORT_C void PerlUtil::setTDes8fromSvPV(TDes8& aDes8, SV* sv) {
  dTHX;
  PerlUtil::setTDes8fromPVn(aDes8, (const U8 *)SvPV_nolen(sv), SvLEN(sv));
}

EXPORT_C SV* PerlUtil::newSvPVfromTDesC16(const TDesC16& aDesC16) {
  dTHX;
  SV* sv = NULL;
  HBufC8* hBuf8 = HBufC8::New(aDesC16.Length() * 3);

  if (hBuf8) {
    TPtr8 aPtr8(hBuf8->Des());
    CnvUtfConverter::ConvertFromUnicodeToUtf8(aPtr8, aDesC16);
    sv = newSVpvn((const char *)(hBuf8->Ptr()), hBuf8->Size());
    delete hBuf8;
    hBuf8 = NULL;
    SvUTF8_on(sv);
  }

  return sv;
}

EXPORT_C void PerlUtil::setSvPVfromTDesC16(SV* sv, const TDesC16& aDesC16) {
  dTHX;
  HBufC8* hBuf8 = HBufC8::New(aDesC16.Length() * 3);

  if (hBuf8) {
    TPtr8 aPtr8(hBuf8->Des());
    CnvUtfConverter::ConvertFromUnicodeToUtf8(aPtr8, aDesC16);
    sv_setpvn(sv, (const char *)(hBuf8->Ptr()), hBuf8->Size());
    delete hBuf8;
    hBuf8 = NULL;
    SvUTF8_on(sv);
  }
}

EXPORT_C HBufC16* PerlUtil::newHBufC16fromSvPV(SV* sv) {
  dTHX;
  return PerlUtil::newHBufC16fromPVn((const U8 *)SvPV_nolen(sv), SvLEN(sv), SvUTF8(sv));
}

void PerlUtil::setTDes16fromSvPV(TDes16& aDes16, SV* sv) {
  dTHX;
  PerlUtil::setTDes16fromPVn(aDes16, (const U8 *)SvPV_nolen(sv), SvLEN(sv), SvUTF8(sv));
}

EXPORT_C HBufC8* PerlUtil::newHBufC8fromPVn(const U8* s, STRLEN n) {
  HBufC8* aBuf8 = HBufC8::New(n);
  if (aBuf8) {
    TPtr8 ptr8 = aBuf8->Des();
    ptr8.Copy(s, n);
  }
  return aBuf8;
}

EXPORT_C void PerlUtil::setTDes8fromPVn(TDes8& aDes8, const U8* s, STRLEN n) {
  // TODO: grow aDes8 if needed
  aDes8.Copy(s, n);
}

EXPORT_C HBufC16* PerlUtil::newHBufC16fromPVn(const U8 *s, STRLEN n, bool utf8) {
  HBufC16 *hBuf16 = HBufC16::New(n); // overallocate

  if (hBuf16) {
    if (utf8) {
      TPtr16 aPtr16(hBuf16->Des());
      TPtrC8 aPtrC8(s, n);
      CnvUtfConverter::ConvertToUnicodeFromUtf8(aPtr16, aPtrC8);
    } else {
      TPtrC8 aPtrC8(s, n);
      hBuf16->Des().Copy(aPtrC8);
    }
  }

  return hBuf16;
}

EXPORT_C void PerlUtil::setTDes16fromPVn(TDes16& aDes16, const U8 *s, STRLEN n, bool utf8) {
  // TODO: grow aDes16 if needed
  if (utf8) {
    TPtrC8 aPtrC8(s, n);
    CnvUtfConverter::ConvertToUnicodeFromUtf8(aDes16, aPtrC8);
  } else {
    TPtrC8 aPtrC8(s, n);
    aDes16.Copy(aPtrC8);
  }
}