Andreas Krennmair:
[apps/madmutt.git] / utf8.c
1 #ifndef HAVE_WC_FUNCS
2
3 #include <errno.h>
4
5 #ifndef EILSEQ
6 #define EILSEQ EINVAL
7 #endif
8
9 int mutt_wctoutf8 (char *s, unsigned int c)
10 {
11   if (c < (1 << 7)) 
12   {
13     if (s)
14       *s++ = c;
15     return 1;
16   }
17   else if (c < (1 << 11))
18   {
19     if (s)
20     {
21       *s++ = 0xc0 | (c >> 6);
22       *s++ = 0x80 | (c & 0x3f);
23     }
24     return 2;
25   }
26   else if (c < (1 << 16))
27   {
28     if (s)
29     {
30       *s++ = 0xe0 | (c >> 12);
31       *s++ = 0x80 | ((c >> 6) & 0x3f);
32       *s++ = 0x80 | (c & 0x3f);
33     }
34     return 3;
35   }
36   else if (c < (1 << 21))
37   {
38     if (s)
39     {
40       *s++ = 0xf0 | (c >> 18);
41       *s++ = 0x80 | ((c >> 12) & 0x3f);
42       *s++ = 0x80 | ((c >> 6) & 0x3f);
43       *s++ = 0x80 | (c & 0x3f);
44     }
45     return 4;
46   }
47   else if (c < (1 << 26))
48   {
49     if (s)
50     {
51       *s++ = 0xf8 | (c >> 24);
52       *s++ = 0x80 | ((c >> 18) & 0x3f);
53       *s++ = 0x80 | ((c >> 12) & 0x3f);
54       *s++ = 0x80 | ((c >> 6) & 0x3f);
55       *s++ = 0x80 | (c & 0x3f);
56     }
57     return 5;
58   }
59   else if (c < (1 << 31))
60   {
61     if (s)
62     {
63       *s++ = 0xfc | (c >> 30);
64       *s++ = 0x80 | ((c >> 24) & 0x3f);
65       *s++ = 0x80 | ((c >> 18) & 0x3f);
66       *s++ = 0x80 | ((c >> 12) & 0x3f);
67       *s++ = 0x80 | ((c >> 6) & 0x3f);
68       *s++ = 0x80 | (c & 0x3f);
69     }
70     return 6;
71   }
72   errno = EILSEQ;
73   return -1;
74 }
75
76 #endif /* !HAVE_WC_FUNCS */