////////////////////////////////////////////////////////////////////////
//
//
convert char to wchar_t
////////////////////////////////////////////////////////////////////////
//
#ifndef __CHAR2W__
#define
__CHAR2W__
#include
<
string
.h
>
static
const
char
gUTFBytes[
256
]
=
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
4
,
4
,
4
,
4
,
5
,
5
,
5
,
5
};
static
const
unsigned
long
gUTFOffsets[
6
]
=
{
0
,
0x3080
,
0xE2080
,
0x3C82080
,
0xFA082080
,
0x82082080
};
static
const
unsigned
char
gFirstByteMark[
7
]
=
{
0x00
,
0x00
,
0xC0
,
0xE0
,
0xF0
,
0xF8
,
0xFC
};
static
int
SWIchar2wchar(
const
unsigned
char
*
src, wchar_t
*
dst,
int
maxdstlen)
{
//
Get pointers to our start and end points of the input buffer
const
unsigned
char
*
srcPtr
=
src;
const
unsigned
char
*
srcEnd
=
src
+
strlen((
const
char
*
)src);
wchar_t
*
dstSave
=
dst;
//
wchar_t *dstEnd = dst+maxdstlen; /* leave room for null */
//
We now loop until we run out of input data.
while
(srcPtr
<
srcEnd) {
unsigned
int
trailingBytes;
unsigned
long
tmpVal
=
0
;
//
Get the next leading byte out
const
unsigned
char
firstByte
=
(unsigned
char
)
*
srcPtr;
//
Special-case ASCII, which is a leading byte value of <= 127
if
(firstByte
<=
127
) {
*
dst
++
=
(wchar_t) firstByte;
srcPtr
++
;
continue
;
}
//
See how many trailing src bytes this sequence is going to require
trailingBytes
=
gUTFBytes[firstByte];
//
Looks ok, so lets build up the value
switch
(trailingBytes) {
case
5
: tmpVal
+=
*
srcPtr
++
; tmpVal
<<=
6
;
case
4
: tmpVal
+=
*
srcPtr
++
; tmpVal
<<=
6
;
case
3
: tmpVal
+=
*
srcPtr
++
; tmpVal
<<=
6
;
case
2
: tmpVal
+=
*
srcPtr
++
; tmpVal
<<=
6
;
case
1
: tmpVal
+=
*
srcPtr
++
; tmpVal
<<=
6
;
case
0
: tmpVal
+=
*
srcPtr
++
;
break
;
}
tmpVal
-=
gUTFOffsets[trailingBytes];
*
dst
++
=
(wchar_t)tmpVal;
}
*
dst
=
L
'
\0
'
;
return
dst
-
dstSave;
//
check this (CARO)
}
#endif