less warnings
[apps/madmutt.git] / utf8.c
1 /*
2  * Copyright notice from original mutt:
3  * [none]
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #ifndef HAVE_WC_FUNCS
15
16 #include <errno.h>
17
18 #ifndef EILSEQ
19 #define EILSEQ EINVAL
20 #endif
21
22 int mutt_wctoutf8 (char *s, unsigned int c)
23 {
24   if (c < (1 << 7)) {
25     if (s)
26       *s++ = c;
27     return 1;
28   }
29   else if (c < (1 << 11)) {
30     if (s) {
31       *s++ = 0xc0 | (c >> 6);
32       *s++ = 0x80 | (c & 0x3f);
33     }
34     return 2;
35   }
36   else if (c < (1 << 16)) {
37     if (s) {
38       *s++ = 0xe0 | (c >> 12);
39       *s++ = 0x80 | ((c >> 6) & 0x3f);
40       *s++ = 0x80 | (c & 0x3f);
41     }
42     return 3;
43   }
44   else if (c < (1 << 21)) {
45     if (s) {
46       *s++ = 0xf0 | (c >> 18);
47       *s++ = 0x80 | ((c >> 12) & 0x3f);
48       *s++ = 0x80 | ((c >> 6) & 0x3f);
49       *s++ = 0x80 | (c & 0x3f);
50     }
51     return 4;
52   }
53   else if (c < (1 << 26)) {
54     if (s) {
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     if (s) {
65       *s++ = 0xfc | (c >> 30);
66       *s++ = 0x80 | ((c >> 24) & 0x3f);
67       *s++ = 0x80 | ((c >> 18) & 0x3f);
68       *s++ = 0x80 | ((c >> 12) & 0x3f);
69       *s++ = 0x80 | ((c >> 6) & 0x3f);
70       *s++ = 0x80 | (c & 0x3f);
71     }
72     return 6;
73   }
74   errno = EILSEQ;
75   return -1;
76 }
77
78 #endif /* !HAVE_WC_FUNCS */