7 bool read_full(int fd, char *buffer, size_t size)
9 size_t n_read_total = 0;
11 while (n_read_total < size) {
12 if ((n_read = read(fd, buffer + n_read_total, size - n_read_total)) == -1) {
16 n_read_total += n_read;
24 #define READVEC(type, n) \
26 for (int i = 0; i < n; i++) { \
27 if (! read_ ## type(fd, &buf[i])) \
31 #define WRITEVEC(type, n) \
32 for (int i = 0; i < n; i++) { \
33 if (! write_ ## type(fd, vec[i])) \
38 #define DEFVEC(type) \
39 bool read_v2 ## type(int fd, v2 ## type *ptr) \
46 bool write_v2 ## type(int fd, v2 ## type val) \
48 type vec[2] = {val.x, val.y}; \
51 bool v2 ## type ## _equals(v2 ## type a, v2 ## type b) \
53 return a.x == b.x && a.y == b.y; \
55 v2 ## type v2 ## type ## _add(v2 ## type a, v2 ## type b) \
57 return (v2 ## type) {a.x + b.x, a.y + b.y}; \
59 bool read_v3 ## type(int fd, v3 ## type *ptr) \
67 bool write_v3 ## type(int fd, v3 ## type val) \
69 type vec[3] = {val.x, val.y, val.z}; \
72 bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
74 return a.x == b.x && a.y == b.y && a.z == b.z; \
76 v3 ## type v3 ## type ## _add(v3 ## type a, v3 ## type b) \
78 return (v3 ## type) {a.x + b.x, a.y + b.y, a.z + b.z}; \
80 bool read_v4 ## type(int fd, v4 ## type *ptr) \
89 bool write_v4 ## type(int fd, v4 ## type val) \
91 type vec[4] = {val.x, val.y, val.z, val.w}; \
94 bool v4 ## type ## _equals(v4 ## type a, v4 ## type b) \
96 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; \
98 v4 ## type v4 ## type ## _add(v4 ## type a, v4 ## type b) \
100 return (v4 ## type) {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; \
103 #define DEFTYP(type, bits) \
104 bool read_ ## type(int fd, type *buf) \
107 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
109 *buf = be ## bits ## toh(encoded); \
112 bool write_ ## type(int fd, type val) \
114 u ## bits encoded = htobe ## bits(val); \
115 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
123 #define DEFTYPES(bits) \
124 DEFTYP(s ## bits, bits) \
125 DEFTYP(u ## bits, bits)
132 #define DEFFLOAT(type) \
133 bool read_ ## type(int fd, type *buf) \
135 if (! read_full(fd, (char *) buf, sizeof(type))) \
139 bool write_ ## type(int fd, type val) \
141 if (write(fd, &val, sizeof(val)) == -1) { \