Andreas Krennmair:
[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     if (s)
17       *s++ = c;
18     return 1;
19   }
20   else if (c < (1 << 11)) {
21     if (s) {
22       *s++ = 0xc0 | (c >> 6);
23       *s++ = 0x80 | (c & 0x3f);
24     }
25     return 2;
26   }
27   else if (c < (1 << 16)) {
28     if (s) {
29       *s++ = 0xe0 | (c >> 12);
30       *s++ = 0x80 | ((c >> 6) & 0x3f);
31       *s++ = 0x80 | (c & 0x3f);
32     }
33     return 3;
34   }
35   else if (c < (1 << 21)) {
36     if (s) {
37       *s++ = 0xf0 | (c >> 18);
38       *s++ = 0x80 | ((c >> 12) & 0x3f);
39       *s++ = 0x80 | ((c >> 6) & 0x3f);
40       *s++ = 0x80 | (c & 0x3f);
41     }
42     return 4;
43   }
44   else if (c < (1 << 26)) {
45     if (s) {
46       *s++ = 0xf8 | (c >> 24);
47       *s++ = 0x80 | ((c >> 18) & 0x3f);
48       *s++ = 0x80 | ((c >> 12) & 0x3f);
49       *s++ = 0x80 | ((c >> 6) & 0x3f);
50       *s++ = 0x80 | (c & 0x3f);
51     }
52     return 5;
53   }
54   else if (c < (1 << 31)) {
55     if (s) {
56       *s++ = 0xfc | (c >> 30);
57       *s++ = 0x80 | ((c >> 24) & 0x3f);
58       *s++ = 0x80 | ((c >> 18) & 0x3f);
59       *s++ = 0x80 | ((c >> 12) & 0x3f);
60       *s++ = 0x80 | ((c >> 6) & 0x3f);
61       *s++ = 0x80 | (c & 0x3f);
62     }
63     return 6;
64   }
65   errno = EILSEQ;
66   return -1;
67 }
68
69 #endif /* !HAVE_WC_FUNCS */