neatroff.ms: mention -mgnu
[neatroff_make.git] / shape / util.c
blob48d5f64742f9774e000f51e975b0c32ad47b4c12
1 #include <unistd.h>
2 #include "util.h"
4 int xread(int fd, void *buf, int len)
6 int n = 0;
7 int r;
8 while ((r = read(fd, buf + n, len - n)) > 0)
9 n += r;
10 return n;
13 int xwrite(int fd, void *buf, int len)
15 int n = 0;
16 int w;
17 while ((w = write(fd, buf + n, len - n)) > 0)
18 n += w;
19 return n;
22 static int readutf8(char **src)
24 int result;
25 int l = 1;
26 char *s = *src;
27 while (l < 6 && (unsigned char) *s & (0x40 >> l))
28 l++;
29 result = (0x3f >> l) & (unsigned char) *s++;
30 while (l--)
31 result = (result << 6) | ((unsigned char) *s++ & 0x3f);
32 *src = s;
33 return result;
36 void utf8_dec(int *dst, char *src)
38 char *s = src;
39 int *d = dst;
40 while (*s) {
41 if (!(~(unsigned char) *s & 0xc0))
42 *d++ = readutf8(&s);
43 else
44 *d++ = *s++;
46 *d = '\0';
49 static void writeutf8(char **dst, int c)
51 char *d = *dst;
52 int l = 0;
53 if (c > 0xffff) {
54 *d++ = 0xf0 | (c >> 18);
55 l = 3;
56 } else if (c > 0x7ff) {
57 *d++ = 0xe0 | (c >> 12);
58 l = 2;
59 } else if (c > 0x7f) {
60 *d++ = 0xc0 | (c >> 6);
61 l = 1;
63 while (l--)
64 *d++ = 0x80 | (c >> (l * 6)) & 0x3f;
65 *dst = d;
68 void utf8_enc(char *dst, int *src)
70 int *s = src;
71 char *d = dst;
72 while (*s) {
73 if (*s & ~0x7f)
74 writeutf8(&d, *s++);
75 else
76 *d++ = *s++;
78 *d = '\0';