Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / c++ / symbian / base / symbianadapter.cpp
1 #include <e32cmn.h>
2 #include <e32des8.h>
3 #include <e32def.h>
4 #include <e32std.h>
5
6 #include "base/fscapi.h"
7 #include "base/util/utils.h"
8 #include "base/util/symbianUtils.h"
9
10 wchar_t * wcsncpy (wchar_t *dst, const wchar_t *src, size_t count) {
11     memcpy(dst, src, count * sizeof(wchar_t));
12     return dst;
13 }
14
15 int towlower(int c) {
16     wchar_t tmpStr[1];
17     tmpStr[0] = (wchar_t)c;
18     TBuf16<1> buf = (const TUint16*)tmpStr;
19     buf.LowerCase();
20     const TUint16* resStr = buf.Ptr();
21     return (int)*resStr;
22 }
23
24 int towupper(int c)  {
25     wchar_t tmpStr[1];
26     tmpStr[0] = (wchar_t)c;
27     TBuf16<1> buf = (const TUint16*)tmpStr;
28     buf.UpperCase();
29     const TUint16* resStr = buf.Ptr();
30     return (int)*resStr;
31 }
32
33 size_t vsnprintf(char* s, size_t size, const char* format, PLATFORM_VA_LIST aq) {
34
35     TPtrC8 formatBuf((const unsigned char*)format);
36     TInt error;
37     RBuf8 formattedBuf;
38
39     TRAP(error, formattedBuf.CreateL(size));
40     if (error == KErrNone) {
41         TRAP(error, formattedBuf.FormatList(formatBuf, aq));
42         if (error == KErrNone) {
43             char* ptr = (char *) formattedBuf.Ptr();
44             size_t finalSize = formattedBuf.Length();
45             if (finalSize < size) {
46                 memcpy(s, ptr, finalSize);
47                 s[finalSize] = 0;           // Symbian descriptors don't have the trailing null char
48                 return finalSize;
49             } else {
50                 // In this case we truncate. We signal this by returning -1
51                 memcpy(s, ptr, size);
52                 return (size_t)-1;
53             }
54         }
55     }
56     // We cannot format the string. Return -1.
57     return (size_t)-1;
58 }
59
60
61 // TODO: convert to the specified encoding, assuming wc is UTF-8
62 char* toMultibyte(const WCHAR *wc, const char *encoding)
63 {
64 #ifdef USE_WCHAR
65     size_t length = wcstombs(NULL, wc, 0) + 1;
66     if(length == (size_t)-1) {
67         //LOG.error("toMultibyte: invalid string.");
68         return strdup("");
69     }
70     char* ret = new char[length];
71     wcstombs(ret, wc, length);
72
73     return ret;
74 #else
75     return stringdup(wc);
76 #endif
77 }
78
79 // TODO: convert to UTF-8 from the specified encoding
80 WCHAR* toWideChar(const char *mb, const char *encoding)
81 {
82 #ifdef USE_WCHAR
83     size_t length = mbstowcs(NULL, mb, 0) + 1;
84     if(length == (size_t)-1) {
85         //LOG.error("toWideChar: invalid string.");
86         return wstrdup(TEXT(""));
87     }
88     WCHAR* ret = new WCHAR[length];
89     mbstowcs(ret, mb, length);
90
91     return ret;
92 #else
93     return stringdup(mb);
94 #endif
95 }
96
97
98 size_t snwprintf(WCHAR *v, size_t size, const WCHAR* format, unsigned long value) {
99
100     TPtrC16 formatBuf((const TUint16*)format);
101     TInt error;
102     RBuf16 formattedBuf;
103
104     TRAP(error, formattedBuf.CreateL(size));
105     if (error == KErrNone) {
106         TRAP(error, formattedBuf.Format(formatBuf, value));
107         if (error == KErrNone) {
108             WCHAR* ptr = (WCHAR *) formattedBuf.Ptr();
109             size_t finalSize = formattedBuf.Length() * sizeof(WCHAR);
110             if (finalSize < size) {
111                 memcpy(v, ptr, finalSize);
112                 v[finalSize] = 0;   // Symbian descriptors don't have the trailing null char
113                 return finalSize;
114             } else {
115                 // In this case we truncate. We signal this by returning -1
116                 memcpy(v, ptr, size);
117                 v[size] = 0;   // Symbian descriptors don't have the trailing null char
118                 return (size_t)-1;
119             }
120         }
121     }
122     // We cannot format the string. Return -1.
123     return (size_t)-1;
124 }
125
126
127 WCHAR *wcschr(const WCHAR *ws, WCHAR wc) {
128     return NULL;
129 }
130
131 WCHAR *wcsstr(WCHAR *ws1, WCHAR *ws2) {
132     return NULL;
133 }
134
135 WCHAR *wcstok(WCHAR *ws1, const WCHAR *ws2) {
136     return NULL;
137 }
138
139 WCHAR *wcsncat(WCHAR *ws1, const WCHAR *ws2, size_t n) {
140     return NULL;
141 }
142
143 double wcstod(const WCHAR *nptr, WCHAR **endptr) {
144     return 0.0;
145 }
146
147 int _wtoi(const WCHAR *str) {
148     return 0;
149 }
150