Line | |
---|
1 | #include <wchar.h> |
---|
2 | #include "dietlocale.h" |
---|
3 | |
---|
4 | static mbstate_t internal; |
---|
5 | |
---|
6 | size_t wcrtomb(char *s, wchar_t wc, mbstate_t *ps) { |
---|
7 | if (!ps) ps=&internal; |
---|
8 | switch (lc_ctype) { |
---|
9 | case CT_8BIT: |
---|
10 | if (!s) return 0; |
---|
11 | *s=wc; |
---|
12 | return 1; |
---|
13 | case CT_UTF8: |
---|
14 | if (!s) return (wc>=0x80); |
---|
15 | { |
---|
16 | unsigned int bits,j,k; |
---|
17 | if (wc>=0x04000000) { bits=30; *s=0xFC; j=6; } else |
---|
18 | if (wc>=0x00200000) { bits=24; *s=0xF8; j=5; } else |
---|
19 | if (wc>=0x00010000) { bits=18; *s=0xF0; j=4; } else |
---|
20 | if (wc>=0x00000800) { bits=12; *s=0xE0; j=3; } else |
---|
21 | if (wc>=0x00000080) { bits=6; *s=0xC0; j=2; } else |
---|
22 | { *s=wc; return 1; } |
---|
23 | *s |= (unsigned char)(wc>>bits); |
---|
24 | for (k=1; k<j; ++k) { |
---|
25 | bits-=6; |
---|
26 | s[k]=0x80+((wc>>bits)&0x3f); |
---|
27 | } |
---|
28 | return k; |
---|
29 | } |
---|
30 | } |
---|
31 | return 0; |
---|
32 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.