Add pancyrillic font
[platform/upstream/kbd.git] / src / wctomb.c
1 /*
2 #include        <sys/types.h>
3 #include        <stdio.h>
4 #include        <stdlib.h>
5 #include        <string.h>
6 #include        <unistd.h>
7 #include        <errno.h>
8 */
9
10 /*
11         the our_* routines are implementations for the corresponding library
12         routines. for a while, i tried to actually name them wctomb etc
13         but stopped that after i found a system which made wchar_t an
14         unsigned char.
15 */
16 enum
17 {
18         T1      = 0x00,
19         Tx      = 0x80,
20         T2      = 0xC0,
21         T3      = 0xE0,
22         T4      = 0xF0,
23         T5      = 0xF8,
24         T6      = 0xFC,
25
26         Bit1    = 7,
27         Bitx    = 6,
28         Bit2    = 5,
29         Bit3    = 4,
30         Bit4    = 3,
31         Bit5    = 2,
32         Bit6    = 2,
33
34         Mask1   = (1<<Bit1)-1,
35         Maskx   = (1<<Bitx)-1,
36         Mask2   = (1<<Bit2)-1,
37         Mask3   = (1<<Bit3)-1,
38         Mask4   = (1<<Bit4)-1,
39         Mask5   = (1<<Bit5)-1,
40         Mask6   = (1<<Bit6)-1,
41
42         Wchar1  = (1UL<<Bit1)-1,
43         Wchar2  = (1UL<<(Bit2+Bitx))-1,
44         Wchar3  = (1UL<<(Bit3+2*Bitx))-1,
45         Wchar4  = (1UL<<(Bit4+3*Bitx))-1,
46         Wchar5  = (1UL<<(Bit5+4*Bitx))-1
47
48 #ifndef EILSEQ
49         , /* we hate ansi c's comma rules */
50         EILSEQ  = 123
51 #endif /* PLAN9 */
52 };
53
54 static int
55 our_wctomb(char *s, unsigned long wc)
56 {
57         if(s == 0)
58                 return 0;               /* no shift states */
59         if(wc & ~Wchar2) {
60                 if(wc & ~Wchar4) {
61                         if(wc & ~Wchar5) {
62                                 /* 6 bytes */
63                                 s[0] = T6 | ((wc >> 5*Bitx) & Mask6);
64                                 s[1] = Tx | ((wc >> 4*Bitx) & Maskx);
65                                 s[2] = Tx | ((wc >> 3*Bitx) & Maskx);
66                                 s[3] = Tx | ((wc >> 2*Bitx) & Maskx);
67                                 s[4] = Tx | ((wc >> 1*Bitx) & Maskx);
68                                 s[5] = Tx |  (wc & Maskx);
69                                 return 6;
70                         }
71                         /* 5 bytes */
72                         s[0] = T5 |  (wc >> 4*Bitx);
73                         s[1] = Tx | ((wc >> 3*Bitx) & Maskx);
74                         s[2] = Tx | ((wc >> 2*Bitx) & Maskx);
75                         s[3] = Tx | ((wc >> 1*Bitx) & Maskx);
76                         s[4] = Tx |  (wc & Maskx);
77                         return 5;
78                 }
79                 if(wc & ~Wchar3) {
80                         /* 4 bytes */
81                         s[0] = T4 |  (wc >> 3*Bitx);
82                         s[1] = Tx | ((wc >> 2*Bitx) & Maskx);
83                         s[2] = Tx | ((wc >> 1*Bitx) & Maskx);
84                         s[3] = Tx |  (wc & Maskx);
85                         return 4;
86                 }
87                 /* 3 bytes */
88                 s[0] = T3 |  (wc >> 2*Bitx);
89                 s[1] = Tx | ((wc >> 1*Bitx) & Maskx);
90                 s[2] = Tx |  (wc & Maskx);
91                 return 3;
92         }
93         if(wc & ~Wchar1) {
94                 /* 2 bytes */
95                 s[0] = T2 | (wc >> 1*Bitx);
96                 s[1] = Tx | (wc & Maskx);
97                 return 2;
98         }
99         /* 1 byte */
100         s[0] = T1 | wc;
101         return 1;
102 }