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